Make WordPress Core


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

Improve unit tests for WP_Tax_Query.

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

Fixes #29718

File:
1 edited

Legend:

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

    r29799 r29805  
    773773        }
    774774
    775         /**
    776          * @ticket 20604
    777          */
    778         function test_taxonomy_empty_or() {
    779                 // An empty tax query should return an empty array, not all posts.
    780 
    781                 $this->factory->post->create_many( 10 );
    782 
    783                 $query = new WP_Query( array(
    784                         'fields'        => 'ids',
    785                         'tax_query' => array(
    786                         'relation' => 'OR',
    787                         array(
    788                                 'taxonomy' => 'post_tag',
    789                                 'field' => 'id',
    790                                 'terms' => false,
    791                                 'operator' => 'IN'
    792                         ),
    793                         array(
    794                                 'taxonomy' => 'category',
    795                                 'field' => 'id',
    796                                 'terms' => false,
    797                                 'operator' => 'IN'
    798                         )
    799                         )
    800                 ) );
    801 
    802                 $posts = $query->get_posts();
    803                 $this->assertEquals( 0 , count( $posts ) );
    804         }
    805 
    806775        function test_meta_between_not_between() {
    807776                $post_id = $this->factory->post->create();
     
    984953        }
    985954
    986         function test_taxonomy_include_children() {
     955        /**
     956         * @group taxonomy
     957         */
     958        public function test_tax_query_single_query_single_term_field_slug() {
     959                $t = $this->factory->term->create( array(
     960                        'taxonomy' => 'category',
     961                        'slug' => 'foo',
     962                        'name' => 'Foo',
     963                ) );
     964                $p1 = $this->factory->post->create();
     965                $p2 = $this->factory->post->create();
     966
     967                wp_set_post_terms( $p1, $t, 'category' );
     968
     969                $q = new WP_Query( array(
     970                        'fields' => 'ids',
     971                        'update_post_meta_cache' => false,
     972                        'update_post_term_cache' => false,
     973                        'tax_query' => array(
     974                                array(
     975                                        'taxonomy' => 'category',
     976                                        'terms' => array( 'foo' ),
     977                                        'field' => 'slug',
     978                                ),
     979                        ),
     980                ) );
     981
     982                $this->assertEquals( array( $p1 ), $q->posts );
     983        }
     984
     985        /**
     986         * @group taxonomy
     987         */
     988        public function test_tax_query_single_query_single_term_field_name() {
     989                $t = $this->factory->term->create( array(
     990                        'taxonomy' => 'category',
     991                        'slug' => 'foo',
     992                        'name' => 'Foo',
     993                ) );
     994                $p1 = $this->factory->post->create();
     995                $p2 = $this->factory->post->create();
     996
     997                wp_set_post_terms( $p1, $t, 'category' );
     998
     999                $q = new WP_Query( array(
     1000                        'fields' => 'ids',
     1001                        'update_post_meta_cache' => false,
     1002                        'update_post_term_cache' => false,
     1003                        'tax_query' => array(
     1004                                array(
     1005                                        'taxonomy' => 'category',
     1006                                        'terms' => array( 'Foo' ),
     1007                                        'field' => 'name',
     1008                                ),
     1009                        ),
     1010                ) );
     1011
     1012                $this->assertEquals( array( $p1 ), $q->posts );
     1013        }
     1014
     1015        /**
     1016         * @group taxonomy
     1017         */
     1018        public function test_tax_query_single_query_single_term_field_term_taxonomy_id() {
     1019                $t = $this->factory->term->create( array(
     1020                        'taxonomy' => 'category',
     1021                        'slug' => 'foo',
     1022                        'name' => 'Foo',
     1023                ) );
     1024                $p1 = $this->factory->post->create();
     1025                $p2 = $this->factory->post->create();
     1026
     1027                $tt_ids = wp_set_post_terms( $p1, $t, 'category' );
     1028
     1029                $q = new WP_Query( array(
     1030                        'fields' => 'ids',
     1031                        'update_post_meta_cache' => false,
     1032                        'update_post_term_cache' => false,
     1033                        'tax_query' => array(
     1034                                array(
     1035                                        'taxonomy' => 'category',
     1036                                        'terms' => $tt_ids,
     1037                                        'field' => 'term_taxonomy_id',
     1038                                ),
     1039                        ),
     1040                ) );
     1041
     1042                $this->assertEquals( array( $p1 ), $q->posts );
     1043        }
     1044
     1045        /**
     1046         * @group taxonomy
     1047         */
     1048        public function test_tax_query_single_query_single_term_field_term_id() {
     1049                $t = $this->factory->term->create( array(
     1050                        'taxonomy' => 'category',
     1051                        'slug' => 'foo',
     1052                        'name' => 'Foo',
     1053                ) );
     1054                $p1 = $this->factory->post->create();
     1055                $p2 = $this->factory->post->create();
     1056
     1057                wp_set_post_terms( $p1, $t, 'category' );
     1058
     1059                $q = new WP_Query( array(
     1060                        'fields' => 'ids',
     1061                        'update_post_meta_cache' => false,
     1062                        'update_post_term_cache' => false,
     1063                        'tax_query' => array(
     1064                                array(
     1065                                        'taxonomy' => 'category',
     1066                                        'terms' => array( $t ),
     1067                                        'field' => 'term_id',
     1068                                ),
     1069                        ),
     1070                ) );
     1071
     1072                $this->assertEquals( array( $p1 ), $q->posts );
     1073        }
     1074
     1075        /**
     1076         * @group taxonomy
     1077         */
     1078        public function test_tax_query_single_query_single_term_operator_in() {
     1079                $t = $this->factory->term->create( array(
     1080                        'taxonomy' => 'category',
     1081                        'slug' => 'foo',
     1082                        'name' => 'Foo',
     1083                ) );
     1084                $p1 = $this->factory->post->create();
     1085                $p2 = $this->factory->post->create();
     1086
     1087                wp_set_post_terms( $p1, $t, 'category' );
     1088
     1089                $q = new WP_Query( array(
     1090                        'fields' => 'ids',
     1091                        'update_post_meta_cache' => false,
     1092                        'update_post_term_cache' => false,
     1093                        'tax_query' => array(
     1094                                array(
     1095                                        'taxonomy' => 'category',
     1096                                        'terms' => array( 'foo' ),
     1097                                        'field' => 'slug',
     1098                                        'operator' => 'IN',
     1099                                ),
     1100                        ),
     1101                ) );
     1102
     1103                $this->assertEquals( array( $p1 ), $q->posts );
     1104        }
     1105
     1106        /**
     1107         * @group taxonomy
     1108         */
     1109        public function test_tax_query_single_query_single_term_operator_not_in() {
     1110                $t = $this->factory->term->create( array(
     1111                        'taxonomy' => 'category',
     1112                        'slug' => 'foo',
     1113                        'name' => 'Foo',
     1114                ) );
     1115                $p1 = $this->factory->post->create();
     1116                $p2 = $this->factory->post->create();
     1117
     1118                wp_set_post_terms( $p1, $t, 'category' );
     1119
     1120                $q = new WP_Query( array(
     1121                        'fields' => 'ids',
     1122                        'update_post_meta_cache' => false,
     1123                        'update_post_term_cache' => false,
     1124                        'tax_query' => array(
     1125                                array(
     1126                                        'taxonomy' => 'category',
     1127                                        'terms' => array( 'foo' ),
     1128                                        'field' => 'slug',
     1129                                        'operator' => 'NOT IN',
     1130                                ),
     1131                        ),
     1132                ) );
     1133
     1134                $this->assertEquals( array( $p2 ), $q->posts );
     1135        }
     1136
     1137        /**
     1138         * @group taxonomy
     1139         */
     1140        public function test_tax_query_single_query_single_term_operator_and() {
     1141                $t = $this->factory->term->create( array(
     1142                        'taxonomy' => 'category',
     1143                        'slug' => 'foo',
     1144                        'name' => 'Foo',
     1145                ) );
     1146                $p1 = $this->factory->post->create();
     1147                $p2 = $this->factory->post->create();
     1148
     1149                wp_set_post_terms( $p1, $t, 'category' );
     1150
     1151                $q = new WP_Query( array(
     1152                        'fields' => 'ids',
     1153                        'update_post_meta_cache' => false,
     1154                        'update_post_term_cache' => false,
     1155                        'tax_query' => array(
     1156                                array(
     1157                                        'taxonomy' => 'category',
     1158                                        'terms' => array( 'foo' ),
     1159                                        'field' => 'slug',
     1160                                        'operator' => 'AND',
     1161                                ),
     1162                        ),
     1163                ) );
     1164
     1165                $this->assertEquals( array( $p1 ), $q->posts );
     1166        }
     1167
     1168        /**
     1169         * @group taxonomy
     1170         */
     1171        public function test_tax_query_single_query_multiple_terms_operator_in() {
     1172                $t1 = $this->factory->term->create( array(
     1173                        'taxonomy' => 'category',
     1174                        'slug' => 'foo',
     1175                        'name' => 'Foo',
     1176                ) );
     1177                $t2 = $this->factory->term->create( array(
     1178                        'taxonomy' => 'category',
     1179                        'slug' => 'bar',
     1180                        'name' => 'Bar',
     1181                ) );
     1182                $p1 = $this->factory->post->create();
     1183                $p2 = $this->factory->post->create();
     1184                $p3 = $this->factory->post->create();
     1185
     1186                wp_set_post_terms( $p1, $t1, 'category' );
     1187                wp_set_post_terms( $p2, $t2, 'category' );
     1188
     1189                $q = new WP_Query( array(
     1190                        'fields' => 'ids',
     1191                        'update_post_meta_cache' => false,
     1192                        'update_post_term_cache' => false,
     1193                        'tax_query' => array(
     1194                                array(
     1195                                        'taxonomy' => 'category',
     1196                                        'terms' => array( 'foo', 'bar' ),
     1197                                        'field' => 'slug',
     1198                                        'operator' => 'IN',
     1199                                ),
     1200                        ),
     1201                ) );
     1202
     1203                $this->assertEquals( array( $p1, $p2 ), $q->posts );
     1204        }
     1205
     1206        /**
     1207         * @group taxonomy
     1208         */
     1209        public function test_tax_query_single_query_multiple_terms_operator_not_in() {
     1210                $t1 = $this->factory->term->create( array(
     1211                        'taxonomy' => 'category',
     1212                        'slug' => 'foo',
     1213                        'name' => 'Foo',
     1214                ) );
     1215                $t2 = $this->factory->term->create( array(
     1216                        'taxonomy' => 'category',
     1217                        'slug' => 'bar',
     1218                        'name' => 'Bar',
     1219                ) );
     1220                $p1 = $this->factory->post->create();
     1221                $p2 = $this->factory->post->create();
     1222                $p3 = $this->factory->post->create();
     1223
     1224                wp_set_post_terms( $p1, $t1, 'category' );
     1225                wp_set_post_terms( $p2, $t2, 'category' );
     1226
     1227                $q = new WP_Query( array(
     1228                        'fields' => 'ids',
     1229                        'update_post_meta_cache' => false,
     1230                        'update_post_term_cache' => false,
     1231                        'tax_query' => array(
     1232                                array(
     1233                                        'taxonomy' => 'category',
     1234                                        'terms' => array( 'foo', 'bar' ),
     1235                                        'field' => 'slug',
     1236                                        'operator' => 'NOT IN',
     1237                                ),
     1238                        ),
     1239                ) );
     1240
     1241                $this->assertEquals( array( $p3 ), $q->posts );
     1242        }
     1243
     1244        /**
     1245         * @group taxonomy
     1246         */
     1247        public function test_tax_query_single_query_multiple_terms_operator_and() {
     1248                $t1 = $this->factory->term->create( array(
     1249                        'taxonomy' => 'category',
     1250                        'slug' => 'foo',
     1251                        'name' => 'Foo',
     1252                ) );
     1253                $t2 = $this->factory->term->create( array(
     1254                        'taxonomy' => 'category',
     1255                        'slug' => 'bar',
     1256                        'name' => 'Bar',
     1257                ) );
     1258                $p1 = $this->factory->post->create();
     1259                $p2 = $this->factory->post->create();
     1260                $p3 = $this->factory->post->create();
     1261
     1262                wp_set_object_terms( $p1, $t1, 'category' );
     1263                wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
     1264
     1265                $q = new WP_Query( array(
     1266                        'fields' => 'ids',
     1267                        'update_post_meta_cache' => false,
     1268                        'update_post_term_cache' => false,
     1269                        'tax_query' => array(
     1270                                array(
     1271                                        'taxonomy' => 'category',
     1272                                        'terms' => array( 'foo', 'bar' ),
     1273                                        'field' => 'slug',
     1274                                        'operator' => 'AND',
     1275                                ),
     1276                        ),
     1277                ) );
     1278
     1279                $this->assertEquals( array( $p2 ), $q->posts );
     1280        }
     1281
     1282        /**
     1283         * @group taxonomy
     1284         */
     1285        public function test_tax_query_multiple_queries_relation_and() {
     1286                $t1 = $this->factory->term->create( array(
     1287                        'taxonomy' => 'category',
     1288                        'slug' => 'foo',
     1289                        'name' => 'Foo',
     1290                ) );
     1291                $t2 = $this->factory->term->create( array(
     1292                        'taxonomy' => 'category',
     1293                        'slug' => 'bar',
     1294                        'name' => 'Bar',
     1295                ) );
     1296                $p1 = $this->factory->post->create();
     1297                $p2 = $this->factory->post->create();
     1298                $p3 = $this->factory->post->create();
     1299
     1300                wp_set_object_terms( $p1, $t1, 'category' );
     1301                wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
     1302
     1303                $q = new WP_Query( array(
     1304                        'fields' => 'ids',
     1305                        'update_post_meta_cache' => false,
     1306                        'update_post_term_cache' => false,
     1307                        'tax_query' => array(
     1308                                'relation' => 'AND',
     1309                                array(
     1310                                        'taxonomy' => 'category',
     1311                                        'terms' => array( 'foo' ),
     1312                                        'field' => 'slug',
     1313                                ),
     1314                                array(
     1315                                        'taxonomy' => 'category',
     1316                                        'terms' => array( 'bar' ),
     1317                                        'field' => 'slug',
     1318                                ),
     1319                        ),
     1320                ) );
     1321
     1322                $this->assertEquals( array( $p2 ), $q->posts );
     1323        }
     1324
     1325        /**
     1326         * @group taxonomy
     1327         */
     1328        public function test_tax_query_multiple_queries_relation_or() {
     1329                $t1 = $this->factory->term->create( array(
     1330                        'taxonomy' => 'category',
     1331                        'slug' => 'foo',
     1332                        'name' => 'Foo',
     1333                ) );
     1334                $t2 = $this->factory->term->create( array(
     1335                        'taxonomy' => 'category',
     1336                        'slug' => 'bar',
     1337                        'name' => 'Bar',
     1338                ) );
     1339                $p1 = $this->factory->post->create();
     1340                $p2 = $this->factory->post->create();
     1341                $p3 = $this->factory->post->create();
     1342
     1343                wp_set_object_terms( $p1, $t1, 'category' );
     1344                wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
     1345
     1346                $q = new WP_Query( array(
     1347                        'fields' => 'ids',
     1348                        'update_post_meta_cache' => false,
     1349                        'update_post_term_cache' => false,
     1350                        'tax_query' => array(
     1351                                'relation' => 'OR',
     1352                                array(
     1353                                        'taxonomy' => 'category',
     1354                                        'terms' => array( 'foo' ),
     1355                                        'field' => 'slug',
     1356                                ),
     1357                                array(
     1358                                        'taxonomy' => 'category',
     1359                                        'terms' => array( 'bar' ),
     1360                                        'field' => 'slug',
     1361                                ),
     1362                        ),
     1363                ) );
     1364
     1365                $this->assertEquals( array( $p1, $p2 ), $q->posts );
     1366        }
     1367
     1368        /**
     1369         * @group taxonomy
     1370         */
     1371        public function test_tax_query_multiple_queries_different_taxonomies() {
     1372                $t1 = $this->factory->term->create( array(
     1373                        'taxonomy' => 'post_tag',
     1374                        'slug' => 'foo',
     1375                        'name' => 'Foo',
     1376                ) );
     1377                $t2 = $this->factory->term->create( array(
     1378                        'taxonomy' => 'category',
     1379                        'slug' => 'bar',
     1380                        'name' => 'Bar',
     1381                ) );
     1382                $p1 = $this->factory->post->create();
     1383                $p2 = $this->factory->post->create();
     1384                $p3 = $this->factory->post->create();
     1385
     1386                wp_set_object_terms( $p1, $t1, 'post_tag' );
     1387                wp_set_object_terms( $p2, $t2, 'category' );
     1388
     1389                $q = new WP_Query( array(
     1390                        'fields' => 'ids',
     1391                        'update_post_meta_cache' => false,
     1392                        'update_post_term_cache' => false,
     1393                        'tax_query' => array(
     1394                                'relation' => 'OR',
     1395                                array(
     1396                                        'taxonomy' => 'post_tag',
     1397                                        'terms' => array( 'foo' ),
     1398                                        'field' => 'slug',
     1399                                ),
     1400                                array(
     1401                                        'taxonomy' => 'category',
     1402                                        'terms' => array( 'bar' ),
     1403                                        'field' => 'slug',
     1404                                ),
     1405                        ),
     1406                ) );
     1407
     1408                $this->assertEquals( array( $p1, $p2 ), $q->posts );
     1409        }
     1410
     1411        /**
     1412         * @ticket 20604
     1413         * @group taxonomy
     1414         */
     1415        public function test_tax_query_relation_or_both_clauses_empty_terms() {
     1416                // An empty tax query should return an empty array, not all posts.
     1417
     1418                $this->factory->post->create_many( 10 );
     1419
     1420                $query = new WP_Query( array(
     1421                        'fields' => 'ids',
     1422                        'update_post_term_cache' => false,
     1423                        'update_post_meta_cache' => false,
     1424                        'tax_query' => array(
     1425                                'relation' => 'OR',
     1426                                array(
     1427                                        'taxonomy' => 'post_tag',
     1428                                        'field' => 'id',
     1429                                        'terms' => false,
     1430                                        'operator' => 'IN'
     1431                                ),
     1432                                array(
     1433                                        'taxonomy' => 'category',
     1434                                        'field' => 'id',
     1435                                        'terms' => false,
     1436                                        'operator' => 'IN'
     1437                                ),
     1438                        )
     1439                ) );
     1440
     1441                $posts = $query->get_posts();
     1442                $this->assertEquals( 0 , count( $posts ) );
     1443        }
     1444
     1445        /**
     1446         * @ticket 20604
     1447         * @group taxonomy
     1448         */
     1449        public function test_tax_query_relation_or_one_clause_empty_terms() {
     1450                // An empty tax query should return an empty array, not all posts.
     1451
     1452                $this->factory->post->create_many( 10 );
     1453
     1454                $query = new WP_Query( array(
     1455                        'fields' => 'ids',
     1456                        'update_post_term_cache' => false,
     1457                        'update_post_meta_cache' => false,
     1458                        'tax_query' => array(
     1459                                'relation' => 'OR',
     1460                                array(
     1461                                        'taxonomy' => 'post_tag',
     1462                                        'field' => 'id',
     1463                                        'terms' => array( 'foo' ),
     1464                                        'operator' => 'IN'
     1465                                ),
     1466                                array(
     1467                                        'taxonomy' => 'category',
     1468                                        'field' => 'id',
     1469                                        'terms' => false,
     1470                                        'operator' => 'IN'
     1471                                ),
     1472                        )
     1473                ) );
     1474
     1475                $posts = $query->get_posts();
     1476                $this->assertEquals( 0 , count( $posts ) );
     1477        }
     1478
     1479        /**
     1480         * @group taxonomy
     1481         */
     1482        public function test_tax_query_include_children() {
    9871483                $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Australia' ) );
    9881484                $cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Sydney', 'parent' => $cat_a ) );
     
    9961492
    9971493                $posts = get_posts( array(
     1494                        'fields' => 'ids',
     1495                        'update_post_meta_cache' => false,
     1496                        'update_post_term_cache' => false,
    9981497                        'tax_query' => array(
    9991498                                array(
     
    10081507
    10091508                $posts = get_posts( array(
     1509                        'fields' => 'ids',
     1510                        'update_post_meta_cache' => false,
     1511                        'update_post_term_cache' => false,
    10101512                        'tax_query' => array(
    10111513                                array(
     
    10211523
    10221524                $posts = get_posts( array(
     1525                        'fields' => 'ids',
     1526                        'update_post_meta_cache' => false,
     1527                        'update_post_term_cache' => false,
    10231528                        'tax_query' => array(
    10241529                                array(
     
    10331538
    10341539                $posts = get_posts( array(
     1540                        'fields' => 'ids',
     1541                        'update_post_meta_cache' => false,
     1542                        'update_post_term_cache' => false,
    10351543                        'tax_query' => array(
    10361544                                array(
     
    10461554
    10471555                $posts = get_posts( array(
     1556                        'fields' => 'ids',
     1557                        'update_post_meta_cache' => false,
     1558                        'update_post_term_cache' => false,
    10481559                        'tax_query' => array(
    10491560                                array(
     
    10581569
    10591570                $posts = get_posts( array(
     1571                        'fields' => 'ids',
     1572                        'update_post_meta_cache' => false,
     1573                        'update_post_term_cache' => false,
    10601574                        'tax_query' => array(
    10611575                                array(
     
    10691583
    10701584                $this->assertEquals( 1 , count( $posts ) );
     1585        }
     1586
     1587        /**
     1588         * @group taxonomy
     1589         */
     1590        function test_category__and_var() {
     1591                $q = new WP_Query();
     1592
     1593                $term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
     1594                $term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
     1595                $post_id = $this->factory->post->create();
     1596
     1597                wp_set_post_categories( $post_id, $term_id );
     1598
     1599                $posts = $q->query( array( 'category__and' => array( $term_id ) ) );
     1600
     1601                $this->assertEmpty( $q->get( 'category__and' ) );
     1602                $this->assertCount( 0, $q->get( 'category__and' ) );
     1603                $this->assertNotEmpty( $q->get( 'category__in' ) );
     1604                $this->assertCount( 1, $q->get( 'category__in' ) );
     1605
     1606                $this->assertNotEmpty( $posts );
     1607                $this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
     1608
     1609                $posts2 = $q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
     1610                $this->assertNotEmpty( $q->get( 'category__and' ) );
     1611                $this->assertCount( 2, $q->get( 'category__and' ) );
     1612                $this->assertEmpty( $q->get( 'category__in' ) );
     1613                $this->assertCount( 0, $q->get( 'category__in' ) );
     1614
     1615                $this->assertEmpty( $posts2 );
     1616        }
     1617
     1618        /**
     1619         * @group taxonomy
     1620         */
     1621        public function test_tax_query_taxonomy_with_attachments() {
     1622                $q = new WP_Query();
     1623
     1624                register_taxonomy_for_object_type( 'post_tag', 'attachment:image' );
     1625                $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) );
     1626                $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
     1627                        'post_mime_type' => 'image/jpeg',
     1628                        'post_type' => 'attachment'
     1629                ) );
     1630                wp_set_object_terms( $image_id, $tag_id, 'post_tag' );
     1631
     1632                $posts = $q->query( array(
     1633                        'fields' => 'ids',
     1634                        'update_post_meta_cache' => false,
     1635                        'update_post_term_cache' => false,
     1636                        'post_type' => 'attachment',
     1637                        'post_status' => 'inherit',
     1638                        'tax_query' => array(
     1639                                array(
     1640                                        'taxonomy' => 'post_tag',
     1641                                        'field' => 'term_id',
     1642                                        'terms' => array( $tag_id )
     1643                                )
     1644                        )
     1645                ) );
     1646
     1647                $this->assertEquals( array( $image_id ), $posts );
     1648        }
     1649
     1650        /**
     1651         * @ticket 27193
     1652         * @group taxonomy
     1653         */
     1654        function test_cat_or_tag() {
     1655                $category1 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'alpha' ) );
     1656                $category2 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'beta' ) );
     1657
     1658                $tag1 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'gamma' ) );
     1659                $tag2 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'delta' ) );
     1660
     1661                $post_id1 = $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $category1 ) ) );
     1662                $terms1 = get_the_category( $post_id1 );
     1663                $this->assertEquals( array( get_category( $category1 ) ), $terms1 );
     1664
     1665                $post_id2 = $this->factory->post->create( array( 'post_title' => 'beta', 'post_category' => array( $category2 ) ) );
     1666                $terms2 = get_the_category( $post_id2 );
     1667                $this->assertEquals( array( get_category( $category2 ) ), $terms2 );
     1668
     1669                $post_id3 = $this->factory->post->create( array( 'post_title' => 'gamma', 'post_tag' => array( $tag1 ) ) );
     1670                $post_id4 = $this->factory->post->create( array( 'post_title' => 'delta', 'post_tag' => array( $tag2 ) ) );
     1671
     1672                $query = new WP_Query( array(
     1673                        'fields' => 'ids',
     1674                        'update_post_meta_cache' => false,
     1675                        'update_post_term_cache' => false,
     1676                        'tax_query' => array(
     1677                                //'relation' => 'OR',
     1678                                array(
     1679                                        'taxonomy' => 'category',
     1680                                        'field' => 'term_id',
     1681                                        'terms' => array( $category1, $category2 )
     1682                                )
     1683                        )
     1684                ) );
     1685                $ids = $query->get_posts();
     1686                $this->assertEquals( array( $post_id1, $post_id2 ), $ids );
     1687        }
     1688
     1689        /**
     1690         * @group taxonomy
     1691         */
     1692        function test_tax_query_no_taxonomy() {
     1693                $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) );
     1694                $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) );
     1695
     1696                $response1 = new WP_Query( array(
     1697                        'tax_query' => array(
     1698                                array( 'terms' => array( $cat_id ) )
     1699                        )
     1700                ) );
     1701                $this->assertEmpty( $response1->posts );
     1702
     1703                $response2 = new WP_Query( array(
     1704                        'fields' => 'ids',
     1705                        'update_post_meta_cache' => false,
     1706                        'update_post_term_cache' => false,
     1707                        'tax_query' => array(
     1708                                array(
     1709                                        'taxonomy' => 'category',
     1710                                        'terms' => array( $cat_id )
     1711                                )
     1712                        )
     1713                ) );
     1714                $this->assertNotEmpty( $response2->posts );
     1715
     1716                $term = get_category( $cat_id );
     1717                $response3 = new WP_Query( array(
     1718                        'fields' => 'ids',
     1719                        'update_post_meta_cache' => false,
     1720                        'update_post_term_cache' => false,
     1721                        'tax_query' => array(
     1722                                array(
     1723                                        'field' => 'term_taxonomy_id',
     1724                                        'terms' => array( $term->term_taxonomy_id )
     1725                                )
     1726                        )
     1727                ) );
     1728                $this->assertNotEmpty( $response3->posts );
     1729        }
     1730
     1731        /**
     1732         * @group taxonomy
     1733         */
     1734        function test_term_taxonomy_id_field_no_taxonomy() {
     1735                $q = new WP_Query();
     1736
     1737                $posts = $this->factory->post->create_many( 5 );
     1738
     1739                $cats = $tags = array();
     1740
     1741                // need term_taxonomy_ids in addition to term_ids, so no factory
     1742                for ( $i = 0; $i < 5; $i++ ) {
     1743                        $cats[$i] = wp_insert_term( 'category-' . $i , 'category' );
     1744                        $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' );
     1745
     1746                        // post 0 gets all terms
     1747                        wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true );
     1748                        wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true );
     1749                }
     1750
     1751                wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
     1752                wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' );
     1753
     1754                wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' );
     1755                wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
     1756
     1757                wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
     1758                wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
     1759
     1760                $results1 = $q->query( array(
     1761                        'fields' => 'ids',
     1762                        'update_post_meta_cache' => false,
     1763                        'update_post_term_cache' => false,
     1764                        'orderby' => 'ID',
     1765                        'order' => 'ASC',
     1766                        'tax_query' => array(
     1767                                'relation' => 'OR',
     1768                                array(
     1769                                        'field' => 'term_taxonomy_id',
     1770                                        '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'] ),
     1771                                        'operator' => 'AND',
     1772                                        'include_children' => false,
     1773                                ),
     1774                                array(
     1775                                        'field' => 'term_taxonomy_id',
     1776                                        'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
     1777                                        'operator' => 'AND',
     1778                                        'include_children' => false,
     1779                                )
     1780                        )
     1781                ) );
     1782
     1783                $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' );
     1784
     1785                $results2 = $q->query( array(
     1786                        'fields' => 'ids',
     1787                        'update_post_meta_cache' => false,
     1788                        'update_post_term_cache' => false,
     1789                        'orderby' => 'ID',
     1790                        'order' => 'ASC',
     1791                        'tax_query' => array(
     1792                                'relation' => 'AND',
     1793                                array(
     1794                                        'field' => 'term_taxonomy_id',
     1795                                        'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ),
     1796                                        'operator' => 'IN',
     1797                                        'include_children' => false,
     1798                                ),
     1799                                array(
     1800                                        'field' => 'term_taxonomy_id',
     1801                                        'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
     1802                                        'operator' => 'IN',
     1803                                        'include_children' => false,
     1804                                )
     1805                        )
     1806                ) );
     1807
     1808                $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' );
     1809        }
     1810
     1811        /**
     1812         * @ticket 28099
     1813         * @group taxonomy
     1814         */
     1815        function test_empty_category__in() {
     1816                $cat_id = $this->factory->category->create();
     1817                $post_id = $this->factory->post->create();
     1818                wp_set_post_categories( $post_id, $cat_id );
     1819
     1820                $q1 = get_posts( array( 'category__in' => array( $cat_id ) ) );
     1821                $this->assertNotEmpty( $q1 );
     1822                $q2 = get_posts( array( 'category__in' => array() ) );
     1823                $this->assertNotEmpty( $q2 );
     1824
     1825                $tag = wp_insert_term( 'woo', 'post_tag' );
     1826                $tag_id = $tag['term_id'];
     1827                $slug = get_tag( $tag_id )->slug;
     1828                wp_set_post_tags( $post_id, $slug );
     1829
     1830                $q3 = get_posts( array( 'tag__in' => array( $tag_id ) ) );
     1831                $this->assertNotEmpty( $q3 );
     1832                $q4 = get_posts( array( 'tag__in' => array() ) );
     1833                $this->assertNotEmpty( $q4 );
     1834
     1835                $q5 = get_posts( array( 'tag_slug__in' => array( $slug ) ) );
     1836                $this->assertNotEmpty( $q5 );
     1837                $q6 = get_posts( array( 'tag_slug__in' => array() ) );
     1838                $this->assertNotEmpty( $q6 );
     1839        }
     1840
     1841        /**
     1842         * @group taxonomy
     1843         * @ticket 29718
     1844         */
     1845        public function test_populate_taxonomy_query_var_from_tax_query() {
     1846                register_taxonomy( 'foo', 'post' );
     1847                $t = $this->factory->term->create( array(
     1848                        'taxonomy' => 'foo',
     1849                ) );
     1850                $c = $this->factory->term->create( array(
     1851                        'taxonomy' => 'category',
     1852                ) );
     1853
     1854                $q = new WP_Query( array(
     1855                        'tax_query' => array(
     1856                                // Empty terms mean that this one should be skipped
     1857                                array(
     1858                                        'taxonomy' => 'bar',
     1859                                        'terms' => array(),
     1860                                ),
     1861
     1862                                // Category and post tags should be skipped
     1863                                array(
     1864                                        'taxonomy' => 'category',
     1865                                        'terms' => array( $c ),
     1866                                ),
     1867
     1868                                array(
     1869                                        'taxonomy' => 'foo',
     1870                                        'terms' => array( $t ),
     1871                                ),
     1872                        ),
     1873                ) );
     1874
     1875                $this->assertSame( 'foo', $q->get( 'taxonomy' ) );
     1876
     1877                _unregister_taxonomy( 'foo' );
     1878        }
     1879
     1880        /**
     1881         * @group taxonomy
     1882         */
     1883        public function test_populate_taxonomy_query_var_from_tax_query_taxonomy_already_set() {
     1884                register_taxonomy( 'foo', 'post' );
     1885                register_taxonomy( 'foo1', 'post' );
     1886                $t = $this->factory->term->create( array(
     1887                        'taxonomy' => 'foo',
     1888                ) );
     1889
     1890                $q = new WP_Query( array(
     1891                        'taxonomy' => 'bar',
     1892                        'tax_query' => array(
     1893                                array(
     1894                                        'taxonomy' => 'foo',
     1895                                        'terms' => array( $t ),
     1896                                ),
     1897                        ),
     1898                ) );
     1899
     1900                $this->assertSame( 'bar', $q->get( 'taxonomy' ) );
     1901
     1902                _unregister_taxonomy( 'foo' );
     1903                _unregister_taxonomy( 'foo1' );
     1904        }
     1905
     1906        /**
     1907         * @group taxonomy
     1908         */
     1909        public function test_populate_term_query_var_from_tax_query() {
     1910                register_taxonomy( 'foo', 'post' );
     1911                $t = $this->factory->term->create( array(
     1912                        'taxonomy' => 'foo',
     1913                        'slug' => 'bar',
     1914                ) );
     1915
     1916                $q = new WP_Query( array(
     1917                        'tax_query' => array(
     1918                                array(
     1919                                        'taxonomy' => 'foo',
     1920                                        'terms' => array( 'bar' ),
     1921                                        'field' => 'slug',
     1922                                ),
     1923                        ),
     1924                ) );
     1925
     1926                $this->assertSame( 'bar', $q->get( 'term' ) );
     1927
     1928                _unregister_taxonomy( 'foo' );
     1929        }
     1930
     1931        /**
     1932         * @group taxonomy
     1933         */
     1934        public function test_populate_term_id_query_var_from_tax_query() {
     1935                register_taxonomy( 'foo', 'post' );
     1936                $t = $this->factory->term->create( array(
     1937                        'taxonomy' => 'foo',
     1938                        'slug' => 'bar',
     1939                ) );
     1940
     1941                $q = new WP_Query( array(
     1942                        'tax_query' => array(
     1943                                array(
     1944                                        'taxonomy' => 'foo',
     1945                                        'terms' => array( $t ),
     1946                                        'field' => 'term_id',
     1947                                ),
     1948                        ),
     1949                ) );
     1950
     1951                $this->assertEquals( $t, $q->get( 'term_id' ) );
     1952
     1953                _unregister_taxonomy( 'foo' );
     1954        }
     1955
     1956        /**
     1957         * @group taxonomy
     1958         * @ticket 29718
     1959         */
     1960        public function test_populate_cat_category_name_query_var_from_tax_query() {
     1961                register_taxonomy( 'foo', 'post' );
     1962                $t = $this->factory->term->create( array(
     1963                        'taxonomy' => 'foo',
     1964                ) );
     1965                $c = $this->factory->term->create( array(
     1966                        'taxonomy' => 'foo',
     1967                        'slug' => 'bar',
     1968                ) );
     1969
     1970                $q = new WP_Query( array(
     1971                        'tax_query' => array(
     1972                                // Non-category should be skipped
     1973                                array(
     1974                                        'taxonomy' => 'foo',
     1975                                        'terms' => array( $t ),
     1976                                ),
     1977
     1978                                // Empty terms mean that this one should be skipped
     1979                                array(
     1980                                        'taxonomy' => 'category',
     1981                                        'terms' => array(),
     1982                                ),
     1983
     1984                                // Category and post tags should be skipped
     1985                                array(
     1986                                        'taxonomy' => 'category',
     1987                                        'terms' => array( $c ),
     1988                                ),
     1989                        ),
     1990                ) );
     1991
     1992                $this->assertEquals( $c, $q->get( 'cat' ) );
     1993                $this->assertEquals( 'bar', $q->get( 'category_name' ) );
     1994
     1995                _unregister_taxonomy( 'foo' );
     1996        }
     1997
     1998        /**
     1999         * @group taxonomy
     2000         * @ticket 29718
     2001         */
     2002        public function test_populate_tag_id_query_var_from_tax_query() {
     2003                register_taxonomy( 'foo', 'post' );
     2004                $t = $this->factory->term->create( array(
     2005                        'taxonomy' => 'foo',
     2006                ) );
     2007                $tag = $this->factory->term->create( array(
     2008                        'taxonomy' => 'post_tag',
     2009                        'slug' => 'bar',
     2010                ) );
     2011
     2012                $q = new WP_Query( array(
     2013                        'tax_query' => array(
     2014                                // Non-tag should be skipped
     2015                                array(
     2016                                        'taxonomy' => 'foo',
     2017                                        'terms' => array( $t ),
     2018                                ),
     2019
     2020                                // Empty terms mean that this one should be skipped
     2021                                array(
     2022                                        'taxonomy' => 'post_tag',
     2023                                        'terms' => array(),
     2024                                ),
     2025
     2026                                // Category and post tags should be skipped
     2027                                array(
     2028                                        'taxonomy' => 'post_tag',
     2029                                        'terms' => array( $tag ),
     2030                                ),
     2031                        ),
     2032                ) );
     2033
     2034                $this->assertEquals( $tag, $q->get( 'tag_id' ) );
     2035
     2036                _unregister_taxonomy( 'foo' );
    10712037        }
    10722038
Note: See TracChangeset for help on using the changeset viewer.