Make WordPress Core

Changeset 29805


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

Location:
trunk/tests/phpunit/tests
Files:
2 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
  • trunk/tests/phpunit/tests/term/query.php

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