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

Fix --network-wide flag in WP-CLI index command #2771

Merged
merged 5 commits into from
May 18, 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
8 changes: 7 additions & 1 deletion bin/setup-cypress-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'"

Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 4 additions & 2 deletions includes/classes/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions includes/classes/Screen/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
Expand Down
47 changes: 44 additions & 3 deletions tests/cypress/integration/wp-cli.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* global indexNames */

describe('WP-CLI Commands', () => {
let indexAllSitesNames = [];

function checkIfNotMissingIndexes(mode = 'singleSite') {
cy.login();

Expand All @@ -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);
});
});
Expand Down Expand Up @@ -196,17 +198,56 @@ 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(() => {
cy.deactivatePlugin('elasticpress', 'wpCli', 'network');
});

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');
});
Expand Down
4 changes: 1 addition & 3 deletions tests/cypress/support/global-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down