| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group rewrite |
| 5 | * @ticket 33920 |
| 6 | */ |
| 7 | class Tests_Rewrite_OldPageSlugRedirect 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 = self::factory()->post->create( |
| 16 | array( |
| 17 | 'post_title' => 'Foo Bar', |
| 18 | 'post_name' => 'foo-bar', |
| 19 | 'post_type' => 'page', |
| 20 | ) |
| 21 | ); |
| 22 | |
| 23 | add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10, 1 ); |
| 24 | |
| 25 | $this->set_permalink_structure( '/%postname%/' ); |
| 26 | |
| 27 | add_rewrite_endpoint( 'custom-endpoint', EP_PERMALINK ); |
| 28 | add_rewrite_endpoint( 'second-endpoint', EP_PERMALINK, 'custom' ); |
| 29 | |
| 30 | flush_rewrite_rules(); |
| 31 | } |
| 32 | |
| 33 | public function tearDown() { |
| 34 | parent::tearDown(); |
| 35 | |
| 36 | $this->old_slug_redirect_url = null; |
| 37 | |
| 38 | remove_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10 ); |
| 39 | } |
| 40 | |
| 41 | public function test_old_slug_redirect() { |
| 42 | $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); |
| 43 | |
| 44 | wp_update_post( |
| 45 | array( |
| 46 | 'ID' => $this->post_id, |
| 47 | 'post_name' => 'bar-baz', |
| 48 | ) |
| 49 | ); |
| 50 | |
| 51 | $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); |
| 52 | |
| 53 | $this->go_to( $old_permalink ); |
| 54 | wp_old_slug_redirect(); |
| 55 | $this->assertEquals( $permalink, $this->old_slug_redirect_url ); |
| 56 | } |
| 57 | |
| 58 | public function test_old_slug_redirect_paged() { |
| 59 | wp_update_post( |
| 60 | array( |
| 61 | 'ID' => $this->post_id, |
| 62 | 'post_content' => 'Test<!--nextpage-->Test', |
| 63 | ) |
| 64 | ); |
| 65 | |
| 66 | $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' ); |
| 67 | |
| 68 | wp_update_post( |
| 69 | array( |
| 70 | 'ID' => $this->post_id, |
| 71 | 'post_name' => 'bar-baz', |
| 72 | ) |
| 73 | ); |
| 74 | |
| 75 | $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' ); |
| 76 | |
| 77 | $this->go_to( $old_permalink ); |
| 78 | wp_old_slug_redirect(); |
| 79 | $this->assertEquals( $permalink, $this->old_slug_redirect_url ); |
| 80 | } |
| 81 | |
| 82 | public function filter_old_slug_redirect_url( $url ) { |
| 83 | $this->old_slug_redirect_url = $url; |
| 84 | return false; |
| 85 | } |
| 86 | } |