link.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * @group link
  4. */
  5. class Tests_Link extends WP_UnitTestCase {
  6. function _get_pagenum_link_cb( $url ) {
  7. return $url . '/WooHoo';
  8. }
  9. /**
  10. * @ticket 8847
  11. */
  12. function test_get_pagenum_link_case_insensitivity() {
  13. $old_req_uri = $_SERVER['REQUEST_URI'];
  14. $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  15. add_filter( 'home_url', array( $this, '_get_pagenum_link_cb' ) );
  16. $_SERVER['REQUEST_URI'] = '/woohoo';
  17. $paged = get_pagenum_link( 2 );
  18. remove_filter( 'home_url', array( $this, '_get_pagenum_link_cb' ) );
  19. $this->assertSame( $paged, home_url( '/WooHoo/page/2/' ) );
  20. $_SERVER['REQUEST_URI'] = $old_req_uri;
  21. }
  22. function test_wp_get_shortlink() {
  23. $post_id = self::factory()->post->create();
  24. $post_id2 = self::factory()->post->create();
  25. // Basic case.
  26. $this->assertSame( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) );
  27. unset( $GLOBALS['post'] );
  28. // Global post is not set.
  29. $this->assertSame( '', wp_get_shortlink( 0, 'post' ) );
  30. $this->assertSame( '', wp_get_shortlink( 0 ) );
  31. $this->assertSame( '', wp_get_shortlink() );
  32. $GLOBALS['post'] = get_post( $post_id );
  33. // Global post is set.
  34. $this->assertSame( get_permalink( $post_id ), wp_get_shortlink( 0, 'post' ) );
  35. $this->assertSame( get_permalink( $post_id ), wp_get_shortlink( 0 ) );
  36. $this->assertSame( get_permalink( $post_id ), wp_get_shortlink() );
  37. // Not the global post.
  38. $this->assertSame( get_permalink( $post_id2 ), wp_get_shortlink( $post_id2, 'post' ) );
  39. unset( $GLOBALS['post'] );
  40. // Global post is not set, once again.
  41. $this->assertSame( '', wp_get_shortlink( 0, 'post' ) );
  42. $this->assertSame( '', wp_get_shortlink( 0 ) );
  43. $this->assertSame( '', wp_get_shortlink() );
  44. $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  45. // With a permalink structure set, get_permalink() will no longer match.
  46. $this->assertNotEquals( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) );
  47. $this->assertSame( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
  48. // Global post and permalink structure are set.
  49. $GLOBALS['post'] = get_post( $post_id );
  50. $this->assertSame( home_url( '?p=' . $post_id ), wp_get_shortlink( 0, 'post' ) );
  51. $this->assertSame( home_url( '?p=' . $post_id ), wp_get_shortlink( 0 ) );
  52. $this->assertSame( home_url( '?p=' . $post_id ), wp_get_shortlink() );
  53. }
  54. function test_wp_get_shortlink_with_page() {
  55. $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
  56. // Basic case.
  57. // Don't test against get_permalink() since it uses ?page_id= for pages.
  58. $this->assertSame( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
  59. $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  60. $this->assertSame( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
  61. }
  62. /**
  63. * @ticket 26871
  64. */
  65. function test_wp_get_shortlink_with_home_page() {
  66. $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
  67. update_option( 'show_on_front', 'page' );
  68. update_option( 'page_on_front', $post_id );
  69. $this->assertSame( home_url( '/' ), wp_get_shortlink( $post_id, 'post' ) );
  70. $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  71. $this->assertSame( home_url( '/' ), wp_get_shortlink( $post_id, 'post' ) );
  72. }
  73. /**
  74. * @ticket 30910
  75. */
  76. public function test_get_permalink_should_not_reveal_post_name_for_post_with_post_status_future() {
  77. update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
  78. flush_rewrite_rules();
  79. $p = self::factory()->post->create(
  80. array(
  81. 'post_status' => 'publish',
  82. 'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
  83. )
  84. );
  85. $non_pretty_permalink = add_query_arg( 'p', $p, trailingslashit( home_url() ) );
  86. $this->assertSame( $non_pretty_permalink, get_permalink( $p ) );
  87. }
  88. /**
  89. * @ticket 30910
  90. */
  91. public function test_get_permalink_should_not_reveal_post_name_for_cpt_with_post_status_future() {
  92. update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
  93. register_post_type( 'wptests_pt', array( 'public' => true ) );
  94. flush_rewrite_rules();
  95. $p = self::factory()->post->create(
  96. array(
  97. 'post_status' => 'future',
  98. 'post_type' => 'wptests_pt',
  99. 'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
  100. )
  101. );
  102. $non_pretty_permalink = add_query_arg(
  103. array(
  104. 'post_type' => 'wptests_pt',
  105. 'p' => $p,
  106. ),
  107. trailingslashit( home_url() )
  108. );
  109. $this->assertSame( $non_pretty_permalink, get_permalink( $p ) );
  110. }
  111. /**
  112. * @ticket 1914
  113. */
  114. public function test_unattached_attachment_has_a_pretty_permalink() {
  115. $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  116. $attachment_id = self::factory()->attachment->create_object(
  117. 'image.jpg',
  118. 0,
  119. array(
  120. 'post_mime_type' => 'image/jpeg',
  121. 'post_type' => 'attachment',
  122. 'post_title' => 'An Attachment!',
  123. 'post_status' => 'inherit',
  124. )
  125. );
  126. $attachment = get_post( $attachment_id );
  127. $this->assertSame( home_url( user_trailingslashit( $attachment->post_name ) ), get_permalink( $attachment_id ) );
  128. }
  129. /**
  130. * @ticket 1914
  131. */
  132. public function test_attachment_attached_to_non_existent_post_type_has_a_pretty_permalink() {
  133. global $wp_post_types;
  134. $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  135. register_post_type( 'not_a_post_type', array( 'public' => true ) );
  136. flush_rewrite_rules();
  137. $post_id = self::factory()->post->create( array( 'post_type' => 'not_a_post_type' ) );
  138. $attachment_id = self::factory()->attachment->create_object(
  139. 'image.jpg',
  140. $post_id,
  141. array(
  142. 'post_mime_type' => 'image/jpeg',
  143. 'post_type' => 'attachment',
  144. 'post_title' => 'An Attachment!',
  145. 'post_status' => 'inherit',
  146. )
  147. );
  148. $attachment = get_post( $attachment_id );
  149. $this->assertSame( get_permalink( $post_id ) . user_trailingslashit( $attachment->post_name ), get_permalink( $attachment_id ) );
  150. foreach ( $wp_post_types as $id => $pt ) {
  151. if ( 'not_a_post_type' === $pt->name ) {
  152. unset( $wp_post_types[ $id ] );
  153. break;
  154. }
  155. }
  156. $this->assertSame( home_url( "/?attachment_id={$attachment->ID}" ), get_permalink( $attachment_id ) );
  157. // Visit permalink.
  158. $this->go_to( get_permalink( $attachment_id ) );
  159. $this->assertQueryTrue( 'is_attachment', 'is_single', 'is_singular' );
  160. }
  161. }