diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/updateWidget.php b/tests/phpunit/tests/admin/includes/ajax-actions/updateWidget.php new file mode 100644 index 0000000000000..8dde018d15382 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/updateWidget.php @@ -0,0 +1,117 @@ +user->create( array( 'role' => 'administrator' ) ); + } + + public function set_up() { + parent::set_up(); + // Initialize the Customizer. + require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; + $GLOBALS['wp_customize'] = new WP_Customize_Manager(); + } + + /** + * Tests failure due to invalid nonce. + * + * @ticket 65252 + */ + public function test_update_widget_invalid_nonce(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'update-widget', + 'nonce' => 'invalid-nonce', + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'update-widget' ); + } + + /** + * Tests failure due to missing widget-id. + * + * @ticket 65252 + */ + public function test_update_widget_missing_widget_id(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'update-widget', + 'nonce' => wp_create_nonce( 'update-widget' ), + ); + + try { + $this->_handleAjax( 'update-widget' ); + } catch ( WPAjaxDieStopException $e ) { + $this->_last_response = $e->getMessage(); + } catch ( WPAjaxDieContinueException $e ) { + } + + $response = json_decode( $this->_last_response, true ); + $this->assertFalse( $response['success'] ); + $this->assertSame( 'missing_widget-id', $response['data'] ); + } + + /** + * Tests failure for template widget. + * + * @ticket 65252 + */ + public function test_update_widget_template_widget_not_updatable(): void { + wp_set_current_user( self::$admin_id ); + + $id_base = 'text'; + $widget_id = $id_base . '-1'; + + $_POST = array( + 'action' => 'update-widget', + 'nonce' => wp_create_nonce( 'update-widget' ), + 'widget-id' => $widget_id, + 'widget-' . $id_base => array( '__i__' => array( 'title' => 'Title' ) ), + ); + + try { + $this->_handleAjax( 'update-widget' ); + } catch ( WPAjaxDieStopException $e ) { + $this->_last_response = $e->getMessage(); + } catch ( WPAjaxDieContinueException $e ) { + } + + $response = json_decode( $this->_last_response, true ); + $this->assertFalse( $response['success'], 'Should fail for template widget' ); + $this->assertSame( 'template_widget_not_updatable', $response['data'] ); + } +}