From 73bd66fff1de140fcfd8b477374c585bf17ebd96 Mon Sep 17 00:00:00 2001 From: Jacob Peattie Date: Fri, 10 Mar 2023 22:03:39 +1100 Subject: [PATCH 1/3] Exclude non-searchable post types from facet post types. --- includes/classes/Feature/InstantResults/InstantResults.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/includes/classes/Feature/InstantResults/InstantResults.php b/includes/classes/Feature/InstantResults/InstantResults.php index 11677dbe70..c0ca856c1d 100644 --- a/includes/classes/Feature/InstantResults/InstantResults.php +++ b/includes/classes/Feature/InstantResults/InstantResults.php @@ -835,9 +835,12 @@ public function get_facets() { $slug ); + $post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types(); + $post_types = array_intersect( $post_types, $taxonomy->object_type ); + $facets[ $name ] = array( 'type' => 'taxonomy', - 'post_types' => $taxonomy->object_type, + 'post_types' => $post_types, 'labels' => array( 'admin' => $admin_label, 'frontend' => $labels->singular_name, From d8af42bc7bf0f10bbba8c08c9280519927ff3403 Mon Sep 17 00:00:00 2001 From: Jacob Peattie Date: Fri, 10 Mar 2023 22:06:55 +1100 Subject: [PATCH 2/3] Use array values. --- includes/classes/Feature/InstantResults/InstantResults.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/classes/Feature/InstantResults/InstantResults.php b/includes/classes/Feature/InstantResults/InstantResults.php index c0ca856c1d..480cb67e1e 100644 --- a/includes/classes/Feature/InstantResults/InstantResults.php +++ b/includes/classes/Feature/InstantResults/InstantResults.php @@ -837,6 +837,7 @@ public function get_facets() { $post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types(); $post_types = array_intersect( $post_types, $taxonomy->object_type ); + $post_types = array_values( $post_types ); $facets[ $name ] = array( 'type' => 'taxonomy', From e89aa26ec64352135f5365e34f49d4ba4c652e5f Mon Sep 17 00:00:00 2001 From: Jacob Peattie Date: Wed, 15 Mar 2023 00:26:38 +1100 Subject: [PATCH 3/3] Test labelling of Instant Results facets. --- .../features/instant-results.cy.js | 30 ++++++++++++++++++- .../test-plugins/cpt-and-custom-tax.php | 17 ++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/tests/cypress/integration/features/instant-results.cy.js b/tests/cypress/integration/features/instant-results.cy.js index c572d69285..116147fe3b 100644 --- a/tests/cypress/integration/features/instant-results.cy.js +++ b/tests/cypress/integration/features/instant-results.cy.js @@ -35,7 +35,7 @@ describe('Instant Results Feature', { tags: '@slow' }, () => { beforeEach(() => { cy.deactivatePlugin( - 'custom-instant-results-template open-instant-results-with-buttons filter-instant-results-per-page', + 'custom-instant-results-template open-instant-results-with-buttons filter-instant-results-per-page cpt-and-custom-tax', 'wpCli', ); cy.login(); @@ -112,6 +112,7 @@ describe('Instant Results Feature', { tags: '@slow' }, () => { * Can change the URL when search term is changed */ it('Can see instant results elements, URL changes, reload, and update after changing search term', () => { + cy.activatePlugin('cpt-and-custom-tax', 'wpCli'); cy.maybeEnableFeature('instant-results'); cy.intercept({ @@ -121,6 +122,25 @@ describe('Instant Results Feature', { tags: '@slow' }, () => { }, }).as('apiRequest'); + /** + * Add product category facet to test the labelling of facets + * with the same name. + */ + cy.intercept('**/wp-admin/admin-ajax.php*').as('ajaxRequest'); + cy.visitAdminPage('admin.php?page=elasticpress'); + cy.get('.ep-feature-instant-results .settings-button').click(); + cy.get('.ep-feature-instant-results .components-form-token-field__input').type( + 'cat{downArrow}{enter}', + ); + cy.get('.ep-feature-instant-results .components-form-token-field__input').type( + 'prod{downArrow}{enter}{esc}', + ); + cy.get('.ep-feature-instant-results .button-primary').click(); + cy.wait('@ajaxRequest'); + + /** + * Perform a search. + */ cy.visit('/'); cy.get('.wp-block-search').last().as('searchBlock'); @@ -136,6 +156,14 @@ describe('Instant Results Feature', { tags: '@slow' }, () => { // Show the number of results cy.get('@searchModal').find('.ep-search-results__title').contains(/\d+/); + /** + * The Category facet should specify its searchable post types. + */ + cy.get('.ep-search-panel__button').contains('Category').as('categoryFacet'); + cy.get('@categoryFacet').should('contain', 'Posts'); + cy.get('@categoryFacet').should('contain', 'Movies'); + cy.get('@categoryFacet').should('not.contain', 'Albums'); + cy.get('.ep-search-sidebar #ep-search-post-type-post') .click() .then(() => { diff --git a/tests/cypress/wordpress-files/test-plugins/cpt-and-custom-tax.php b/tests/cypress/wordpress-files/test-plugins/cpt-and-custom-tax.php index 80e16ab030..1efcbac578 100644 --- a/tests/cypress/wordpress-files/test-plugins/cpt-and-custom-tax.php +++ b/tests/cypress/wordpress-files/test-plugins/cpt-and-custom-tax.php @@ -12,7 +12,7 @@ namespace ElasticPress\Tests\E2e; /** - * Create a CPT called "Movies". + * Create a CPT called "Movies" and a non-searchable CPT called "Group". */ function create_post_type() { register_post_type( @@ -24,6 +24,21 @@ function create_post_type() { ], 'public' => true, 'has_archive' => true, + 'taxonomies' => [ 'category' ], + ] + ); + + register_post_type( + 'group', + [ + 'labels' => [ + 'name' => __( 'Albums' ), + 'singular_name' => __( 'Album' ), + ], + 'exclude_from_search' => true, + 'has_archive' => true, + 'public' => true, + 'taxonomies' => [ 'category' ], ] ); }