Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ public function get_items_permissions_check( $request ) {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
retrieve_widgets();

$data = array();
foreach ( (array) wp_get_sidebars_widgets() as $id => $widgets ) {
foreach ( wp_get_sidebars_widgets() as $id => $widgets ) {
$sidebar = $this->get_sidebar( $id );

if ( ! $sidebar ) {
Expand Down Expand Up @@ -135,6 +137,8 @@ public function get_item_permissions_check( $request ) {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_item( $request ) {
retrieve_widgets();

$sidebar = $this->get_sidebar( $request['id'] );

if ( ! $sidebar ) {
Expand Down
77 changes: 76 additions & 1 deletion tests/phpunit/tests/rest-api/rest-sidebars-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
*/

/**
* Tests for REST API for Menus.
* Tests for REST API for Widgets.
*
* @see WP_Test_REST_Controller_Testcase
* @group restapi
* @group widgets
* @covers WP_REST_Sidebars_Controller
*/
class WP_Test_REST_Sidebars_Controller extends WP_Test_REST_Controller_Testcase {
Expand Down Expand Up @@ -61,6 +62,18 @@ public function setUp() {
update_option( 'sidebars_widgets', array() );
}

function clean_up_global_scope() {
global $wp_widget_factory, $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates;

$wp_registered_sidebars = array();
$wp_registered_widgets = array();
$wp_registered_widget_controls = array();
$wp_registered_widget_updates = array();
$wp_widget_factory->widgets = array();

parent::clean_up_global_scope();
}

private function setup_widget( $option_name, $number, $settings ) {
update_option(
$option_name,
Expand Down Expand Up @@ -128,6 +141,8 @@ public function test_context_param() {
* @ticket 41683
*/
public function test_get_items() {
wp_widgets_init();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this go in setUp() so that we can avoid some repetition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explored doing that. But it skews all the other tests that don't have it. In reviewing other widget tests, this init function is placed in each test that needs it. Not sure why yet. But it's on my test review TODO list across the test suite.


$request = new WP_REST_Request( 'GET', '/wp/v2/sidebars' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
Expand Down Expand Up @@ -193,6 +208,8 @@ public function test_get_items_basic_sidebar() {
* @ticket 41683
*/
public function test_get_items_active_sidebar_with_widgets() {
wp_widgets_init();

$this->setup_widget(
'widget_rss',
1,
Expand Down Expand Up @@ -241,6 +258,56 @@ public function test_get_items_active_sidebar_with_widgets() {
);
}

/**
* @ticket 53489
*/
public function test_get_items_when_registering_new_sidebars() {
register_sidebar(
array(
'name' => 'New Sidebar',
'id' => 'new-sidebar',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
)
);

$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->assertSame(
array(
array(
'id' => 'wp_inactive_widgets',
'name' => 'Inactive widgets',
'description' => '',
'class' => '',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
'status' => 'inactive',
'widgets' => array(),
),
array(
'id' => 'new-sidebar',
'name' => 'New Sidebar',
'description' => '',
'class' => '',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
'status' => 'active',
'widgets' => array(),
),
),
$data
);
}

/**
* @ticket 41683
*/
Expand Down Expand Up @@ -317,6 +384,8 @@ public function test_create_item() {
* @ticket 41683
*/
public function test_update_item() {
wp_widgets_init();

$this->setup_widget(
'widget_rss',
1,
Expand Down Expand Up @@ -382,6 +451,8 @@ public function test_update_item() {
* @ticket 41683
*/
public function test_update_item_removes_widget_from_existing_sidebar() {
wp_widgets_init();

$this->setup_widget(
'widget_text',
1,
Expand Down Expand Up @@ -423,6 +494,8 @@ public function test_update_item_removes_widget_from_existing_sidebar() {
* @ticket 41683
*/
public function test_update_item_moves_omitted_widget_to_inactive_sidebar() {
wp_widgets_init();

$this->setup_widget(
'widget_text',
1,
Expand Down Expand Up @@ -465,6 +538,8 @@ public function test_update_item_moves_omitted_widget_to_inactive_sidebar() {
* @ticket 41683
*/
public function test_get_items_inactive_widgets() {
wp_widgets_init();

$this->setup_widget(
'widget_rss',
1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function test_context_param() {
* @ticket 41683
*/
public function test_get_items() {
wp_widgets_init();
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/widget-types' );
$response = rest_get_server()->dispatch( $request );
Expand Down