query.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. // Test the output of Comment Querying functions
  3. /**
  4. * @group comment
  5. */
  6. class Tests_Comment_Query extends WP_UnitTestCase {
  7. protected $post_id;
  8. protected $comment_id;
  9. function setUp() {
  10. parent::setUp();
  11. $this->post_id = $this->factory->post->create();
  12. }
  13. /**
  14. * @ticket 21101
  15. */
  16. function test_get_comment_comment_approved_0() {
  17. $comment_id = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id ) );
  18. $comments_approved_0 = get_comments( array( 'status' => 'hold' ) );
  19. $this->assertEquals( 0, count( $comments_approved_0 ) );
  20. }
  21. /**
  22. * @ticket 21101
  23. */
  24. function test_get_comment_comment_approved_1() {
  25. $comment_id = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id ) );
  26. $comments_approved_1 = get_comments( array( 'status' => 'approve' ) );
  27. $this->assertEquals( 1, count( $comments_approved_1 ) );
  28. $result = $comments_approved_1[0];
  29. $this->assertEquals( $comment_id, $result->comment_ID );
  30. }
  31. function test_get_comments_for_post() {
  32. $post_id = $this->factory->post->create();
  33. $this->factory->comment->create_post_comments( $post_id, 10 );
  34. $comments = get_comments( array( 'post_id' => $post_id ) );
  35. $this->assertEquals( 10, count( $comments ) );
  36. foreach ( $comments as $comment ) {
  37. $this->assertEquals( $post_id, $comment->comment_post_ID );
  38. }
  39. $post_id2 = $this->factory->post->create();
  40. $this->factory->comment->create_post_comments( $post_id2, 10 );
  41. $comments = get_comments( array( 'post_id' => $post_id2 ) );
  42. $this->assertEquals( 10, count( $comments ) );
  43. foreach ( $comments as $comment ) {
  44. $this->assertEquals( $post_id2, $comment->comment_post_ID );
  45. }
  46. $post_id3 = $this->factory->post->create();
  47. $this->factory->comment->create_post_comments( $post_id3, 10, array( 'comment_approved' => '0' ) );
  48. $comments = get_comments( array( 'post_id' => $post_id3 ) );
  49. $this->assertEquals( 10, count( $comments ) );
  50. foreach ( $comments as $comment ) {
  51. $this->assertEquals( $post_id3, $comment->comment_post_ID );
  52. }
  53. $comments = get_comments( array( 'post_id' => $post_id3, 'status' => 'hold' ) );
  54. $this->assertEquals( 10, count( $comments ) );
  55. foreach ( $comments as $comment ) {
  56. $this->assertEquals( $post_id3, $comment->comment_post_ID );
  57. }
  58. $comments = get_comments( array( 'post_id' => $post_id3, 'status' => 'approve' ) );
  59. $this->assertEquals( 0, count( $comments ) );
  60. $this->factory->comment->create_post_comments( $post_id3, 10, array( 'comment_approved' => '1' ) );
  61. $comments = get_comments( array( 'post_id' => $post_id3 ) );
  62. $this->assertEquals( 20, count( $comments ) );
  63. foreach ( $comments as $comment ) {
  64. $this->assertEquals( $post_id3, $comment->comment_post_ID );
  65. }
  66. }
  67. /**
  68. * @ticket 21003
  69. */
  70. function test_orderby_meta() {
  71. $comment_id = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id ) );
  72. $comment_id2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id ) );
  73. $comment_id3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id ) );
  74. add_comment_meta( $comment_id, 'key', 'value1', true );
  75. add_comment_meta( $comment_id, 'key1', 'value1', true );
  76. add_comment_meta( $comment_id, 'key3', 'value3', true );
  77. add_comment_meta( $comment_id2, 'key', 'value2', true );
  78. add_comment_meta( $comment_id2, 'key2', 'value2', true );
  79. add_comment_meta( $comment_id3, 'key3', 'value3', true );
  80. $comments = get_comments( array( 'meta_key' => 'key', 'orderby' => array( 'key' ) ) );
  81. $this->assertEquals( 2, count( $comments ) );
  82. $this->assertEquals( $comment_id2, $comments[0]->comment_ID );
  83. $this->assertEquals( $comment_id, $comments[1]->comment_ID );
  84. $comments = get_comments( array( 'meta_key' => 'key', 'orderby' => array( 'meta_value' ) ) );
  85. $this->assertEquals( 2, count( $comments ) );
  86. $this->assertEquals( $comment_id2, $comments[0]->comment_ID );
  87. $this->assertEquals( $comment_id, $comments[1]->comment_ID );
  88. $comments = get_comments( array( 'meta_key' => 'key', 'orderby' => array( 'key' ), 'order' => 'ASC' ) );
  89. $this->assertEquals( 2, count( $comments ) );
  90. $this->assertEquals( $comment_id, $comments[0]->comment_ID );
  91. $this->assertEquals( $comment_id2, $comments[1]->comment_ID );
  92. $comments = get_comments( array( 'meta_key' => 'key', 'orderby' => array( 'meta_value' ), 'order' => 'ASC' ) );
  93. $this->assertEquals( 2, count( $comments ) );
  94. $this->assertEquals( $comment_id, $comments[0]->comment_ID );
  95. $this->assertEquals( $comment_id2, $comments[1]->comment_ID );
  96. $comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'key' ) ) );
  97. $this->assertEquals( 2, count( $comments ) );
  98. $this->assertEquals( $comment_id, $comments[0]->comment_ID );
  99. $this->assertEquals( $comment_id3, $comments[1]->comment_ID );
  100. $comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'meta_value' ) ) );
  101. $this->assertEquals( 2, count( $comments ) );
  102. $this->assertEquals( $comment_id, $comments[0]->comment_ID );
  103. $this->assertEquals( $comment_id3, $comments[1]->comment_ID );
  104. // value1 is present on two different keys for $comment_id yet we should get only one instance
  105. // of that comment in the results
  106. $comments = get_comments( array( 'meta_value' => 'value1', 'orderby' => array( 'key' ) ) );
  107. $this->assertEquals( 1, count( $comments ) );
  108. $comments = get_comments( array( 'meta_value' => 'value1', 'orderby' => array( 'meta_value' ) ) );
  109. $this->assertEquals( 1, count( $comments ) );
  110. }
  111. function test_get_status() {
  112. $post_id = $this->factory->post->create();
  113. $this->factory->comment->create_post_comments( $post_id, 10 );
  114. $post_id2 = $this->factory->post->create();
  115. $this->factory->comment->create_post_comments( $post_id2, 10 );
  116. $post_id3 = $this->factory->post->create();
  117. $this->factory->comment->create_post_comments( $post_id3, 10, array( 'comment_approved' => '0' ) );
  118. $post_id4 = $this->factory->post->create();
  119. $this->factory->comment->create_post_comments( $post_id4, 10, array( 'comment_approved' => 'trash' ) );
  120. $post_id5 = $this->factory->post->create();
  121. $this->factory->comment->create_post_comments( $post_id5, 10, array( 'comment_approved' => 'spam' ) );
  122. $this->assertEquals( 30, count( get_comments() ) );
  123. $this->assertEquals( 30, count( get_comments( array( 'status' => 'all' ) ) ) );
  124. $this->assertEquals( 20, count( get_comments( array( 'status' => 'approve' ) ) ) );
  125. $this->assertEquals( 10, count( get_comments( array( 'status' => 'hold' ) ) ) );
  126. $this->assertEquals( 10, count( get_comments( array( 'status' => 'trash' ) ) ) );
  127. $this->assertEquals( 10, count( get_comments( array( 'status' => 'spam' ) ) ) );
  128. }
  129. }