dateQuery.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 ] = self::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 = self::factory()->comment->create(
  37. array(
  38. 'comment_date' => $comment_date,
  39. 'comment_post_ID' => $this->posts[ $comment_parent ],
  40. )
  41. );
  42. }
  43. }
  44. public function _get_query_result( $args = array() ) {
  45. $args = wp_parse_args(
  46. $args,
  47. array(
  48. 'post_id' => $this->posts[1],
  49. 'orderby' => 'comment_ID', // Same order they were created.
  50. 'order' => 'ASC',
  51. )
  52. );
  53. return get_comments( $args );
  54. }
  55. public function test_year() {
  56. $comments = $this->_get_query_result(
  57. array(
  58. 'date_query' => array(
  59. array(
  60. 'year' => 2008,
  61. ),
  62. ),
  63. )
  64. );
  65. $expected_dates = array(
  66. '2008-03-29 09:04:25',
  67. '2008-12-10 13:06:27',
  68. );
  69. $this->assertSame( $expected_dates, wp_list_pluck( $comments, 'comment_date' ) );
  70. }
  71. }