diff --git a/tests/cypress/integration/dashboard-sync.spec.js b/tests/cypress/integration/dashboard-sync.spec.js index e00dbf3c53..f946764a65 100644 --- a/tests/cypress/integration/dashboard-sync.spec.js +++ b/tests/cypress/integration/dashboard-sync.spec.js @@ -76,7 +76,7 @@ describe('Dashboard Sync', () => { // Sync and remove, so EP doesn't think it is a fresh install. cy.wpCli('wp elasticpress index --setup --yes'); - cy.wpCli('wp elasticpress delete-index --yes'); + cy.wpCli('wp elasticpress delete-index --yes --network-wide'); cy.visitAdminPage('network/admin.php?page=elasticpress-health'); cy.get('.wrap').should( diff --git a/tests/cypress/integration/features/facets.spec.js b/tests/cypress/integration/features/facets.spec.js index d934bb9ccb..77035816c1 100644 --- a/tests/cypress/integration/features/facets.spec.js +++ b/tests/cypress/integration/features/facets.spec.js @@ -6,58 +6,65 @@ describe('Facets Feature', () => { * @param {string} category The category slug. */ function createWidget(title, category) { - cy.intercept('/wp-json/wp/v2/widget-types/*/encode*').as('legacyWidgets'); cy.openWidgetsPage(); - cy.get('.edit-widgets-header-toolbar__inserter-toggle').click(); - cy.get('.block-editor-inserter__panel-content [class*="legacy-widget/ep-facet"]').click({ - force: true, + let createdWidgetsLength = 0; + + cy.get('.is-opened').then(($openedWidgetArea) => { + createdWidgetsLength = $openedWidgetArea.find('.wp-block-legacy-widget').length; + + cy.get('.edit-widgets-header-toolbar__inserter-toggle').click(); + cy.get('.block-editor-inserter__panel-content [class*="legacy-widget/ep-facet"]') + .click({ + force: true, + }) + .then(() => { + cy.get( + `.is-opened .wp-block-legacy-widget:eq(${createdWidgetsLength}) .wp-block-legacy-widget__edit-form:visible .widefat:visible`, + { timeout: 10000 }, + ) + .closest('.widget-ep-facet') + .within(() => { + cy.get('input[name^="widget-ep-facet"][name$="[title]"]').clearThenType( + title, + true, + ); + cy.get('select[name^="widget-ep-facet"][name$="[facet]"]').select( + category, + ); + }); + + /** + * Wait for WordPress to recognize the title typed. + * + * @todo investigate why this is needed. + */ + // eslint-disable-next-line cypress/no-unnecessary-waiting + cy.wait(2000); + + cy.get('.edit-widgets-header__actions .components-button.is-primary').click(); + cy.get('body').should('contain.text', 'Widgets saved.'); + }); }); - cy.wait('@legacyWidgets'); - // eslint-disable-next-line cypress/no-unnecessary-waiting -- JS processing - cy.wait(1000); - - cy.get('.is-opened .widget-ep-facet') - .last() - .within(() => { - cy.get('input[name^="widget-ep-facet"][name$="[title]"]').clearThenType( - title, - true, - ); - cy.get('select[name^="widget-ep-facet"][name$="[facet]"]').select(category); - }); - - /** - * Wait for WordPress to recognize the title typed. - * - * @todo investigate why this is needed. - */ - // eslint-disable-next-line cypress/no-unnecessary-waiting - cy.wait(2000); - - cy.get('.edit-widgets-header__actions .components-button.is-primary').click(); - cy.get('body').should('contain.text', 'Widgets saved.'); } before(() => { cy.maybeEnableFeature('facets'); - cy.wpCli('widget reset --all'); cy.wpCli('elasticpress index --setup --yes'); - - // Initial widget that will be used for all tests. - createWidget('Facet (categories)', 'category'); }); it('Can see the widget in the frontend', () => { + cy.wpCli('widget reset --all'); + + createWidget('Facet (categories)', 'category'); + cy.visit('/'); - // Check if the widget is visible. + // Check if the first widget is visible. cy.get('.widget_ep-facet').should('be.visible'); cy.contains('.widget-title', 'Facet (categories)').should('be.visible'); - }); - it('Can use widgets', () => { // Create a second widget, so we can test both working together. createWidget('Facet (Tags)', 'post_tag'); diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index 1983e0204d..233db50237 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -158,37 +158,7 @@ Cypress.Commands.add('publishPost', (postData) => { }); Cypress.Commands.add('updateFeatures', (newFeaturesValues = {}) => { - const features = { - search: { - active: 1, - highlight_enabled: true, - highlight_excerpt: true, - highlight_tag: 'mark', - highlight_color: '#157d84', - }, - related_posts: { - active: 1, - }, - facets: { - active: 1, - }, - searchordering: { - active: 1, - }, - autosuggest: { - active: 1, - }, - woocommerce: { - active: 0, - }, - protected_content: { - active: 0, - }, - users: { - active: 0, - }, - ...newFeaturesValues, - }; + const features = Object.assign({}, cy.elasticPress.defaultFeatures, ...newFeaturesValues); const escapedFeatures = JSON.stringify(features); diff --git a/tests/cypress/support/global-hooks.js b/tests/cypress/support/global-hooks.js index f46b981735..f52f467b95 100644 --- a/tests/cypress/support/global-hooks.js +++ b/tests/cypress/support/global-hooks.js @@ -1,41 +1,29 @@ window.indexNames = null; -let setFeatures = false; - before(() => { - // Clear sync from previous tests. - cy.wpCli('wp elasticpress clear-index'); + cy.wpCliEval( + ` + // Clear any stuck sync process. + \\ElasticPress\\IndexHelper::factory()->clear_index_meta(); - if (!window.indexNames) { - cy.wpCli('wp elasticpress get-indexes').then((wpCliResponse) => { - window.indexNames = JSON.parse(wpCliResponse.stdout); - }); - } + $features = json_decode( '${JSON.stringify(cy.elasticPress.defaultFeatures)}', true ); + + if ( ! \\ElasticPress\\Utils\\is_epio() ) { + $host = \\ElasticPress\\Utils\\get_host(); + $host = str_replace( '172.17.0.1', 'localhost', $host ); + $index_name = \\ElasticPress\\Indexables::factory()->get( 'post' )->get_index_name(); + $as_endpoint_url = $host . $index_name . '/_search'; + + $features['autosuggest']['endpoint_url'] = $as_endpoint_url; + } - if (!setFeatures) { - cy.wpCliEval( - `if ( \\ElasticPress\\Utils\\is_epio() ) { - exit; - } - $host = \\ElasticPress\\Utils\\get_host(); - $host = str_replace( '172.17.0.1', 'localhost', $host ); - $index_name = \\ElasticPress\\Indexables::factory()->get( 'post' )->get_index_name(); - echo $host . $index_name . '/_search'; - `, - ).then((searchEndpointUrl) => { - if (searchEndpointUrl.stdout === '') { - cy.updateFeatures(); - } else { - cy.updateFeatures({ - autosuggest: { - active: 1, - endpoint_url: searchEndpointUrl.stdout, - }, - }); - } - }); - setFeatures = true; - } + update_option( 'ep_feature_settings', $features ); + + WP_CLI::runcommand('elasticpress get-indexes'); + `, + ).then((wpCliResponse) => { + window.indexNames = JSON.parse(wpCliResponse.stdout); + }); }); afterEach(() => { diff --git a/tests/cypress/support/index.js b/tests/cypress/support/index.js index c041045886..f42dec1025 100644 --- a/tests/cypress/support/index.js +++ b/tests/cypress/support/index.js @@ -19,3 +19,36 @@ import './global-hooks'; // Alternatively you can use CommonJS syntax: // require('./commands') + +cy.elasticPress = { + defaultFeatures: { + search: { + active: 1, + highlight_enabled: true, + highlight_excerpt: true, + highlight_tag: 'mark', + highlight_color: '#157d84', + }, + related_posts: { + active: 1, + }, + facets: { + active: 1, + }, + searchordering: { + active: 1, + }, + autosuggest: { + active: 1, + }, + woocommerce: { + active: 0, + }, + protected_content: { + active: 0, + }, + users: { + active: 0, + }, + }, +};