From 634788cc6a25041493c1a37335efe38916a020ba Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Thu, 4 Apr 2024 15:09:24 -0300 Subject: [PATCH 1/3] FIx post type addition --- .../classes/Feature/Documents/Documents.php | 26 +++------- tests/php/features/TestDocuments.php | 47 +++++++++++++++++++ 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/includes/classes/Feature/Documents/Documents.php b/includes/classes/Feature/Documents/Documents.php index 29ee7352e..f7e0d552b 100644 --- a/includes/classes/Feature/Documents/Documents.php +++ b/includes/classes/Feature/Documents/Documents.php @@ -128,8 +128,14 @@ public function setup_document_search( $query ) { return; } + // Return if attachments are not involved in the query. + // If post_type is empty, attachments will be included automatically. + $post_type = (array) $query->get( 'post_type', [] ); + if ( ! empty( $post_type ) && ! in_array( 'attachment', $post_type, true ) ) { + return; + } + $this->maybe_set_post_status( $query ); - $this->maybe_set_post_type( $query ); $this->maybe_set_mime_type( $query ); } @@ -523,24 +529,6 @@ protected function maybe_set_post_status( $query ) { $query->set( 'post_status', array_unique( $post_status ) ); } - /** - * Add the attachment post type to post_type. - * - * @param WP_Query $query WP_Query to modify to search. - * @return void - */ - protected function maybe_set_post_type( $query ) { - $post_type = $query->get( 'post_type', [] ); - if ( empty( $post_type ) || ! is_string( $post_type ) || 'any' === $post_type ) { - return; - } - - $post_type = explode( ' ', $post_type ); - $post_type[] = 'attachment'; - - $query->set( 'post_type', array_unique( $post_type ) ); - } - /** * Add allowed mime types. If mime types are already set, append. * diff --git a/tests/php/features/TestDocuments.php b/tests/php/features/TestDocuments.php index ee9b85036..cca28ce3c 100644 --- a/tests/php/features/TestDocuments.php +++ b/tests/php/features/TestDocuments.php @@ -300,4 +300,51 @@ public function test_ep_allowed_documents_ingest_mime_types_filter() { $this->assertSame( 'text/test', $feature->get_allowed_ingest_mime_types()['test'] ); } + + /** + * Depending on the WP_Query post_type parameter, attachments should be added by default. + * + * @since 5.1.0 + * @group documents + */ + public function test_empty_post_type() { + $this->ep_factory->post->create( + array( + 'post_title' => 'findme', + 'post_type' => 'post', + ) + ); + $this->ep_factory->post->create( + array( + 'post_title' => 'findme', + 'post_type' => 'attachment', + 'post_mime_type' => 'image', + ) + ); + + // No post type, attachment added by default + $query = new \WP_Query( [ 's' => 'findme' ] ); + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 2, $query->post_count ); + + // Post type as string, attachment not added by default + $query = new \WP_Query( + [ + 'post_type' => 'post', + 's' => 'findme', + ] + ); + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 1, $query->post_count ); + + // Post type as array, attachment not added by default + $query = new \WP_Query( + [ + 'post_type' => [ 'post' ], + 's' => 'findme', + ] + ); + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 1, $query->post_count ); + } } From 402f6c6c52b1938b2bc7b599b9135a0e1b873a21 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 10 Apr 2024 10:48:44 -0300 Subject: [PATCH 2/3] fix unit test --- tests/php/features/TestDocuments.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/php/features/TestDocuments.php b/tests/php/features/TestDocuments.php index cca28ce3c..eb3525a5d 100644 --- a/tests/php/features/TestDocuments.php +++ b/tests/php/features/TestDocuments.php @@ -308,6 +308,10 @@ public function test_ep_allowed_documents_ingest_mime_types_filter() { * @group documents */ public function test_empty_post_type() { + ElasticPress\Features::factory()->activate_feature( 'search' ); + ElasticPress\Features::factory()->activate_feature( 'documents' ); + ElasticPress\Features::factory()->setup_features(); + $this->ep_factory->post->create( array( 'post_title' => 'findme', @@ -318,10 +322,12 @@ public function test_empty_post_type() { array( 'post_title' => 'findme', 'post_type' => 'attachment', - 'post_mime_type' => 'image', + 'post_mime_type' => 'application/msword', ) ); + ElasticPress\Elasticsearch::factory()->refresh_indices(); + // No post type, attachment added by default $query = new \WP_Query( [ 's' => 'findme' ] ); $this->assertTrue( $query->elasticsearch_success ); From 48037ce4fda229bfa76c43f3df3b9b6110c47191 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 10 Apr 2024 11:19:27 -0300 Subject: [PATCH 3/3] Reset posts for facets tests --- tests/php/features/TestFacetTypeDate.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/php/features/TestFacetTypeDate.php b/tests/php/features/TestFacetTypeDate.php index f3a3df3f8..65d5379aa 100644 --- a/tests/php/features/TestFacetTypeDate.php +++ b/tests/php/features/TestFacetTypeDate.php @@ -26,6 +26,10 @@ class TestFacetTypeDate extends BaseTestCase { * Setup each test. */ public function set_up() { + ElasticPress\Elasticsearch::factory()->delete_all_indices(); + ElasticPress\Indexables::factory()->get( 'post' )->put_mapping(); + ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue(); + $facet_feature = Features::factory()->get_registered_feature( 'facets' ); $this->facet_type = $facet_feature->types['date']; @@ -332,6 +336,7 @@ public function testQueryPost() { 'ep_integrate' => true, ] ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 2, $query->found_posts ); // get all posts published on or after 2022-01-01. @@ -341,6 +346,7 @@ public function testQueryPost() { 'ep_integrate' => true, ] ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 4, $query->found_posts ); // get all posts published on or before 2022-01-01. @@ -350,6 +356,7 @@ public function testQueryPost() { 'ep_integrate' => true, ] ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 2, $query->found_posts ); // passing invalid date shouldn't apply any filter. @@ -359,6 +366,7 @@ public function testQueryPost() { 'ep_integrate' => true, ] ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 5, $query->found_posts ); } }