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

Add confirmation for destructive WP-CLI commands #2120

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 11 additions & 3 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public function get_indexes( $args, $assoc_args ) {
* Delete the index for each indexable. !!Warning!! This removes your elasticsearch index(s)
* for the entire site.
*
* @synopsis [--index-name] [--network-wide]
* @synopsis [--index-name] [--network-wide] [--yes]
* @subcommand delete-index
* @since 0.9
* @param array $args Positional CLI args.
Expand All @@ -376,6 +376,8 @@ public function delete_index( $args, $assoc_args ) {
$this->connect_check();
$this->index_occurring();

WP_CLI::confirm( esc_html__( 'Are you sure you want to delete your Elasticsearch index?' ), $assoc_args );

// If index name is specified, just delete it and end the command.
if ( ! empty( $assoc_args['index-name'] ) ) {
$result = Elasticsearch::factory()->delete_index( $assoc_args['index-name'] );
Expand Down Expand Up @@ -535,7 +537,7 @@ public function delete_transient_on_int( $signal_no ) {
/**
* Index all posts for a site or network wide
*
* @synopsis [--setup] [--network-wide] [--per-page] [--nobulk] [--show-errors] [--offset] [--indexables] [--show-bulk-errors] [--show-nobulk-errors] [--post-type] [--include] [--post-ids] [--ep-host] [--ep-prefix]
* @synopsis [--setup] [--network-wide] [--per-page] [--nobulk] [--show-errors] [--offset] [--indexables] [--show-bulk-errors] [--show-nobulk-errors] [--post-type] [--include] [--post-ids] [--ep-host] [--ep-prefix] [--yes]
*
* @param array $args Positional CLI args.
* @since 0.1.2
Expand All @@ -544,6 +546,12 @@ public function delete_transient_on_int( $signal_no ) {
public function index( $args, $assoc_args ) {
global $wp_actions;

$setup_option = isset( $assoc_args['setup'] ) ? $assoc_args['setup'] : false;

if ( true === $setup_option ) {
WP_CLI::confirm( esc_html__( 'Indexing with setup option needs to delete Elasticsearch index first, are you sure you want to delete your Elasticsearch index?' ), $assoc_args );
}

if ( ! function_exists( 'pcntl_signal' ) ) {
WP_CLI::warning( esc_html__( 'Function pcntl_signal not available. Make sure to run `wp elasticpress clear-index` in case the process is killed.' ) );
} else {
Expand Down Expand Up @@ -605,7 +613,7 @@ public function index( $args, $assoc_args ) {
}

// Run setup if flag was passed.
if ( isset( $assoc_args['setup'] ) && true === $assoc_args['setup'] ) {
if ( true === $setup_option ) {

// Right now setup is just the put_mapping command, as this also deletes the index(s) first.
if ( ! $this->put_mapping_helper( $args, $assoc_args ) ) {
Expand Down
8 changes: 4 additions & 4 deletions tests/wpa/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testDashboardSyncComplete() {
* @testdox I can search on the front end and ES returns a proper response code.
*/
public function testSearch() {
$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$I = $this->openBrowserPage();

Expand All @@ -72,7 +72,7 @@ public function testSearch() {
* @testdox I dont see a post in search that only matches by title when title is set as not searchable in the weighting dashboard.
*/
public function testWeightingOnOff() {
$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$I = $this->openBrowserPage();

Expand Down Expand Up @@ -118,7 +118,7 @@ public function testWeightingOnOff() {
* @testdox I can increase post_title weighting and influence search results.
*/
public function testTitleContentWeighting() {
$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$I = $this->openBrowserPage();

Expand Down Expand Up @@ -176,7 +176,7 @@ public function testTitleContentWeighting() {
* @testdox When I type in a search field on the front end, I see the autosuggest dropdown.
*/
public function testAutosuggestDropdownShows() {
$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$I = $this->openBrowserPage();

Expand Down
12 changes: 6 additions & 6 deletions tests/wpa/DashboardSyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function testClickSyncButtonSinglesite() {

$I->loginAs( 'wpsnapshots' );

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$this->runCommand( 'wp elasticpress delete-index' );
$this->runCommand( 'wp elasticpress delete-index --yes' );

$I->moveTo( 'wp-admin/admin.php?page=elasticpress-health' );

Expand Down Expand Up @@ -55,9 +55,9 @@ public function testClickSyncButtonMultisite() {

$this->activatePlugin( $I, 'elasticpress', true );

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$this->runCommand( 'wp elasticpress delete-index --network-wide' );
$this->runCommand( 'wp elasticpress delete-index --network-wide --yes' );

$I->moveTo( 'wp-admin/network/sites.php' );

Expand Down Expand Up @@ -91,9 +91,9 @@ public function testResumeSync() {

$I->loginAs( 'wpsnapshots' );

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$this->runCommand( 'wp elasticpress delete-index' );
$this->runCommand( 'wp elasticpress delete-index --yes' );

$I->moveTo( 'wp-admin/admin.php?page=elasticpress-health' );

Expand Down
2 changes: 1 addition & 1 deletion tests/wpa/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function testUnsupportedElasticsearchVersion() {
return;
}

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$this->deactivatePlugin( $I );

Expand Down
2 changes: 1 addition & 1 deletion tests/wpa/StandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function setUp() {
if ( ! $initialized ) {
$initialized = true;

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );
}
}

Expand Down
13 changes: 9 additions & 4 deletions tests/wpa/WpCliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function testIndexCommandWithNetworkWide() {
*/
public function testIndexCommandWithSetup() {
$cli_result = $this->runCommand( 'wp elasticpress index --setup' )['stdout'];
$this->assertStringContainsString( 'Indexing with setup option needs to delete Elasticsearch index first, are you sure you want to delete your Elasticsearch index?', $cli_result );

$cli_result = $this->runCommand( 'wp elasticpress index --setup --yes' )['stdout'];

$this->assertStringContainsString( 'Mapping sent', $cli_result );

Expand Down Expand Up @@ -174,7 +177,9 @@ public function testDeleteIndexCommand() {
$I->loginAs( 'wpsnapshots' );

$cli_result = $this->runCommand( 'wp elasticpress delete-index' )['stdout'];
$this->assertStringContainsString( 'Are you sure you want to delete your Elasticsearch index?', $cli_result );

$cli_result = $this->runCommand( 'wp elasticpress delete-index --yes' )['stdout'];
$this->assertStringContainsString( 'Index deleted', $cli_result );

$I->moveTo( 'wp-admin/admin.php?page=elasticpress-health' );
Expand All @@ -200,7 +205,7 @@ public function testDeleteIndexCommandWithNetworkWide() {

$I->moveTo( 'wp-admin/network/admin.php?page=elasticpress-health' );

$cli_result = $this->runCommand( 'wp elasticpress delete-index --network-wide' )['stdout'];
$cli_result = $this->runCommand( 'wp elasticpress delete-index --network-wide --yes' )['stdout'];

$this->assertStringContainsString( 'Index deleted', $cli_result );

Expand Down Expand Up @@ -240,7 +245,7 @@ public function testPutMappingCommandWithNetworkWide() {

$I->checkOptions( '.index-toggle' );

$this->runCommand( 'wp elasticpress delete-index --network-wide' );
$this->runCommand( 'wp elasticpress delete-index --network-wide --yes' );

$cli_result = $this->runCommand( 'wp elasticpress put-mapping --network-wide' )['stdout'];

Expand Down Expand Up @@ -307,13 +312,13 @@ public function testListFeaturesCommand() {
* @testdox If user runs wp elasticpress stats command, it should return the number of documents indexed and index size.
*/
public function testStatsCommand() {
$this->runCommand( 'wp elasticpress delete-index' );
$this->runCommand( 'wp elasticpress delete-index --yes' );

$cli_result = $this->runCommand( 'wp elasticpress stats' )['stdout'];

$this->assertStringContainsString( 'is not currently indexed', $cli_result );

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$cli_result = $this->runCommand( 'wp elasticpress stats' )['stdout'];

Expand Down
2 changes: 1 addition & 1 deletion tests/wpa/after-test/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testDeleteIndexes() {

foreach ( (array) $cluster_indexes as $index ) {
if ( $docker_cid && false !== strpos( $index['index'], $docker_cid) ) {
$this->runCommand( 'wp elasticpress delete-index --index-name=' . $index['index'] );
$this->runCommand( 'wp elasticpress delete-index --yes --index-name=' . $index['index'] );
}
}
$this->assertTrue(true);
Expand Down
8 changes: 4 additions & 4 deletions tests/wpa/features/AutosuggestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FeatureAutosuggestTest extends TestBase {
* @testdox If the user types a post title in the search box, a drop-down appears with that post.
*/
public function testSeeAutosuggestDropdown() {
$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$I = $this->openBrowserPage();

Expand All @@ -38,7 +38,7 @@ public function testSeeAutosuggestDropdown() {
* @testdox If the user types a post title in the search box, a drop-down appears with that post.
*/
public function testSeeTypedPostTitleInDropdown() {
$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$I = $this->openBrowserPage();

Expand Down Expand Up @@ -75,7 +75,7 @@ public function testSearchForPostByCategory() {
],
] );

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$I = $this->openBrowserPage();

Expand All @@ -100,7 +100,7 @@ public function testSearchForPostByCategory() {
* @testdox If a user clicks a post in the autosuggest drop down, they are taken directly to the post.
*/
public function testClickSuggestionGoToPost() {
$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$I = $this->openBrowserPage();

Expand Down
2 changes: 1 addition & 1 deletion tests/wpa/features/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testSearchPdfAfterCliIndexSetup() {

$this->activateDocumentFeature( $I );

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$this->uploadFile( $I, dirname( __DIR__ ) . '/test-docs/pdf-file.pdf' );

Expand Down
2 changes: 1 addition & 1 deletion tests/wpa/features/ProtectedContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testProtectedContentPostsList() {
* @testdox I see 2 hits as in ES query results on WordPress Dashboard -> Draft Posts List Screen.
*/
public function testProtectedContentPostsDraftsList() {
$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$I = $this->openBrowserPage();

Expand Down
6 changes: 3 additions & 3 deletions tests/wpa/features/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FeatureSearchTest extends TestBase {
* @testdox If user searches from default WordPress search (front-end), it should fetch results from Elasticsearch
*/
public function testSearchResultsFetchFromElasticsearch() {
$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$I = $this->openBrowserPage();

Expand Down Expand Up @@ -42,7 +42,7 @@ public function testExactMatchesShowHigher() {

$I->loginAs( 'wpsnapshots' );

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$this->publishPost( [
'title' => 'Higher',
Expand Down Expand Up @@ -73,7 +73,7 @@ public function testNewerDuplicatedPostsShowHigher() {

$I->loginAs( 'wpsnapshots' );

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );

$this->publishPost( [
'title' => 'Duplicated post',
Expand Down
2 changes: 1 addition & 1 deletion tests/wpa/indexables/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UserTest extends TestBase {
public function setUp() {
parent::setUp();

$this->runCommand( 'wp elasticpress index --setup' );
$this->runCommand( 'wp elasticpress index --setup --yes' );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion wpacceptance.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"wp --allow-root import /var/www/html/content-example.xml --authors=create",

"wp --allow-root plugin activate elasticpress",
"wp --allow-root elasticpress index --setup --show-errors",
"wp --allow-root elasticpress index --setup --yes --show-errors",

"wp --allow-root plugin deactivate woocommerce"
]
Expand Down