From 0bb0369f2ba1c11af5e5e2abf3c75e46b98a93b1 Mon Sep 17 00:00:00 2001 From: Nirav7707 Date: Mon, 25 Apr 2022 19:33:39 +0530 Subject: [PATCH 1/2] Test: wp_publish_post provides unique slug to post Add testcase for the wp_publish_post function to ensure that post have same title have different slug. --- tests/phpunit/tests/post.php | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 8180a867dcaec..1a80b3327ffc7 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -1844,4 +1844,53 @@ public function test_unstick_post_with_non_sticky_post_id_does_not_update_option unstick_post( 3 ); $this->assertSameSets( array( 1, 2, 2 ), get_option( 'sticky_posts' ) ); } + + /** + * Ensure updated post have a different slug then already publish post with same name. + * + * @ticket 50447 + * @covers ::wp_update_post + */ + public function test_updated_draft_post_have_unique_slug_from_publish_post_with_same_title() { + + // Publish post metainfo. + $post = array( + 'post_name' => 'test', + 'post_title' => 'test', + 'post_status' => 'publish', + ); + + // Add post. + $post_id = wp_insert_post( $post ); + + // Testcase for publish post with $post_id + $this->assertSame( 102, $post_id ); + $this->assertSame( 'test', get_post( $post_id )->post_name ); + $this->assertSame( 'test', get_post( $post_id )->post_title ); + $this->assertSame( 'publish', get_post( $post_id )->post_status ); + + // Draft post metainfo. + $draft_post = array( + 'post_title' => 'test', + 'post_status' => 'draft', + 'post_name' => 'test' + ); + + // Add draft post. + $draft_post_id = wp_insert_post( $draft_post ); + + // Testcase for draft post with $draft_post_id. + $this->assertSame( 103, $draft_post_id ); + $this->assertSame( 'test', get_post( $draft_post_id )->post_title ); + $this->assertSame( 'draft', get_post( $draft_post_id )->post_status ); + + // Update draft post. + wp_publish_post( $draft_post_id ); + + // Testcase for updated draft post. + $this->assertSame( 'test', get_post( $draft_post_id )->post_title ); + $this->assertSame( 'publish', get_post( $draft_post_id )->post_status ); + $this->assertSame( 'test-2', get_post( $draft_post_id )->post_name ); + $this->assertNotSame( 'test', get_post( $draft_post_id )->post_name ); + } } From 0cd439ad66a12ca738d752a1169468b4fb18701b Mon Sep 17 00:00:00 2001 From: Nirav7707 Date: Tue, 26 Apr 2022 11:33:06 +0530 Subject: [PATCH 2/2] Refect: change wp_insert_post with factory object Change the way of post creation from wp_insert_post to factory object to follow test standard. --- tests/phpunit/tests/post.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 1a80b3327ffc7..7d9751bb632ec 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -1849,7 +1849,7 @@ public function test_unstick_post_with_non_sticky_post_id_does_not_update_option * Ensure updated post have a different slug then already publish post with same name. * * @ticket 50447 - * @covers ::wp_update_post + * @covers ::wp_publish_post */ public function test_updated_draft_post_have_unique_slug_from_publish_post_with_same_title() { @@ -1861,13 +1861,12 @@ public function test_updated_draft_post_have_unique_slug_from_publish_post_with_ ); // Add post. - $post_id = wp_insert_post( $post ); + $post_id = self::factory()->post->create( $post ); // Testcase for publish post with $post_id - $this->assertSame( 102, $post_id ); - $this->assertSame( 'test', get_post( $post_id )->post_name ); - $this->assertSame( 'test', get_post( $post_id )->post_title ); - $this->assertSame( 'publish', get_post( $post_id )->post_status ); + $post_object = get_post( $post_id ); + $this->assertSame( 'test', $post_object->post_title ); + $this->assertSame( 'test', $post_object->post_name ); // Draft post metainfo. $draft_post = array( @@ -1877,20 +1876,21 @@ public function test_updated_draft_post_have_unique_slug_from_publish_post_with_ ); // Add draft post. - $draft_post_id = wp_insert_post( $draft_post ); + $draft_post_id = self::factory()->post->create( $draft_post ); // Testcase for draft post with $draft_post_id. - $this->assertSame( 103, $draft_post_id ); - $this->assertSame( 'test', get_post( $draft_post_id )->post_title ); - $this->assertSame( 'draft', get_post( $draft_post_id )->post_status ); + $post_object = get_post( $draft_post_id ); + $this->assertSame( 'test', $post_object->post_title ); + $this->assertSame( 'draft', $post_object->post_status ); // Update draft post. wp_publish_post( $draft_post_id ); // Testcase for updated draft post. - $this->assertSame( 'test', get_post( $draft_post_id )->post_title ); - $this->assertSame( 'publish', get_post( $draft_post_id )->post_status ); - $this->assertSame( 'test-2', get_post( $draft_post_id )->post_name ); - $this->assertNotSame( 'test', get_post( $draft_post_id )->post_name ); + $post_object = get_post( $draft_post_id ); + $this->assertSame( 'test', $post_object->post_title ); + $this->assertSame( 'publish', $post_object->post_status ); + $this->assertNotSame( 'test', $post_object->post_name ); + $this->assertSame( wp_unique_post_slug($post_object->post_name, $post_object->ID, $post_object->post_status, $post_object->post_type, $post_object->post_parent), $post_object->post_name ); } }