Make WordPress Core


Ignore:
Timestamp:
10/02/2014 01:07:20 AM (12 years ago)
Author:
boonebgorges
Message:

Improve unit tests for WP_Tax_Query.

  • Exhaustive tests for publicly available functionality of WP_Tax_Query.
  • For tests that are related to the tax_query argument as used in WP_Query, move to tests/post/query.php.
  • Add some tax_query tests to cover single vs multiple queries using AND and OR; various values for 'field'; various values for 'operator'.
  • Improve test names.
  • Correct @group annotations.
  • Improve performance of some WP_Query-related tests by declaring 'update_post_meta/term_cache' false.

Fixes #29718

File:
1 edited

Legend:

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

    r28783 r29805  
    77        protected $q;
    88
    9         function setUp() {
     9        public function setUp() {
    1010                parent::setUp();
    1111                unset( $this->q );
     
    1313        }
    1414
    15         function test_category__and_var() {
    16                 $term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
    17                 $term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
    18                 $post_id = $this->factory->post->create();
    19 
    20                 wp_set_post_categories( $post_id, $term_id );
    21 
    22                 $posts = $this->q->query( array( 'category__and' => array( $term_id ) ) );
    23 
    24                 $this->assertEmpty( $this->q->get( 'category__and' ) );
    25                 $this->assertCount( 0, $this->q->get( 'category__and' ) );
    26                 $this->assertNotEmpty( $this->q->get( 'category__in' ) );
    27                 $this->assertCount( 1, $this->q->get( 'category__in' ) );
    28 
    29                 $this->assertNotEmpty( $posts );
    30                 $this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
    31 
    32                 $posts2 = $this->q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
    33                 $this->assertNotEmpty( $this->q->get( 'category__and' ) );
    34                 $this->assertCount( 2, $this->q->get( 'category__and' ) );
    35                 $this->assertEmpty( $this->q->get( 'category__in' ) );
    36                 $this->assertCount( 0, $this->q->get( 'category__in' ) );
    37 
    38                 $this->assertEmpty( $posts2 );
    39         }
    40 
    41         function test_taxonomy_with_attachments() {
    42                 register_taxonomy_for_object_type( 'post_tag', 'attachment:image' );
    43                 $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) );
    44                 $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
    45                         'post_mime_type' => 'image/jpeg',
    46                         'post_type' => 'attachment'
    47                 ) );
    48                 wp_set_object_terms( $image_id, $tag_id, 'post_tag' );
    49 
    50                 $posts = $this->q->query( array(
    51                         'fields' => 'ids',
    52                         'post_type' => 'attachment',
    53                         'post_status' => 'inherit',
    54                         'tax_query' => array(
    55                                 array(
    56                                         'taxonomy' => 'post_tag',
    57                                         'field' => 'term_id',
    58                                         'terms' => array( $tag_id )
    59                                 )
    60                         )
    61                 ) );
    62 
    63                 $this->assertEquals( array( $image_id ), $posts );
    64         }
    65 
    66         /**
    67          * @ticket 27193
    68          */
    69         function test_cat_or_tag() {
    70                 $category1 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'alpha' ) );
    71                 $category2 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'beta' ) );
    72 
    73                 $tag1 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'gamma' ) );
    74                 $tag2 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'delta' ) );
    75 
    76                 $post_id1 = $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $category1 ) ) );
    77                 $terms1 = get_the_category( $post_id1 );
    78                 $this->assertEquals( array( get_category( $category1 ) ), $terms1 );
    79 
    80                 $post_id2 = $this->factory->post->create( array( 'post_title' => 'beta', 'post_category' => array( $category2 ) ) );
    81                 $terms2 = get_the_category( $post_id2 );
    82                 $this->assertEquals( array( get_category( $category2 ) ), $terms2 );
    83 
    84                 $post_id3 = $this->factory->post->create( array( 'post_title' => 'gamma', 'post_tag' => array( $tag1 ) ) );
    85                 $post_id4 = $this->factory->post->create( array( 'post_title' => 'delta', 'post_tag' => array( $tag2 ) ) );
    86 
    87                 $query = new WP_Query( array(
    88                         'fields' => 'ids',
    89                         'tax_query' => array(
    90                                 //'relation' => 'OR',
    91                                 array(
    92                                         'taxonomy' => 'category',
    93                                         'field' => 'term_id',
    94                                         'terms' => array( $category1, $category2 )
    95                                 )
    96                         )
    97                 ) );
    98                 $ids = $query->get_posts();
    99                 $this->assertEquals( array( $post_id1, $post_id2 ), $ids );
    100         }
    101 
    102         function test_tax_query_no_taxonomy() {
    103                 $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) );
    104                 $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) );
    105 
    106                 $response1 = new WP_Query( array(
    107                         'tax_query' => array(
    108                                 array( 'terms' => array( $cat_id ) )
    109                         )
    110                 ) );
    111                 $this->assertEmpty( $response1->posts );
    112 
    113                 $response2 = new WP_Query( array(
    114                         'tax_query' => array(
    115                                 array(
    116                                         'taxonomy' => 'category',
    117                                         'terms' => array( $cat_id )
    118                                 )
    119                         )
    120                 ) );
    121                 $this->assertNotEmpty( $response2->posts );
    122 
    123                 $term = get_category( $cat_id );
    124                 $response3 = new WP_Query( array(
    125                         'tax_query' => array(
    126                                 array(
    127                                         'field' => 'term_taxonomy_id',
    128                                         'terms' => array( $term->term_taxonomy_id )
    129                                 )
    130                         )
    131                 ) );
    132                 $this->assertNotEmpty( $response3->posts );
    133         }
    134 
    135         function test_term_taxonomy_id_field_no_taxonomy() {
    136                 $posts = $this->factory->post->create_many( 5 );
    137 
    138                 $cats = $tags = array();
    139 
    140                 // need term_taxonomy_ids in addition to term_ids, so no factory
    141                 for ( $i = 0; $i < 5; $i++ ) {
    142                         $cats[$i] = wp_insert_term( 'category-' . $i , 'category' );
    143                         $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' );
    144 
    145                         // post 0 gets all terms
    146                         wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true );
    147                         wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true );
    148                 }
    149 
    150                 wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
    151                 wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' );
    152 
    153                 wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' );
    154                 wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
    155 
    156                 wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
    157                 wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
    158 
    159                 $results1 = $this->q->query( array(
    160                         'fields' => 'ids',
    161                         'orderby' => 'ID',
    162                         'order' => 'ASC',
    163                         'tax_query' => array(
    164                                 'relation' => 'OR',
    165                                 array(
    166                                         'field' => 'term_taxonomy_id',
    167                                         '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'] ),
    168                                         'operator' => 'AND',
    169                                         'include_children' => false,
    170                                 ),
    171                                 array(
    172                                         'field' => 'term_taxonomy_id',
    173                                         'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
    174                                         'operator' => 'AND',
    175                                         'include_children' => false,
    176                                 )
    177                         )
    178                 ) );
    179 
    180                 $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' );
    181 
    182                 $results2 = $this->q->query( array(
    183                         'fields' => 'ids',
    184                         'orderby' => 'ID',
    185                         'order' => 'ASC',
    186                         'tax_query' => array(
    187                                 'relation' => 'AND',
    188                                 array(
    189                                         'field' => 'term_taxonomy_id',
    190                                         'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ),
    191                                         'operator' => 'IN',
    192                                         'include_children' => false,
    193                                 ),
    194                                 array(
    195                                         'field' => 'term_taxonomy_id',
    196                                         'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
    197                                         'operator' => 'IN',
    198                                         'include_children' => false,
    199                                 )
    200                         )
    201                 ) );
    202 
    203                 $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' );
    204         }
    205 
    206         /**
    207          * @ticket 28099
    208          */
    209         function test_empty__in() {
    210                 $cat_id = $this->factory->category->create();
    211                 $post_id = $this->factory->post->create();
    212                 wp_set_post_categories( $post_id, $cat_id );
    213 
    214                 $q1 = get_posts( array( 'category__in' => array( $cat_id ) ) );
    215                 $this->assertNotEmpty( $q1 );
    216                 $q2 = get_posts( array( 'category__in' => array() ) );
    217                 $this->assertNotEmpty( $q2 );
    218 
    219                 $tag = wp_insert_term( 'woo', 'post_tag' );
    220                 $tag_id = $tag['term_id'];
    221                 $slug = get_tag( $tag_id )->slug;
    222                 wp_set_post_tags( $post_id, $slug );
    223 
    224                 $q3 = get_posts( array( 'tag__in' => array( $tag_id ) ) );
    225                 $this->assertNotEmpty( $q3 );
    226                 $q4 = get_posts( array( 'tag__in' => array() ) );
    227                 $this->assertNotEmpty( $q4 );
    228 
    229                 $q5 = get_posts( array( 'tag_slug__in' => array( $slug ) ) );
    230                 $this->assertNotEmpty( $q5 );
    231                 $q6 = get_posts( array( 'tag_slug__in' => array() ) );
    232                 $this->assertNotEmpty( $q6 );
     15        public function test_construct_with_relation_default() {
     16                $tq = new WP_Tax_Query( array() );
     17                $this->assertSame( 'AND', $tq->relation );
     18        }
     19
     20        public function test_construct_with_relation_or_lowercase() {
     21                $tq = new WP_Tax_Query( array(
     22                        'relation' => 'or',
     23                ) );
     24                $this->assertSame( 'OR', $tq->relation );
     25        }
     26
     27        public function test_construct_with_relation_or_uppercase() {
     28                $tq = new WP_Tax_Query( array(
     29                        'relation' => 'OR',
     30                ) );
     31                $this->assertSame( 'OR', $tq->relation );
     32        }
     33
     34        public function test_construct_with_relation_other() {
     35                $tq = new WP_Tax_Query( array(
     36                        'relation' => 'foo',
     37                ) );
     38                $this->assertSame( 'AND', $tq->relation );
     39        }
     40
     41        public function test_construct_fill_missing_query_params() {
     42                $tq = new WP_Tax_Query( array(
     43                        array(),
     44                ) );
     45
     46                $expected = array(
     47                        'taxonomy' => '',
     48                        'terms' => array(),
     49                        'include_children' => true,
     50                        'field' => 'term_id',
     51                        'operator' => 'IN',
     52                );
     53
     54                $this->assertEquals( $expected, $tq->queries[0] );
     55        }
     56
     57        public function test_construct_fill_missing_query_params_merge_with_passed_values() {
     58                $tq = new WP_Tax_Query( array(
     59                        array(
     60                                'taxonomy' => 'foo',
     61                                'include_children' => false,
     62                                'foo' => 'bar',
     63                        ),
     64                ) );
     65
     66                $expected = array(
     67                        'taxonomy' => 'foo',
     68                        'terms' => array(),
     69                        'include_children' => false,
     70                        'field' => 'term_id',
     71                        'operator' => 'IN',
     72                        'foo' => 'bar',
     73                );
     74
     75                $this->assertEquals( $expected, $tq->queries[0] );
     76        }
     77
     78        public function test_construct_cast_terms_to_array() {
     79                $tq = new WP_Tax_Query( array(
     80                        array(
     81                                'terms' => 'foo',
     82                        ),
     83                ) );
     84
     85                $this->assertEquals( array( 'foo', ), $tq->queries[0]['terms'] );
     86        }
     87
     88        public function test_transform_query_terms_empty() {
     89                $tq = new WP_Tax_Query( array(
     90                        array(),
     91                ) );
     92                $query = $tq->queries[0];
     93
     94                $tq->transform_query( $tq->queries[0], 'term_id' );
     95
     96                $this->assertSame( $query, $tq->queries[0] );
     97        }
     98
     99        public function test_transform_query_field_same_as_resulting_field() {
     100                $tq = new WP_Tax_Query( array(
     101                        array(
     102                                'field' => 'term_id',
     103                        ),
     104                ) );
     105                $query = $tq->queries[0];
     106
     107                $tq->transform_query( $tq->queries[0], 'term_id' );
     108
     109                $this->assertSame( $query, $tq->queries[0] );
     110        }
     111
     112        public function test_transform_query_resulting_field_sanitized() {
     113                $t1 = $this->factory->category->create( array( 'slug' => 'foo', ) );
     114                $t2 = $this->factory->category->create( array( 'slug' => 'bar', ) );
     115                $p = $this->factory->post->create();
     116                wp_set_post_categories( $p, $t1 );
     117
     118                $tq1 = new WP_Tax_Query( array(
     119                        array(
     120                                'terms' => array( 'foo' ),
     121                                'field' => 'slug',
     122                        ),
     123                ) );
     124                $tq1->transform_query( $tq1->queries[0], 'term_taxonomy_id' );
     125
     126                $tq2 = new WP_Tax_Query( array(
     127                        array(
     128                                'terms' => array( 'foo' ),
     129                                'field' => 'slug',
     130                        ),
     131                ) );
     132                $tq2->transform_query( $tq2->queries[0], 'TERM_ ta%xonomy_id' );
     133
     134                $this->assertSame( $tq1->queries[0], $tq2->queries[0] );
     135        }
     136
     137        public function test_transform_query_field_slug() {
     138                $t1 = $this->factory->category->create( array( 'slug' => 'foo', ) );
     139                $p = $this->factory->post->create();
     140                $tt_ids = wp_set_post_categories( $p, $t1 );
     141
     142                $tq = new WP_Tax_Query( array(
     143                        array(
     144                                'taxonomy' => 'category',
     145                                'terms' => array( 'foo' ),
     146                                'field' => 'slug',
     147                        ),
     148                ) );
     149                $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
     150
     151                $this->assertSame( $tt_ids, $tq->queries[0]['terms'] );
     152                $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
     153        }
     154
     155        public function test_transform_query_field_name() {
     156                $t1 = $this->factory->category->create( array( 'slug' => 'foo', 'name' => 'Foo', ) );
     157                $p = $this->factory->post->create();
     158                $tt_ids = wp_set_post_categories( $p, $t1 );
     159
     160                $tq = new WP_Tax_Query( array(
     161                        array(
     162                                'taxonomy' => 'category',
     163                                'terms' => array( 'Foo' ),
     164                                'field' => 'name',
     165                        ),
     166                ) );
     167                $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
     168
     169                $this->assertSame( $tt_ids, $tq->queries[0]['terms'] );
     170                $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
     171        }
     172
     173        public function test_transform_query_field_term_taxonomy_id() {
     174                $t1 = $this->factory->category->create( array( 'slug' => 'foo', 'name' => 'Foo', ) );
     175                $p = $this->factory->post->create();
     176                $tt_ids = wp_set_post_categories( $p, $t1 );
     177
     178                $tq = new WP_Tax_Query( array(
     179                        array(
     180                                'taxonomy' => 'category',
     181                                'terms' => $tt_ids,
     182                                'field' => 'term_taxonomy_id',
     183                        ),
     184                ) );
     185                $tq->transform_query( $tq->queries[0], 'term_id' );
     186
     187                $this->assertEquals( array( $t1 ), $tq->queries[0]['terms'] );
     188                $this->assertSame( 'term_id', $tq->queries[0]['field'] );
     189        }
     190
     191        public function test_transform_query_field_term_taxonomy_default() {
     192                $t1 = $this->factory->category->create( array( 'slug' => 'foo', 'name' => 'Foo', ) );
     193                $p = $this->factory->post->create();
     194                $tt_ids = wp_set_post_categories( $p, $t1 );
     195
     196                $tq = new WP_Tax_Query( array(
     197                        array(
     198                                'taxonomy' => 'category',
     199                                'terms' => array( $t1 ),
     200                                'field' => 'foo', // Anything defaults to term_id
     201                        ),
     202                ) );
     203                $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
     204
     205                $this->assertEquals( $tt_ids, $tq->queries[0]['terms'] );
     206                $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
     207        }
     208
     209        public function test_transform_query_nonexistent_terms() {
     210                $tq = new WP_Tax_Query( array(
     211                        array(
     212                                'terms' => array( 'foo' ),
     213                                'field' => 'slug',
     214                                'operator' => 'AND',
     215                        ),
     216                ) );
     217                $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
     218
     219                $this->assertTrue( is_wp_error( $tq->queries[0] ) );
    233220        }
    234221}
Note: See TracChangeset for help on using the changeset viewer.