mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
REST API: Add widget endpoints
Adds the sidebars, widgets and widget-types REST API endpoints from the Gutenberg plugin. Fixes #41683. Props TimothyBlynJacobs, spacedmonkey, zieladam, jorgefilipecosta, youknowriad, kevin940726. git-svn-id: https://develop.svn.wordpress.org/trunk@50995 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -134,6 +134,13 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
|
||||
'/wp/v2/plugins',
|
||||
'/wp/v2/plugins/(?P<plugin>[^.\/]+(?:\/[^.\/]+)?)',
|
||||
'/wp/v2/block-directory/search',
|
||||
'/wp/v2/sidebars',
|
||||
'/wp/v2/sidebars/(?P<id>[\w-]+)',
|
||||
'/wp/v2/widget-types',
|
||||
'/wp/v2/widget-types/(?P<id>[a-zA-Z0-9_-]+)',
|
||||
'/wp/v2/widget-types/(?P<id>[a-zA-Z0-9_-]+)/encode',
|
||||
'/wp/v2/widgets',
|
||||
'/wp/v2/widgets/(?P<id>[\w\-]+)',
|
||||
'/wp-site-health/v1',
|
||||
'/wp-site-health/v1/tests/background-updates',
|
||||
'/wp-site-health/v1/tests/loopback-requests',
|
||||
|
||||
627
tests/phpunit/tests/rest-api/rest-sidebars-controller.php
Normal file
627
tests/phpunit/tests/rest-api/rest-sidebars-controller.php
Normal file
@@ -0,0 +1,627 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_REST_Sidebars_Controller functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 5.8.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for REST API for Menus.
|
||||
*
|
||||
* @see WP_Test_REST_Controller_Testcase
|
||||
* @group restapi
|
||||
* @covers WP_REST_Sidebars_Controller
|
||||
*/
|
||||
class WP_Test_REST_Sidebars_Controller extends WP_Test_REST_Controller_Testcase {
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $admin_id;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $author_id;
|
||||
|
||||
/**
|
||||
* Create fake data before our tests run.
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$admin_id = $factory->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 41683
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = rest_get_server()->get_routes();
|
||||
$this->assertArrayHasKey( '/wp/v2/sidebars', $routes );
|
||||
$this->assertArrayHasKey( '/wp/v2/sidebars/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
464
tests/phpunit/tests/rest-api/rest-widget-types-controller.php
Normal file
464
tests/phpunit/tests/rest-api/rest-widget-types-controller.php
Normal file
@@ -0,0 +1,464 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_Test_REST_Widget_Types_Controller functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 5.8.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for WP_REST_Widget_Types_Controller.
|
||||
*
|
||||
* @since 5.8.0
|
||||
*
|
||||
* @see WP_TEST_REST_Controller_Testcase
|
||||
* @group restapi
|
||||
* @covers WP_REST_Widget_Types_Controller
|
||||
*/
|
||||
class WP_Test_REST_Widget_Types_Controller extends WP_Test_REST_Controller_Testcase {
|
||||
|
||||
/**
|
||||
* Admin user ID.
|
||||
*
|
||||
* @since 5.8.0
|
||||
*
|
||||
* @var int $subscriber_id
|
||||
*/
|
||||
protected static $admin_id;
|
||||
|
||||
/**
|
||||
* Subscriber user ID.
|
||||
*
|
||||
* @since 5.8.0
|
||||
*
|
||||
* @var int $subscriber_id
|
||||
*/
|
||||
protected static $subscriber_id;
|
||||
|
||||
/**
|
||||
* Create fake data before our tests run.
|
||||
*
|
||||
* @since 5.8.0
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$admin_id = $factory->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 41683
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$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<id>[a-zA-Z0-9_-]+)', $routes );
|
||||
$this->assertCount( 1, $routes['/wp/v2/widget-types/(?P<id>[a-zA-Z0-9_-]+)'] );
|
||||
$this->assertArrayHasKey( '/wp/v2/widget-types/(?P<id>[a-zA-Z0-9_-]+)/encode', $routes );
|
||||
$this->assertCount( 1, $routes['/wp/v2/widget-types/(?P<id>[a-zA-Z0-9_-]+)/encode'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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 41683
|
||||
*/
|
||||
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.8.0
|
||||
*
|
||||
* @param array $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'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 41683
|
||||
*/
|
||||
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(
|
||||
"<p>\n" .
|
||||
"\t\t\t<label for=\"widget-search--1-title\">Title:</label>\n" .
|
||||
"\t\t\t<input class=\"widefat\" id=\"widget-search--1-title\" name=\"widget-search[-1][title]\" type=\"text\" value=\"\" />\n" .
|
||||
"\t\t</p>",
|
||||
$data['form']
|
||||
);
|
||||
$this->assertStringMatchesFormat(
|
||||
"<div class=\"widget widget_search\"><form role=\"search\" method=\"get\" id=\"searchform\" class=\"searchform\" action=\"%s\">\n" .
|
||||
"\t\t\t\t<div>\n" .
|
||||
"\t\t\t\t\t<label class=\"screen-reader-text\" for=\"s\">Search for:</label>\n" .
|
||||
"\t\t\t\t\t<input type=\"text\" value=\"\" name=\"s\" id=\"s\" />\n" .
|
||||
"\t\t\t\t\t<input type=\"submit\" id=\"searchsubmit\" value=\"Search\" />\n" .
|
||||
"\t\t\t\t</div>\n" .
|
||||
"\t\t\t</form></div>",
|
||||
$data['preview']
|
||||
);
|
||||
$this->assertEqualSets(
|
||||
array(
|
||||
'encoded' => base64_encode( serialize( array() ) ),
|
||||
'hash' => wp_hash( serialize( array() ) ),
|
||||
'raw' => new stdClass,
|
||||
),
|
||||
$data['instance']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 41683
|
||||
*/
|
||||
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(
|
||||
"<p>\n" .
|
||||
"\t\t\t<label for=\"widget-search-8-title\">Title:</label>\n" .
|
||||
"\t\t\t<input class=\"widefat\" id=\"widget-search-8-title\" name=\"widget-search[8][title]\" type=\"text\" value=\"\" />\n" .
|
||||
"\t\t</p>",
|
||||
$data['form']
|
||||
);
|
||||
$this->assertStringMatchesFormat(
|
||||
"<div class=\"widget widget_search\"><form role=\"search\" method=\"get\" id=\"searchform\" class=\"searchform\" action=\"%s\">\n" .
|
||||
"\t\t\t\t<div>\n" .
|
||||
"\t\t\t\t\t<label class=\"screen-reader-text\" for=\"s\">Search for:</label>\n" .
|
||||
"\t\t\t\t\t<input type=\"text\" value=\"\" name=\"s\" id=\"s\" />\n" .
|
||||
"\t\t\t\t\t<input type=\"submit\" id=\"searchsubmit\" value=\"Search\" />\n" .
|
||||
"\t\t\t\t</div>\n" .
|
||||
"\t\t\t</form></div>",
|
||||
$data['preview']
|
||||
);
|
||||
$this->assertEqualSets(
|
||||
array(
|
||||
'encoded' => base64_encode( serialize( array() ) ),
|
||||
'hash' => wp_hash( serialize( array() ) ),
|
||||
'raw' => new stdClass,
|
||||
),
|
||||
$data['instance']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 41683
|
||||
*/
|
||||
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(
|
||||
"<p>\n" .
|
||||
"\t\t\t<label for=\"widget-search--1-title\">Title:</label>\n" .
|
||||
"\t\t\t<input class=\"widefat\" id=\"widget-search--1-title\" name=\"widget-search[-1][title]\" type=\"text\" value=\"Test title\" />\n" .
|
||||
"\t\t</p>",
|
||||
$data['form']
|
||||
);
|
||||
$this->assertStringMatchesFormat(
|
||||
"<div class=\"widget widget_search\"><h2 class=\"widgettitle\">Test title</h2><form role=\"search\" method=\"get\" id=\"searchform\" class=\"searchform\" action=\"%s\">\n" .
|
||||
"\t\t\t\t<div>\n" .
|
||||
"\t\t\t\t\t<label class=\"screen-reader-text\" for=\"s\">Search for:</label>\n" .
|
||||
"\t\t\t\t\t<input type=\"text\" value=\"\" name=\"s\" id=\"s\" />\n" .
|
||||
"\t\t\t\t\t<input type=\"submit\" id=\"searchsubmit\" value=\"Search\" />\n" .
|
||||
"\t\t\t\t</div>\n" .
|
||||
"\t\t\t</form></div>",
|
||||
$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']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 41683
|
||||
*/
|
||||
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(
|
||||
"<p>\n" .
|
||||
"\t\t\t<label for=\"widget-search--1-title\">Title:</label>\n" .
|
||||
"\t\t\t<input class=\"widefat\" id=\"widget-search--1-title\" name=\"widget-search[-1][title]\" type=\"text\" value=\"Updated title\" />\n" .
|
||||
"\t\t</p>",
|
||||
$data['form']
|
||||
);
|
||||
$this->assertStringMatchesFormat(
|
||||
"<div class=\"widget widget_search\"><h2 class=\"widgettitle\">Updated title</h2><form role=\"search\" method=\"get\" id=\"searchform\" class=\"searchform\" action=\"%s\">\n" .
|
||||
"\t\t\t\t<div>\n" .
|
||||
"\t\t\t\t\t<label class=\"screen-reader-text\" for=\"s\">Search for:</label>\n" .
|
||||
"\t\t\t\t\t<input type=\"text\" value=\"\" name=\"s\" id=\"s\" />\n" .
|
||||
"\t\t\t\t\t<input type=\"submit\" id=\"searchsubmit\" value=\"Search\" />\n" .
|
||||
"\t\t\t\t</div>\n" .
|
||||
"\t\t\t</form></div>",
|
||||
$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']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 41683
|
||||
*/
|
||||
public function test_encode_form_data_no_raw() {
|
||||
global $wp_widget_factory;
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$wp_widget_factory->widgets['WP_Widget_Search']->widget_options['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(
|
||||
"<p>\n" .
|
||||
"\t\t\t<label for=\"widget-search--1-title\">Title:</label>\n" .
|
||||
"\t\t\t<input class=\"widefat\" id=\"widget-search--1-title\" name=\"widget-search[-1][title]\" type=\"text\" value=\"Test title\" />\n" .
|
||||
"\t\t</p>",
|
||||
$data['form']
|
||||
);
|
||||
$this->assertStringMatchesFormat(
|
||||
"<div class=\"widget widget_search\"><h2 class=\"widgettitle\">Test title</h2><form role=\"search\" method=\"get\" id=\"searchform\" class=\"searchform\" action=\"%s\">\n" .
|
||||
"\t\t\t\t<div>\n" .
|
||||
"\t\t\t\t\t<label class=\"screen-reader-text\" for=\"s\">Search for:</label>\n" .
|
||||
"\t\t\t\t\t<input type=\"text\" value=\"\" name=\"s\" id=\"s\" />\n" .
|
||||
"\t\t\t\t\t<input type=\"submit\" id=\"searchsubmit\" value=\"Search\" />\n" .
|
||||
"\t\t\t\t</div>\n" .
|
||||
"\t\t\t</form></div>",
|
||||
$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']->widget_options['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() {}
|
||||
}
|
||||
1388
tests/phpunit/tests/rest-api/rest-widgets-controller.php
Normal file
1388
tests/phpunit/tests/rest-api/rest-widgets-controller.php
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user