From 036cb74d461cc55c82feeefdfe1c962654356b97 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Sat, 8 Mar 2014 21:26:26 +0000 Subject: [PATCH] Add unit tests for searching by relevance. props wonderboymusic. see #7394, #20044. git-svn-id: https://develop.svn.wordpress.org/trunk@27474 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/query/search.php | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/phpunit/tests/query/search.php diff --git a/tests/phpunit/tests/query/search.php b/tests/phpunit/tests/query/search.php new file mode 100644 index 0000000000..d3b67b4dd0 --- /dev/null +++ b/tests/phpunit/tests/query/search.php @@ -0,0 +1,61 @@ +post_type = rand_str( 12 ); + register_post_type( $this->post_type ); + + $this->q = new WP_Query(); + } + + function tearDown() { + parent::tearDown(); + + _unregister_post_type( $this->post_type ); + unset( $this->q ); + } + + function get_search_results( $terms ) { + $args = http_build_query( array( 's' => $terms, 'post_type' => $this->post_type ) ); + return $this->q->query( $args ); + } + + function test_search_order_title_relevance() { + foreach ( range( 1, 7 ) as $i ) + $this->factory->post->create( array( 'post_content' => $i . rand_str() . ' about', 'post_type' => $this->post_type ) ); + $post_id = $this->factory->post->create( array( 'post_title' => 'About', 'post_type' => $this->post_type ) ); + + $posts = $this->get_search_results( 'About' ); + $this->assertEquals( $post_id, reset( $posts )->ID ); + } + + function test_search_terms_query_var() { + $terms = 'This is a search term'; + $query = new WP_Query( array( 's' => 'This is a search term' ) ); + $this->assertNotEquals( explode( ' ', $terms ), $query->get( 'search_terms' ) ); + $this->assertEquals( array( 'search', 'term' ), $query->get( 'search_terms' ) ); + } + + function test_filter_stopwords() { + $terms = 'This is a search term'; + add_filter( 'wp_search_stopwords', array( $this, 'filter_wp_search_stopwords' ) ); + $query = new WP_Query( array( 's' => $terms ) ); + remove_filter( 'wp_search_stopwords', array( $this, 'filter_wp_search_stopwords' ) ); + + $this->assertNotEquals( array( 'search', 'term' ), $query->get( 'search_terms' ) ); + $this->assertEquals( array( 'This', 'is', 'search', 'term' ), $query->get( 'search_terms' ) ); + } + + function filter_wp_search_stopwords() { + return array(); + } +}