| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group rewrite |
| | 5 | * @ticket 33920 |
| | 6 | */ |
| | 7 | class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { |
| | 8 | protected $old_slug_redirect_url; |
| | 9 | |
| | 10 | protected $post_id; |
| | 11 | |
| | 12 | public static function setUpBeforeClass() { |
| | 13 | WP_UnitTestCase::setUpBeforeClass(); |
| | 14 | |
| | 15 | global $wp_rewrite; |
| | 16 | |
| | 17 | $wp_rewrite->init(); |
| | 18 | $wp_rewrite->set_permalink_structure( '/%postname%/' ); |
| | 19 | add_rewrite_endpoint( 'custom-endpoint', EP_PERMALINK ); |
| | 20 | $wp_rewrite->flush_rules(); |
| | 21 | } |
| | 22 | |
| | 23 | public static function tearDownAfterClass() { |
| | 24 | WP_UnitTestCase::tearDownAfterClass(); |
| | 25 | |
| | 26 | global $wp_rewrite; |
| | 27 | |
| | 28 | $wp_rewrite->set_permalink_structure( '' ); |
| | 29 | $wp_rewrite->init(); |
| | 30 | } |
| | 31 | |
| | 32 | public function setUp() { |
| | 33 | parent::setUp(); |
| | 34 | |
| | 35 | $this->post_id = $this->factory->post->create( array( |
| | 36 | 'post_title' => 'Foo Bar', |
| | 37 | 'post_name' => 'foo-bar', |
| | 38 | ) ); |
| | 39 | |
| | 40 | add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10, 1 ); |
| | 41 | } |
| | 42 | |
| | 43 | public function tearDown() { |
| | 44 | parent::tearDown(); |
| | 45 | |
| | 46 | $this->old_slug_redirect_url = null; |
| | 47 | |
| | 48 | remove_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10 ); |
| | 49 | } |
| | 50 | |
| | 51 | public function test_old_slug_redirect() { |
| | 52 | $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); |
| | 53 | |
| | 54 | wp_update_post( array( |
| | 55 | 'ID' => $this->post_id, |
| | 56 | 'post_name' => 'bar-baz', |
| | 57 | ) ); |
| | 58 | |
| | 59 | $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); |
| | 60 | |
| | 61 | $this->go_to( $old_permalink ); |
| | 62 | $this->assertQueryTrue( 'is_404' ); |
| | 63 | |
| | 64 | wp_old_slug_redirect(); |
| | 65 | $this->assertEquals( $permalink, $this->old_slug_redirect_url ); |
| | 66 | } |
| | 67 | |
| | 68 | public function test_old_slug_redirect_endpoint() { |
| | 69 | $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'custom-endpoint' ); |
| | 70 | |
| | 71 | wp_update_post( array( |
| | 72 | 'ID' => $this->post_id, |
| | 73 | 'post_name' => 'bar-baz', |
| | 74 | ) ); |
| | 75 | |
| | 76 | $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'custom-endpoint' ); |
| | 77 | |
| | 78 | $this->go_to( $old_permalink ); |
| | 79 | $GLOBALS['wp_query']->query_vars['custom-endpoint'] = true; |
| | 80 | $this->assertQueryTrue( 'is_404' ); |
| | 81 | |
| | 82 | wp_old_slug_redirect(); |
| | 83 | $this->assertEquals( $permalink, $this->old_slug_redirect_url ); |
| | 84 | } |
| | 85 | |
| | 86 | public function test_old_slug_redirect_feed() { |
| | 87 | $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'feed' ); |
| | 88 | |
| | 89 | wp_update_post( array( |
| | 90 | 'ID' => $this->post_id, |
| | 91 | 'post_name' => 'bar-baz', |
| | 92 | ) ); |
| | 93 | |
| | 94 | $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'feed' ); |
| | 95 | |
| | 96 | $this->go_to( $old_permalink ); |
| | 97 | |
| | 98 | wp_old_slug_redirect(); |
| | 99 | $this->assertEquals( $permalink, $this->old_slug_redirect_url ); |
| | 100 | } |
| | 101 | |
| | 102 | public function filter_old_slug_redirect_url( $url ) { |
| | 103 | $this->old_slug_redirect_url = $url; |
| | 104 | return false; |
| | 105 | } |
| | 106 | } |