From 570d86da90ee15c2e23967f483ceeb0531e90182 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 17 Jul 2021 10:23:57 +0000 Subject: [PATCH] Tests: Correct the test for autosaving a post with Ajax. `wp_autosave()` only updates drafts and auto-drafts created by the current user if the post is not locked. As a result of previous Ajax test refactoring, setting the current user and creating a test post ended up in different methods, with the user being set after the post is already created. This resulted in the test post being created with the `post_author` field set to zero, and the current user check in `wp_autosave()` failed. Instead of updating the original post as the test intended, it created a new autosave. The test only passed accidentally due to `assertGreaterThanOrEqual()` not performing a strict type check. Follow-up to [26995], [35311]. See #53363. git-svn-id: https://develop.svn.wordpress.org/trunk@51450 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/ajax/Autosave.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/tests/phpunit/tests/ajax/Autosave.php b/tests/phpunit/tests/ajax/Autosave.php index 457bcc4c71..9bd4cbccb3 100644 --- a/tests/phpunit/tests/ajax/Autosave.php +++ b/tests/phpunit/tests/ajax/Autosave.php @@ -34,17 +34,11 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase { self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) ); self::$user_ids[] = self::$editor_id; - self::$post_id = $factory->post->create( array( 'post_status' => 'draft' ) ); - self::$post = get_post( self::$post_id ); - } - - /** - * Sets up the test fixture. - */ - public function setUp() { - parent::setUp(); // Set a user so the $post has 'post_author'. wp_set_current_user( self::$admin_id ); + + self::$post_id = $factory->post->create( array( 'post_status' => 'draft' ) ); + self::$post = get_post( self::$post_id ); } /** @@ -62,7 +56,7 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase { 'data' => array( 'wp_autosave' => array( 'post_id' => self::$post_id, - '_wpnonce' => wp_create_nonce( 'update-post_' . self::$post->ID ), + '_wpnonce' => wp_create_nonce( 'update-post_' . self::$post_id ), 'post_content' => self::$post->post_content . PHP_EOL . $md5, 'post_type' => 'post', ), @@ -85,7 +79,7 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase { // Check that the edit happened. $post = get_post( self::$post_id ); - $this->assertGreaterThanOrEqual( 0, strpos( self::$post->post_content, $md5 ) ); + $this->assertNotFalse( strpos( $post->post_content, $md5 ) ); } /** @@ -136,7 +130,7 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase { // Check if the autosave post was created. $autosave = wp_get_post_autosave( self::$post_id, get_current_user_id() ); $this->assertNotEmpty( $autosave ); - $this->assertGreaterThanOrEqual( 0, strpos( $autosave->post_content, $md5 ) ); + $this->assertNotFalse( strpos( $autosave->post_content, $md5 ) ); } /**