|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | /** |
11 | | - * Tests for REST API for Menus. |
| 11 | + * Tests for REST API for Widgets. |
12 | 12 | * |
13 | 13 | * @see WP_Test_REST_Controller_Testcase |
14 | 14 | * @group restapi |
| 15 | + * @group widgets |
15 | 16 | * @covers WP_REST_Sidebars_Controller |
16 | 17 | */ |
17 | 18 | class WP_Test_REST_Sidebars_Controller extends WP_Test_REST_Controller_Testcase { |
@@ -61,6 +62,18 @@ public function setUp() { |
61 | 62 | update_option( 'sidebars_widgets', array() ); |
62 | 63 | } |
63 | 64 |
|
| 65 | + function clean_up_global_scope() { |
| 66 | + global $wp_widget_factory, $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates; |
| 67 | + |
| 68 | + $wp_registered_sidebars = array(); |
| 69 | + $wp_registered_widgets = array(); |
| 70 | + $wp_registered_widget_controls = array(); |
| 71 | + $wp_registered_widget_updates = array(); |
| 72 | + $wp_widget_factory->widgets = array(); |
| 73 | + |
| 74 | + parent::clean_up_global_scope(); |
| 75 | + } |
| 76 | + |
64 | 77 | private function setup_widget( $option_name, $number, $settings ) { |
65 | 78 | update_option( |
66 | 79 | $option_name, |
@@ -128,6 +141,8 @@ public function test_context_param() { |
128 | 141 | * @ticket 41683 |
129 | 142 | */ |
130 | 143 | public function test_get_items() { |
| 144 | + wp_widgets_init(); |
| 145 | + |
131 | 146 | $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars' ); |
132 | 147 | $response = rest_get_server()->dispatch( $request ); |
133 | 148 | $data = $response->get_data(); |
@@ -193,6 +208,8 @@ public function test_get_items_basic_sidebar() { |
193 | 208 | * @ticket 41683 |
194 | 209 | */ |
195 | 210 | public function test_get_items_active_sidebar_with_widgets() { |
| 211 | + wp_widgets_init(); |
| 212 | + |
196 | 213 | $this->setup_widget( |
197 | 214 | 'widget_rss', |
198 | 215 | 1, |
@@ -241,6 +258,56 @@ public function test_get_items_active_sidebar_with_widgets() { |
241 | 258 | ); |
242 | 259 | } |
243 | 260 |
|
| 261 | + /** |
| 262 | + * @ticket 53489 |
| 263 | + */ |
| 264 | + public function test_get_items_when_registering_new_sidebars() { |
| 265 | + register_sidebar( |
| 266 | + array( |
| 267 | + 'name' => 'New Sidebar', |
| 268 | + 'id' => 'new-sidebar', |
| 269 | + 'before_widget' => '', |
| 270 | + 'after_widget' => '', |
| 271 | + 'before_title' => '', |
| 272 | + 'after_title' => '', |
| 273 | + ) |
| 274 | + ); |
| 275 | + |
| 276 | + $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars' ); |
| 277 | + $response = rest_get_server()->dispatch( $request ); |
| 278 | + $data = $response->get_data(); |
| 279 | + $data = $this->remove_links( $data ); |
| 280 | + $this->assertSame( |
| 281 | + array( |
| 282 | + array( |
| 283 | + 'id' => 'wp_inactive_widgets', |
| 284 | + 'name' => 'Inactive widgets', |
| 285 | + 'description' => '', |
| 286 | + 'class' => '', |
| 287 | + 'before_widget' => '', |
| 288 | + 'after_widget' => '', |
| 289 | + 'before_title' => '', |
| 290 | + 'after_title' => '', |
| 291 | + 'status' => 'inactive', |
| 292 | + 'widgets' => array(), |
| 293 | + ), |
| 294 | + array( |
| 295 | + 'id' => 'new-sidebar', |
| 296 | + 'name' => 'New Sidebar', |
| 297 | + 'description' => '', |
| 298 | + 'class' => '', |
| 299 | + 'before_widget' => '', |
| 300 | + 'after_widget' => '', |
| 301 | + 'before_title' => '', |
| 302 | + 'after_title' => '', |
| 303 | + 'status' => 'active', |
| 304 | + 'widgets' => array(), |
| 305 | + ), |
| 306 | + ), |
| 307 | + $data |
| 308 | + ); |
| 309 | + } |
| 310 | + |
244 | 311 | /** |
245 | 312 | * @ticket 41683 |
246 | 313 | */ |
@@ -317,6 +384,8 @@ public function test_create_item() { |
317 | 384 | * @ticket 41683 |
318 | 385 | */ |
319 | 386 | public function test_update_item() { |
| 387 | + wp_widgets_init(); |
| 388 | + |
320 | 389 | $this->setup_widget( |
321 | 390 | 'widget_rss', |
322 | 391 | 1, |
@@ -382,6 +451,8 @@ public function test_update_item() { |
382 | 451 | * @ticket 41683 |
383 | 452 | */ |
384 | 453 | public function test_update_item_removes_widget_from_existing_sidebar() { |
| 454 | + wp_widgets_init(); |
| 455 | + |
385 | 456 | $this->setup_widget( |
386 | 457 | 'widget_text', |
387 | 458 | 1, |
@@ -423,6 +494,8 @@ public function test_update_item_removes_widget_from_existing_sidebar() { |
423 | 494 | * @ticket 41683 |
424 | 495 | */ |
425 | 496 | public function test_update_item_moves_omitted_widget_to_inactive_sidebar() { |
| 497 | + wp_widgets_init(); |
| 498 | + |
426 | 499 | $this->setup_widget( |
427 | 500 | 'widget_text', |
428 | 501 | 1, |
@@ -465,6 +538,8 @@ public function test_update_item_moves_omitted_widget_to_inactive_sidebar() { |
465 | 538 | * @ticket 41683 |
466 | 539 | */ |
467 | 540 | public function test_get_items_inactive_widgets() { |
| 541 | + wp_widgets_init(); |
| 542 | + |
468 | 543 | $this->setup_widget( |
469 | 544 | 'widget_rss', |
470 | 545 | 1, |
|
0 commit comments