| 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 test_old_slug_redirect_attachment() { |
| 107 | $file = DIR_TESTDATA . '/images/canola.jpg'; |
| 108 | $attachment_id = $this->factory->attachment->create_object( $file, $this->post_id, array( |
| 109 | 'post_mime_type' => 'image/jpeg', |
| 110 | 'post_name' => 'my-attachment', |
| 111 | ) ); |
| 112 | |
| 113 | $old_permalink = get_attachment_link( $attachment_id ); |
| 114 | |
| 115 | wp_update_post( array( |
| 116 | 'ID' => $this->post_id, |
| 117 | 'post_name' => 'bar-baz', |
| 118 | ) ); |
| 119 | |
| 120 | $this->go_to( $old_permalink ); |
| 121 | wp_old_slug_redirect(); |
| 122 | $this->assertNull( $this->old_slug_redirect_url ); |
| 123 | $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' ); |
| 124 | } |
| 125 | |
| 126 | public function test_old_slug_redirect_paged() { |
| 127 | wp_update_post( array( |
| 128 | 'ID' => $this->post_id, |
| 129 | 'post_content' => 'Test<!--nextpage-->Test', |
| 130 | ) ); |
| 131 | |
| 132 | $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' ); |
| 133 | |
| 134 | wp_update_post( array( |
| 135 | 'ID' => $this->post_id, |
| 136 | 'post_name' => 'bar-baz', |
| 137 | ) ); |
| 138 | |
| 139 | $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' ); |
| 140 | |
| 141 | $this->go_to( $old_permalink ); |
| 142 | wp_old_slug_redirect(); |
| 143 | $this->assertEquals( $permalink, $this->old_slug_redirect_url ); |
| 144 | } |
| 145 | |
| 146 | public function filter_old_slug_redirect_url( $url ) { |
| 147 | $this->old_slug_redirect_url = $url; |
| 148 | return false; |
| 149 | } |
| 150 | } |