From f833346ff5731fdb5fb8957a6be5c56937623275 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 28 Oct 2022 20:28:29 +0500 Subject: [PATCH 1/4] Add Tests for Search highlight --- includes/classes/Feature/Search/Search.php | 9 +- tests/php/indexables/TestPost.php | 224 +++++++++++++++++++-- 2 files changed, 215 insertions(+), 18 deletions(-) diff --git a/includes/classes/Feature/Search/Search.php b/includes/classes/Feature/Search/Search.php index 1b7c519c3d..cf473e5227 100644 --- a/includes/classes/Feature/Search/Search.php +++ b/includes/classes/Feature/Search/Search.php @@ -266,7 +266,7 @@ public function allow_excerpt_html() { if ( ! empty( $settings['highlight_excerpt'] ) && true === $settings['highlight_excerpt'] ) { remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' ); - add_filter( 'get_the_excerpt', [ $this, 'ep_highlight_excerpt' ] ); + add_filter( 'get_the_excerpt', [ $this, 'ep_highlight_excerpt' ], 10, 2 ); add_filter( 'ep_highlighting_fields', [ $this, 'ep_highlight_add_excerpt_field' ] ); } } @@ -276,9 +276,11 @@ public function allow_excerpt_html() { * logic for the excerpt filter allowing the currently selected tag. * * @param string $text - excerpt string + * @param WP_POST $post - Post Object. + * * @return string $text - the new excerpt */ - public function ep_highlight_excerpt( $text ) { + public function ep_highlight_excerpt( $text, $post ) { $settings = $this->get_settings(); @@ -290,11 +292,10 @@ public function ep_highlight_excerpt( $text ) { // reproduces wp_trim_excerpt filter, preserving the excerpt_more and excerpt_length filters if ( '' === $text ) { - $text = get_the_content( '' ); + $text = get_the_content( '', false, $post ); $text = apply_filters( 'the_content', $text ); $text = str_replace( '\]\]\>', ']]>', $text ); $text = strip_tags( $text, '<' . esc_html( $settings['highlight_tag'] ) . '>' ); - // use the defined length, if already applied... $excerpt_length = apply_filters( 'excerpt_length', 55 ); diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index 1a5c949f81..bd83c09fba 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -3137,15 +3137,15 @@ public function testMetaQueryOrRelationWithSort() { $this->ep_factory->post->create( array( 'post_content' => 'the post content findme', - 'meta_input' => array( 'test_key' => date('Ymd') - 5 ), + 'meta_input' => array( 'test_key' => date( 'Ymd' ) - 5 ), ), ); $this->ep_factory->post->create( array( 'post_content' => 'the post content findme', 'meta_input' => array( - 'test_key' => date('Ymd') + 5, - 'test_key2' => date('Ymd') + 6, + 'test_key' => date( 'Ymd' ) + 5, + 'test_key2' => date( 'Ymd' ) + 6, ), ), ); @@ -3153,8 +3153,8 @@ public function testMetaQueryOrRelationWithSort() { array( 'post_content' => 'the post content findme', 'meta_input' => array( - 'test_key' => date('Ymd') + 5, - 'test_key2' => date('Ymd') + 6, + 'test_key' => date( 'Ymd' ) + 5, + 'test_key2' => date( 'Ymd' ) + 6, ), ), ); @@ -3163,28 +3163,28 @@ public function testMetaQueryOrRelationWithSort() { ElasticPress\Elasticsearch::factory()->refresh_indices(); $args = array( 'ep_integrate' => true, - 'meta_key' => 'test_key', - 'meta_query' => array( + 'meta_key' => 'test_key', + 'meta_query' => array( 'relation' => 'or', array( 'key' => 'test_key', - 'value' => date('Ymd'), + 'value' => date( 'Ymd' ), 'compare' => '<=', - 'type' => 'NUMERIC', + 'type' => 'NUMERIC', ), array( 'key' => 'test_key2', - 'value' => date('Ymd'), + 'value' => date( 'Ymd' ), 'compare' => '>=', - 'type' => 'NUMERIC', + 'type' => 'NUMERIC', ), ), - 'orderby' => 'meta_value_num', - 'order' => 'ASC', + 'orderby' => 'meta_value_num', + 'order' => 'ASC', ); $query = new \WP_Query( $args ); - $args = $post->format_args($args, new \WP_Query() ); + $args = $post->format_args( $args, new \WP_Query() ); $outer_must = $args['post_filter']['bool']['must'][0]['bool']['must']; @@ -7590,4 +7590,200 @@ public function testGetAllDistinctValues() { $this->assertContains( 'lorem', $distinct_values_2 ); $this->assertContains( 'ipsum', $distinct_values_2 ); } + + /** + * Tests search term wrapped in html tags. + */ + public function testHighlightTags() { + + ElasticPress\Features::factory()->update_feature( + 'search', + array( + 'active' => true, + 'highlight_enabled' => true, + ) + ); + + $this->ep_factory->post->create( + array( + 'post_content' => 'test content', + 'post_title' => 'test title', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $args = array( + 's' => 'test', + ); + $query = new \WP_Query( $args ); + + $this->assertStringContainsString( 'test', $query->posts[0]->post_content ); + $this->assertStringContainsString( 'test', $query->posts[0]->post_title ); + + // bypass the highlighting the search term + add_filter( 'ep_highlight_should_add_clause', '__return_false' ); + + $query = new \WP_Query( $args ); + + $this->assertEquals( 'test content', $query->posts[0]->post_content ); + $this->assertEquals( 'test title', $query->posts[0]->post_title ); + + remove_filter( 'ep_highlight_should_add_clause', '__return_false' ); + + } + + /** + * Tests search term is wrapped in html tag with custom class + */ + public function testHighlightTagsWithCustomClass() { + + ElasticPress\Features::factory()->update_feature( + 'search', + array( + 'active' => true, + 'highlight_enabled' => true, + ) + ); + + $this->ep_factory->post->create( + array( + 'post_content' => 'test content', + 'post_title' => 'test title', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + add_filter( + 'ep_highlighting_class', + function( $class ) { + return 'my-custom-class'; + } + ); + + $args = array( + 's' => 'test', + ); + $query = new \WP_Query( $args ); + + $this->assertStringContainsString( 'test', $query->posts[0]->post_content ); + $this->assertStringContainsString( 'test', $query->posts[0]->post_title ); + + } + + /** + * Tests search term is wrapped in html tag only for tite. + */ + public function testHighlightTagsOnlyForTitle() { + + ElasticPress\Features::factory()->update_feature( + 'search', + array( + 'active' => true, + 'highlight_enabled' => true, + ) + ); + + $this->ep_factory->post->create( + array( + 'post_content' => 'test content', + 'post_title' => 'test title', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + add_filter( + 'ep_highlighting_fields', + function( $fields ) { + return array( 'post_title' ); + } + ); + + $args = array( + 's' => 'test', + ); + $query = new \WP_Query( $args ); + + $this->assertStringContainsString( 'test', $query->posts[0]->post_title ); + $this->assertStringNotContainsString( 'test', $query->posts[0]->post_content ); + } + + /** + * Test get_the_excerpt() has HTML tags when highlight_excerpt is enabled. + */ + public function testExcerptHasHiglightHTMLTags() { + + ElasticPress\Features::factory()->update_feature( + 'search', + array( + 'active' => true, + 'highlight_enabled' => true, + 'highlight_excerpt' => true, + ) + ); + + $this->ep_factory->post->create( array( 'post_excerpt' => 'test excerpt' ) ); + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $args = array( + 's' => 'test', + ); + $query = new \WP_Query( $args ); + + $expected_result = 'test excerpt'; + $this->assertEquals( $expected_result, $query->posts[0]->post_excerpt ); + $this->assertEquals( $expected_result, get_the_excerpt( $query->posts[0] ) ); + + // test post without excerpt + $this->ep_factory->post->create( array( 'post_content' => 'new post', 'post_excerpt' => '' ) ); + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $args = array( + 's' => 'new', + ); + $query = new \WP_Query( $args ); + + // using StringContainsString because the_content filter adds the break line. + $this->assertStringContainsString( 'new post', get_the_excerpt( $query->posts[0] ) ); + } + + /** + * Tests highlight parameters are not added to the query when search term is empty. + */ + public function testHighlightTagsNotSetWhenSearchIsEmpty() { + + ElasticPress\Features::factory()->update_feature( + 'search', + array( + 'active' => true, + 'highlight_enabled' => true, + ) + ); + + $this->ep_factory->post->create( array( 'post_content' => 'test content' ) ); + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + add_action( + 'pre_http_request', + function( $preempt, $parsed_args, $url ) { + + $body = json_decode( $parsed_args['body'], true ); + $this->assertArrayNotHasKey( 'highlight', $body ); + return $preempt; + }, + 10, + 3 + ); + + $args = array( + 's' => '', + 'ep_integrate' => true, + ); + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + } + } From d690a98de0ce821631332dbdfce5fb60bd8d30d1 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Mon, 7 Nov 2022 15:26:03 +0500 Subject: [PATCH 2/4] Minor change --- includes/classes/Feature/Search/Search.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/classes/Feature/Search/Search.php b/includes/classes/Feature/Search/Search.php index cf473e5227..f358f84f19 100644 --- a/includes/classes/Feature/Search/Search.php +++ b/includes/classes/Feature/Search/Search.php @@ -296,6 +296,7 @@ public function ep_highlight_excerpt( $text, $post ) { $text = apply_filters( 'the_content', $text ); $text = str_replace( '\]\]\>', ']]>', $text ); $text = strip_tags( $text, '<' . esc_html( $settings['highlight_tag'] ) . '>' ); + // use the defined length, if already applied... $excerpt_length = apply_filters( 'excerpt_length', 55 ); From 377b7b655ba61c026d245ce9c89f9f4756802891 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Mon, 7 Nov 2022 15:28:22 +0500 Subject: [PATCH 3/4] Fix linting issue --- includes/classes/Feature/Search/Search.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/Feature/Search/Search.php b/includes/classes/Feature/Search/Search.php index f358f84f19..79958db3e9 100644 --- a/includes/classes/Feature/Search/Search.php +++ b/includes/classes/Feature/Search/Search.php @@ -275,7 +275,7 @@ public function allow_excerpt_html() { * Called by allow_excerpt_html * logic for the excerpt filter allowing the currently selected tag. * - * @param string $text - excerpt string + * @param string $text - excerpt string * @param WP_POST $post - Post Object. * * @return string $text - the new excerpt From e139dfb766ea9fd4f7c2cb45457830d8ce40a604 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Tue, 8 Nov 2022 19:01:49 +0500 Subject: [PATCH 4/4] Feedback adjust --- includes/classes/Feature/Search/Search.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/classes/Feature/Search/Search.php b/includes/classes/Feature/Search/Search.php index 79958db3e9..f6f2dc7bc3 100644 --- a/includes/classes/Feature/Search/Search.php +++ b/includes/classes/Feature/Search/Search.php @@ -275,10 +275,10 @@ public function allow_excerpt_html() { * Called by allow_excerpt_html * logic for the excerpt filter allowing the currently selected tag. * - * @param string $text - excerpt string - * @param WP_POST $post - Post Object. + * @param string $text excerpt string + * @param WP_Post $post Post Object * - * @return string $text - the new excerpt + * @return string $text the new excerpt */ public function ep_highlight_excerpt( $text, $post ) {