| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Test search query vars, without any initial posts in the database |
| | 5 | * |
| | 6 | * @group query |
| | 7 | * @group search |
| | 8 | */ |
| | 9 | class Tests_Query_Search extends WP_UnitTestCase { |
| | 10 | protected $q; |
| | 11 | |
| | 12 | function setUp() { |
| | 13 | parent::setUp(); |
| | 14 | |
| | 15 | unset( $this->q ); |
| | 16 | $this->q = new WP_Query(); |
| | 17 | } |
| | 18 | |
| | 19 | function test_search_fields_filter() { |
| | 20 | $p1 = $this->factory->post->create( array( 'post_title' => 'Burrito' ) ); |
| | 21 | $p2 = $this->factory->post->create( array( 'post_content' => 'Burrito' ) ); |
| | 22 | $p3 = $this->factory->post->create( array( 'post_excerpt' => 'Burrito' ) ); |
| | 23 | |
| | 24 | add_filter( 'search_fields', array( $this, 'filter_search_fields' ) ); |
| | 25 | $posts = $this->q->query( array( |
| | 26 | 'fields' => 'ids', |
| | 27 | 's' => 'Burrito' |
| | 28 | ) ); |
| | 29 | $this->assertEquals( array( $p3 ), $posts ); |
| | 30 | remove_filter( 'search_fields', array( $this, 'filter_search_fields' ) ); |
| | 31 | |
| | 32 | add_filter( 'search_fields', array( $this, 'empty_search_fields' ) ); |
| | 33 | $posts2 = $this->q->query( array( |
| | 34 | 'fields' => 'ids', |
| | 35 | 's' => 'Burrito' |
| | 36 | ) ); |
| | 37 | $this->assertEqualSets( array( $p3, $p2, $p1 ), $posts2 ); |
| | 38 | remove_filter( 'search_fields', array( $this, 'empty_search_fields' ) ); |
| | 39 | } |
| | 40 | |
| | 41 | function filter_search_fields() { |
| | 42 | global $wpdb; |
| | 43 | return array( "$wpdb->posts.post_excerpt" ); |
| | 44 | } |
| | 45 | |
| | 46 | function empty_search_fields() { |
| | 47 | return array(); |
| | 48 | } |
| | 49 | } |
| | 50 | No newline at end of file |