From 33a3d61b2263f378d9c3872a7f676dda67b95654 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Wed, 24 Jan 2018 06:38:58 +0000 Subject: [PATCH] Tests: Improve the old date redirect tests. Props frank-klein. See #15397. git-svn-id: https://develop.svn.wordpress.org/trunk@42587 602fd350-edb4-49c9-b593-d223f7449a82 --- .../phpunit/tests/rewrite/oldDateRedirect.php | 101 +++++++++--------- 1 file changed, 50 insertions(+), 51 deletions(-) diff --git a/tests/phpunit/tests/rewrite/oldDateRedirect.php b/tests/phpunit/tests/rewrite/oldDateRedirect.php index 9bff121044..04be57ce23 100644 --- a/tests/phpunit/tests/rewrite/oldDateRedirect.php +++ b/tests/phpunit/tests/rewrite/oldDateRedirect.php @@ -6,16 +6,29 @@ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase { protected $old_date_redirect_url; - protected $post_id; + public static $post_id; - public function setUp() { - parent::setUp(); + public static $attachment_id; - $this->post_id = self::factory()->post->create( array( + public static function wpSetUpBeforeClass() { + self::$post_id = self::factory()->post->create( array( 'post_title' => 'Foo Bar', 'post_name' => 'foo-bar', ) ); + self::$attachment_id = self::factory()->attachment->create_object( + array( + 'file' => DIR_TESTDATA . '/images/canola.jpg', + 'post_mime_type' => 'image/jpeg', + 'post_name' => 'my-attachment', + 'post_parent' => self::$post_id, + ) + ); + } + + public function setUp() { + parent::setUp(); + add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_date_redirect_url' ), 10, 1 ); $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); @@ -30,57 +43,49 @@ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase { parent::tearDown(); $this->old_date_redirect_url = null; - - remove_filter( 'old_slug_redirect_url', array( $this, 'filter_old_date_redirect_url' ), 10 ); } public function test_old_date_redirect() { - $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) ); $time = '2004-01-03 00:00:00'; wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_date' => $time, 'post_date_gmt' => get_gmt_from_date( $time ), ) ); - $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(); - $this->assertEquals( $permalink, $this->old_date_redirect_url ); + $this->assertSame( $permalink, $this->old_date_redirect_url ); } public function test_old_date_slug_redirect() { - $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) ); $time = '2004-01-03 00:00:00'; wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_date' => $time, 'post_date_gmt' => get_gmt_from_date( $time ), '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(); - $this->assertEquals( $permalink, $this->old_date_redirect_url ); + $this->assertSame( $permalink, $this->old_date_redirect_url ); } public function test_old_date_redirect_attachment() { - $file = DIR_TESTDATA . '/images/canola.jpg'; - $attachment_id = self::factory()->attachment->create_object( $file, $this->post_id, array( - 'post_mime_type' => 'image/jpeg', - 'post_name' => 'my-attachment', - ) ); - - $old_permalink = get_attachment_link( $attachment_id ); + $old_permalink = get_attachment_link( self::$attachment_id ); $time = '2004-01-03 00:00:00'; wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_date' => $time, 'post_date_gmt' => get_gmt_from_date( $time ), ) ); @@ -90,32 +95,26 @@ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase { $this->assertNull( $this->old_date_redirect_url ); $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' ); - $old_permalink = get_attachment_link( $attachment_id ); + $old_permalink = get_attachment_link( self::$attachment_id ); wp_update_post( array( - 'ID' => $attachment_id, + 'ID' => self::$attachment_id, 'post_name' => 'the-attachment', ) ); - $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(); - $this->assertEquals( $permalink, $this->old_date_redirect_url ); + $this->assertSame( $permalink, $this->old_date_redirect_url ); } public function test_old_date_slug_redirect_attachment() { - $file = DIR_TESTDATA . '/images/canola.jpg'; - $attachment_id = self::factory()->attachment->create_object( $file, $this->post_id, array( - 'post_mime_type' => 'image/jpeg', - 'post_name' => 'my-attachment', - ) ); - - $old_permalink = get_attachment_link( $attachment_id ); + $old_permalink = get_attachment_link( self::$attachment_id ); $time = '2004-01-03 00:00:00'; wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_date' => $time, 'post_date_gmt' => get_gmt_from_date( $time ), 'post_name' => 'bar-baz', @@ -126,71 +125,71 @@ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase { $this->assertNull( $this->old_date_redirect_url ); $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' ); - $old_permalink = get_attachment_link( $attachment_id ); + $old_permalink = get_attachment_link( self::$attachment_id ); wp_update_post( array( - 'ID' => $attachment_id, + 'ID' => self::$attachment_id, 'post_name' => 'the-attachment', ) ); - $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(); - $this->assertEquals( $permalink, $this->old_date_redirect_url ); + $this->assertSame( $permalink, $this->old_date_redirect_url ); } public function test_old_date_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' ); $time = '2004-01-03 00:00:00'; wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_date' => $time, 'post_date_gmt' => get_gmt_from_date( $time ), ) ); - $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(); - $this->assertEquals( $permalink, $this->old_date_redirect_url ); + $this->assertSame( $permalink, $this->old_date_redirect_url ); } public function test_old_date_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' ); $time = '2004-01-04 12:00:00'; wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_date' => $time, 'post_date_gmt' => get_gmt_from_date( $time ), '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(); - $this->assertEquals( $permalink, $this->old_date_redirect_url ); + $this->assertSame( $permalink, $this->old_date_redirect_url ); } public function test_old_date_slug_doesnt_redirect_when_reused() { - $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) ); $time = '2004-01-04 12:00:00'; wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_date' => $time, 'post_date_gmt' => get_gmt_from_date( $time ), 'post_name' => 'bar-baz', @@ -203,7 +202,7 @@ class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase { $permalink = user_trailingslashit( get_permalink( $new_post_id ) ); - $this->assertEquals( $old_permalink, $permalink ); + $this->assertSame( $old_permalink, $permalink ); $this->go_to( $old_permalink ); wp_old_slug_redirect();