dateQuery.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Tests to make sure querying posts based on various date parameters
  4. * using "date_query" works as expected.
  5. *
  6. * No need to do a full repeat of all of the post tests again since
  7. * the query SQL is the same for both just with a different column.
  8. *
  9. * @ticket 18694
  10. *
  11. * @group comment
  12. * @group date
  13. * @group datequery
  14. */
  15. class Tests_Comment_DateQuery extends WP_UnitTestCase {
  16. public $posts = array();
  17. public function setUp() {
  18. parent::setUp();
  19. // Just some dummy posts to use as parents for comments
  20. for ( $i = 1; $i <= 2; $i++ ) {
  21. $this->posts[$i] = $this->factory->post->create();
  22. }
  23. // Be careful modifying this. Tests are coded to expect this exact sample data.
  24. // Format is 'datetime' => 'post number (not ID)'
  25. $comment_dates = array(
  26. '2007-01-22 03:49:21' => 1,
  27. '2007-05-16 17:32:22' => 1,
  28. '2007-09-24 07:17:23' => 1,
  29. '2008-03-29 09:04:25' => 1,
  30. '2008-07-15 11:32:26' => 2, // This one should never be in the results
  31. '2008-12-10 13:06:27' => 1,
  32. '2009-06-11 21:30:28' => 1,
  33. '2009-12-18 10:42:29' => 1,
  34. );
  35. foreach ( $comment_dates as $comment_date => $comment_parent ) {
  36. $result = $this->factory->comment->create( array(
  37. 'comment_date' => $comment_date,
  38. 'comment_post_ID' => $this->posts[ $comment_parent ],
  39. ) );
  40. }
  41. }
  42. public function _get_query_result( $args = array() ) {
  43. $args = wp_parse_args( $args, array(
  44. 'post_id' => $this->posts[1],
  45. 'orderby' => 'comment_ID', // Same order they were created
  46. 'order' => 'ASC',
  47. ) );
  48. return get_comments( $args );
  49. }
  50. public function test_year() {
  51. $comments = $this->_get_query_result( array(
  52. 'date_query' => array(
  53. array(
  54. 'year' => 2008,
  55. ),
  56. ),
  57. ) );
  58. $expected_dates = array(
  59. '2008-03-29 09:04:25',
  60. '2008-12-10 13:06:27',
  61. );
  62. $this->assertEquals( $expected_dates, wp_list_pluck( $comments, 'comment_date' ) );
  63. }
  64. }