diff --git a/features/widget-reset.feature b/features/widget-reset.feature index 4c2d84bdcc..f102ee3774 100644 --- a/features/widget-reset.feature +++ b/features/widget-reset.feature @@ -30,16 +30,24 @@ Feature: Reset WordPress sidebars """ Warning: Sidebar 'sidebar-1' is already empty. """ + And STDOUT should be: + """ + Success: Sidebar already reset. + """ + And the return code should be 0 When I try `wp widget reset non-existing-sidebar-id` Then STDERR should be: """ Warning: Invalid sidebar: non-existing-sidebar-id + Error: No sidebars reset. """ + And the return code should be 1 When I run `wp widget add calendar sidebar-1 --title="Calendar"` Then STDOUT should not be empty - And I run `wp widget list sidebar-1 --format=count` + + When I run `wp widget list sidebar-1 --format=count` Then STDOUT should be: """ 1 @@ -47,19 +55,28 @@ Feature: Reset WordPress sidebars When I run `wp widget add search sidebar-2 --title="Quick Search"` Then STDOUT should not be empty - And I run `wp widget list sidebar-2 --format=count` + + When I run `wp widget list sidebar-2 --format=count` Then STDOUT should be: """ 1 """ - When I run `wp widget reset sidebar-1 sidebar-2` - And I run `wp widget list sidebar-1 --format=count` + When I try `wp widget reset sidebar-1 sidebar-2 non-existing-sidebar-id` + Then STDERR should be: + """ + Warning: Invalid sidebar: non-existing-sidebar-id + Error: Only reset 2 of 3 sidebars. + """ + And the return code should be 1 + + When I run `wp widget list sidebar-1 --format=count` Then STDOUT should be: """ 0 """ - And I run `wp widget list sidebar-2 --format=count` + + When I run `wp widget list sidebar-2 --format=count` Then STDOUT should be: """ 0 @@ -79,7 +96,16 @@ Feature: Reset WordPress sidebars Then STDOUT should not be empty When I run `wp widget reset --all` - And I run `wp widget list sidebar-1 --format=count` + Then STDOUT should be: + """ + Sidebar 'sidebar-1' reset. + Sidebar 'sidebar-2' reset. + Sidebar 'sidebar-3' reset. + Success: Reset 3 of 3 sidebars. + """ + And the return code should be 0 + + When I run `wp widget list sidebar-1 --format=count` Then STDOUT should be: """ 0 diff --git a/features/widget.feature b/features/widget.feature index e5eb4609ff..1beb08bfc4 100644 --- a/features/widget.feature +++ b/features/widget.feature @@ -1,11 +1,11 @@ Feature: Manage widgets in WordPress sidebar - Scenario: Widget CRUD + Background: Given a WP install - When I run `wp theme install p2 --activate` Then STDOUT should not be empty + Scenario: Widget CRUD When I run `wp widget list sidebar-1 --fields=name,id,position` Then STDOUT should be a table containing rows: | name | id | position | @@ -34,9 +34,11 @@ Feature: Manage widgets in WordPress sidebar When I run `wp widget deactivate meta-2` Then STDOUT should be: - """ - Success: 1 widget deactivated. - """ + """ + Success: 1 widget deactivated. + """ + And STDERR should be empty + And the return code should be 0 When I run `wp widget list sidebar-1 --fields=name,id,position` Then STDOUT should be a table containing rows: @@ -57,6 +59,8 @@ Feature: Manage widgets in WordPress sidebar """ Success: 2 widgets removed from sidebar. """ + And STDERR should be empty + And the return code should be 0 When I run `wp widget list sidebar-1 --fields=name,id,position` Then STDOUT should be a table containing rows: @@ -87,3 +91,86 @@ Feature: Manage widgets in WordPress sidebar Then STDOUT should be a table containing rows: | name | position | options | | calendar | 2 | {"title":"Calendar"} | + + Scenario: Validate sidebar widgets + When I try `wp widget update calendar-999` + Then STDERR should be: + """ + Error: Widget doesn't exist. + """ + And the return code should be 1 + + When I try `wp widget move calendar-999` + Then STDERR should be: + """ + Error: Widget doesn't exist. + """ + And the return code should be 1 + + Scenario: Return code is 0 when all widgets exist, deactivation + When I run `wp widget deactivate recent-posts-2` + Then STDOUT should be: + """ + Success: 1 widget deactivated. + """ + And STDERR should be empty + And the return code should be 0 + + When I run `wp widget deactivate search-2 archives-2` + Then STDOUT should be: + """ + Success: 2 widgets deactivated. + """ + And STDERR should be empty + And the return code should be 0 + + Scenario: Return code is 0 when all widgets exist, deletion + When I run `wp widget delete recent-posts-2` + Then STDOUT should be: + """ + Success: 1 widget removed from sidebar. + """ + And STDERR should be empty + And the return code should be 0 + + When I run `wp widget delete search-2 archives-2` + Then STDOUT should be: + """ + Success: 2 widgets removed from sidebar. + """ + And STDERR should be empty + And the return code should be 0 + + Scenario: Return code is 1 when 1 or more widgets doesn't exist, deactivation + When I try `wp widget deactivate calendar-999` + Then STDERR should be: + """ + Warning: Widget 'calendar-999' doesn't exist. + Error: No widgets deactivated. + """ + And the return code should be 1 + + When I try `wp widget deactivate recent-posts-2 calendar-999` + Then STDERR should be: + """ + Warning: Widget 'calendar-999' doesn't exist. + Error: Only deactivated 1 of 2 widgets. + """ + And the return code should be 1 + + Scenario: Return code is 1 when 1 or more widgets doesn't exist, deletion + When I try `wp widget delete calendar-999` + Then STDERR should be: + """ + Warning: Widget 'calendar-999' doesn't exist. + Error: No widgets removed from sidebar. + """ + And the return code should be 1 + + When I try `wp widget delete recent-posts-2 calendar-999` + Then STDERR should be: + """ + Warning: Widget 'calendar-999' doesn't exist. + Error: Only removed 1 of 2 widgets from sidebar. + """ + And the return code should be 1 diff --git a/php/commands/widget.php b/php/commands/widget.php index 494060115f..14739c6a15 100644 --- a/php/commands/widget.php +++ b/php/commands/widget.php @@ -182,7 +182,9 @@ public function add( $args, $assoc_args ) { public function update( $args, $assoc_args ) { list( $widget_id ) = $args; - $this->validate_sidebar_widget( $widget_id ); + if ( ! $this->validate_sidebar_widget( $widget_id ) ) { + WP_CLI::error( "Widget doesn't exist." ); + } if ( empty( $assoc_args ) ) { WP_CLI::error( "No options specified to update." ); @@ -231,7 +233,9 @@ public function update( $args, $assoc_args ) { public function move( $args, $assoc_args ) { list( $widget_id ) = $args; - $this->validate_sidebar_widget( $widget_id ); + if ( ! $this->validate_sidebar_widget( $widget_id ) ) { + WP_CLI::error( "Widget doesn't exist." ); + } if ( empty( $assoc_args['position'] ) && empty( $assoc_args['sidebar-id'] ) ) { WP_CLI::error( "A new position or new sidebar must be specified." ); @@ -275,10 +279,14 @@ public function move( $args, $assoc_args ) { */ public function deactivate( $args, $assoc_args ) { - $count = 0; + $count = $errors = 0; foreach( $args as $widget_id ) { - $this->validate_sidebar_widget( $widget_id ); + if ( ! $this->validate_sidebar_widget( $widget_id ) ) { + WP_CLI::warning( "Widget '{$widget_id}' doesn't exist." ); + $errors++; + continue; + } list( $name, $option_index, $sidebar_id, $sidebar_index ) = $this->get_widget_data( $widget_id ); if ( 'wp_inactive_widgets' == $sidebar_id ) { @@ -292,8 +300,17 @@ public function deactivate( $args, $assoc_args ) { } - $success_message = ( 1 === $count ) ? '%d widget deactivated.' : '%d widgets deactivated.'; - WP_CLI::success( sprintf( $success_message, $count ) ); + if ( $errors ) { + if ( $count ) { + $widget_count = count( $args ); + WP_CLI::error( "Only deactivated {$count} of {$widget_count} widgets." ); + } else { + WP_CLI::error( "No widgets deactivated." ); + } + } else { + $success_message = ( 1 === $count ) ? '%d widget deactivated.' : '%d widgets deactivated.'; + WP_CLI::success( sprintf( $success_message, $count ) ); + } } /** @@ -314,10 +331,14 @@ public function deactivate( $args, $assoc_args ) { */ public function delete( $args, $assoc_args ) { - $count = 0; + $count = $errors = 0; foreach( $args as $widget_id ) { - $this->validate_sidebar_widget( $widget_id ); + if ( ! $this->validate_sidebar_widget( $widget_id ) ) { + WP_CLI::warning( "Widget '{$widget_id}' doesn't exist." ); + $errors++; + continue; + } // Remove the widget's settings. list( $name, $option_index, $sidebar_id, $sidebar_index ) = $this->get_widget_data( $widget_id ); @@ -334,8 +355,17 @@ public function delete( $args, $assoc_args ) { $count++; } - $success_message = ( 1 === $count ) ? '%d widget removed from sidebar.' : '%d widgets removed from sidebar.'; - WP_CLI::success( sprintf( $success_message, $count ) ); + if ( $errors ) { + if ( $count ) { + $widget_count = count( $args ); + WP_CLI::error( "Only removed {$count} of {$widget_count} widgets from sidebar." ); + } else { + WP_CLI::error( "No widgets removed from sidebar." ); + } + } else { + $success_message = ( 1 === $count ) ? '%d widget removed from sidebar.' : '%d widgets removed from sidebar.'; + WP_CLI::success( sprintf( $success_message, $count ) ); + } } /** @@ -394,9 +424,11 @@ public function reset( $args, $assoc_args ) { WP_CLI::error( 'No sidebar registered.' ); } + $count = $errors = 0; foreach ( $args as $sidebar_id ) { if ( ! array_key_exists( $sidebar_id, $wp_registered_sidebars ) ) { WP_CLI::warning( sprintf( 'Invalid sidebar: %s', $sidebar_id ) ); + $errors++; continue; } @@ -410,9 +442,27 @@ public function reset( $args, $assoc_args ) { list( $name, $option_index, $new_sidebar_id, $sidebar_index ) = $this->get_widget_data( $widget_id ); $this->move_sidebar_widget( $widget_id, $new_sidebar_id, 'wp_inactive_widgets', $sidebar_index, 0 ); } - WP_CLI::success( sprintf( "Sidebar '%s' reset.", $sidebar_id ) ); + WP_CLI::log( sprintf( "Sidebar '%s' reset.", $sidebar_id ) ); + $count++; } } + + $sidebar_count = count( $args ); + if ( $errors ) { + if ( $count ) { + WP_CLI::error( "Only reset {$count} of {$sidebar_count} sidebars." ); + } else { + WP_CLI::error( 'No sidebars reset.' ); + } + } else { + if ( $count ) { + WP_CLI::success( "Reset {$count} of {$sidebar_count} sidebars." ); + } else { + $message = $sidebar_count > 1 ? 'Sidebars' : 'Sidebar'; + WP_CLI::success( "{$message} already reset." ); + } + } + } /** @@ -448,11 +498,7 @@ private function validate_sidebar_widget( $widget_id ) { } } - - if ( false === $widget_exists ) { - WP_CLI::error( "Specified widget isn't present on sidebar." ); - } - + return $widget_exists; } /**