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