Changeset 26056
- Timestamp:
- 11/08/2013 11:13:41 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/post/query.php
r26055 r26056 5 5 */ 6 6 class Tests_Post_Query extends WP_UnitTestCase { 7 8 parent::setUp();9 10 11 12 $post_id = $this->factory->post->create();13 add_post_meta( $post_id, 'foo', rand_str() );14 add_post_meta( $post_id, 'foo', rand_str() );15 $post_id2 = $this->factory->post->create();16 add_post_meta( $post_id2, 'bar', 'val2' );17 $post_id3 = $this->factory->post->create();18 add_post_meta( $post_id3, 'baz', rand_str() );19 $post_id4 = $this->factory->post->create();20 add_post_meta( $post_id4, 'froo', rand_str() );21 $post_id5 = $this->factory->post->create();22 add_post_meta( $post_id5, 'tango', 'val2' );23 $post_id6 = $this->factory->post->create();24 add_post_meta( $post_id6, 'bar', 'val1' );25 26 $query = new WP_Query( array(27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 ) );44 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 );54 55 56 57 $post_id = $this->factory->post->create();58 add_post_meta( $post_id, 'foo', rand_str() );59 add_post_meta( $post_id, 'foo', rand_str() );60 $post_id2 = $this->factory->post->create();61 add_post_meta( $post_id2, 'bar', 'val2' );62 add_post_meta( $post_id2, 'foo', rand_str() );63 $post_id3 = $this->factory->post->create();64 add_post_meta( $post_id3, 'baz', rand_str() );65 $post_id4 = $this->factory->post->create();66 add_post_meta( $post_id4, 'froo', rand_str() );67 $post_id5 = $this->factory->post->create();68 add_post_meta( $post_id5, 'tango', 'val2' );69 $post_id6 = $this->factory->post->create();70 add_post_meta( $post_id6, 'bar', 'val1' );71 add_post_meta( $post_id6, 'foo', rand_str() );72 $post_id7 = $this->factory->post->create();73 add_post_meta( $post_id7, 'foo', rand_str() );74 add_post_meta( $post_id7, 'froo', rand_str() );75 add_post_meta( $post_id7, 'baz', rand_str() );76 add_post_meta( $post_id7, 'bar', 'val2' );77 78 $query = new WP_Query( array(79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 ) );96 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 );106 107 $query = new WP_Query( array(108 109 110 111 112 113 114 115 116 117 ) );118 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 );128 129 130 131 132 133 134 $post_id = $this->factory->post->create();135 add_post_meta( $post_id, 'foo', rand_str() );136 $post_id2 = $this->factory->post->create();137 add_post_meta( $post_id2, 'bar', rand_str() );138 $post_id3 = $this->factory->post->create();139 add_post_meta( $post_id3, 'bar', rand_str() );140 $post_id4 = $this->factory->post->create();141 add_post_meta( $post_id4, 'baz', rand_str() );142 $post_id5 = $this->factory->post->create();143 add_post_meta( $post_id5, 'foo', rand_str() );144 145 $query = new WP_Query( array(146 147 array(148 149 150 ),151 152 ) );153 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 }160 161 $query = new WP_Query( array(162 163 array(164 165 166 ),167 168 169 170 ),171 172 ) );173 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 }180 181 $query = new WP_Query( array(182 183 array(184 185 186 ),187 188 189 190 ),191 192 193 194 ),195 196 ) );197 198 $posts = $query->get_posts();199 $this->assertEquals( 0, count( $posts ) );200 7 function setUp() { 8 parent::setUp(); 9 } 10 11 function test_meta_key_or_query() { 12 $post_id = $this->factory->post->create(); 13 add_post_meta( $post_id, 'foo', rand_str() ); 14 add_post_meta( $post_id, 'foo', rand_str() ); 15 $post_id2 = $this->factory->post->create(); 16 add_post_meta( $post_id2, 'bar', 'val2' ); 17 $post_id3 = $this->factory->post->create(); 18 add_post_meta( $post_id3, 'baz', rand_str() ); 19 $post_id4 = $this->factory->post->create(); 20 add_post_meta( $post_id4, 'froo', rand_str() ); 21 $post_id5 = $this->factory->post->create(); 22 add_post_meta( $post_id5, 'tango', 'val2' ); 23 $post_id6 = $this->factory->post->create(); 24 add_post_meta( $post_id6, 'bar', 'val1' ); 25 26 $query = new WP_Query( array( 27 'meta_query' => array( 28 array( 29 'key' => 'foo' 30 ), 31 array( 32 'key' => 'bar', 33 'value' => 'val2' 34 ), 35 array( 36 'key' => 'baz' 37 ), 38 array( 39 'key' => 'froo' 40 ), 41 'relation' => 'OR', 42 ), 43 ) ); 44 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 ); 54 } 55 56 function test_meta_key_and_query() { 57 $post_id = $this->factory->post->create(); 58 add_post_meta( $post_id, 'foo', rand_str() ); 59 add_post_meta( $post_id, 'foo', rand_str() ); 60 $post_id2 = $this->factory->post->create(); 61 add_post_meta( $post_id2, 'bar', 'val2' ); 62 add_post_meta( $post_id2, 'foo', rand_str() ); 63 $post_id3 = $this->factory->post->create(); 64 add_post_meta( $post_id3, 'baz', rand_str() ); 65 $post_id4 = $this->factory->post->create(); 66 add_post_meta( $post_id4, 'froo', rand_str() ); 67 $post_id5 = $this->factory->post->create(); 68 add_post_meta( $post_id5, 'tango', 'val2' ); 69 $post_id6 = $this->factory->post->create(); 70 add_post_meta( $post_id6, 'bar', 'val1' ); 71 add_post_meta( $post_id6, 'foo', rand_str() ); 72 $post_id7 = $this->factory->post->create(); 73 add_post_meta( $post_id7, 'foo', rand_str() ); 74 add_post_meta( $post_id7, 'froo', rand_str() ); 75 add_post_meta( $post_id7, 'baz', rand_str() ); 76 add_post_meta( $post_id7, 'bar', 'val2' ); 77 78 $query = new WP_Query( array( 79 'meta_query' => array( 80 array( 81 'key' => 'foo' 82 ), 83 array( 84 'key' => 'bar', 85 'value' => 'val2' 86 ), 87 array( 88 'key' => 'baz' 89 ), 90 array( 91 'key' => 'froo' 92 ), 93 'relation' => 'AND', 94 ), 95 ) ); 96 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 ); 106 107 $query = new WP_Query( array( 108 'meta_query' => array( 109 array( 110 'key' => 'foo' 111 ), 112 array( 113 'key' => 'bar', 114 ), 115 'relation' => 'AND', 116 ), 117 ) ); 118 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 ); 128 } 129 130 /** 131 * @ticket 18158 132 */ 133 function test_meta_key_not_exists() { 134 $post_id = $this->factory->post->create(); 135 add_post_meta( $post_id, 'foo', rand_str() ); 136 $post_id2 = $this->factory->post->create(); 137 add_post_meta( $post_id2, 'bar', rand_str() ); 138 $post_id3 = $this->factory->post->create(); 139 add_post_meta( $post_id3, 'bar', rand_str() ); 140 $post_id4 = $this->factory->post->create(); 141 add_post_meta( $post_id4, 'baz', rand_str() ); 142 $post_id5 = $this->factory->post->create(); 143 add_post_meta( $post_id5, 'foo', rand_str() ); 144 145 $query = new WP_Query( array( 146 'meta_query' => array( 147 array( 148 'key' => 'foo', 149 'compare' => 'NOT EXISTS', 150 ), 151 ), 152 ) ); 153 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 } 160 161 $query = new WP_Query( array( 162 'meta_query' => array( 163 array( 164 'key' => 'foo', 165 'compare' => 'NOT EXISTS', 166 ), 167 array( 168 'key' => 'bar', 169 'compare' => 'NOT EXISTS', 170 ), 171 ), 172 ) ); 173 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 } 180 181 $query = new WP_Query( array( 182 'meta_query' => array( 183 array( 184 'key' => 'foo', 185 'compare' => 'NOT EXISTS', 186 ), 187 array( 188 'key' => 'bar', 189 'compare' => 'NOT EXISTS', 190 ), 191 array( 192 'key' => 'baz', 193 'compare' => 'NOT EXISTS', 194 ), 195 ) 196 ) ); 197 198 $posts = $query->get_posts(); 199 $this->assertEquals( 0, count( $posts ) ); 200 } 201 201 202 202 /** … … 333 333 } 334 334 335 336 337 338 339 // An empty tax query should return an empty array, not all posts.340 341 $this->factory->post->create_many( 10 );342 343 $query = new WP_Query( array(344 345 346 'relation' => 'OR',347 array(348 'taxonomy' => 'post_tag',349 'field' => 'id',350 'terms' => false,351 'operator' => 'IN'352 ),353 array(354 'taxonomy' => 'category',355 'field' => 'id',356 'terms' => false,357 'operator' => 'IN'358 )359 360 ) );361 362 $posts = $query->get_posts();363 $this->assertEquals( 0 , count( $posts ) );364 365 366 367 $post_id = $this->factory->post->create();368 add_post_meta( $post_id, 'time', 500 );369 $post_id2 = $this->factory->post->create();370 add_post_meta( $post_id2, 'time', 1001 );371 $post_id3 = $this->factory->post->create();372 add_post_meta( $post_id3, 'time', 0 );373 $post_id4 = $this->factory->post->create();374 add_post_meta( $post_id4, 'time', 1 );375 $post_id5 = $this->factory->post->create();376 add_post_meta( $post_id5, 'time', 1000 );377 378 $args = array(379 'meta_key' => 'time',380 'meta_value' => array( 1, 1000 ),381 'meta_type' => 'numeric',382 'meta_compare' => 'NOT BETWEEN'383 384 385 $query = new WP_Query( $args );386 $this->assertEquals( 2, count ( $query->posts ) );387 foreach ( $query->posts as $post ) {388 $this->assertInstanceOf( 'WP_Post', $post );389 $this->assertEquals( 'raw', $post->filter );390 }391 $posts = wp_list_pluck( $query->posts, 'ID' );392 $this->assertEqualSets( array( $post_id2, $post_id3 ), $posts );393 394 $args = array(395 'meta_key' => 'time',396 'meta_value' => array( 1, 1000 ),397 'meta_type' => 'numeric',398 'meta_compare' => 'BETWEEN'399 400 401 $query = new WP_Query( $args );402 $this->assertEquals( 3, count ( $query->posts ) );403 foreach ( $query->posts as $post ) {404 $this->assertInstanceOf( 'WP_Post', $post );405 $this->assertEquals( 'raw', $post->filter );406 }407 $posts = wp_list_pluck( $query->posts, 'ID' );408 $this->assertEqualSets( array( $post_id, $post_id4, $post_id5 ), $posts );409 410 411 412 413 414 415 // compare should default to IN when meta_value is an array416 $post_id = $this->factory->post->create();417 add_post_meta( $post_id, 'foo', 'bar' );418 $post_id2 = $this->factory->post->create();419 add_post_meta( $post_id2, 'bar', 'baz' );420 $post_id3 = $this->factory->post->create();421 add_post_meta( $post_id3, 'foo', 'baz' );422 $post_id4 = $this->factory->post->create();423 add_post_meta( $post_id4, 'baz', 'bar' );424 $post_id5 = $this->factory->post->create();425 add_post_meta( $post_id5, 'foo', rand_str() );426 427 $posts = get_posts( array(428 429 430 ) );431 432 $this->assertEquals( 2, count( $posts ) );433 $posts = wp_list_pluck( $posts, 'ID' );434 $this->assertEqualSets( array( $post_id, $post_id3 ), $posts );435 436 $posts = get_posts( array(437 438 439 440 ) );441 442 $this->assertEquals( 2, count( $posts ) );443 foreach ( $posts as $post ) {444 $this->assertInstanceOf( 'WP_Post', $post );445 $this->assertEquals( 'raw', $post->filter );446 }447 $posts = wp_list_pluck( $posts, 'ID' );448 $this->assertEqualSets( array( $post_id, $post_id3 ), $posts );449 450 451 452 453 454 455 $post_id = $this->factory->post->create();456 add_post_meta( $post_id, 'city', 'Lorem' );457 add_post_meta( $post_id, 'address', '123 Lorem St.' );458 $post_id2 = $this->factory->post->create();459 add_post_meta( $post_id2, 'city', 'Lorem' );460 $post_id3 = $this->factory->post->create();461 add_post_meta( $post_id3, 'city', 'Loren' );462 463 $args = array(464 465 array(466 467 468 )469 470 );471 472 $posts = get_posts( $args );473 $this->assertEquals( 2, count( $posts ) );474 foreach ( $posts as $post ) {475 $this->assertInstanceOf( 'WP_Post', $post );476 $this->assertEquals( 'raw', $post->filter );477 }478 $posts = wp_list_pluck( $posts, 'ID' );479 $this->assertEqualSets( array( $post_id, $post_id2 ), $posts );480 481 482 483 484 485 486 $post_id = $this->factory->post->create();487 add_post_meta( $post_id, 'foo', '0' );488 add_post_meta( $post_id, 'bar', 0 );489 $post_id2 = $this->factory->post->create();490 add_post_meta( $post_id2, 'foo', 1 );491 $post_id3 = $this->factory->post->create();492 add_post_meta( $post_id3, 'baz', 0 );493 $post_id4 = $this->factory->post->create();494 add_post_meta( $post_id4, 'baz', 0 );495 $post_id5 = $this->factory->post->create();496 add_post_meta( $post_id5, 'baz', 0 );497 add_post_meta( $post_id5, 'bar', '0' );498 $post_id6 = $this->factory->post->create();499 add_post_meta( $post_id6, 'baz', 0 );500 501 $posts = get_posts( array( 'meta_key' => 'foo', 'meta_value' => '0' ) );502 $this->assertEquals( 1, count ( $posts ) );503 foreach ( $posts as $post ) {504 $this->assertInstanceOf( 'WP_Post', $post );505 $this->assertEquals( 'raw', $post->filter );506 }507 $this->assertEquals( $post_id, $posts[0]->ID );508 509 $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => '0' ) );510 $this->assertEquals( 2, count ( $posts ) );511 foreach ( $posts as $post ) {512 $this->assertInstanceOf( 'WP_Post', $post );513 $this->assertEquals( 'raw', $post->filter );514 }515 $posts = wp_list_pluck( $posts, 'ID' );516 $this->assertEqualSets( array( $post_id, $post_id5 ), $posts );517 518 519 $this->assertEquals( 2, count ( $posts ) );520 foreach ( $posts as $post ) {521 $this->assertInstanceOf( 'WP_Post', $post );522 $this->assertEquals( 'raw', $post->filter );523 }524 $posts = wp_list_pluck( $posts, 'ID' );525 $this->assertEqualSets( array( $post_id, $post_id5 ), $posts );526 527 528 $this->assertEquals( 5, count ( $posts ) );529 foreach ( $posts as $post ) {530 $this->assertInstanceOf( 'WP_Post', $post );531 $this->assertEquals( 'raw', $post->filter );532 }533 $posts = wp_list_pluck( $posts, 'ID' );534 $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );535 536 537 $this->assertEquals( 5, count ( $posts ) );538 foreach ( $posts as $post ) {539 $this->assertInstanceOf( 'WP_Post', $post );540 $this->assertEquals( 'raw', $post->filter );541 }542 $posts = wp_list_pluck( $posts, 'ID' );543 $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );544 335 /** 336 * @ticket 20604 337 */ 338 function test_taxonomy_empty_or() { 339 // An empty tax query should return an empty array, not all posts. 340 341 $this->factory->post->create_many( 10 ); 342 343 $query = new WP_Query( array( 344 'fields' => 'ids', 345 'tax_query' => array( 346 'relation' => 'OR', 347 array( 348 'taxonomy' => 'post_tag', 349 'field' => 'id', 350 'terms' => false, 351 'operator' => 'IN' 352 ), 353 array( 354 'taxonomy' => 'category', 355 'field' => 'id', 356 'terms' => false, 357 'operator' => 'IN' 358 ) 359 ) 360 ) ); 361 362 $posts = $query->get_posts(); 363 $this->assertEquals( 0 , count( $posts ) ); 364 } 365 366 function test_meta_between_not_between() { 367 $post_id = $this->factory->post->create(); 368 add_post_meta( $post_id, 'time', 500 ); 369 $post_id2 = $this->factory->post->create(); 370 add_post_meta( $post_id2, 'time', 1001 ); 371 $post_id3 = $this->factory->post->create(); 372 add_post_meta( $post_id3, 'time', 0 ); 373 $post_id4 = $this->factory->post->create(); 374 add_post_meta( $post_id4, 'time', 1 ); 375 $post_id5 = $this->factory->post->create(); 376 add_post_meta( $post_id5, 'time', 1000 ); 377 378 $args = array( 379 'meta_key' => 'time', 380 'meta_value' => array( 1, 1000 ), 381 'meta_type' => 'numeric', 382 'meta_compare' => 'NOT BETWEEN' 383 ); 384 385 $query = new WP_Query( $args ); 386 $this->assertEquals( 2, count ( $query->posts ) ); 387 foreach ( $query->posts as $post ) { 388 $this->assertInstanceOf( 'WP_Post', $post ); 389 $this->assertEquals( 'raw', $post->filter ); 390 } 391 $posts = wp_list_pluck( $query->posts, 'ID' ); 392 $this->assertEqualSets( array( $post_id2, $post_id3 ), $posts ); 393 394 $args = array( 395 'meta_key' => 'time', 396 'meta_value' => array( 1, 1000 ), 397 'meta_type' => 'numeric', 398 'meta_compare' => 'BETWEEN' 399 ); 400 401 $query = new WP_Query( $args ); 402 $this->assertEquals( 3, count ( $query->posts ) ); 403 foreach ( $query->posts as $post ) { 404 $this->assertInstanceOf( 'WP_Post', $post ); 405 $this->assertEquals( 'raw', $post->filter ); 406 } 407 $posts = wp_list_pluck( $query->posts, 'ID' ); 408 $this->assertEqualSets( array( $post_id, $post_id4, $post_id5 ), $posts ); 409 } 410 411 /** 412 * @ticket 16829 413 */ 414 function test_meta_default_compare() { 415 // compare should default to IN when meta_value is an array 416 $post_id = $this->factory->post->create(); 417 add_post_meta( $post_id, 'foo', 'bar' ); 418 $post_id2 = $this->factory->post->create(); 419 add_post_meta( $post_id2, 'bar', 'baz' ); 420 $post_id3 = $this->factory->post->create(); 421 add_post_meta( $post_id3, 'foo', 'baz' ); 422 $post_id4 = $this->factory->post->create(); 423 add_post_meta( $post_id4, 'baz', 'bar' ); 424 $post_id5 = $this->factory->post->create(); 425 add_post_meta( $post_id5, 'foo', rand_str() ); 426 427 $posts = get_posts( array( 428 'meta_key' => 'foo', 429 'meta_value' => array( 'bar', 'baz' ) 430 ) ); 431 432 $this->assertEquals( 2, count( $posts ) ); 433 $posts = wp_list_pluck( $posts, 'ID' ); 434 $this->assertEqualSets( array( $post_id, $post_id3 ), $posts ); 435 436 $posts = get_posts( array( 437 'meta_key' => 'foo', 438 'meta_value' => array( 'bar', 'baz' ), 439 'meta_compare' => 'IN' 440 ) ); 441 442 $this->assertEquals( 2, count( $posts ) ); 443 foreach ( $posts as $post ) { 444 $this->assertInstanceOf( 'WP_Post', $post ); 445 $this->assertEquals( 'raw', $post->filter ); 446 } 447 $posts = wp_list_pluck( $posts, 'ID' ); 448 $this->assertEqualSets( array( $post_id, $post_id3 ), $posts ); 449 } 450 451 /** 452 * @ticket 17264 453 */ 454 function test_duplicate_posts_when_no_key() { 455 $post_id = $this->factory->post->create(); 456 add_post_meta( $post_id, 'city', 'Lorem' ); 457 add_post_meta( $post_id, 'address', '123 Lorem St.' ); 458 $post_id2 = $this->factory->post->create(); 459 add_post_meta( $post_id2, 'city', 'Lorem' ); 460 $post_id3 = $this->factory->post->create(); 461 add_post_meta( $post_id3, 'city', 'Loren' ); 462 463 $args = array( 464 'meta_query' => array( 465 array( 466 'value' => 'lorem', 467 'compare' => 'LIKE' 468 ) 469 ) 470 ); 471 472 $posts = get_posts( $args ); 473 $this->assertEquals( 2, count( $posts ) ); 474 foreach ( $posts as $post ) { 475 $this->assertInstanceOf( 'WP_Post', $post ); 476 $this->assertEquals( 'raw', $post->filter ); 477 } 478 $posts = wp_list_pluck( $posts, 'ID' ); 479 $this->assertEqualSets( array( $post_id, $post_id2 ), $posts ); 480 } 481 482 /** 483 * @ticket 15292 484 */ 485 function test_empty_meta_value() { 486 $post_id = $this->factory->post->create(); 487 add_post_meta( $post_id, 'foo', '0' ); 488 add_post_meta( $post_id, 'bar', 0 ); 489 $post_id2 = $this->factory->post->create(); 490 add_post_meta( $post_id2, 'foo', 1 ); 491 $post_id3 = $this->factory->post->create(); 492 add_post_meta( $post_id3, 'baz', 0 ); 493 $post_id4 = $this->factory->post->create(); 494 add_post_meta( $post_id4, 'baz', 0 ); 495 $post_id5 = $this->factory->post->create(); 496 add_post_meta( $post_id5, 'baz', 0 ); 497 add_post_meta( $post_id5, 'bar', '0' ); 498 $post_id6 = $this->factory->post->create(); 499 add_post_meta( $post_id6, 'baz', 0 ); 500 501 $posts = get_posts( array( 'meta_key' => 'foo', 'meta_value' => '0' ) ); 502 $this->assertEquals( 1, count ( $posts ) ); 503 foreach ( $posts as $post ) { 504 $this->assertInstanceOf( 'WP_Post', $post ); 505 $this->assertEquals( 'raw', $post->filter ); 506 } 507 $this->assertEquals( $post_id, $posts[0]->ID ); 508 509 $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => '0' ) ); 510 $this->assertEquals( 2, count ( $posts ) ); 511 foreach ( $posts as $post ) { 512 $this->assertInstanceOf( 'WP_Post', $post ); 513 $this->assertEquals( 'raw', $post->filter ); 514 } 515 $posts = wp_list_pluck( $posts, 'ID' ); 516 $this->assertEqualSets( array( $post_id, $post_id5 ), $posts ); 517 518 $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => 0 ) ); 519 $this->assertEquals( 2, count ( $posts ) ); 520 foreach ( $posts as $post ) { 521 $this->assertInstanceOf( 'WP_Post', $post ); 522 $this->assertEquals( 'raw', $post->filter ); 523 } 524 $posts = wp_list_pluck( $posts, 'ID' ); 525 $this->assertEqualSets( array( $post_id, $post_id5 ), $posts ); 526 527 $posts = get_posts( array( 'meta_value' => 0 ) ); 528 $this->assertEquals( 5, count ( $posts ) ); 529 foreach ( $posts as $post ) { 530 $this->assertInstanceOf( 'WP_Post', $post ); 531 $this->assertEquals( 'raw', $post->filter ); 532 } 533 $posts = wp_list_pluck( $posts, 'ID' ); 534 $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts ); 535 536 $posts = get_posts( array( 'meta_value' => '0' ) ); 537 $this->assertEquals( 5, count ( $posts ) ); 538 foreach ( $posts as $post ) { 539 $this->assertInstanceOf( 'WP_Post', $post ); 540 $this->assertEquals( 'raw', $post->filter ); 541 } 542 $posts = wp_list_pluck( $posts, 'ID' ); 543 $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts ); 544 } 545 545 546 546 function test_taxonomy_include_children() { … … 556 556 557 557 $posts = get_posts( array( 558 558 'tax_query' => array( 559 559 array( 560 560 'taxonomy' => 'category', … … 562 562 'terms' => array( $cat_a ), 563 563 ) 564 564 ) 565 565 ) ); 566 566 … … 568 568 569 569 $posts = get_posts( array( 570 570 'tax_query' => array( 571 571 array( 572 572 'taxonomy' => 'category', … … 575 575 'include_children' => false 576 576 ) 577 577 ) 578 578 ) ); 579 579 … … 581 581 582 582 $posts = get_posts( array( 583 583 'tax_query' => array( 584 584 array( 585 585 'taxonomy' => 'category', … … 587 587 'terms' => array( $cat_b ), 588 588 ) 589 589 ) 590 590 ) ); 591 591 … … 593 593 594 594 $posts = get_posts( array( 595 595 'tax_query' => array( 596 596 array( 597 597 'taxonomy' => 'category', … … 600 600 'include_children' => false 601 601 ) 602 602 ) 603 603 ) ); 604 604 … … 606 606 607 607 $posts = get_posts( array( 608 608 'tax_query' => array( 609 609 array( 610 610 'taxonomy' => 'category', … … 612 612 'terms' => array( $cat_c ), 613 613 ) 614 614 ) 615 615 ) ); 616 616 … … 618 618 619 619 $posts = get_posts( array( 620 620 'tax_query' => array( 621 621 array( 622 622 'taxonomy' => 'category', … … 625 625 'include_children' => false 626 626 ) 627 627 ) 628 628 ) ); 629 629
Note: See TracChangeset
for help on using the changeset viewer.