Make WordPress Core

Changeset 29799


Ignore:
Timestamp:
09/30/2014 09:17:38 PM (10 years ago)
Author:
boonebgorges
Message:

Improve unit tests related to WP_Meta_Query.

  • More complete test coverage for publicly available methods of WP_Meta_Query.
  • Move tests that rely on WP_Query (via the meta_query parameter) to tests/post/query.php.
  • Better coverage for basic use cases of 'meta_query', including all values of 'compare' and default values of 'key', 'value', and 'compare'.
  • Improve performance for tests that run WP_Query, by retrieving only post IDs and not prefetching postmeta and post terms.
  • Add 'public' visibility keywords to test methods.
  • Whitespace cleanup.

Fixes #29560

Location:
trunk/tests/phpunit/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/meta/query.php

    r27689 r29799  
    33 * Test WP_Meta_Query, in wp-includes/meta.php
    44 *
     5 * See tests/post/query.php for tests that involve post queries.
     6 *
    57 * @group meta
    68 */
    79class Tests_Meta_Query extends WP_UnitTestCase {
    810
    9     function test_default_relation() {
     11    public function test_empty_meta_query_param() {
     12        $query = new WP_Meta_Query();
     13        $this->assertSame( null, $query->relation );
     14    }
     15
     16    public function test_default_relation() {
    1017        $query = new WP_Meta_Query( array( array( 'key' => 'abc' ) ) );
    11 
    1218        $this->assertEquals( 'AND', $query->relation );
    1319    }
    1420
    15     function test_set_relation() {
     21    public function test_set_relation() {
    1622
    1723        $query = new WP_Meta_Query( array( array( 'key' => 'abc' ), 'relation' => 'AND' ) );
     
    2228
    2329        $this->assertEquals( 'OR', $query->relation );
     30    }
     31
     32    /**
     33     * Non-arrays should not be added to the queries array.
     34     */
     35    public function test_invalid_query_clauses() {
     36        $query = new WP_Meta_Query( array(
     37            'foo', // empty string
     38            5, // int
     39            false, // bool
     40            array(),
     41        ) );
     42
     43        $this->assertSame( array( array() ), $query->queries );
    2444    }
    2545
     
    2949     * @ticket 19729
    3050     */
    31     function test_single_inner_join_for_keys_only() {
     51    public function test_single_inner_join_for_keys_only() {
    3252
    3353        global $wpdb;
     
    5878
    5979    /**
     80     * WP_Query-style query must be at index 0 for order_by=meta_value to work.
     81     */
     82    public function test_parse_query_vars_simple_query_index_0() {
     83        $qv = array(
     84            'meta_query' => array(
     85                array(
     86                    'key' => 'foo1',
     87                    'compare' => 'baz1',
     88                    'value' => 'bar1',
     89                ),
     90            ),
     91            'meta_key' => 'foo',
     92            'meta_compare' => 'bar',
     93            'meta_value' => 'baz',
     94        );
     95
     96        $query = new WP_Meta_Query();
     97        $query->parse_query_vars( $qv );
     98
     99        $expected0 = array(
     100            'key' => 'foo',
     101            'compare' => 'bar',
     102            'value' => 'baz',
     103        );
     104        $this->assertEquals( $expected0, $query->queries[0] );
     105
     106        $expected1 = array(
     107            'key' => 'foo1',
     108            'compare' => 'baz1',
     109            'value' => 'bar1',
     110        );
     111        $this->assertEquals( $expected1, $query->queries[1] );
     112    }
     113
     114    /**
     115     * When no meta_value is provided, no 'value' should be set in the parsed queries.
     116     */
     117    public function test_parse_query_vars_with_no_meta_value() {
     118        $qv = array(
     119            'meta_key' => 'foo',
     120            'meta_type' => 'bar',
     121            'meta_compare' => '=',
     122        );
     123
     124        $query = new WP_Meta_Query();
     125        $query->parse_query_vars( $qv );
     126
     127        $this->assertTrue( ! isset( $query->queries[0]['value'] ) );
     128    }
     129
     130    /**
     131     * WP_Query sets meta_value to '' by default. It should be removed by parse_query_vars().
     132     */
     133    public function test_parse_query_vars_with_default_meta_compare() {
     134        $qv = array(
     135            'meta_key' => 'foo',
     136            'meta_type' => 'bar',
     137            'meta_compare' => '=',
     138            'meta_value' => '',
     139        );
     140
     141        $query = new WP_Meta_Query();
     142        $query->parse_query_vars( $qv );
     143
     144        $this->assertTrue( ! isset( $query->queries[0]['value'] ) );
     145    }
     146
     147    /**
    60148     * Test the conversion between "WP_Query" style meta args (meta_value=x&meta_key=y)
    61      * to a meta query array
    62      */
    63     function test_parse_query_vars() {
     149     * to a meta query array.
     150     */
     151    public function test_parse_query_vars() {
    64152
    65153        $query = new WP_Meta_Query();
     
    82170
    83171    /**
    84      * @ticket 22096
    85      */
    86     function test_empty_value_sql() {
    87         global $wpdb;
    88 
    89         $query = new WP_Meta_Query();
    90 
    91         $the_complex_query['meta_query'] = array(
    92             array( 'key' => 'my_first_key', 'value' => 'my_amazing_value' ),
    93             array( 'key' => 'my_second_key', 'compare' => 'NOT EXISTS' ),
    94             array( 'key' => 'my_third_key', 'value' => array( ), 'compare' => 'IN' ),
    95         );
    96 
    97         $query->parse_query_vars( $the_complex_query );
    98 
    99         $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
    100 
    101         // We should have 2 joins - one for my_first_key and one for my_second_key
    102         $this->assertEquals( 2, substr_count( $sql['join'], 'INNER JOIN' ) );
    103 
    104         // The WHERE should check my_third_key against an unaliased table
    105         $this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'my_third_key'" ) );
    106 
    107     }
    108 
    109     /**
    110      * @ticket 22967
    111      */
    112     function test_null_value_sql() {
    113         global $wpdb;
    114 
    115         $query = new WP_Meta_Query( array(
    116             array( 'key' => 'abc', 'value' => null, 'compare' => '=' )
    117         ) );
    118         $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
    119 
    120         $this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = '')" ) );
    121     }
    122 
    123     /**
    124172     * @ticket 23033
    125173     */
    126     function test_get_cast_for_type() {
     174    public function test_get_cast_for_type() {
    127175        $query = new WP_Meta_Query();
    128176        $this->assertEquals( 'BINARY', $query->get_cast_for_type( 'BINARY' ) );
     
    151199        $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10,  5)' ) );
    152200
     201        $this->assertEquals( 'CHAR', $query->get_cast_for_type() );
    153202        $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
    154203    }
    155204
    156     function test_not_exists() {
     205    /**
     206     * Invalid $type will fail to get a table from _get_meta_table()
     207     */
     208    public function test_get_sql_invalid_type() {
     209        $query = new WP_Meta_Query();
     210        $this->assertFalse( $query->get_sql( 'foo', 'foo', 'foo' ) );
     211    }
     212
     213    /**
     214     * @ticket 22096
     215     */
     216    public function test_empty_value_sql() {
     217        global $wpdb;
     218
     219        $query = new WP_Meta_Query();
     220
     221        $the_complex_query['meta_query'] = array(
     222            array( 'key' => 'my_first_key', 'value' => 'my_amazing_value' ),
     223            array( 'key' => 'my_second_key', 'compare' => 'NOT EXISTS' ),
     224            array( 'key' => 'my_third_key', 'value' => array( ), 'compare' => 'IN' ),
     225        );
     226
     227        $query->parse_query_vars( $the_complex_query );
     228
     229        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     230
     231        // We should have 2 joins - one for my_first_key and one for my_second_key
     232        $this->assertEquals( 2, substr_count( $sql['join'], 'INNER JOIN' ) );
     233
     234        // The WHERE should check my_third_key against an unaliased table
     235        $this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'my_third_key'" ) );
     236
     237    }
     238
     239    /**
     240     * @ticket 22967
     241     */
     242    public function test_null_value_sql() {
     243        global $wpdb;
     244
     245        $query = new WP_Meta_Query( array(
     246            array( 'key' => 'abc', 'value' => null, 'compare' => '=' )
     247        ) );
     248        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     249
     250        $this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = '')" ) );
     251    }
     252
     253    /**
     254     * "key only queries" are queries that don't need to match a value, so
     255     * they can be grouped together into a single clause without JOINs
     256     */
     257    public function test_get_sql_key_only_queries() {
     258        global $wpdb;
     259
     260        $query1 = new WP_Meta_Query( array(
     261            'relation' => 'OR',
     262
     263            // Empty 'compare'
     264            array(
     265                'key' => 'foo',
     266            ),
     267
     268            // Non-empty 'compare'
     269            array(
     270                'key' => 'bar',
     271                'compare' => '<',
     272            ),
     273
     274            // NOT EXISTS
     275            array(
     276                'key' => 'baz',
     277                'compare' => 'NOT EXISTS',
     278            ),
     279
     280            // Has a value
     281            array(
     282                'key' => 'barry',
     283                'value' => 'foo',
     284            ),
     285
     286            // Has no key
     287            array(
     288                'value' => 'bar',
     289            ),
     290        ) );
     291
     292        $sql = $query1->get_sql( 'post', $wpdb->posts, 'ID', $this );
     293
     294        // 'foo' and 'bar' should be queried against the non-aliased table
     295        $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'foo'" ) );
     296        $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'bar'" ) );
     297
     298        // NOT EXISTS compare queries are not key-only so should not be non-aliased
     299        $this->assertSame( 0, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'baz'" ) );
     300
     301        // When a value exists, it's not a key-only query
     302        $this->assertSame( 0, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'barry'" ) );
     303
     304        // 'AND' queries don't have key-only queries
     305        $query2 = new WP_Meta_Query( array(
     306            'relation' => 'AND',
     307
     308            // Empty 'compare'
     309            array(
     310                'key' => 'foo',
     311            ),
     312
     313            // Non-empty 'compare'
     314            array(
     315                'key' => 'bar',
     316                'compare' => '<',
     317            ),
     318        ) );
     319
     320        $sql = $query2->get_sql( 'post', $wpdb->posts, 'ID', $this );
     321
     322        // Only 'foo' should be queried against the non-aliased table
     323        $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'foo'" ) );
     324        $this->assertSame( 0, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'bar'" ) );
     325    }
     326
     327    /**
     328     * Key-only and regular queries should have the key trimmed
     329     */
     330    public function test_get_sql_trim_key() {
     331        global $wpdb;
     332
     333        $query = new WP_Meta_Query( array(
     334            array(
     335                'key' => '  foo  ',
     336            ),
     337            array(
     338                'key' => '  bar  ',
     339                'value' => 'value',
     340            ),
     341        ) );
     342
     343        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     344
     345        $this->assertSame( 1, substr_count( $sql['where'], "meta_key = 'foo'" ) );
     346        $this->assertSame( 1, substr_count( $sql['where'], "meta_key = 'bar'" ) );
     347    }
     348
     349    public function test_convert_null_value_to_empty_string() {
     350        global $wpdb;
     351
     352        $query = new WP_Meta_Query( array(
     353            array(
     354                'key' => 'foo',
     355                'value' => null,
     356            ),
     357        ) );
     358
     359        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     360
     361        $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = ''" ) );
     362    }
     363
     364    public function test_get_sql_convert_lowercase_compare_to_uppercase() {
     365        global $wpdb;
     366
     367        $query = new WP_Meta_Query( array(
     368            array(
     369                'key' => 'foo',
     370                'value' => 'bar',
     371                'compare' => 'regExp',
     372            ),
     373        ) );
     374
     375        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     376
     377        $this->assertSame( 1, substr_count( $sql['where'], "REGEXP" ) );
     378    }
     379
     380    public function test_get_sql_empty_meta_compare_with_array_value() {
     381        global $wpdb;
     382
     383        $query = new WP_Meta_Query( array(
     384            array(
     385                'key' => 'foo',
     386                'value' => array( 'bar', 'baz' ),
     387            ),
     388        ) );
     389
     390        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     391
     392        $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) IN" ) );
     393    }
     394
     395    public function test_get_sql_empty_meta_compare_with_non_array_value() {
     396        global $wpdb;
     397
     398        $query = new WP_Meta_Query( array(
     399            array(
     400                'key' => 'foo',
     401                'value' => 'bar',
     402            ),
     403        ) );
     404
     405        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     406
     407        $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) =" ) );
     408    }
     409
     410    public function test_get_sql_invalid_meta_compare() {
     411        global $wpdb;
     412
     413        $query = new WP_Meta_Query( array(
     414            array(
     415                'key' => 'foo',
     416                'value' => 'bar',
     417                'compare' => 'INVALID COMPARE',
     418            ),
     419        ) );
     420
     421        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     422
     423        $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) =" ) );
     424    }
     425
     426    /**
     427     * This is the clause that ensures that empty arrays are not valid queries.
     428     */
     429    public function test_get_sql_null_value_and_empty_key_should_not_have_table_join() {
     430        global $wpdb;
     431
     432        $query = new WP_Meta_Query( array(
     433            array(
     434                'key' => 'foo',
     435                'value' => 'bar',
     436            ),
     437            array(),
     438        ) );
     439
     440        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     441
     442        // There should be no JOIN against an aliased table.
     443        $this->assertSame( 0, substr_count( $sql['join'], "AS mt" ) );
     444    }
     445
     446    public function test_get_sql_compare_array_comma_separated_values() {
     447        global $wpdb;
     448
     449        // Single value.
     450        $query = new WP_Meta_Query( array(
     451            array(
     452                'key' => 'foo',
     453                'compare' => 'IN',
     454                'value' => 'bar',
     455            ),
     456        ) );
     457
     458        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     459
     460        $this->assertSame( 1, substr_count( $sql['where'], "('bar')" ) );
     461
     462        // Multiple values, no spaces.
     463        $query = new WP_Meta_Query( array(
     464            array(
     465                'key' => 'foo',
     466                'compare' => 'IN',
     467                'value' => 'bar,baz',
     468            ),
     469        ) );
     470
     471        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     472
     473        $this->assertSame( 1, substr_count( $sql['where'], "('bar','baz')" ) );
     474
     475        // Multiple values, spaces.
     476        $query = new WP_Meta_Query( array(
     477            array(
     478                'key' => 'foo',
     479                'compare' => 'IN',
     480                'value' => 'bar,baz,   barry',
     481            ),
     482        ) );
     483
     484        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     485
     486        $this->assertSame( 1, substr_count( $sql['where'], "('bar','baz','barry')" ) );
     487    }
     488
     489    public function test_get_sql_compare_array() {
     490        global $wpdb;
     491
     492        $query = new WP_Meta_Query( array(
     493            array(
     494                'key' => 'foo',
     495                'compare' => 'IN',
     496                'value' => array( 'bar', 'baz' ),
     497            ),
     498        ) );
     499
     500        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     501
     502        $this->assertSame( 1, substr_count( $sql['where'], "('bar','baz')" ) );
     503    }
     504
     505    /**
     506     * Non-array values are trimmed. @todo Why?
     507     */
     508    public function test_get_sql_trim_string_value() {
     509        global $wpdb;
     510
     511        $query = new WP_Meta_Query( array(
     512            array(
     513                'key' => 'foo',
     514                'value' => '  bar  ',
     515            ),
     516        ) );
     517
     518        $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     519
     520        $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = 'bar'" ) );
     521    }
     522
     523    public function test_not_exists() {
    157524        global $wpdb;
    158525
     
    175542    }
    176543
    177     function test_empty_compare() {
     544    public function test_empty_compare() {
    178545        global $wpdb;
    179546
  • trunk/tests/phpunit/tests/post/query.php

    r29760 r29799  
    11<?php
    22
    3 /**
    4  * @group meta
    5  */
    63class Tests_Post_Query extends WP_UnitTestCase {
    74    function setUp() {
     
    96    }
    107
    11     function test_meta_key_or_query() {
     8    /**
     9     * @group meta
     10     */
     11    public function test_meta_query_no_key() {
     12        $p1 = $this->factory->post->create();
     13        $p2 = $this->factory->post->create();
     14        $p3 = $this->factory->post->create();
     15
     16        add_post_meta( $p1, 'foo', 'bar' );
     17        add_post_meta( $p2, 'oof', 'bar' );
     18        add_post_meta( $p3, 'oof', 'baz' );
     19
     20        $query = new WP_Query( array(
     21            'update_post_meta_cache' => false,
     22            'update_post_term_cache' => false,
     23            'fields' => 'ids',
     24            'meta_query' => array(
     25                array(
     26                    'value' => 'bar',
     27                ),
     28            ),
     29        ) );
     30
     31        $expected = array( $p1, $p2 );
     32        $this->assertEqualSets( $expected, $query->posts );
     33    }
     34
     35    /**
     36     * @group meta
     37     */
     38    public function test_meta_query_no_value() {
     39        $p1 = $this->factory->post->create();
     40        $p2 = $this->factory->post->create();
     41        $p3 = $this->factory->post->create();
     42
     43        add_post_meta( $p1, 'foo', 'bar' );
     44        add_post_meta( $p2, 'oof', 'bar' );
     45        add_post_meta( $p3, 'oof', 'baz' );
     46
     47        $query = new WP_Query( array(
     48            'update_post_meta_cache' => false,
     49            'update_post_term_cache' => false,
     50            'fields' => 'ids',
     51            'meta_query' => array(
     52                array(
     53                    'key' => 'oof',
     54                ),
     55            ),
     56        ) );
     57
     58        $expected = array( $p2, $p3 );
     59        $this->assertEqualSets( $expected, $query->posts );
     60    }
     61
     62    /**
     63     * @group meta
     64     */
     65    public function test_meta_query_single_query_compare_default() {
     66        $p1 = $this->factory->post->create();
     67        $p2 = $this->factory->post->create();
     68
     69        add_post_meta( $p1, 'foo', 'bar' );
     70
     71        $query = new WP_Query( array(
     72            'update_post_meta_cache' => false,
     73            'update_post_term_cache' => false,
     74            'fields' => 'ids',
     75            'meta_query' => array(
     76                array(
     77                    'key' => 'foo',
     78                    'value' => 'bar',
     79                ),
     80            ),
     81        ) );
     82
     83        $expected = array( $p1 );
     84        $this->assertEqualSets( $expected, $query->posts );
     85    }
     86
     87    /**
     88     * @group meta
     89     */
     90    public function test_meta_query_single_query_compare_equals() {
     91        $p1 = $this->factory->post->create();
     92        $p2 = $this->factory->post->create();
     93
     94        add_post_meta( $p1, 'foo', 'bar' );
     95
     96        $query = new WP_Query( array(
     97            'update_post_meta_cache' => false,
     98            'update_post_term_cache' => false,
     99            'fields' => 'ids',
     100            'meta_query' => array(
     101                array(
     102                    'key' => 'foo',
     103                    'value' => 'bar',
     104                    'compare' => '=',
     105                ),
     106            ),
     107        ) );
     108
     109        $expected = array( $p1 );
     110        $this->assertEqualSets( $expected, $query->posts );
     111    }
     112
     113    /**
     114     * @group meta
     115     */
     116    public function test_meta_query_single_query_compare_not_equals() {
     117        $p1 = $this->factory->post->create();
     118        $p2 = $this->factory->post->create();
     119        $p3 = $this->factory->post->create();
     120
     121        add_post_meta( $p1, 'foo', 'bar' );
     122        add_post_meta( $p2, 'foo', 'baz' );
     123
     124        $query = new WP_Query( array(
     125            'update_post_meta_cache' => false,
     126            'update_post_term_cache' => false,
     127            'fields' => 'ids',
     128            'meta_query' => array(
     129                array(
     130                    'key' => 'foo',
     131                    'value' => 'bar',
     132                    'compare' => '!=',
     133                ),
     134            ),
     135        ) );
     136
     137        $expected = array( $p2 );
     138        $this->assertEqualSets( $expected, $query->posts );
     139    }
     140
     141    /**
     142     * @group meta
     143     */
     144    public function test_meta_query_single_query_compare_arithmetic_comparisons() {
     145        $p1 = $this->factory->post->create();
     146        $p2 = $this->factory->post->create();
     147        $p3 = $this->factory->post->create();
     148
     149        add_post_meta( $p1, 'foo', '1' );
     150        add_post_meta( $p2, 'foo', '2' );
     151        add_post_meta( $p3, 'foo', '3' );
     152
     153        // <
     154        $query = new WP_Query( array(
     155            'update_post_meta_cache' => false,
     156            'update_post_term_cache' => false,
     157            'fields' => 'ids',
     158            'meta_query' => array(
     159                array(
     160                    'key' => 'foo',
     161                    'value' => 2,
     162                    'compare' => '<',
     163                ),
     164            ),
     165        ) );
     166
     167        $expected = array( $p1 );
     168        $this->assertEqualSets( $expected, $query->posts );
     169
     170        // <=
     171        $query = new WP_Query( array(
     172            'update_post_meta_cache' => false,
     173            'update_post_term_cache' => false,
     174            'fields' => 'ids',
     175            'meta_query' => array(
     176                array(
     177                    'key' => 'foo',
     178                    'value' => 2,
     179                    'compare' => '<=',
     180                ),
     181            ),
     182        ) );
     183
     184        $expected = array( $p1, $p2 );
     185        $this->assertEqualSets( $expected, $query->posts );
     186
     187        // >=
     188        $query = new WP_Query( array(
     189            'update_post_meta_cache' => false,
     190            'update_post_term_cache' => false,
     191            'fields' => 'ids',
     192            'meta_query' => array(
     193                array(
     194                    'key' => 'foo',
     195                    'value' => 2,
     196                    'compare' => '>=',
     197                ),
     198            ),
     199        ) );
     200
     201        $expected = array( $p2, $p3 );
     202        $this->assertEqualSets( $expected, $query->posts );
     203
     204        // >
     205        $query = new WP_Query( array(
     206            'update_post_meta_cache' => false,
     207            'update_post_term_cache' => false,
     208            'fields' => 'ids',
     209            'meta_query' => array(
     210                array(
     211                    'key' => 'foo',
     212                    'value' => 2,
     213                    'compare' => '>',
     214                ),
     215            ),
     216        ) );
     217
     218        $expected = array( $p3 );
     219        $this->assertEqualSets( $expected, $query->posts );
     220    }
     221
     222    /**
     223     * @group meta
     224     */
     225    public function test_meta_query_single_query_compare_like() {
     226        $p1 = $this->factory->post->create();
     227        $p2 = $this->factory->post->create();
     228
     229        add_post_meta( $p1, 'foo', 'bar' );
     230
     231        $query = new WP_Query( array(
     232            'update_post_meta_cache' => false,
     233            'update_post_term_cache' => false,
     234            'fields' => 'ids',
     235            'meta_query' => array(
     236                array(
     237                    'key' => 'foo',
     238                    'value' => 'ba',
     239                    'compare' => 'LIKE',
     240                ),
     241            ),
     242        ) );
     243
     244        $expected = array( $p1 );
     245        $this->assertEqualSets( $expected, $query->posts );
     246    }
     247
     248    /**
     249     * @group meta
     250     */
     251    public function test_meta_query_single_query_compare_not_like() {
     252        $p1 = $this->factory->post->create();
     253        $p2 = $this->factory->post->create();
     254        $p3 = $this->factory->post->create();
     255
     256        add_post_meta( $p1, 'foo', 'bar' );
     257        add_post_meta( $p2, 'foo', 'rab' );
     258
     259        $query = new WP_Query( array(
     260            'update_post_meta_cache' => false,
     261            'update_post_term_cache' => false,
     262            'fields' => 'ids',
     263            'meta_query' => array(
     264                array(
     265                    'key' => 'foo',
     266                    'value' => 'ba',
     267                    'compare' => 'NOT LIKE',
     268                ),
     269            ),
     270        ) );
     271
     272        $expected = array( $p2 );
     273        $this->assertEqualSets( $expected, $query->posts );
     274    }
     275
     276    /**
     277     * @group meta
     278     */
     279    public function test_meta_query_single_query_compare_between_not_between() {
     280        $p1 = $this->factory->post->create();
     281        $p2 = $this->factory->post->create();
     282        $p3 = $this->factory->post->create();
     283
     284        add_post_meta( $p1, 'foo', '1' );
     285        add_post_meta( $p2, 'foo', '10' );
     286        add_post_meta( $p3, 'foo', '100' );
     287
     288        $query = new WP_Query( array(
     289            'update_post_meta_cache' => false,
     290            'update_post_term_cache' => false,
     291            'fields' => 'ids',
     292            'meta_query' => array(
     293                array(
     294                    'key' => 'foo',
     295                    'value' => array( 9, 12 ),
     296                    'compare' => 'BETWEEN',
     297                    'type' => 'NUMERIC',
     298                ),
     299            ),
     300        ) );
     301
     302        $expected = array( $p2 );
     303        $this->assertEqualSets( $expected, $query->posts );
     304
     305        $query = new WP_Query( array(
     306            'update_post_meta_cache' => false,
     307            'update_post_term_cache' => false,
     308            'fields' => 'ids',
     309            'meta_query' => array(
     310                array(
     311                    'key' => 'foo',
     312                    'value' => array( 9, 12 ),
     313                    'compare' => 'NOT BETWEEN',
     314                    'type' => 'NUMERIC',
     315                ),
     316            ),
     317        ) );
     318
     319        $expected = array( $p1, $p3 );
     320        $this->assertEqualSets( $expected, $query->posts );
     321    }
     322
     323    /**
     324     * @group meta
     325     */
     326    public function test_meta_query_single_query_compare_regexp_rlike() {
     327        $p1 = $this->factory->post->create();
     328        $p2 = $this->factory->post->create();
     329
     330        add_post_meta( $p1, 'foo', 'bar' );
     331        add_post_meta( $p2, 'foo', 'baz' );
     332
     333        $query = new WP_Query( array(
     334            'update_post_meta_cache' => false,
     335            'update_post_term_cache' => false,
     336            'fields' => 'ids',
     337            'meta_query' => array(
     338                array(
     339                    'key' => 'foo',
     340                    'value' => 'z$',
     341                    'compare' => 'REGEXP',
     342                ),
     343            ),
     344        ) );
     345
     346        $expected = array( $p2 );
     347        $this->assertEqualSets( $expected, $query->posts );
     348
     349        // RLIKE is a synonym for REGEXP.
     350        $query = new WP_Query( array(
     351            'update_post_meta_cache' => false,
     352            'update_post_term_cache' => false,
     353            'fields' => 'ids',
     354            'meta_query' => array(
     355                array(
     356                    'key' => 'foo',
     357                    'value' => 'z$',
     358                    'compare' => 'RLIKE',
     359                ),
     360            ),
     361        ) );
     362
     363        $expected = array( $p2 );
     364        $this->assertEqualSets( $expected, $query->posts );
     365    }
     366
     367    /**
     368     * @group meta
     369     */
     370    public function test_meta_query_single_query_compare_not_regexp() {
     371        $p1 = $this->factory->post->create();
     372        $p2 = $this->factory->post->create();
     373
     374        add_post_meta( $p1, 'foo', 'bar' );
     375        add_post_meta( $p2, 'foo', 'baz' );
     376
     377        $query = new WP_Query( array(
     378            'update_post_meta_cache' => false,
     379            'update_post_term_cache' => false,
     380            'fields' => 'ids',
     381            'meta_query' => array(
     382                array(
     383                    'key' => 'foo',
     384                    'value' => 'z$',
     385                    'compare' => 'NOT REGEXP',
     386                ),
     387            ),
     388        ) );
     389
     390        $expected = array( $p1 );
     391        $this->assertEqualSets( $expected, $query->posts );
     392    }
     393
     394    /**
     395     * @group meta
     396     */
     397    public function test_meta_query_relation_default() {
     398        $p1 = $this->factory->post->create();
     399        $p2 = $this->factory->post->create();
     400        $p3 = $this->factory->post->create();
     401
     402        add_post_meta( $p1, 'foo', 'foo value 1' );
     403        add_post_meta( $p1, 'bar', 'bar value 1' );
     404        add_post_meta( $p2, 'foo', 'foo value 1' );
     405        add_post_meta( $p2, 'bar', 'bar value 2' );
     406
     407        $query = new WP_Query( array(
     408            'update_post_meta_cache' => false,
     409            'update_post_term_cache' => false,
     410            'fields' => 'ids',
     411            'meta_query' => array(
     412                array(
     413                    'key' => 'foo',
     414                    'value' => 'foo value 1',
     415                ),
     416                array(
     417                    'key' => 'bar',
     418                    'value' => 'bar value 1',
     419                ),
     420            ),
     421        ) );
     422
     423        $expected = array( $p1 );
     424        $this->assertEquals( $expected, $query->posts );
     425    }
     426
     427    /**
     428     * @group meta
     429     */
     430    public function test_meta_query_relation_or() {
    12431        $post_id = $this->factory->post->create();
    13432        add_post_meta( $post_id, 'foo', rand_str() );
     
    25444
    26445        $query = new WP_Query( array(
     446            'update_post_meta_cache' => false,
     447            'update_post_term_cache' => false,
     448            'fields' => 'ids',
    27449            'meta_query' => array(
    28450                array(
     
    43465        ) );
    44466
    45         $posts = $query->get_posts();
    46         $this->assertEquals( 4, count( $posts ) );
    47         foreach ( $posts as $post ) {
    48             $this->assertInstanceOf( 'WP_Post', $post );
    49             $this->assertEquals( 'raw', $post->filter );
    50         }
    51 
    52         $post_ids = wp_list_pluck( $posts, 'ID' );
    53         $this->assertEqualSets( array( $post_id, $post_id2, $post_id3, $post_id4 ), $post_ids );
    54     }
    55 
    56     function test_meta_key_and_query() {
     467        $expected = array( $post_id, $post_id2, $post_id3, $post_id4 );
     468        $this->assertEqualSets( $expected, $query->posts );
     469    }
     470
     471    /**
     472     * @group meta
     473     */
     474    public function test_meta_query_relation_and() {
    57475        $post_id = $this->factory->post->create();
    58476        add_post_meta( $post_id, 'foo', rand_str() );
     
    93511                'relation' => 'AND',
    94512            ),
    95         ) );
    96 
    97         $posts = $query->get_posts();
    98         $this->assertEquals( 1, count( $posts ) );
    99         foreach ( $posts as $post ) {
    100             $this->assertInstanceOf( 'WP_Post', $post );
    101             $this->assertEquals( 'raw', $post->filter );
    102         }
    103 
    104         $post_ids = wp_list_pluck( $posts, 'ID' );
    105         $this->assertEquals( array( $post_id7 ), $post_ids );
     513            'update_post_meta_cache' => false,
     514            'update_post_term_cache' => false,
     515            'fields' => 'ids',
     516        ) );
     517
     518        $expected = array( $post_id7 );
     519        $this->assertEqualSets( $expected, $query->posts );
    106520
    107521        $query = new WP_Query( array(
     
    115529                'relation' => 'AND',
    116530            ),
    117         ) );
    118 
    119         $posts = $query->get_posts();
    120         $this->assertEquals( 3, count( $posts ) );
    121         foreach ( $posts as $post ) {
    122             $this->assertInstanceOf( 'WP_Post', $post );
    123             $this->assertEquals( 'raw', $post->filter );
    124         }
    125 
    126         $post_ids = wp_list_pluck( $posts, 'ID' );
    127         $this->assertEqualSets( array( $post_id2, $post_id6, $post_id7 ), $post_ids );
     531            'update_post_meta_cache' => false,
     532            'update_post_term_cache' => false,
     533            'fields' => 'ids',
     534        ) );
     535
     536        $expected = array( $post_id2, $post_id6, $post_id7 );
     537        $this->assertEqualSets( $expected, $query->posts );
    128538    }
    129539
    130540    /**
    131541     * @ticket 18158
    132      */
    133     function test_meta_key_not_exists() {
     542     * @group meta
     543     */
     544    public function test_meta_query_compare_not_exists() {
    134545        $post_id = $this->factory->post->create();
    135546        add_post_meta( $post_id, 'foo', rand_str() );
     
    145556        $query = new WP_Query( array(
    146557            'meta_query' => array(
    147             array(
    148                 'key' => 'foo',
    149                 'compare' => 'NOT EXISTS',
    150             ),
    151             ),
    152         ) );
    153 
    154         $posts = $query->get_posts();
    155         $this->assertEquals( 3, count( $posts ) );
    156         foreach ( $posts as $post ) {
    157             $this->assertInstanceOf( 'WP_Post', $post );
    158             $this->assertEquals( 'raw', $post->filter );
    159         }
    160 
    161         $query = new WP_Query( array(
    162             'meta_query' => array(
    163             array(
    164                 'key' => 'foo',
    165                 'compare' => 'NOT EXISTS',
    166             ),
    167                 array(
    168                 'key' => 'bar',
    169                 'compare' => 'NOT EXISTS',
    170             ),
    171             ),
    172         ) );
    173 
    174         $posts = $query->get_posts();
    175         $this->assertEquals( 1, count( $posts ) );
    176         foreach ( $posts as $post ) {
    177             $this->assertInstanceOf( 'WP_Post', $post );
    178             $this->assertEquals( 'raw', $post->filter );
    179         }
    180 
    181         $query = new WP_Query( array(
    182             'meta_query' => array(
    183             array(
    184                 'key' => 'foo',
    185                 'compare' => 'NOT EXISTS',
    186             ),
    187                 array(
    188                 'key' => 'bar',
    189                 'compare' => 'NOT EXISTS',
    190             ),
    191                 array(
    192                 'key' => 'baz',
    193                 'compare' => 'NOT EXISTS',
    194             ),
    195             )
    196         ) );
    197 
    198         $posts = $query->get_posts();
    199         $this->assertEquals( 0, count( $posts ) );
     558                array(
     559                    'key' => 'foo',
     560                    'compare' => 'NOT EXISTS',
     561                ),
     562            ),
     563            'update_post_meta_cache' => false,
     564            'update_post_term_cache' => false,
     565            'fields' => 'ids',
     566        ) );
     567
     568        $expected = array( $post_id2, $post_id3, $post_id4 );
     569        $this->assertEqualSets( $expected, $query->posts );
     570
     571        $query = new WP_Query( array(
     572            'meta_query' => array(
     573                array(
     574                    'key' => 'foo',
     575                    'compare' => 'NOT EXISTS',
     576                ),
     577                array(
     578                    'key' => 'bar',
     579                    'compare' => 'NOT EXISTS',
     580                ),
     581            ),
     582            'update_post_meta_cache' => false,
     583            'update_post_term_cache' => false,
     584            'fields' => 'ids',
     585        ) );
     586
     587        $expected = array( $post_id4 );
     588        $this->assertEquals( $expected, $query->posts );
     589
     590        $query = new WP_Query( array(
     591            'meta_query' => array(
     592                array(
     593                    'key' => 'foo',
     594                    'compare' => 'NOT EXISTS',
     595                ),
     596                array(
     597                    'key' => 'bar',
     598                    'compare' => 'NOT EXISTS',
     599                ),
     600                array(
     601                    'key' => 'baz',
     602                    'compare' => 'NOT EXISTS',
     603                ),
     604            ),
     605            'update_post_meta_cache' => false,
     606            'update_post_term_cache' => false,
     607            'fields' => 'ids',
     608        ) );
     609
     610        $this->assertEquals( 0, count( $query->posts ) );
    200611    }
    201612
    202613    /**
    203614     * @ticket 23033
    204      */
    205     function test_meta_query_decimal_results() {
     615     * @group meta
     616     */
     617    public function test_meta_query_decimal_results() {
    206618        $post_1 = $this->factory->post->create();
    207619        $post_2 = $this->factory->post->create();
     
    216628        $query = new WP_Query( array(
    217629            'meta_query' => array(
    218                     array(
    219                         'key' => 'decimal_value',
    220                         'value' => '.300',
    221                         'compare' => '=',
    222                         'type' => 'DECIMAL(10,2)'
    223                     )
    224                 ),
    225         ) );
    226         $this->assertEqualSets( array( $post_3 ), wp_list_pluck( $query->posts, 'ID' ) );
    227 
    228         $query = new WP_Query( array(
    229             'meta_query' => array(
    230                     array(
    231                         'key' => 'decimal_value',
    232                         'value' => '0.35',
    233                         'compare' => '>',
    234                         'type' => 'DECIMAL(10,2)'
    235                     )
    236                 ),
    237         ) );
    238         $this->assertEqualSets( array( $post_4 ), wp_list_pluck( $query->posts, 'ID' ) );
    239 
    240         $query = new WP_Query( array(
    241             'meta_query' => array(
    242                     array(
    243                         'key' => 'decimal_value',
    244                         'value' => '0.3',
    245                         'compare' => '>=',
    246                         'type' => 'DECIMAL(10,2)'
    247                     )
    248                 ),
    249         ) );
    250         $this->assertEqualSets( array( $post_3, $post_4 ), wp_list_pluck( $query->posts, 'ID' ) );
    251 
    252         $query = new WP_Query( array(
    253             'meta_query' => array(
    254                     array(
    255                         'key' => 'decimal_value',
    256                         'value' => '0',
    257                         'compare' => '<',
    258                         'type' => 'DECIMAL(10,2)'
    259                     )
    260                 ),
    261         ) );
    262         $this->assertEqualSets( array( $post_1 ), wp_list_pluck( $query->posts, 'ID' ) );
    263 
    264         $query = new WP_Query( array(
    265             'meta_query' => array(
    266                     array(
    267                         'key' => 'decimal_value',
    268                         'value' => '0.3',
    269                         'compare' => '<=',
    270                         'type' => 'DECIMAL(10,2)'
    271                     )
    272                 ),
    273 
    274         ) );
    275         $this->assertEqualSets( array( $post_1, $post_2, $post_3 ), wp_list_pluck( $query->posts, 'ID' ) );
    276 
    277         $query = new WP_Query( array(
    278             'meta_query' => array(
    279                     array(
    280                         'key' => 'decimal_value',
    281                         'value' => array( 0.23409845, .31 ),
    282                         'compare' => 'BETWEEN',
    283                         'type' => 'DECIMAL(10, 10)'
    284                     )
    285                 ),
    286         ) );
    287         $this->assertEqualSets( array( $post_3 ), wp_list_pluck( $query->posts, 'ID' ) );
    288 
    289         $query = new WP_Query( array(
    290             'meta_query' => array(
    291                     array(
    292                         'key' => 'decimal_value',
    293                         'value' => array( 0.23409845, .31 ),
    294                         'compare' => 'NOT BETWEEN',
    295                         'type' => 'DECIMAL(10,10)'
    296                     )
    297                 ),
    298         ) );
    299         $this->assertEqualSets( array( $post_1, $post_2, $post_4 ), wp_list_pluck( $query->posts, 'ID' ) );
    300 
    301         $query = new WP_Query( array(
    302             'meta_query' => array(
    303                     array(
    304                         'key' => 'decimal_value',
    305                         'value' => '.3',
    306                         'compare' => 'LIKE',
    307                         'type' => 'DECIMAL(10,2)'
    308                     )
    309                 ),
    310         ) );
    311         $this->assertEqualSets( array( $post_1, $post_3 ), wp_list_pluck( $query->posts, 'ID' ) );
    312 
    313         $query = new WP_Query( array(
    314             'meta_query' => array(
    315                     array(
    316                         'key' => 'decimal_value',
    317                         'value' => '.3',
    318                         'compare' => 'NOT LIKE',
    319                         'type' => 'DECIMAL(10,2)'
    320                     )
    321                 ),
    322         ) );
    323         $this->assertEqualSets( array( $post_2, $post_4 ), wp_list_pluck( $query->posts, 'ID' ) );
     630                array(
     631                    'key' => 'decimal_value',
     632                    'value' => '.300',
     633                    'compare' => '=',
     634                    'type' => 'DECIMAL(10,2)'
     635                )
     636            ),
     637            'update_post_meta_cache' => false,
     638            'update_post_term_cache' => false,
     639            'fields' => 'ids',
     640        ) );
     641        $this->assertEqualSets( array( $post_3 ), $query->posts );
     642
     643        $query = new WP_Query( array(
     644            'meta_query' => array(
     645                array(
     646                    'key' => 'decimal_value',
     647                    'value' => '0.35',
     648                    'compare' => '>',
     649                    'type' => 'DECIMAL(10,2)'
     650                )
     651            ),
     652            'update_post_meta_cache' => false,
     653            'update_post_term_cache' => false,
     654            'fields' => 'ids',
     655        ) );
     656        $this->assertEqualSets( array( $post_4 ), $query->posts );
     657
     658        $query = new WP_Query( array(
     659            'meta_query' => array(
     660                array(
     661                    'key' => 'decimal_value',
     662                    'value' => '0.3',
     663                    'compare' => '>=',
     664                    'type' => 'DECIMAL(10,2)'
     665                )
     666            ),
     667            'update_post_meta_cache' => false,
     668            'update_post_term_cache' => false,
     669            'fields' => 'ids',
     670        ) );
     671        $this->assertEqualSets( array( $post_3, $post_4 ), $query->posts );
     672
     673        $query = new WP_Query( array(
     674            'meta_query' => array(
     675                array(
     676                    'key' => 'decimal_value',
     677                    'value' => '0',
     678                    'compare' => '<',
     679                    'type' => 'DECIMAL(10,2)'
     680                )
     681            ),
     682            'update_post_meta_cache' => false,
     683            'update_post_term_cache' => false,
     684            'fields' => 'ids',
     685        ) );
     686        $this->assertEqualSets( array( $post_1 ), $query->posts, 'ID' );
     687
     688        $query = new WP_Query( array(
     689            'meta_query' => array(
     690                array(
     691                    'key' => 'decimal_value',
     692                    'value' => '0.3',
     693                    'compare' => '<=',
     694                    'type' => 'DECIMAL(10,2)'
     695                )
     696            ),
     697            'update_post_meta_cache' => false,
     698            'update_post_term_cache' => false,
     699            'fields' => 'ids',
     700        ) );
     701        $this->assertEqualSets( array( $post_1, $post_2, $post_3 ), $query->posts );
     702
     703        $query = new WP_Query( array(
     704            'meta_query' => array(
     705                array(
     706                    'key' => 'decimal_value',
     707                    'value' => array( 0.23409845, .31 ),
     708                    'compare' => 'BETWEEN',
     709                    'type' => 'DECIMAL(10, 10)'
     710                )
     711            ),
     712            'update_post_meta_cache' => false,
     713            'update_post_term_cache' => false,
     714            'fields' => 'ids',
     715        ) );
     716        $this->assertEqualSets( array( $post_3 ), $query->posts );
     717
     718        $query = new WP_Query( array(
     719            'meta_query' => array(
     720                array(
     721                    'key' => 'decimal_value',
     722                    'value' => array( 0.23409845, .31 ),
     723                    'compare' => 'NOT BETWEEN',
     724                    'type' => 'DECIMAL(10,10)'
     725                )
     726            ),
     727            'update_post_meta_cache' => false,
     728            'update_post_term_cache' => false,
     729            'fields' => 'ids',
     730        ) );
     731        $this->assertEqualSets( array( $post_1, $post_2, $post_4 ), $query->posts );
     732
     733        $query = new WP_Query( array(
     734            'meta_query' => array(
     735                array(
     736                    'key' => 'decimal_value',
     737                    'value' => '.3',
     738                    'compare' => 'LIKE',
     739                    'type' => 'DECIMAL(10,2)'
     740                )
     741            ),
     742            'update_post_meta_cache' => false,
     743            'update_post_term_cache' => false,
     744            'fields' => 'ids',
     745        ) );
     746        $this->assertEqualSets( array( $post_1, $post_3 ), $query->posts );
     747
     748        $query = new WP_Query( array(
     749            'meta_query' => array(
     750                array(
     751                    'key' => 'decimal_value',
     752                    'value' => '.3',
     753                    'compare' => 'NOT LIKE',
     754                    'type' => 'DECIMAL(10,2)'
     755                )
     756            ),
     757            'update_post_meta_cache' => false,
     758            'update_post_term_cache' => false,
     759            'fields' => 'ids',
     760        ) );
     761        $this->assertEqualSets( array( $post_2, $post_4 ), $query->posts );
    324762
    325763        $query = new WP_Query( array(
     
    327765            'order' => 'DESC',
    328766            'meta_key' => 'decimal_value',
    329             'meta_type' => 'DECIMAL(10, 2)'
    330         ) );
    331         $this->assertEqualSets( array( $post_4, $post_3, $post_2, $post_1 ), wp_list_pluck( $query->posts, 'ID' ) );
    332 
     767            'meta_type' => 'DECIMAL(10, 2)',
     768            'update_post_meta_cache' => false,
     769            'update_post_term_cache' => false,
     770            'fields' => 'ids',
     771        ) );
     772        $this->assertEqualSets( array( $post_4, $post_3, $post_2, $post_1 ), $query->posts );
    333773    }
    334774
Note: See TracChangeset for help on using the changeset viewer.