Make WordPress Core


Ignore:
Timestamp:
01/27/2015 08:03:50 PM (12 years ago)
Author:
boonebgorges
Message:

Improve organiation of tax_query and meta_query unit tests.

meta_query tests have been moved to tests/phpunit/tests/query/metaQuery.php and tax_query tests to tests/phpunit/tests/query/taxQuery.php. This is an improvement because (a) it better corresponds to the way that other WP_Query parameter tests are organized, (b) splitting meta and tax tests into separate classes simplifies the required @group annotations, and (c) the tests have nothing to do with posts per se, and so do not belong in the post subdirectory.

The tests previously found at tests/phpunit/tests/query/taxQuery.php have been moved to isTerm.php in the same directory. These tests are related to the is_* functions that have to do with taxonomy terms, like is_category().

See #26999.

File:
1 edited

Legend:

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

    r30771 r31286  
    22
    33/**
    4  * Test the is_*() functions in query.php across the URL structure
    5  *
    6  * This exercises both query.php and rewrite.php: urls are fed through the rewrite code,
    7  * then we test the effects of each url on the wp_query object.
    8  *
    94 * @group query
    10  * @group rewrite
     5 * @group taxonomy
    116 */
    127class Tests_Query_TaxQuery extends WP_UnitTestCase {
    13         protected $tag_id;
    14         protected $cat_id;
    15         protected $tax_id;
    16         protected $tax_id2;
    17         protected $post_id;
    18 
    19         protected $cat;
    20         protected $uncat;
    21         protected $tag;
    22         protected $tax;
    23 
    24         function setUp() {
    25                 global $wp_rewrite;
    26                 parent::setUp();
    27 
    28                 set_current_screen( 'front' );
    29 
    30                 $GLOBALS['wp_the_query'] = new WP_Query();
    31                 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
    32 
    33                 $wp_rewrite->init();
    34                 $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    35 
    36                 create_initial_taxonomies();
    37                 register_taxonomy( 'testtax', 'post', array( 'public' => true ) );
    38 
    39                 $wp_rewrite->flush_rules();
    40 
    41                 $this->tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) );
    42                 $this->cat_id = $this->factory->category->create( array( 'slug' => 'cat-slug' ) );
    43                 $this->tax_id = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug' ) );
    44                 $this->tax_id2 = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug2' ) );
    45                 $this->post_id = $this->factory->post->create();
    46                 wp_set_object_terms( $this->post_id, $this->cat_id, 'category' );
    47                 wp_set_object_terms( $this->post_id, array( $this->tax_id, $this->tax_id2 ), 'testtax' );
    48 
    49                 $this->cat = get_term( $this->cat_id, 'category' );
    50                 _make_cat_compat( $this->cat );
    51                 $this->tag = get_term( $this->tag_id, 'post_tag' );
    52 
    53                 $this->uncat = get_term_by( 'slug', 'uncategorized', 'category' );
    54                 _make_cat_compat( $this->uncat );
    55 
    56                 add_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
    57         }
    58 
    59         function tearDown() {
    60                 global $wp_rewrite;
    61                 parent::tearDown();
    62 
    63                 _unregister_taxonomy( 'testtax' );
    64 
    65                 $wp_rewrite->init();
    66 
    67                 remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
    68         }
    69 
    70         function test_tag_action_tax() {
    71                 // tag with tax added
    72                 $this->go_to( home_url( "/tag/tag-slug/" ) );
    73                 $this->assertQueryTrue( 'is_tag', 'is_archive' );
    74                 $this->assertNotEmpty( get_query_var( 'tax_query' ) );
    75                 $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
    76                 $this->assertNotEmpty( get_query_var( 'term_id' ) );
    77                 $this->assertNotEmpty( get_query_var( 'tag_id' ) );
    78                 $this->assertEquals( get_queried_object(), $this->tag );
    79         }
    80 
    81         function test_tag_query_cat_action_tax() {
    82                 // tag + category with tax added
    83                 $this->go_to( home_url( "/tag/tag-slug/?cat=$this->cat_id" ) );
    84                 $this->assertQueryTrue( 'is_category', 'is_tag', 'is_archive' );
    85                 $this->assertNotEmpty( get_query_var( 'tax_query' ) );
    86                 $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
    87                 $this->assertNotEmpty( get_query_var( 'term_id' ) );
    88                 $this->assertNotEmpty( get_query_var( 'cat' ) );
    89                 $this->assertNotEmpty( get_query_var( 'tag_id' ) );
    90                 $this->assertEquals( get_queried_object(), $this->cat );
    91         }
    92 
    93         function test_tag_query_cat_query_tax_action_tax() {
    94                 // tag + category + tax with tax added
    95                 $this->go_to( home_url( "/tag/tag-slug/?cat=$this->cat_id&testtax=tax-slug2" ) );
    96                 $this->assertQueryTrue( 'is_category', 'is_tag', 'is_tax', 'is_archive' );
    97                 $this->assertNotEmpty( get_query_var( 'tax_query' ) );
    98                 $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
    99                 $this->assertNotEmpty( get_query_var( 'term_id' ) );
    100                 $this->assertNotEmpty( get_query_var( 'cat' ) );
    101                 $this->assertNotEmpty( get_query_var( 'tag_id' ) );
    102                 $this->assertNotEmpty( get_query_var( 'testtax' ) );
    103                 $this->assertEquals( get_queried_object(), $this->cat );
    104         }
    105 
    106         function test_cat_action_tax() {
    107                 // category with tax added
    108                 $this->go_to( home_url( "/category/cat-slug/" ) );
    109                 $this->assertQueryTrue( 'is_category', 'is_archive' );
    110                 $this->assertNotEmpty( get_query_var( 'cat' ) );
    111                 $this->assertNotEmpty( get_query_var( 'tax_query' ) );
    112                 $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
    113                 $this->assertNotEmpty( get_query_var( 'term_id' ) );
    114                 $this->assertEquals( get_queried_object(), $this->cat );
     8        public function test_tax_query_single_query_single_term_field_slug() {
     9                $t = $this->factory->term->create( array(
     10                        'taxonomy' => 'category',
     11                        'slug' => 'foo',
     12                        'name' => 'Foo',
     13                ) );
     14                $p1 = $this->factory->post->create();
     15                $p2 = $this->factory->post->create();
     16
     17                wp_set_post_terms( $p1, $t, 'category' );
     18
     19                $q = new WP_Query( array(
     20                        'fields' => 'ids',
     21                        'update_post_meta_cache' => false,
     22                        'update_post_term_cache' => false,
     23                        'tax_query' => array(
     24                                array(
     25                                        'taxonomy' => 'category',
     26                                        'terms' => array( 'foo' ),
     27                                        'field' => 'slug',
     28                                ),
     29                        ),
     30                ) );
     31
     32                $this->assertEquals( array( $p1 ), $q->posts );
     33        }
     34
     35        public function test_tax_query_single_query_single_term_field_name() {
     36                $t = $this->factory->term->create( array(
     37                        'taxonomy' => 'category',
     38                        'slug' => 'foo',
     39                        'name' => 'Foo',
     40                ) );
     41                $p1 = $this->factory->post->create();
     42                $p2 = $this->factory->post->create();
     43
     44                wp_set_post_terms( $p1, $t, 'category' );
     45
     46                $q = new WP_Query( array(
     47                        'fields' => 'ids',
     48                        'update_post_meta_cache' => false,
     49                        'update_post_term_cache' => false,
     50                        'tax_query' => array(
     51                                array(
     52                                        'taxonomy' => 'category',
     53                                        'terms' => array( 'Foo' ),
     54                                        'field' => 'name',
     55                                ),
     56                        ),
     57                ) );
     58
     59                $this->assertEquals( array( $p1 ), $q->posts );
     60        }
     61
     62        public function test_tax_query_single_query_single_term_field_term_taxonomy_id() {
     63                $t = $this->factory->term->create( array(
     64                        'taxonomy' => 'category',
     65                        'slug' => 'foo',
     66                        'name' => 'Foo',
     67                ) );
     68                $p1 = $this->factory->post->create();
     69                $p2 = $this->factory->post->create();
     70
     71                $tt_ids = wp_set_post_terms( $p1, $t, 'category' );
     72
     73                $q = new WP_Query( array(
     74                        'fields' => 'ids',
     75                        'update_post_meta_cache' => false,
     76                        'update_post_term_cache' => false,
     77                        'tax_query' => array(
     78                                array(
     79                                        'taxonomy' => 'category',
     80                                        'terms' => $tt_ids,
     81                                        'field' => 'term_taxonomy_id',
     82                                ),
     83                        ),
     84                ) );
     85
     86                $this->assertEquals( array( $p1 ), $q->posts );
     87        }
     88
     89        public function test_tax_query_single_query_single_term_field_term_id() {
     90                $t = $this->factory->term->create( array(
     91                        'taxonomy' => 'category',
     92                        'slug' => 'foo',
     93                        'name' => 'Foo',
     94                ) );
     95                $p1 = $this->factory->post->create();
     96                $p2 = $this->factory->post->create();
     97
     98                wp_set_post_terms( $p1, $t, 'category' );
     99
     100                $q = new WP_Query( array(
     101                        'fields' => 'ids',
     102                        'update_post_meta_cache' => false,
     103                        'update_post_term_cache' => false,
     104                        'tax_query' => array(
     105                                array(
     106                                        'taxonomy' => 'category',
     107                                        'terms' => array( $t ),
     108                                        'field' => 'term_id',
     109                                ),
     110                        ),
     111                ) );
     112
     113                $this->assertEquals( array( $p1 ), $q->posts );
     114        }
     115
     116        public function test_tax_query_single_query_single_term_operator_in() {
     117                $t = $this->factory->term->create( array(
     118                        'taxonomy' => 'category',
     119                        'slug' => 'foo',
     120                        'name' => 'Foo',
     121                ) );
     122                $p1 = $this->factory->post->create();
     123                $p2 = $this->factory->post->create();
     124
     125                wp_set_post_terms( $p1, $t, 'category' );
     126
     127                $q = new WP_Query( array(
     128                        'fields' => 'ids',
     129                        'update_post_meta_cache' => false,
     130                        'update_post_term_cache' => false,
     131                        'tax_query' => array(
     132                                array(
     133                                        'taxonomy' => 'category',
     134                                        'terms' => array( 'foo' ),
     135                                        'field' => 'slug',
     136                                        'operator' => 'IN',
     137                                ),
     138                        ),
     139                ) );
     140
     141                $this->assertEquals( array( $p1 ), $q->posts );
     142        }
     143
     144        public function test_tax_query_single_query_single_term_operator_not_in() {
     145                $t = $this->factory->term->create( array(
     146                        'taxonomy' => 'category',
     147                        'slug' => 'foo',
     148                        'name' => 'Foo',
     149                ) );
     150                $p1 = $this->factory->post->create();
     151                $p2 = $this->factory->post->create();
     152
     153                wp_set_post_terms( $p1, $t, 'category' );
     154
     155                $q = new WP_Query( array(
     156                        'fields' => 'ids',
     157                        'update_post_meta_cache' => false,
     158                        'update_post_term_cache' => false,
     159                        'tax_query' => array(
     160                                array(
     161                                        'taxonomy' => 'category',
     162                                        'terms' => array( 'foo' ),
     163                                        'field' => 'slug',
     164                                        'operator' => 'NOT IN',
     165                                ),
     166                        ),
     167                ) );
     168
     169                $this->assertEquals( array( $p2 ), $q->posts );
     170        }
     171
     172        public function test_tax_query_single_query_single_term_operator_and() {
     173                $t = $this->factory->term->create( array(
     174                        'taxonomy' => 'category',
     175                        'slug' => 'foo',
     176                        'name' => 'Foo',
     177                ) );
     178                $p1 = $this->factory->post->create();
     179                $p2 = $this->factory->post->create();
     180
     181                wp_set_post_terms( $p1, $t, 'category' );
     182
     183                $q = new WP_Query( array(
     184                        'fields' => 'ids',
     185                        'update_post_meta_cache' => false,
     186                        'update_post_term_cache' => false,
     187                        'tax_query' => array(
     188                                array(
     189                                        'taxonomy' => 'category',
     190                                        'terms' => array( 'foo' ),
     191                                        'field' => 'slug',
     192                                        'operator' => 'AND',
     193                                ),
     194                        ),
     195                ) );
     196
     197                $this->assertEquals( array( $p1 ), $q->posts );
     198        }
     199
     200        public function test_tax_query_single_query_multiple_terms_operator_in() {
     201                $t1 = $this->factory->term->create( array(
     202                        'taxonomy' => 'category',
     203                        'slug' => 'foo',
     204                        'name' => 'Foo',
     205                ) );
     206                $t2 = $this->factory->term->create( array(
     207                        'taxonomy' => 'category',
     208                        'slug' => 'bar',
     209                        'name' => 'Bar',
     210                ) );
     211                $p1 = $this->factory->post->create();
     212                $p2 = $this->factory->post->create();
     213                $p3 = $this->factory->post->create();
     214
     215                wp_set_post_terms( $p1, $t1, 'category' );
     216                wp_set_post_terms( $p2, $t2, 'category' );
     217
     218                $q = new WP_Query( array(
     219                        'fields' => 'ids',
     220                        'update_post_meta_cache' => false,
     221                        'update_post_term_cache' => false,
     222                        'tax_query' => array(
     223                                array(
     224                                        'taxonomy' => 'category',
     225                                        'terms' => array( 'foo', 'bar' ),
     226                                        'field' => 'slug',
     227                                        'operator' => 'IN',
     228                                ),
     229                        ),
     230                ) );
     231
     232                $this->assertEqualSets( array( $p1, $p2 ), $q->posts );
     233        }
     234
     235        public function test_tax_query_single_query_multiple_terms_operator_not_in() {
     236                $t1 = $this->factory->term->create( array(
     237                        'taxonomy' => 'category',
     238                        'slug' => 'foo',
     239                        'name' => 'Foo',
     240                ) );
     241                $t2 = $this->factory->term->create( array(
     242                        'taxonomy' => 'category',
     243                        'slug' => 'bar',
     244                        'name' => 'Bar',
     245                ) );
     246                $p1 = $this->factory->post->create();
     247                $p2 = $this->factory->post->create();
     248                $p3 = $this->factory->post->create();
     249
     250                wp_set_post_terms( $p1, $t1, 'category' );
     251                wp_set_post_terms( $p2, $t2, 'category' );
     252
     253                $q = new WP_Query( array(
     254                        'fields' => 'ids',
     255                        'update_post_meta_cache' => false,
     256                        'update_post_term_cache' => false,
     257                        'tax_query' => array(
     258                                array(
     259                                        'taxonomy' => 'category',
     260                                        'terms' => array( 'foo', 'bar' ),
     261                                        'field' => 'slug',
     262                                        'operator' => 'NOT IN',
     263                                ),
     264                        ),
     265                ) );
     266
     267                $this->assertEquals( array( $p3 ), $q->posts );
    115268        }
    116269
    117270        /**
    118          * @ticket 26627
     271         * @ticket 18105
    119272         */
    120         function test_cat_uncat_action_tax() {
    121                 // category with tax added
    122                 add_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 );
    123 
    124                 $this->go_to( home_url( "/category/uncategorized/" ) );
    125                 $this->assertQueryTrue( 'is_category', 'is_archive' );
    126                 $this->assertNotEmpty( get_query_var( 'cat' ) );
    127                 $this->assertNotEmpty( get_query_var( 'tax_query' ) );
    128                 $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
    129                 $this->assertNotEmpty( get_query_var( 'term_id' ) );
    130                 $this->assertEquals( get_queried_object(), $this->uncat );
    131 
    132                 remove_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 );
    133         }
    134 
    135         function _cat_uncat_action_tax( &$query ) {
    136                 $this->assertTrue( $query->is_category() );
    137                 $this->assertTrue( $query->is_archive() );
    138                 $this->assertNotEmpty( $query->get( 'category_name' ) );
    139                 $this->assertNotEmpty( $query->get( 'tax_query' ) );
    140                 $this->assertEquals( $query->get_queried_object(), $this->uncat );
     273        public function test_tax_query_single_query_multiple_queries_operator_not_in() {
     274                $t1 = $this->factory->term->create( array(
     275                        'taxonomy' => 'category',
     276                        'slug' => 'foo',
     277                        'name' => 'Foo',
     278                ) );
     279                $t2 = $this->factory->term->create( array(
     280                        'taxonomy' => 'category',
     281                        'slug' => 'bar',
     282                        'name' => 'Bar',
     283                ) );
     284                $p1 = $this->factory->post->create();
     285                $p2 = $this->factory->post->create();
     286                $p3 = $this->factory->post->create();
     287
     288                wp_set_post_terms( $p1, $t1, 'category' );
     289                wp_set_post_terms( $p2, $t2, 'category' );
     290
     291                $q = new WP_Query( array(
     292                        'fields' => 'ids',
     293                        'update_post_meta_cache' => false,
     294                        'update_post_term_cache' => false,
     295                        'tax_query' => array(
     296                                'relation' => 'AND',
     297                                array(
     298                                        'taxonomy' => 'category',
     299                                        'terms' => array( 'foo' ),
     300                                        'field' => 'slug',
     301                                        'operator' => 'NOT IN',
     302                                ),
     303                                array(
     304                                        'taxonomy' => 'category',
     305                                        'terms' => array( 'bar' ),
     306                                        'field' => 'slug',
     307                                        'operator' => 'NOT IN',
     308                                ),
     309                        ),
     310                ) );
     311
     312                $this->assertEquals( array( $p3 ), $q->posts );
     313        }
     314
     315        public function test_tax_query_single_query_multiple_terms_operator_and() {
     316                $t1 = $this->factory->term->create( array(
     317                        'taxonomy' => 'category',
     318                        'slug' => 'foo',
     319                        'name' => 'Foo',
     320                ) );
     321                $t2 = $this->factory->term->create( array(
     322                        'taxonomy' => 'category',
     323                        'slug' => 'bar',
     324                        'name' => 'Bar',
     325                ) );
     326                $p1 = $this->factory->post->create();
     327                $p2 = $this->factory->post->create();
     328                $p3 = $this->factory->post->create();
     329
     330                wp_set_object_terms( $p1, $t1, 'category' );
     331                wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
     332
     333                $q = new WP_Query( array(
     334                        'fields' => 'ids',
     335                        'update_post_meta_cache' => false,
     336                        'update_post_term_cache' => false,
     337                        'tax_query' => array(
     338                                array(
     339                                        'taxonomy' => 'category',
     340                                        'terms' => array( 'foo', 'bar' ),
     341                                        'field' => 'slug',
     342                                        'operator' => 'AND',
     343                                ),
     344                        ),
     345                ) );
     346
     347                $this->assertEquals( array( $p2 ), $q->posts );
    141348        }
    142349
    143350        /**
    144          * @ticket 26728
     351         * @ticket 29181
    145352         */
    146         function test_tax_action_tax() {
    147                 // tax with tax added
    148                 $this->go_to( home_url( '/testtax/tax-slug2/' ) );
    149                 $this->assertQueryTrue( 'is_tax', 'is_archive' );
    150                 $this->assertNotEmpty( get_query_var( 'tax_query' ) );
    151                 $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
    152                 $this->assertNotEmpty( get_query_var( 'term_id' ) );
    153                 $this->assertEquals( get_queried_object(), get_term( $this->tax_id, 'testtax' ) );
    154         }
    155 
    156         function test_tax_query_tag_action_tax() {
    157                 // tax + tag with tax added
    158                 $this->go_to( home_url( "/testtax/tax-slug2/?tag_id=$this->tag_id" ) );
    159                 $this->assertQueryTrue( 'is_tag', 'is_tax', 'is_archive' );
    160                 $this->assertNotEmpty( get_query_var( 'tax_query' ) );
    161                 $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
    162                 $this->assertNotEmpty( get_query_var( 'term_id' ) );
    163                 $this->assertNotEmpty( get_query_var( 'tag_id' ) );
    164                 $this->assertEquals( get_queried_object(), $this->tag );
    165         }
    166 
    167         function test_tax_query_cat_action_tax() {
    168                 // tax + cat with tax added
    169                 $this->go_to( home_url( "/testtax/tax-slug2/?cat=$this->cat_id" ) );
    170                 $this->assertQueryTrue( 'is_category', 'is_tax', 'is_archive' );
    171                 $this->assertNotEmpty( get_query_var( 'tax_query' ) );
    172                 $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
    173                 $this->assertNotEmpty( get_query_var( 'term_id' ) );
    174                 $this->assertNotEmpty( get_query_var( 'cat' ) );
    175                 $this->assertEquals( get_queried_object(), $this->cat );
    176         }
    177 
    178         function pre_get_posts_tax_category_tax_query( &$query ) {
    179                 $query->set( 'tax_query', array(
    180                         array( 'taxonomy' => 'testtax', 'field' => 'term_id', 'terms' => $this->tax_id )
    181                 ) );
     353        public function test_tax_query_operator_not_exists() {
     354                register_taxonomy( 'wptests_tax1', 'post' );
     355                register_taxonomy( 'wptests_tax2', 'post' );
     356
     357                $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
     358                $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
     359
     360                $p1 = $this->factory->post->create();
     361                $p2 = $this->factory->post->create();
     362                $p3 = $this->factory->post->create();
     363
     364                wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );
     365                wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );
     366
     367                $q = new WP_Query( array(
     368                        'fields' => 'ids',
     369                        'orderby' => 'ID',
     370                        'order' => 'ASC',
     371                        'tax_query' => array(
     372                                array(
     373                                        'taxonomy' => 'wptests_tax2',
     374                                        'operator' => 'NOT EXISTS',
     375                                ),
     376                        ),
     377                ) );
     378
     379                $this->assertEqualSets( array( $p1, $p3 ), $q->posts );
    182380        }
    183381
    184382        /**
    185          * @group 30623
     383         * @ticket 29181
    186384         */
    187         public function test_get_queried_object_with_custom_taxonomy_tax_query_and_field_term_id_should_return_term_object() {
    188                 // Don't override the args provided below.
    189                 remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
    190 
    191                 $args = array(
     385        public function test_tax_query_operator_exists() {
     386                register_taxonomy( 'wptests_tax1', 'post' );
     387                register_taxonomy( 'wptests_tax2', 'post' );
     388
     389                $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
     390                $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
     391
     392                $p1 = $this->factory->post->create();
     393                $p2 = $this->factory->post->create();
     394                $p3 = $this->factory->post->create();
     395
     396                wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );
     397                wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );
     398
     399                $q = new WP_Query( array(
     400                        'fields' => 'ids',
     401                        'orderby' => 'ID',
     402                        'order' => 'ASC',
     403                        'tax_query' => array(
     404                                array(
     405                                        'taxonomy' => 'wptests_tax2',
     406                                        'operator' => 'EXISTS',
     407                                ),
     408                        ),
     409                ) );
     410
     411                $this->assertEqualSets( array( $p2 ), $q->posts );
     412        }
     413
     414        /**
     415         * @ticket 29181
     416         */
     417        public function test_tax_query_operator_exists_should_ignore_terms() {
     418                register_taxonomy( 'wptests_tax1', 'post' );
     419                register_taxonomy( 'wptests_tax2', 'post' );
     420
     421                $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
     422                $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
     423
     424                $p1 = $this->factory->post->create();
     425                $p2 = $this->factory->post->create();
     426                $p3 = $this->factory->post->create();
     427
     428                wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );
     429                wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );
     430
     431                $q = new WP_Query( array(
     432                        'fields' => 'ids',
     433                        'orderby' => 'ID',
     434                        'order' => 'ASC',
     435                        'tax_query' => array(
     436                                array(
     437                                        'taxonomy' => 'wptests_tax2',
     438                                        'operator' => 'EXISTS',
     439                                        'terms' => array( 'foo', 'bar' ),
     440                                ),
     441                        ),
     442                ) );
     443
     444                $this->assertEqualSets( array( $p2 ), $q->posts );
     445        }
     446
     447        /**
     448         * @ticket 29181
     449         */
     450        public function test_tax_query_operator_exists_with_no_taxonomy() {
     451                register_taxonomy( 'wptests_tax1', 'post' );
     452                register_taxonomy( 'wptests_tax2', 'post' );
     453
     454                $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
     455                $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
     456
     457                $p1 = $this->factory->post->create();
     458                $p2 = $this->factory->post->create();
     459                $p3 = $this->factory->post->create();
     460
     461                wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );
     462                wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );
     463
     464                $q = new WP_Query( array(
     465                        'fields' => 'ids',
     466                        'orderby' => 'ID',
     467                        'order' => 'ASC',
     468                        'tax_query' => array(
     469                                array(
     470                                        'operator' => 'EXISTS',
     471                                ),
     472                        ),
     473                ) );
     474
     475                $this->assertEmpty( $q->posts );
     476        }
     477
     478        public function test_tax_query_multiple_queries_relation_and() {
     479                $t1 = $this->factory->term->create( array(
     480                        'taxonomy' => 'category',
     481                        'slug' => 'foo',
     482                        'name' => 'Foo',
     483                ) );
     484                $t2 = $this->factory->term->create( array(
     485                        'taxonomy' => 'category',
     486                        'slug' => 'bar',
     487                        'name' => 'Bar',
     488                ) );
     489                $p1 = $this->factory->post->create();
     490                $p2 = $this->factory->post->create();
     491                $p3 = $this->factory->post->create();
     492
     493                wp_set_object_terms( $p1, $t1, 'category' );
     494                wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
     495
     496                $q = new WP_Query( array(
     497                        'fields' => 'ids',
     498                        'update_post_meta_cache' => false,
     499                        'update_post_term_cache' => false,
    192500                        'tax_query' => array(
    193501                                'relation' => 'AND',
    194502                                array(
    195                                         'taxonomy' => 'testtax',
     503                                        'taxonomy' => 'category',
     504                                        'terms' => array( 'foo' ),
     505                                        'field' => 'slug',
     506                                ),
     507                                array(
     508                                        'taxonomy' => 'category',
     509                                        'terms' => array( 'bar' ),
     510                                        'field' => 'slug',
     511                                ),
     512                        ),
     513                ) );
     514
     515                $this->assertEquals( array( $p2 ), $q->posts );
     516        }
     517
     518        public function test_tax_query_multiple_queries_relation_or() {
     519                $t1 = $this->factory->term->create( array(
     520                        'taxonomy' => 'category',
     521                        'slug' => 'foo',
     522                        'name' => 'Foo',
     523                ) );
     524                $t2 = $this->factory->term->create( array(
     525                        'taxonomy' => 'category',
     526                        'slug' => 'bar',
     527                        'name' => 'Bar',
     528                ) );
     529                $p1 = $this->factory->post->create();
     530                $p2 = $this->factory->post->create();
     531                $p3 = $this->factory->post->create();
     532
     533                wp_set_object_terms( $p1, $t1, 'category' );
     534                wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
     535
     536                $q = new WP_Query( array(
     537                        'fields' => 'ids',
     538                        'update_post_meta_cache' => false,
     539                        'update_post_term_cache' => false,
     540                        'tax_query' => array(
     541                                'relation' => 'OR',
     542                                array(
     543                                        'taxonomy' => 'category',
     544                                        'terms' => array( 'foo' ),
     545                                        'field' => 'slug',
     546                                ),
     547                                array(
     548                                        'taxonomy' => 'category',
     549                                        'terms' => array( 'bar' ),
     550                                        'field' => 'slug',
     551                                ),
     552                        ),
     553                ) );
     554
     555                $this->assertEqualSets( array( $p1, $p2 ), $q->posts );
     556        }
     557
     558        public function test_tax_query_multiple_queries_different_taxonomies() {
     559                $t1 = $this->factory->term->create( array(
     560                        'taxonomy' => 'post_tag',
     561                        'slug' => 'foo',
     562                        'name' => 'Foo',
     563                ) );
     564                $t2 = $this->factory->term->create( array(
     565                        'taxonomy' => 'category',
     566                        'slug' => 'bar',
     567                        'name' => 'Bar',
     568                ) );
     569                $p1 = $this->factory->post->create();
     570                $p2 = $this->factory->post->create();
     571                $p3 = $this->factory->post->create();
     572
     573                wp_set_object_terms( $p1, $t1, 'post_tag' );
     574                wp_set_object_terms( $p2, $t2, 'category' );
     575
     576                $q = new WP_Query( array(
     577                        'fields' => 'ids',
     578                        'update_post_meta_cache' => false,
     579                        'update_post_term_cache' => false,
     580                        'tax_query' => array(
     581                                'relation' => 'OR',
     582                                array(
     583                                        'taxonomy' => 'post_tag',
     584                                        'terms' => array( 'foo' ),
     585                                        'field' => 'slug',
     586                                ),
     587                                array(
     588                                        'taxonomy' => 'category',
     589                                        'terms' => array( 'bar' ),
     590                                        'field' => 'slug',
     591                                ),
     592                        ),
     593                ) );
     594
     595                $this->assertEqualSets( array( $p1, $p2 ), $q->posts );
     596        }
     597
     598        /**
     599         * @ticket 29738
     600         */
     601        public function test_tax_query_two_nested_queries() {
     602                register_taxonomy( 'foo', 'post' );
     603                register_taxonomy( 'bar', 'post' );
     604
     605                $foo_term_1 = $this->factory->term->create( array(
     606                        'taxonomy' => 'foo',
     607                ) );
     608                $foo_term_2 = $this->factory->term->create( array(
     609                        'taxonomy' => 'foo',
     610                ) );
     611                $bar_term_1 = $this->factory->term->create( array(
     612                        'taxonomy' => 'bar',
     613                ) );
     614                $bar_term_2 = $this->factory->term->create( array(
     615                        'taxonomy' => 'bar',
     616                ) );
     617
     618                $p1 = $this->factory->post->create();
     619                $p2 = $this->factory->post->create();
     620                $p3 = $this->factory->post->create();
     621
     622                wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );
     623                wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );
     624                wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );
     625                wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );
     626                wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );
     627                wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );
     628
     629                $q = new WP_Query( array(
     630                        'fields' => 'ids',
     631                        'update_post_meta_cache' => false,
     632                        'update_post_term_cache' => false,
     633                        'tax_query' => array(
     634                                'relation' => 'OR',
     635                                array(
     636                                        'relation' => 'AND',
     637                                        array(
     638                                                'taxonomy' => 'foo',
     639                                                'terms' => array( $foo_term_1 ),
     640                                                'field' => 'term_id',
     641                                        ),
     642                                        array(
     643                                                'taxonomy' => 'bar',
     644                                                'terms' => array( $bar_term_1 ),
     645                                                'field' => 'term_id',
     646                                        ),
     647                                ),
     648                                array(
     649                                        'relation' => 'AND',
     650                                        array(
     651                                                'taxonomy' => 'foo',
     652                                                'terms' => array( $foo_term_2 ),
     653                                                'field' => 'term_id',
     654                                        ),
     655                                        array(
     656                                                'taxonomy' => 'bar',
     657                                                'terms' => array( $bar_term_2 ),
     658                                                'field' => 'term_id',
     659                                        ),
     660                                ),
     661                        ),
     662                ) );
     663
     664                _unregister_taxonomy( 'foo' );
     665                _unregister_taxonomy( 'bar' );
     666
     667                $this->assertEqualSets( array( $p1, $p2 ), $q->posts );
     668        }
     669
     670        /**
     671         * @ticket 29738
     672         */
     673        public function test_tax_query_one_nested_query_one_first_order_query() {
     674                register_taxonomy( 'foo', 'post' );
     675                register_taxonomy( 'bar', 'post' );
     676
     677                $foo_term_1 = $this->factory->term->create( array(
     678                        'taxonomy' => 'foo',
     679                ) );
     680                $foo_term_2 = $this->factory->term->create( array(
     681                        'taxonomy' => 'foo',
     682                ) );
     683                $bar_term_1 = $this->factory->term->create( array(
     684                        'taxonomy' => 'bar',
     685                ) );
     686                $bar_term_2 = $this->factory->term->create( array(
     687                        'taxonomy' => 'bar',
     688                ) );
     689
     690                $p1 = $this->factory->post->create();
     691                $p2 = $this->factory->post->create();
     692                $p3 = $this->factory->post->create();
     693
     694                wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );
     695                wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );
     696                wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );
     697                wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );
     698                wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );
     699                wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );
     700
     701                $q = new WP_Query( array(
     702                        'fields' => 'ids',
     703                        'update_post_meta_cache' => false,
     704                        'update_post_term_cache' => false,
     705                        'tax_query' => array(
     706                                'relation' => 'OR',
     707                                array(
     708                                        'taxonomy' => 'foo',
     709                                        'terms' => array( $foo_term_2 ),
    196710                                        'field' => 'term_id',
    197                                         'terms' => array(
    198                                                 $this->tax_id,
     711                                ),
     712                                array(
     713                                        'relation' => 'AND',
     714                                        array(
     715                                                'taxonomy' => 'foo',
     716                                                'terms' => array( $foo_term_1 ),
     717                                                'field' => 'term_id',
    199718                                        ),
     719                                        array(
     720                                                'taxonomy' => 'bar',
     721                                                'terms' => array( $bar_term_1 ),
     722                                                'field' => 'term_id',
     723                                        ),
     724                                ),
     725                        ),
     726                ) );
     727
     728                _unregister_taxonomy( 'foo' );
     729                _unregister_taxonomy( 'bar' );
     730
     731                $this->assertEqualSets( array( $p1, $p2 ), $q->posts );
     732        }
     733
     734        /**
     735         * @ticket 29738
     736         */
     737        public function test_tax_query_one_double_nested_query_one_first_order_query() {
     738                register_taxonomy( 'foo', 'post' );
     739                register_taxonomy( 'bar', 'post' );
     740
     741                $foo_term_1 = $this->factory->term->create( array(
     742                        'taxonomy' => 'foo',
     743                ) );
     744                $foo_term_2 = $this->factory->term->create( array(
     745                        'taxonomy' => 'foo',
     746                ) );
     747                $bar_term_1 = $this->factory->term->create( array(
     748                        'taxonomy' => 'bar',
     749                ) );
     750                $bar_term_2 = $this->factory->term->create( array(
     751                        'taxonomy' => 'bar',
     752                ) );
     753
     754                $p1 = $this->factory->post->create();
     755                $p2 = $this->factory->post->create();
     756                $p3 = $this->factory->post->create();
     757                $p4 = $this->factory->post->create();
     758
     759                wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );
     760                wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );
     761                wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );
     762                wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );
     763                wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );
     764                wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );
     765
     766                $q = new WP_Query( array(
     767                        'fields' => 'ids',
     768                        'update_post_meta_cache' => false,
     769                        'update_post_term_cache' => false,
     770                        'tax_query' => array(
     771                                'relation' => 'OR',
     772                                array(
     773                                        'taxonomy' => 'foo',
     774                                        'terms' => array( $foo_term_2 ),
     775                                        'field' => 'term_id',
     776                                ),
     777                                array(
     778                                        'relation' => 'AND',
     779                                        array(
     780                                                'taxonomy' => 'foo',
     781                                                'terms' => array( $foo_term_1 ),
     782                                                'field' => 'term_id',
     783                                        ),
     784                                        array(
     785                                                'relation' => 'OR',
     786                                                array(
     787                                                        'taxonomy' => 'bar',
     788                                                        'terms' => array( $bar_term_1 ),
     789                                                        'field' => 'term_id',
     790                                                ),
     791                                                array(
     792                                                        'taxonomy' => 'bar',
     793                                                        'terms' => array( $bar_term_2 ),
     794                                                        'field' => 'term_id',
     795                                                ),
     796                                        ),
     797                                ),
     798                        ),
     799                ) );
     800
     801                _unregister_taxonomy( 'foo' );
     802                _unregister_taxonomy( 'bar' );
     803
     804                $this->assertEqualSets( array( $p1, $p2, $p3 ), $q->posts );
     805        }
     806
     807        /**
     808         * @ticket 20604
     809         */
     810        public function test_tax_query_relation_or_both_clauses_empty_terms() {
     811                // An empty tax query should return an empty array, not all posts.
     812
     813                $this->factory->post->create_many( 10 );
     814
     815                $query = new WP_Query( array(
     816                        'fields' => 'ids',
     817                        'update_post_term_cache' => false,
     818                        'update_post_meta_cache' => false,
     819                        'tax_query' => array(
     820                                'relation' => 'OR',
     821                                array(
     822                                        'taxonomy' => 'post_tag',
     823                                        'field' => 'id',
     824                                        'terms' => false,
     825                                        'operator' => 'IN'
     826                                ),
     827                                array(
     828                                        'taxonomy' => 'category',
     829                                        'field' => 'id',
     830                                        'terms' => false,
     831                                        'operator' => 'IN'
    200832                                ),
    201833                        )
    202                 );
    203 
    204                 $q = new WP_Query( $args );
    205                 $object = $q->get_queried_object();
    206 
    207                 $expected = get_term( $this->tax_id, 'testtax' );
    208 
    209                 $this->assertEquals( $expected, $object );
     834                ) );
     835
     836                $posts = $query->get_posts();
     837                $this->assertEquals( 0 , count( $posts ) );
    210838        }
    211839
    212840        /**
    213          * @group 30623
     841         * @ticket 20604
    214842         */
    215         public function test_get_queried_object_with_custom_taxonomy_tax_query_and_field_slug_should_return_term_object() {
    216                 // Don't override the args provided below.
    217                 remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
    218 
    219                 $args = array(
     843        public function test_tax_query_relation_or_one_clause_empty_terms() {
     844                // An empty tax query should return an empty array, not all posts.
     845
     846                $this->factory->post->create_many( 10 );
     847
     848                $query = new WP_Query( array(
     849                        'fields' => 'ids',
     850                        'update_post_term_cache' => false,
     851                        'update_post_meta_cache' => false,
     852                        'tax_query' => array(
     853                                'relation' => 'OR',
     854                                array(
     855                                        'taxonomy' => 'post_tag',
     856                                        'field' => 'id',
     857                                        'terms' => array( 'foo' ),
     858                                        'operator' => 'IN'
     859                                ),
     860                                array(
     861                                        'taxonomy' => 'category',
     862                                        'field' => 'id',
     863                                        'terms' => false,
     864                                        'operator' => 'IN'
     865                                ),
     866                        )
     867                ) );
     868
     869                $posts = $query->get_posts();
     870                $this->assertEquals( 0 , count( $posts ) );
     871        }
     872
     873        public function test_tax_query_include_children() {
     874                $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Australia' ) );
     875                $cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Sydney', 'parent' => $cat_a ) );
     876                $cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'East Syndney', 'parent' => $cat_b ) );
     877                $cat_d = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'West Syndney', 'parent' => $cat_b ) );
     878
     879                $post_a = $this->factory->post->create( array( 'post_category' => array( $cat_a ) ) );
     880                $post_b = $this->factory->post->create( array( 'post_category' => array( $cat_b ) ) );
     881                $post_c = $this->factory->post->create( array( 'post_category' => array( $cat_c ) ) );
     882                $post_d = $this->factory->post->create( array( 'post_category' => array( $cat_d ) ) );
     883
     884                $posts = get_posts( array(
     885                        'fields' => 'ids',
     886                        'update_post_meta_cache' => false,
     887                        'update_post_term_cache' => false,
     888                        'tax_query' => array(
     889                                array(
     890                                        'taxonomy' => 'category',
     891                                        'field' => 'id',
     892                                        'terms' => array( $cat_a ),
     893                                )
     894                        )
     895                ) );
     896
     897                $this->assertEquals( 4 , count( $posts ) );
     898
     899                $posts = get_posts( array(
     900                        'fields' => 'ids',
     901                        'update_post_meta_cache' => false,
     902                        'update_post_term_cache' => false,
     903                        'tax_query' => array(
     904                                array(
     905                                        'taxonomy' => 'category',
     906                                        'field' => 'id',
     907                                        'terms' => array( $cat_a ),
     908                                        'include_children' => false
     909                                )
     910                        )
     911                ) );
     912
     913                $this->assertEquals( 1 , count( $posts ) );
     914
     915                $posts = get_posts( array(
     916                        'fields' => 'ids',
     917                        'update_post_meta_cache' => false,
     918                        'update_post_term_cache' => false,
     919                        'tax_query' => array(
     920                                array(
     921                                        'taxonomy' => 'category',
     922                                        'field' => 'id',
     923                                        'terms' => array( $cat_b ),
     924                                )
     925                        )
     926                ) );
     927
     928                $this->assertEquals( 3 , count( $posts ) );
     929
     930                $posts = get_posts( array(
     931                        'fields' => 'ids',
     932                        'update_post_meta_cache' => false,
     933                        'update_post_term_cache' => false,
     934                        'tax_query' => array(
     935                                array(
     936                                        'taxonomy' => 'category',
     937                                        'field' => 'id',
     938                                        'terms' => array( $cat_b ),
     939                                        'include_children' => false
     940                                )
     941                        )
     942                ) );
     943
     944                $this->assertEquals( 1 , count( $posts ) );
     945
     946                $posts = get_posts( array(
     947                        'fields' => 'ids',
     948                        'update_post_meta_cache' => false,
     949                        'update_post_term_cache' => false,
     950                        'tax_query' => array(
     951                                array(
     952                                        'taxonomy' => 'category',
     953                                        'field' => 'id',
     954                                        'terms' => array( $cat_c ),
     955                                )
     956                        )
     957                ) );
     958
     959                $this->assertEquals( 1 , count( $posts ) );
     960
     961                $posts = get_posts( array(
     962                        'fields' => 'ids',
     963                        'update_post_meta_cache' => false,
     964                        'update_post_term_cache' => false,
     965                        'tax_query' => array(
     966                                array(
     967                                        'taxonomy' => 'category',
     968                                        'field' => 'id',
     969                                        'terms' => array( $cat_c ),
     970                                        'include_children' => false
     971                                )
     972                        )
     973                ) );
     974
     975                $this->assertEquals( 1 , count( $posts ) );
     976        }
     977
     978        public function test_tax_query_taxonomy_with_attachments() {
     979                $q = new WP_Query();
     980
     981                register_taxonomy_for_object_type( 'post_tag', 'attachment:image' );
     982                $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) );
     983                $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
     984                        'post_mime_type' => 'image/jpeg',
     985                        'post_type' => 'attachment'
     986                ) );
     987                wp_set_object_terms( $image_id, $tag_id, 'post_tag' );
     988
     989                $posts = $q->query( array(
     990                        'fields' => 'ids',
     991                        'update_post_meta_cache' => false,
     992                        'update_post_term_cache' => false,
     993                        'post_type' => 'attachment',
     994                        'post_status' => 'inherit',
     995                        'tax_query' => array(
     996                                array(
     997                                        'taxonomy' => 'post_tag',
     998                                        'field' => 'term_id',
     999                                        'terms' => array( $tag_id )
     1000                                )
     1001                        )
     1002                ) );
     1003
     1004                $this->assertEquals( array( $image_id ), $posts );
     1005        }
     1006
     1007        public function test_tax_query_no_taxonomy() {
     1008                $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) );
     1009                $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) );
     1010
     1011                $response1 = new WP_Query( array(
     1012                        'tax_query' => array(
     1013                                array( 'terms' => array( $cat_id ) )
     1014                        )
     1015                ) );
     1016                $this->assertEmpty( $response1->posts );
     1017
     1018                $response2 = new WP_Query( array(
     1019                        'fields' => 'ids',
     1020                        'update_post_meta_cache' => false,
     1021                        'update_post_term_cache' => false,
     1022                        'tax_query' => array(
     1023                                array(
     1024                                        'taxonomy' => 'category',
     1025                                        'terms' => array( $cat_id )
     1026                                )
     1027                        )
     1028                ) );
     1029                $this->assertNotEmpty( $response2->posts );
     1030
     1031                $term = get_category( $cat_id );
     1032                $response3 = new WP_Query( array(
     1033                        'fields' => 'ids',
     1034                        'update_post_meta_cache' => false,
     1035                        'update_post_term_cache' => false,
     1036                        'tax_query' => array(
     1037                                array(
     1038                                        'field' => 'term_taxonomy_id',
     1039                                        'terms' => array( $term->term_taxonomy_id )
     1040                                )
     1041                        )
     1042                ) );
     1043                $this->assertNotEmpty( $response3->posts );
     1044        }
     1045
     1046        public function test_term_taxonomy_id_field_no_taxonomy() {
     1047                $q = new WP_Query();
     1048
     1049                $posts = $this->factory->post->create_many( 5 );
     1050
     1051                $cats = $tags = array();
     1052
     1053                // need term_taxonomy_ids in addition to term_ids, so no factory
     1054                for ( $i = 0; $i < 5; $i++ ) {
     1055                        $cats[$i] = wp_insert_term( 'category-' . $i , 'category' );
     1056                        $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' );
     1057
     1058                        // post 0 gets all terms
     1059                        wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true );
     1060                        wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true );
     1061                }
     1062
     1063                wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
     1064                wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' );
     1065
     1066                wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' );
     1067                wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
     1068
     1069                wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
     1070                wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
     1071
     1072                $results1 = $q->query( array(
     1073                        'fields' => 'ids',
     1074                        'update_post_meta_cache' => false,
     1075                        'update_post_term_cache' => false,
     1076                        'orderby' => 'ID',
     1077                        'order' => 'ASC',
     1078                        'tax_query' => array(
     1079                                'relation' => 'OR',
     1080                                array(
     1081                                        'field' => 'term_taxonomy_id',
     1082                                        'terms' => array( $cats[0]['term_taxonomy_id'], $cats[2]['term_taxonomy_id'], $cats[4]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'], $tags[2]['term_taxonomy_id'], $cats[4]['term_taxonomy_id'] ),
     1083                                        'operator' => 'AND',
     1084                                        'include_children' => false,
     1085                                ),
     1086                                array(
     1087                                        'field' => 'term_taxonomy_id',
     1088                                        'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
     1089                                        'operator' => 'AND',
     1090                                        'include_children' => false,
     1091                                )
     1092                        )
     1093                ) );
     1094
     1095                $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' );
     1096
     1097                $results2 = $q->query( array(
     1098                        'fields' => 'ids',
     1099                        'update_post_meta_cache' => false,
     1100                        'update_post_term_cache' => false,
     1101                        'orderby' => 'ID',
     1102                        'order' => 'ASC',
    2201103                        'tax_query' => array(
    2211104                                'relation' => 'AND',
    2221105                                array(
    223                                         'taxonomy' => 'testtax',
    224                                         'field' => 'slug',
    225                                         'terms' => array(
    226                                                 'tax-slug',
    227                                         ),
    228                                 ),
     1106                                        'field' => 'term_taxonomy_id',
     1107                                        'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ),
     1108                                        'operator' => 'IN',
     1109                                        'include_children' => false,
     1110                                ),
     1111                                array(
     1112                                        'field' => 'term_taxonomy_id',
     1113                                        'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
     1114                                        'operator' => 'IN',
     1115                                        'include_children' => false,
     1116                                )
    2291117                        )
    230                 );
    231 
    232                 $q = new WP_Query( $args );
    233                 $object = $q->get_queried_object();
    234 
    235                 $expected = get_term( $this->tax_id, 'testtax' );
    236 
    237                 // Only compare term_id because object_id may or may not be part of either value.
    238                 $this->assertEquals( $expected->term_id, $object->term_id );
     1118                ) );
     1119
     1120                $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' );
    2391121        }
    2401122
    2411123        /**
    242          * @group 30623
     1124         * @ticket 29738
    2431125         */
    244         public function test_get_queried_object_with_custom_taxonomy_tax_query_with_multiple_clauses_should_return_term_object_corresponding_to_the_first_queried_tax() {
    245                 // Don't override the args provided below.
    246                 remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
    247 
    248                 register_taxonomy( 'testtax2', 'post' );
    249                 $testtax2_term_id = $this->factory->term->create( array(
    250                         'taxonomy' => 'testtax2',
    251                         'slug' => 'testtax2-slug',
    252                 ) );
    253 
    254                 $args = array(
    255                         'tax_query' => array(
    256                                 'relation' => 'AND',
    257                                 array(
    258                                         'taxonomy' => 'testtax',
    259                                         'field' => 'slug',
    260                                         'terms' => array(
    261                                                 'tax-slug',
    262                                         ),
    263                                 ),
    264                                 array(
    265                                         'taxonomy' => 'testtax2',
    266                                         'field' => 'slug',
    267                                         'terms' => array(
    268                                                 'testtax2-slug',
    269                                         ),
    270                                 ),
    271                         )
    272                 );
    273 
    274                 $q = new WP_Query( $args );
    275                 $object = $q->get_queried_object();
    276 
    277                 $expected = get_term( $this->tax_id, 'testtax' );
    278 
    279                 // Only compare term_id because object_id may or may not be part of either value.
    280                 $this->assertEquals( $expected->term_id, $object->term_id );
     1126        public function test_populate_taxonomy_query_var_from_tax_query() {
     1127                register_taxonomy( 'foo', 'post' );
     1128                $t = $this->factory->term->create( array(
     1129                        'taxonomy' => 'foo',
     1130                ) );
     1131                $c = $this->factory->term->create( array(
     1132                        'taxonomy' => 'category',
     1133                ) );
     1134
     1135                $q = new WP_Query( array(
     1136                        'tax_query' => array(
     1137                                // Empty terms mean that this one should be skipped
     1138                                array(
     1139                                        'taxonomy' => 'bar',
     1140                                        'terms' => array(),
     1141                                ),
     1142
     1143                                // Category and post tags should be skipped
     1144                                array(
     1145                                        'taxonomy' => 'category',
     1146                                        'terms' => array( $c ),
     1147                                ),
     1148
     1149                                array(
     1150                                        'taxonomy' => 'foo',
     1151                                        'terms' => array( $t ),
     1152                                ),
     1153                        ),
     1154                ) );
     1155
     1156                $this->assertSame( 'foo', $q->get( 'taxonomy' ) );
     1157
     1158                _unregister_taxonomy( 'foo' );
     1159        }
     1160
     1161        public function test_populate_taxonomy_query_var_from_tax_query_taxonomy_already_set() {
     1162                register_taxonomy( 'foo', 'post' );
     1163                register_taxonomy( 'foo1', 'post' );
     1164                $t = $this->factory->term->create( array(
     1165                        'taxonomy' => 'foo',
     1166                ) );
     1167
     1168                $q = new WP_Query( array(
     1169                        'taxonomy' => 'bar',
     1170                        'tax_query' => array(
     1171                                array(
     1172                                        'taxonomy' => 'foo',
     1173                                        'terms' => array( $t ),
     1174                                ),
     1175                        ),
     1176                ) );
     1177
     1178                $this->assertSame( 'bar', $q->get( 'taxonomy' ) );
     1179
     1180                _unregister_taxonomy( 'foo' );
     1181                _unregister_taxonomy( 'foo1' );
     1182        }
     1183
     1184        public function test_populate_term_query_var_from_tax_query() {
     1185                register_taxonomy( 'foo', 'post' );
     1186                $t = $this->factory->term->create( array(
     1187                        'taxonomy' => 'foo',
     1188                        'slug' => 'bar',
     1189                ) );
     1190
     1191                $q = new WP_Query( array(
     1192                        'tax_query' => array(
     1193                                array(
     1194                                        'taxonomy' => 'foo',
     1195                                        'terms' => array( 'bar' ),
     1196                                        'field' => 'slug',
     1197                                ),
     1198                        ),
     1199                ) );
     1200
     1201                $this->assertSame( 'bar', $q->get( 'term' ) );
     1202
     1203                _unregister_taxonomy( 'foo' );
     1204        }
     1205
     1206        public function test_populate_term_id_query_var_from_tax_query() {
     1207                register_taxonomy( 'foo', 'post' );
     1208                $t = $this->factory->term->create( array(
     1209                        'taxonomy' => 'foo',
     1210                        'slug' => 'bar',
     1211                ) );
     1212
     1213                $q = new WP_Query( array(
     1214                        'tax_query' => array(
     1215                                array(
     1216                                        'taxonomy' => 'foo',
     1217                                        'terms' => array( $t ),
     1218                                        'field' => 'term_id',
     1219                                ),
     1220                        ),
     1221                ) );
     1222
     1223                $this->assertEquals( $t, $q->get( 'term_id' ) );
     1224
     1225                _unregister_taxonomy( 'foo' );
     1226        }
     1227
     1228        /**
     1229         * @ticket 29738
     1230         */
     1231        public function test_populate_cat_category_name_query_var_from_tax_query() {
     1232                register_taxonomy( 'foo', 'post' );
     1233                $t = $this->factory->term->create( array(
     1234                        'taxonomy' => 'foo',
     1235                ) );
     1236                $c = $this->factory->term->create( array(
     1237                        'taxonomy' => 'category',
     1238                        'slug' => 'bar',
     1239                ) );
     1240
     1241                $q = new WP_Query( array(
     1242                        'tax_query' => array(
     1243                                // Non-category should be skipped
     1244                                array(
     1245                                        'taxonomy' => 'foo',
     1246                                        'terms' => array( $t ),
     1247                                ),
     1248
     1249                                // Empty terms mean that this one should be skipped
     1250                                array(
     1251                                        'taxonomy' => 'category',
     1252                                        'terms' => array(),
     1253                                ),
     1254
     1255                                // Category and post tags should be skipped
     1256                                array(
     1257                                        'taxonomy' => 'category',
     1258                                        'terms' => array( $c ),
     1259                                ),
     1260                        ),
     1261                ) );
     1262
     1263                $this->assertEquals( $c, $q->get( 'cat' ) );
     1264                $this->assertEquals( 'bar', $q->get( 'category_name' ) );
     1265
     1266                _unregister_taxonomy( 'foo' );
     1267        }
     1268
     1269        /**
     1270         * @ticket 29738
     1271         */
     1272        public function test_populate_tag_id_query_var_from_tax_query() {
     1273                register_taxonomy( 'foo', 'post' );
     1274                $t = $this->factory->term->create( array(
     1275                        'taxonomy' => 'foo',
     1276                ) );
     1277                $tag = $this->factory->term->create( array(
     1278                        'taxonomy' => 'post_tag',
     1279                        'slug' => 'bar',
     1280                ) );
     1281
     1282                $q = new WP_Query( array(
     1283                        'tax_query' => array(
     1284                                // Non-tag should be skipped
     1285                                array(
     1286                                        'taxonomy' => 'foo',
     1287                                        'terms' => array( $t ),
     1288                                ),
     1289
     1290                                // Empty terms mean that this one should be skipped
     1291                                array(
     1292                                        'taxonomy' => 'post_tag',
     1293                                        'terms' => array(),
     1294                                ),
     1295
     1296                                // Category and post tags should be skipped
     1297                                array(
     1298                                        'taxonomy' => 'post_tag',
     1299                                        'terms' => array( $tag ),
     1300                                ),
     1301                        ),
     1302                ) );
     1303
     1304                $this->assertEquals( $tag, $q->get( 'tag_id' ) );
     1305
     1306                _unregister_taxonomy( 'foo' );
    2811307        }
    2821308}
Note: See TracChangeset for help on using the changeset viewer.