oldSlugRedirect.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @group rewrite
  4. * @ticket 33920
  5. */
  6. class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase {
  7. protected $old_slug_redirect_url;
  8. protected $post_id;
  9. public function setUp() {
  10. parent::setUp();
  11. $this->post_id = self::factory()->post->create(
  12. array(
  13. 'post_title' => 'Foo Bar',
  14. 'post_name' => 'foo-bar',
  15. )
  16. );
  17. add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10, 1 );
  18. $this->set_permalink_structure( '/%postname%/' );
  19. add_rewrite_endpoint( 'custom-endpoint', EP_PERMALINK );
  20. add_rewrite_endpoint( 'second-endpoint', EP_PERMALINK, 'custom' );
  21. flush_rewrite_rules();
  22. }
  23. public function tearDown() {
  24. $this->old_slug_redirect_url = null;
  25. parent::tearDown();
  26. }
  27. public function test_old_slug_redirect() {
  28. $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
  29. wp_update_post(
  30. array(
  31. 'ID' => $this->post_id,
  32. 'post_name' => 'bar-baz',
  33. )
  34. );
  35. $permalink = user_trailingslashit( get_permalink( $this->post_id ) );
  36. $this->go_to( $old_permalink );
  37. wp_old_slug_redirect();
  38. $this->assertSame( $permalink, $this->old_slug_redirect_url );
  39. }
  40. public function test_old_slug_redirect_attachment() {
  41. $file = DIR_TESTDATA . '/images/canola.jpg';
  42. $attachment_id = self::factory()->attachment->create_object(
  43. $file,
  44. $this->post_id,
  45. array(
  46. 'post_mime_type' => 'image/jpeg',
  47. 'post_name' => 'my-attachment',
  48. )
  49. );
  50. $old_permalink = get_attachment_link( $attachment_id );
  51. wp_update_post(
  52. array(
  53. 'ID' => $this->post_id,
  54. 'post_name' => 'bar-baz',
  55. )
  56. );
  57. $this->go_to( $old_permalink );
  58. wp_old_slug_redirect();
  59. $this->assertNull( $this->old_slug_redirect_url );
  60. $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
  61. $old_permalink = get_attachment_link( $attachment_id );
  62. wp_update_post(
  63. array(
  64. 'ID' => $attachment_id,
  65. 'post_name' => 'the-attachment',
  66. )
  67. );
  68. $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'the-attachment' );
  69. $this->go_to( $old_permalink );
  70. wp_old_slug_redirect();
  71. $this->assertSame( $permalink, $this->old_slug_redirect_url );
  72. }
  73. public function test_old_slug_redirect_paged() {
  74. wp_update_post(
  75. array(
  76. 'ID' => $this->post_id,
  77. 'post_content' => 'Test<!--nextpage-->Test',
  78. )
  79. );
  80. $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
  81. wp_update_post(
  82. array(
  83. 'ID' => $this->post_id,
  84. 'post_name' => 'bar-baz',
  85. )
  86. );
  87. $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
  88. $this->go_to( $old_permalink );
  89. wp_old_slug_redirect();
  90. $this->assertSame( $permalink, $this->old_slug_redirect_url );
  91. }
  92. /**
  93. * @ticket 35031
  94. */
  95. public function test_old_slug_doesnt_redirect_when_reused() {
  96. $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
  97. wp_update_post(
  98. array(
  99. 'ID' => $this->post_id,
  100. 'post_name' => 'bar-baz',
  101. )
  102. );
  103. $new_post_id = self::factory()->post->create(
  104. array(
  105. 'post_title' => 'Foo Bar',
  106. 'post_name' => 'foo-bar',
  107. )
  108. );
  109. $permalink = user_trailingslashit( get_permalink( $new_post_id ) );
  110. $this->assertSame( $old_permalink, $permalink );
  111. $this->go_to( $old_permalink );
  112. wp_old_slug_redirect();
  113. $this->assertNull( $this->old_slug_redirect_url );
  114. }
  115. public function filter_old_slug_redirect_url( $url ) {
  116. $this->old_slug_redirect_url = $url;
  117. return false;
  118. }
  119. }