From f8645755c15b6579d45d7fd3ee130d20be9755c5 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 17 May 2022 15:20:50 -0300 Subject: [PATCH 1/5] Effectly use the network-wide flag --- includes/classes/IndexHelper.php | 6 ++++-- includes/classes/Screen/Sync.php | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/includes/classes/IndexHelper.php b/includes/classes/IndexHelper.php index 7221d4bf93..f7cd78cd48 100644 --- a/includes/classes/IndexHelper.php +++ b/includes/classes/IndexHelper.php @@ -133,8 +133,10 @@ protected function build_index_meta() { $global_indexables = $this->filter_indexables( Indexables::factory()->get_all( true, true ) ); $non_global_indexables = $this->filter_indexables( Indexables::factory()->get_all( false, true ) ); - if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { - if ( empty( $this->args['network_wide'] ) || ! is_numeric( $this->args['network_wide'] ) ) { + $is_network_wide = isset( $this->args['network_wide'] ) && ! is_null( $this->args['network_wide'] ); + + if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK && $is_network_wide ) { + if ( ! is_numeric( $this->args['network_wide'] ) ) { $this->args['network_wide'] = 0; } diff --git a/includes/classes/Screen/Sync.php b/includes/classes/Screen/Sync.php index 03515b6f74..235f59c7fc 100644 --- a/includes/classes/Screen/Sync.php +++ b/includes/classes/Screen/Sync.php @@ -94,6 +94,7 @@ public function action_wp_ajax_ep_index() { 'put_mapping' => ! empty( $_REQUEST['put_mapping'] ), 'output_method' => [ $this, 'index_output' ], 'show_errors' => true, + 'network_wide' => 0, ] ); } From ff2ad379d674b6ffb452df69d7a3ccf57c433fcb Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 17 May 2022 15:21:34 -0300 Subject: [PATCH 2/5] Adjust get_index_names, so it only return all sites if network activated --- includes/classes/Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/Command.php b/includes/classes/Command.php index 13a515849b..a3689be3ee 100644 --- a/includes/classes/Command.php +++ b/includes/classes/Command.php @@ -441,7 +441,7 @@ public function get_indexes( $args, $assoc_args ) { * @return array */ protected function get_index_names() { - $sites = ( is_multisite() ) ? Utils\get_sites() : array( array( 'blog_id' => get_current_blog_id() ) ); + $sites = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ? Utils\get_sites() : array( array( 'blog_id' => get_current_blog_id() ) ); $all_indexables = Indexables::factory()->get_all(); From ac2b160d475d6172230ad32b587081cf42cdb569 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 17 May 2022 15:21:50 -0300 Subject: [PATCH 3/5] Simplify index clearing --- tests/cypress/support/global-hooks.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/cypress/support/global-hooks.js b/tests/cypress/support/global-hooks.js index 19d6a0d42a..f46b981735 100644 --- a/tests/cypress/support/global-hooks.js +++ b/tests/cypress/support/global-hooks.js @@ -4,9 +4,7 @@ let setFeatures = false; before(() => { // Clear sync from previous tests. - cy.wpCli( - 'wp eval \'delete_transient( "ep_wpcli_sync" ); delete_option( "ep_index_meta" ); delete_site_transient( "ep_wpcli_sync" ); delete_site_option( "ep_index_meta" );\'', - ); + cy.wpCli('wp elasticpress clear-index'); if (!window.indexNames) { cy.wpCli('wp elasticpress get-indexes').then((wpCliResponse) => { From d5ac214c203d80bee918436c8608f304f6e5be6c Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 17 May 2022 15:22:17 -0300 Subject: [PATCH 4/5] Create new site in Cypress + fix in docker_cid storage --- bin/setup-cypress-env.sh | 8 +++++++- .../wordpress-files/test-mu-plugins/unique-index-name.php | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/bin/setup-cypress-env.sh b/bin/setup-cypress-env.sh index 59df0a096f..a8caabf224 100755 --- a/bin/setup-cypress-env.sh +++ b/bin/setup-cypress-env.sh @@ -61,7 +61,13 @@ fi npm run env run tests-cli "wp core multisite-convert" -# Not sure why, wp-env makes it http://localhost:8889/:8889 +SITES_COUNT=$(npm --silent run env run tests-cli "wp site list --format=count") +if [ $SITES_COUNT -eq 1 ]; then + npm run env run tests-cli "wp site create --slug=second-site --title='Second Site'" + npm --silent run env run tests-cli "wp search-replace localhost/ localhost:8889/ --all-tables" +fi + +# Not sure why, wp-env makes it http://localhost:8889/:8889 (not related to the command above) npm run env run tests-cli "option set home 'http://localhost:8889'" npm run env run tests-cli "option set siteurl 'http://localhost:8889'" diff --git a/tests/cypress/wordpress-files/test-mu-plugins/unique-index-name.php b/tests/cypress/wordpress-files/test-mu-plugins/unique-index-name.php index c922628bba..9c7262392a 100644 --- a/tests/cypress/wordpress-files/test-mu-plugins/unique-index-name.php +++ b/tests/cypress/wordpress-files/test-mu-plugins/unique-index-name.php @@ -26,10 +26,10 @@ * @return string */ function get_docker_cid() { - $docker_cid = get_option( 'ep_tests_docker_cid', '' ); + $docker_cid = get_site_option( 'ep_tests_docker_cid', '' ); if ( ! $docker_cid ) { $docker_cid = exec( 'cat /etc/hostname' ); - update_option( 'ep_tests_docker_cid', $docker_cid ); + update_site_option( 'ep_tests_docker_cid', $docker_cid ); } return $docker_cid; } From 3bc0ced81a392212a78842373a297c3c61f40b5a Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 17 May 2022 15:22:30 -0300 Subject: [PATCH 5/5] Properly test the --network-wide flag --- tests/cypress/integration/wp-cli.spec.js | 47 ++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/tests/cypress/integration/wp-cli.spec.js b/tests/cypress/integration/wp-cli.spec.js index 412cab7c5d..e56f002e9e 100644 --- a/tests/cypress/integration/wp-cli.spec.js +++ b/tests/cypress/integration/wp-cli.spec.js @@ -1,6 +1,8 @@ /* global indexNames */ describe('WP-CLI Commands', () => { + let indexAllSitesNames = []; + function checkIfNotMissingIndexes(mode = 'singleSite') { cy.login(); @@ -19,7 +21,7 @@ describe('WP-CLI Commands', () => { cy.get('.metabox-holder') .invoke('text') .then((text) => { - indexNames.forEach((index) => { + (mode === 'singleSite' ? indexNames : indexAllSitesNames).forEach((index) => { expect(text).to.contains(index); }); }); @@ -196,6 +198,9 @@ describe('WP-CLI Commands', () => { context('multisite parameters', () => { before(() => { cy.activatePlugin('elasticpress', 'wpCli', 'network'); + cy.wpCli('elasticpress get-indexes').then((wpCliResponse) => { + indexAllSitesNames = JSON.parse(wpCliResponse.stdout); + }); }); after(() => { @@ -203,10 +208,46 @@ describe('WP-CLI Commands', () => { }); it('Can index all blogs in network if user specifies --network-wide argument', () => { + // eslint-disable-next-line jest/valid-expect-in-promise cy.wpCli('wp elasticpress index --network-wide') .its('stdout') - .should('contain', 'Indexing posts on site') - .should('contain', 'Number of posts indexed on site'); + .then((output) => { + expect((output.match(/Indexing posts on site/g) || []).length).to.equal(2); + expect( + (output.match(/Number of posts indexed on site/g) || []).length, + ).to.equal(2); + expect(output).to.contain('Network alias created'); + }); + + checkIfNotMissingIndexes('network'); + }); + + it('Can index only current site if user does not specify --network-wide argument', () => { + // eslint-disable-next-line jest/valid-expect-in-promise + cy.wpCli(`wp elasticpress index`) + .its('stdout') + .then((output) => { + expect((output.match(/Indexing posts on site/g) || []).length).to.equal(1); + expect( + (output.match(/Number of posts indexed on site/g) || []).length, + ).to.equal(1); + expect(output).to.not.contain('Network alias created'); + }); + + checkIfNotMissingIndexes('network'); + }); + + it('Can index only site in the --url parameter if user does not specify --network-wide argument', () => { + // eslint-disable-next-line jest/valid-expect-in-promise + cy.wpCli(`wp elasticpress index --url=${Cypress.config('baseUrl')}/second-site`) + .its('stdout') + .then((output) => { + expect((output.match(/Indexing posts on site/g) || []).length).to.equal(1); + expect( + (output.match(/Number of posts indexed on site/g) || []).length, + ).to.equal(1); + expect(output).to.not.contain('Network alias created'); + }); checkIfNotMissingIndexes('network'); });