Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for unpublished post statuses. #1156

Merged
merged 9 commits into from
Nov 22, 2023
24 changes: 22 additions & 2 deletions includes/classes/DistributorPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,9 @@ protected function post_data() {
'excerpt' => $this->post->post_excerpt,
'parent' => ! empty( $this->post->post_parent ) ? (int) $this->post->post_parent : 0,
'status' => $this->post->post_status,
'date' => $this->post->post_date,
'date_gmt' => $this->post->post_date_gmt,

'distributor_media' => $this->get_media(),
'distributor_terms' => $this->get_terms(),
'distributor_meta' => $this->get_meta(),
Expand Down Expand Up @@ -857,11 +860,19 @@ protected function to_insert( $args = array() ) {
if ( ! empty( $args['remote_post_id'] ) ) {
// Updating an existing post.
$insert['ID'] = (int) $args['remote_post_id'];
// Never update the post status when updating a post.
unset( $insert['post_status'] );
} elseif ( ! empty( $args['post_status'] ) ) {
$insert['post_status'] = $args['post_status'];
}

if ( ! empty( $args['post_status'] ) ) {
$insert['post_status'] = $args['post_status'];
if (
isset( $insert['post_status'] )
&& 'future' === $insert['post_status']
) {
// Set the post date to the future date.
$insert['post_date'] = $post_data['date'];
$insert['post_date_gmt'] = $post_data['date_gmt'];
}

// Post meta used by wp_insert_post, wp_update_post.
Expand Down Expand Up @@ -921,6 +932,15 @@ protected function to_pull_list( $args = array() ) {
protected function to_rest( $rest_args = array() ) {
$post_data = $this->post_data();

/*
* Unset dates.
*
* External connections do not allow for the pulling or pushing of
* scheduled posts so these can be ignored.
*/
unset( $post_data['date'] );
unset( $post_data['date_gmt'] );

if ( ! empty( $post_data['parent'] ) ) {
$post_data['distributor_original_post_parent'] = (int) $post_data['parent'];
}
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"cypress-mochawesome-reporter": "^3.5.1",
"eslint-plugin-cypress": "^2.12.1",
"jsdoc": "^3.6.11",
"mochawesome-json-to-md": "^0.7.2",
"node-wp-i18n": "^1.2.6",
"wp-hookdoc": "^0.2.0"
},
Expand Down
185 changes: 178 additions & 7 deletions tests/php/DistributorPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,173 @@ public function test_get_media_with_attachments() {
$this->assertEquals( $post_media_expected, $post_media_actual );
}

/**
* Test methods for formatting the post data without blocks.
*
* @covers ::post_data()
* @covers ::to_insert()
* @covers ::to_json()
* @runInSeparateProcess
*/
public function test_scheduled_post_data_without_blocks() {
$this->setup_post_mock(
array(
'post_status' => 'future',
'post_date' => '2120-01-01 00:00:00',
'post_date_gmt' => '2120-01-01 00:00:00',
'post_modified' => '2120-01-01 00:00:00',
'post_modified_gmt' => '2120-01-01 00:00:00',
)
);
$this->setup_post_meta_mock(
array (
'dt_original_post_id' => array( '10' ),
'dt_original_blog_id' => array( '2' ),
'dt_syndicate_time' => array ( '1670383190' ),
'dt_original_post_url' => array ( 'http://origin.example.org/?p=10' ),
)
);

\WP_Mock::userFunction(
'get_the_title',
array(
'return' => 'Test Post',
)
);
\WP_Mock::userFunction(
'get_bloginfo',
array(
'return' => function( $info ) {
switch ( $info ) {
case 'charset':
return 'UTF-8';
case 'name':
return 'Test Internal Origin';
default:
return '';
}
},
)
);
\WP_Mock::userFunction(
'get_permalink',
array(
'return' => 'http://example.org/?p=1',
)
);

// Get Media: mock empty set as method is tested above.
\WP_Mock::userFunction(
'has_blocks',
array(
'return' => false,
)
);
\WP_Mock::userFunction(
'get_attached_media',
array(
'return' => array(),
)
);
\WP_Mock::userFunction(
'get_post_thumbnail_id',
array(
'return' => false,
)
);

// Get Terms: mock empty set as method is tested above.
\WP_Mock::userFunction(
'get_taxonomies',
array(
'return' => array( 'category', 'post_tag' ),
)
);
\WP_Mock::userFunction(
'wp_get_object_terms',
array(
'return' => array(),
)
);

$dt_post = new DistributorPost( 1 );
$post_data_actual = $dt_post->post_data();

$post_data_expected = array(
'title' => 'Test Post',
'slug' => 'test-post',
'type' => 'post',
'content' => 'Test Content',
'excerpt' => 'Test Excerpt',
'parent' => 0,
'status' => 'future',
'date' => '2120-01-01 00:00:00',
'date_gmt' => '2120-01-01 00:00:00',
'distributor_media' => array(),
'distributor_terms' => array(
'category' => array(),
'post_tag' => array(),
),
'distributor_meta' => array(),
'distributor_original_site_name' => 'Test Internal Origin',
'distributor_original_site_url' => 'http://test.com',
'distributor_original_post_url' => 'http://example.org/?p=1',
'distributor_original_post_id' => 1,
);

$this->assertSame( $post_data_expected, $post_data_actual, 'Post data is not in an expected form' );

// Make sure it looks good to insert.
$to_insert_actual = $dt_post->to_insert();
$to_insert_expected = array(
'post_title' => 'Test Post',
'post_name' => 'test-post',
'post_type' => 'post',
'post_content' => 'Test Content',
'post_excerpt' => 'Test Excerpt',
'post_status' => 'future',
'terms' => array(
'category' => array(),
'post_tag' => array(),
),
'meta' => array(),
'media' => array(),
'post_author' => 1,
'post_date' => '2120-01-01 00:00:00',
'post_date_gmt' => '2120-01-01 00:00:00',
'meta_input' => array(
'dt_original_post_id' => 1,
'dt_original_post_url' => 'http://example.org/?p=1',
),
);

$this->assertSame( $to_insert_expected, $to_insert_actual, 'Insert post data is not in an expected form' );

// Make sure it looks correct for a REST request.
$to_rest_actual = $dt_post->to_rest();
$to_rest_expected = array(
'title' => 'Test Post',
'slug' => 'test-post',
'type' => 'post',
'content' => 'Test Content',
'excerpt' => 'Test Excerpt',
'status' => 'future',
'distributor_media' => array(),
'distributor_terms' => array(
'category' => array(),
'post_tag' => array(),
),
'distributor_meta' => array(),
'distributor_original_site_name' => 'Test Internal Origin',
'distributor_original_site_url' => 'http://test.com',
'distributor_original_post_url' => 'http://example.org/?p=1',
'distributor_remote_post_id' => 1,

);

$this->assertSame( $to_rest_expected, $to_rest_actual, 'REST API data is not in an expected form' );
}

/**
* Test methods for formatting the post data without blocks.
*
Expand Down Expand Up @@ -1468,6 +1635,8 @@ public function test_post_data_without_blocks() {
'excerpt' => 'Test Excerpt',
'parent' => 0,
'status' => 'publish',
'date' => '2020-01-01 00:00:00',
'date_gmt' => '2020-01-01 00:00:00',
'distributor_media' => array(),
'distributor_terms' => array(
'category' => array(),
Expand All @@ -1480,7 +1649,7 @@ public function test_post_data_without_blocks() {
'distributor_original_post_id' => 1,
);

$this->assertSame( $post_data_expected, $post_data_actual );
$this->assertSame( $post_data_expected, $post_data_actual, 'Post data is not in an expected form' );

// Make sure it looks good to insert.
$to_insert_actual = $dt_post->to_insert();
Expand All @@ -1504,7 +1673,7 @@ public function test_post_data_without_blocks() {
),
);

$this->assertSame( $to_insert_expected, $to_insert_actual );
$this->assertSame( $to_insert_expected, $to_insert_actual, 'Insert post data is not in an expected form' );

// Make sure it looks correct for a REST request.
$to_rest_actual = $dt_post->to_rest();
Expand All @@ -1528,7 +1697,7 @@ public function test_post_data_without_blocks() {

);

$this->assertSame( $to_rest_expected, $to_rest_actual );
$this->assertSame( $to_rest_expected, $to_rest_actual, 'REST API data is not in an expected form' );
}

/**
Expand Down Expand Up @@ -1644,8 +1813,10 @@ public function test_post_data_with_blocks() {
'type' => 'post',
'content' => '<!-- wp:paragraph --><p>Test Content</p><!-- /wp:paragraph -->',
'excerpt' => 'Test Excerpt',
'parent' => 0,
'parent' => 0,
'status' => 'publish',
'date' => '2020-01-01 00:00:00',
'date_gmt' => '2020-01-01 00:00:00',
'distributor_media' => array(),
'distributor_terms' => array(
'category' => array(),
Expand All @@ -1658,7 +1829,7 @@ public function test_post_data_with_blocks() {
'distributor_original_post_id' => 1,
);

$this->assertSame( $post_data_expected, $post_data_actual );
$this->assertSame( $post_data_expected, $post_data_actual, 'Post data is not in an expected form' );

// Make sure it looks good to insert.
$to_insert_actual = $dt_post->to_insert();
Expand All @@ -1682,7 +1853,7 @@ public function test_post_data_with_blocks() {
),
);

$this->assertSame( $to_insert_expected, $to_insert_actual );
$this->assertSame( $to_insert_expected, $to_insert_actual, 'Insert post data is not in an expected form' );

// Make sure it looks correct for a REST request.
$to_rest_actual = $dt_post->to_rest();
Expand All @@ -1706,7 +1877,7 @@ public function test_post_data_with_blocks() {
'distributor_raw_content' => '<!-- wp:paragraph --><p>Test Content</p><!-- /wp:paragraph -->',
);

$this->assertSame( $to_rest_expected, $to_rest_actual );
$this->assertSame( $to_rest_expected, $to_rest_actual, 'REST API data is not in an expected form' );
}

/**
Expand Down
16 changes: 11 additions & 5 deletions tests/php/NetworkSiteConnectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ public function test_push() {
'get_post', [
'return' => (object) [
'ID' => 111,
'post_content' => '',
'post_excerpt' => '',
'post_type' => '',
'post_name' => '',
'post_status' => 'publish',
'post_content' => '',
'post_excerpt' => '',
'post_type' => '',
'post_name' => '',
'post_status' => 'publish',
'post_date' => '2020-01-01 00:00:00',
'post_date_gmt' => '2020-01-01 00:00:00',
],
]
);
Expand Down Expand Up @@ -277,6 +279,8 @@ public function test_pull() {
'post_status' => 'publish',
'post_content' => 'My post content',
'post_excerpt' => 'My post excerpt',
'post_date' => '2020-01-01 00:00:00',
'post_date_gmt' => '2020-01-01 00:00:00',
'meta' => [],
],
]
Expand Down Expand Up @@ -429,6 +433,8 @@ public function test_remote_get() {
'post_status' => 'publish',
'post_content' => 'My post content',
'post_excerpt' => 'My post excerpt',
'post_date' => '2020-01-01 00:00:00',
'post_date_gmt' => '2020-01-01 00:00:00',
],
]
);
Expand Down
Loading
Loading