Skip to content

Commit

Permalink
REST API: Remove duplicates in the widget types endpoint.
Browse files Browse the repository at this point in the history
Props noisysocks, spacedmonkey, imath, isabel_brison.
Fixes #53305.


git-svn-id: https://develop.svn.wordpress.org/trunk@51049 602fd350-edb4-49c9-b593-d223f7449a82
TimothyBJacobs committed May 31, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 712ca22 commit 8edfb80
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -225,7 +225,7 @@ protected function get_widgets() {
}
$widget['classname'] = ltrim( $classname, '_' );

$widgets[] = $widget;
$widgets[ $widget['id'] ] = $widget;
}

return $widgets;
46 changes: 46 additions & 0 deletions tests/phpunit/tests/rest-api/rest-widget-types-controller.php
Original file line number Diff line number Diff line change
@@ -61,6 +61,22 @@ public static function wpTearDownAfterClass() {
self::delete_user( self::$subscriber_id );
}

private function setup_widget( $id_base, $number, $settings ) {
global $wp_widget_factory;

$option_name = "widget_$id_base";
update_option(
$option_name,
array(
$number => $settings,
)
);

$widget_object = $wp_widget_factory->get_widget_object( $id_base );
$widget_object->_set( $number );
$widget_object->_register_one( $number );
}

/**
* @ticket 41683
*/
@@ -106,7 +122,37 @@ public function test_get_items() {
$widget_type = $endpoint->get_widget( $item['name'] );
$this->check_widget_type_object( $widget_type, $item, $item['_links'] );
}
}

/**
* @ticket 53305
*/
public function test_get_items_removes_duplicates() {
wp_set_current_user( self::$admin_id );
$this->setup_widget(
'text',
1,
array(
'text' => 'Custom text test',
)
);
$this->setup_widget(
'text',
2,
array(
'text' => 'Custom text test',
)
);
$request = new WP_REST_Request( 'GET', '/wp/v2/widget-types' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$text_widgets = array_filter(
$data,
function( $widget ) {
return 'text' === $widget['id'];
}
);
$this->assertCount( 1, $text_widgets );
}

/**

0 comments on commit 8edfb80

Please sign in to comment.