Make WordPress Core

Ticket #29718: 29718.02.patch

File 29718.02.patch, 42.7 KB (added by boonebgorges, 11 years ago)
  • tests/phpunit/tests/post/query.php

    diff --git tests/phpunit/tests/post/query.php tests/phpunit/tests/post/query.php
    index 004e710..ec3ef99 100644
    class Tests_Post_Query extends WP_UnitTestCase { 
    332332
    333333        }
    334334
    335         /**
    336          * @ticket 20604
    337          */
    338         function test_taxonomy_empty_or() {
    339                 // An empty tax query should return an empty array, not all posts.
    340 
    341                 $this->factory->post->create_many( 10 );
    342 
    343                 $query = new WP_Query( array(
    344                         'fields'        => 'ids',
    345                         'tax_query' => array(
    346                         'relation' => 'OR',
    347                         array(
    348                                 'taxonomy' => 'post_tag',
    349                                 'field' => 'id',
    350                                 'terms' => false,
    351                                 'operator' => 'IN'
    352                         ),
    353                         array(
    354                                 'taxonomy' => 'category',
    355                                 'field' => 'id',
    356                                 'terms' => false,
    357                                 'operator' => 'IN'
    358                         )
    359                         )
    360                 ) );
    361 
    362                 $posts = $query->get_posts();
    363                 $this->assertEquals( 0 , count( $posts ) );
    364         }
    365 
    366335        function test_meta_between_not_between() {
    367336                $post_id = $this->factory->post->create();
    368337                add_post_meta( $post_id, 'time', 500 );
    class Tests_Post_Query extends WP_UnitTestCase { 
    543512                $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );
    544513        }
    545514
    546         function test_taxonomy_include_children() {
     515        /**
     516         * @group taxonomy
     517         */
     518        public function test_tax_query_single_query_single_term_field_slug() {
     519                $t = $this->factory->term->create( array(
     520                        'taxonomy' => 'category',
     521                        'slug' => 'foo',
     522                        'name' => 'Foo',
     523                ) );
     524                $p1 = $this->factory->post->create();
     525                $p2 = $this->factory->post->create();
     526
     527                wp_set_post_terms( $p1, $t, 'category' );
     528
     529                $q = new WP_Query( array(
     530                        'fields' => 'ids',
     531                        'update_post_meta_cache' => false,
     532                        'update_post_term_cache' => false,
     533                        'tax_query' => array(
     534                                array(
     535                                        'taxonomy' => 'category',
     536                                        'terms' => array( 'foo' ),
     537                                        'field' => 'slug',
     538                                ),
     539                        ),
     540                ) );
     541
     542                $this->assertEquals( array( $p1 ), $q->posts );
     543        }
     544
     545        /**
     546         * @group taxonomy
     547         */
     548        public function test_tax_query_single_query_single_term_field_name() {
     549                $t = $this->factory->term->create( array(
     550                        'taxonomy' => 'category',
     551                        'slug' => 'foo',
     552                        'name' => 'Foo',
     553                ) );
     554                $p1 = $this->factory->post->create();
     555                $p2 = $this->factory->post->create();
     556
     557                wp_set_post_terms( $p1, $t, 'category' );
     558
     559                $q = new WP_Query( array(
     560                        'fields' => 'ids',
     561                        'update_post_meta_cache' => false,
     562                        'update_post_term_cache' => false,
     563                        'tax_query' => array(
     564                                array(
     565                                        'taxonomy' => 'category',
     566                                        'terms' => array( 'Foo' ),
     567                                        'field' => 'name',
     568                                ),
     569                        ),
     570                ) );
     571
     572                $this->assertEquals( array( $p1 ), $q->posts );
     573        }
     574
     575        /**
     576         * @group taxonomy
     577         */
     578        public function test_tax_query_single_query_single_term_field_term_taxonomy_id() {
     579                $t = $this->factory->term->create( array(
     580                        'taxonomy' => 'category',
     581                        'slug' => 'foo',
     582                        'name' => 'Foo',
     583                ) );
     584                $p1 = $this->factory->post->create();
     585                $p2 = $this->factory->post->create();
     586
     587                $tt_ids = wp_set_post_terms( $p1, $t, 'category' );
     588
     589                $q = new WP_Query( array(
     590                        'fields' => 'ids',
     591                        'update_post_meta_cache' => false,
     592                        'update_post_term_cache' => false,
     593                        'tax_query' => array(
     594                                array(
     595                                        'taxonomy' => 'category',
     596                                        'terms' => $tt_ids,
     597                                        'field' => 'term_taxonomy_id',
     598                                ),
     599                        ),
     600                ) );
     601
     602                $this->assertEquals( array( $p1 ), $q->posts );
     603        }
     604
     605        /**
     606         * @group taxonomy
     607         */
     608        public function test_tax_query_single_query_single_term_field_term_id() {
     609                $t = $this->factory->term->create( array(
     610                        'taxonomy' => 'category',
     611                        'slug' => 'foo',
     612                        'name' => 'Foo',
     613                ) );
     614                $p1 = $this->factory->post->create();
     615                $p2 = $this->factory->post->create();
     616
     617                wp_set_post_terms( $p1, $t, 'category' );
     618
     619                $q = new WP_Query( array(
     620                        'fields' => 'ids',
     621                        'update_post_meta_cache' => false,
     622                        'update_post_term_cache' => false,
     623                        'tax_query' => array(
     624                                array(
     625                                        'taxonomy' => 'category',
     626                                        'terms' => array( $t ),
     627                                        'field' => 'term_id',
     628                                ),
     629                        ),
     630                ) );
     631
     632                $this->assertEquals( array( $p1 ), $q->posts );
     633        }
     634
     635        /**
     636         * @group taxonomy
     637         */
     638        public function test_tax_query_single_query_single_term_operator_in() {
     639                $t = $this->factory->term->create( array(
     640                        'taxonomy' => 'category',
     641                        'slug' => 'foo',
     642                        'name' => 'Foo',
     643                ) );
     644                $p1 = $this->factory->post->create();
     645                $p2 = $this->factory->post->create();
     646
     647                wp_set_post_terms( $p1, $t, 'category' );
     648
     649                $q = new WP_Query( array(
     650                        'fields' => 'ids',
     651                        'update_post_meta_cache' => false,
     652                        'update_post_term_cache' => false,
     653                        'tax_query' => array(
     654                                array(
     655                                        'taxonomy' => 'category',
     656                                        'terms' => array( 'foo' ),
     657                                        'field' => 'slug',
     658                                        'operator' => 'IN',
     659                                ),
     660                        ),
     661                ) );
     662
     663                $this->assertEquals( array( $p1 ), $q->posts );
     664        }
     665
     666        /**
     667         * @group taxonomy
     668         */
     669        public function test_tax_query_single_query_single_term_operator_not_in() {
     670                $t = $this->factory->term->create( array(
     671                        'taxonomy' => 'category',
     672                        'slug' => 'foo',
     673                        'name' => 'Foo',
     674                ) );
     675                $p1 = $this->factory->post->create();
     676                $p2 = $this->factory->post->create();
     677
     678                wp_set_post_terms( $p1, $t, 'category' );
     679
     680                $q = new WP_Query( array(
     681                        'fields' => 'ids',
     682                        'update_post_meta_cache' => false,
     683                        'update_post_term_cache' => false,
     684                        'tax_query' => array(
     685                                array(
     686                                        'taxonomy' => 'category',
     687                                        'terms' => array( 'foo' ),
     688                                        'field' => 'slug',
     689                                        'operator' => 'NOT IN',
     690                                ),
     691                        ),
     692                ) );
     693
     694                $this->assertEquals( array( $p2 ), $q->posts );
     695        }
     696
     697        /**
     698         * @group taxonomy
     699         */
     700        public function test_tax_query_single_query_single_term_operator_and() {
     701                $t = $this->factory->term->create( array(
     702                        'taxonomy' => 'category',
     703                        'slug' => 'foo',
     704                        'name' => 'Foo',
     705                ) );
     706                $p1 = $this->factory->post->create();
     707                $p2 = $this->factory->post->create();
     708
     709                wp_set_post_terms( $p1, $t, 'category' );
     710
     711                $q = new WP_Query( array(
     712                        'fields' => 'ids',
     713                        'update_post_meta_cache' => false,
     714                        'update_post_term_cache' => false,
     715                        'tax_query' => array(
     716                                array(
     717                                        'taxonomy' => 'category',
     718                                        'terms' => array( 'foo' ),
     719                                        'field' => 'slug',
     720                                        'operator' => 'AND',
     721                                ),
     722                        ),
     723                ) );
     724
     725                $this->assertEquals( array( $p1 ), $q->posts );
     726        }
     727
     728        /**
     729         * @group taxonomy
     730         */
     731        public function test_tax_query_single_query_multiple_terms_operator_in() {
     732                $t1 = $this->factory->term->create( array(
     733                        'taxonomy' => 'category',
     734                        'slug' => 'foo',
     735                        'name' => 'Foo',
     736                ) );
     737                $t2 = $this->factory->term->create( array(
     738                        'taxonomy' => 'category',
     739                        'slug' => 'bar',
     740                        'name' => 'Bar',
     741                ) );
     742                $p1 = $this->factory->post->create();
     743                $p2 = $this->factory->post->create();
     744                $p3 = $this->factory->post->create();
     745
     746                wp_set_post_terms( $p1, $t1, 'category' );
     747                wp_set_post_terms( $p2, $t2, 'category' );
     748
     749                $q = new WP_Query( array(
     750                        'fields' => 'ids',
     751                        'update_post_meta_cache' => false,
     752                        'update_post_term_cache' => false,
     753                        'tax_query' => array(
     754                                array(
     755                                        'taxonomy' => 'category',
     756                                        'terms' => array( 'foo', 'bar' ),
     757                                        'field' => 'slug',
     758                                        'operator' => 'IN',
     759                                ),
     760                        ),
     761                ) );
     762
     763                $this->assertEquals( array( $p1, $p2 ), $q->posts );
     764        }
     765
     766        /**
     767         * @group taxonomy
     768         */
     769        public function test_tax_query_single_query_multiple_terms_operator_not_in() {
     770                $t1 = $this->factory->term->create( array(
     771                        'taxonomy' => 'category',
     772                        'slug' => 'foo',
     773                        'name' => 'Foo',
     774                ) );
     775                $t2 = $this->factory->term->create( array(
     776                        'taxonomy' => 'category',
     777                        'slug' => 'bar',
     778                        'name' => 'Bar',
     779                ) );
     780                $p1 = $this->factory->post->create();
     781                $p2 = $this->factory->post->create();
     782                $p3 = $this->factory->post->create();
     783
     784                wp_set_post_terms( $p1, $t1, 'category' );
     785                wp_set_post_terms( $p2, $t2, 'category' );
     786
     787                $q = new WP_Query( array(
     788                        'fields' => 'ids',
     789                        'update_post_meta_cache' => false,
     790                        'update_post_term_cache' => false,
     791                        'tax_query' => array(
     792                                array(
     793                                        'taxonomy' => 'category',
     794                                        'terms' => array( 'foo', 'bar' ),
     795                                        'field' => 'slug',
     796                                        'operator' => 'NOT IN',
     797                                ),
     798                        ),
     799                ) );
     800
     801                $this->assertEquals( array( $p3 ), $q->posts );
     802        }
     803
     804        /**
     805         * @group taxonomy
     806         */
     807        public function test_tax_query_single_query_multiple_terms_operator_and() {
     808                $t1 = $this->factory->term->create( array(
     809                        'taxonomy' => 'category',
     810                        'slug' => 'foo',
     811                        'name' => 'Foo',
     812                ) );
     813                $t2 = $this->factory->term->create( array(
     814                        'taxonomy' => 'category',
     815                        'slug' => 'bar',
     816                        'name' => 'Bar',
     817                ) );
     818                $p1 = $this->factory->post->create();
     819                $p2 = $this->factory->post->create();
     820                $p3 = $this->factory->post->create();
     821
     822                wp_set_object_terms( $p1, $t1, 'category' );
     823                wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
     824
     825                $q = new WP_Query( array(
     826                        'fields' => 'ids',
     827                        'update_post_meta_cache' => false,
     828                        'update_post_term_cache' => false,
     829                        'tax_query' => array(
     830                                array(
     831                                        'taxonomy' => 'category',
     832                                        'terms' => array( 'foo', 'bar' ),
     833                                        'field' => 'slug',
     834                                        'operator' => 'AND',
     835                                ),
     836                        ),
     837                ) );
     838
     839                $this->assertEquals( array( $p2 ), $q->posts );
     840        }
     841
     842        /**
     843         * @group taxonomy
     844         */
     845        public function test_tax_query_multiple_queries_relation_and() {
     846                $t1 = $this->factory->term->create( array(
     847                        'taxonomy' => 'category',
     848                        'slug' => 'foo',
     849                        'name' => 'Foo',
     850                ) );
     851                $t2 = $this->factory->term->create( array(
     852                        'taxonomy' => 'category',
     853                        'slug' => 'bar',
     854                        'name' => 'Bar',
     855                ) );
     856                $p1 = $this->factory->post->create();
     857                $p2 = $this->factory->post->create();
     858                $p3 = $this->factory->post->create();
     859
     860                wp_set_object_terms( $p1, $t1, 'category' );
     861                wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
     862
     863                $q = new WP_Query( array(
     864                        'fields' => 'ids',
     865                        'update_post_meta_cache' => false,
     866                        'update_post_term_cache' => false,
     867                        'tax_query' => array(
     868                                'relation' => 'AND',
     869                                array(
     870                                        'taxonomy' => 'category',
     871                                        'terms' => array( 'foo' ),
     872                                        'field' => 'slug',
     873                                ),
     874                                array(
     875                                        'taxonomy' => 'category',
     876                                        'terms' => array( 'bar' ),
     877                                        'field' => 'slug',
     878                                ),
     879                        ),
     880                ) );
     881
     882                $this->assertEquals( array( $p2 ), $q->posts );
     883        }
     884
     885        /**
     886         * @group taxonomy
     887         */
     888        public function test_tax_query_multiple_queries_relation_or() {
     889                $t1 = $this->factory->term->create( array(
     890                        'taxonomy' => 'category',
     891                        'slug' => 'foo',
     892                        'name' => 'Foo',
     893                ) );
     894                $t2 = $this->factory->term->create( array(
     895                        'taxonomy' => 'category',
     896                        'slug' => 'bar',
     897                        'name' => 'Bar',
     898                ) );
     899                $p1 = $this->factory->post->create();
     900                $p2 = $this->factory->post->create();
     901                $p3 = $this->factory->post->create();
     902
     903                wp_set_object_terms( $p1, $t1, 'category' );
     904                wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
     905
     906                $q = new WP_Query( array(
     907                        'fields' => 'ids',
     908                        'update_post_meta_cache' => false,
     909                        'update_post_term_cache' => false,
     910                        'tax_query' => array(
     911                                'relation' => 'OR',
     912                                array(
     913                                        'taxonomy' => 'category',
     914                                        'terms' => array( 'foo' ),
     915                                        'field' => 'slug',
     916                                ),
     917                                array(
     918                                        'taxonomy' => 'category',
     919                                        'terms' => array( 'bar' ),
     920                                        'field' => 'slug',
     921                                ),
     922                        ),
     923                ) );
     924
     925                $this->assertEquals( array( $p1, $p2 ), $q->posts );
     926        }
     927
     928        /**
     929         * @group taxonomy
     930         */
     931        public function test_tax_query_multiple_queries_different_taxonomies() {
     932                $t1 = $this->factory->term->create( array(
     933                        'taxonomy' => 'post_tag',
     934                        'slug' => 'foo',
     935                        'name' => 'Foo',
     936                ) );
     937                $t2 = $this->factory->term->create( array(
     938                        'taxonomy' => 'category',
     939                        'slug' => 'bar',
     940                        'name' => 'Bar',
     941                ) );
     942                $p1 = $this->factory->post->create();
     943                $p2 = $this->factory->post->create();
     944                $p3 = $this->factory->post->create();
     945
     946                wp_set_object_terms( $p1, $t1, 'post_tag' );
     947                wp_set_object_terms( $p2, $t2, 'category' );
     948
     949                $q = new WP_Query( array(
     950                        'fields' => 'ids',
     951                        'update_post_meta_cache' => false,
     952                        'update_post_term_cache' => false,
     953                        'tax_query' => array(
     954                                'relation' => 'OR',
     955                                array(
     956                                        'taxonomy' => 'post_tag',
     957                                        'terms' => array( 'foo' ),
     958                                        'field' => 'slug',
     959                                ),
     960                                array(
     961                                        'taxonomy' => 'category',
     962                                        'terms' => array( 'bar' ),
     963                                        'field' => 'slug',
     964                                ),
     965                        ),
     966                ) );
     967
     968                $this->assertEquals( array( $p1, $p2 ), $q->posts );
     969        }
     970
     971        /**
     972         * @ticket 20604
     973         * @group taxonomy
     974         */
     975        public function test_tax_query_relation_or_both_clauses_empty_terms() {
     976                // An empty tax query should return an empty array, not all posts.
     977
     978                $this->factory->post->create_many( 10 );
     979
     980                $query = new WP_Query( array(
     981                        'fields' => 'ids',
     982                        'update_post_term_cache' => false,
     983                        'update_post_meta_cache' => false,
     984                        'tax_query' => array(
     985                                'relation' => 'OR',
     986                                array(
     987                                        'taxonomy' => 'post_tag',
     988                                        'field' => 'id',
     989                                        'terms' => false,
     990                                        'operator' => 'IN'
     991                                ),
     992                                array(
     993                                        'taxonomy' => 'category',
     994                                        'field' => 'id',
     995                                        'terms' => false,
     996                                        'operator' => 'IN'
     997                                ),
     998                        )
     999                ) );
     1000
     1001                $posts = $query->get_posts();
     1002                $this->assertEquals( 0 , count( $posts ) );
     1003        }
     1004
     1005        /**
     1006         * @ticket 20604
     1007         * @group taxonomy
     1008         */
     1009        public function test_tax_query_relation_or_one_clause_empty_terms() {
     1010                // An empty tax query should return an empty array, not all posts.
     1011
     1012                $this->factory->post->create_many( 10 );
     1013
     1014                $query = new WP_Query( array(
     1015                        'fields' => 'ids',
     1016                        'update_post_term_cache' => false,
     1017                        'update_post_meta_cache' => false,
     1018                        'tax_query' => array(
     1019                                'relation' => 'OR',
     1020                                array(
     1021                                        'taxonomy' => 'post_tag',
     1022                                        'field' => 'id',
     1023                                        'terms' => array( 'foo' ),
     1024                                        'operator' => 'IN'
     1025                                ),
     1026                                array(
     1027                                        'taxonomy' => 'category',
     1028                                        'field' => 'id',
     1029                                        'terms' => false,
     1030                                        'operator' => 'IN'
     1031                                ),
     1032                        )
     1033                ) );
     1034
     1035                $posts = $query->get_posts();
     1036                $this->assertEquals( 0 , count( $posts ) );
     1037        }
     1038
     1039        /**
     1040         * @group taxonomy
     1041         */
     1042        public function test_tax_query_include_children() {
    5471043                $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Australia' ) );
    5481044                $cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Sydney', 'parent' => $cat_a ) );
    5491045                $cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'East Syndney', 'parent' => $cat_b ) );
    class Tests_Post_Query extends WP_UnitTestCase { 
    5551051                $post_d = $this->factory->post->create( array( 'post_category' => array( $cat_d ) ) );
    5561052
    5571053                $posts = get_posts( array(
     1054                        'fields' => 'ids',
     1055                        'update_post_meta_cache' => false,
     1056                        'update_post_term_cache' => false,
    5581057                        'tax_query' => array(
    5591058                                array(
    5601059                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    5671066                $this->assertEquals( 4 , count( $posts ) );
    5681067
    5691068                $posts = get_posts( array(
     1069                        'fields' => 'ids',
     1070                        'update_post_meta_cache' => false,
     1071                        'update_post_term_cache' => false,
    5701072                        'tax_query' => array(
    5711073                                array(
    5721074                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    5801082                $this->assertEquals( 1 , count( $posts ) );
    5811083
    5821084                $posts = get_posts( array(
     1085                        'fields' => 'ids',
     1086                        'update_post_meta_cache' => false,
     1087                        'update_post_term_cache' => false,
    5831088                        'tax_query' => array(
    5841089                                array(
    5851090                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    5921097                $this->assertEquals( 3 , count( $posts ) );
    5931098
    5941099                $posts = get_posts( array(
     1100                        'fields' => 'ids',
     1101                        'update_post_meta_cache' => false,
     1102                        'update_post_term_cache' => false,
    5951103                        'tax_query' => array(
    5961104                                array(
    5971105                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    6051113                $this->assertEquals( 1 , count( $posts ) );
    6061114
    6071115                $posts = get_posts( array(
     1116                        'fields' => 'ids',
     1117                        'update_post_meta_cache' => false,
     1118                        'update_post_term_cache' => false,
    6081119                        'tax_query' => array(
    6091120                                array(
    6101121                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    6171128                $this->assertEquals( 1 , count( $posts ) );
    6181129
    6191130                $posts = get_posts( array(
     1131                        'fields' => 'ids',
     1132                        'update_post_meta_cache' => false,
     1133                        'update_post_term_cache' => false,
    6201134                        'tax_query' => array(
    6211135                                array(
    6221136                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    6311145        }
    6321146
    6331147        /**
     1148         * @group taxonomy
     1149         */
     1150        function test_category__and_var() {
     1151                $q = new WP_Query();
     1152
     1153                $term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
     1154                $term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
     1155                $post_id = $this->factory->post->create();
     1156
     1157                wp_set_post_categories( $post_id, $term_id );
     1158
     1159                $posts = $q->query( array( 'category__and' => array( $term_id ) ) );
     1160
     1161                $this->assertEmpty( $q->get( 'category__and' ) );
     1162                $this->assertCount( 0, $q->get( 'category__and' ) );
     1163                $this->assertNotEmpty( $q->get( 'category__in' ) );
     1164                $this->assertCount( 1, $q->get( 'category__in' ) );
     1165
     1166                $this->assertNotEmpty( $posts );
     1167                $this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
     1168
     1169                $posts2 = $q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
     1170                $this->assertNotEmpty( $q->get( 'category__and' ) );
     1171                $this->assertCount( 2, $q->get( 'category__and' ) );
     1172                $this->assertEmpty( $q->get( 'category__in' ) );
     1173                $this->assertCount( 0, $q->get( 'category__in' ) );
     1174
     1175                $this->assertEmpty( $posts2 );
     1176        }
     1177
     1178        /**
     1179         * @group taxonomy
     1180         */
     1181        public function test_tax_query_taxonomy_with_attachments() {
     1182                $q = new WP_Query();
     1183
     1184                register_taxonomy_for_object_type( 'post_tag', 'attachment:image' );
     1185                $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) );
     1186                $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
     1187                        'post_mime_type' => 'image/jpeg',
     1188                        'post_type' => 'attachment'
     1189                ) );
     1190                wp_set_object_terms( $image_id, $tag_id, 'post_tag' );
     1191
     1192                $posts = $q->query( array(
     1193                        'fields' => 'ids',
     1194                        'update_post_meta_cache' => false,
     1195                        'update_post_term_cache' => false,
     1196                        'post_type' => 'attachment',
     1197                        'post_status' => 'inherit',
     1198                        'tax_query' => array(
     1199                                array(
     1200                                        'taxonomy' => 'post_tag',
     1201                                        'field' => 'term_id',
     1202                                        'terms' => array( $tag_id )
     1203                                )
     1204                        )
     1205                ) );
     1206
     1207                $this->assertEquals( array( $image_id ), $posts );
     1208        }
     1209
     1210        /**
     1211         * @ticket 27193
     1212         * @group taxonomy
     1213         */
     1214        function test_cat_or_tag() {
     1215                $category1 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'alpha' ) );
     1216                $category2 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'beta' ) );
     1217
     1218                $tag1 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'gamma' ) );
     1219                $tag2 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'delta' ) );
     1220
     1221                $post_id1 = $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $category1 ) ) );
     1222                $terms1 = get_the_category( $post_id1 );
     1223                $this->assertEquals( array( get_category( $category1 ) ), $terms1 );
     1224
     1225                $post_id2 = $this->factory->post->create( array( 'post_title' => 'beta', 'post_category' => array( $category2 ) ) );
     1226                $terms2 = get_the_category( $post_id2 );
     1227                $this->assertEquals( array( get_category( $category2 ) ), $terms2 );
     1228
     1229                $post_id3 = $this->factory->post->create( array( 'post_title' => 'gamma', 'post_tag' => array( $tag1 ) ) );
     1230                $post_id4 = $this->factory->post->create( array( 'post_title' => 'delta', 'post_tag' => array( $tag2 ) ) );
     1231
     1232                $query = new WP_Query( array(
     1233                        'fields' => 'ids',
     1234                        'update_post_meta_cache' => false,
     1235                        'update_post_term_cache' => false,
     1236                        'tax_query' => array(
     1237                                //'relation' => 'OR',
     1238                                array(
     1239                                        'taxonomy' => 'category',
     1240                                        'field' => 'term_id',
     1241                                        'terms' => array( $category1, $category2 )
     1242                                )
     1243                        )
     1244                ) );
     1245                $ids = $query->get_posts();
     1246                $this->assertEquals( array( $post_id1, $post_id2 ), $ids );
     1247        }
     1248
     1249        /**
     1250         * @group taxonomy
     1251         */
     1252        function test_tax_query_no_taxonomy() {
     1253                $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) );
     1254                $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) );
     1255
     1256                $response1 = new WP_Query( array(
     1257                        'tax_query' => array(
     1258                                array( 'terms' => array( $cat_id ) )
     1259                        )
     1260                ) );
     1261                $this->assertEmpty( $response1->posts );
     1262
     1263                $response2 = new WP_Query( array(
     1264                        'fields' => 'ids',
     1265                        'update_post_meta_cache' => false,
     1266                        'update_post_term_cache' => false,
     1267                        'tax_query' => array(
     1268                                array(
     1269                                        'taxonomy' => 'category',
     1270                                        'terms' => array( $cat_id )
     1271                                )
     1272                        )
     1273                ) );
     1274                $this->assertNotEmpty( $response2->posts );
     1275
     1276                $term = get_category( $cat_id );
     1277                $response3 = new WP_Query( array(
     1278                        'fields' => 'ids',
     1279                        'update_post_meta_cache' => false,
     1280                        'update_post_term_cache' => false,
     1281                        'tax_query' => array(
     1282                                array(
     1283                                        'field' => 'term_taxonomy_id',
     1284                                        'terms' => array( $term->term_taxonomy_id )
     1285                                )
     1286                        )
     1287                ) );
     1288                $this->assertNotEmpty( $response3->posts );
     1289        }
     1290
     1291        /**
     1292         * @group taxonomy
     1293         */
     1294        function test_term_taxonomy_id_field_no_taxonomy() {
     1295                $q = new WP_Query();
     1296
     1297                $posts = $this->factory->post->create_many( 5 );
     1298
     1299                $cats = $tags = array();
     1300
     1301                // need term_taxonomy_ids in addition to term_ids, so no factory
     1302                for ( $i = 0; $i < 5; $i++ ) {
     1303                        $cats[$i] = wp_insert_term( 'category-' . $i , 'category' );
     1304                        $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' );
     1305
     1306                        // post 0 gets all terms
     1307                        wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true );
     1308                        wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true );
     1309                }
     1310
     1311                wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
     1312                wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' );
     1313
     1314                wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' );
     1315                wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
     1316
     1317                wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
     1318                wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
     1319
     1320                $results1 = $q->query( array(
     1321                        'fields' => 'ids',
     1322                        'update_post_meta_cache' => false,
     1323                        'update_post_term_cache' => false,
     1324                        'orderby' => 'ID',
     1325                        'order' => 'ASC',
     1326                        'tax_query' => array(
     1327                                'relation' => 'OR',
     1328                                array(
     1329                                        'field' => 'term_taxonomy_id',
     1330                                        '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'] ),
     1331                                        'operator' => 'AND',
     1332                                        'include_children' => false,
     1333                                ),
     1334                                array(
     1335                                        'field' => 'term_taxonomy_id',
     1336                                        'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
     1337                                        'operator' => 'AND',
     1338                                        'include_children' => false,
     1339                                )
     1340                        )
     1341                ) );
     1342
     1343                $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' );
     1344
     1345                $results2 = $q->query( array(
     1346                        'fields' => 'ids',
     1347                        'update_post_meta_cache' => false,
     1348                        'update_post_term_cache' => false,
     1349                        'orderby' => 'ID',
     1350                        'order' => 'ASC',
     1351                        'tax_query' => array(
     1352                                'relation' => 'AND',
     1353                                array(
     1354                                        'field' => 'term_taxonomy_id',
     1355                                        'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ),
     1356                                        'operator' => 'IN',
     1357                                        'include_children' => false,
     1358                                ),
     1359                                array(
     1360                                        'field' => 'term_taxonomy_id',
     1361                                        'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
     1362                                        'operator' => 'IN',
     1363                                        'include_children' => false,
     1364                                )
     1365                        )
     1366                ) );
     1367
     1368                $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' );
     1369        }
     1370
     1371        /**
     1372         * @ticket 28099
     1373         * @group taxonomy
     1374         */
     1375        function test_empty_category__in() {
     1376                $cat_id = $this->factory->category->create();
     1377                $post_id = $this->factory->post->create();
     1378                wp_set_post_categories( $post_id, $cat_id );
     1379
     1380                $q1 = get_posts( array( 'category__in' => array( $cat_id ) ) );
     1381                $this->assertNotEmpty( $q1 );
     1382                $q2 = get_posts( array( 'category__in' => array() ) );
     1383                $this->assertNotEmpty( $q2 );
     1384
     1385                $tag = wp_insert_term( 'woo', 'post_tag' );
     1386                $tag_id = $tag['term_id'];
     1387                $slug = get_tag( $tag_id )->slug;
     1388                wp_set_post_tags( $post_id, $slug );
     1389
     1390                $q3 = get_posts( array( 'tag__in' => array( $tag_id ) ) );
     1391                $this->assertNotEmpty( $q3 );
     1392                $q4 = get_posts( array( 'tag__in' => array() ) );
     1393                $this->assertNotEmpty( $q4 );
     1394
     1395                $q5 = get_posts( array( 'tag_slug__in' => array( $slug ) ) );
     1396                $this->assertNotEmpty( $q5 );
     1397                $q6 = get_posts( array( 'tag_slug__in' => array() ) );
     1398                $this->assertNotEmpty( $q6 );
     1399        }
     1400
     1401        /**
     1402         * @group taxonomy
     1403         * @ticket 29718
     1404         */
     1405        public function test_populate_taxonomy_query_var_from_tax_query() {
     1406                register_taxonomy( 'foo', 'post' );
     1407                $t = $this->factory->term->create( array(
     1408                        'taxonomy' => 'foo',
     1409                ) );
     1410                $c = $this->factory->term->create( array(
     1411                        'taxonomy' => 'category',
     1412                ) );
     1413
     1414                $q = new WP_Query( array(
     1415                        'tax_query' => array(
     1416                                // Empty terms mean that this one should be skipped
     1417                                array(
     1418                                        'taxonomy' => 'bar',
     1419                                        'terms' => array(),
     1420                                ),
     1421
     1422                                // Category and post tags should be skipped
     1423                                array(
     1424                                        'taxonomy' => 'category',
     1425                                        'terms' => array( $c ),
     1426                                ),
     1427
     1428                                array(
     1429                                        'taxonomy' => 'foo',
     1430                                        'terms' => array( $t ),
     1431                                ),
     1432                        ),
     1433                ) );
     1434
     1435                $this->assertSame( 'foo', $q->get( 'taxonomy' ) );
     1436
     1437                _unregister_taxonomy( 'foo' );
     1438        }
     1439
     1440        /**
     1441         * @group taxonomy
     1442         */
     1443        public function test_populate_taxonomy_query_var_from_tax_query_taxonomy_already_set() {
     1444                register_taxonomy( 'foo', 'post' );
     1445                register_taxonomy( 'foo1', 'post' );
     1446                $t = $this->factory->term->create( array(
     1447                        'taxonomy' => 'foo',
     1448                ) );
     1449
     1450                $q = new WP_Query( array(
     1451                        'taxonomy' => 'bar',
     1452                        'tax_query' => array(
     1453                                array(
     1454                                        'taxonomy' => 'foo',
     1455                                        'terms' => array( $t ),
     1456                                ),
     1457                        ),
     1458                ) );
     1459
     1460                $this->assertSame( 'bar', $q->get( 'taxonomy' ) );
     1461
     1462                _unregister_taxonomy( 'foo' );
     1463                _unregister_taxonomy( 'bar' );
     1464        }
     1465
     1466        /**
     1467         * @group taxonomy
     1468         */
     1469        public function test_populate_term_query_var_from_tax_query() {
     1470                register_taxonomy( 'foo', 'post' );
     1471                $t = $this->factory->term->create( array(
     1472                        'taxonomy' => 'foo',
     1473                        'slug' => 'bar',
     1474                ) );
     1475
     1476                $q = new WP_Query( array(
     1477                        'tax_query' => array(
     1478                                array(
     1479                                        'taxonomy' => 'foo',
     1480                                        'terms' => array( 'bar' ),
     1481                                        'field' => 'slug',
     1482                                ),
     1483                        ),
     1484                ) );
     1485
     1486                $this->assertSame( 'bar', $q->get( 'term' ) );
     1487
     1488                _unregister_taxonomy( 'foo' );
     1489        }
     1490
     1491        /**
     1492         * @group taxonomy
     1493         */
     1494        public function test_populate_term_id_query_var_from_tax_query() {
     1495                register_taxonomy( 'foo', 'post' );
     1496                $t = $this->factory->term->create( array(
     1497                        'taxonomy' => 'foo',
     1498                        'slug' => 'bar',
     1499                ) );
     1500
     1501                $q = new WP_Query( array(
     1502                        'tax_query' => array(
     1503                                array(
     1504                                        'taxonomy' => 'foo',
     1505                                        'terms' => array( $t ),
     1506                                        'field' => 'term_id',
     1507                                ),
     1508                        ),
     1509                ) );
     1510
     1511                $this->assertEquals( $t, $q->get( 'term_id' ) );
     1512
     1513                _unregister_taxonomy( 'foo' );
     1514        }
     1515
     1516        /**
     1517         * @group taxonomy
     1518         * @ticket 29718
     1519         */
     1520        public function test_populate_cat_category_name_query_var_from_tax_query() {
     1521                register_taxonomy( 'foo', 'post' );
     1522                $t = $this->factory->term->create( array(
     1523                        'taxonomy' => 'foo',
     1524                ) );
     1525                $c = $this->factory->term->create( array(
     1526                        'taxonomy' => 'foo',
     1527                        'slug' => 'bar',
     1528                ) );
     1529
     1530                $q = new WP_Query( array(
     1531                        'tax_query' => array(
     1532                                // Non-category should be skipped
     1533                                array(
     1534                                        'taxonomy' => 'foo',
     1535                                        'terms' => array( $t ),
     1536                                ),
     1537
     1538                                // Empty terms mean that this one should be skipped
     1539                                array(
     1540                                        'taxonomy' => 'category',
     1541                                        'terms' => array(),
     1542                                ),
     1543
     1544                                // Category and post tags should be skipped
     1545                                array(
     1546                                        'taxonomy' => 'category',
     1547                                        'terms' => array( $c ),
     1548                                ),
     1549                        ),
     1550                ) );
     1551
     1552                $this->assertEquals( $c, $q->get( 'cat' ) );
     1553                $this->assertEquals( 'bar', $q->get( 'category_name' ) );
     1554
     1555                _unregister_taxonomy( 'foo' );
     1556        }
     1557
     1558        /**
     1559         * @group taxonomy
     1560         * @ticket 29718
     1561         */
     1562        public function test_populate_tag_id_query_var_from_tax_query() {
     1563                register_taxonomy( 'foo', 'post' );
     1564                $t = $this->factory->term->create( array(
     1565                        'taxonomy' => 'foo',
     1566                ) );
     1567                $tag = $this->factory->term->create( array(
     1568                        'taxonomy' => 'post_tag',
     1569                        'slug' => 'bar',
     1570                ) );
     1571
     1572                $q = new WP_Query( array(
     1573                        'tax_query' => array(
     1574                                // Non-tag should be skipped
     1575                                array(
     1576                                        'taxonomy' => 'foo',
     1577                                        'terms' => array( $t ),
     1578                                ),
     1579
     1580                                // Empty terms mean that this one should be skipped
     1581                                array(
     1582                                        'taxonomy' => 'post_tag',
     1583                                        'terms' => array(),
     1584                                ),
     1585
     1586                                // Category and post tags should be skipped
     1587                                array(
     1588                                        'taxonomy' => 'post_tag',
     1589                                        'terms' => array( $tag ),
     1590                                ),
     1591                        ),
     1592                ) );
     1593
     1594                $this->assertEquals( $tag, $q->get( 'tag_id' ) );
     1595
     1596                _unregister_taxonomy( 'foo' );
     1597        }
     1598
     1599        /**
    6341600         * @ticket 22448
    6351601         */
    6361602        function test_the_posts_filter() {
    class Tests_Post_Query extends WP_UnitTestCase { 
    8311797                        $q3->request
    8321798                );
    8331799        }
    834 }
    835  No newline at end of file
     1800
     1801}
  • tests/phpunit/tests/term/query.php

    diff --git tests/phpunit/tests/term/query.php tests/phpunit/tests/term/query.php
    index 0a3c724..5d839cb 100644
     
    66class Tests_Tax_Query extends WP_UnitTestCase {
    77        protected $q;
    88
    9         function setUp() {
     9        public function setUp() {
    1010                parent::setUp();
    1111                unset( $this->q );
    1212                $this->q = new WP_Query();
    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();
     15        public function test_construct_with_relation_default() {
     16                $tq = new WP_Tax_Query( array() );
     17                $this->assertSame( 'AND', $tq->relation );
     18        }
    1919
    20                 wp_set_post_categories( $post_id, $term_id );
     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        }
    2126
    22                 $posts = $this->q->query( array( 'category__and' => array( $term_id ) ) );
     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        }
    2333
    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' ) );
     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        }
    2840
    29                 $this->assertNotEmpty( $posts );
    30                 $this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
     41        public function test_construct_fill_missing_query_params() {
     42                $tq = new WP_Tax_Query( array(
     43                        array(),
     44                ) );
    3145
    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' ) );
     46                $expected = array(
     47                        'taxonomy' => '',
     48                        'terms' => array(),
     49                        'include_children' => true,
     50                        'field' => 'term_id',
     51                        'operator' => 'IN',
     52                );
    3753
    38                 $this->assertEmpty( $posts2 );
     54                $this->assertEquals( $expected, $tq->queries[0] );
    3955        }
    4056
    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                         )
     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                        ),
    6164                ) );
    6265
    63                 $this->assertEquals( array( $image_id ), $posts );
     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] );
    6476        }
    6577
    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' ) );
     78        public function test_construct_cast_terms_to_array() {
     79                $tq = new WP_Tax_Query( array(
     80                        array(
     81                                'terms' => 'foo',
     82                        ),
     83                ) );
    7284
    73                 $tag1 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'gamma' ) );
    74                 $tag2 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'delta' ) );
     85                $this->assertEquals( array( 'foo', ), $tq->queries[0]['terms'] );
     86        }
    7587
    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 );
     88        public function test_transform_query_terms_empty() {
     89                $tq = new WP_Tax_Query( array(
     90                        array(),
     91                ) );
     92                $query = $tq->queries[0];
    7993
    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 );
     94                $tq->transform_query( $tq->queries[0], 'term_id' );
    8395
    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 ) ) );
     96                $this->assertSame( $query, $tq->queries[0] );
     97        }
    8698
    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                         )
     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                        ),
    97104                ) );
    98                 $ids = $query->get_posts();
    99                 $this->assertEquals( array( $post_id1, $post_id2 ), $ids );
    100         }
     105                $query = $tq->queries[0];
    101106
    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 ) ) );
     107                $tq->transform_query( $tq->queries[0], 'term_id' );
    105108
    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                         )
     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                        ),
    120123                ) );
    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                         )
     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                        ),
    131131                ) );
    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                         )
     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                        ),
    178148                ) );
     149                $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
    179150
    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                         )
     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                        ),
    201166                ) );
     167                $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
    202168
    203                 $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' );
     169                $this->assertSame( $tt_ids, $tq->queries[0]['terms'] );
     170                $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
    204171        }
    205172
    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 );
     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' );
    213186
    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 );
     187                $this->assertEquals( array( $t1 ), $tq->queries[0]['terms'] );
     188                $this->assertSame( 'term_id', $tq->queries[0]['field'] );
     189        }
    218190
    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 );
     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' );
    223204
    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 );
     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' );
    228218
    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 );
     219                $this->assertTrue( is_wp_error( $tq->queries[0] ) );
    233220        }
    234221}