getCommentsPagesCount.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Validate the logic of get_comments_pages_count
  4. *
  5. * @group comment
  6. */
  7. class Tests_Comment_GetCommentsPagesCount extends WP_UnitTestCase {
  8. protected $option_page_comments;
  9. protected $option_comments_per_page;
  10. protected $option_thread_comments;
  11. protected $option_posts_per_rss;
  12. /**
  13. * setUp options
  14. */
  15. function setUp() {
  16. parent::setUp();
  17. $this->option_page_comments = get_option( 'page_comments' );
  18. $this->option_page_comments = get_option( 'comments_per_page' );
  19. $this->option_page_comments = get_option( 'thread_comments' );
  20. $this->option_posts_per_rss = get_option( 'posts_per_rss' );
  21. update_option( 'page_comments', true );
  22. }
  23. /**
  24. * tearDown options
  25. */
  26. function tearDown() {
  27. update_option( 'page_comments', $this->option_page_comments );
  28. update_option( 'comments_per_page', $this->option_page_comments );
  29. update_option( 'thread_comments', $this->option_page_comments );
  30. update_option( 'posts_per_rss', $this->option_posts_per_rss );
  31. parent::tearDown();
  32. }
  33. /**
  34. * Validate get_comments_pages_count for empty comments
  35. */
  36. function test_empty() {
  37. // Setup post and comments.
  38. $post_id = self::factory()->post->create(
  39. array(
  40. 'post_title' => 'comment--post',
  41. 'post_type' => 'post',
  42. )
  43. );
  44. $this->go_to( '/?p=' . $post_id );
  45. global $wp_query;
  46. unset( $wp_query->comments );
  47. $comments = get_comments( array( 'post_id' => $post_id ) );
  48. $this->assertSame( 0, get_comment_pages_count( $comments, 10, false ) );
  49. $this->assertSame( 0, get_comment_pages_count( $comments, 1, false ) );
  50. $this->assertSame( 0, get_comment_pages_count( $comments, 0, false ) );
  51. $this->assertSame( 0, get_comment_pages_count( $comments, 10, true ) );
  52. $this->assertSame( 0, get_comment_pages_count( $comments, 5 ) );
  53. $this->assertSame( 0, get_comment_pages_count( $comments ) );
  54. $this->assertSame( 0, get_comment_pages_count( null, 1 ) );
  55. }
  56. /**
  57. * Validate get_comments_pages_count for treaded comments
  58. */
  59. function test_threaded_comments() {
  60. // Setup post and comments.
  61. $post = self::factory()->post->create_and_get(
  62. array(
  63. 'post_title' => 'comment--post',
  64. 'post_type' => 'post',
  65. )
  66. );
  67. $comments = self::factory()->comment->create_post_comments( $post->ID, 15 );
  68. self::factory()->comment->create_post_comments( $post->ID, 6, array( 'comment_parent' => $comments[0] ) );
  69. $comments = get_comments( array( 'post_id' => $post->ID ) );
  70. $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
  71. $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
  72. $this->assertEquals( 4, get_comment_pages_count( $comments, 4, true ) );
  73. }
  74. /**
  75. * Validate get_comments_pages_count for option tread_comments
  76. */
  77. function test_option_thread_comments() {
  78. // Setup post and comments.
  79. $post = self::factory()->post->create_and_get(
  80. array(
  81. 'post_title' => 'comment--post',
  82. 'post_type' => 'post',
  83. )
  84. );
  85. $comments = self::factory()->comment->create_post_comments( $post->ID, 15 );
  86. self::factory()->comment->create_post_comments( $post->ID, 6, array( 'comment_parent' => $comments[0] ) );
  87. $comments = get_comments( array( 'post_id' => $post->ID ) );
  88. update_option( 'thread_comments', false );
  89. $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
  90. $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
  91. $this->assertEquals( 3, get_comment_pages_count( $comments, 10, null ) );
  92. $this->assertEquals( 3, get_comment_pages_count( $comments, 10 ) );
  93. update_option( 'thread_comments', true );
  94. $this->assertEquals( 3, get_comment_pages_count( $comments, 10, false ) );
  95. $this->assertEquals( 2, get_comment_pages_count( $comments, 10, true ) );
  96. $this->assertEquals( 2, get_comment_pages_count( $comments, 10, null ) );
  97. $this->assertEquals( 2, get_comment_pages_count( $comments, 10 ) );
  98. }
  99. /**
  100. * Validate $wp_query logic of get_comment_pages_count
  101. */
  102. function test_wp_query_comments_per_page() {
  103. global $wp_query;
  104. update_option( 'posts_per_rss', 100 );
  105. $post = self::factory()->post->create_and_get(
  106. array(
  107. 'post_title' => 'comment-post',
  108. 'post_type' => 'post',
  109. )
  110. );
  111. $comments = self::factory()->comment->create_post_comments( $post->ID, 25 );
  112. $wp_query = new WP_Query(
  113. array(
  114. 'p' => $post->ID,
  115. 'comments_per_page' => 10,
  116. 'feed' => 'comments-',
  117. )
  118. );
  119. update_option( 'comments_per_page', 25 );
  120. $this->assertEquals( 3, get_comment_pages_count() );
  121. $this->assertEquals( 2, get_comment_pages_count( null, 20 ) );
  122. $wp_query = new WP_Query(
  123. array(
  124. 'p' => $post->ID,
  125. 'comments_per_page' => null,
  126. 'feed' => 'comments-',
  127. )
  128. );
  129. $this->assertEquals( 1, get_comment_pages_count() );
  130. $this->assertEquals( 5, get_comment_pages_count( null, 5 ) );
  131. $wp_query->query_vars['comments_per_page'] = null;
  132. update_option( 'comments_per_page', 5 );
  133. $this->assertEquals( 5, get_comment_pages_count() );
  134. $this->assertEquals( 3, get_comment_pages_count( null, 11 ) );
  135. $this->assertEquals( 5, get_comment_pages_count( null, 0 ) );
  136. }
  137. /**
  138. * Validate max_num_comment_pages logic of get_comment_pages_count
  139. */
  140. function test_max_num_comment_pages() {
  141. global $wp_query;
  142. $wp_query = new WP_Query();
  143. $org_max_num_comment_pages = $wp_query->max_num_comment_pages;
  144. $wp_query->max_num_comment_pages = 7;
  145. $this->assertEquals( 7, get_comment_pages_count() );
  146. $this->assertEquals( 7, get_comment_pages_count( null, null, null ) );
  147. $this->assertEquals( 0, get_comment_pages_count( array(), null, null ) );
  148. $wp_query->max_num_comment_pages = $org_max_num_comment_pages;
  149. }
  150. }