link.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. global $wp_rewrite;
  15. $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
  16. $wp_rewrite->flush_rules();
  17. add_filter( 'home_url', array( $this, '_get_pagenum_link_cb' ) );
  18. $_SERVER['REQUEST_URI'] = '/woohoo';
  19. $paged = get_pagenum_link( 2 );
  20. remove_filter( 'home_url', array( $this, '_get_pagenum_link_cb' ) );
  21. $this->assertEquals( $paged, home_url( '/WooHoo/page/2/' ) );
  22. $_SERVER['REQUEST_URI'] = $old_req_uri;
  23. }
  24. function test_wp_get_shortlink() {
  25. $post_id = $this->factory->post->create();
  26. $post_id2 = $this->factory->post->create();
  27. // Basic case
  28. $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) );
  29. unset( $GLOBALS['post'] );
  30. // Global post is not set
  31. $this->assertEquals( '', wp_get_shortlink( 0, 'post' ) );
  32. $this->assertEquals( '', wp_get_shortlink( 0 ) );
  33. $this->assertEquals( '', wp_get_shortlink() );
  34. $GLOBALS['post'] = get_post( $post_id );
  35. // Global post is set
  36. $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink( 0, 'post' ) );
  37. $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink( 0 ) );
  38. $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink() );
  39. // Not the global post
  40. $this->assertEquals( get_permalink( $post_id2 ), wp_get_shortlink( $post_id2, 'post' ) );
  41. unset( $GLOBALS['post'] );
  42. // Global post is not set, once again
  43. $this->assertEquals( '', wp_get_shortlink( 0, 'post' ) );
  44. $this->assertEquals( '', wp_get_shortlink( 0 ) );
  45. $this->assertEquals( '', wp_get_shortlink() );
  46. global $wp_rewrite;
  47. $wp_rewrite->permalink_structure = '';
  48. $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  49. $wp_rewrite->flush_rules();
  50. // With a permalink structure set, get_permalink() will no longer match.
  51. $this->assertNotEquals( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) );
  52. $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
  53. // Global post and permalink structure are set
  54. $GLOBALS['post'] = get_post( $post_id );
  55. $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( 0, 'post' ) );
  56. $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( 0 ) );
  57. $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink() );
  58. $wp_rewrite->set_permalink_structure( '' );
  59. $wp_rewrite->flush_rules();
  60. }
  61. function test_wp_get_shortlink_with_page() {
  62. $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
  63. // Basic case
  64. // Don't test against get_permalink() since it uses ?page_id= for pages.
  65. $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
  66. global $wp_rewrite;
  67. $wp_rewrite->permalink_structure = '';
  68. $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  69. $wp_rewrite->flush_rules();
  70. $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
  71. $wp_rewrite->set_permalink_structure( '' );
  72. $wp_rewrite->flush_rules();
  73. }
  74. /**
  75. * @ticket 17807
  76. */
  77. function test_get_adjacent_post() {
  78. // Need some sample posts to test adjacency
  79. $post_one = $this->factory->post->create_and_get( array(
  80. 'post_title' => 'First',
  81. 'post_date' => '2012-01-01 12:00:00'
  82. ) );
  83. $post_two = $this->factory->post->create_and_get( array(
  84. 'post_title' => 'Second',
  85. 'post_date' => '2012-02-01 12:00:00'
  86. ) );
  87. $post_three = $this->factory->post->create_and_get( array(
  88. 'post_title' => 'Third',
  89. 'post_date' => '2012-03-01 12:00:00'
  90. ) );
  91. $post_four = $this->factory->post->create_and_get( array(
  92. 'post_title' => 'Fourth',
  93. 'post_date' => '2012-04-01 12:00:00'
  94. ) );
  95. // Assign some terms
  96. wp_set_object_terms( $post_one->ID, 'wordpress', 'category', false );
  97. wp_set_object_terms( $post_three->ID, 'wordpress', 'category', false );
  98. wp_set_object_terms( $post_two->ID, 'plugins', 'post_tag', false );
  99. wp_set_object_terms( $post_four->ID, 'plugins', 'post_tag', false );
  100. // Test normal post adjacency
  101. $this->go_to( get_permalink( $post_two->ID ) );
  102. $this->assertEquals( $post_one, get_adjacent_post( false, '', true ) );
  103. $this->assertEquals( $post_three, get_adjacent_post( false, '', false ) );
  104. $this->assertNotEquals( $post_two, get_adjacent_post( false, '', true ) );
  105. $this->assertNotEquals( $post_two, get_adjacent_post( false, '', false ) );
  106. // Test category adjacency
  107. $this->go_to( get_permalink( $post_one->ID ) );
  108. $this->assertEquals( '', get_adjacent_post( true, '', true, 'category' ) );
  109. $this->assertEquals( $post_three, get_adjacent_post( true, '', false, 'category' ) );
  110. // Test tag adjacency
  111. $this->go_to( get_permalink( $post_two->ID ) );
  112. $this->assertEquals( '', get_adjacent_post( true, '', true, 'post_tag' ) );
  113. $this->assertEquals( $post_four, get_adjacent_post( true, '', false, 'post_tag' ) );
  114. // Test normal boundary post
  115. $this->go_to( get_permalink( $post_two->ID ) );
  116. $this->assertEquals( array( $post_one ), get_boundary_post( false, '', true ) );
  117. $this->assertEquals( array( $post_four ), get_boundary_post( false, '', false ) );
  118. // Test category boundary post
  119. $this->go_to( get_permalink( $post_one->ID ) );
  120. $this->assertEquals( array( $post_one ), get_boundary_post( true, '', true, 'category' ) );
  121. $this->assertEquals( array( $post_three ), get_boundary_post( true, '', false, 'category' ) );
  122. // Test tag boundary post
  123. $this->go_to( get_permalink( $post_two->ID ) );
  124. $this->assertEquals( array( $post_two ), get_boundary_post( true, '', true, 'post_tag' ) );
  125. $this->assertEquals( array( $post_four ), get_boundary_post( true, '', false, 'post_tag' ) );
  126. }
  127. }