diff --git a/phpunit/class-rest-sidebars-controller-test.php b/phpunit/class-rest-sidebars-controller-test.php deleted file mode 100644 index 4c1b8f0d72fe25..00000000000000 --- a/phpunit/class-rest-sidebars-controller-test.php +++ /dev/null @@ -1,627 +0,0 @@ -user->create( - array( - 'role' => 'administrator', - ) - ); - self::$author_id = $factory->user->create( - array( - 'role' => 'author', - ) - ); - } - - public static function wpTearDownAfterClass() { - wp_delete_user( self::$admin_id ); - wp_delete_user( self::$author_id ); - } - - public function setUp() { - parent::setUp(); - - wp_set_current_user( self::$admin_id ); - - // Unregister all widgets and sidebars. - global $wp_registered_sidebars, $_wp_sidebars_widgets; - $wp_registered_sidebars = array(); - $_wp_sidebars_widgets = array(); - update_option( 'sidebars_widgets', array() ); - } - - private function setup_widget( $option_name, $number, $settings ) { - update_option( - $option_name, - array( - $number => $settings, - ) - ); - } - - private function setup_sidebar( $id, $attrs = array(), $widgets = array() ) { - global $wp_registered_sidebars; - update_option( - 'sidebars_widgets', - array( - $id => $widgets, - ) - ); - $wp_registered_sidebars[ $id ] = array_merge( - array( - 'id' => $id, - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', - ), - $attrs - ); - - global $wp_registered_widgets; - foreach ( $wp_registered_widgets as $wp_registered_widget ) { - if ( is_array( $wp_registered_widget['callback'] ) ) { - $wp_registered_widget['callback'][0]->_register(); - } - } - } - - /** - * @ticket 51460 - */ - public function test_register_routes() { - $routes = rest_get_server()->get_routes(); - $this->assertArrayHasKey( '/wp/v2/sidebars', $routes ); - $this->assertArrayHasKey( '/wp/v2/sidebars/(?P[\w-]+)', $routes ); - } - - /** - * @ticket 51460 - */ - public function test_context_param() { - // Collection. - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/sidebars' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); - $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); - // Single. - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/sidebars/sidebar-1' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); - $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); - } - - /** - * @ticket 51460 - */ - public function test_get_items() { - $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( array(), $data ); - } - - /** - * @ticket 51460 - */ - public function test_get_items_no_permission() { - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 ); - } - - /** - * @ticket 51460 - */ - public function test_get_items_wrong_permission_author() { - wp_set_current_user( self::$author_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 ); - } - - /** - * @ticket 51460 - */ - public function test_get_items_basic_sidebar() { - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $data = $this->remove_links( $data ); - $this->assertEquals( - array( - array( - 'id' => 'sidebar-1', - 'name' => 'Test sidebar', - 'description' => '', - 'status' => 'active', - 'widgets' => array(), - 'class' => '', - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', - ), - ), - $data - ); - } - - /** - * @ticket 51460 - */ - public function test_get_items_active_sidebar_with_widgets() { - $this->setup_widget( - 'widget_rss', - 1, - array( - 'title' => 'RSS test', - ) - ); - $this->setup_widget( - 'widget_text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1', 'rss-1' ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $data = $this->remove_links( $data ); - $this->assertEquals( - array( - array( - 'id' => 'sidebar-1', - 'name' => 'Test sidebar', - 'description' => '', - 'status' => 'active', - 'widgets' => array( - 'text-1', - 'rss-1', - ), - 'class' => '', - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', - ), - ), - $data - ); - } - - /** - * @ticket 51460 - */ - public function test_get_item() { - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars/sidebar-1' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $data = $this->remove_links( $data ); - $this->assertEquals( - array( - 'id' => 'sidebar-1', - 'name' => 'Test sidebar', - 'description' => '', - 'status' => 'active', - 'widgets' => array(), - 'class' => '', - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', - ), - $data - ); - } - - /** - * @ticket 51460 - */ - public function test_get_item_no_permission() { - wp_set_current_user( 0 ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars/sidebar-1' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 ); - } - - /** - * @ticket 51460 - */ - public function test_get_item_wrong_permission_author() { - wp_set_current_user( self::$author_id ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars/sidebar-1' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 ); - } - - /** - * The test_create_item() method does not exist for sidebar. - */ - public function test_create_item() { - } - - /** - * @ticket 51460 - */ - public function test_update_item() { - $this->setup_widget( - 'widget_rss', - 1, - array( - 'title' => 'RSS test', - ) - ); - $this->setup_widget( - 'widget_text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_widget( - 'widget_text', - 2, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1', 'rss-1' ) - ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/sidebars/sidebar-1' ); - $request->set_body_params( - array( - 'widgets' => array( - 'text-1', - 'text-2', - ), - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $data = $this->remove_links( $data ); - $this->assertEquals( - array( - 'id' => 'sidebar-1', - 'name' => 'Test sidebar', - 'description' => '', - 'status' => 'active', - 'widgets' => array( - 'text-1', - 'text-2', - ), - 'class' => '', - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', - ), - $data - ); - } - - /** - * @ticket 51460 - */ - public function test_update_item_removes_widget_from_existing_sidebar() { - $this->setup_widget( - 'widget_text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1' ) - ); - $this->setup_sidebar( - 'sidebar-2', - array( - 'name' => 'Test sidebar 2', - ), - array() - ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/sidebars/sidebar-2' ); - $request->set_body_params( - array( - 'widgets' => array( - 'text-1', - ), - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertContains( 'text-1', $data['widgets'] ); - - $this->assertNotContains( 'text-1', rest_do_request( '/wp/v2/sidebars/sidebar-1' )->get_data()['widgets'] ); - } - - /** - * @ticket 51460 - */ - public function test_update_item_moves_omitted_widget_to_inactive_sidebar() { - $this->setup_widget( - 'widget_text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_widget( - 'widget_text', - 2, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1' ) - ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/sidebars/sidebar-1' ); - $request->set_body_params( - array( - 'widgets' => array( - 'text-2', - ), - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertContains( 'text-2', $data['widgets'] ); - $this->assertNotContains( 'text-1', $data['widgets'] ); - - $this->assertContains( 'text-1', rest_do_request( '/wp/v2/sidebars/wp_inactive_widgets' )->get_data()['widgets'] ); - } - - /** - * @ticket 51460 - */ - public function test_get_items_inactive_widgets() { - $this->setup_widget( - 'widget_rss', - 1, - array( - 'title' => 'RSS test', - ) - ); - $this->setup_widget( - 'widget_text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1' ) - ); - update_option( - 'sidebars_widgets', - array_merge( - get_option( 'sidebars_widgets' ), - array( - 'wp_inactive_widgets' => array( 'rss-1', 'rss' ), - ) - ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars' ); - $request->set_param( 'context', 'view' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $data = $this->remove_links( $data ); - $this->assertEquals( - array( - array( - 'id' => 'sidebar-1', - 'name' => 'Test sidebar', - 'description' => '', - 'status' => 'active', - 'widgets' => array( - 'text-1', - ), - 'class' => '', - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', - ), - array( - 'id' => 'wp_inactive_widgets', - 'name' => 'Inactive widgets', - 'description' => '', - 'status' => 'inactive', - 'widgets' => array( - 'rss-1', - ), - 'class' => '', - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', - ), - ), - $data - ); - } - - /** - * @ticket 51460 - */ - public function test_update_item_no_permission() { - wp_set_current_user( 0 ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/sidebars/sidebar-1' ); - $request->set_body_params( - array( - 'widgets' => array(), - ) - ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 ); - } - - /** - * @ticket 51460 - */ - public function test_update_item_wrong_permission_author() { - wp_set_current_user( self::$author_id ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/sidebars/sidebar-1' ); - $request->set_body_params( - array( - 'widgets' => array(), - ) - ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 ); - } - - /** - * The test_delete_item() method does not exist for sidebar. - */ - public function test_delete_item() { - } - - /** - * The test_prepare_item() method does not exist for sidebar. - */ - public function test_prepare_item() { - } - - /** - * @ticket 51460 - */ - public function test_get_item_schema() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/sidebars' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'status', $properties ); - $this->assertArrayHasKey( 'widgets', $properties ); - $this->assertArrayHasKey( 'class', $properties ); - $this->assertArrayHasKey( 'before_widget', $properties ); - $this->assertArrayHasKey( 'after_widget', $properties ); - $this->assertArrayHasKey( 'before_title', $properties ); - $this->assertArrayHasKey( 'after_title', $properties ); - $this->assertCount( 10, $properties ); - } - - /** - * Helper to remove links key. - * - * @param array $data Array of data. - * - * @return array - */ - protected function remove_links( $data ) { - if ( ! is_array( $data ) ) { - return $data; - } - $count = 0; - foreach ( $data as $item ) { - if ( isset( $item['_links'] ) ) { - unset( $data[ $count ]['_links'] ); - } - $count ++; - } - - return $data; - } -} diff --git a/phpunit/class-rest-widget-types-controller-test.php b/phpunit/class-rest-widget-types-controller-test.php deleted file mode 100644 index f24be60d8f3e8e..00000000000000 --- a/phpunit/class-rest-widget-types-controller-test.php +++ /dev/null @@ -1,457 +0,0 @@ -user->create( - array( - 'role' => 'administrator', - ) - ); - self::$subscriber_id = $factory->user->create( - array( - 'role' => 'subscriber', - ) - ); - } - - public static function wpTearDownAfterClass() { - self::delete_user( self::$admin_id ); - self::delete_user( self::$subscriber_id ); - } - - /** - * @ticket 51460 - */ - public function test_register_routes() { - $this->markTestSkipped( - 'The test is failing with latest WordPress core.' - ); - $routes = rest_get_server()->get_routes(); - $this->assertArrayHasKey( '/wp/v2/widget-types', $routes ); - $this->assertCount( 1, $routes['/wp/v2/widget-types'] ); - $this->assertArrayHasKey( '/wp/v2/widget-types/(?P[a-zA-Z0-9_-]+)', $routes ); - $this->assertCount( 1, $routes['/wp/v2/widget-types/(?P[a-zA-Z0-9_-]+)'] ); - $this->assertArrayHasKey( '/wp/v2/widget-types/(?P[a-zA-Z0-9_-]+)/encode', $routes ); - $this->assertCount( 1, $routes['/wp/v2/widget-types/(?P[a-zA-Z0-9_-]+)/encode'] ); - } - - /** - * @ticket 51460 - */ - public function test_context_param() { - // Collection. - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/widget-types' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); - $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); - // Single. - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/widget-types/calendar' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); - $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); - } - - /** - * @ticket 51460 - */ - public function test_get_items() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/widget-types' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertGreaterThan( 1, count( $data ) ); - $endpoint = new WP_REST_Widget_Types_Controller; - foreach ( $data as $item ) { - $widget_type = $endpoint->get_widget( $item['name'] ); - $this->check_widget_type_object( $widget_type, $item, $item['_links'] ); - } - - } - - /** - * @ticket 51460 - */ - public function test_get_item() { - $widget_name = 'calendar'; - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/widget-types/' . $widget_name ); - $response = rest_get_server()->dispatch( $request ); - $endpoint = new WP_REST_Widget_Types_Controller; - $widget_type = $endpoint->get_widget( $widget_name ); - $this->check_widget_type_object( $widget_type, $response->get_data(), $response->get_links() ); - } - - /** - * @ticket 51460 - */ - public function test_get_widget_legacy() { - $widget_id = 'legacy'; - wp_register_sidebar_widget( - $widget_id, - 'WP legacy widget', - function() {} - ); - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/widget-types/' . $widget_id ); - $response = rest_get_server()->dispatch( $request ); - $endpoint = new WP_REST_Widget_Types_Controller; - $widget_type = $endpoint->get_widget( $widget_id ); - $this->check_widget_type_object( $widget_type, $response->get_data(), $response->get_links() ); - } - - /** - * @ticket 51460 - */ - public function test_get_widget_invalid_name() { - $widget_type = 'fake'; - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/widget-types/' . $widget_type ); - $response = rest_get_server()->dispatch( $request ); - - $this->assertErrorResponse( 'rest_widget_type_invalid', $response, 404 ); - } - - /** - * @ticket 51460 - */ - public function test_get_item_schema() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/widget-types' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - $this->assertCount( 5, $properties ); - - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'is_multi', $properties ); - $this->assertArrayHasKey( 'classname', $properties ); - } - - /** - * @ticket 51460 - */ - public function test_get_items_wrong_permission() { - wp_set_current_user( self::$subscriber_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/widget-types' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 ); - } - - /** - * @ticket 51460 - */ - public function test_get_item_wrong_permission() { - wp_set_current_user( self::$subscriber_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/widget-types/calendar' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 ); - } - - /** - * @ticket 51460 - */ - public function test_get_items_no_permission() { - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'GET', '/wp/v2/widget-types' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 ); - } - - /** - * @ticket 51460 - */ - public function test_get_item_no_permission() { - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'GET', '/wp/v2/widget-types/calendar' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 ); - } - - /** - * @ticket 51460 - */ - public function test_prepare_item() { - $endpoint = new WP_REST_Widget_Types_Controller; - $widget_type = $endpoint->get_widget( 'calendar' ); - $request = new WP_REST_Request; - $request->set_param( 'context', 'edit' ); - $response = $endpoint->prepare_item_for_response( $widget_type, $request ); - $this->check_widget_type_object( $widget_type, $response->get_data(), $response->get_links() ); - } - - /** - * Util check widget type object against. - * - * @since 5.6.0 - * - * @param WP_Widget_Type $widget_type Sample widget type. - * @param array $data Data to compare against. - * @param array $links Links to compare again. - */ - protected function check_widget_type_object( $widget_type, $data, $links ) { - // Test data. - $extra_fields = array( - 'name', - 'id_base', - 'option_name', - 'control_options', - 'widget_options', - 'widget_class', - 'is_multi', - ); - - foreach ( $extra_fields as $extra_field ) { - if ( isset( $widget_type->$extra_field ) ) { - $this->assertSame( $data[ $extra_field ], $widget_type->$extra_field, 'Field ' . $extra_field ); - } - } - - // Test links. - $this->assertSame( rest_url( 'wp/v2/widget-types' ), $links['collection'][0]['href'] ); - } - - public function test_encode_form_data_with_no_input() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'POST', '/wp/v2/widget-types/search/encode' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( - "

\n" . - "\t\t\t\n" . - "\t\t\t\n" . - "\t\t

", - $data['form'] - ); - $this->assertStringMatchesFormat( - "
\n" . - "\t\t\t\t
\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t
\n" . - "\t\t\t
", - $data['preview'] - ); - $this->assertEqualSets( - array( - 'encoded' => base64_encode( serialize( array() ) ), - 'hash' => wp_hash( serialize( array() ) ), - 'raw' => new stdClass, - ), - $data['instance'] - ); - } - - public function test_encode_form_data_with_number() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'POST', '/wp/v2/widget-types/search/encode' ); - $request->set_param( 'number', 8 ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( - "

\n" . - "\t\t\t\n" . - "\t\t\t\n" . - "\t\t

", - $data['form'] - ); - $this->assertStringMatchesFormat( - "
\n" . - "\t\t\t\t
\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t
\n" . - "\t\t\t
", - $data['preview'] - ); - $this->assertEqualSets( - array( - 'encoded' => base64_encode( serialize( array() ) ), - 'hash' => wp_hash( serialize( array() ) ), - 'raw' => new stdClass, - ), - $data['instance'] - ); - } - - public function test_encode_form_data_with_instance() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'POST', '/wp/v2/widget-types/search/encode' ); - $request->set_param( - 'instance', - array( - 'encoded' => base64_encode( serialize( array( 'title' => 'Test title' ) ) ), - 'hash' => wp_hash( serialize( array( 'title' => 'Test title' ) ) ), - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( - "

\n" . - "\t\t\t\n" . - "\t\t\t\n" . - "\t\t

", - $data['form'] - ); - $this->assertStringMatchesFormat( - "

Test title

\n" . - "\t\t\t\t
\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t
\n" . - "\t\t\t
", - $data['preview'] - ); - $this->assertEqualSets( - array( - 'encoded' => base64_encode( serialize( array( 'title' => 'Test title' ) ) ), - 'hash' => wp_hash( serialize( array( 'title' => 'Test title' ) ) ), - 'raw' => array( 'title' => 'Test title' ), - ), - $data['instance'] - ); - } - - public function test_encode_form_data_with_form_data() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'POST', '/wp/v2/widget-types/search/encode' ); - $request->set_param( 'form_data', 'widget-search[-1][title]=Updated+title' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( - "

\n" . - "\t\t\t\n" . - "\t\t\t\n" . - "\t\t

", - $data['form'] - ); - $this->assertStringMatchesFormat( - "

Updated title

\n" . - "\t\t\t\t
\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t
\n" . - "\t\t\t
", - $data['preview'] - ); - $this->assertEqualSets( - array( - 'encoded' => base64_encode( serialize( array( 'title' => 'Updated title' ) ) ), - 'hash' => wp_hash( serialize( array( 'title' => 'Updated title' ) ) ), - 'raw' => array( 'title' => 'Updated title' ), - ), - $data['instance'] - ); - } - - public function test_encode_form_data_no_raw() { - global $wp_widget_factory; - - $this->markTestSkipped( - 'The test is failing with latest WordPress core.' - ); - - wp_set_current_user( self::$admin_id ); - $wp_widget_factory->widgets['WP_Widget_Search']->show_instance_in_rest = false; - $request = new WP_REST_Request( 'POST', '/wp/v2/widget-types/search/encode' ); - $request->set_param( - 'instance', - array( - 'encoded' => base64_encode( serialize( array( 'title' => 'Test title' ) ) ), - 'hash' => wp_hash( serialize( array( 'title' => 'Test title' ) ) ), - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( - "

\n" . - "\t\t\t\n" . - "\t\t\t\n" . - "\t\t

", - $data['form'] - ); - $this->assertStringMatchesFormat( - "

Test title

\n" . - "\t\t\t\t
\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t\t\n" . - "\t\t\t\t
\n" . - "\t\t\t
", - $data['preview'] - ); - $this->assertEqualSets( - array( - 'encoded' => base64_encode( serialize( array( 'title' => 'Test title' ) ) ), - 'hash' => wp_hash( serialize( array( 'title' => 'Test title' ) ) ), - ), - $data['instance'] - ); - $wp_widget_factory->widgets['WP_Widget_Search']->show_instance_in_rest = true; - } - - - /** - * The test_create_item() method does not exist for widget types. - */ - public function test_create_item() {} - - /** - * The test_update_item() method does not exist for widget types. - */ - public function test_update_item() {} - - /** - * The test_delete_item() method does not exist for widget types. - */ - public function test_delete_item() {} -} diff --git a/phpunit/class-rest-widgets-controller-test.php b/phpunit/class-rest-widgets-controller-test.php deleted file mode 100644 index 4674808ab7e4ce..00000000000000 --- a/phpunit/class-rest-widgets-controller-test.php +++ /dev/null @@ -1,1387 +0,0 @@ -user->create( - array( - 'role' => 'administrator', - 'user_login' => 'superadmin', - ) - ); - if ( is_multisite() ) { - update_site_option( 'site_admins', array( 'superadmin' ) ); - } - self::$admin_id = $factory->user->create( - array( - 'role' => 'administrator', - ) - ); - self::$editor_id = $factory->user->create( - array( - 'role' => 'editor', - ) - ); - self::$author_id = $factory->user->create( - array( - 'role' => 'author', - ) - ); - self::$subscriber_id = $factory->user->create( - array( - 'role' => 'subscriber', - ) - ); - } - - public function setUp() { - global $wp_registered_widgets, $wp_registered_sidebars, $_wp_sidebars_widgets, $wp_widget_factory; - - parent::setUp(); - - wp_set_current_user( self::$admin_id ); - - // Unregister all widgets and sidebars. - $wp_registered_widgets = array(); - $wp_registered_sidebars = array(); - $_wp_sidebars_widgets = array(); - update_option( 'sidebars_widgets', array() ); - - // Re-register core widgets. - $wp_widget_factory->_register_widgets(); - - // Register a non-multi widget for testing. - wp_register_widget_control( - 'testwidget', - 'WP test widget', - function () { - $settings = get_option( 'widget_testwidget' ); - - // check if anything's been sent. - if ( isset( $_POST['update_testwidget'] ) ) { - $settings['id'] = $_POST['test_id']; - $settings['title'] = $_POST['test_title']; - - update_option( 'widget_testwidget', $settings ); - } - - echo 'WP test widget form'; - }, - 100, - 200 - ); - wp_register_sidebar_widget( - 'testwidget', - 'WP test widget', - function () { - $settings = wp_parse_args( - get_option( 'widget_testwidget' ), - array( - 'id' => 'Default id', - 'title' => 'Default text', - ) - ); - echo '

' . $settings['id'] . '

' . $settings['title'] . ''; - }, - array( - 'description' => 'A non-multi widget for testing.', - ) - ); - } - - private function setup_widget( $id_base, $number, $settings ) { - $option_name = "widget_$id_base"; - update_option( - $option_name, - array( - $number => $settings, - ) - ); - - $widget_object = gutenberg_get_widget_object( $id_base ); - $widget_object->_set( $number ); - $widget_object->_register_one( $number ); - } - - private function setup_sidebar( $id, $attrs = array(), $widgets = array() ) { - global $wp_registered_sidebars; - update_option( - 'sidebars_widgets', - array_merge( - (array) get_option( 'sidebars_widgets', array() ), - array( - $id => $widgets, - ) - ) - ); - $wp_registered_sidebars[ $id ] = array_merge( - array( - 'id' => $id, - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', - ), - $attrs - ); - } - - /** - * @ticket 51460 - */ - public function test_register_routes() { - $routes = rest_get_server()->get_routes(); - $this->assertArrayHasKey( '/wp/v2/widgets', $routes ); - $this->assertArrayHasKey( '/wp/v2/widgets/(?P[\w\-]+)', $routes ); - } - - /** - * @ticket 51460 - */ - public function test_context_param() { - } - - /** - * @ticket 51460 - */ - public function test_get_items_no_widgets() { - $request = new WP_REST_Request( 'GET', '/wp/v2/widgets' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( array(), $data ); - } - - /** - * @ticket 51460 - */ - public function test_get_items_no_permission() { - wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'GET', '/wp/v2/widgets' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 ); - } - - /** - * @ticket 51460 - */ - public function test_get_items_wrong_permission_author() { - wp_set_current_user( self::$author_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/widgets' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 ); - } - - /** - * @ticket 51460 - */ - public function test_get_items() { - global $wp_widget_factory; - - $this->markTestSkipped( - 'The test is failing with latest WordPress core.' - ); - - $wp_widget_factory->widgets['WP_Widget_RSS']->show_instance_in_rest = false; - - $block_content = '

Block test

'; - - $this->setup_widget( - 'rss', - 1, - array( - 'title' => 'RSS test', - ) - ); - $this->setup_widget( - 'block', - 1, - array( - 'content' => $block_content, - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'block-1', 'rss-1', 'testwidget' ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/widgets' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $data = $this->remove_links( $data ); - $this->assertEqualSets( - array( - array( - 'id' => 'block-1', - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'encoded' => base64_encode( - serialize( - array( - 'content' => $block_content, - ) - ) - ), - 'hash' => wp_hash( - serialize( - array( - 'content' => $block_content, - ) - ) - ), - 'raw' => array( - 'content' => $block_content, - ), - ), - 'id_base' => 'block', - 'rendered' => '

Block test

', - ), - array( - 'id' => 'rss-1', - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'encoded' => base64_encode( - serialize( - array( - 'title' => 'RSS test', - ) - ) - ), - 'hash' => wp_hash( - serialize( - array( - 'title' => 'RSS test', - ) - ) - ), - ), - 'id_base' => 'rss', - 'rendered' => '', - ), - array( - 'id' => 'testwidget', - 'sidebar' => 'sidebar-1', - 'instance' => null, - 'id_base' => 'testwidget', - 'rendered' => '

Default id

Default text', - ), - ), - $data - ); - - $wp_widget_factory->widgets['WP_Widget_RSS']->show_instance_in_rest = true; - } - - /** - * Test a GET request in edit context. In particular, we expect rendered_form to be served correctly. - */ - public function test_get_items_edit_context() { - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1', 'testwidget' ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/widgets' ); - $request['context'] = 'edit'; - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $data = $this->remove_links( $data ); - $this->assertEqualSets( - array( - array( - 'id' => 'text-1', - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'encoded' => base64_encode( - serialize( - array( - 'text' => 'Custom text test', - ) - ) - ), - 'hash' => wp_hash( - serialize( - array( - 'text' => 'Custom text test', - ) - ) - ), - 'raw' => array( - 'text' => 'Custom text test', - ), - ), - 'id_base' => 'text', - 'rendered' => '
Custom text test
', - 'rendered_form' => '' . "\n" . - ' ' . "\n" . - ' ' . "\n" . - ' ', - ), - array( - 'id' => 'testwidget', - 'sidebar' => 'sidebar-1', - 'instance' => null, - 'id_base' => 'testwidget', - 'rendered' => '

Default id

Default text', - 'rendered_form' => 'WP test widget form', - ), - ), - $data - ); - } - - /** - * @ticket 51460 - */ - public function test_get_item() { - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1' ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/widgets/text-1' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEqualSets( - array( - 'id' => 'text-1', - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'encoded' => base64_encode( - serialize( - array( - 'text' => 'Custom text test', - ) - ) - ), - 'hash' => wp_hash( - serialize( - array( - 'text' => 'Custom text test', - ) - ) - ), - 'raw' => array( - 'text' => 'Custom text test', - ), - ), - 'id_base' => 'text', - 'rendered' => '
Custom text test
', - ), - $data - ); - } - - /** - * @ticket 51460 - */ - public function test_get_item_no_permission() { - wp_set_current_user( 0 ); - - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1' ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/widgets/text-1' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 ); - } - - /** - * @ticket 51460 - */ - public function test_get_item_wrong_permission_author() { - wp_set_current_user( self::$author_id ); - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/widgets/text-1' ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 ); - } - - /** - * @ticket 51460 - */ - public function test_create_item() { - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/widgets' ); - $request->set_body_params( - array( - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'encoded' => base64_encode( - serialize( - array( - 'text' => 'Updated text test', - ) - ) - ), - 'hash' => wp_hash( - serialize( - array( - 'text' => 'Updated text test', - ) - ) - ), - ), - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 'text-2', $data['id'] ); - $this->assertEquals( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( - array( - 'text' => 'Updated text test', - 'title' => '', - 'filter' => false, - ), - get_option( 'widget_text' )[2] - ); - } - - /** - * @ticket 51460 - */ - public function test_create_item_malformed_instance() { - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/widgets' ); - $request->set_body_params( - array( - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'encoded' => base64_encode( - serialize( - array( - 'text' => 'Updated text test', - ) - ) - ), - 'hash' => 'badhash', - ), - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_invalid_widget', $response, 400 ); - } - - /** - * @ticket 51460 - */ - public function test_create_item_bad_instance() { - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/widgets' ); - $request->set_body_params( - array( - 'sidebar' => 'sidebar-1', - 'instance' => array(), - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_invalid_widget', $response, 400 ); - } - - /** - * @ticket 51460 - */ - public function test_create_item_using_raw_instance() { - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/widgets' ); - $request->set_body_params( - array( - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'raw' => array( - 'content' => '

Block test

', - ), - ), - 'id_base' => 'block', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 'block-2', $data['id'] ); - $this->assertEquals( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( - array( - 'content' => '

Block test

', - ), - get_option( 'widget_block' )[2] - ); - } - - /** - * @ticket 51460 - */ - public function test_create_item_raw_instance_not_supported() { - global $wp_widget_factory; - - $this->markTestSkipped( - 'The test is failing with latest WordPress core.' - ); - - $wp_widget_factory->widgets['WP_Widget_Text']->show_instance_in_rest = false; - - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/widgets' ); - $request->set_body_params( - array( - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'raw' => array( - 'title' => 'Updated text test', - ), - ), - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_invalid_widget', $response, 400 ); - - $wp_widget_factory->widgets['WP_Widget_Text']->show_instance_in_rest = true; - } - - /** - * @ticket 51460 - */ - public function test_create_item_using_form_data() { - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/widgets' ); - $request->set_body_params( - array( - 'sidebar' => 'sidebar-1', - 'form_data' => 'widget-text[2][text]=Updated+text+test', - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 'text-2', $data['id'] ); - $this->assertEquals( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( - array( - 'text' => 'Updated text test', - 'title' => '', - 'filter' => false, - ), - $data['instance']['raw'] - ); - } - - /** - * @ticket 51460 - */ - public function test_create_item_multiple_in_a_row() { - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/widgets' ); - $request->set_body_params( - array( - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'raw' => array( 'text' => 'Text 1' ), - ), - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 'text-2', $data['id'] ); - $this->assertEquals( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( - array( - 'text' => 'Text 1', - 'title' => '', - 'filter' => false, - ), - $data['instance']['raw'] - ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/widgets' ); - $request->set_body_params( - array( - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'raw' => array( 'text' => 'Text 2' ), - ), - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 'text-3', $data['id'] ); - $this->assertEquals( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( - array( - 'text' => 'Text 2', - 'title' => '', - 'filter' => false, - ), - $data['instance']['raw'] - ); - - $sidebar = rest_do_request( '/wp/v2/sidebars/sidebar-1' ); - $this->assertContains( 'text-2', $sidebar->get_data()['widgets'] ); - $this->assertContains( 'text-3', $sidebar->get_data()['widgets'] ); - } - - /** - * @ticket 51460 - */ - public function test_create_item_second_instance() { - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ) - ); - - $request = new WP_REST_Request( 'POST', '/wp/v2/widgets' ); - $request->set_body_params( - array( - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'raw' => array( - 'text' => 'Updated text test', - ), - ), - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( 'text-2', $data['id'] ); - $this->assertEquals( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( - array( - 'text' => 'Updated text test', - 'title' => '', - 'filter' => false, - ), - $data['instance']['raw'] - ); - } - - /** - * @ticket 51460 - */ - public function test_update_item() { - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1', 'rss-1' ) - ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/widgets/text-1' ); - $request->set_body_params( - array( - 'id' => 'text-1', - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'raw' => array( - 'text' => 'Updated text test', - ), - ), - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'text-1', $data['id'] ); - $this->assertEquals( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( - array( - 'text' => 'Updated text test', - 'title' => '', - 'filter' => false, - ), - $data['instance']['raw'] - ); - } - - /** - * @ticket 51460 - */ - public function test_update_item_reassign_sidebar() { - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1', 'rss-1' ) - ); - $this->setup_sidebar( - 'sidebar-2', - array( - 'name' => 'Test sidebar', - ), - array() - ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/widgets/text-1' ); - $request->set_body_params( - array( - 'sidebar' => 'sidebar-2', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $error = $response->as_error(); - $this->assertNotWPError( $error, $error ? $error->get_error_message() : '' ); - $this->assertEquals( 'sidebar-2', $response->get_data()['sidebar'] ); - - $sidebar1 = rest_do_request( '/wp/v2/sidebars/sidebar-1' ); - $this->assertNotContains( 'text-1', $sidebar1->get_data()['widgets'] ); - - $sidebar2 = rest_do_request( '/wp/v2/sidebars/sidebar-2' ); - $this->assertContains( 'text-1', $sidebar2->get_data()['widgets'] ); - } - - /** - * @ticket 51460 - */ - public function test_update_item_shouldnt_require_id_base() { - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1', 'rss-1' ) - ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/widgets/text-1' ); - $request->set_body_params( - array( - 'id' => 'text-1', - 'instance' => array( - 'raw' => array( - 'text' => 'Updated text test', - ), - ), - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 'text-1', $data['id'] ); - $this->assertEquals( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( - array( - 'text' => 'Updated text test', - 'title' => '', - 'filter' => false, - ), - $data['instance']['raw'] - ); - } - - /** - * @group multisite - */ - public function test_store_html_as_admin() { - if ( is_multisite() ) { - $this->assertEquals( - '
alert(1)
', - $this->update_text_widget_with_raw_html( '' ) - ); - } else { - $this->assertEquals( - '
', - $this->update_text_widget_with_raw_html( '' ) - ); - } - } - - /** - * @group multisite - */ - public function test_store_html_as_superadmin() { - wp_set_current_user( self::$superadmin_id ); - if ( is_multisite() ) { - $this->assertEquals( - '
', - $this->update_text_widget_with_raw_html( '' ) - ); - } else { - $this->assertEquals( - '
', - $this->update_text_widget_with_raw_html( '' ) - ); - } - } - - protected function update_text_widget_with_raw_html( $html ) { - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1' ) - ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/widgets/text-1' ); - $request->set_body_params( - array( - 'id' => 'text-1', - 'instance' => array( - 'raw' => array( - 'text' => $html, - ), - ), - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - - return $data['rendered']; - } - - /** - * @ticket 51460 - */ - public function test_update_item_legacy_widget() { - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'testwidget' ) - ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/widgets/testwidget' ); - $request->set_body_params( - array( - 'id' => 'testwidget', - 'name' => 'WP test widget', - 'form_data' => 'test_id=My+test+id&test_title=My+test+title&update_testwidget=true', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $data = $this->remove_links( $data ); - $this->assertEquals( - array( - 'id' => 'testwidget', - 'sidebar' => 'sidebar-1', - 'instance' => null, - 'rendered' => '

My test id

My test title', - 'rendered_form' => 'WP test widget form', - 'id_base' => 'testwidget', - ), - $data - ); - } - - /** - * @ticket 51460 - */ - public function test_create_item_legacy_widget() { - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array() - ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/widgets/testwidget' ); - $request->set_body_params( - array( - 'id' => 'testwidget', - 'sidebar' => 'sidebar-1', - 'name' => 'WP test widget', - 'form_data' => 'test_id=My+test+id&test_title=My+test+title&update_testwidget=true', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $data = $this->remove_links( $data ); - $this->assertEquals( - array( - 'id' => 'testwidget', - 'sidebar' => 'sidebar-1', - 'instance' => null, - 'rendered' => '

My test id

My test title', - 'rendered_form' => 'WP test widget form', - 'id_base' => 'testwidget', - ), - $data - ); - } - - /** - * @ticket 51460 - */ - public function test_update_item_no_permission() { - wp_set_current_user( 0 ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/sidebars/sidebar-1' ); - $request->set_body_params( - array( - 'widgets' => array(), - ) - ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 ); - } - - /** - * @ticket 51460 - */ - public function test_update_item_wrong_permission_author() { - wp_set_current_user( self::$author_id ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/sidebars/sidebar-1' ); - $request->set_body_params( - array( - 'widgets' => array(), - ) - ); - $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 ); - } - - /** - * Tests if the endpoint correctly handles "slashable" characters such as " or '. - */ - public function test_update_item_slashing() { - $this->setup_widget( 'text', 1, array( 'text' => 'Custom text test' ) ); - $this->setup_sidebar( 'sidebar-1', array( 'name' => 'Test sidebar' ), array( 'text-1', 'rss-1' ) ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/widgets/text-1' ); - $request->set_body_params( - array( - 'id' => 'text-1', - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'raw' => array( - 'text' => 'Updated \\" \\\' text test', - ), - ), - 'id_base' => 'text', - ) - ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEqualSets( - array( - 'text' => 'Updated \\" \\\' text test', - 'title' => '', - 'filter' => false, - ), - $data['instance']['raw'] - ); - - $this->assertEquals( - '
Updated \\" \\\' text test
', - $data['rendered'] - ); - } - - /** - * @ticket 51460 - */ - public function test_delete_item() { - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1', 'rss-1' ) - ); - - $request = new WP_REST_Request( 'DELETE', '/wp/v2/widgets/text-1' ); - $response = rest_do_request( $request ); - - $this->assertEqualSets( - array( - 'id' => 'text-1', - 'sidebar' => 'wp_inactive_widgets', - 'instance' => array( - 'encoded' => base64_encode( - serialize( - array( - 'text' => 'Custom text test', - ) - ) - ), - 'hash' => wp_hash( - serialize( - array( - 'text' => 'Custom text test', - ) - ) - ), - 'raw' => array( - 'text' => 'Custom text test', - ), - ), - 'id_base' => 'text', - 'rendered' => '', - 'rendered_form' => '' . "\n" . - ' ' . "\n" . - ' ' . "\n" . - ' ', - ), - $response->get_data() - ); - } - - /** - * @ticket 51460 - */ - public function test_delete_item_force() { - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1', 'rss-1' ) - ); - - $request = new WP_REST_Request( 'DELETE', '/wp/v2/widgets/text-1' ); - $request->set_query_params( array( 'force' => true ) ); - $response = rest_do_request( $request ); - - $this->assertEqualSets( - array( - 'deleted' => true, - 'previous' => array( - - 'id' => 'text-1', - 'sidebar' => 'sidebar-1', - 'instance' => array( - 'encoded' => base64_encode( - serialize( - array( - 'text' => 'Custom text test', - ) - ) - ), - 'hash' => wp_hash( - serialize( - array( - 'text' => 'Custom text test', - ) - ) - ), - 'raw' => array( - 'text' => 'Custom text test', - ), - ), - 'id_base' => 'text', - 'rendered' => '
Custom text test
', - 'rendered_form' => '' . "\n" . - ' ' . "\n" . - ' ' . "\n" . - ' ', - - ), - ), - $response->get_data() - ); - - $response = rest_do_request( '/wp/v2/widgets/text-1' ); - $this->assertEquals( 404, $response->get_status() ); - } - - /** - * @ticket 51460 - */ - public function test_delete_item_logged_out() { - wp_set_current_user( 0 ); - - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1', 'rss-1' ) - ); - - $request = new WP_REST_Request( 'DELETE', '/wp/v2/widgets/text-1' ); - $response = rest_do_request( $request ); - - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 401 ); - } - - /** - * @ticket 51460 - */ - public function test_delete_item_author() { - wp_set_current_user( self::$author_id ); - - $this->setup_widget( - 'text', - 1, - array( - 'text' => 'Custom text test', - ) - ); - $this->setup_sidebar( - 'sidebar-1', - array( - 'name' => 'Test sidebar', - ), - array( 'text-1', 'rss-1' ) - ); - - $request = new WP_REST_Request( 'DELETE', '/wp/v2/widgets/text-1' ); - $response = rest_do_request( $request ); - - $this->assertErrorResponse( 'rest_cannot_manage_widgets', $response, 403 ); - } - - /** - * The test_prepare_item() method does not exist for sidebar. - */ - public function test_prepare_item() { - } - - /** - * @ticket 51460 - */ - public function test_get_item_schema() { - wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/widgets' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertEquals( 7, count( $properties ) ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'id_base', $properties ); - $this->assertArrayHasKey( 'sidebar', $properties ); - $this->assertArrayHasKey( 'rendered', $properties ); - $this->assertArrayHasKey( 'rendered_form', $properties ); - $this->assertArrayHasKey( 'instance', $properties ); - $this->assertArrayHasKey( 'form_data', $properties ); - } - - /** - * Helper to remove links key. - * - * @param array $data Array of data. - * - * @return array - */ - protected function remove_links( $data ) { - if ( ! is_array( $data ) ) { - return $data; - } - $count = 0; - foreach ( $data as $item ) { - if ( is_array( $item ) && isset( $item['_links'] ) ) { - unset( $data[ $count ]['_links'] ); - } - $count ++; - } - - return $data; - } -}