Ticket #29560: 29560.patch
File 29560.patch, 36.8 KB (added by , 10 years ago) |
---|
-
tests/phpunit/tests/meta/query.php
diff --git tests/phpunit/tests/meta/query.php tests/phpunit/tests/meta/query.php index fdd7693..6da583f 100644
2 2 /** 3 3 * Test WP_Meta_Query, in wp-includes/meta.php 4 4 * 5 * See tests/post/query.php for tests that involve post queries. 6 * 5 7 * @group meta 6 8 */ 7 9 class Tests_Meta_Query extends WP_UnitTestCase { 8 10 9 function test_default_relation() { 11 public function test_empty_meta_query_param() { 12 $query = new WP_Meta_Query(); 13 $this->assertSame( null, $query->relation ); 14 } 15 16 public function test_default_relation() { 10 17 $query = new WP_Meta_Query( array( array( 'key' => 'abc' ) ) ); 11 18 12 19 $this->assertEquals( 'AND', $query->relation ); 13 20 } 14 21 15 function test_set_relation() {22 public function test_set_relation() { 16 23 17 24 $query = new WP_Meta_Query( array( array( 'key' => 'abc' ), 'relation' => 'AND' ) ); 18 25 … … class Tests_Meta_Query extends WP_UnitTestCase { 24 31 } 25 32 26 33 /** 34 * Non-arrays should not be added to the queries array 35 */ 36 public function test_invalid_query_clauses() { 37 $query = new WP_Meta_Query( array( 38 'foo', // empty string 39 5, // int 40 false, // bool 41 array(), 42 ) ); 43 44 $this->assertSame( array( array() ), $query->queries ); 45 } 46 47 /** 27 48 * Test all key only meta queries use the same INNER JOIN when using relation=OR 28 49 * 29 50 * @ticket 19729 30 51 */ 31 function test_single_inner_join_for_keys_only() {52 public function test_single_inner_join_for_keys_only() { 32 53 33 54 global $wpdb; 34 55 … … class Tests_Meta_Query extends WP_UnitTestCase { 57 78 } 58 79 59 80 /** 81 * WP_Query-style query must be at index 0 for order_by=meta_value to work 82 */ 83 public function test_parse_query_vars_simple_query_index_0() { 84 $qv = array( 85 'meta_query' => array( 86 array( 87 'key' => 'foo1', 88 'compare' => 'baz1', 89 'value' => 'bar1', 90 ), 91 ), 92 'meta_key' => 'foo', 93 'meta_compare' => 'bar', 94 'meta_value' => 'baz', 95 ); 96 97 $query = new WP_Meta_Query(); 98 $query->parse_query_vars( $qv ); 99 100 $expected0 = array( 101 'key' => 'foo', 102 'compare' => 'bar', 103 'value' => 'baz', 104 ); 105 $this->assertEquals( $expected0, $query->queries[0] ); 106 107 $expected1 = array( 108 'key' => 'foo1', 109 'compare' => 'baz1', 110 'value' => 'bar1', 111 ); 112 $this->assertEquals( $expected1, $query->queries[1] ); 113 } 114 115 /** 116 * When no meta_value is provided, no 'value' should be set in the parsed queries 117 */ 118 public function test_parse_query_vars_with_no_meta_value() { 119 $qv = array( 120 'meta_key' => 'foo', 121 'meta_type' => 'bar', 122 'meta_compare' => '=', 123 ); 124 125 $query = new WP_Meta_Query(); 126 $query->parse_query_vars( $qv ); 127 128 $this->assertTrue( ! isset( $query->queries[0]['value'] ) ); 129 } 130 131 /** 132 * WP_Query sets meta_value to '' by default. It should be removed by parse_query_vars() 133 */ 134 public function test_parse_query_vars_with_default_meta_compare() { 135 $qv = array( 136 'meta_key' => 'foo', 137 'meta_type' => 'bar', 138 'meta_compare' => '=', 139 'meta_value' => '', 140 ); 141 142 $query = new WP_Meta_Query(); 143 $query->parse_query_vars( $qv ); 144 145 $this->assertTrue( ! isset( $query->queries[0]['value'] ) ); 146 } 147 148 /** 60 149 * Test the conversion between "WP_Query" style meta args (meta_value=x&meta_key=y) 61 150 * to a meta query array 62 151 */ 63 function test_parse_query_vars() {152 public function test_parse_query_vars() { 64 153 65 154 $query = new WP_Meta_Query(); 66 155 … … class Tests_Meta_Query extends WP_UnitTestCase { 81 170 } 82 171 83 172 /** 173 * @ticket 23033 174 */ 175 public function test_get_cast_for_type() { 176 $query = new WP_Meta_Query(); 177 $this->assertEquals( 'BINARY', $query->get_cast_for_type( 'BINARY' ) ); 178 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'CHAR' ) ); 179 $this->assertEquals( 'DATE', $query->get_cast_for_type( 'DATE' ) ); 180 $this->assertEquals( 'DATETIME', $query->get_cast_for_type( 'DATETIME' ) ); 181 $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'SIGNED' ) ); 182 $this->assertEquals( 'UNSIGNED', $query->get_cast_for_type( 'UNSIGNED' ) ); 183 $this->assertEquals( 'TIME', $query->get_cast_for_type( 'TIME' ) ); 184 $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'NUMERIC' ) ); 185 $this->assertEquals( 'NUMERIC(10)', $query->get_cast_for_type( 'NUMERIC(10)' ) ); 186 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10)' ) ); 187 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10 )' ) ); 188 $this->assertEquals( 'NUMERIC(10, 5)', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) ); 189 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) ); 190 $this->assertEquals( 'NUMERIC(10,5)', $query->get_cast_for_type( 'NUMERIC(10,5)' ) ); 191 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10, 5 )' ) ); 192 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5 )' ) ); 193 $this->assertEquals( 'DECIMAL', $query->get_cast_for_type( 'DECIMAL' ) ); 194 $this->assertEquals( 'DECIMAL(10)', $query->get_cast_for_type( 'DECIMAL(10)' ) ); 195 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10 )' ) ); 196 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10)' ) ); 197 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10 )' ) ); 198 $this->assertEquals( 'DECIMAL(10, 5)', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) ); 199 $this->assertEquals( 'DECIMAL(10,5)', $query->get_cast_for_type( 'DECIMAL(10,5)' ) ); 200 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) ); 201 202 $this->assertEquals( 'CHAR', $query->get_cast_for_type() ); 203 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) ); 204 } 205 206 /** 207 * Invalid $type will fail to get a table from _get_meta_table() 208 */ 209 public function test_get_sql_invalid_type() { 210 $query = new WP_Meta_Query(); 211 $this->assertFalse( $query->get_sql( 'foo', 'foo', 'foo' ) ); 212 } 213 214 /** 84 215 * @ticket 22096 85 216 */ 86 function test_empty_value_sql() {217 public function test_empty_value_sql() { 87 218 global $wpdb; 88 219 89 220 $query = new WP_Meta_Query(); … … class Tests_Meta_Query extends WP_UnitTestCase { 109 240 /** 110 241 * @ticket 22967 111 242 */ 112 function test_null_value_sql() {243 public function test_null_value_sql() { 113 244 global $wpdb; 114 245 115 246 $query = new WP_Meta_Query( array( … … class Tests_Meta_Query extends WP_UnitTestCase { 121 252 } 122 253 123 254 /** 124 * @ticket 23033 255 * "key only queries" are queries that don't need to match a value, so 256 * they can be grouped together into a single clause without JOINs 125 257 */ 126 function test_get_cast_for_type() { 127 $query = new WP_Meta_Query(); 128 $this->assertEquals( 'BINARY', $query->get_cast_for_type( 'BINARY' ) ); 129 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'CHAR' ) ); 130 $this->assertEquals( 'DATE', $query->get_cast_for_type( 'DATE' ) ); 131 $this->assertEquals( 'DATETIME', $query->get_cast_for_type( 'DATETIME' ) ); 132 $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'SIGNED' ) ); 133 $this->assertEquals( 'UNSIGNED', $query->get_cast_for_type( 'UNSIGNED' ) ); 134 $this->assertEquals( 'TIME', $query->get_cast_for_type( 'TIME' ) ); 135 $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'NUMERIC' ) ); 136 $this->assertEquals( 'NUMERIC(10)', $query->get_cast_for_type( 'NUMERIC(10)' ) ); 137 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10)' ) ); 138 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10 )' ) ); 139 $this->assertEquals( 'NUMERIC(10, 5)', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) ); 140 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) ); 141 $this->assertEquals( 'NUMERIC(10,5)', $query->get_cast_for_type( 'NUMERIC(10,5)' ) ); 142 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10, 5 )' ) ); 143 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5 )' ) ); 144 $this->assertEquals( 'DECIMAL', $query->get_cast_for_type( 'DECIMAL' ) ); 145 $this->assertEquals( 'DECIMAL(10)', $query->get_cast_for_type( 'DECIMAL(10)' ) ); 146 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10 )' ) ); 147 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10)' ) ); 148 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10 )' ) ); 149 $this->assertEquals( 'DECIMAL(10, 5)', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) ); 150 $this->assertEquals( 'DECIMAL(10,5)', $query->get_cast_for_type( 'DECIMAL(10,5)' ) ); 151 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) ); 258 public function test_get_sql_key_only_queries() { 259 global $wpdb; 152 260 153 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) ); 261 $query1 = new WP_Meta_Query( array( 262 'relation' => 'OR', 263 264 // Empty 'compare' 265 array( 266 'key' => 'foo', 267 ), 268 269 // Non-empty 'compare' 270 array( 271 'key' => 'bar', 272 'compare' => '<', 273 ), 274 275 // NOT EXISTS 276 array( 277 'key' => 'baz', 278 'compare' => 'NOT EXISTS', 279 ), 280 281 // Has a value 282 array( 283 'key' => 'barry', 284 'value' => 'foo', 285 ), 286 287 // Has no key 288 array( 289 'value' => 'bar', 290 ), 291 ) ); 292 293 $sql = $query1->get_sql( 'post', $wpdb->posts, 'ID', $this ); 294 295 // 'foo' and 'bar' should be queried against the non-aliased table 296 $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'foo'" ) ); 297 $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'bar'" ) ); 298 299 // NOT EXISTS compare queries are not key-only so should not be non-aliased 300 $this->assertSame( 0, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'baz'" ) ); 301 302 // When a value exists, it's not a key-only query 303 $this->assertSame( 0, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'barry'" ) ); 304 305 // 'AND' queries don't have key-only queries 306 $query2 = new WP_Meta_Query( array( 307 'relation' => 'AND', 308 309 // Empty 'compare' 310 array( 311 'key' => 'foo', 312 ), 313 314 // Non-empty 'compare' 315 array( 316 'key' => 'bar', 317 'compare' => '<', 318 ), 319 ) ); 320 321 $sql = $query2->get_sql( 'post', $wpdb->posts, 'ID', $this ); 322 323 // Only 'foo' should be queried against the non-aliased table 324 $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'foo'" ) ); 325 $this->assertSame( 0, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'bar'" ) ); 326 } 327 328 /** 329 * Key-only and regular queries should have the key trimmed 330 */ 331 public function test_get_sql_trim_key() { 332 global $wpdb; 333 334 $query = new WP_Meta_Query( array( 335 array( 336 'key' => ' foo ', 337 ), 338 array( 339 'key' => ' bar ', 340 'value' => 'value', 341 ), 342 ) ); 343 344 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 345 346 $this->assertSame( 1, substr_count( $sql['where'], "meta_key = 'foo'" ) ); 347 $this->assertSame( 1, substr_count( $sql['where'], "meta_key = 'bar'" ) ); 348 } 349 350 public function test_convert_null_value_to_empty_string() { 351 global $wpdb; 352 353 $query = new WP_Meta_Query( array( 354 array( 355 'key' => 'foo', 356 'value' => null, 357 ), 358 ) ); 359 360 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 361 362 $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = ''" ) ); 363 } 364 365 public function test_get_sql_convert_lowercase_compare_to_uppercase() { 366 global $wpdb; 367 368 $query = new WP_Meta_Query( array( 369 array( 370 'key' => 'foo', 371 'value' => 'bar', 372 'compare' => 'regExp', 373 ), 374 ) ); 375 376 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 377 378 $this->assertSame( 1, substr_count( $sql['where'], "REGEXP" ) ); 379 } 380 381 public function test_get_sql_empty_meta_compare_with_array_value() { 382 global $wpdb; 383 384 $query = new WP_Meta_Query( array( 385 array( 386 'key' => 'foo', 387 'value' => array( 'bar', 'baz' ), 388 ), 389 ) ); 390 391 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 392 393 $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) IN" ) ); 394 } 395 396 public function test_get_sql_empty_meta_compare_with_non_array_value() { 397 global $wpdb; 398 399 $query = new WP_Meta_Query( array( 400 array( 401 'key' => 'foo', 402 'value' => 'bar', 403 ), 404 ) ); 405 406 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 407 408 $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) =" ) ); 409 } 410 411 public function test_get_sql_invalid_meta_compare() { 412 global $wpdb; 413 414 $query = new WP_Meta_Query( array( 415 array( 416 'key' => 'foo', 417 'value' => 'bar', 418 'compare' => 'INVALID COMPARE', 419 ), 420 ) ); 421 422 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 423 424 $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) =" ) ); 425 } 426 427 /** 428 * This is the clause that ensures that empty arrays are not valid queries 429 */ 430 public function test_get_sql_null_value_and_empty_key_should_not_have_table_join() { 431 global $wpdb; 432 433 $query = new WP_Meta_Query( array( 434 array( 435 'key' => 'foo', 436 'value' => 'bar', 437 ), 438 array(), 439 ) ); 440 441 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 442 443 // There should be no JOIN against an aliased table 444 $this->assertSame( 0, substr_count( $sql['join'], "AS mt" ) ); 445 } 446 447 public function test_get_sql_compare_array_comma_separated_values() { 448 global $wpdb; 449 450 // Single 451 $query = new WP_Meta_Query( array( 452 array( 453 'key' => 'foo', 454 'compare' => 'IN', 455 'value' => 'bar', 456 ), 457 ) ); 458 459 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 460 461 $this->assertSame( 1, substr_count( $sql['where'], "('bar')" ) ); 462 463 // Multiple, no spaces 464 $query = new WP_Meta_Query( array( 465 array( 466 'key' => 'foo', 467 'compare' => 'IN', 468 'value' => 'bar,baz', 469 ), 470 ) ); 471 472 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 473 474 $this->assertSame( 1, substr_count( $sql['where'], "('bar','baz')" ) ); 475 476 // Multiple, spaces 477 $query = new WP_Meta_Query( array( 478 array( 479 'key' => 'foo', 480 'compare' => 'IN', 481 'value' => 'bar,baz, barry', 482 ), 483 ) ); 484 485 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 486 487 $this->assertSame( 1, substr_count( $sql['where'], "('bar','baz','barry')" ) ); 488 } 489 490 public function test_get_sql_compare_array() { 491 global $wpdb; 492 493 $query = new WP_Meta_Query( array( 494 array( 495 'key' => 'foo', 496 'compare' => 'IN', 497 'value' => array( 'bar', 'baz' ), 498 ), 499 ) ); 500 501 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 502 503 $this->assertSame( 1, substr_count( $sql['where'], "('bar','baz')" ) ); 504 } 505 506 /** 507 * Non-array values are trimmed. @todo Why? 508 */ 509 public function test_get_sql_trim_string_value() { 510 global $wpdb; 511 512 $query = new WP_Meta_Query( array( 513 array( 514 'key' => 'foo', 515 'value' => ' bar ', 516 ), 517 ) ); 518 519 $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 520 521 $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = 'bar'" ) ); 154 522 } 155 523 156 function test_not_exists() {524 public function test_not_exists() { 157 525 global $wpdb; 158 526 159 527 $query = new WP_Meta_Query( array( … … class Tests_Meta_Query extends WP_UnitTestCase { 174 542 $this->assertContains( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] ); 175 543 } 176 544 177 function test_empty_compare() {545 public function test_empty_compare() { 178 546 global $wpdb; 179 547 180 548 $query = new WP_Meta_Query( array( -
tests/phpunit/tests/post/query.php
diff --git tests/phpunit/tests/post/query.php tests/phpunit/tests/post/query.php index 004e710..0c565cc 100644
1 1 <?php 2 2 3 /**4 * @group meta5 */6 3 class Tests_Post_Query extends WP_UnitTestCase { 7 4 function setUp() { 8 5 parent::setUp(); 9 6 } 10 7 11 function test_meta_key_or_query() { 8 /** 9 * @group meta 10 */ 11 public function test_meta_query_no_key() { 12 $p1 = $this->factory->post->create(); 13 $p2 = $this->factory->post->create(); 14 $p3 = $this->factory->post->create(); 15 16 add_post_meta( $p1, 'foo', 'bar' ); 17 add_post_meta( $p2, 'oof', 'bar' ); 18 add_post_meta( $p3, 'oof', 'baz' ); 19 20 $query = new WP_Query( array( 21 'update_post_meta_cache' => false, 22 'update_post_term_cache' => false, 23 'fields' => 'ids', 24 'meta_query' => array( 25 array( 26 'value' => 'bar', 27 ), 28 ), 29 ) ); 30 31 $expected = array( $p1, $p2 ); 32 $this->assertEqualSets( $expected, $query->posts ); 33 } 34 35 /** 36 * @group meta 37 */ 38 public function test_meta_query_no_value() { 39 $p1 = $this->factory->post->create(); 40 $p2 = $this->factory->post->create(); 41 $p3 = $this->factory->post->create(); 42 43 add_post_meta( $p1, 'foo', 'bar' ); 44 add_post_meta( $p2, 'oof', 'bar' ); 45 add_post_meta( $p3, 'oof', 'baz' ); 46 47 $query = new WP_Query( array( 48 'update_post_meta_cache' => false, 49 'update_post_term_cache' => false, 50 'fields' => 'ids', 51 'meta_query' => array( 52 array( 53 'key' => 'oof', 54 ), 55 ), 56 ) ); 57 58 $expected = array( $p2, $p3 ); 59 $this->assertEqualSets( $expected, $query->posts ); 60 } 61 62 /** 63 * @group meta 64 */ 65 public function test_meta_query_single_query_compare_default() { 66 $p1 = $this->factory->post->create(); 67 $p2 = $this->factory->post->create(); 68 69 add_post_meta( $p1, 'foo', 'bar' ); 70 71 $query = new WP_Query( array( 72 'update_post_meta_cache' => false, 73 'update_post_term_cache' => false, 74 'fields' => 'ids', 75 'meta_query' => array( 76 array( 77 'key' => 'foo', 78 'value' => 'bar', 79 ), 80 ), 81 ) ); 82 83 $expected = array( $p1 ); 84 $this->assertEqualSets( $expected, $query->posts ); 85 } 86 87 /** 88 * @group meta 89 */ 90 public function test_meta_query_single_query_compare_equals() { 91 $p1 = $this->factory->post->create(); 92 $p2 = $this->factory->post->create(); 93 94 add_post_meta( $p1, 'foo', 'bar' ); 95 96 $query = new WP_Query( array( 97 'update_post_meta_cache' => false, 98 'update_post_term_cache' => false, 99 'fields' => 'ids', 100 'meta_query' => array( 101 array( 102 'key' => 'foo', 103 'value' => 'bar', 104 'compare' => '=', 105 ), 106 ), 107 ) ); 108 109 $expected = array( $p1 ); 110 $this->assertEqualSets( $expected, $query->posts ); 111 } 112 113 /** 114 * @group meta 115 */ 116 public function test_meta_query_single_query_compare_not_equals() { 117 $p1 = $this->factory->post->create(); 118 $p2 = $this->factory->post->create(); 119 $p3 = $this->factory->post->create(); 120 121 add_post_meta( $p1, 'foo', 'bar' ); 122 add_post_meta( $p2, 'foo', 'baz' ); 123 124 $query = new WP_Query( array( 125 'update_post_meta_cache' => false, 126 'update_post_term_cache' => false, 127 'fields' => 'ids', 128 'meta_query' => array( 129 array( 130 'key' => 'foo', 131 'value' => 'bar', 132 'compare' => '!=', 133 ), 134 ), 135 ) ); 136 137 $expected = array( $p2 ); 138 $this->assertEqualSets( $expected, $query->posts ); 139 } 140 141 /** 142 * @group meta 143 */ 144 public function test_meta_query_single_query_compare_arithmetic_comparisons() { 145 $p1 = $this->factory->post->create(); 146 $p2 = $this->factory->post->create(); 147 $p3 = $this->factory->post->create(); 148 149 add_post_meta( $p1, 'foo', '1' ); 150 add_post_meta( $p2, 'foo', '2' ); 151 add_post_meta( $p3, 'foo', '3' ); 152 153 // < 154 $query = new WP_Query( array( 155 'update_post_meta_cache' => false, 156 'update_post_term_cache' => false, 157 'fields' => 'ids', 158 'meta_query' => array( 159 array( 160 'key' => 'foo', 161 'value' => 2, 162 'compare' => '<', 163 ), 164 ), 165 ) ); 166 167 $expected = array( $p1 ); 168 $this->assertEqualSets( $expected, $query->posts ); 169 170 // <= 171 $query = new WP_Query( array( 172 'update_post_meta_cache' => false, 173 'update_post_term_cache' => false, 174 'fields' => 'ids', 175 'meta_query' => array( 176 array( 177 'key' => 'foo', 178 'value' => 2, 179 'compare' => '<=', 180 ), 181 ), 182 ) ); 183 184 $expected = array( $p1, $p2 ); 185 $this->assertEqualSets( $expected, $query->posts ); 186 187 // >= 188 $query = new WP_Query( array( 189 'update_post_meta_cache' => false, 190 'update_post_term_cache' => false, 191 'fields' => 'ids', 192 'meta_query' => array( 193 array( 194 'key' => 'foo', 195 'value' => 2, 196 'compare' => '>=', 197 ), 198 ), 199 ) ); 200 201 $expected = array( $p2, $p3 ); 202 $this->assertEqualSets( $expected, $query->posts ); 203 204 // > 205 $query = new WP_Query( array( 206 'update_post_meta_cache' => false, 207 'update_post_term_cache' => false, 208 'fields' => 'ids', 209 'meta_query' => array( 210 array( 211 'key' => 'foo', 212 'value' => 2, 213 'compare' => '>', 214 ), 215 ), 216 ) ); 217 218 $expected = array( $p3 ); 219 $this->assertEqualSets( $expected, $query->posts ); 220 } 221 222 /** 223 * @group meta 224 */ 225 public function test_meta_query_single_query_compare_like() { 226 $p1 = $this->factory->post->create(); 227 $p2 = $this->factory->post->create(); 228 229 add_post_meta( $p1, 'foo', 'bar' ); 230 231 $query = new WP_Query( array( 232 'update_post_meta_cache' => false, 233 'update_post_term_cache' => false, 234 'fields' => 'ids', 235 'meta_query' => array( 236 array( 237 'key' => 'foo', 238 'value' => 'ba', 239 'compare' => 'LIKE', 240 ), 241 ), 242 ) ); 243 244 $expected = array( $p1 ); 245 $this->assertEqualSets( $expected, $query->posts ); 246 } 247 248 /** 249 * @group meta 250 */ 251 public function test_meta_query_single_query_compare_not_like() { 252 $p1 = $this->factory->post->create(); 253 $p2 = $this->factory->post->create(); 254 $p3 = $this->factory->post->create(); 255 256 add_post_meta( $p1, 'foo', 'bar' ); 257 add_post_meta( $p2, 'foo', 'rab' ); 258 259 $query = new WP_Query( array( 260 'update_post_meta_cache' => false, 261 'update_post_term_cache' => false, 262 'fields' => 'ids', 263 'meta_query' => array( 264 array( 265 'key' => 'foo', 266 'value' => 'ba', 267 'compare' => 'NOT LIKE', 268 ), 269 ), 270 ) ); 271 272 $expected = array( $p2 ); 273 $this->assertEqualSets( $expected, $query->posts ); 274 } 275 276 /** 277 * @group meta 278 */ 279 public function test_meta_query_single_query_compare_between_not_between() { 280 $p1 = $this->factory->post->create(); 281 $p2 = $this->factory->post->create(); 282 $p3 = $this->factory->post->create(); 283 284 add_post_meta( $p1, 'foo', '1' ); 285 add_post_meta( $p2, 'foo', '10' ); 286 add_post_meta( $p3, 'foo', '100' ); 287 288 $query = new WP_Query( array( 289 'update_post_meta_cache' => false, 290 'update_post_term_cache' => false, 291 'fields' => 'ids', 292 'meta_query' => array( 293 array( 294 'key' => 'foo', 295 'value' => array( 9, 12 ), 296 'compare' => 'BETWEEN', 297 'type' => 'NUMERIC', 298 ), 299 ), 300 ) ); 301 302 $expected = array( $p2 ); 303 $this->assertEqualSets( $expected, $query->posts ); 304 305 $query = new WP_Query( array( 306 'update_post_meta_cache' => false, 307 'update_post_term_cache' => false, 308 'fields' => 'ids', 309 'meta_query' => array( 310 array( 311 'key' => 'foo', 312 'value' => array( 9, 12 ), 313 'compare' => 'NOT BETWEEN', 314 'type' => 'NUMERIC', 315 ), 316 ), 317 ) ); 318 319 $expected = array( $p1, $p3 ); 320 $this->assertEqualSets( $expected, $query->posts ); 321 } 322 323 /** 324 * @group meta 325 */ 326 public function test_meta_query_single_query_compare_regexp_rlike() { 327 $p1 = $this->factory->post->create(); 328 $p2 = $this->factory->post->create(); 329 330 add_post_meta( $p1, 'foo', 'bar' ); 331 add_post_meta( $p2, 'foo', 'baz' ); 332 333 $query = new WP_Query( array( 334 'update_post_meta_cache' => false, 335 'update_post_term_cache' => false, 336 'fields' => 'ids', 337 'meta_query' => array( 338 array( 339 'key' => 'foo', 340 'value' => 'z$', 341 'compare' => 'REGEXP', 342 ), 343 ), 344 ) ); 345 346 $expected = array( $p2 ); 347 $this->assertEqualSets( $expected, $query->posts ); 348 349 // RLIKE is a synonym for REGEXP 350 $query = new WP_Query( array( 351 'update_post_meta_cache' => false, 352 'update_post_term_cache' => false, 353 'fields' => 'ids', 354 'meta_query' => array( 355 array( 356 'key' => 'foo', 357 'value' => 'z$', 358 'compare' => 'RLIKE', 359 ), 360 ), 361 ) ); 362 363 $expected = array( $p2 ); 364 $this->assertEqualSets( $expected, $query->posts ); 365 } 366 367 /** 368 * @group meta 369 */ 370 public function test_meta_query_single_query_compare_not_regexp() { 371 $p1 = $this->factory->post->create(); 372 $p2 = $this->factory->post->create(); 373 374 add_post_meta( $p1, 'foo', 'bar' ); 375 add_post_meta( $p2, 'foo', 'baz' ); 376 377 $query = new WP_Query( array( 378 'update_post_meta_cache' => false, 379 'update_post_term_cache' => false, 380 'fields' => 'ids', 381 'meta_query' => array( 382 array( 383 'key' => 'foo', 384 'value' => 'z$', 385 'compare' => 'NOT REGEXP', 386 ), 387 ), 388 ) ); 389 390 $expected = array( $p1 ); 391 $this->assertEqualSets( $expected, $query->posts ); 392 } 393 394 /** 395 * @group meta 396 */ 397 public function test_meta_query_relation_default() { 398 $p1 = $this->factory->post->create(); 399 $p2 = $this->factory->post->create(); 400 $p3 = $this->factory->post->create(); 401 402 add_post_meta( $p1, 'foo', 'foo value 1' ); 403 add_post_meta( $p1, 'bar', 'bar value 1' ); 404 add_post_meta( $p2, 'foo', 'foo value 1' ); 405 add_post_meta( $p2, 'bar', 'bar value 2' ); 406 407 $query = new WP_Query( array( 408 'update_post_meta_cache' => false, 409 'update_post_term_cache' => false, 410 'fields' => 'ids', 411 'meta_query' => array( 412 array( 413 'key' => 'foo', 414 'value' => 'foo value 1', 415 ), 416 array( 417 'key' => 'bar', 418 'value' => 'bar value 1', 419 ), 420 ), 421 ) ); 422 423 $expected = array( $p1 ); 424 $this->assertEquals( $expected, $query->posts ); 425 } 426 427 /** 428 * @group meta 429 */ 430 public function test_meta_query_relation_or() { 12 431 $post_id = $this->factory->post->create(); 13 432 add_post_meta( $post_id, 'foo', rand_str() ); 14 433 add_post_meta( $post_id, 'foo', rand_str() ); … … class Tests_Post_Query extends WP_UnitTestCase { 24 443 add_post_meta( $post_id6, 'bar', 'val1' ); 25 444 26 445 $query = new WP_Query( array( 446 'update_post_meta_cache' => false, 447 'update_post_term_cache' => false, 448 'fields' => 'ids', 27 449 'meta_query' => array( 28 450 array( 29 451 'key' => 'foo' … … class Tests_Post_Query extends WP_UnitTestCase { 42 464 ), 43 465 ) ); 44 466 45 $posts = $query->get_posts(); 46 $this->assertEquals( 4, count( $posts ) ); 47 foreach ( $posts as $post ) { 48 $this->assertInstanceOf( 'WP_Post', $post ); 49 $this->assertEquals( 'raw', $post->filter ); 50 } 51 52 $post_ids = wp_list_pluck( $posts, 'ID' ); 53 $this->assertEqualSets( array( $post_id, $post_id2, $post_id3, $post_id4 ), $post_ids ); 467 $expected = array( $post_id, $post_id2, $post_id3, $post_id4 ); 468 $this->assertEqualSets( $expected, $query->posts ); 54 469 } 55 470 56 function test_meta_key_and_query() { 471 /** 472 * @group meta 473 */ 474 public function test_meta_query_relation_and() { 57 475 $post_id = $this->factory->post->create(); 58 476 add_post_meta( $post_id, 'foo', rand_str() ); 59 477 add_post_meta( $post_id, 'foo', rand_str() ); … … class Tests_Post_Query extends WP_UnitTestCase { 92 510 ), 93 511 'relation' => 'AND', 94 512 ), 513 'update_post_meta_cache' => false, 514 'update_post_term_cache' => false, 515 'fields' => 'ids', 95 516 ) ); 96 517 97 $posts = $query->get_posts(); 98 $this->assertEquals( 1, count( $posts ) ); 99 foreach ( $posts as $post ) { 100 $this->assertInstanceOf( 'WP_Post', $post ); 101 $this->assertEquals( 'raw', $post->filter ); 102 } 103 104 $post_ids = wp_list_pluck( $posts, 'ID' ); 105 $this->assertEquals( array( $post_id7 ), $post_ids ); 518 $expected = array( $post_id7 ); 519 $this->assertEqualSets( $expected, $query->posts ); 106 520 107 521 $query = new WP_Query( array( 108 522 'meta_query' => array( … … class Tests_Post_Query extends WP_UnitTestCase { 114 528 ), 115 529 'relation' => 'AND', 116 530 ), 531 'update_post_meta_cache' => false, 532 'update_post_term_cache' => false, 533 'fields' => 'ids', 117 534 ) ); 118 535 119 $posts = $query->get_posts(); 120 $this->assertEquals( 3, count( $posts ) ); 121 foreach ( $posts as $post ) { 122 $this->assertInstanceOf( 'WP_Post', $post ); 123 $this->assertEquals( 'raw', $post->filter ); 124 } 125 126 $post_ids = wp_list_pluck( $posts, 'ID' ); 127 $this->assertEqualSets( array( $post_id2, $post_id6, $post_id7 ), $post_ids ); 536 $expected = array( $post_id2, $post_id6, $post_id7 ); 537 $this->assertEqualSets( $expected, $query->posts ); 128 538 } 129 539 130 540 /** 131 541 * @ticket 18158 542 * @group meta 132 543 */ 133 function test_meta_key_not_exists() {544 public function test_meta_query_compare_not_exists() { 134 545 $post_id = $this->factory->post->create(); 135 546 add_post_meta( $post_id, 'foo', rand_str() ); 136 547 $post_id2 = $this->factory->post->create(); … … class Tests_Post_Query extends WP_UnitTestCase { 144 555 145 556 $query = new WP_Query( array( 146 557 'meta_query' => array( 147 array(148 'key' => 'foo',149 'compare' => 'NOT EXISTS',150 ),558 array( 559 'key' => 'foo', 560 'compare' => 'NOT EXISTS', 561 ), 151 562 ), 563 'update_post_meta_cache' => false, 564 'update_post_term_cache' => false, 565 'fields' => 'ids', 152 566 ) ); 153 567 154 $posts = $query->get_posts(); 155 $this->assertEquals( 3, count( $posts ) ); 156 foreach ( $posts as $post ) { 157 $this->assertInstanceOf( 'WP_Post', $post ); 158 $this->assertEquals( 'raw', $post->filter ); 159 } 568 $expected = array( $post_id2, $post_id3, $post_id4 ); 569 $this->assertEqualSets( $expected, $query->posts ); 160 570 161 571 $query = new WP_Query( array( 162 572 'meta_query' => array( 163 array(164 'key' => 'foo',165 'compare' => 'NOT EXISTS',166 ),167 573 array( 168 'key' => 'bar', 169 'compare' => 'NOT EXISTS', 170 ), 574 'key' => 'foo', 575 'compare' => 'NOT EXISTS', 576 ), 577 array( 578 'key' => 'bar', 579 'compare' => 'NOT EXISTS', 580 ), 171 581 ), 582 'update_post_meta_cache' => false, 583 'update_post_term_cache' => false, 584 'fields' => 'ids', 172 585 ) ); 173 586 174 $posts = $query->get_posts(); 175 $this->assertEquals( 1, count( $posts ) ); 176 foreach ( $posts as $post ) { 177 $this->assertInstanceOf( 'WP_Post', $post ); 178 $this->assertEquals( 'raw', $post->filter ); 179 } 587 $expected = array( $post_id4 ); 588 $this->assertEquals( $expected, $query->posts ); 180 589 181 590 $query = new WP_Query( array( 182 591 'meta_query' => array( 183 array(184 'key' => 'foo',185 'compare' => 'NOT EXISTS',186 ),187 592 array( 188 'key' => 'bar',189 'compare' => 'NOT EXISTS',190 ),593 'key' => 'foo', 594 'compare' => 'NOT EXISTS', 595 ), 191 596 array( 192 'key' => 'baz', 193 'compare' => 'NOT EXISTS', 597 'key' => 'bar', 598 'compare' => 'NOT EXISTS', 599 ), 600 array( 601 'key' => 'baz', 602 'compare' => 'NOT EXISTS', 603 ), 194 604 ), 195 ) 605 'update_post_meta_cache' => false, 606 'update_post_term_cache' => false, 607 'fields' => 'ids', 196 608 ) ); 197 609 198 $posts = $query->get_posts(); 199 $this->assertEquals( 0, count( $posts ) ); 610 $this->assertEquals( 0, count( $query->posts ) ); 200 611 } 201 612 202 613 /** 203 614 * @ticket 23033 615 * @group meta 204 616 */ 205 function test_meta_query_decimal_results() {617 public function test_meta_query_decimal_results() { 206 618 $post_1 = $this->factory->post->create(); 207 619 $post_2 = $this->factory->post->create(); 208 620 $post_3 = $this->factory->post->create(); … … class Tests_Post_Query extends WP_UnitTestCase { 215 627 216 628 $query = new WP_Query( array( 217 629 'meta_query' => array( 218 array( 219 'key' => 'decimal_value', 220 'value' => '.300', 221 'compare' => '=', 222 'type' => 'DECIMAL(10,2)' 223 ) 224 ), 630 array( 631 'key' => 'decimal_value', 632 'value' => '.300', 633 'compare' => '=', 634 'type' => 'DECIMAL(10,2)' 635 ) 636 ), 637 'update_post_meta_cache' => false, 638 'update_post_term_cache' => false, 639 'fields' => 'ids', 225 640 ) ); 226 $this->assertEqualSets( array( $post_3 ), wp_list_pluck( $query->posts, 'ID' ));641 $this->assertEqualSets( array( $post_3 ), $query->posts ); 227 642 228 643 $query = new WP_Query( array( 229 644 'meta_query' => array( 230 array( 231 'key' => 'decimal_value', 232 'value' => '0.35', 233 'compare' => '>', 234 'type' => 'DECIMAL(10,2)' 235 ) 236 ), 645 array( 646 'key' => 'decimal_value', 647 'value' => '0.35', 648 'compare' => '>', 649 'type' => 'DECIMAL(10,2)' 650 ) 651 ), 652 'update_post_meta_cache' => false, 653 'update_post_term_cache' => false, 654 'fields' => 'ids', 237 655 ) ); 238 $this->assertEqualSets( array( $post_4 ), wp_list_pluck( $query->posts, 'ID' ));656 $this->assertEqualSets( array( $post_4 ), $query->posts ); 239 657 240 658 $query = new WP_Query( array( 241 659 'meta_query' => array( 242 array( 243 'key' => 'decimal_value', 244 'value' => '0.3', 245 'compare' => '>=', 246 'type' => 'DECIMAL(10,2)' 247 ) 248 ), 660 array( 661 'key' => 'decimal_value', 662 'value' => '0.3', 663 'compare' => '>=', 664 'type' => 'DECIMAL(10,2)' 665 ) 666 ), 667 'update_post_meta_cache' => false, 668 'update_post_term_cache' => false, 669 'fields' => 'ids', 249 670 ) ); 250 $this->assertEqualSets( array( $post_3, $post_4 ), wp_list_pluck( $query->posts, 'ID' ));671 $this->assertEqualSets( array( $post_3, $post_4 ), $query->posts ); 251 672 252 673 $query = new WP_Query( array( 253 674 'meta_query' => array( 254 array( 255 'key' => 'decimal_value', 256 'value' => '0', 257 'compare' => '<', 258 'type' => 'DECIMAL(10,2)' 259 ) 260 ), 675 array( 676 'key' => 'decimal_value', 677 'value' => '0', 678 'compare' => '<', 679 'type' => 'DECIMAL(10,2)' 680 ) 681 ), 682 'update_post_meta_cache' => false, 683 'update_post_term_cache' => false, 684 'fields' => 'ids', 261 685 ) ); 262 $this->assertEqualSets( array( $post_1 ), wp_list_pluck( $query->posts, 'ID' ));686 $this->assertEqualSets( array( $post_1 ), $query->posts, 'ID' ); 263 687 264 688 $query = new WP_Query( array( 265 689 'meta_query' => array( 266 array( 267 'key' => 'decimal_value', 268 'value' => '0.3', 269 'compare' => '<=', 270 'type' => 'DECIMAL(10,2)' 271 ) 272 ), 273 690 array( 691 'key' => 'decimal_value', 692 'value' => '0.3', 693 'compare' => '<=', 694 'type' => 'DECIMAL(10,2)' 695 ) 696 ), 697 'update_post_meta_cache' => false, 698 'update_post_term_cache' => false, 699 'fields' => 'ids', 274 700 ) ); 275 $this->assertEqualSets( array( $post_1, $post_2, $post_3 ), wp_list_pluck( $query->posts, 'ID' ));701 $this->assertEqualSets( array( $post_1, $post_2, $post_3 ), $query->posts ); 276 702 277 703 $query = new WP_Query( array( 278 704 'meta_query' => array( 279 array( 280 'key' => 'decimal_value', 281 'value' => array( 0.23409845, .31 ), 282 'compare' => 'BETWEEN', 283 'type' => 'DECIMAL(10, 10)' 284 ) 285 ), 705 array( 706 'key' => 'decimal_value', 707 'value' => array( 0.23409845, .31 ), 708 'compare' => 'BETWEEN', 709 'type' => 'DECIMAL(10, 10)' 710 ) 711 ), 712 'update_post_meta_cache' => false, 713 'update_post_term_cache' => false, 714 'fields' => 'ids', 286 715 ) ); 287 $this->assertEqualSets( array( $post_3 ), wp_list_pluck( $query->posts, 'ID' ));716 $this->assertEqualSets( array( $post_3 ), $query->posts ); 288 717 289 718 $query = new WP_Query( array( 290 719 'meta_query' => array( 291 array( 292 'key' => 'decimal_value', 293 'value' => array( 0.23409845, .31 ), 294 'compare' => 'NOT BETWEEN', 295 'type' => 'DECIMAL(10,10)' 296 ) 297 ), 720 array( 721 'key' => 'decimal_value', 722 'value' => array( 0.23409845, .31 ), 723 'compare' => 'NOT BETWEEN', 724 'type' => 'DECIMAL(10,10)' 725 ) 726 ), 727 'update_post_meta_cache' => false, 728 'update_post_term_cache' => false, 729 'fields' => 'ids', 298 730 ) ); 299 $this->assertEqualSets( array( $post_1, $post_2, $post_4 ), wp_list_pluck( $query->posts, 'ID' ));731 $this->assertEqualSets( array( $post_1, $post_2, $post_4 ), $query->posts ); 300 732 301 733 $query = new WP_Query( array( 302 734 'meta_query' => array( 303 array( 304 'key' => 'decimal_value', 305 'value' => '.3', 306 'compare' => 'LIKE', 307 'type' => 'DECIMAL(10,2)' 308 ) 309 ), 735 array( 736 'key' => 'decimal_value', 737 'value' => '.3', 738 'compare' => 'LIKE', 739 'type' => 'DECIMAL(10,2)' 740 ) 741 ), 742 'update_post_meta_cache' => false, 743 'update_post_term_cache' => false, 744 'fields' => 'ids', 310 745 ) ); 311 $this->assertEqualSets( array( $post_1, $post_3 ), wp_list_pluck( $query->posts, 'ID' ));746 $this->assertEqualSets( array( $post_1, $post_3 ), $query->posts ); 312 747 313 748 $query = new WP_Query( array( 314 749 'meta_query' => array( 315 array( 316 'key' => 'decimal_value', 317 'value' => '.3', 318 'compare' => 'NOT LIKE', 319 'type' => 'DECIMAL(10,2)' 320 ) 321 ), 750 array( 751 'key' => 'decimal_value', 752 'value' => '.3', 753 'compare' => 'NOT LIKE', 754 'type' => 'DECIMAL(10,2)' 755 ) 756 ), 757 'update_post_meta_cache' => false, 758 'update_post_term_cache' => false, 759 'fields' => 'ids', 322 760 ) ); 323 $this->assertEqualSets( array( $post_2, $post_4 ), wp_list_pluck( $query->posts, 'ID' ));761 $this->assertEqualSets( array( $post_2, $post_4 ), $query->posts ); 324 762 325 763 $query = new WP_Query( array( 326 764 'orderby' => 'meta_value', 327 765 'order' => 'DESC', 328 766 'meta_key' => 'decimal_value', 329 'meta_type' => 'DECIMAL(10, 2)' 767 'meta_type' => 'DECIMAL(10, 2)', 768 'update_post_meta_cache' => false, 769 'update_post_term_cache' => false, 770 'fields' => 'ids', 330 771 ) ); 331 $this->assertEqualSets( array( $post_4, $post_3, $post_2, $post_1 ), wp_list_pluck( $query->posts, 'ID' ) ); 332 772 $this->assertEqualSets( array( $post_4, $post_3, $post_2, $post_1 ), $query->posts ); 333 773 } 334 774 335 775 /** … … class Tests_Post_Query extends WP_UnitTestCase { 831 1271 $q3->request 832 1272 ); 833 1273 } 834 } 835 No newline at end of file 1274 }