Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tests for Search highlight & Fix issue where EP returns incorrect excerpt #3114

Merged
merged 6 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
}
}
Expand All @@ -275,10 +275,12 @@ 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
* @return string $text - the new excerpt
* @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();

Expand All @@ -290,7 +292,7 @@ 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'] ) . '>' );
Expand Down
224 changes: 210 additions & 14 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3137,24 +3137,24 @@ 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,
),
),
);
$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,
),
),
);
Expand All @@ -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'];

Expand Down Expand Up @@ -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( '<mark class=\'ep-highlight\'>test</mark>', $query->posts[0]->post_content );
$this->assertStringContainsString( '<mark class=\'ep-highlight\'>test</mark>', $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( '<mark class=\'my-custom-class\'>test</mark>', $query->posts[0]->post_content );
$this->assertStringContainsString( '<mark class=\'my-custom-class\'>test</mark>', $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( '<mark class=\'ep-highlight\'>test</mark>', $query->posts[0]->post_title );
$this->assertStringNotContainsString( '<mark class=\'ep-highlight\'>test</mark>', $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 = '<mark class=\'ep-highlight\'>test</mark> 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( '<mark class=\'ep-highlight\'>new</mark> 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 );
}

}