Make WordPress Core

Changeset 26056


Ignore:
Timestamp:
11/08/2013 11:13:41 PM (11 years ago)
Author:
wonderboymusic
Message:

Fix inexcusable whitespace in tests/phpunit/tests/post/query.php.

Props wonderboymusic.

File:
1 edited

Legend:

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

    r26055 r26056  
    55 */
    66class Tests_Post_Query extends WP_UnitTestCase {
    7     function setUp() {
    8     parent::setUp();
    9     }
    10 
    11     function test_meta_key_or_query() {
    12     $post_id = $this->factory->post->create();
    13     add_post_meta( $post_id, 'foo', rand_str() );
    14     add_post_meta( $post_id, 'foo', rand_str() );
    15     $post_id2 = $this->factory->post->create();
    16     add_post_meta( $post_id2, 'bar', 'val2' );
    17     $post_id3 = $this->factory->post->create();
    18     add_post_meta( $post_id3, 'baz', rand_str() );
    19     $post_id4 = $this->factory->post->create();
    20     add_post_meta( $post_id4, 'froo', rand_str() );
    21     $post_id5 = $this->factory->post->create();
    22     add_post_meta( $post_id5, 'tango', 'val2' );
    23     $post_id6 = $this->factory->post->create();
    24     add_post_meta( $post_id6, 'bar', 'val1' );
    25 
    26     $query = new WP_Query( array(
    27         'meta_query' => array(
    28             array(
    29                 'key' => 'foo'
    30             ),
    31             array(
    32                 'key' => 'bar',
    33                 'value' => 'val2'
    34             ),
    35             array(
    36                 'key' => 'baz'
    37             ),
    38             array(
    39                 'key' => 'froo'
    40             ),
    41             'relation' => 'OR',
    42         ),
    43     ) );
    44 
    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() {
    57     $post_id = $this->factory->post->create();
    58     add_post_meta( $post_id, 'foo', rand_str() );
    59     add_post_meta( $post_id, 'foo', rand_str() );
    60     $post_id2 = $this->factory->post->create();
    61     add_post_meta( $post_id2, 'bar', 'val2' );
    62     add_post_meta( $post_id2, 'foo', rand_str() );
    63     $post_id3 = $this->factory->post->create();
    64     add_post_meta( $post_id3, 'baz', rand_str() );
    65     $post_id4 = $this->factory->post->create();
    66     add_post_meta( $post_id4, 'froo', rand_str() );
    67     $post_id5 = $this->factory->post->create();
    68     add_post_meta( $post_id5, 'tango', 'val2' );
    69     $post_id6 = $this->factory->post->create();
    70     add_post_meta( $post_id6, 'bar', 'val1' );
    71     add_post_meta( $post_id6, 'foo', rand_str() );
    72     $post_id7 = $this->factory->post->create();
    73     add_post_meta( $post_id7, 'foo', rand_str() );
    74     add_post_meta( $post_id7, 'froo', rand_str() );
    75     add_post_meta( $post_id7, 'baz', rand_str() );
    76     add_post_meta( $post_id7, 'bar', 'val2' );
    77 
    78     $query = new WP_Query( array(
    79         'meta_query' => array(
    80             array(
    81                 'key' => 'foo'
    82             ),
    83             array(
    84                 'key' => 'bar',
    85                 'value' => 'val2'
    86             ),
    87             array(
    88                 'key' => 'baz'
    89             ),
    90             array(
    91                 'key' => 'froo'
    92             ),
    93             'relation' => 'AND',
    94         ),
    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 );
    106 
    107     $query = new WP_Query( array(
    108         'meta_query' => array(
    109             array(
    110                 'key' => 'foo'
    111             ),
    112             array(
    113                 'key' => 'bar',
    114             ),
    115             'relation' => 'AND',
    116         ),
    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 );
    128     }
    129 
    130     /**
    131     * @ticket 18158
    132     */
    133     function test_meta_key_not_exists() {
    134     $post_id = $this->factory->post->create();
    135     add_post_meta( $post_id, 'foo', rand_str() );
    136     $post_id2 = $this->factory->post->create();
    137     add_post_meta( $post_id2, 'bar', rand_str() );
    138     $post_id3 = $this->factory->post->create();
    139     add_post_meta( $post_id3, 'bar', rand_str() );
    140     $post_id4 = $this->factory->post->create();
    141     add_post_meta( $post_id4, 'baz', rand_str() );
    142     $post_id5 = $this->factory->post->create();
    143     add_post_meta( $post_id5, 'foo', rand_str() );
    144 
    145     $query = new WP_Query( array(
    146         '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 ) );
    200     }
     7    function setUp() {
     8        parent::setUp();
     9    }
     10
     11    function test_meta_key_or_query() {
     12        $post_id = $this->factory->post->create();
     13        add_post_meta( $post_id, 'foo', rand_str() );
     14        add_post_meta( $post_id, 'foo', rand_str() );
     15        $post_id2 = $this->factory->post->create();
     16        add_post_meta( $post_id2, 'bar', 'val2' );
     17        $post_id3 = $this->factory->post->create();
     18        add_post_meta( $post_id3, 'baz', rand_str() );
     19        $post_id4 = $this->factory->post->create();
     20        add_post_meta( $post_id4, 'froo', rand_str() );
     21        $post_id5 = $this->factory->post->create();
     22        add_post_meta( $post_id5, 'tango', 'val2' );
     23        $post_id6 = $this->factory->post->create();
     24        add_post_meta( $post_id6, 'bar', 'val1' );
     25
     26        $query = new WP_Query( array(
     27            'meta_query' => array(
     28                array(
     29                    'key' => 'foo'
     30                ),
     31                array(
     32                    'key' => 'bar',
     33                    'value' => 'val2'
     34                ),
     35                array(
     36                    'key' => 'baz'
     37                ),
     38                array(
     39                    'key' => 'froo'
     40                ),
     41                'relation' => 'OR',
     42            ),
     43        ) );
     44
     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() {
     57        $post_id = $this->factory->post->create();
     58        add_post_meta( $post_id, 'foo', rand_str() );
     59        add_post_meta( $post_id, 'foo', rand_str() );
     60        $post_id2 = $this->factory->post->create();
     61        add_post_meta( $post_id2, 'bar', 'val2' );
     62        add_post_meta( $post_id2, 'foo', rand_str() );
     63        $post_id3 = $this->factory->post->create();
     64        add_post_meta( $post_id3, 'baz', rand_str() );
     65        $post_id4 = $this->factory->post->create();
     66        add_post_meta( $post_id4, 'froo', rand_str() );
     67        $post_id5 = $this->factory->post->create();
     68        add_post_meta( $post_id5, 'tango', 'val2' );
     69        $post_id6 = $this->factory->post->create();
     70        add_post_meta( $post_id6, 'bar', 'val1' );
     71        add_post_meta( $post_id6, 'foo', rand_str() );
     72        $post_id7 = $this->factory->post->create();
     73        add_post_meta( $post_id7, 'foo', rand_str() );
     74        add_post_meta( $post_id7, 'froo', rand_str() );
     75        add_post_meta( $post_id7, 'baz', rand_str() );
     76        add_post_meta( $post_id7, 'bar', 'val2' );
     77
     78        $query = new WP_Query( array(
     79            'meta_query' => array(
     80                array(
     81                    'key' => 'foo'
     82                ),
     83                array(
     84                    'key' => 'bar',
     85                    'value' => 'val2'
     86                ),
     87                array(
     88                    'key' => 'baz'
     89                ),
     90                array(
     91                    'key' => 'froo'
     92                ),
     93                'relation' => 'AND',
     94            ),
     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 );
     106
     107        $query = new WP_Query( array(
     108            'meta_query' => array(
     109                array(
     110                    'key' => 'foo'
     111                ),
     112                array(
     113                    'key' => 'bar',
     114                ),
     115                'relation' => 'AND',
     116            ),
     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 );
     128    }
     129
     130    /**
     131    * @ticket 18158
     132    */
     133    function test_meta_key_not_exists() {
     134        $post_id = $this->factory->post->create();
     135        add_post_meta( $post_id, 'foo', rand_str() );
     136        $post_id2 = $this->factory->post->create();
     137        add_post_meta( $post_id2, 'bar', rand_str() );
     138        $post_id3 = $this->factory->post->create();
     139        add_post_meta( $post_id3, 'bar', rand_str() );
     140        $post_id4 = $this->factory->post->create();
     141        add_post_meta( $post_id4, 'baz', rand_str() );
     142        $post_id5 = $this->factory->post->create();
     143        add_post_meta( $post_id5, 'foo', rand_str() );
     144
     145        $query = new WP_Query( array(
     146            '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 ) );
     200    }
    201201
    202202    /**
     
    333333    }
    334334
    335     /**
    336     * @ticket 20604
    337     */
    338     function test_taxonomy_empty_or() {
    339     // An empty tax query should return an empty array, not all posts.
    340 
    341     $this->factory->post->create_many( 10 );
    342 
    343     $query = new WP_Query( array(
    344         'fields'    => 'ids',
    345         'tax_query' => array(
    346         'relation' => 'OR',
    347         array(
    348             'taxonomy' => 'post_tag',
    349             'field' => 'id',
    350             'terms' => false,
    351             'operator' => 'IN'
    352         ),
    353         array(
    354             'taxonomy' => 'category',
    355             'field' => 'id',
    356             'terms' => false,
    357             'operator' => 'IN'
    358         )
    359         )
    360     ) );
    361 
    362     $posts = $query->get_posts();
    363     $this->assertEquals( 0 , count( $posts ) );
    364     }
    365 
    366     function test_meta_between_not_between() {
    367     $post_id = $this->factory->post->create();
    368     add_post_meta( $post_id, 'time', 500 );
    369     $post_id2 = $this->factory->post->create();
    370     add_post_meta( $post_id2, 'time', 1001 );
    371     $post_id3 = $this->factory->post->create();
    372     add_post_meta( $post_id3, 'time', 0 );
    373     $post_id4 = $this->factory->post->create();
    374     add_post_meta( $post_id4, 'time', 1 );
    375     $post_id5 = $this->factory->post->create();
    376     add_post_meta( $post_id5, 'time', 1000 );
    377 
    378     $args = array(
    379         'meta_key' => 'time',
    380         'meta_value' => array( 1, 1000 ),
    381         'meta_type' => 'numeric',
    382         'meta_compare' => 'NOT BETWEEN'
    383         );
    384 
    385     $query = new WP_Query( $args );
    386     $this->assertEquals( 2, count ( $query->posts ) );
    387     foreach ( $query->posts as $post ) {
    388         $this->assertInstanceOf( 'WP_Post', $post );
    389         $this->assertEquals( 'raw', $post->filter );
    390     }
    391     $posts = wp_list_pluck( $query->posts, 'ID' );
    392     $this->assertEqualSets( array( $post_id2, $post_id3 ), $posts );
    393 
    394     $args = array(
    395         'meta_key' => 'time',
    396         'meta_value' => array( 1, 1000 ),
    397         'meta_type' => 'numeric',
    398         'meta_compare' => 'BETWEEN'
    399         );
    400 
    401     $query = new WP_Query( $args );
    402     $this->assertEquals( 3, count ( $query->posts ) );
    403     foreach ( $query->posts as $post ) {
    404         $this->assertInstanceOf( 'WP_Post', $post );
    405         $this->assertEquals( 'raw', $post->filter );
    406     }
    407     $posts = wp_list_pluck( $query->posts, 'ID' );
    408     $this->assertEqualSets( array( $post_id, $post_id4, $post_id5 ), $posts );
    409     }
    410 
    411     /**
    412     * @ticket 16829
    413     */
    414     function test_meta_default_compare() {
    415     // compare should default to IN when meta_value is an array
    416     $post_id = $this->factory->post->create();
    417     add_post_meta( $post_id, 'foo', 'bar' );
    418     $post_id2 = $this->factory->post->create();
    419     add_post_meta( $post_id2, 'bar', 'baz' );
    420     $post_id3 = $this->factory->post->create();
    421     add_post_meta( $post_id3, 'foo', 'baz' );
    422     $post_id4 = $this->factory->post->create();
    423     add_post_meta( $post_id4, 'baz', 'bar' );
    424     $post_id5 = $this->factory->post->create();
    425     add_post_meta( $post_id5, 'foo', rand_str() );
    426 
    427     $posts = get_posts( array(
    428         'meta_key' => 'foo',
    429         'meta_value' => array( 'bar', 'baz' )
    430     ) );
    431 
    432     $this->assertEquals( 2, count( $posts ) );
    433     $posts = wp_list_pluck( $posts, 'ID' );
    434     $this->assertEqualSets( array( $post_id, $post_id3 ), $posts );
    435 
    436     $posts = get_posts( array(
    437         'meta_key' => 'foo',
    438         'meta_value' => array( 'bar', 'baz' ),
    439         'meta_compare' => 'IN'
    440     ) );
    441 
    442     $this->assertEquals( 2, count( $posts ) );
    443     foreach ( $posts as $post ) {
    444         $this->assertInstanceOf( 'WP_Post', $post );
    445         $this->assertEquals( 'raw', $post->filter );
    446     }
    447     $posts = wp_list_pluck( $posts, 'ID' );
    448     $this->assertEqualSets( array( $post_id, $post_id3 ), $posts );
    449     }
    450 
    451     /**
    452     * @ticket 17264
    453     */
    454     function test_duplicate_posts_when_no_key() {
    455     $post_id = $this->factory->post->create();
    456     add_post_meta( $post_id, 'city', 'Lorem' );
    457     add_post_meta( $post_id, 'address', '123 Lorem St.' );
    458     $post_id2 = $this->factory->post->create();
    459     add_post_meta( $post_id2, 'city', 'Lorem' );
    460     $post_id3 = $this->factory->post->create();
    461     add_post_meta( $post_id3, 'city', 'Loren' );
    462 
    463     $args = array(
    464         'meta_query' => array(
    465         array(
    466             'value' => 'lorem',
    467             'compare' => 'LIKE'
    468         )
    469         )
    470     );
    471 
    472     $posts = get_posts( $args );
    473     $this->assertEquals( 2, count( $posts ) );
    474     foreach ( $posts as $post ) {
    475         $this->assertInstanceOf( 'WP_Post', $post );
    476         $this->assertEquals( 'raw', $post->filter );
    477     }
    478     $posts = wp_list_pluck( $posts, 'ID' );
    479     $this->assertEqualSets( array( $post_id, $post_id2 ), $posts );
    480     }
    481 
    482     /**
    483     * @ticket 15292
    484     */
    485     function test_empty_meta_value() {
    486     $post_id = $this->factory->post->create();
    487     add_post_meta( $post_id, 'foo', '0' );
    488     add_post_meta( $post_id, 'bar', 0 );
    489     $post_id2 = $this->factory->post->create();
    490     add_post_meta( $post_id2, 'foo', 1 );
    491     $post_id3 = $this->factory->post->create();
    492     add_post_meta( $post_id3, 'baz', 0 );
    493     $post_id4 = $this->factory->post->create();
    494     add_post_meta( $post_id4, 'baz', 0 );
    495     $post_id5 = $this->factory->post->create();
    496     add_post_meta( $post_id5, 'baz', 0 );
    497     add_post_meta( $post_id5, 'bar', '0' );
    498     $post_id6 = $this->factory->post->create();
    499     add_post_meta( $post_id6, 'baz', 0 );
    500 
    501     $posts = get_posts( array( 'meta_key' => 'foo', 'meta_value' => '0' ) );
    502     $this->assertEquals( 1, count ( $posts ) );
    503     foreach ( $posts as $post ) {
    504         $this->assertInstanceOf( 'WP_Post', $post );
    505         $this->assertEquals( 'raw', $post->filter );
    506     }
    507     $this->assertEquals( $post_id, $posts[0]->ID );
    508 
    509     $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => '0' ) );
    510     $this->assertEquals( 2, count ( $posts ) );
    511     foreach ( $posts as $post ) {
    512         $this->assertInstanceOf( 'WP_Post', $post );
    513         $this->assertEquals( 'raw', $post->filter );
    514     }
    515     $posts = wp_list_pluck( $posts, 'ID' );
    516     $this->assertEqualSets( array( $post_id, $post_id5 ), $posts );
    517 
    518     $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => 0 ) );
    519     $this->assertEquals( 2, count ( $posts ) );
    520     foreach ( $posts as $post ) {
    521         $this->assertInstanceOf( 'WP_Post', $post );
    522         $this->assertEquals( 'raw', $post->filter );
    523     }
    524     $posts = wp_list_pluck( $posts, 'ID' );
    525     $this->assertEqualSets( array( $post_id, $post_id5 ), $posts );
    526 
    527     $posts = get_posts( array( 'meta_value' => 0 ) );
    528     $this->assertEquals( 5, count ( $posts ) );
    529     foreach ( $posts as $post ) {
    530         $this->assertInstanceOf( 'WP_Post', $post );
    531         $this->assertEquals( 'raw', $post->filter );
    532     }
    533     $posts = wp_list_pluck( $posts, 'ID' );
    534     $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );
    535 
    536     $posts = get_posts( array( 'meta_value' => '0' ) );
    537     $this->assertEquals( 5, count ( $posts ) );
    538     foreach ( $posts as $post ) {
    539         $this->assertInstanceOf( 'WP_Post', $post );
    540         $this->assertEquals( 'raw', $post->filter );
    541     }
    542     $posts = wp_list_pluck( $posts, 'ID' );
    543     $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );
    544     }
     335    /**
     336    * @ticket 20604
     337    */
     338    function test_taxonomy_empty_or() {
     339        // An empty tax query should return an empty array, not all posts.
     340
     341        $this->factory->post->create_many( 10 );
     342
     343        $query = new WP_Query( array(
     344            'fields'    => 'ids',
     345            'tax_query' => array(
     346            'relation' => 'OR',
     347            array(
     348                'taxonomy' => 'post_tag',
     349                'field' => 'id',
     350                'terms' => false,
     351                'operator' => 'IN'
     352            ),
     353            array(
     354                'taxonomy' => 'category',
     355                'field' => 'id',
     356                'terms' => false,
     357                'operator' => 'IN'
     358            )
     359            )
     360        ) );
     361
     362        $posts = $query->get_posts();
     363        $this->assertEquals( 0 , count( $posts ) );
     364    }
     365
     366    function test_meta_between_not_between() {
     367        $post_id = $this->factory->post->create();
     368        add_post_meta( $post_id, 'time', 500 );
     369        $post_id2 = $this->factory->post->create();
     370        add_post_meta( $post_id2, 'time', 1001 );
     371        $post_id3 = $this->factory->post->create();
     372        add_post_meta( $post_id3, 'time', 0 );
     373        $post_id4 = $this->factory->post->create();
     374        add_post_meta( $post_id4, 'time', 1 );
     375        $post_id5 = $this->factory->post->create();
     376        add_post_meta( $post_id5, 'time', 1000 );
     377
     378        $args = array(
     379            'meta_key' => 'time',
     380            'meta_value' => array( 1, 1000 ),
     381            'meta_type' => 'numeric',
     382            'meta_compare' => 'NOT BETWEEN'
     383            );
     384
     385        $query = new WP_Query( $args );
     386        $this->assertEquals( 2, count ( $query->posts ) );
     387        foreach ( $query->posts as $post ) {
     388            $this->assertInstanceOf( 'WP_Post', $post );
     389            $this->assertEquals( 'raw', $post->filter );
     390        }
     391        $posts = wp_list_pluck( $query->posts, 'ID' );
     392        $this->assertEqualSets( array( $post_id2, $post_id3 ), $posts );
     393
     394        $args = array(
     395            'meta_key' => 'time',
     396            'meta_value' => array( 1, 1000 ),
     397            'meta_type' => 'numeric',
     398            'meta_compare' => 'BETWEEN'
     399            );
     400
     401        $query = new WP_Query( $args );
     402        $this->assertEquals( 3, count ( $query->posts ) );
     403        foreach ( $query->posts as $post ) {
     404            $this->assertInstanceOf( 'WP_Post', $post );
     405            $this->assertEquals( 'raw', $post->filter );
     406        }
     407        $posts = wp_list_pluck( $query->posts, 'ID' );
     408        $this->assertEqualSets( array( $post_id, $post_id4, $post_id5 ), $posts );
     409    }
     410
     411    /**
     412    * @ticket 16829
     413    */
     414    function test_meta_default_compare() {
     415        // compare should default to IN when meta_value is an array
     416        $post_id = $this->factory->post->create();
     417        add_post_meta( $post_id, 'foo', 'bar' );
     418        $post_id2 = $this->factory->post->create();
     419        add_post_meta( $post_id2, 'bar', 'baz' );
     420        $post_id3 = $this->factory->post->create();
     421        add_post_meta( $post_id3, 'foo', 'baz' );
     422        $post_id4 = $this->factory->post->create();
     423        add_post_meta( $post_id4, 'baz', 'bar' );
     424        $post_id5 = $this->factory->post->create();
     425        add_post_meta( $post_id5, 'foo', rand_str() );
     426
     427        $posts = get_posts( array(
     428            'meta_key' => 'foo',
     429            'meta_value' => array( 'bar', 'baz' )
     430        ) );
     431
     432        $this->assertEquals( 2, count( $posts ) );
     433        $posts = wp_list_pluck( $posts, 'ID' );
     434        $this->assertEqualSets( array( $post_id, $post_id3 ), $posts );
     435
     436        $posts = get_posts( array(
     437            'meta_key' => 'foo',
     438            'meta_value' => array( 'bar', 'baz' ),
     439            'meta_compare' => 'IN'
     440        ) );
     441
     442        $this->assertEquals( 2, count( $posts ) );
     443        foreach ( $posts as $post ) {
     444            $this->assertInstanceOf( 'WP_Post', $post );
     445            $this->assertEquals( 'raw', $post->filter );
     446        }
     447        $posts = wp_list_pluck( $posts, 'ID' );
     448        $this->assertEqualSets( array( $post_id, $post_id3 ), $posts );
     449    }
     450
     451    /**
     452    * @ticket 17264
     453    */
     454    function test_duplicate_posts_when_no_key() {
     455        $post_id = $this->factory->post->create();
     456        add_post_meta( $post_id, 'city', 'Lorem' );
     457        add_post_meta( $post_id, 'address', '123 Lorem St.' );
     458        $post_id2 = $this->factory->post->create();
     459        add_post_meta( $post_id2, 'city', 'Lorem' );
     460        $post_id3 = $this->factory->post->create();
     461        add_post_meta( $post_id3, 'city', 'Loren' );
     462
     463        $args = array(
     464            'meta_query' => array(
     465            array(
     466                'value' => 'lorem',
     467                'compare' => 'LIKE'
     468            )
     469            )
     470        );
     471
     472        $posts = get_posts( $args );
     473        $this->assertEquals( 2, count( $posts ) );
     474        foreach ( $posts as $post ) {
     475            $this->assertInstanceOf( 'WP_Post', $post );
     476            $this->assertEquals( 'raw', $post->filter );
     477        }
     478        $posts = wp_list_pluck( $posts, 'ID' );
     479        $this->assertEqualSets( array( $post_id, $post_id2 ), $posts );
     480    }
     481
     482    /**
     483    * @ticket 15292
     484    */
     485    function test_empty_meta_value() {
     486        $post_id = $this->factory->post->create();
     487        add_post_meta( $post_id, 'foo', '0' );
     488        add_post_meta( $post_id, 'bar', 0 );
     489        $post_id2 = $this->factory->post->create();
     490        add_post_meta( $post_id2, 'foo', 1 );
     491        $post_id3 = $this->factory->post->create();
     492        add_post_meta( $post_id3, 'baz', 0 );
     493        $post_id4 = $this->factory->post->create();
     494        add_post_meta( $post_id4, 'baz', 0 );
     495        $post_id5 = $this->factory->post->create();
     496        add_post_meta( $post_id5, 'baz', 0 );
     497        add_post_meta( $post_id5, 'bar', '0' );
     498        $post_id6 = $this->factory->post->create();
     499        add_post_meta( $post_id6, 'baz', 0 );
     500
     501        $posts = get_posts( array( 'meta_key' => 'foo', 'meta_value' => '0' ) );
     502        $this->assertEquals( 1, count ( $posts ) );
     503        foreach ( $posts as $post ) {
     504            $this->assertInstanceOf( 'WP_Post', $post );
     505            $this->assertEquals( 'raw', $post->filter );
     506        }
     507        $this->assertEquals( $post_id, $posts[0]->ID );
     508
     509        $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => '0' ) );
     510        $this->assertEquals( 2, count ( $posts ) );
     511        foreach ( $posts as $post ) {
     512            $this->assertInstanceOf( 'WP_Post', $post );
     513            $this->assertEquals( 'raw', $post->filter );
     514        }
     515        $posts = wp_list_pluck( $posts, 'ID' );
     516        $this->assertEqualSets( array( $post_id, $post_id5 ), $posts );
     517
     518        $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => 0 ) );
     519        $this->assertEquals( 2, count ( $posts ) );
     520        foreach ( $posts as $post ) {
     521            $this->assertInstanceOf( 'WP_Post', $post );
     522            $this->assertEquals( 'raw', $post->filter );
     523        }
     524        $posts = wp_list_pluck( $posts, 'ID' );
     525        $this->assertEqualSets( array( $post_id, $post_id5 ), $posts );
     526
     527        $posts = get_posts( array( 'meta_value' => 0 ) );
     528        $this->assertEquals( 5, count ( $posts ) );
     529        foreach ( $posts as $post ) {
     530            $this->assertInstanceOf( 'WP_Post', $post );
     531            $this->assertEquals( 'raw', $post->filter );
     532        }
     533        $posts = wp_list_pluck( $posts, 'ID' );
     534        $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );
     535
     536        $posts = get_posts( array( 'meta_value' => '0' ) );
     537        $this->assertEquals( 5, count ( $posts ) );
     538        foreach ( $posts as $post ) {
     539            $this->assertInstanceOf( 'WP_Post', $post );
     540            $this->assertEquals( 'raw', $post->filter );
     541        }
     542        $posts = wp_list_pluck( $posts, 'ID' );
     543        $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );
     544    }
    545545
    546546    function test_taxonomy_include_children() {
     
    556556
    557557        $posts = get_posts( array(
    558             'tax_query' => array(
     558            'tax_query' => array(
    559559                array(
    560560                    'taxonomy' => 'category',
     
    562562                    'terms' => array( $cat_a ),
    563563                )
    564             )
     564            )
    565565        ) );
    566566
     
    568568
    569569        $posts = get_posts( array(
    570             'tax_query' => array(
     570            'tax_query' => array(
    571571                array(
    572572                    'taxonomy' => 'category',
     
    575575                    'include_children' => false
    576576                )
    577             )
     577            )
    578578        ) );
    579579
     
    581581
    582582        $posts = get_posts( array(
    583             'tax_query' => array(
     583            'tax_query' => array(
    584584                array(
    585585                    'taxonomy' => 'category',
     
    587587                    'terms' => array( $cat_b ),
    588588                )
    589             )
     589            )
    590590        ) );
    591591
     
    593593
    594594        $posts = get_posts( array(
    595             'tax_query' => array(
     595            'tax_query' => array(
    596596                array(
    597597                    'taxonomy' => 'category',
     
    600600                    'include_children' => false
    601601                )
    602             )
     602            )
    603603        ) );
    604604
     
    606606
    607607        $posts = get_posts( array(
    608             'tax_query' => array(
     608            'tax_query' => array(
    609609                array(
    610610                    'taxonomy' => 'category',
     
    612612                    'terms' => array( $cat_c ),
    613613                )
    614             )
     614            )
    615615        ) );
    616616
     
    618618
    619619        $posts = get_posts( array(
    620             'tax_query' => array(
     620            'tax_query' => array(
    621621                array(
    622622                    'taxonomy' => 'category',
     
    625625                    'include_children' => false
    626626                )
    627             )
     627            )
    628628        ) );
    629629
Note: See TracChangeset for help on using the changeset viewer.