Ticket #18694: 18694.5.patch
File 18694.5.patch, 43.3 KB (added by , 11 years ago) |
---|
-
src/wp-includes/comment.php
197 197 var $meta_query = false; 198 198 199 199 /** 200 * Date query container 201 * 202 * @since 3.7.0 203 * @access public 204 * @var object WP_Date_Query 205 */ 206 var $date_query = false; 207 208 /** 200 209 * Execute the query 201 210 * 202 211 * @since 3.1.0 … … 231 240 'meta_key' => '', 232 241 'meta_value' => '', 233 242 'meta_query' => '', 243 'date_query' => null, // See WP_Date_Query 234 244 ); 235 245 236 246 $groupby = ''; … … 360 370 $groupby = "{$wpdb->comments}.comment_ID"; 361 371 } 362 372 373 if ( ! empty( $date_query ) && is_array( $date_query ) ) { 374 $date_query_object = new WP_Date_Query( $date_query, 'comment_date' ); 375 $where .= $date_query_object->get_sql(); 376 } 377 363 378 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' ); 364 379 $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); 365 380 foreach ( $pieces as $piece ) -
src/wp-includes/query.php
867 867 var $meta_query = false; 868 868 869 869 /** 870 * Date query container 871 * 872 * @since 3.7.0 873 * @access public 874 * @var object WP_Date_Query 875 */ 876 var $date_query = false; 877 878 /** 870 879 * Holds the data for a single object that is queried. 871 880 * 872 881 * Holds the contents of a post, page, category, attachment. … … 1445 1454 $qv['monthnum'] = absint($qv['monthnum']); 1446 1455 $qv['day'] = absint($qv['day']); 1447 1456 $qv['w'] = absint($qv['w']); 1448 $qv['m'] = absint($qv['m']);1457 $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] ); 1449 1458 $qv['paged'] = absint($qv['paged']); 1450 1459 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers 1451 1460 $qv['pagename'] = trim( $qv['pagename'] ); … … 2045 2054 if ( '' !== $q['menu_order'] ) 2046 2055 $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order']; 2047 2056 2048 // If a month is specified in the querystring, load that month2057 // The "m" parameter is meant for months but accepts datetimes of varying specificity 2049 2058 if ( $q['m'] ) { 2050 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']); 2051 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4); 2052 if ( strlen($q['m']) > 5 ) 2053 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2); 2054 if ( strlen($q['m']) > 7 ) 2055 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2); 2056 if ( strlen($q['m']) > 9 ) 2057 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2); 2058 if ( strlen($q['m']) > 11 ) 2059 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2); 2060 if ( strlen($q['m']) > 13 ) 2061 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2); 2059 $m_strlen = strlen( $q['m'] ); 2060 $m_parameters = array(); 2061 2062 if ( $m_strlen > 3 ) 2063 $m_parameters['year'] = substr( $q['m'], 0, 4 ); 2064 2065 if ( $m_strlen > 5 ) 2066 $m_parameters['monthnum'] = substr( $q['m'], 4, 2 ); 2067 2068 if ( $m_strlen > 7 ) 2069 $m_parameters['day'] = substr( $q['m'], 6, 2 ); 2070 2071 if ( $m_strlen > 9 ) 2072 $m_parameters['hour'] = substr( $q['m'], 8, 2 ); 2073 2074 if ( $m_strlen > 11 ) 2075 $m_parameters['minute'] = substr( $q['m'], 10, 2 ); 2076 2077 if ( $m_strlen > 13 ) 2078 $m_parameters['second'] = substr( $q['m'], 12, 2 ); 2079 2080 if ( $m_parameters ) { 2081 $date_query = new WP_Date_Query( array( $m_parameters ) ); 2082 $where .= $date_query->get_sql(); 2083 } 2084 unset( $m_parameters, $date_query ); 2062 2085 } 2063 2086 2087 // Handle the other individual date parameters 2088 $date_parameters = array(); 2089 2064 2090 if ( '' !== $q['hour'] ) 2065 $ where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";2091 $date_parameters['hour'] = $q['hour']; 2066 2092 2067 2093 if ( '' !== $q['minute'] ) 2068 $ where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";2094 $date_parameters['minute'] = $q['minute']; 2069 2095 2070 2096 if ( '' !== $q['second'] ) 2071 $ where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";2097 $date_parameters['second'] = $q['second']; 2072 2098 2073 2099 if ( $q['year'] ) 2074 $ where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";2100 $date_parameters['year'] = $q['year']; 2075 2101 2076 2102 if ( $q['monthnum'] ) 2077 $ where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";2103 $date_parameters['monthnum'] = $q['monthnum']; 2078 2104 2105 if ( $q['w'] ) 2106 $date_parameters['week'] = $q['w']; 2107 2079 2108 if ( $q['day'] ) 2080 $ where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";2109 $date_parameters['day'] = $q['day']; 2081 2110 2111 if ( $date_parameters ) { 2112 $date_query = new WP_Date_Query( array( $date_parameters ) ); 2113 $where .= $date_query->get_sql(); 2114 } 2115 unset( $date_parameters, $date_query ); 2116 2117 // Handle complex date queries 2118 if ( ! empty( $q['date_query'] ) ) { 2119 $this->date_query = new WP_Date_Query( $q['date_query'] ); 2120 $where .= $this->date_query->get_sql(); 2121 } 2122 2123 2082 2124 // If we've got a post_type AND it's not "any" post_type. 2083 2125 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) { 2084 2126 foreach ( (array)$q['post_type'] as $_post_type ) { … … 2147 2189 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'"; 2148 2190 } 2149 2191 2150 if ( $q['w'] )2151 $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";2152 2192 2153 2193 if ( intval($q['comments_popup']) ) 2154 2194 $q['p'] = absint($q['comments_popup']); … … 3571 3611 } 3572 3612 3573 3613 /** 3614 * WP_Date_Query will generate a MySQL WHERE clause for the specified date-based parameters. 3615 * 3616 * Initialize the class by passing an array of arrays of parameters. Example: 3617 * 3618 * $date_query = new WP_Date_Query( array( 3619 * 'column' => 'optional, column to query against, default is post_date', 3620 * 'compare' => 'optional, see WP_Date_Query::get_compare()', 3621 * 'relation' => 'optional, OR or AND, how the sub-arrays should be compared, default is AND', 3622 * array( 3623 * 'column' => 'see above', 3624 * 'compare' => 'see above', 3625 * 'after' => 'string or array, see WP_Date_Query::build_mysql_datetime()', 3626 * 'before' => 'string or array, see WP_Date_Query::build_mysql_datetime()', 3627 * 'inclusive' => 'boolean, for after/before, whether exact value should be matched or not', 3628 * 'year' => '4 digit int', 3629 * 'month' => 'int, 1-12', 3630 * 'week' => 'int, 0-53', 3631 * 'day' => 'int, 1-31', 3632 * 'hour' => 'int, 0-23', 3633 * 'minute' => 'int, 0-60', 3634 * 'second' => 'int, 0-60', 3635 * ), 3636 * array( 3637 * ... 3638 * ), 3639 * ... 3640 * ) ); 3641 * 3642 * Then call the get_sql() method to get the MySQL WHERE string: 3643 * 3644 * $where .= $date_query->get_sql(); 3645 * 3646 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page. 3647 * 3648 * @since 3.7.0 3649 */ 3650 class WP_Date_Query { 3651 /** 3652 * List of date queries. 3653 * 3654 * @since 3.7.0 3655 * @access public 3656 * @var array 3657 */ 3658 public $queries = array(); 3659 3660 /** 3661 * The relation between the queries. Can be either 'AND' or 'OR' and can be changed via the query arguments. 3662 * 3663 * @since 3.7.0 3664 * @access public 3665 * @var string 3666 */ 3667 public $relation = 'AND'; 3668 3669 /** 3670 * The column to query against. Can be changed via the query arguments. 3671 * 3672 * @since 3.7.0 3673 * @access public 3674 * @var string 3675 */ 3676 public $column = 'post_date'; 3677 3678 /** 3679 * The value comparison operator. Can be changed via the query arguments. 3680 * 3681 * @since 3.7.0 3682 * @access public 3683 * @var array 3684 */ 3685 public $compare = '='; 3686 3687 /** 3688 * Constructor 3689 * 3690 * @param array $date_query A date query parameter array, see class descriptor for further details. 3691 * @param array (optional) $default_column What column name to query against. Defaults to "post_date". 3692 */ 3693 function __construct( $date_query, $default_column = 'post_date' ) { 3694 if ( empty( $date_query ) || ! is_array( $date_query ) ) 3695 return; 3696 3697 if ( isset( $date_query['relation'] ) && strtoupper( $date_query['relation'] ) == 'OR' ) { 3698 $this->relation = 'OR'; 3699 } else { 3700 $this->relation = 'AND'; 3701 } 3702 3703 if ( ! empty( $date_query['column'] ) ) 3704 $this->column = esc_sql( $date_query['column'] ); 3705 else 3706 $this->column = esc_sql( $default_column ); 3707 3708 $this->column = $this->validate_column( $this->column ); 3709 3710 $this->compare = $this->get_compare( $date_query ); 3711 3712 // If an array of arrays wasn't passed, fix it 3713 if ( ! isset( $date_query[0] ) ) 3714 $date_query = array( $date_query ); 3715 3716 $this->queries = array(); 3717 foreach ( $date_query as $key => $query ) { 3718 if ( ! is_array( $query ) ) 3719 continue; 3720 3721 $this->queries[$key] = $query; 3722 } 3723 } 3724 3725 /** 3726 * Determines and validates what comparison operator to use. 3727 * 3728 * @since 3.7.0 3729 * @access public 3730 * 3731 * @param array $query A date query or a date subquery 3732 * @return string The comparison operator 3733 */ 3734 public function get_compare( $query ) { 3735 if ( ! empty( $query['compare'] ) && in_array( $query['compare'], array( '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) 3736 return strtoupper( $query['compare'] ); 3737 3738 return $this->compare; 3739 } 3740 3741 /** 3742 * Validates a column name parameter. 3743 * 3744 * @since 3.7.0 3745 * @access public 3746 * 3747 * @param string $column The user-supplied column name. 3748 * @return string A validated column name value. 3749 */ 3750 public function validate_column( $column ) { 3751 if ( ! in_array( $column, apply_filters( 'date_query_valid_columns', array( 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt', 'comment_date', 'comment_date_gmt' ) ) ) ) 3752 $column = 'post_date'; 3753 3754 return $column; 3755 } 3756 3757 /** 3758 * Turns an array of date query parameters into a MySQL string. 3759 * 3760 * @since 3.7.0 3761 * @access public 3762 * 3763 * @return string MySQL WHERE parameters 3764 */ 3765 public function get_sql() { 3766 global $wpdb; 3767 3768 // The parts of the final query 3769 $where = array(); 3770 3771 foreach ( $this->queries as $key => $query ) { 3772 3773 // The sub-parts of a $where part 3774 $where_parts = array(); 3775 3776 $column = ( ! empty( $query['column'] ) ) ? esc_sql( $query['column'] ) : $this->column; 3777 3778 $column = $this->validate_column( $column ); 3779 3780 $compare = $this->get_compare( $query ); 3781 3782 $lt = '<'; 3783 $gt = '>'; 3784 if ( ! empty( $query['inclusive'] ) ) { 3785 $lt .= '='; 3786 $gt .= '='; 3787 } 3788 3789 // Range queries 3790 if ( ! empty( $query['after'] ) ) 3791 $where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], true ) ); 3792 3793 if ( ! empty( $query['before'] ) ) 3794 $where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], false ) ); 3795 3796 // Specific value queries 3797 3798 if ( isset( $query['year'] ) && $value = $this->build_value( $compare, $query['year'] ) ) 3799 $where_parts[] = "YEAR( $column ) $compare $value"; 3800 3801 if ( isset( $query['month'] ) && $value = $this->build_value( $compare, $query['month'] ) ) 3802 $where_parts[] = "MONTH( $column ) $compare $value"; 3803 3804 // Legacy 3805 if ( isset( $query['monthnum'] ) && $value = $this->build_value( $compare, $query['monthnum'] ) ) 3806 $where_parts[] = "MONTH( $column ) $compare $value"; 3807 3808 if ( isset( $query['week'] ) && false !== ( $value = $this->build_value( $compare, $query['week'] ) ) ) 3809 $where_parts[] = _wp_mysql_week( $column ) . " $compare $value"; 3810 3811 // Legacy 3812 if ( isset( $query['w'] ) && false !== ( $value = $this->build_value( $compare, $query['w'] ) ) ) 3813 $where_parts[] = _wp_mysql_week( $column ) . " $compare $value"; 3814 3815 if ( isset( $query['dayofyear'] ) && $value = $this->build_value( $compare, $query['dayofyear'] ) ) 3816 $where_parts[] = "DAYOFYEAR( $column ) $compare $value"; 3817 3818 if ( isset( $query['day'] ) && $value = $this->build_value( $compare, $query['day'] ) ) 3819 $where_parts[] = "DAYOFMONTH( $column ) $compare $value"; 3820 3821 if ( isset( $query['dayofweek'] ) && $value = $this->build_value( $compare, $query['dayofweek'] ) ) 3822 $where_parts[] = "DAYOFWEEK( $column ) $compare $value"; 3823 3824 if ( isset( $query['hour'] ) || isset( $query['minute'] ) || isset( $query['second'] ) ) { 3825 // Avoid notices 3826 foreach ( array( 'hour', 'minute', 'second' ) as $unit ) { 3827 if ( ! isset( $query[$unit] ) ) { 3828 $query[$unit] = null; 3829 } 3830 } 3831 3832 if ( $time_query = $this->build_time_query( $column, $compare, $query['hour'], $query['minute'], $query['second'] ) ) { 3833 $where_parts[] = $time_query; 3834 } 3835 } 3836 3837 // Combine the parts of this subquery into a single string 3838 if ( ! empty( $where_parts ) ) 3839 $where[$key] = '( ' . implode( ' AND ', $where_parts ) . ' )'; 3840 } 3841 3842 // Combine the subquery strings into a single string 3843 if ( ! empty( $where ) ) 3844 $where = ' AND ( ' . implode( " {$this->relation} ", $where ) . ' )'; 3845 else 3846 $where = ''; 3847 3848 return apply_filters( 'get_date_sql', $where, $this ); 3849 } 3850 3851 /** 3852 * Builds and validates a value string based on the comparison operator. 3853 * 3854 * @since 3.7.0 3855 * @access public 3856 * 3857 * @param string $compare The compare operator to use 3858 * @param string|array $value The value 3859 * @return string|int|false The value to be used in SQL or false on error. 3860 */ 3861 public function build_value( $compare, $value ) { 3862 if ( ! isset( $value ) ) 3863 return false; 3864 3865 switch ( $compare ) { 3866 case 'IN': 3867 case 'NOT IN': 3868 return '(' . implode( ',', array_map( 'intval', (array) $value ) ) . ')'; 3869 3870 case 'BETWEEN': 3871 case 'NOT BETWEEN': 3872 if ( ! is_array( $value ) || 2 != count( $value ) || ! isset( $value[0] ) || ! isset( $value[1] ) ) 3873 $value = array( $value, $value ); 3874 3875 $value = array_map( 'intval', $value ); 3876 3877 return $value[0] . ' AND ' . $value[1]; 3878 3879 default; 3880 return (int) $value; 3881 } 3882 } 3883 3884 /** 3885 * Builds a MySQL format date/time based on some query parameters. 3886 * 3887 * You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to 3888 * either the maximum or minimum values (controlled by the $default_to parameter). Alternatively you can 3889 * pass a string that that will be run through strtotime(). 3890 * 3891 * @since 3.7.0 3892 * @access public 3893 * 3894 * @param string|array $datetime An array of parameters or a strotime() string 3895 * @param string $default_to Controls what values default to if they are missing from $datetime. Pass "min" or "max". 3896 * @return string|false A MySQL format date/time or false on failure 3897 */ 3898 public function build_mysql_datetime( $datetime, $default_to_max = false ) { 3899 $now = current_time( 'timestamp' ); 3900 3901 if ( is_array( $datetime ) ) { 3902 $datetime = array_map( 'absint', $datetime ); 3903 3904 if ( ! isset( $datetime['year'] ) ) 3905 $datetime['year'] = gmdate( 'Y', $now ); 3906 3907 if ( ! isset( $datetime['month'] ) ) 3908 $datetime['month'] = ( $default_to_max ) ? 12 : 1; 3909 3910 if ( ! isset( $datetime['day'] ) ) 3911 $datetime['day'] = ( $default_to_max ) ? (int) date( 't', mktime( 0, 0, 0, $datetime['month'], 1, $datetime['year'] ) ) : 1; 3912 3913 if ( ! isset( $datetime['hour'] ) ) 3914 $datetime['hour'] = ( $default_to_max ) ? 23 : 0; 3915 3916 if ( ! isset( $datetime['minute'] ) ) 3917 $datetime['minute'] = ( $default_to_max ) ? 59 : 0; 3918 3919 if ( ! isset( $datetime['second'] ) ) 3920 $datetime['second'] = ( $default_to_max ) ? 59 : 0; 3921 3922 return sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $datetime['year'], $datetime['month'], $datetime['day'], $datetime['hour'], $datetime['minute'], $datetime['second'] ); 3923 } else { 3924 // Timezone issues here possibly 3925 return gmdate( 'Y-m-d H:i:s', strtotime( $datetime, $now ) ); 3926 } 3927 3928 return false; 3929 } 3930 3931 /** 3932 * Builds a query string for comparing time values (hour, minute, second). 3933 * 3934 * If just hour, minute, or second is set than a normal comparison will be done. 3935 * However if multiple values are passed, a pseudo-decimal time will be created 3936 * in order to be able to accurately compare against. 3937 * 3938 * @since 3.7.0 3939 * @access public 3940 * 3941 * @param string $column The column to query against. Needs to be pre-validated! 3942 * @param string $compare The comparison operator. Needs to be pre-validated! 3943 * @param int|null $hour Optional. An hour value (0-23). 3944 * @param int|null $minute Optional. A minute value (0-59). 3945 * @param int|null $second Optional. A second value (0-59). 3946 * @return string|false A query part or false on failure. 3947 */ 3948 public function build_time_query( $column, $compare, $hour = null, $minute = null, $second = null ) { 3949 global $wpdb; 3950 3951 // Have to have at least one 3952 if ( ! isset( $hour ) && ! isset( $minute ) && ! isset( $second ) ) 3953 return false; 3954 3955 // Complex combined queries aren't supported for multi-value queries 3956 if ( in_array( $compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 3957 $return = array(); 3958 3959 if ( isset( $hour ) && false !== ( $value = $this->build_value( $compare, $hour ) ) ) 3960 $return[] = "HOUR( $column ) $compare $value"; 3961 3962 if ( isset( $minute ) && false !== ( $value = $this->build_value( $compare, $minute ) ) ) 3963 $return[] = "MINUTE( $column ) $compare $value"; 3964 3965 if ( isset( $second ) && false !== ( $value = $this->build_value( $compare, $second ) ) ) 3966 $return[] = "SECOND( $column ) $compare $value"; 3967 3968 return implode( ' AND ', $return ); 3969 } 3970 3971 // Cases where just one unit is set 3972 if ( isset( $hour ) && ! isset( $minute ) && ! isset( $second ) && false !== ( $value = $this->build_value( $compare, $hour ) ) ) { 3973 return "HOUR( $column ) $compare $value"; 3974 } elseif ( ! isset( $hour ) && isset( $minute ) && ! isset( $second ) && false !== ( $value = $this->build_value( $compare, $minute ) ) ) { 3975 return "MINUTE( $column ) $compare $value"; 3976 } elseif ( ! isset( $hour ) && ! isset( $minute ) && isset( $second ) && false !== ( $value = $this->build_value( $compare, $second ) ) ) { 3977 return "SECOND( $column ) $compare $value"; 3978 } 3979 3980 // Single units were already handled. Since hour & second isn't allowed, minute must to be set. 3981 if ( ! isset( $minute ) ) 3982 return false; 3983 3984 $format = $time = ''; 3985 3986 // Hour 3987 if ( $hour ) { 3988 $format .= '%H.'; 3989 $time .= sprintf( '%02d', $hour ) . '.'; 3990 } else { 3991 $format .= '0.'; 3992 $time .= '0.'; 3993 } 3994 3995 // Minute 3996 $format .= '%i'; 3997 $time .= sprintf( '%02d', $minute ); 3998 3999 if ( isset( $second ) ) { 4000 $format .= '%s'; 4001 $time .= sprintf( '%02d', $second ); 4002 } 4003 4004 return $wpdb->prepare( "DATE_FORMAT( $column, %s ) $compare %f", $format, $time ); 4005 } 4006 } 4007 4008 /** 3574 4009 * Redirect old slugs to the correct permalink. 3575 4010 * 3576 4011 * Attempts to find the current slug from the past slugs. -
tests/tests/comment/dateQuery.php
1 <?php 2 3 /** 4 * Tests to make sure querying posts based on various date parameters 5 * using "date_query" works as expected. 6 * 7 * No need to do a full repeat of all of the post tests again since 8 * the query SQL is the same for both just with a different column. 9 * 10 * @ticket 18694 11 * 12 * @group comment 13 * @group date 14 * @group datequery 15 */ 16 class Tests_Comment_DateQuery extends WP_UnitTestCase { 17 18 public $posts = array(); 19 20 public function setUp() { 21 parent::setUp(); 22 23 // Just some dummy posts to use as parents for comments 24 for ( $i = 1; $i <= 2; $i++ ) { 25 $this->posts[$i] = $this->factory->post->create(); 26 } 27 28 // Be careful modifying this. Tests are coded to expect this exact sample data. 29 // Format is 'datetime' => 'post number (not ID)' 30 $comment_dates = array( 31 '2007-01-22 03:49:21' => 1, 32 '2007-05-16 17:32:22' => 1, 33 '2007-09-24 07:17:23' => 1, 34 '2008-03-29 09:04:25' => 1, 35 '2008-07-15 11:32:26' => 2, // This one should never be in the results 36 '2008-12-10 13:06:27' => 1, 37 '2009-06-11 21:30:28' => 1, 38 '2009-12-18 10:42:29' => 1, 39 ); 40 41 foreach ( $comment_dates as $comment_date => $comment_parent ) { 42 $result = $this->factory->comment->create( array( 43 'comment_date' => $comment_date, 44 'comment_post_ID' => $this->posts[ $comment_parent ], 45 ) ); 46 } 47 } 48 49 public function _get_query_result( $args = array() ) { 50 $args = wp_parse_args( $args, array( 51 'post_id' => $this->posts[1], 52 'orderby' => 'comment_ID', // Same order they were created 53 'order' => 'ASC', 54 ) ); 55 56 return get_comments( $args ); 57 } 58 59 public function test_year() { 60 $comments = $this->_get_query_result( array( 61 'date_query' => array( 62 array( 63 'year' => 2008, 64 ), 65 ), 66 ) ); 67 68 $expected_dates = array( 69 '2008-03-29 09:04:25', 70 '2008-12-10 13:06:27', 71 ); 72 73 $this->assertEquals( $expected_dates, wp_list_pluck( $comments, 'comment_date' ) ); 74 } 75 } 76 No newline at end of file -
tests/tests/query/date.php
1 <?php 2 3 /** 4 * Tests to make sure querying posts based on various date parameters works as expected. 5 * 6 * @group query 7 * @group date 8 */ 9 class Tests_Query_Date extends WP_UnitTestCase { 10 11 public $q; 12 13 public function setUp() { 14 parent::setUp(); 15 16 // Be careful modifying this. Tests are coded to expect this exact sample data. 17 $post_dates = array( 18 '1972-05-24 14:53:45', 19 '1984-07-28 19:28:56', 20 '2003-05-27 22:45:07', 21 '2004-01-03 08:54:10', 22 '2004-05-22 12:34:12', 23 '2005-02-17 00:00:15', 24 '2005-12-31 23:59:20', 25 '2007-01-22 03:49:21', 26 '2007-05-16 17:32:22', 27 '2007-09-24 07:17:23', 28 '2008-03-29 09:04:25', 29 '2008-07-15 11:32:26', 30 '2008-12-10 13:06:27', 31 '2009-06-11 21:30:28', 32 '2009-12-18 10:42:29', 33 '2010-06-17 17:09:30', 34 '2011-02-23 12:12:31', 35 '2011-07-04 01:56:32', 36 '2011-12-12 16:39:33', 37 '2012-06-13 14:03:34', 38 '2025-04-20 10:13:00', 39 '2025-04-20 10:13:01', 40 '2025-05-20 10:13:01', 41 ); 42 43 foreach ( $post_dates as $post_date ) { 44 $this->factory->post->create( array( 'post_date' => $post_date ) ); 45 } 46 47 unset( $this->q ); 48 $this->q = new WP_Query(); 49 } 50 51 public function _get_query_result( $args = array() ) { 52 $args = wp_parse_args( $args, array( 53 'post_status' => 'any', // For the future post 54 'posts_per_page' => '-1', // To make sure results are accurate 55 'orderby' => 'ID', // Same order they were created 56 'order' => 'ASC', 57 ) ); 58 59 return $this->q->query( $args ); 60 } 61 62 public function test_simple_year_expecting_results() { 63 $posts = $this->_get_query_result( array( 64 'year' => 2008, 65 ) ); 66 67 $expected_dates = array( 68 '2008-03-29 09:04:25', 69 '2008-07-15 11:32:26', 70 '2008-12-10 13:06:27', 71 ); 72 73 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 74 } 75 76 public function test_simple_year_expecting_noresults() { 77 $posts = $this->_get_query_result( array( 78 'year' => 2000, 79 ) ); 80 81 $this->assertCount( 0, $posts ); 82 } 83 84 public function test_simple_m_with_year_expecting_results() { 85 $posts = $this->_get_query_result( array( 86 'm' => '2007', 87 ) ); 88 89 $expected_dates = array( 90 '2007-01-22 03:49:21', 91 '2007-05-16 17:32:22', 92 '2007-09-24 07:17:23', 93 ); 94 95 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 96 } 97 98 public function test_simple_m_with_year_expecting_noresults() { 99 $posts = $this->_get_query_result( array( 100 'm' => '1999', 101 ) ); 102 103 $this->assertCount( 0, $posts ); 104 } 105 106 public function test_simple_m_with_yearmonth_expecting_results() { 107 $posts = $this->_get_query_result( array( 108 'm' => '202504', 109 ) ); 110 111 $expected_dates = array( 112 '2025-04-20 10:13:00', 113 '2025-04-20 10:13:01', 114 ); 115 116 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 117 } 118 119 public function test_simple_m_with_yearmonth_expecting_noresults() { 120 $posts = $this->_get_query_result( array( 121 'm' => '202502', 122 ) ); 123 124 $this->assertCount( 0, $posts ); 125 } 126 127 public function test_simple_m_with_yearmonthday_expecting_results() { 128 $posts = $this->_get_query_result( array( 129 'm' => '20250420', 130 ) ); 131 132 $expected_dates = array( 133 '2025-04-20 10:13:00', 134 '2025-04-20 10:13:01', 135 ); 136 137 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 138 } 139 140 public function test_simple_m_with_yearmonthday_expecting_noresults() { 141 $posts = $this->_get_query_result( array( 142 'm' => '20250419', 143 ) ); 144 145 $this->assertCount( 0, $posts ); 146 } 147 148 public function test_simple_m_with_yearmonthdayhour_expecting_results() { 149 $posts = $this->_get_query_result( array( 150 'm' => '2025042010', 151 ) ); 152 153 $expected_dates = array( 154 '2025-04-20 10:13:00', 155 '2025-04-20 10:13:01', 156 ); 157 158 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 159 } 160 161 public function test_simple_m_with_yearmonthdayhour_expecting_noresults() { 162 $posts = $this->_get_query_result( array( 163 'm' => '2025042009', 164 ) ); 165 166 $this->assertCount( 0, $posts ); 167 } 168 169 /** 170 * @ticket 24884 171 */ 172 public function test_simple_m_with_yearmonthdayhourminute_expecting_results() { 173 $posts = $this->_get_query_result( array( 174 'm' => '202504201013', 175 ) ); 176 177 $expected_dates = array( 178 '2025-04-20 10:13:00', 179 '2025-04-20 10:13:01', 180 ); 181 182 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 183 } 184 185 /** 186 * @ticket 24884 187 */ 188 public function test_simple_m_with_yearmonthdayhourminute_expecting_noresults() { 189 $posts = $this->_get_query_result( array( 190 'm' => '202504201012', 191 ) ); 192 193 $this->assertCount( 0, $posts ); 194 } 195 196 /** 197 * @ticket 24884 198 */ 199 public function test_simple_m_with_yearmonthdayhourminutesecond_expecting_results() { 200 $posts = $this->_get_query_result( array( 201 'm' => '20250420101301', 202 ) ); 203 204 $expected_dates = array( 205 '2025-04-20 10:13:01', 206 ); 207 208 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 209 } 210 211 /** 212 * @ticket 24884 213 */ 214 public function test_simple_m_with_yearmonthdayhourminutesecond_expecting_noresults() { 215 $posts = $this->_get_query_result( array( 216 'm' => '20250420101302', 217 ) ); 218 219 $this->assertCount( 0, $posts ); 220 } 221 222 /** 223 * @ticket 24884 224 */ 225 public function test_simple_m_with_yearmonthdayhourminutesecond_and_dashes_expecting_results() { 226 $posts = $this->_get_query_result( array( 227 'm' => '2025-04-20 10:13:00', 228 ) ); 229 230 $expected_dates = array( 231 '2025-04-20 10:13:00', 232 ); 233 234 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 235 } 236 237 /** 238 * @ticket 24884 239 */ 240 public function test_simple_m_with_yearmonthdayhourminutesecond_and_dashesletters_expecting_results() { 241 $posts = $this->_get_query_result( array( 242 'm' => 'alpha2025-04-20 10:13:00', 243 ) ); 244 245 $expected_dates = array( 246 '2025-04-20 10:13:00', 247 ); 248 249 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 250 } 251 252 public function test_simple_monthnum_expecting_results() { 253 $posts = $this->_get_query_result( array( 254 'monthnum' => 5, 255 ) ); 256 257 $expected_dates = array( 258 '1972-05-24 14:53:45', 259 '2003-05-27 22:45:07', 260 '2004-05-22 12:34:12', 261 '2007-05-16 17:32:22', 262 '2025-05-20 10:13:01', 263 ); 264 265 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 266 } 267 268 public function test_simple_monthnum_expecting_noresults() { 269 $posts = $this->_get_query_result( array( 270 'monthnum' => 8, 271 ) ); 272 273 $this->assertCount( 0, $posts ); 274 } 275 276 public function test_simple_w_as_in_week_expecting_results() { 277 $posts = $this->_get_query_result( array( 278 'w' => 24, 279 ) ); 280 281 $expected_dates = array( 282 '2009-06-11 21:30:28', 283 '2010-06-17 17:09:30', 284 '2012-06-13 14:03:34', 285 ); 286 287 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 288 } 289 290 public function test_simple_w_as_in_week_expecting_noresults() { 291 $posts = $this->_get_query_result( array( 292 'w' => 2, 293 ) ); 294 295 $this->assertCount( 0, $posts ); 296 } 297 298 public function test_simple_day_expecting_results() { 299 $posts = $this->_get_query_result( array( 300 'day' => 22, 301 ) ); 302 303 $expected_dates = array( 304 '2004-05-22 12:34:12', 305 '2007-01-22 03:49:21', 306 ); 307 308 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 309 } 310 311 public function test_simple_day_expecting_noresults() { 312 $posts = $this->_get_query_result( array( 313 'day' => 30, 314 ) ); 315 316 $this->assertCount( 0, $posts ); 317 } 318 319 public function test_simple_hour_expecting_results() { 320 $posts = $this->_get_query_result( array( 321 'hour' => 21, 322 ) ); 323 324 $expected_dates = array( 325 '2009-06-11 21:30:28', 326 ); 327 328 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 329 } 330 331 public function test_simple_hour_expecting_noresults() { 332 $posts = $this->_get_query_result( array( 333 'hour' => 2, 334 ) ); 335 336 $this->assertCount( 0, $posts ); 337 } 338 339 public function test_simple_minute_expecting_results() { 340 $posts = $this->_get_query_result( array( 341 'minute' => 32, 342 ) ); 343 344 $expected_dates = array( 345 '2007-05-16 17:32:22', 346 '2008-07-15 11:32:26', 347 ); 348 349 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 350 } 351 352 public function test_simple_minute_expecting_noresults() { 353 $posts = $this->_get_query_result( array( 354 'minute' => 1, 355 ) ); 356 357 $this->assertCount( 0, $posts ); 358 } 359 360 public function test_simple_second_expecting_results() { 361 $posts = $this->_get_query_result( array( 362 'second' => 30, 363 ) ); 364 365 $expected_dates = array( 366 '2010-06-17 17:09:30', 367 ); 368 369 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 370 } 371 372 public function test_simple_second_expecting_noresults() { 373 $posts = $this->_get_query_result( array( 374 'second' => 50, 375 ) ); 376 377 $this->assertCount( 0, $posts ); 378 } 379 } 380 No newline at end of file -
tests/tests/query/dateQuery.php
1 <?php 2 3 /** 4 * Tests to make sure querying posts based on various date parameters using "date_query" works as expected. 5 * 6 * @ticket 18694 7 * 8 * @group query 9 * @group date 10 * @group datequery 11 */ 12 class Tests_Query_DateQuery extends WP_UnitTestCase { 13 14 public $q; 15 16 public function setUp() { 17 parent::setUp(); 18 19 // Be careful modifying this. Tests are coded to expect this exact sample data. 20 $post_dates = array( 21 '1972-05-24 14:53:45', 22 '1984-07-28 19:28:56', 23 '2003-05-27 22:45:07', 24 '2004-01-03 08:54:10', 25 '2004-05-22 12:34:12', 26 '2005-02-17 00:00:15', 27 '2005-12-31 23:59:20', 28 '2007-01-22 03:49:21', 29 '2007-05-16 17:32:22', 30 '2007-09-24 07:17:23', 31 '2008-03-29 09:04:25', 32 '2008-07-15 11:32:26', 33 '2008-12-10 13:06:27', 34 '2009-06-11 21:30:28', 35 '2009-12-18 10:42:29', 36 '2010-06-17 17:09:30', 37 '2011-02-23 12:12:31', 38 '2011-07-04 01:56:32', 39 '2011-12-12 16:39:33', 40 '2012-06-13 14:03:34', 41 '2025-04-20 10:13:00', 42 '2025-04-20 10:13:01', 43 '2025-05-20 10:13:01', 44 ); 45 46 foreach ( $post_dates as $post_date ) { 47 $this->factory->post->create( array( 'post_date' => $post_date ) ); 48 } 49 50 unset( $this->q ); 51 $this->q = new WP_Query(); 52 } 53 54 public function _get_query_result( $args = array() ) { 55 $args = wp_parse_args( $args, array( 56 'post_status' => 'any', // For the future post 57 'posts_per_page' => '-1', // To make sure results are accurate 58 'orderby' => 'ID', // Same order they were created 59 'order' => 'ASC', 60 ) ); 61 62 return $this->q->query( $args ); 63 } 64 65 public function test_date_query_before_array() { 66 $posts = $this->_get_query_result( array( 67 'date_query' => array( 68 array( 69 'before' => array( 70 'year' => 2008, 71 'month' => 6, 72 ), 73 ), 74 ), 75 ) ); 76 77 $expected_dates = array( 78 '1972-05-24 14:53:45', 79 '1984-07-28 19:28:56', 80 '2003-05-27 22:45:07', 81 '2004-01-03 08:54:10', 82 '2004-05-22 12:34:12', 83 '2005-02-17 00:00:15', 84 '2005-12-31 23:59:20', 85 '2007-01-22 03:49:21', 86 '2007-05-16 17:32:22', 87 '2007-09-24 07:17:23', 88 '2008-03-29 09:04:25', 89 ); 90 91 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 92 } 93 94 /** 95 * Specifically tests to make sure values are defaulting to 96 * their minimum values when being used with "before". 97 */ 98 public function test_date_query_before_array_test_defaulting() { 99 $posts = $this->_get_query_result( array( 100 'date_query' => array( 101 array( 102 'before' => array( 103 'year' => 2008, 104 ), 105 ), 106 ), 107 ) ); 108 109 $expected_dates = array( 110 '1972-05-24 14:53:45', 111 '1984-07-28 19:28:56', 112 '2003-05-27 22:45:07', 113 '2004-01-03 08:54:10', 114 '2004-05-22 12:34:12', 115 '2005-02-17 00:00:15', 116 '2005-12-31 23:59:20', 117 '2007-01-22 03:49:21', 118 '2007-05-16 17:32:22', 119 '2007-09-24 07:17:23', 120 ); 121 122 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 123 } 124 125 public function test_date_query_before_string() { 126 $posts = $this->_get_query_result( array( 127 'date_query' => array( 128 array( 129 'before' => 'May 4th, 2008', 130 ), 131 ), 132 ) ); 133 134 $expected_dates = array( 135 '1972-05-24 14:53:45', 136 '1984-07-28 19:28:56', 137 '2003-05-27 22:45:07', 138 '2004-01-03 08:54:10', 139 '2004-05-22 12:34:12', 140 '2005-02-17 00:00:15', 141 '2005-12-31 23:59:20', 142 '2007-01-22 03:49:21', 143 '2007-05-16 17:32:22', 144 '2007-09-24 07:17:23', 145 '2008-03-29 09:04:25', 146 ); 147 148 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 149 } 150 151 public function test_date_query_after_array() { 152 $posts = $this->_get_query_result( array( 153 'date_query' => array( 154 array( 155 'after' => array( 156 'year' => 2009, 157 'month' => 12, 158 'day' => 31, 159 ), 160 ), 161 ), 162 ) ); 163 164 $expected_dates = array( 165 '2010-06-17 17:09:30', 166 '2011-02-23 12:12:31', 167 '2011-07-04 01:56:32', 168 '2011-12-12 16:39:33', 169 '2012-06-13 14:03:34', 170 '2025-04-20 10:13:00', 171 '2025-04-20 10:13:01', 172 '2025-05-20 10:13:01', 173 ); 174 175 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 176 } 177 178 /** 179 * Specifically tests to make sure values are defaulting to 180 * their maximum values when being used with "after". 181 */ 182 public function test_date_query_after_array_test_defaulting() { 183 $posts = $this->_get_query_result( array( 184 'date_query' => array( 185 array( 186 'after' => array( 187 'year' => 2008, 188 ), 189 ), 190 ), 191 ) ); 192 193 $expected_dates = array( 194 '2009-06-11 21:30:28', 195 '2009-12-18 10:42:29', 196 '2010-06-17 17:09:30', 197 '2011-02-23 12:12:31', 198 '2011-07-04 01:56:32', 199 '2011-12-12 16:39:33', 200 '2012-06-13 14:03:34', 201 '2025-04-20 10:13:00', 202 '2025-04-20 10:13:01', 203 '2025-05-20 10:13:01', 204 ); 205 206 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 207 } 208 209 public function test_date_query_after_string() { 210 $posts = $this->_get_query_result( array( 211 'date_query' => array( 212 array( 213 'after' => '2009-12-18 10:42:29', 214 ), 215 ), 216 ) ); 217 218 $expected_dates = array( 219 '2010-06-17 17:09:30', 220 '2011-02-23 12:12:31', 221 '2011-07-04 01:56:32', 222 '2011-12-12 16:39:33', 223 '2012-06-13 14:03:34', 224 '2025-04-20 10:13:00', 225 '2025-04-20 10:13:01', 226 '2025-05-20 10:13:01', 227 ); 228 229 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 230 } 231 232 public function test_date_query_after_string_inclusive() { 233 $posts = $this->_get_query_result( array( 234 'date_query' => array( 235 array( 236 'after' => '2009-12-18 10:42:29', 237 'inclusive' => true, 238 ), 239 ), 240 ) ); 241 242 $expected_dates = array( 243 '2009-12-18 10:42:29', 244 '2010-06-17 17:09:30', 245 '2011-02-23 12:12:31', 246 '2011-07-04 01:56:32', 247 '2011-12-12 16:39:33', 248 '2012-06-13 14:03:34', 249 '2025-04-20 10:13:00', 250 '2025-04-20 10:13:01', 251 '2025-05-20 10:13:01', 252 ); 253 254 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 255 } 256 257 public function test_date_query_year_expecting_results() { 258 $posts = $this->_get_query_result( array( 259 'date_query' => array( 260 array( 261 'year' => 2009, 262 ), 263 ), 264 ) ); 265 266 $expected_dates = array( 267 '2009-06-11 21:30:28', 268 '2009-12-18 10:42:29', 269 ); 270 271 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 272 } 273 274 public function test_date_query_year_expecting_noresults() { 275 $posts = $this->_get_query_result( array( 276 'date_query' => array( 277 array( 278 'year' => 2001, 279 ), 280 ), 281 ) ); 282 283 $this->assertCount( 0, $posts ); 284 } 285 286 public function test_date_query_month_expecting_results() { 287 $posts = $this->_get_query_result( array( 288 'date_query' => array( 289 array( 290 'month' => 12, 291 ), 292 ), 293 ) ); 294 295 $expected_dates = array( 296 '2005-12-31 23:59:20', 297 '2008-12-10 13:06:27', 298 '2009-12-18 10:42:29', 299 '2011-12-12 16:39:33', 300 ); 301 302 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 303 } 304 305 public function test_date_query_month_expecting_noresults() { 306 $posts = $this->_get_query_result( array( 307 'date_query' => array( 308 array( 309 'month' => 8, 310 ), 311 ), 312 ) ); 313 314 $this->assertCount( 0, $posts ); 315 } 316 317 public function test_date_query_week_expecting_results() { 318 $posts = $this->_get_query_result( array( 319 'date_query' => array( 320 array( 321 'week' => 1, 322 ), 323 ), 324 ) ); 325 326 $expected_dates = array( 327 '2004-01-03 08:54:10', 328 ); 329 330 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 331 } 332 333 public function test_date_query_week_expecting_noresults() { 334 $posts = $this->_get_query_result( array( 335 'date_query' => array( 336 array( 337 'week' => 10, 338 ), 339 ), 340 ) ); 341 342 $this->assertCount( 0, $posts ); 343 } 344 345 public function test_date_query_day_expecting_results() { 346 $posts = $this->_get_query_result( array( 347 'date_query' => array( 348 array( 349 'day' => 17, 350 ), 351 ), 352 ) ); 353 354 $expected_dates = array( 355 '2005-02-17 00:00:15', 356 '2010-06-17 17:09:30', 357 ); 358 359 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 360 } 361 362 public function test_date_query_day_expecting_noresults() { 363 $posts = $this->_get_query_result( array( 364 'date_query' => array( 365 array( 366 'day' => 19, 367 ), 368 ), 369 ) ); 370 371 $this->assertCount( 0, $posts ); 372 } 373 374 public function test_date_query_dayofweek_expecting_results() { 375 $posts = $this->_get_query_result( array( 376 'date_query' => array( 377 array( 378 'dayofweek' => 7, 379 ), 380 ), 381 ) ); 382 383 $expected_dates = array( 384 '1984-07-28 19:28:56', 385 '2004-01-03 08:54:10', 386 '2004-05-22 12:34:12', 387 '2005-12-31 23:59:20', 388 '2008-03-29 09:04:25', 389 ); 390 391 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 392 } 393 394 public function test_date_query_hour_expecting_results() { 395 $posts = $this->_get_query_result( array( 396 'date_query' => array( 397 array( 398 'hour' => 13, 399 ), 400 ), 401 ) ); 402 403 $expected_dates = array( 404 '2008-12-10 13:06:27', 405 ); 406 407 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 408 } 409 410 public function test_date_query_hour_expecting_noresults() { 411 $posts = $this->_get_query_result( array( 412 'date_query' => array( 413 array( 414 'hour' => 2, 415 ), 416 ), 417 ) ); 418 419 $this->assertCount( 0, $posts ); 420 } 421 422 public function test_date_query_minute_expecting_results() { 423 $posts = $this->_get_query_result( array( 424 'date_query' => array( 425 array( 426 'minute' => 56, 427 ), 428 ), 429 ) ); 430 431 $expected_dates = array( 432 '2011-07-04 01:56:32', 433 ); 434 435 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 436 } 437 438 public function test_date_query_minute_expecting_noresults() { 439 $posts = $this->_get_query_result( array( 440 'date_query' => array( 441 array( 442 'minute' => 2, 443 ), 444 ), 445 ) ); 446 447 $this->assertCount( 0, $posts ); 448 } 449 450 public function test_date_query_second_expecting_results() { 451 $posts = $this->_get_query_result( array( 452 'date_query' => array( 453 array( 454 'second' => 21, 455 ), 456 ), 457 ) ); 458 459 $expected_dates = array( 460 '2007-01-22 03:49:21', 461 ); 462 463 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 464 } 465 466 public function test_date_query_second_expecting_noresults() { 467 $posts = $this->_get_query_result( array( 468 'date_query' => array( 469 array( 470 'second' => 2, 471 ), 472 ), 473 ) ); 474 475 $this->assertCount( 0, $posts ); 476 } 477 478 public function test_date_query_between_two_times() { 479 $posts = $this->_get_query_result( array( 480 'date_query' => array( 481 array( 482 'hour' => 9, 483 'minute' => 0, 484 'compare' => '>=', 485 ), 486 array( 487 'hour' => '17', 488 'minute' => '0', 489 'compare' => '<=', 490 ), 491 ), 492 ) ); 493 494 $expected_dates = array( 495 '1972-05-24 14:53:45', 496 '2004-05-22 12:34:12', 497 '2008-03-29 09:04:25', 498 '2008-07-15 11:32:26', 499 '2008-12-10 13:06:27', 500 '2009-12-18 10:42:29', 501 '2011-02-23 12:12:31', 502 '2011-12-12 16:39:33', 503 '2012-06-13 14:03:34', 504 '2025-04-20 10:13:00', 505 '2025-04-20 10:13:01', 506 '2025-05-20 10:13:01', 507 ); 508 509 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 510 } 511 512 public function test_date_query_relation_or() { 513 $posts = $this->_get_query_result( array( 514 'date_query' => array( 515 array( 516 'hour' => 14, 517 ), 518 array( 519 'minute' => 34, 520 ), 521 'relation' => 'OR', 522 ), 523 ) ); 524 525 $expected_dates = array( 526 '1972-05-24 14:53:45', 527 '2004-05-22 12:34:12', 528 '2012-06-13 14:03:34', 529 ); 530 531 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 532 } 533 534 public function test_date_query_compare_greater_than_or_equal_to() { 535 $posts = $this->_get_query_result( array( 536 'date_query' => array( 537 array( 538 'hour' => 14, 539 'minute' => 34, 540 ), 541 'compare' => '>=', 542 ), 543 ) ); 544 545 $expected_dates = array( 546 '1972-05-24 14:53:45', 547 '1984-07-28 19:28:56', 548 '2003-05-27 22:45:07', 549 '2005-12-31 23:59:20', 550 '2007-05-16 17:32:22', 551 '2009-06-11 21:30:28', 552 '2010-06-17 17:09:30', 553 '2011-12-12 16:39:33', 554 ); 555 556 $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); 557 } 558 } 559 No newline at end of file