From 225b6cbe78cdc275c2d9f214c12876b756392d52 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 6 Sep 2022 01:25:45 +0000 Subject: [PATCH] Tests: Consistently create a post fixture in old date or slug redirect tests. This affects: * `Tests_Rewrite_OldDateRedirect` * `Tests_Rewrite_OldSlugRedirect` This commit updates the latter test class to create a post in the `wpSetUpBeforeClass()` method, for consistency with the former class. This ensures that both classes declare the `$post_id` property as `static`, to avoid a situation where non-static access is accidentally used when copying similar test cases from one class to the other. Follow-up to [34659], [42587], [54077]. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@54078 602fd350-edb4-49c9-b593-d223f7449a82 --- .../phpunit/tests/rewrite/oldSlugRedirect.php | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/tests/phpunit/tests/rewrite/oldSlugRedirect.php b/tests/phpunit/tests/rewrite/oldSlugRedirect.php index 754ad996ff..cf38ba452e 100644 --- a/tests/phpunit/tests/rewrite/oldSlugRedirect.php +++ b/tests/phpunit/tests/rewrite/oldSlugRedirect.php @@ -8,17 +8,19 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { protected $old_slug_redirect_url; - protected $post_id; + protected static $post_id; - public function set_up() { - parent::set_up(); - - $this->post_id = self::factory()->post->create( + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { + self::$post_id = $factory->post->create( array( 'post_title' => 'Foo Bar', 'post_name' => 'foo-bar', ) ); + } + + public function set_up() { + parent::set_up(); add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10, 1 ); @@ -37,16 +39,16 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { } public function test_old_slug_redirect() { - $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) ); wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_name' => 'bar-baz', ) ); - $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $permalink = user_trailingslashit( get_permalink( self::$post_id ) ); $this->go_to( $old_permalink ); wp_old_slug_redirect(); @@ -57,16 +59,16 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { * @ticket 36723 */ public function test_old_slug_redirect_cache() { - $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) ); wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_name' => 'bar-baz', ) ); - $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $permalink = user_trailingslashit( get_permalink( self::$post_id ) ); $this->go_to( $old_permalink ); @@ -83,16 +85,16 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { * @ticket 36723 */ public function test_old_slug_redirect_cache_invalidation() { - $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) ); wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_name' => 'bar-baz', ) ); - $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $permalink = user_trailingslashit( get_permalink( self::$post_id ) ); $this->go_to( $old_permalink ); @@ -101,12 +103,12 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_name' => 'foo-bar-baz', ) ); - $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $permalink = user_trailingslashit( get_permalink( self::$post_id ) ); $num_queries = get_num_queries(); wp_old_slug_redirect(); @@ -118,7 +120,7 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { $file = DIR_TESTDATA . '/images/canola.jpg'; $attachment_id = self::factory()->attachment->create_object( $file, - $this->post_id, + self::$post_id, array( 'post_mime_type' => 'image/jpeg', 'post_name' => 'my-attachment', @@ -129,7 +131,7 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_name' => 'bar-baz', ) ); @@ -148,7 +150,7 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { ) ); - $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'the-attachment' ); + $permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'the-attachment' ); $this->go_to( $old_permalink ); wp_old_slug_redirect(); @@ -158,21 +160,21 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { public function test_old_slug_redirect_paged() { wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_content' => 'TestTest', ) ); - $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' ); + $old_permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'page/2' ); wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_name' => 'bar-baz', ) ); - $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' ); + $permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'page/2' ); $this->go_to( $old_permalink ); wp_old_slug_redirect(); @@ -183,11 +185,11 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { * @ticket 35031 */ public function test_old_slug_doesnt_redirect_when_reused() { - $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) ); wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_name' => 'bar-baz', ) );