Changeset 42343 for trunk/tests/phpunit/tests/query/search.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/query/search.php (modified) (25 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/query/search.php
r38844 r42343 1 1 <?php 2 2 /** 3 *4 3 * @group query 5 4 * @group search … … 26 25 27 26 function get_search_results( $terms ) { 28 $args = http_build_query( array( 's' => $terms, 'post_type' => $this->post_type ) ); 27 $args = http_build_query( 28 array( 29 's' => $terms, 30 'post_type' => $this->post_type, 31 ) 32 ); 29 33 return $this->q->query( $args ); 30 34 } 31 35 32 36 function test_search_order_title_relevance() { 33 foreach ( range( 1, 7 ) as $i ) 34 self::factory()->post->create( array( 'post_content' => $i . rand_str() . ' about', 'post_type' => $this->post_type ) ); 35 $post_id = self::factory()->post->create( array( 'post_title' => 'About', 'post_type' => $this->post_type ) ); 37 foreach ( range( 1, 7 ) as $i ) { 38 self::factory()->post->create( 39 array( 40 'post_content' => $i . rand_str() . ' about', 41 'post_type' => $this->post_type, 42 ) 43 ); 44 } 45 $post_id = self::factory()->post->create( 46 array( 47 'post_title' => 'About', 48 'post_type' => $this->post_type, 49 ) 50 ); 36 51 37 52 $posts = $this->get_search_results( 'About' ); … … 67 82 68 83 // Create a post with a title which starts with a hyphen 69 $post_id = self::factory()->post->create( array( 70 'post_content' => $title, 'post_type' => $this->post_type 71 ) ); 84 $post_id = self::factory()->post->create( 85 array( 86 'post_content' => $title, 87 'post_type' => $this->post_type, 88 ) 89 ); 72 90 73 91 // By default, we can use the hyphen prefix to exclude results … … 77 95 add_filter( 'wp_query_search_exclusion_prefix', '__return_false' ); 78 96 $result = $this->get_search_results( $title ); 79 $post = array_pop( $result );97 $post = array_pop( $result ); 80 98 $this->assertEquals( $post->ID, $post_id ); 81 99 remove_filter( 'wp_query_search_exclusion_prefix', '__return_false' ); … … 89 107 90 108 // Create a post with a title that starts with a non-hyphen prefix. 91 $post_id = self::factory()->post->create( array( 92 'post_content' => $title, 'post_type' => $this->post_type 93 ) ); 109 $post_id = self::factory()->post->create( 110 array( 111 'post_content' => $title, 112 'post_type' => $this->post_type, 113 ) 114 ); 94 115 95 116 // By default, we should get the result. 96 117 $result = $this->get_search_results( $title ); 97 $post = array_pop( $result );118 $post = array_pop( $result ); 98 119 $this->assertEquals( $post->ID, $post_id ); 99 120 … … 113 134 */ 114 135 public function test_s_should_exclude_term_prefixed_with_dash() { 115 $p1 = self::factory()->post->create( array( 116 'post_status' => 'publish', 117 'post_content' => 'This post has foo but also bar', 118 ) ); 119 $p2 = self::factory()->post->create( array( 120 'post_status' => 'publish', 121 'post_content' => 'This post has only foo', 122 ) ); 123 124 $q = new WP_Query( array( 125 's' => 'foo -bar', 126 'fields' => 'ids', 127 ) ); 136 $p1 = self::factory()->post->create( 137 array( 138 'post_status' => 'publish', 139 'post_content' => 'This post has foo but also bar', 140 ) 141 ); 142 $p2 = self::factory()->post->create( 143 array( 144 'post_status' => 'publish', 145 'post_content' => 'This post has only foo', 146 ) 147 ); 148 149 $q = new WP_Query( 150 array( 151 's' => 'foo -bar', 152 'fields' => 'ids', 153 ) 154 ); 128 155 129 156 $this->assertEqualSets( array( $p2 ), $q->posts ); … … 134 161 */ 135 162 public function test_s_should_exclude_first_term_if_prefixed_with_dash() { 136 $p1 = self::factory()->post->create( array( 137 'post_status' => 'publish', 138 'post_content' => 'This post has foo but also bar', 139 ) ); 140 $p2 = self::factory()->post->create( array( 141 'post_status' => 'publish', 142 'post_content' => 'This post has only bar', 143 ) ); 144 145 $q = new WP_Query( array( 146 's' => '-foo bar', 147 'fields' => 'ids', 148 ) ); 163 $p1 = self::factory()->post->create( 164 array( 165 'post_status' => 'publish', 166 'post_content' => 'This post has foo but also bar', 167 ) 168 ); 169 $p2 = self::factory()->post->create( 170 array( 171 'post_status' => 'publish', 172 'post_content' => 'This post has only bar', 173 ) 174 ); 175 176 $q = new WP_Query( 177 array( 178 's' => '-foo bar', 179 'fields' => 'ids', 180 ) 181 ); 149 182 150 183 $this->assertEqualSets( array( $p2 ), $q->posts ); … … 155 188 */ 156 189 public function test_s_should_not_exclude_for_dashes_in_the_middle_of_words() { 157 $p1 = self::factory()->post->create( array( 158 'post_status' => 'publish', 159 'post_content' => 'This post has foo but also bar', 160 ) ); 161 $p2 = self::factory()->post->create( array( 162 'post_status' => 'publish', 163 'post_content' => 'This post has only bar', 164 ) ); 165 $p3 = self::factory()->post->create( array( 166 'post_status' => 'publish', 167 'post_content' => 'This post has only foo-bar', 168 ) ); 169 170 $q = new WP_Query( array( 171 's' => 'foo-bar', 172 'fields' => 'ids', 173 ) ); 190 $p1 = self::factory()->post->create( 191 array( 192 'post_status' => 'publish', 193 'post_content' => 'This post has foo but also bar', 194 ) 195 ); 196 $p2 = self::factory()->post->create( 197 array( 198 'post_status' => 'publish', 199 'post_content' => 'This post has only bar', 200 ) 201 ); 202 $p3 = self::factory()->post->create( 203 array( 204 'post_status' => 'publish', 205 'post_content' => 'This post has only foo-bar', 206 ) 207 ); 208 209 $q = new WP_Query( 210 array( 211 's' => 'foo-bar', 212 'fields' => 'ids', 213 ) 214 ); 174 215 175 216 $this->assertEqualSets( array( $p3 ), $q->posts ); … … 180 221 */ 181 222 public function test_s_should_not_exclude_for_dashes_between_words() { 182 $p1 = self::factory()->post->create( array( 183 'post_status' => 'publish', 184 'post_content' => 'This post has foo but also bar', 185 ) ); 186 $p2 = self::factory()->post->create( array( 187 'post_status' => 'publish', 188 'post_content' => 'This post has only bar', 189 ) ); 190 $p3 = self::factory()->post->create( array( 191 'post_status' => 'publish', 192 'post_content' => 'This post has only foo - bar', 193 ) ); 194 195 $q = new WP_Query( array( 196 's' => 'foo - bar', 197 'fields' => 'ids', 198 ) ); 223 $p1 = self::factory()->post->create( 224 array( 225 'post_status' => 'publish', 226 'post_content' => 'This post has foo but also bar', 227 ) 228 ); 229 $p2 = self::factory()->post->create( 230 array( 231 'post_status' => 'publish', 232 'post_content' => 'This post has only bar', 233 ) 234 ); 235 $p3 = self::factory()->post->create( 236 array( 237 'post_status' => 'publish', 238 'post_content' => 'This post has only foo - bar', 239 ) 240 ); 241 242 $q = new WP_Query( 243 array( 244 's' => 'foo - bar', 245 'fields' => 'ids', 246 ) 247 ); 199 248 200 249 $this->assertEqualSets( array( $p1, $p3 ), $q->posts ); … … 205 254 */ 206 255 public function test_search_orderby_should_be_empty_when_search_string_is_longer_than_6_words_and_exclusion_operator_is_used() { 207 $q = new WP_Query( array( 208 's' => 'foo1 foo2 foo3 foo4 foo5 foo6 foo7 -bar', 209 'fields' => 'ids', 210 ) ); 256 $q = new WP_Query( 257 array( 258 's' => 'foo1 foo2 foo3 foo4 foo5 foo6 foo7 -bar', 259 'fields' => 'ids', 260 ) 261 ); 211 262 212 263 $this->assertNotRegExp( '|ORDER BY \(CASE[^\)]+\)|', $q->request ); … … 217 268 */ 218 269 public function test_s_zero() { 219 $p1 = $this->factory->post->create( array( 220 'post_status' => 'publish', 221 'post_title' => '1', 222 'post_content' => 'this post contains no zeroes', 223 'post_excerpt' => 'this post contains no zeroes', 224 ) ); 225 226 $p2 = $this->factory->post->create( array( 227 'post_status' => 'publish', 228 'post_title' => '0', 229 'post_content' => 'this post contains zeroes', 230 'post_excerpt' => 'this post containts zeroes', 231 ) ); 232 233 $q = new WP_Query( array( 234 's' => '0', 235 'fields' => 'ids', 236 ) ); 270 $p1 = $this->factory->post->create( 271 array( 272 'post_status' => 'publish', 273 'post_title' => '1', 274 'post_content' => 'this post contains no zeroes', 275 'post_excerpt' => 'this post contains no zeroes', 276 ) 277 ); 278 279 $p2 = $this->factory->post->create( 280 array( 281 'post_status' => 'publish', 282 'post_title' => '0', 283 'post_content' => 'this post contains zeroes', 284 'post_excerpt' => 'this post containts zeroes', 285 ) 286 ); 287 288 $q = new WP_Query( 289 array( 290 's' => '0', 291 'fields' => 'ids', 292 ) 293 ); 237 294 238 295 $this->assertEqualSets( array( $p2 ), $q->posts ); … … 245 302 add_filter( 'posts_search', array( $this, 'filter_posts_search' ) ); 246 303 add_filter( 'posts_search_orderby', array( $this, 'filter_posts_search' ) ); 247 $q = new WP_Query( array( 248 's' => 'foo', 249 'suppress_filters' => true, 250 ) ); 304 $q = new WP_Query( 305 array( 306 's' => 'foo', 307 'suppress_filters' => true, 308 ) 309 ); 251 310 remove_filter( 'posts_search', array( $this, 'filter_posts_search' ) ); 252 311 remove_filter( 'posts_search_orderby', array( $this, 'filter_posts_search' ) ); … … 259 318 */ 260 319 public function test_search_post_excerpt() { 261 $p1 = self::factory()->post->create( array( 262 'post_status' => 'publish', 263 'post_content' => 'This post has foo but also bar', 264 ) ); 265 $p2 = self::factory()->post->create( array( 266 'post_status' => 'publish', 267 'post_content' => '', 268 'post_excerpt' => 'This post has bar and baz', 269 ) ); 270 $p3 = self::factory()->post->create( array( 271 'post_status' => 'publish', 272 'post_content' => '', 273 'post_excerpt' => 'This post has only foo', 274 ) ); 275 276 $q = new WP_Query( array( 277 's' => 'foo', 278 'fields' => 'ids', 279 ) ); 320 $p1 = self::factory()->post->create( 321 array( 322 'post_status' => 'publish', 323 'post_content' => 'This post has foo but also bar', 324 ) 325 ); 326 $p2 = self::factory()->post->create( 327 array( 328 'post_status' => 'publish', 329 'post_content' => '', 330 'post_excerpt' => 'This post has bar and baz', 331 ) 332 ); 333 $p3 = self::factory()->post->create( 334 array( 335 'post_status' => 'publish', 336 'post_content' => '', 337 'post_excerpt' => 'This post has only foo', 338 ) 339 ); 340 341 $q = new WP_Query( 342 array( 343 's' => 'foo', 344 'fields' => 'ids', 345 ) 346 ); 280 347 281 348 $this->assertEqualSets( array( $p1, $p3 ), $q->posts ); 282 349 283 $q = new WP_Query( array( 284 's' => 'bar', 285 'fields' => 'ids', 286 ) ); 350 $q = new WP_Query( 351 array( 352 's' => 'bar', 353 'fields' => 'ids', 354 ) 355 ); 287 356 288 357 $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); 289 358 290 $q = new WP_Query( array( 291 's' => 'baz', 292 'fields' => 'ids', 293 ) ); 359 $q = new WP_Query( 360 array( 361 's' => 'baz', 362 'fields' => 'ids', 363 ) 364 ); 294 365 295 366 $this->assertEqualSets( array( $p2 ), $q->posts ); … … 300 371 */ 301 372 public function test_search_order_title_before_excerpt_and_content() { 302 $p1 = self::factory()->post->create( array( 303 'post_status' => 'publish', 304 'post_title' => 'This post has foo', 305 'post_content' => '', 306 'post_excerpt' => '', 307 ) ); 308 309 $p2 = self::factory()->post->create( array( 310 'post_status' => 'publish', 311 'post_title' => '', 312 'post_content' => 'This post has foo', 313 'post_excerpt' => '', 314 ) ); 315 316 $p3 = self::factory()->post->create( array( 317 'post_status' => 'publish', 318 'post_title' => '', 319 'post_content' => '', 320 'post_excerpt' => 'This post has foo', 321 ) ); 322 323 $q = new WP_Query( array( 324 's' => 'this post has foo', 325 'fields' => 'ids', 326 'orderby' => false, 327 ) ); 373 $p1 = self::factory()->post->create( 374 array( 375 'post_status' => 'publish', 376 'post_title' => 'This post has foo', 377 'post_content' => '', 378 'post_excerpt' => '', 379 ) 380 ); 381 382 $p2 = self::factory()->post->create( 383 array( 384 'post_status' => 'publish', 385 'post_title' => '', 386 'post_content' => 'This post has foo', 387 'post_excerpt' => '', 388 ) 389 ); 390 391 $p3 = self::factory()->post->create( 392 array( 393 'post_status' => 'publish', 394 'post_title' => '', 395 'post_content' => '', 396 'post_excerpt' => 'This post has foo', 397 ) 398 ); 399 400 $q = new WP_Query( 401 array( 402 's' => 'this post has foo', 403 'fields' => 'ids', 404 'orderby' => false, 405 ) 406 ); 328 407 329 408 $this->assertSame( array( $p1, $p3, $p2 ), $q->posts ); … … 337 416 */ 338 417 public function test_exclude_file_names_in_attachment_search_by_default() { 339 $attachment = self::factory()->post->create( array( 340 'post_type' => 'attachment', 341 'post_status' => 'publish', 342 'post_title' => 'bar foo', 343 'post_content' => 'foo bar', 344 'post_excerpt' => 'This post has foo', 345 ) ); 418 $attachment = self::factory()->post->create( 419 array( 420 'post_type' => 'attachment', 421 'post_status' => 'publish', 422 'post_title' => 'bar foo', 423 'post_content' => 'foo bar', 424 'post_excerpt' => 'This post has foo', 425 ) 426 ); 346 427 347 428 add_post_meta( $attachment, '_wp_attached_file', 'some-image2.png', true ); 348 429 349 430 // Pass post_type an array value. 350 $q = new WP_Query( array( 351 's' => 'image2', 352 'fields' => 'ids', 353 'post_type' => 'attachment', 354 'post_status' => 'inherit', 355 ) ); 431 $q = new WP_Query( 432 array( 433 's' => 'image2', 434 'fields' => 'ids', 435 'post_type' => 'attachment', 436 'post_status' => 'inherit', 437 ) 438 ); 356 439 357 440 $this->assertNotEquals( array( $attachment ), $q->posts ); … … 362 445 */ 363 446 public function test_include_file_names_in_attachment_search_as_string() { 364 $attachment = self::factory()->post->create( array( 365 'post_type' => 'attachment', 366 'post_status' => 'publish', 367 'post_title' => 'bar foo', 368 'post_content' => 'foo bar', 369 'post_excerpt' => 'This post has foo', 370 ) ); 447 $attachment = self::factory()->post->create( 448 array( 449 'post_type' => 'attachment', 450 'post_status' => 'publish', 451 'post_title' => 'bar foo', 452 'post_content' => 'foo bar', 453 'post_excerpt' => 'This post has foo', 454 ) 455 ); 371 456 372 457 add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true ); … … 374 459 375 460 // Pass post_type a string value. 376 $q = new WP_Query( array( 377 's' => 'image1', 378 'fields' => 'ids', 379 'post_type' => 'attachment', 380 'post_status' => 'inherit', 381 ) ); 461 $q = new WP_Query( 462 array( 463 's' => 'image1', 464 'fields' => 'ids', 465 'post_type' => 'attachment', 466 'post_status' => 'inherit', 467 ) 468 ); 382 469 383 470 $this->assertSame( array( $attachment ), $q->posts ); … … 388 475 */ 389 476 public function test_include_file_names_in_attachment_search_as_array() { 390 $attachment = self::factory()->post->create( array( 391 'post_type' => 'attachment', 392 'post_status' => 'publish', 393 'post_title' => 'bar foo', 394 'post_content' => 'foo bar', 395 'post_excerpt' => 'This post has foo', 396 ) ); 477 $attachment = self::factory()->post->create( 478 array( 479 'post_type' => 'attachment', 480 'post_status' => 'publish', 481 'post_title' => 'bar foo', 482 'post_content' => 'foo bar', 483 'post_excerpt' => 'This post has foo', 484 ) 485 ); 397 486 398 487 add_post_meta( $attachment, '_wp_attached_file', 'some-image2.png', true ); … … 400 489 401 490 // Pass post_type an array value. 402 $q = new WP_Query( array( 403 's' => 'image2', 404 'fields' => 'ids', 405 'post_type' => array( 'attachment' ), 406 'post_status' => 'inherit', 407 ) ); 491 $q = new WP_Query( 492 array( 493 's' => 'image2', 494 'fields' => 'ids', 495 'post_type' => array( 'attachment' ), 496 'post_status' => 'inherit', 497 ) 498 ); 408 499 409 500 $this->assertSame( array( $attachment ), $q->posts ); … … 414 505 */ 415 506 public function test_exclude_attachment_file_names_in_general_searches() { 416 $attachment = self::factory()->post->create( array( 417 'post_type' => 'attachment', 418 'post_status' => 'publish', 419 'post_title' => 'bar foo', 420 'post_content' => 'foo bar', 421 'post_excerpt' => 'This post has foo', 422 ) ); 507 $attachment = self::factory()->post->create( 508 array( 509 'post_type' => 'attachment', 510 'post_status' => 'publish', 511 'post_title' => 'bar foo', 512 'post_content' => 'foo bar', 513 'post_excerpt' => 'This post has foo', 514 ) 515 ); 423 516 424 517 add_post_meta( $attachment, '_wp_attached_file', 'some-image3.png', true ); 425 518 426 $q = new WP_Query( array( 427 's' => 'image3', 428 'fields' => 'ids', 429 'post_type' => array( 'post', 'page', 'attachment' ), 430 'post_status' => 'inherit', 431 ) ); 519 $q = new WP_Query( 520 array( 521 's' => 'image3', 522 'fields' => 'ids', 523 'post_type' => array( 'post', 'page', 'attachment' ), 524 'post_status' => 'inherit', 525 ) 526 ); 432 527 433 528 $this->assertNotEquals( array( $attachment ), $q->posts ); … … 438 533 */ 439 534 public function test_include_file_names_in_attachment_search_with_meta_query() { 440 $attachment = self::factory()->post->create( array( 441 'post_type' => 'attachment', 442 'post_status' => 'publish', 443 'post_title' => 'bar foo', 444 'post_content' => 'foo bar', 445 'post_excerpt' => 'This post has foo', 446 ) ); 535 $attachment = self::factory()->post->create( 536 array( 537 'post_type' => 'attachment', 538 'post_status' => 'publish', 539 'post_title' => 'bar foo', 540 'post_content' => 'foo bar', 541 'post_excerpt' => 'This post has foo', 542 ) 543 ); 447 544 448 545 add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true ); … … 451 548 452 549 // Pass post_type a string value. 453 $q = new WP_Query( array( 454 's' => 'image4', 455 'fields' => 'ids', 456 'post_type' => 'attachment', 457 'post_status' => 'inherit', 458 'meta_query' => array( 459 array( 460 'key' => '_test_meta_key', 461 'value' => 'value', 462 'compare' => '=', 550 $q = new WP_Query( 551 array( 552 's' => 'image4', 553 'fields' => 'ids', 554 'post_type' => 'attachment', 555 'post_status' => 'inherit', 556 'meta_query' => array( 557 array( 558 'key' => '_test_meta_key', 559 'value' => 'value', 560 'compare' => '=', 561 ), 463 562 ), 464 ) ,465 ) );563 ) 564 ); 466 565 467 566 $this->assertSame( array( $attachment ), $q->posts ); … … 472 571 */ 473 572 public function test_include_file_names_in_attachment_search_with_tax_query() { 474 $attachment = self::factory()->post->create( array( 475 'post_type' => 'attachment', 476 'post_status' => 'publish', 477 'post_title' => 'bar foo', 478 'post_content' => 'foo bar', 479 'post_excerpt' => 'This post has foo', 480 ) ); 573 $attachment = self::factory()->post->create( 574 array( 575 'post_type' => 'attachment', 576 'post_status' => 'publish', 577 'post_title' => 'bar foo', 578 'post_content' => 'foo bar', 579 'post_excerpt' => 'This post has foo', 580 ) 581 ); 481 582 482 583 // Add a tag to the post. … … 487 588 488 589 // Pass post_type a string value. 489 $q = new WP_Query( array( 490 's' => 'image5', 491 'fields' => 'ids', 492 'post_type' => 'attachment', 493 'post_status' => 'inherit', 494 'tax_query' => array( 495 array( 496 'taxonomy' => 'post_tag', 497 'field' => 'slug', 498 'terms' => 'test', 590 $q = new WP_Query( 591 array( 592 's' => 'image5', 593 'fields' => 'ids', 594 'post_type' => 'attachment', 595 'post_status' => 'inherit', 596 'tax_query' => array( 597 array( 598 'taxonomy' => 'post_tag', 599 'field' => 'slug', 600 'terms' => 'test', 601 ), 499 602 ), 500 ) ,501 ) );603 ) 604 ); 502 605 503 606 $this->assertSame( array( $attachment ), $q->posts ); … … 510 613 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); 511 614 512 apply_filters( 'posts_clauses', array( 513 'where' => '', 514 'groupby' => '', 515 'join' => '', 516 'orderby' => '', 517 'distinct' => '', 518 'fields' => '', 519 'limit' => '', 520 ) ); 615 apply_filters( 616 'posts_clauses', array( 617 'where' => '', 618 'groupby' => '', 619 'join' => '', 620 'orderby' => '', 621 'distinct' => '', 622 'fields' => '', 623 'limit' => '', 624 ) 625 ); 521 626 522 627 $result = has_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
Note: See TracChangeset
for help on using the changeset viewer.