Make WordPress Core

Ticket #29718: 29718.patch

File 29718.patch, 37.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..77db35b 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_empty_or() {
     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         * @group taxonomy
     1007         */
     1008        public function test_tax_query_include_children() {
    5471009                $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Australia' ) );
    5481010                $cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Sydney', 'parent' => $cat_a ) );
    5491011                $cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'East Syndney', 'parent' => $cat_b ) );
    class Tests_Post_Query extends WP_UnitTestCase { 
    5551017                $post_d = $this->factory->post->create( array( 'post_category' => array( $cat_d ) ) );
    5561018
    5571019                $posts = get_posts( array(
     1020                        'fields' => 'ids',
     1021                        'update_post_meta_cache' => false,
     1022                        'update_post_term_cache' => false,
    5581023                        'tax_query' => array(
    5591024                                array(
    5601025                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    5671032                $this->assertEquals( 4 , count( $posts ) );
    5681033
    5691034                $posts = get_posts( array(
     1035                        'fields' => 'ids',
     1036                        'update_post_meta_cache' => false,
     1037                        'update_post_term_cache' => false,
    5701038                        'tax_query' => array(
    5711039                                array(
    5721040                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    5801048                $this->assertEquals( 1 , count( $posts ) );
    5811049
    5821050                $posts = get_posts( array(
     1051                        'fields' => 'ids',
     1052                        'update_post_meta_cache' => false,
     1053                        'update_post_term_cache' => false,
    5831054                        'tax_query' => array(
    5841055                                array(
    5851056                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    5921063                $this->assertEquals( 3 , count( $posts ) );
    5931064
    5941065                $posts = get_posts( array(
     1066                        'fields' => 'ids',
     1067                        'update_post_meta_cache' => false,
     1068                        'update_post_term_cache' => false,
    5951069                        'tax_query' => array(
    5961070                                array(
    5971071                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    6051079                $this->assertEquals( 1 , count( $posts ) );
    6061080
    6071081                $posts = get_posts( array(
     1082                        'fields' => 'ids',
     1083                        'update_post_meta_cache' => false,
     1084                        'update_post_term_cache' => false,
    6081085                        'tax_query' => array(
    6091086                                array(
    6101087                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    6171094                $this->assertEquals( 1 , count( $posts ) );
    6181095
    6191096                $posts = get_posts( array(
     1097                        'fields' => 'ids',
     1098                        'update_post_meta_cache' => false,
     1099                        'update_post_term_cache' => false,
    6201100                        'tax_query' => array(
    6211101                                array(
    6221102                                        'taxonomy' => 'category',
    class Tests_Post_Query extends WP_UnitTestCase { 
    6311111        }
    6321112
    6331113        /**
     1114         * @group taxonomy
     1115         */
     1116        function test_category__and_var() {
     1117                $q = new WP_Query();
     1118
     1119                $term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
     1120                $term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
     1121                $post_id = $this->factory->post->create();
     1122
     1123                wp_set_post_categories( $post_id, $term_id );
     1124
     1125                $posts = $q->query( array( 'category__and' => array( $term_id ) ) );
     1126
     1127                $this->assertEmpty( $q->get( 'category__and' ) );
     1128                $this->assertCount( 0, $q->get( 'category__and' ) );
     1129                $this->assertNotEmpty( $q->get( 'category__in' ) );
     1130                $this->assertCount( 1, $q->get( 'category__in' ) );
     1131
     1132                $this->assertNotEmpty( $posts );
     1133                $this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
     1134
     1135                $posts2 = $q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
     1136                $this->assertNotEmpty( $q->get( 'category__and' ) );
     1137                $this->assertCount( 2, $q->get( 'category__and' ) );
     1138                $this->assertEmpty( $q->get( 'category__in' ) );
     1139                $this->assertCount( 0, $q->get( 'category__in' ) );
     1140
     1141                $this->assertEmpty( $posts2 );
     1142        }
     1143
     1144        /**
     1145         * @group taxonomy
     1146         */
     1147        public function test_tax_query_taxonomy_with_attachments() {
     1148                $q = new WP_Query();
     1149
     1150                register_taxonomy_for_object_type( 'post_tag', 'attachment:image' );
     1151                $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) );
     1152                $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
     1153                        'post_mime_type' => 'image/jpeg',
     1154                        'post_type' => 'attachment'
     1155                ) );
     1156                wp_set_object_terms( $image_id, $tag_id, 'post_tag' );
     1157
     1158                $posts = $q->query( array(
     1159                        'fields' => 'ids',
     1160                        'update_post_meta_cache' => false,
     1161                        'update_post_term_cache' => false,
     1162                        'post_type' => 'attachment',
     1163                        'post_status' => 'inherit',
     1164                        'tax_query' => array(
     1165                                array(
     1166                                        'taxonomy' => 'post_tag',
     1167                                        'field' => 'term_id',
     1168                                        'terms' => array( $tag_id )
     1169                                )
     1170                        )
     1171                ) );
     1172
     1173                $this->assertEquals( array( $image_id ), $posts );
     1174        }
     1175
     1176        /**
     1177         * @ticket 27193
     1178         * @group taxonomy
     1179         */
     1180        function test_cat_or_tag() {
     1181                $category1 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'alpha' ) );
     1182                $category2 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'beta' ) );
     1183
     1184                $tag1 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'gamma' ) );
     1185                $tag2 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'delta' ) );
     1186
     1187                $post_id1 = $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $category1 ) ) );
     1188                $terms1 = get_the_category( $post_id1 );
     1189                $this->assertEquals( array( get_category( $category1 ) ), $terms1 );
     1190
     1191                $post_id2 = $this->factory->post->create( array( 'post_title' => 'beta', 'post_category' => array( $category2 ) ) );
     1192                $terms2 = get_the_category( $post_id2 );
     1193                $this->assertEquals( array( get_category( $category2 ) ), $terms2 );
     1194
     1195                $post_id3 = $this->factory->post->create( array( 'post_title' => 'gamma', 'post_tag' => array( $tag1 ) ) );
     1196                $post_id4 = $this->factory->post->create( array( 'post_title' => 'delta', 'post_tag' => array( $tag2 ) ) );
     1197
     1198                $query = new WP_Query( array(
     1199                        'fields' => 'ids',
     1200                        'update_post_meta_cache' => false,
     1201                        'update_post_term_cache' => false,
     1202                        'tax_query' => array(
     1203                                //'relation' => 'OR',
     1204                                array(
     1205                                        'taxonomy' => 'category',
     1206                                        'field' => 'term_id',
     1207                                        'terms' => array( $category1, $category2 )
     1208                                )
     1209                        )
     1210                ) );
     1211                $ids = $query->get_posts();
     1212                $this->assertEquals( array( $post_id1, $post_id2 ), $ids );
     1213        }
     1214
     1215        /**
     1216         * @group taxonomy
     1217         */
     1218        function test_tax_query_no_taxonomy() {
     1219                $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) );
     1220                $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) );
     1221
     1222                $response1 = new WP_Query( array(
     1223                        'tax_query' => array(
     1224                                array( 'terms' => array( $cat_id ) )
     1225                        )
     1226                ) );
     1227                $this->assertEmpty( $response1->posts );
     1228
     1229                $response2 = new WP_Query( array(
     1230                        'fields' => 'ids',
     1231                        'update_post_meta_cache' => false,
     1232                        'update_post_term_cache' => false,
     1233                        'tax_query' => array(
     1234                                array(
     1235                                        'taxonomy' => 'category',
     1236                                        'terms' => array( $cat_id )
     1237                                )
     1238                        )
     1239                ) );
     1240                $this->assertNotEmpty( $response2->posts );
     1241
     1242                $term = get_category( $cat_id );
     1243                $response3 = new WP_Query( array(
     1244                        'fields' => 'ids',
     1245                        'update_post_meta_cache' => false,
     1246                        'update_post_term_cache' => false,
     1247                        'tax_query' => array(
     1248                                array(
     1249                                        'field' => 'term_taxonomy_id',
     1250                                        'terms' => array( $term->term_taxonomy_id )
     1251                                )
     1252                        )
     1253                ) );
     1254                $this->assertNotEmpty( $response3->posts );
     1255        }
     1256
     1257        /**
     1258         * @group taxonomy
     1259         */
     1260        function test_term_taxonomy_id_field_no_taxonomy() {
     1261                $q = new WP_Query();
     1262
     1263                $posts = $this->factory->post->create_many( 5 );
     1264
     1265                $cats = $tags = array();
     1266
     1267                // need term_taxonomy_ids in addition to term_ids, so no factory
     1268                for ( $i = 0; $i < 5; $i++ ) {
     1269                        $cats[$i] = wp_insert_term( 'category-' . $i , 'category' );
     1270                        $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' );
     1271
     1272                        // post 0 gets all terms
     1273                        wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true );
     1274                        wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true );
     1275                }
     1276
     1277                wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
     1278                wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' );
     1279
     1280                wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' );
     1281                wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
     1282
     1283                wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
     1284                wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
     1285
     1286                $results1 = $q->query( array(
     1287                        'fields' => 'ids',
     1288                        'update_post_meta_cache' => false,
     1289                        'update_post_term_cache' => false,
     1290                        'orderby' => 'ID',
     1291                        'order' => 'ASC',
     1292                        'tax_query' => array(
     1293                                'relation' => 'OR',
     1294                                array(
     1295                                        'field' => 'term_taxonomy_id',
     1296                                        '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'] ),
     1297                                        'operator' => 'AND',
     1298                                        'include_children' => false,
     1299                                ),
     1300                                array(
     1301                                        'field' => 'term_taxonomy_id',
     1302                                        'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
     1303                                        'operator' => 'AND',
     1304                                        'include_children' => false,
     1305                                )
     1306                        )
     1307                ) );
     1308
     1309                $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' );
     1310
     1311                $results2 = $q->query( array(
     1312                        'fields' => 'ids',
     1313                        'update_post_meta_cache' => false,
     1314                        'update_post_term_cache' => false,
     1315                        'orderby' => 'ID',
     1316                        'order' => 'ASC',
     1317                        'tax_query' => array(
     1318                                'relation' => 'AND',
     1319                                array(
     1320                                        'field' => 'term_taxonomy_id',
     1321                                        'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ),
     1322                                        'operator' => 'IN',
     1323                                        'include_children' => false,
     1324                                ),
     1325                                array(
     1326                                        'field' => 'term_taxonomy_id',
     1327                                        'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
     1328                                        'operator' => 'IN',
     1329                                        'include_children' => false,
     1330                                )
     1331                        )
     1332                ) );
     1333
     1334                $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' );
     1335        }
     1336
     1337        /**
     1338         * @ticket 28099
     1339         * @group taxonomy
     1340         */
     1341        function test_empty_category__in() {
     1342                $cat_id = $this->factory->category->create();
     1343                $post_id = $this->factory->post->create();
     1344                wp_set_post_categories( $post_id, $cat_id );
     1345
     1346                $q1 = get_posts( array( 'category__in' => array( $cat_id ) ) );
     1347                $this->assertNotEmpty( $q1 );
     1348                $q2 = get_posts( array( 'category__in' => array() ) );
     1349                $this->assertNotEmpty( $q2 );
     1350
     1351                $tag = wp_insert_term( 'woo', 'post_tag' );
     1352                $tag_id = $tag['term_id'];
     1353                $slug = get_tag( $tag_id )->slug;
     1354                wp_set_post_tags( $post_id, $slug );
     1355
     1356                $q3 = get_posts( array( 'tag__in' => array( $tag_id ) ) );
     1357                $this->assertNotEmpty( $q3 );
     1358                $q4 = get_posts( array( 'tag__in' => array() ) );
     1359                $this->assertNotEmpty( $q4 );
     1360
     1361                $q5 = get_posts( array( 'tag_slug__in' => array( $slug ) ) );
     1362                $this->assertNotEmpty( $q5 );
     1363                $q6 = get_posts( array( 'tag_slug__in' => array() ) );
     1364                $this->assertNotEmpty( $q6 );
     1365        }
     1366
     1367        /**
    6341368         * @ticket 22448
    6351369         */
    6361370        function test_the_posts_filter() {
    class Tests_Post_Query extends WP_UnitTestCase { 
    8311565                        $q3->request
    8321566                );
    8331567        }
    834 }
    835  No newline at end of file
     1568}
  • 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}