Changeset 42343 for trunk/tests/phpunit/tests/post/query.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/post/query.php (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/post/query.php
r40278 r42343 12 12 $q = new WP_Query(); 13 13 14 $term_id = self::factory()->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) ); 15 $term_id2 = self::factory()->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) ); 16 $post_id = self::factory()->post->create(); 14 $term_id = self::factory()->category->create( 15 array( 16 'slug' => 'woo', 17 'name' => 'WOO!', 18 ) 19 ); 20 $term_id2 = self::factory()->category->create( 21 array( 22 'slug' => 'hoo', 23 'name' => 'HOO!', 24 ) 25 ); 26 $post_id = self::factory()->post->create(); 17 27 18 28 wp_set_post_categories( $post_id, $term_id ); … … 42 52 */ 43 53 function test_empty_category__in() { 44 $cat_id = self::factory()->category->create();54 $cat_id = self::factory()->category->create(); 45 55 $post_id = self::factory()->post->create(); 46 56 wp_set_post_categories( $post_id, $cat_id ); … … 51 61 $this->assertNotEmpty( $q2 ); 52 62 53 $tag = wp_insert_term( 'woo', 'post_tag' );63 $tag = wp_insert_term( 'woo', 'post_tag' ); 54 64 $tag_id = $tag['term_id']; 55 $slug = get_tag( $tag_id )->slug;65 $slug = get_tag( $tag_id )->slug; 56 66 wp_set_post_tags( $post_id, $slug ); 57 67 … … 73 83 // Create posts and clear their caches. 74 84 $post_ids = self::factory()->post->create_many( 4 ); 75 foreach ( $post_ids as $post_id ) 85 foreach ( $post_ids as $post_id ) { 76 86 clean_post_cache( $post_id ); 87 } 77 88 78 89 add_filter( 'the_posts', array( $this, 'the_posts_filter' ) ); 79 90 80 $query = new WP_Query( array( 81 'post_type' => 'post', 82 'posts_per_page' => 3, 83 ) ); 91 $query = new WP_Query( 92 array( 93 'post_type' => 'post', 94 'posts_per_page' => 3, 95 ) 96 ); 84 97 85 98 // Fourth post added in filter … … 109 122 110 123 // Add some custom data to each post. 111 foreach ( $posts as $key => $post ) 124 foreach ( $posts as $key => $post ) { 112 125 $posts[ $key ]->custom_data = array( $post->ID, 'custom data' ); 126 } 113 127 114 128 return $posts; … … 116 130 117 131 function test_post__in_ordering() { 118 $post_id1 = self::factory()->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) ); 119 $post_id2 = self::factory()->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) ); 120 $post_id3 = self::factory()->post->create( array( 121 'post_type' => 'page', 122 'post_parent' => $post_id2, 123 'menu_order' => rand( 1, 100 ) 124 ) ); 125 $post_id4 = self::factory()->post->create( array( 126 'post_type' => 'page', 127 'post_parent' => $post_id2, 128 'menu_order' => rand( 1, 100 ) 129 ) ); 130 $post_id5 = self::factory()->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) ); 132 $post_id1 = self::factory()->post->create( 133 array( 134 'post_type' => 'page', 135 'menu_order' => rand( 1, 100 ), 136 ) 137 ); 138 $post_id2 = self::factory()->post->create( 139 array( 140 'post_type' => 'page', 141 'menu_order' => rand( 1, 100 ), 142 ) 143 ); 144 $post_id3 = self::factory()->post->create( 145 array( 146 'post_type' => 'page', 147 'post_parent' => $post_id2, 148 'menu_order' => rand( 1, 100 ), 149 ) 150 ); 151 $post_id4 = self::factory()->post->create( 152 array( 153 'post_type' => 'page', 154 'post_parent' => $post_id2, 155 'menu_order' => rand( 1, 100 ), 156 ) 157 ); 158 $post_id5 = self::factory()->post->create( 159 array( 160 'post_type' => 'page', 161 'menu_order' => rand( 1, 100 ), 162 ) 163 ); 131 164 132 165 $ordered = array( $post_id2, $post_id4, $post_id3, $post_id1, $post_id5 ); 133 166 134 $q = new WP_Query( array( 135 'post_type' => 'any', 136 'post__in' => $ordered, 137 'orderby' => 'post__in' 138 ) ); 167 $q = new WP_Query( 168 array( 169 'post_type' => 'any', 170 'post__in' => $ordered, 171 'orderby' => 'post__in', 172 ) 173 ); 139 174 $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) ); 140 175 } 141 176 142 177 function test_post__in_attachment_ordering() { 143 $post_id = self::factory()->post->create(); 144 $att_ids = array(); 145 $file = DIR_TESTDATA . '/images/canola.jpg'; 146 $att_ids[1] = self::factory()->attachment->create_object( $file, $post_id, array( 147 'post_mime_type' => 'image/jpeg', 148 'menu_order' => rand( 1, 100 ) 149 ) ); 150 $att_ids[2] = self::factory()->attachment->create_object( $file, $post_id, array( 151 'post_mime_type' => 'image/jpeg', 152 'menu_order' => rand( 1, 100 ) 153 ) ); 154 $att_ids[3] = self::factory()->attachment->create_object( $file, $post_id, array( 155 'post_mime_type' => 'image/jpeg', 156 'menu_order' => rand( 1, 100 ) 157 ) ); 158 $att_ids[4] = self::factory()->attachment->create_object( $file, $post_id, array( 159 'post_mime_type' => 'image/jpeg', 160 'menu_order' => rand( 1, 100 ) 161 ) ); 162 $att_ids[5] = self::factory()->attachment->create_object( $file, $post_id, array( 163 'post_mime_type' => 'image/jpeg', 164 'menu_order' => rand( 1, 100 ) 165 ) ); 178 $post_id = self::factory()->post->create(); 179 $att_ids = array(); 180 $file = DIR_TESTDATA . '/images/canola.jpg'; 181 $att_ids[1] = self::factory()->attachment->create_object( 182 $file, $post_id, array( 183 'post_mime_type' => 'image/jpeg', 184 'menu_order' => rand( 1, 100 ), 185 ) 186 ); 187 $att_ids[2] = self::factory()->attachment->create_object( 188 $file, $post_id, array( 189 'post_mime_type' => 'image/jpeg', 190 'menu_order' => rand( 1, 100 ), 191 ) 192 ); 193 $att_ids[3] = self::factory()->attachment->create_object( 194 $file, $post_id, array( 195 'post_mime_type' => 'image/jpeg', 196 'menu_order' => rand( 1, 100 ), 197 ) 198 ); 199 $att_ids[4] = self::factory()->attachment->create_object( 200 $file, $post_id, array( 201 'post_mime_type' => 'image/jpeg', 202 'menu_order' => rand( 1, 100 ), 203 ) 204 ); 205 $att_ids[5] = self::factory()->attachment->create_object( 206 $file, $post_id, array( 207 'post_mime_type' => 'image/jpeg', 208 'menu_order' => rand( 1, 100 ), 209 ) 210 ); 166 211 167 212 $ordered = array( $att_ids[5], $att_ids[1], $att_ids[4], $att_ids[3], $att_ids[2] ); 168 213 169 $attached = new WP_Query( array( 170 'post__in' => $ordered, 171 'post_type' => 'attachment', 172 'post_parent' => $post_id, 173 'post_mime_type' => 'image', 174 'post_status' => 'inherit', 175 'posts_per_page' => '-1', 176 'orderby' => 'post__in' 177 ) ); 214 $attached = new WP_Query( 215 array( 216 'post__in' => $ordered, 217 'post_type' => 'attachment', 218 'post_parent' => $post_id, 219 'post_mime_type' => 'image', 220 'post_status' => 'inherit', 221 'posts_per_page' => '-1', 222 'orderby' => 'post__in', 223 ) 224 ); 178 225 $this->assertSame( $ordered, wp_list_pluck( $attached->posts, 'ID' ) ); 179 226 } … … 183 230 */ 184 231 public function test_post_name__in_ordering() { 185 $post_id1 = self::factory()->post->create( array( 'post_name' => 'id-1', 'post_type' => 'page' ) ); 186 $post_id2 = self::factory()->post->create( array( 'post_name' => 'id-2', 'post_type' => 'page' ) ); 187 $post_id3 = self::factory()->post->create( array( 188 'post_name' => 'id-3', 189 'post_type' => 'page', 190 'post_parent' => $post_id2 191 ) ); 232 $post_id1 = self::factory()->post->create( 233 array( 234 'post_name' => 'id-1', 235 'post_type' => 'page', 236 ) 237 ); 238 $post_id2 = self::factory()->post->create( 239 array( 240 'post_name' => 'id-2', 241 'post_type' => 'page', 242 ) 243 ); 244 $post_id3 = self::factory()->post->create( 245 array( 246 'post_name' => 'id-3', 247 'post_type' => 'page', 248 'post_parent' => $post_id2, 249 ) 250 ); 192 251 193 252 $ordered = array( 'id-2', 'id-3', 'id-1' ); 194 253 195 $q = new WP_Query( array( 196 'post_type' => 'any', 197 'post_name__in' => $ordered, 198 'orderby' => 'post_name__in' 199 ) ); 254 $q = new WP_Query( 255 array( 256 'post_type' => 'any', 257 'post_name__in' => $ordered, 258 'orderby' => 'post_name__in', 259 ) 260 ); 200 261 201 262 $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'post_name' ) ); … … 223 284 224 285 /** 225 *226 286 * @ticket 17065 227 287 */ … … 229 289 global $wpdb; 230 290 231 $q1 = new WP_Query( array( 232 'orderby' => array( 233 'type' => 'DESC', 234 'name' => 'ASC' 235 ) 236 ) ); 291 $q1 = new WP_Query( 292 array( 293 'orderby' => array( 294 'type' => 'DESC', 295 'name' => 'ASC', 296 ), 297 ) 298 ); 237 299 $this->assertContains( 238 300 "ORDER BY $wpdb->posts.post_type DESC, $wpdb->posts.post_name ASC", … … 258 320 259 321 /** 260 *261 322 * @ticket 17065 262 323 */ … … 264 325 global $wpdb; 265 326 266 $q1 = new WP_Query( array( 267 'orderby' => array( 268 'post_type' => 'foo' 269 ) 270 ) ); 327 $q1 = new WP_Query( 328 array( 329 'orderby' => array( 330 'post_type' => 'foo', 331 ), 332 ) 333 ); 271 334 $this->assertContains( 272 335 "ORDER BY $wpdb->posts.post_type DESC", … … 274 337 ); 275 338 276 $q2 = new WP_Query( array( 277 'orderby' => 'title', 278 'order' => 'foo' 279 ) ); 339 $q2 = new WP_Query( 340 array( 341 'orderby' => 'title', 342 'order' => 'foo', 343 ) 344 ); 280 345 $this->assertContains( 281 346 "ORDER BY $wpdb->posts.post_title DESC", … … 283 348 ); 284 349 285 $q3 = new WP_Query( array( 286 'order' => 'asc' 287 ) ); 350 $q3 = new WP_Query( 351 array( 352 'order' => 'asc', 353 ) 354 ); 288 355 $this->assertContains( 289 356 "ORDER BY $wpdb->posts.post_date ASC", … … 331 398 */ 332 399 public function test_orderby_rand_with_seed() { 333 $q = new WP_Query( array( 334 'orderby' => 'RAND(5)', 335 ) ); 400 $q = new WP_Query( 401 array( 402 'orderby' => 'RAND(5)', 403 ) 404 ); 336 405 337 406 $this->assertContains( 'ORDER BY RAND(5)', $q->request ); … … 342 411 */ 343 412 public function test_orderby_rand_should_ignore_invalid_seed() { 344 $q = new WP_Query( array( 345 'orderby' => 'RAND(foo)', 346 ) ); 413 $q = new WP_Query( 414 array( 415 'orderby' => 'RAND(foo)', 416 ) 417 ); 347 418 348 419 $this->assertNotContains( 'ORDER BY RAND', $q->request ); … … 353 424 */ 354 425 public function test_orderby_rand_with_seed_should_be_case_insensitive() { 355 $q = new WP_Query( array( 356 'orderby' => 'rand(5)', 357 ) ); 426 $q = new WP_Query( 427 array( 428 'orderby' => 'rand(5)', 429 ) 430 ); 358 431 359 432 $this->assertContains( 'ORDER BY RAND(5)', $q->request ); … … 368 441 $q = new WP_Query(); 369 442 370 $post_ids[0] = self::factory()->post->create( array( 'post_title' => 'woo', 'post_date' => '2015-07-23 00:00:00' ) ); 371 $post_ids[1] = self::factory()->post->create( array( 'post_title' => 'hoo', 'post_date' => '2015-07-23 00:00:00' ) ); 372 $post_ids[2] = self::factory()->post->create( array( 'post_title' => 'test', 'post_date' => '2015-07-23 00:00:00' ) ); 373 $post_ids[3] = self::factory()->post->create( array( 'post_title' => 'me', 'post_date' => '2015-07-23 00:00:00' ) ); 443 $post_ids[0] = self::factory()->post->create( 444 array( 445 'post_title' => 'woo', 446 'post_date' => '2015-07-23 00:00:00', 447 ) 448 ); 449 $post_ids[1] = self::factory()->post->create( 450 array( 451 'post_title' => 'hoo', 452 'post_date' => '2015-07-23 00:00:00', 453 ) 454 ); 455 $post_ids[2] = self::factory()->post->create( 456 array( 457 'post_title' => 'test', 458 'post_date' => '2015-07-23 00:00:00', 459 ) 460 ); 461 $post_ids[3] = self::factory()->post->create( 462 array( 463 'post_title' => 'me', 464 'post_date' => '2015-07-23 00:00:00', 465 ) 466 ); 374 467 375 468 $requested = array( $post_ids[0], $post_ids[3] ); 376 $q->query( array( 377 'post_name__in' => array( 'woo', 'me' ), 378 'fields' => 'ids', 379 ) ); 469 $q->query( 470 array( 471 'post_name__in' => array( 'woo', 'me' ), 472 'fields' => 'ids', 473 ) 474 ); 380 475 $actual_posts = $q->get_posts(); 381 476 $this->assertEqualSets( $requested, $actual_posts ); 382 477 383 478 $requested = array( $post_ids[1], $post_ids[2] ); 384 $q->query( array( 385 'post_name__in' => array( 'hoo', 'test' ), 386 'fields' => 'ids', 387 ) ); 479 $q->query( 480 array( 481 'post_name__in' => array( 'hoo', 'test' ), 482 'fields' => 'ids', 483 ) 484 ); 388 485 $actual_posts = $q->get_posts(); 389 486 $this->assertEqualSets( $requested, $actual_posts ); … … 399 496 400 497 $num_queries = $wpdb->num_queries; 401 $q = new WP_Query( array( 402 'fields' => 'ids', 403 'no_found_rows' => true, 404 ) ); 498 $q = new WP_Query( 499 array( 500 'fields' => 'ids', 501 'no_found_rows' => true, 502 ) 503 ); 405 504 406 505 remove_filter( 'posts_pre_query', array( __CLASS__, 'filter_posts_pre_query' ) ); … … 461 560 } 462 561 463 $q = new WP_Query( array( 464 'post_type' => 'wptests_pt', 465 'posts_per_page' => 1, 466 'fields' => 'ids', 467 ) ); 562 $q = new WP_Query( 563 array( 564 'post_type' => 'wptests_pt', 565 'posts_per_page' => 1, 566 'fields' => 'ids', 567 ) 568 ); 468 569 469 570 $this->assertEquals( 2, $q->found_posts ); … … 482 583 } 483 584 484 $q = new WP_Query( array( 485 'post_type' => 'wptests_pt', 486 'posts_per_page' => 1, 487 'fields' => 'id=>parent', 488 ) ); 585 $q = new WP_Query( 586 array( 587 'post_type' => 'wptests_pt', 588 'posts_per_page' => 1, 589 'fields' => 'id=>parent', 590 ) 591 ); 489 592 490 593 $this->assertEquals( 2, $q->found_posts ); … … 504 607 505 608 add_filter( 'split_the_query', '__return_true' ); 506 $q = new WP_Query( array( 507 'post_type' => 'wptests_pt', 508 'posts_per_page' => 1, 509 ) ); 609 $q = new WP_Query( 610 array( 611 'post_type' => 'wptests_pt', 612 'posts_per_page' => 1, 613 ) 614 ); 510 615 remove_filter( 'split_the_query', '__return_true' ); 511 616 … … 527 632 // ! $split_the_query 528 633 add_filter( 'split_the_query', '__return_false' ); 529 $q = new WP_Query( array( 530 'post_type' => 'wptests_pt', 531 'posts_per_page' => 1, 532 ) ); 634 $q = new WP_Query( 635 array( 636 'post_type' => 'wptests_pt', 637 'posts_per_page' => 1, 638 ) 639 ); 533 640 remove_filter( 'split_the_query', '__return_false' ); 534 641
Note: See TracChangeset
for help on using the changeset viewer.