diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 6f2641e9f9..1c462d0a36 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -30,604 +30,6 @@ class Tests_Post extends WP_UnitTestCase { remove_role( 'grammarian' ); } - public function set_up() { - parent::set_up(); - - wp_set_current_user( self::$editor_id ); - _set_cron_array( array() ); - } - - /** - * Helper function: return the timestamp(s) of cron jobs for the specified hook and post. - */ - private function next_schedule_for_post( $hook, $post_id ) { - return wp_next_scheduled( 'publish_future_post', array( 0 => (int) $post_id ) ); - } - - /** - * Helper function, unsets current user globally. - */ - private function unset_current_user() { - global $current_user, $user_ID; - - $current_user = null; - $user_ID = null; - } - - /** - * Test simple valid behavior: insert and get a post. - * - * @dataProvider data_vb_insert_get_delete - */ - public function test_vb_insert_get_delete( $post_type ) { - register_post_type( - 'cpt', - array( - 'taxonomies' => array( 'post_tag', 'ctax' ), - ) - ); - register_taxonomy( 'ctax', 'cpt' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => "{$post_type}_content", - 'post_title' => "{$post_type}_title", - 'tax_input' => array( - 'post_tag' => 'tag1,tag2', - 'ctax' => 'cterm1,cterm2', - ), - 'post_type' => $post_type, - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - $this->assertIsInt( $post_id ); - $this->assertGreaterThan( 0, $post_id ); - - // Fetch the post and make sure it matches. - $post = get_post( $post_id ); - - $this->assertSame( $data['post_content'], $post->post_content ); - $this->assertSame( $data['post_title'], $post->post_title ); - $this->assertSame( $data['post_status'], $post->post_status ); - $this->assertEquals( $data['post_author'], $post->post_author ); - - // Test cache state. - $post_cache = wp_cache_get( $post_id, 'posts' ); - $this->assertInstanceOf( 'stdClass', $post_cache ); - $this->assertSame( $post_id, $post_cache->ID ); - - update_object_term_cache( $post_id, $post_type ); - $term_cache = wp_cache_get( $post_id, 'post_tag_relationships' ); - $this->assertIsArray( $term_cache ); - $this->assertCount( 2, $term_cache ); - - $term_cache = wp_cache_get( $post_id, 'ctax_relationships' ); - if ( 'cpt' === $post_type ) { - $this->assertIsArray( $term_cache ); - $this->assertCount( 2, $term_cache ); - } else { - $this->assertFalse( $term_cache ); - } - - wp_delete_post( $post_id, true ); - - $this->assertFalse( wp_cache_get( $post_id, 'posts' ) ); - $this->assertFalse( wp_cache_get( $post_id, 'post_tag_relationships' ) ); - $this->assertFalse( wp_cache_get( $post_id, 'ctax_relationships' ) ); - - $GLOBALS['wp_taxonomies']['post_tag']->object_type = array( 'post' ); - } - - public function data_vb_insert_get_delete() { - $post_types = array( 'post', 'cpt' ); - - return $this->text_array_to_dataprovider( $post_types ); - } - - /** - * Insert a post with a future date, and make sure the status and cron schedule are correct. - */ - public function test_vb_insert_future() { - $future_date = strtotime( '+1 day' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - $this->assertIsInt( $post_id ); - $this->assertGreaterThan( 0, $post_id ); - - // Fetch the post and make sure it matches. - $post = get_post( $post_id ); - - $this->assertSame( $data['post_content'], $post->post_content ); - $this->assertSame( $data['post_title'], $post->post_title ); - $this->assertSame( 'future', $post->post_status ); - $this->assertEquals( $data['post_author'], $post->post_author ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // There should be a publish_future_post hook scheduled on the future date. - $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - } - - /** - * Insert a post with a future date, and make sure the status and cron schedule are correct. - */ - public function test_vb_insert_future_over_dst() { - // Some magic days - one DST one not. - $future_date_1 = strtotime( 'June 21st +1 year' ); - $future_date_2 = strtotime( 'Jan 11th +1 year' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - - // Fetch the post and make sure has the correct date and status. - $post = get_post( $post_id ); - $this->assertSame( 'future', $post->post_status ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // Check that there's a publish_future_post job scheduled at the right time. - $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - - // Now save it again with a date further in the future. - $data['ID'] = $post_id; - $data['post_date'] = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' ); - $data['post_date_gmt'] = null; - wp_update_post( $data ); - - // Fetch the post again and make sure it has the new post_date. - $post = get_post( $post_id ); - $this->assertSame( 'future', $post->post_status ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // And the correct date on the cron job. - $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - } - - /** - * Future post bug: posts get published at the wrong time if you edit the timestamp. - * - * @ticket 4710 - */ - public function test_vb_insert_future_edit_bug() { - $future_date_1 = strtotime( '+1 day' ); - $future_date_2 = strtotime( '+2 day' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - - // Fetch the post and make sure has the correct date and status. - $post = get_post( $post_id ); - $this->assertSame( 'future', $post->post_status ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // Check that there's a publish_future_post job scheduled at the right time. - $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - - // Now save it again with a date further in the future. - $data['ID'] = $post_id; - $data['post_date'] = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' ); - $data['post_date_gmt'] = null; - wp_update_post( $data ); - - // Fetch the post again and make sure it has the new post_date. - $post = get_post( $post_id ); - $this->assertSame( 'future', $post->post_status ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // And the correct date on the cron job. - $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - } - - /** - * Insert a draft post with a future date, and make sure no cron schedule is set. - */ - public function test_vb_insert_future_draft() { - $future_date = strtotime( '+1 day' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'draft', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - $this->assertIsInt( $post_id ); - $this->assertGreaterThan( 0, $post_id ); - - // Fetch the post and make sure it matches. - $post = get_post( $post_id ); - - $this->assertSame( $data['post_content'], $post->post_content ); - $this->assertSame( $data['post_title'], $post->post_title ); - $this->assertSame( 'draft', $post->post_status ); - $this->assertEquals( $data['post_author'], $post->post_author ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // There should be a publish_future_post hook scheduled on the future date. - $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - - } - - /** - * Insert a future post, then edit and change it to draft, and make sure cron gets it right. - */ - public function test_vb_insert_future_change_to_draft() { - $future_date_1 = strtotime( '+1 day' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - - // Fetch the post and make sure has the correct date and status. - $post = get_post( $post_id ); - $this->assertSame( 'future', $post->post_status ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // Check that there's a publish_future_post job scheduled at the right time. - $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - - // Now save it again with status set to draft. - $data['ID'] = $post_id; - $data['post_status'] = 'draft'; - wp_update_post( $data ); - - // Fetch the post again and make sure it has the new post_date. - $post = get_post( $post_id ); - $this->assertSame( 'draft', $post->post_status ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // And the correct date on the cron job. - $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - } - - /** - * Insert a future post, then edit and change the status, and make sure cron gets it right. - * - * @dataProvider data_vb_insert_future_change_status - */ - public function test_vb_insert_future_change_status( $status ) { - $future_date_1 = strtotime( '+1 day' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => "{$status}_content", - 'post_title' => "{$status}_title", - 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - - // Fetch the post and make sure has the correct date and status. - $post = get_post( $post_id ); - $this->assertSame( 'future', $post->post_status ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // Check that there's a publish_future_post job scheduled at the right time. - $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - - // Now save it again with status changed. - $data['ID'] = $post_id; - $data['post_status'] = $status; - wp_update_post( $data ); - - // Fetch the post again and make sure it has the new post_date. - $post = get_post( $post_id ); - $this->assertSame( $status, $post->post_status ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // And the correct date on the cron job. - $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - } - - public function data_vb_insert_future_change_status() { - $statuses = array( - 'draft', - 'static', - 'object', - 'attachment', - 'inherit', - 'pending', - ); - - return $this->text_array_to_dataprovider( $statuses ); - } - - /** - * Insert a draft post with a future date, and make sure no cron schedule is set. - */ - public function test_vb_insert_future_private() { - $future_date = strtotime( '+1 day' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'private', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - $this->assertIsInt( $post_id ); - $this->assertGreaterThan( 0, $post_id ); - - // Fetch the post and make sure it matches. - $post = get_post( $post_id ); - - $this->assertSame( $data['post_content'], $post->post_content ); - $this->assertSame( $data['post_title'], $post->post_title ); - $this->assertSame( 'private', $post->post_status ); - $this->assertEquals( $data['post_author'], $post->post_author ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // There should be a publish_future_post hook scheduled on the future date. - $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - } - - /** - * Insert a post with an invalid date, make sure it fails. - * - * @ticket 17180 - */ - public function test_vb_insert_invalid_date() { - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_date' => '2012-02-30 00:00:00', - ); - - // Test both return paths with or without WP_Error. - $post_id = wp_insert_post( $data, true ); - $this->assertWPError( $post_id ); - $this->assertSame( 'invalid_date', $post_id->get_error_code() ); - - $post_id = wp_insert_post( $data ); - $this->assertSame( 0, $post_id ); - } - - /** - * Insert a future post, then edit and change it to private, and make sure cron gets it right. - */ - public function test_vb_insert_future_change_to_private() { - $future_date_1 = strtotime( '+1 day' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - - // Fetch the post and make sure has the correct date and status. - $post = get_post( $post_id ); - $this->assertSame( 'future', $post->post_status ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // Check that there's a publish_future_post job scheduled at the right time. - $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - - // Now save it again with status set to draft. - $data['ID'] = $post_id; - $data['post_status'] = 'private'; - wp_update_post( $data ); - - // Fetch the post again and make sure it has the new post_date. - $post = get_post( $post_id ); - $this->assertSame( 'private', $post->post_status ); - $this->assertSame( $data['post_date'], $post->post_date ); - - // And the correct date on the cron job. - $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - } - - /** - * @ticket 5305 - */ - public function test_wp_insert_post_should_not_allow_a_bare_numeric_slug_that_might_conflict_with_a_date_archive_when_generating_from_an_empty_post_title() { - $this->set_permalink_structure( '/%postname%/' ); - - $post_id = wp_insert_post( - array( - 'post_title' => '', - 'post_content' => 'test', - 'post_status' => 'publish', - 'post_type' => 'post', - ) - ); - - $post = get_post( $post_id ); - - $this->assertSame( "$post_id-2", $post->post_name ); - } - - /** - * @ticket 5305 - * @ticket 33392 - */ - public function test_wp_insert_post_should_invalidate_post_cache_before_generating_guid_when_post_name_is_empty_and_is_generated_from_the_post_ID() { - register_post_type( 'wptests_pt' ); - - $post_id = wp_insert_post( - array( - 'post_title' => '', - 'post_type' => 'wptests_pt', - 'post_status' => 'publish', - ) - ); - - $post = get_post( $post_id ); - - $this->assertStringContainsString( 'wptests_pt=' . $post_id, $post->guid ); - } - - /** - * @ticket 55877 - * @covers ::wp_insert_post - */ - public function test_wp_insert_post_should_not_trigger_warning_for_pending_posts_with_unknown_cpt() { - $post_id = wp_insert_post( - array( - 'post_title' => 'title', - 'post_type' => 'unknown', - 'post_status' => 'pending', - ) - ); - - $this->assertIsInt( $post_id ); - $this->assertGreaterThan( 0, $post_id ); - } - - /** - * @ticket 20451 - */ - public function test_wp_insert_post_with_meta_input() { - $post_id = wp_insert_post( - array( - 'post_title' => '', - 'post_content' => 'test', - 'post_status' => 'publish', - 'post_type' => 'post', - 'meta_input' => array( - 'hello' => 'world', - 'foo' => 'bar', - ), - ) - ); - - $this->assertSame( 'world', get_post_meta( $post_id, 'hello', true ) ); - $this->assertSame( 'bar', get_post_meta( $post_id, 'foo', true ) ); - } - - /** - * "When I delete a future post using wp_delete_post( $post->ID ) it does not update the cron correctly." - * - * @ticket 5364 - */ - public function test_delete_future_post_cron() { - $future_date = strtotime( '+1 day' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - - // Check that there's a publish_future_post job scheduled at the right time. - $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - - // Now delete the post and make sure the cron entry is removed. - wp_delete_post( $post_id ); - - $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); - } - - /** - * Bug: permalink doesn't work if post title is empty. - * - * Might only fail if the post ID is greater than four characters. - * - * @ticket 5305 - */ - public function test_permalink_without_title() { - $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => '', - 'post_date' => '2007-10-31 06:15:00', - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - - // Permalink should include the post ID at the end. - $expected = get_option( 'siteurl' ) . '/2007/10/31/' . $post_id . '/'; - $this->assertSame( $expected, get_permalink( $post_id ) ); - } - - /** - * @ticket 23708 - */ - public function test_get_post_ancestors_within_loop() { - global $post; - - $parent_id = self::factory()->post->create(); - $post = self::factory()->post->create_and_get( - array( - 'post_parent' => $parent_id, - ) - ); - - $this->assertSame( array( $parent_id ), get_post_ancestors( 0 ) ); - } - - /** - * @ticket 23474 - */ - public function test_update_invalid_post_id() { - $post_id = self::factory()->post->create(); - $post = get_post( $post_id, ARRAY_A ); - - $post['ID'] = 123456789; - - $this->assertSame( 0, wp_insert_post( $post ) ); - $this->assertSame( 0, wp_update_post( $post ) ); - - $this->assertInstanceOf( 'WP_Error', wp_insert_post( $post, true ) ); - $this->assertInstanceOf( 'WP_Error', wp_update_post( $post, true ) ); - - } - public function test_parse_post_content_single_page() { global $multipage, $pages, $numpages; @@ -736,34 +138,6 @@ class Tests_Post extends WP_UnitTestCase { $this->assertSame( array( 'Page 0' ), $pages ); } - /** - * @ticket 19373 - */ - public function test_insert_programmatic_sanitized() { - $this->unset_current_user(); - - register_taxonomy( 'test_tax', 'post' ); - - $title = 'title'; - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => $title, - 'tax_input' => array( - 'test_tax' => array( 'term', 'term2', 'term3' ), - ), - ); - - $post_id = wp_insert_post( $data, true, true ); - $this->assertIsInt( $post_id ); - $this->assertGreaterThan( 0, $post_id ); - - $post = get_post( $post_id ); - $this->assertEquals( self::$editor_id, $post->post_author ); - $this->assertSame( $title, $post->post_title ); - } - /** * @ticket 24803 */ @@ -878,6 +252,8 @@ class Tests_Post extends WP_UnitTestCase { $post = self::factory()->post->create( array( 'post_type' => $post_type ) ); wp_set_object_terms( $post, 'foo', $tax ); + wp_set_current_user( self::$editor_id ); + $wp_tag_cloud = wp_tag_cloud( array( 'post_type' => $post_type, @@ -935,91 +311,6 @@ class Tests_Post extends WP_UnitTestCase { } } - /** - * @ticket 31168 - */ - public function test_wp_insert_post_default_comment_ping_status_open() { - $post_id = self::factory()->post->create( - array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => 'title', - ) - ); - $post = get_post( $post_id ); - - $this->assertSame( 'open', $post->comment_status ); - $this->assertSame( 'open', $post->ping_status ); - } - - /** - * @ticket 31168 - */ - public function test_wp_insert_post_page_default_comment_ping_status_closed() { - $post_id = self::factory()->post->create( - array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_type' => 'page', - ) - ); - $post = get_post( $post_id ); - - $this->assertSame( 'closed', $post->comment_status ); - $this->assertSame( 'closed', $post->ping_status ); - } - - /** - * @ticket 31168 - */ - public function test_wp_insert_post_cpt_default_comment_ping_status_open() { - $post_type = rand_str( 20 ); - register_post_type( $post_type, array( 'supports' => array( 'comments', 'trackbacks' ) ) ); - - $post_id = self::factory()->post->create( - array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => $post_type, - ) - ); - $post = get_post( $post_id ); - - _unregister_post_type( $post_type ); - - $this->assertSame( 'open', $post->comment_status ); - $this->assertSame( 'open', $post->ping_status ); - } - - /** - * @ticket 31168 - */ - public function test_wp_insert_post_cpt_default_comment_ping_status_closed() { - $post_type = rand_str( 20 ); - register_post_type( $post_type ); - - $post_id = self::factory()->post->create( - array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => rand_str(), - 'post_title' => rand_str(), - 'post_type' => $post_type, - ) - ); - $post = get_post( $post_id ); - - _unregister_post_type( $post_type ); - - $this->assertSame( 'closed', $post->comment_status ); - $this->assertSame( 'closed', $post->ping_status ); - } - /** * If a post is sticky and is updated by a user that does not have the publish_post capability, * it should _stay_ sticky. @@ -1127,74 +418,6 @@ class Tests_Post extends WP_UnitTestCase { $this->assertSame( 1, $a2->get_call_count() ); } - /** - * If a post is updated without providing a post_name param, - * a new slug should not be generated. - * - * @ticket 34865 - */ - public function test_post_updates_without_slug_provided() { - $post_id = self::factory()->post->create( - array( - 'post_title' => 'Stuff', - 'post_status' => 'publish', - ) - ); - - $data = array( - 'ID' => $post_id, - 'post_title' => 'Stuff and Things', - ); - - wp_insert_post( $data ); - - $updated_post = get_post( $post_id ); - // Ensure changing the post_title didn't modify the post_name. - $this->assertSame( 'stuff', $updated_post->post_name ); - } - - /** - * @ticket 32585 - */ - public function test_wp_insert_post_author_zero() { - $post_id = self::factory()->post->create( array( 'post_author' => 0 ) ); - - $this->assertEquals( 0, get_post( $post_id )->post_author ); - } - - /** - * @ticket 32585 - */ - public function test_wp_insert_post_author_null() { - $post_id = self::factory()->post->create( array( 'post_author' => null ) ); - - $this->assertEquals( self::$editor_id, get_post( $post_id )->post_author ); - } - - /** - * @ticket 15946 - */ - public function test_wp_insert_post_should_respect_post_date_gmt() { - $data = array( - 'post_author' => self::$editor_id, - 'post_status' => 'publish', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_date_gmt' => '2014-01-01 12:00:00', - ); - - // Insert a post and make sure the ID is OK. - $post_id = wp_insert_post( $data ); - - $post = get_post( $post_id ); - - $this->assertSame( $data['post_content'], $post->post_content ); - $this->assertSame( $data['post_title'], $post->post_title ); - $this->assertEquals( $data['post_author'], $post->post_author ); - $this->assertSame( get_date_from_gmt( $data['post_date_gmt'] ), $post->post_date ); - $this->assertSame( $data['post_date_gmt'], $post->post_date_gmt ); - } - public function test_wp_delete_post_reassign_hierarchical_post_type() { $grandparent_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); $parent_page_id = self::factory()->post->create( @@ -1219,57 +442,6 @@ class Tests_Post extends WP_UnitTestCase { $this->assertSame( 0, get_post( $page_id )->post_parent ); } - /** - * Test ensuring that the post_name (UUID) is preserved when wp_insert_post()/wp_update_post() is called. - * - * @see _wp_customize_changeset_filter_insert_post_data() - * @ticket 30937 - */ - public function test_wp_insert_post_for_customize_changeset_should_not_drop_post_name() { - $this->assertSame( 10, has_filter( 'wp_insert_post_data', '_wp_customize_changeset_filter_insert_post_data' ) ); - - $changeset_data = array( - 'blogname' => array( - 'value' => 'Hello World', - ), - ); - - wp_set_current_user( $this->factory()->user->create( array( 'role' => 'contributor' ) ) ); - - $uuid = wp_generate_uuid4(); - $post_id = wp_insert_post( - array( - 'post_type' => 'customize_changeset', - 'post_name' => strtoupper( $uuid ), - 'post_content' => wp_json_encode( $changeset_data ), - ) - ); - $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected lower-case UUID4 to be inserted.' ); - $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); - - $changeset_data['blogname']['value'] = 'Hola Mundo'; - wp_update_post( - array( - 'ID' => $post_id, - 'post_status' => 'draft', - 'post_content' => wp_json_encode( $changeset_data ), - ) - ); - $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected post_name to not have been dropped for drafts.' ); - $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); - - $changeset_data['blogname']['value'] = 'Hallo Welt'; - wp_update_post( - array( - 'ID' => $post_id, - 'post_status' => 'pending', - 'post_content' => wp_json_encode( $changeset_data ), - ) - ); - $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected post_name to not have been dropped for pending.' ); - $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); - } - /** * Test ensuring that the post_slug can be filtered with a custom value short circuiting the built in * function that tries to create a unique name based on the post name. @@ -1297,324 +469,6 @@ class Tests_Post extends WP_UnitTestCase { return 'override-slug-' . $post_type; } - /** - * @ticket 48113 - */ - public function test_insert_post_should_respect_date_floating_post_status_arg() { - register_post_status( 'floating', array( 'date_floating' => true ) ); - - $post_id = self::factory()->post->create( - array( - 'post_status' => 'floating', - 'post_date' => null, - 'post_date_gmt' => null, - ) - ); - - $post = get_post( $post_id ); - self::assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); - } - - /** - * @ticket 48113 - */ - public function test_insert_post_should_respect_date_floating_post_status_arg_not_set() { - register_post_status( 'not-floating', array( 'date_floating' => false ) ); - - $post_id = self::factory()->post->create( - array( - 'post_status' => 'floating', - 'post_date' => null, - 'post_date_gmt' => null, - ) - ); - - $post = get_post( $post_id ); - self::assertEqualsWithDelta( - strtotime( gmdate( 'Y-m-d H:i:s' ) ), - strtotime( $post->post_date_gmt ), - 2, - 'The dates should be equal' - ); - } - - /** - * Test ensuring that wp_update_post() does not unintentionally modify post tags - * if the post has several tags with the same name but different slugs. - * - * Tags should only be modified if 'tags_input' parameter was explicitly provided, - * and is different from the existing tags. - * - * @ticket 45121 - */ - public function test_update_post_should_only_modify_post_tags_if_different_tags_input_was_provided() { - $tag_1 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_1' ) ); - $tag_2 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_2' ) ); - $tag_3 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_3' ) ); - - $post_id = self::factory()->post->create( - array( - 'tags_input' => array( $tag_1['term_id'], $tag_2['term_id'] ), - ) - ); - - $post = get_post( $post_id ); - - $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); - $this->assertSameSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags ); - - wp_update_post( $post ); - - $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); - $this->assertSameSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags ); - - wp_update_post( - array( - 'ID' => $post->ID, - 'tags_input' => array( $tag_2['term_id'], $tag_3['term_id'] ), - ) - ); - - $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); - $this->assertSameSets( array( $tag_2['term_id'], $tag_3['term_id'] ), $tags ); - } - - /** - * @ticket 52187 - */ - public function test_insert_empty_post_date() { - $post_date_gmt = '2020-12-29 10:11:45'; - $invalid_date = '2020-12-41 14:15:27'; - - // Empty post_date_gmt with floating status - $post_id = self::factory()->post->create( - array( - 'post_status' => 'draft', - ) - ); - $post = get_post( $post_id ); - $this->assertEqualsWithDelta( - strtotime( gmdate( 'Y-m-d H:i:s' ) ), - strtotime( $post->post_date ), - 2, - 'The dates should be equal' - ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); - - $post_id = self::factory()->post->create( - array( - 'post_date_gmt' => '0000-00-00 00:00:00', - 'post_status' => 'draft', - ) - ); - $post = get_post( $post_id ); - $this->assertEqualsWithDelta( - strtotime( gmdate( 'Y-m-d H:i:s' ) ), - strtotime( $post->post_date ), - 2, - 'The dates should be equal' - ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); - - // Empty post_date_gmt without floating status - $post_id = self::factory()->post->create( - array( - 'post_status' => 'publish', - ) - ); - $post = get_post( $post_id ); - $this->assertEqualsWithDelta( - strtotime( gmdate( 'Y-m-d H:i:s' ) ), - strtotime( $post->post_date ), - 2, - 'The dates should be equal' - ); - $this->assertEqualsWithDelta( - strtotime( gmdate( 'Y-m-d H:i:s' ) ), - strtotime( get_gmt_from_date( $post->post_date ) ), - 2, - 'The dates should be equal' - ); - - $post_id = self::factory()->post->create( - array( - 'post_date_gmt' => '0000-00-00 00:00:00', - 'post_status' => 'publish', - ) - ); - $post = get_post( $post_id ); - $this->assertEqualsWithDelta( - strtotime( gmdate( 'Y-m-d H:i:s' ) ), - strtotime( $post->post_date ), - 2, - 'The dates should be equal' - ); - $this->assertEqualsWithDelta( - strtotime( gmdate( 'Y-m-d H:i:s' ) ), - strtotime( get_gmt_from_date( $post->post_date ) ), - 2, - 'The dates should be equal' - ); - - // Valid post_date_gmt - $post_id = self::factory()->post->create( - array( - 'post_date_gmt' => $post_date_gmt, - ) - ); - $post = get_post( $post_id ); - $this->assertSame( get_date_from_gmt( $post_date_gmt ), $post->post_date ); - $this->assertSame( $post_date_gmt, $post->post_date_gmt ); - - // Invalid post_date_gmt - $post_id = self::factory()->post->create( - array( - 'post_date_gmt' => $invalid_date, - ) - ); - $post = get_post( $post_id ); - $this->assertSame( '1970-01-01 00:00:00', $post->post_date ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); - } - - /** - * @ticket 52187 - */ - public function test_insert_valid_post_date() { - $post_date = '2020-12-28 11:26:35'; - $post_date_gmt = '2020-12-29 10:11:45'; - $invalid_date = '2020-12-41 14:15:27'; - - // Empty post_date_gmt with floating status - $post_id = self::factory()->post->create( - array( - 'post_date' => $post_date, - 'post_status' => 'draft', - ) - ); - $post = get_post( $post_id ); - $this->assertSame( $post_date, $post->post_date ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); - - $post_id = self::factory()->post->create( - array( - 'post_date' => $post_date, - 'post_date_gmt' => '0000-00-00 00:00:00', - 'post_status' => 'draft', - ) - ); - $post = get_post( $post_id ); - $this->assertSame( $post_date, $post->post_date ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); - - // Empty post_date_gmt without floating status - $post_id = self::factory()->post->create( - array( - 'post_date' => $post_date, - 'post_status' => 'publish', - ) - ); - $post = get_post( $post_id ); - $this->assertSame( $post_date, $post->post_date ); - $this->assertSame( get_gmt_from_date( $post_date ), $post->post_date_gmt ); - - $post_id = self::factory()->post->create( - array( - 'post_date' => $post_date, - 'post_date_gmt' => '0000-00-00 00:00:00', - 'post_status' => 'publish', - ) - ); - $post = get_post( $post_id ); - $this->assertSame( $post_date, $post->post_date ); - $this->assertSame( get_gmt_from_date( $post_date ), $post->post_date_gmt ); - - // Valid post_date_gmt - $post_id = self::factory()->post->create( - array( - 'post_date' => $post_date, - 'post_date_gmt' => $post_date_gmt, - ) - ); - $post = get_post( $post_id ); - $this->assertSame( $post_date, $post->post_date ); - $this->assertSame( $post_date_gmt, $post->post_date_gmt ); - - // Invalid post_date_gmt - $post_id = self::factory()->post->create( - array( - 'post_date' => $post_date, - 'post_date_gmt' => $invalid_date, - ) - ); - $post = get_post( $post_id ); - $this->assertSame( $post_date, $post->post_date ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); - } - - /** - * @ticket 52187 - */ - public function test_insert_invalid_post_date() { - $post_date = '2020-12-28 11:26:35'; - $post_date_gmt = '2020-12-29 10:11:45'; - $invalid_date = '2020-12-41 14:15:27'; - - // Empty post_date_gmt with floating status - $post_id = self::factory()->post->create( - array( - 'post_date' => $invalid_date, - 'post_status' => 'draft', - ) - ); - $this->assertSame( 0, $post_id ); - - $post_id = self::factory()->post->create( - array( - 'post_date' => $invalid_date, - 'post_date_gmt' => '0000-00-00 00:00:00', - 'post_status' => 'draft', - ) - ); - $this->assertSame( 0, $post_id ); - - // Empty post_date_gmt without floating status - $post_id = self::factory()->post->create( - array( - 'post_date' => $invalid_date, - 'post_status' => 'publish', - ) - ); - $this->assertSame( 0, $post_id ); - - $post_id = self::factory()->post->create( - array( - 'post_date' => $invalid_date, - 'post_date_gmt' => '0000-00-00 00:00:00', - 'post_status' => 'publish', - ) - ); - $this->assertSame( 0, $post_id ); - - // Valid post_date_gmt - $post_id = self::factory()->post->create( - array( - 'post_date' => $invalid_date, - 'post_date_gmt' => $post_date_gmt, - ) - ); - $this->assertSame( 0, $post_id ); - - // Invalid post_date_gmt - $post_id = self::factory()->post->create( - array( - 'post_date' => $invalid_date, - 'post_date_gmt' => $invalid_date, - ) - ); - $this->assertSame( 0, $post_id ); - } - /** * @ticket 52187 */ diff --git a/tests/phpunit/tests/post/wpInsertPost.php b/tests/phpunit/tests/post/wpInsertPost.php index c1a60753da..e51190c7aa 100644 --- a/tests/phpunit/tests/post/wpInsertPost.php +++ b/tests/phpunit/tests/post/wpInsertPost.php @@ -2,11 +2,13 @@ /** * @group post + * @covers ::wp_insert_post */ class Tests_Post_wpInsertPost extends WP_UnitTestCase { protected static $user_ids = array( 'administrator' => null, + 'editor' => null, 'contributor' => null, ); @@ -17,6 +19,11 @@ class Tests_Post_wpInsertPost extends WP_UnitTestCase { 'role' => 'administrator', ) ), + 'editor' => $factory->user->create( + array( + 'role' => 'editor', + ) + ), 'contributor' => $factory->user->create( array( 'role' => 'contributor', @@ -65,6 +72,1154 @@ class Tests_Post_wpInsertPost extends WP_UnitTestCase { ); } + /** + * Helper function: return the timestamp(s) of cron jobs for the specified hook and post. + */ + private function next_schedule_for_post( $hook, $post_id ) { + return wp_next_scheduled( 'publish_future_post', array( 0 => (int) $post_id ) ); + } + + /** + * Helper function, unsets current user globally. + */ + private function unset_current_user() { + global $current_user, $user_ID; + + $current_user = null; + $user_ID = null; + } + + /** + * Test simple valid behavior: insert and get a post. + * + * @dataProvider data_vb_insert_get_delete + */ + public function test_vb_insert_get_delete( $post_type ) { + register_post_type( + 'cpt', + array( + 'taxonomies' => array( 'post_tag', 'ctax' ), + ) + ); + register_taxonomy( 'ctax', 'cpt' ); + + wp_set_current_user( self::$user_ids['editor'] ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => "{$post_type}_content", + 'post_title' => "{$post_type}_title", + 'tax_input' => array( + 'post_tag' => 'tag1,tag2', + 'ctax' => 'cterm1,cterm2', + ), + 'post_type' => $post_type, + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + $this->assertIsInt( $post_id ); + $this->assertGreaterThan( 0, $post_id ); + + // Fetch the post and make sure it matches. + $post = get_post( $post_id ); + + $this->assertSame( $data['post_content'], $post->post_content ); + $this->assertSame( $data['post_title'], $post->post_title ); + $this->assertSame( $data['post_status'], $post->post_status ); + $this->assertEquals( $data['post_author'], $post->post_author ); + + // Test cache state. + $post_cache = wp_cache_get( $post_id, 'posts' ); + $this->assertInstanceOf( 'stdClass', $post_cache ); + $this->assertSame( $post_id, $post_cache->ID ); + + update_object_term_cache( $post_id, $post_type ); + $term_cache = wp_cache_get( $post_id, 'post_tag_relationships' ); + $this->assertIsArray( $term_cache ); + $this->assertCount( 2, $term_cache ); + + $term_cache = wp_cache_get( $post_id, 'ctax_relationships' ); + if ( 'cpt' === $post_type ) { + $this->assertIsArray( $term_cache ); + $this->assertCount( 2, $term_cache ); + } else { + $this->assertFalse( $term_cache ); + } + + wp_delete_post( $post_id, true ); + + $this->assertFalse( wp_cache_get( $post_id, 'posts' ) ); + $this->assertFalse( wp_cache_get( $post_id, 'post_tag_relationships' ) ); + $this->assertFalse( wp_cache_get( $post_id, 'ctax_relationships' ) ); + + $GLOBALS['wp_taxonomies']['post_tag']->object_type = array( 'post' ); + } + + public function data_vb_insert_get_delete() { + $post_types = array( 'post', 'cpt' ); + + return $this->text_array_to_dataprovider( $post_types ); + } + + /** + * Insert a post with a future date, and make sure the status and cron schedule are correct. + */ + public function test_vb_insert_future() { + $future_date = strtotime( '+1 day' ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + $this->assertIsInt( $post_id ); + $this->assertGreaterThan( 0, $post_id ); + + // Fetch the post and make sure it matches. + $post = get_post( $post_id ); + + $this->assertSame( $data['post_content'], $post->post_content ); + $this->assertSame( $data['post_title'], $post->post_title ); + $this->assertSame( 'future', $post->post_status ); + $this->assertEquals( $data['post_author'], $post->post_author ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // There should be a publish_future_post hook scheduled on the future date. + $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + } + + /** + * Insert a post with a future date, and make sure the status and cron schedule are correct. + */ + public function test_vb_insert_future_over_dst() { + // Some magic days - one DST one not. + $future_date_1 = strtotime( 'June 21st +1 year' ); + $future_date_2 = strtotime( 'Jan 11th +1 year' ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + + // Fetch the post and make sure has the correct date and status. + $post = get_post( $post_id ); + $this->assertSame( 'future', $post->post_status ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // Check that there's a publish_future_post job scheduled at the right time. + $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + + // Now save it again with a date further in the future. + $data['ID'] = $post_id; + $data['post_date'] = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' ); + $data['post_date_gmt'] = null; + wp_update_post( $data ); + + // Fetch the post again and make sure it has the new post_date. + $post = get_post( $post_id ); + $this->assertSame( 'future', $post->post_status ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // And the correct date on the cron job. + $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + } + + /** + * Future post bug: posts get published at the wrong time if you edit the timestamp. + * + * @ticket 4710 + */ + public function test_vb_insert_future_edit_bug() { + $future_date_1 = strtotime( '+1 day' ); + $future_date_2 = strtotime( '+2 day' ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + + // Fetch the post and make sure has the correct date and status. + $post = get_post( $post_id ); + $this->assertSame( 'future', $post->post_status ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // Check that there's a publish_future_post job scheduled at the right time. + $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + + // Now save it again with a date further in the future. + $data['ID'] = $post_id; + $data['post_date'] = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' ); + $data['post_date_gmt'] = null; + wp_update_post( $data ); + + // Fetch the post again and make sure it has the new post_date. + $post = get_post( $post_id ); + $this->assertSame( 'future', $post->post_status ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // And the correct date on the cron job. + $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + } + + /** + * Insert a draft post with a future date, and make sure no cron schedule is set. + */ + public function test_vb_insert_future_draft() { + $future_date = strtotime( '+1 day' ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'draft', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + $this->assertIsInt( $post_id ); + $this->assertGreaterThan( 0, $post_id ); + + // Fetch the post and make sure it matches. + $post = get_post( $post_id ); + + $this->assertSame( $data['post_content'], $post->post_content ); + $this->assertSame( $data['post_title'], $post->post_title ); + $this->assertSame( 'draft', $post->post_status ); + $this->assertEquals( $data['post_author'], $post->post_author ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // There should be a publish_future_post hook scheduled on the future date. + $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + + } + + /** + * Insert a future post, then edit and change it to draft, and make sure cron gets it right. + */ + public function test_vb_insert_future_change_to_draft() { + $future_date_1 = strtotime( '+1 day' ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + + // Fetch the post and make sure has the correct date and status. + $post = get_post( $post_id ); + $this->assertSame( 'future', $post->post_status ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // Check that there's a publish_future_post job scheduled at the right time. + $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + + // Now save it again with status set to draft. + $data['ID'] = $post_id; + $data['post_status'] = 'draft'; + wp_update_post( $data ); + + // Fetch the post again and make sure it has the new post_date. + $post = get_post( $post_id ); + $this->assertSame( 'draft', $post->post_status ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // And the correct date on the cron job. + $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + } + + /** + * Insert a future post, then edit and change the status, and make sure cron gets it right. + * + * @dataProvider data_vb_insert_future_change_status + */ + public function test_vb_insert_future_change_status( $status ) { + $future_date_1 = strtotime( '+1 day' ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => "{$status}_content", + 'post_title' => "{$status}_title", + 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + + // Fetch the post and make sure has the correct date and status. + $post = get_post( $post_id ); + $this->assertSame( 'future', $post->post_status ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // Check that there's a publish_future_post job scheduled at the right time. + $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + + // Now save it again with status changed. + $data['ID'] = $post_id; + $data['post_status'] = $status; + wp_update_post( $data ); + + // Fetch the post again and make sure it has the new post_date. + $post = get_post( $post_id ); + $this->assertSame( $status, $post->post_status ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // And the correct date on the cron job. + $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + } + + public function data_vb_insert_future_change_status() { + $statuses = array( + 'draft', + 'static', + 'object', + 'attachment', + 'inherit', + 'pending', + ); + + return $this->text_array_to_dataprovider( $statuses ); + } + + /** + * Insert a draft post with a future date, and make sure no cron schedule is set. + */ + public function test_vb_insert_future_private() { + $future_date = strtotime( '+1 day' ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'private', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + $this->assertIsInt( $post_id ); + $this->assertGreaterThan( 0, $post_id ); + + // Fetch the post and make sure it matches. + $post = get_post( $post_id ); + + $this->assertSame( $data['post_content'], $post->post_content ); + $this->assertSame( $data['post_title'], $post->post_title ); + $this->assertSame( 'private', $post->post_status ); + $this->assertEquals( $data['post_author'], $post->post_author ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // There should be a publish_future_post hook scheduled on the future date. + $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + } + + /** + * Insert a post with an invalid date, make sure it fails. + * + * @ticket 17180 + */ + public function test_vb_insert_invalid_date() { + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_date' => '2012-02-30 00:00:00', + ); + + // Test both return paths with or without WP_Error. + $post_id = wp_insert_post( $data, true ); + $this->assertWPError( $post_id ); + $this->assertSame( 'invalid_date', $post_id->get_error_code() ); + + $post_id = wp_insert_post( $data ); + $this->assertSame( 0, $post_id ); + } + + /** + * Insert a future post, then edit and change it to private, and make sure cron gets it right. + */ + public function test_vb_insert_future_change_to_private() { + $future_date_1 = strtotime( '+1 day' ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + + // Fetch the post and make sure has the correct date and status. + $post = get_post( $post_id ); + $this->assertSame( 'future', $post->post_status ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // Check that there's a publish_future_post job scheduled at the right time. + $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + + // Now save it again with status set to draft. + $data['ID'] = $post_id; + $data['post_status'] = 'private'; + wp_update_post( $data ); + + // Fetch the post again and make sure it has the new post_date. + $post = get_post( $post_id ); + $this->assertSame( 'private', $post->post_status ); + $this->assertSame( $data['post_date'], $post->post_date ); + + // And the correct date on the cron job. + $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + } + + /** + * @ticket 5305 + */ + public function test_wp_insert_post_should_not_allow_a_bare_numeric_slug_that_might_conflict_with_a_date_archive_when_generating_from_an_empty_post_title() { + $this->set_permalink_structure( '/%postname%/' ); + + $post_id = wp_insert_post( + array( + 'post_title' => '', + 'post_content' => 'test', + 'post_status' => 'publish', + 'post_type' => 'post', + ) + ); + + $post = get_post( $post_id ); + + $this->assertSame( "$post_id-2", $post->post_name ); + } + + /** + * @ticket 5305 + * @ticket 33392 + */ + public function test_wp_insert_post_should_invalidate_post_cache_before_generating_guid_when_post_name_is_empty_and_is_generated_from_the_post_ID() { + register_post_type( 'wptests_pt' ); + + $post_id = wp_insert_post( + array( + 'post_title' => '', + 'post_type' => 'wptests_pt', + 'post_status' => 'publish', + ) + ); + + $post = get_post( $post_id ); + + $this->assertStringContainsString( 'wptests_pt=' . $post_id, $post->guid ); + } + + /** + * @ticket 55877 + * @covers ::wp_insert_post + */ + public function test_wp_insert_post_should_not_trigger_warning_for_pending_posts_with_unknown_cpt() { + $post_id = wp_insert_post( + array( + 'post_title' => 'title', + 'post_type' => 'unknown', + 'post_status' => 'pending', + ) + ); + + $this->assertIsInt( $post_id ); + $this->assertGreaterThan( 0, $post_id ); + } + + /** + * @ticket 20451 + */ + public function test_wp_insert_post_with_meta_input() { + $post_id = wp_insert_post( + array( + 'post_title' => '', + 'post_content' => 'test', + 'post_status' => 'publish', + 'post_type' => 'post', + 'meta_input' => array( + 'hello' => 'world', + 'foo' => 'bar', + ), + ) + ); + + $this->assertSame( 'world', get_post_meta( $post_id, 'hello', true ) ); + $this->assertSame( 'bar', get_post_meta( $post_id, 'foo', true ) ); + } + + /** + * "When I delete a future post using wp_delete_post( $post->ID ) it does not update the cron correctly." + * + * @ticket 5364 + * @covers ::wp_delete_post + */ + public function test_delete_future_post_cron() { + $future_date = strtotime( '+1 day' ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + + // Check that there's a publish_future_post job scheduled at the right time. + $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + + // Now delete the post and make sure the cron entry is removed. + wp_delete_post( $post_id ); + + $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); + } + + /** + * Bug: permalink doesn't work if post title is empty. + * + * Might only fail if the post ID is greater than four characters. + * + * @ticket 5305 + */ + public function test_permalink_without_title() { + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => '', + 'post_date' => '2007-10-31 06:15:00', + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + + // Permalink should include the post ID at the end. + $expected = get_option( 'siteurl' ) . '/2007/10/31/' . $post_id . '/'; + $this->assertSame( $expected, get_permalink( $post_id ) ); + } + + /** + * @ticket 23708 + */ + public function test_get_post_ancestors_within_loop() { + global $post; + + $parent_id = self::factory()->post->create(); + $post = self::factory()->post->create_and_get( + array( + 'post_parent' => $parent_id, + ) + ); + + $this->assertSame( array( $parent_id ), get_post_ancestors( 0 ) ); + } + + /** + * @ticket 23474 + * @covers ::wp_update_post + */ + public function test_update_invalid_post_id() { + $post_id = self::factory()->post->create(); + $post = get_post( $post_id, ARRAY_A ); + + $post['ID'] = 123456789; + + $this->assertSame( 0, wp_insert_post( $post ) ); + $this->assertSame( 0, wp_update_post( $post ) ); + + $this->assertInstanceOf( 'WP_Error', wp_insert_post( $post, true ) ); + $this->assertInstanceOf( 'WP_Error', wp_update_post( $post, true ) ); + + } + + /** + * @ticket 19373 + */ + public function test_insert_programmatic_sanitized() { + $this->unset_current_user(); + + register_taxonomy( 'test_tax', 'post' ); + + $title = 'title'; + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => $title, + 'tax_input' => array( + 'test_tax' => array( 'term', 'term2', 'term3' ), + ), + ); + + $post_id = wp_insert_post( $data, true, true ); + $this->assertIsInt( $post_id ); + $this->assertGreaterThan( 0, $post_id ); + + $post = get_post( $post_id ); + $this->assertEquals( self::$user_ids['editor'], $post->post_author ); + $this->assertSame( $title, $post->post_title ); + } + + /** + * @ticket 31168 + */ + public function test_wp_insert_post_default_comment_ping_status_open() { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + ) + ); + $post = get_post( $post_id ); + + $this->assertSame( 'open', $post->comment_status ); + $this->assertSame( 'open', $post->ping_status ); + } + + /** + * @ticket 31168 + */ + public function test_wp_insert_post_page_default_comment_ping_status_closed() { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_type' => 'page', + ) + ); + $post = get_post( $post_id ); + + $this->assertSame( 'closed', $post->comment_status ); + $this->assertSame( 'closed', $post->ping_status ); + } + + /** + * @ticket 31168 + */ + public function test_wp_insert_post_cpt_default_comment_ping_status_open() { + $post_type = rand_str( 20 ); + register_post_type( $post_type, array( 'supports' => array( 'comments', 'trackbacks' ) ) ); + + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => $post_type, + ) + ); + $post = get_post( $post_id ); + + _unregister_post_type( $post_type ); + + $this->assertSame( 'open', $post->comment_status ); + $this->assertSame( 'open', $post->ping_status ); + } + + /** + * @ticket 31168 + */ + public function test_wp_insert_post_cpt_default_comment_ping_status_closed() { + $post_type = rand_str( 20 ); + register_post_type( $post_type ); + + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => $post_type, + ) + ); + $post = get_post( $post_id ); + + _unregister_post_type( $post_type ); + + $this->assertSame( 'closed', $post->comment_status ); + $this->assertSame( 'closed', $post->ping_status ); + } + + /** + * If a post is updated without providing a post_name param, + * a new slug should not be generated. + * + * @ticket 34865 + */ + public function test_post_updates_without_slug_provided() { + $post_id = self::factory()->post->create( + array( + 'post_title' => 'Stuff', + 'post_status' => 'publish', + ) + ); + + $data = array( + 'ID' => $post_id, + 'post_title' => 'Stuff and Things', + ); + + wp_insert_post( $data ); + + $updated_post = get_post( $post_id ); + // Ensure changing the post_title didn't modify the post_name. + $this->assertSame( 'stuff', $updated_post->post_name ); + } + + /** + * @ticket 32585 + */ + public function test_wp_insert_post_author_zero() { + $post_id = self::factory()->post->create( array( 'post_author' => 0 ) ); + + $this->assertEquals( 0, get_post( $post_id )->post_author ); + } + + /** + * @ticket 32585 + */ + public function test_wp_insert_post_author_null() { + wp_set_current_user( self::$user_ids['editor'] ); + + $post_id = self::factory()->post->create( array( 'post_author' => null ) ); + + $this->assertEquals( self::$user_ids['editor'], get_post( $post_id )->post_author ); + } + + /** + * @ticket 15946 + */ + public function test_wp_insert_post_should_respect_post_date_gmt() { + $data = array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_date_gmt' => '2014-01-01 12:00:00', + ); + + // Insert a post and make sure the ID is OK. + $post_id = wp_insert_post( $data ); + + $post = get_post( $post_id ); + + $this->assertSame( $data['post_content'], $post->post_content ); + $this->assertSame( $data['post_title'], $post->post_title ); + $this->assertEquals( $data['post_author'], $post->post_author ); + $this->assertSame( get_date_from_gmt( $data['post_date_gmt'] ), $post->post_date ); + $this->assertSame( $data['post_date_gmt'], $post->post_date_gmt ); + } + + /** + * Test ensuring that the post_name (UUID) is preserved when wp_insert_post()/wp_update_post() is called. + * + * @see _wp_customize_changeset_filter_insert_post_data() + * @ticket 30937 + */ + public function test_wp_insert_post_for_customize_changeset_should_not_drop_post_name() { + $this->assertSame( 10, has_filter( 'wp_insert_post_data', '_wp_customize_changeset_filter_insert_post_data' ) ); + + $changeset_data = array( + 'blogname' => array( + 'value' => 'Hello World', + ), + ); + + wp_set_current_user( self::$user_ids['contributor'] ); + + $uuid = wp_generate_uuid4(); + $post_id = wp_insert_post( + array( + 'post_type' => 'customize_changeset', + 'post_name' => strtoupper( $uuid ), + 'post_content' => wp_json_encode( $changeset_data ), + ) + ); + $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected lower-case UUID4 to be inserted.' ); + $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); + + $changeset_data['blogname']['value'] = 'Hola Mundo'; + wp_update_post( + array( + 'ID' => $post_id, + 'post_status' => 'draft', + 'post_content' => wp_json_encode( $changeset_data ), + ) + ); + $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected post_name to not have been dropped for drafts.' ); + $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); + + $changeset_data['blogname']['value'] = 'Hallo Welt'; + wp_update_post( + array( + 'ID' => $post_id, + 'post_status' => 'pending', + 'post_content' => wp_json_encode( $changeset_data ), + ) + ); + $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected post_name to not have been dropped for pending.' ); + $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); + } + + /** + * @ticket 48113 + */ + public function test_insert_post_should_respect_date_floating_post_status_arg() { + register_post_status( 'floating', array( 'date_floating' => true ) ); + + $post_id = self::factory()->post->create( + array( + 'post_status' => 'floating', + 'post_date' => null, + 'post_date_gmt' => null, + ) + ); + + $post = get_post( $post_id ); + self::assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); + } + + /** + * @ticket 48113 + */ + public function test_insert_post_should_respect_date_floating_post_status_arg_not_set() { + register_post_status( 'not-floating', array( 'date_floating' => false ) ); + + $post_id = self::factory()->post->create( + array( + 'post_status' => 'floating', + 'post_date' => null, + 'post_date_gmt' => null, + ) + ); + + $post = get_post( $post_id ); + self::assertEqualsWithDelta( + strtotime( gmdate( 'Y-m-d H:i:s' ) ), + strtotime( $post->post_date_gmt ), + 2, + 'The dates should be equal' + ); + } + + /** + * Test ensuring that wp_update_post() does not unintentionally modify post tags + * if the post has several tags with the same name but different slugs. + * + * Tags should only be modified if 'tags_input' parameter was explicitly provided, + * and is different from the existing tags. + * + * @ticket 45121 + * @covers ::wp_update_post + */ + public function test_update_post_should_only_modify_post_tags_if_different_tags_input_was_provided() { + $tag_1 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_1' ) ); + $tag_2 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_2' ) ); + $tag_3 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_3' ) ); + + $post_id = self::factory()->post->create( + array( + 'tags_input' => array( $tag_1['term_id'], $tag_2['term_id'] ), + ) + ); + + $post = get_post( $post_id ); + + $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); + $this->assertSameSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags ); + + wp_update_post( $post ); + + $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); + $this->assertSameSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags ); + + wp_update_post( + array( + 'ID' => $post->ID, + 'tags_input' => array( $tag_2['term_id'], $tag_3['term_id'] ), + ) + ); + + $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); + $this->assertSameSets( array( $tag_2['term_id'], $tag_3['term_id'] ), $tags ); + } + + /** + * @ticket 52187 + */ + public function test_insert_empty_post_date() { + $post_date_gmt = '2020-12-29 10:11:45'; + $invalid_date = '2020-12-41 14:15:27'; + + // Empty post_date_gmt with floating status + $post_id = self::factory()->post->create( + array( + 'post_status' => 'draft', + ) + ); + $post = get_post( $post_id ); + $this->assertEqualsWithDelta( + strtotime( gmdate( 'Y-m-d H:i:s' ) ), + strtotime( $post->post_date ), + 2, + 'The dates should be equal' + ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); + + $post_id = self::factory()->post->create( + array( + 'post_date_gmt' => '0000-00-00 00:00:00', + 'post_status' => 'draft', + ) + ); + $post = get_post( $post_id ); + $this->assertEqualsWithDelta( + strtotime( gmdate( 'Y-m-d H:i:s' ) ), + strtotime( $post->post_date ), + 2, + 'The dates should be equal' + ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); + + // Empty post_date_gmt without floating status + $post_id = self::factory()->post->create( + array( + 'post_status' => 'publish', + ) + ); + $post = get_post( $post_id ); + $this->assertEqualsWithDelta( + strtotime( gmdate( 'Y-m-d H:i:s' ) ), + strtotime( $post->post_date ), + 2, + 'The dates should be equal' + ); + $this->assertEqualsWithDelta( + strtotime( gmdate( 'Y-m-d H:i:s' ) ), + strtotime( get_gmt_from_date( $post->post_date ) ), + 2, + 'The dates should be equal' + ); + + $post_id = self::factory()->post->create( + array( + 'post_date_gmt' => '0000-00-00 00:00:00', + 'post_status' => 'publish', + ) + ); + $post = get_post( $post_id ); + $this->assertEqualsWithDelta( + strtotime( gmdate( 'Y-m-d H:i:s' ) ), + strtotime( $post->post_date ), + 2, + 'The dates should be equal' + ); + $this->assertEqualsWithDelta( + strtotime( gmdate( 'Y-m-d H:i:s' ) ), + strtotime( get_gmt_from_date( $post->post_date ) ), + 2, + 'The dates should be equal' + ); + + // Valid post_date_gmt + $post_id = self::factory()->post->create( + array( + 'post_date_gmt' => $post_date_gmt, + ) + ); + $post = get_post( $post_id ); + $this->assertSame( get_date_from_gmt( $post_date_gmt ), $post->post_date ); + $this->assertSame( $post_date_gmt, $post->post_date_gmt ); + + // Invalid post_date_gmt + $post_id = self::factory()->post->create( + array( + 'post_date_gmt' => $invalid_date, + ) + ); + $post = get_post( $post_id ); + $this->assertSame( '1970-01-01 00:00:00', $post->post_date ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); + } + + /** + * @ticket 52187 + */ + public function test_insert_valid_post_date() { + $post_date = '2020-12-28 11:26:35'; + $post_date_gmt = '2020-12-29 10:11:45'; + $invalid_date = '2020-12-41 14:15:27'; + + // Empty post_date_gmt with floating status + $post_id = self::factory()->post->create( + array( + 'post_date' => $post_date, + 'post_status' => 'draft', + ) + ); + $post = get_post( $post_id ); + $this->assertSame( $post_date, $post->post_date ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); + + $post_id = self::factory()->post->create( + array( + 'post_date' => $post_date, + 'post_date_gmt' => '0000-00-00 00:00:00', + 'post_status' => 'draft', + ) + ); + $post = get_post( $post_id ); + $this->assertSame( $post_date, $post->post_date ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); + + // Empty post_date_gmt without floating status + $post_id = self::factory()->post->create( + array( + 'post_date' => $post_date, + 'post_status' => 'publish', + ) + ); + $post = get_post( $post_id ); + $this->assertSame( $post_date, $post->post_date ); + $this->assertSame( get_gmt_from_date( $post_date ), $post->post_date_gmt ); + + $post_id = self::factory()->post->create( + array( + 'post_date' => $post_date, + 'post_date_gmt' => '0000-00-00 00:00:00', + 'post_status' => 'publish', + ) + ); + $post = get_post( $post_id ); + $this->assertSame( $post_date, $post->post_date ); + $this->assertSame( get_gmt_from_date( $post_date ), $post->post_date_gmt ); + + // Valid post_date_gmt + $post_id = self::factory()->post->create( + array( + 'post_date' => $post_date, + 'post_date_gmt' => $post_date_gmt, + ) + ); + $post = get_post( $post_id ); + $this->assertSame( $post_date, $post->post_date ); + $this->assertSame( $post_date_gmt, $post->post_date_gmt ); + + // Invalid post_date_gmt + $post_id = self::factory()->post->create( + array( + 'post_date' => $post_date, + 'post_date_gmt' => $invalid_date, + ) + ); + $post = get_post( $post_id ); + $this->assertSame( $post_date, $post->post_date ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); + } + + /** + * @ticket 52187 + */ + public function test_insert_invalid_post_date() { + $post_date = '2020-12-28 11:26:35'; + $post_date_gmt = '2020-12-29 10:11:45'; + $invalid_date = '2020-12-41 14:15:27'; + + // Empty post_date_gmt with floating status + $post_id = self::factory()->post->create( + array( + 'post_date' => $invalid_date, + 'post_status' => 'draft', + ) + ); + $this->assertSame( 0, $post_id ); + + $post_id = self::factory()->post->create( + array( + 'post_date' => $invalid_date, + 'post_date_gmt' => '0000-00-00 00:00:00', + 'post_status' => 'draft', + ) + ); + $this->assertSame( 0, $post_id ); + + // Empty post_date_gmt without floating status + $post_id = self::factory()->post->create( + array( + 'post_date' => $invalid_date, + 'post_status' => 'publish', + ) + ); + $this->assertSame( 0, $post_id ); + + $post_id = self::factory()->post->create( + array( + 'post_date' => $invalid_date, + 'post_date_gmt' => '0000-00-00 00:00:00', + 'post_status' => 'publish', + ) + ); + $this->assertSame( 0, $post_id ); + + // Valid post_date_gmt + $post_id = self::factory()->post->create( + array( + 'post_date' => $invalid_date, + 'post_date_gmt' => $post_date_gmt, + ) + ); + $this->assertSame( 0, $post_id ); + + // Invalid post_date_gmt + $post_id = self::factory()->post->create( + array( + 'post_date' => $invalid_date, + 'post_date_gmt' => $invalid_date, + ) + ); + $this->assertSame( 0, $post_id ); + } + /** * @ticket 11863 */