mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-03-30 10:14:31 +00:00
REST API: Introduce plugin management and block directory endpoints.
These endpoints facilitate the Block Directory Inserter feature in Gutenberg. Users can now install, activate, deactivate, and delete plugins over the REST API. The block directoryendpoint allows searching for available blocks from the WordPress.org block directory. Props cklee, talldanwp, noisysocks, joen, soean, youknowriad, dufresnesteven, gziolo, dd32, tellyworth, ryelle, spacedmonkey, TimothyBlynJacobs. Fixes #50321. git-svn-id: https://develop.svn.wordpress.org/trunk@48242 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
BIN
tests/phpunit/data/plugins/link-manager.zip
Normal file
BIN
tests/phpunit/data/plugins/link-manager.zip
Normal file
Binary file not shown.
297
tests/phpunit/tests/rest-api/rest-block-directory-controller.php
Normal file
297
tests/phpunit/tests/rest-api/rest-block-directory-controller.php
Normal file
@@ -0,0 +1,297 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_REST_Block_Directory_Controller functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST API
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group restapi
|
||||
*/
|
||||
class WP_REST_Block_Directory_Controller_Test extends WP_Test_REST_Controller_Testcase {
|
||||
|
||||
/**
|
||||
* Administrator user id.
|
||||
*
|
||||
* @since 5.5.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected static $admin_id;
|
||||
|
||||
/**
|
||||
* Set up class test fixtures.
|
||||
*
|
||||
* @since 5.5.0
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory WordPress unit test factory.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$admin_id = $factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_multisite() ) {
|
||||
grant_super_admin( self::$admin_id );
|
||||
}
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
self::delete_user( self::$admin_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50321
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = rest_get_server()->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( '/wp/v2/block-directory/search', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50321
|
||||
*/
|
||||
public function test_context_param() {
|
||||
// Collection.
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/block-directory/search' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
|
||||
$this->assertEquals( array( 'view' ), $data['endpoints'][0]['args']['context']['enum'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50321
|
||||
*/
|
||||
public function test_get_items() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/block-directory/search' );
|
||||
$request->set_query_params( array( 'term' => 'foo' ) );
|
||||
|
||||
$result = rest_do_request( $request );
|
||||
$this->assertNotWPError( $result->as_error() );
|
||||
$this->assertEquals( 200, $result->status );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50321
|
||||
*/
|
||||
public function test_get_items_wdotorg_unavailable() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/block-directory/search' );
|
||||
$request->set_query_params( array( 'term' => 'foo' ) );
|
||||
|
||||
$this->prevent_requests_to_host( 'api.wordpress.org' );
|
||||
|
||||
$this->expectException( 'PHPUnit_Framework_Error_Warning' );
|
||||
$response = rest_do_request( $request );
|
||||
$this->assertErrorResponse( 'plugins_api_failed', $response, 500 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50321
|
||||
*/
|
||||
public function test_get_items_logged_out() {
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/block-directory/search' );
|
||||
$request->set_query_params( array( 'term' => 'foo' ) );
|
||||
$response = rest_do_request( $request );
|
||||
$this->assertErrorResponse( 'rest_block_directory_cannot_view', $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50321
|
||||
*/
|
||||
public function test_get_items_no_results() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/block-directory/search' );
|
||||
$request->set_query_params( array( 'term' => '0c4549ee68f24eaaed46a49dc983ecde' ) );
|
||||
$response = rest_do_request( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
// Should produce a 200 status with an empty array.
|
||||
$this->assertEquals( 200, $response->status );
|
||||
$this->assertEquals( array(), $data );
|
||||
}
|
||||
|
||||
public function test_get_item() {
|
||||
$this->markTestSkipped( 'Controller does not have get_item route.' );
|
||||
}
|
||||
|
||||
public function test_create_item() {
|
||||
$this->markTestSkipped( 'Controller does not have create_item route.' );
|
||||
}
|
||||
|
||||
public function test_update_item() {
|
||||
$this->markTestSkipped( 'Controller does not have update_item route.' );
|
||||
}
|
||||
|
||||
public function test_delete_item() {
|
||||
$this->markTestSkipped( 'Controller does not have delete_item route.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50321
|
||||
*/
|
||||
public function test_prepare_item() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$controller = new WP_REST_Block_Directory_Controller();
|
||||
|
||||
$plugin = $this->get_mock_plugin();
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/block-directory/search' );
|
||||
$request->set_query_params( array( 'term' => 'block' ) );
|
||||
|
||||
$response = $controller->prepare_item_for_response( $plugin, $request );
|
||||
|
||||
$expected = array(
|
||||
'name' => 'sortabrilliant/guidepost',
|
||||
'title' => 'Guidepost',
|
||||
'description' => 'A guidepost gives you directions. It lets you know where you’re going. It gives you a preview of what’s to come. How does it work? Guideposts are magic, no they...',
|
||||
'id' => 'guidepost',
|
||||
'rating' => 4.3,
|
||||
'rating_count' => 90,
|
||||
'active_installs' => 100,
|
||||
'author_block_rating' => 0,
|
||||
'author_block_count' => 1,
|
||||
'author' => 'sorta brilliant',
|
||||
'icon' => 'https://ps.w.org/guidepost/assets/icon-128x128.jpg?rev=2235512',
|
||||
'assets' => array(
|
||||
'https://ps.w.org/guidepost/tags/1.2.1/build/index.js?v=1584940380',
|
||||
'https://ps.w.org/guidepost/tags/1.2.1/build/guidepost-editor.css?v=1584940380',
|
||||
'https://ps.w.org/guidepost/tags/1.2.1/build/guidepost-style.css?v=1584940380',
|
||||
'https://ps.w.org/guidepost/tags/1.2.1/build/guidepost-theme.js?v=1584940380',
|
||||
),
|
||||
'last_updated' => '2020-03-23T05:13:00',
|
||||
'humanized_updated' => '3 months ago',
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected, $response->get_data() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50321
|
||||
*/
|
||||
public function test_get_item_schema() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/block-directory/search' );
|
||||
$request->set_query_params( array( 'term' => 'foo' ) );
|
||||
$response = rest_do_request( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
// Check endpoints
|
||||
$this->assertEquals( array( 'GET' ), $data['endpoints'][0]['methods'] );
|
||||
$this->assertTrue( $data['endpoints'][0]['args']['term']['required'] );
|
||||
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertCount( 14, $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'title', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'rating', $properties );
|
||||
$this->assertArrayHasKey( 'rating_count', $properties );
|
||||
$this->assertArrayHasKey( 'active_installs', $properties );
|
||||
$this->assertArrayHasKey( 'author_block_rating', $properties );
|
||||
$this->assertArrayHasKey( 'author_block_count', $properties );
|
||||
$this->assertArrayHasKey( 'author', $properties );
|
||||
$this->assertArrayHasKey( 'icon', $properties );
|
||||
$this->assertArrayHasKey( 'last_updated', $properties );
|
||||
$this->assertArrayHasKey( 'humanized_updated', $properties );
|
||||
$this->assertArrayHasKey( 'assets', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate a network failure on outbound http requests to a given hostname.
|
||||
*
|
||||
* @since 5.5.0
|
||||
*
|
||||
* @param string $blocked_host The host to block connections to.
|
||||
*/
|
||||
private function prevent_requests_to_host( $blocked_host = 'api.wordpress.org' ) {
|
||||
add_filter(
|
||||
'pre_http_request',
|
||||
static function ( $return, $args, $url ) use ( $blocked_host ) {
|
||||
if ( @parse_url( $url, PHP_URL_HOST ) === $blocked_host ) {
|
||||
return new WP_Error( 'plugins_api_failed', "An expected error occurred connecting to $blocked_host because of a unit test", "cURL error 7: Failed to connect to $blocked_host port 80: Connection refused" );
|
||||
|
||||
}
|
||||
|
||||
return $return;
|
||||
},
|
||||
10,
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an example of the data returned from the {@see plugins_api()} for a block.
|
||||
*
|
||||
* @since 5.5.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_mock_plugin() {
|
||||
return array(
|
||||
'name' => 'Guidepost',
|
||||
'slug' => 'guidepost',
|
||||
'version' => '1.2.1',
|
||||
'author' => '<a href="https://sortabrilliant.com">sorta brilliant</a>',
|
||||
'author_profile' => 'https://profiles.wordpress.org/sortabrilliant',
|
||||
'requires' => '5.0',
|
||||
'tested' => '5.4.0',
|
||||
'requires_php' => '5.6',
|
||||
'rating' => 86,
|
||||
'ratings' => array(
|
||||
5 => 50,
|
||||
4 => 25,
|
||||
3 => 7,
|
||||
2 => 5,
|
||||
1 => 3,
|
||||
),
|
||||
'num_ratings' => 90,
|
||||
'support_threads' => 1,
|
||||
'support_threads_resolved' => 0,
|
||||
'active_installs' => 100,
|
||||
'downloaded' => 1112,
|
||||
'last_updated' => '2020-03-23 5:13am GMT',
|
||||
'added' => '2020-01-29',
|
||||
'homepage' => 'https://sortabrilliant.com/guidepost/',
|
||||
'description' => '<p>A guidepost gives you directions. It lets you know where you’re going. It gives you a preview of what’s to come. How does it work? Guideposts are magic, no they really are.</p>',
|
||||
'short_description' => 'A guidepost gives you directions. It lets you know where you’re going. It gives you a preview of what’s to come.',
|
||||
'download_link' => 'https://downloads.wordpress.org/plugin/guidepost.1.2.1.zip',
|
||||
'tags' => array(
|
||||
'block' => 'block',
|
||||
'heading' => 'heading',
|
||||
'style' => 'style',
|
||||
),
|
||||
'donate_link' => '',
|
||||
'icons' => array(
|
||||
'1x' => 'https://ps.w.org/guidepost/assets/icon-128x128.jpg?rev=2235512',
|
||||
'2x' => 'https://ps.w.org/guidepost/assets/icon-256x256.jpg?rev=2235512',
|
||||
),
|
||||
'blocks' => array(
|
||||
'sortabrilliant/guidepost' => array(
|
||||
'name' => 'sortabrilliant/guidepost',
|
||||
'title' => 'Guidepost',
|
||||
),
|
||||
),
|
||||
'block_assets' => array(
|
||||
0 => '/tags/1.2.1/build/index.js',
|
||||
1 => '/tags/1.2.1/build/guidepost-editor.css',
|
||||
2 => '/tags/1.2.1/build/guidepost-style.css',
|
||||
3 => '/tags/1.2.1/build/guidepost-theme.js',
|
||||
),
|
||||
'author_block_count' => 1,
|
||||
'author_block_rating' => 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
1127
tests/phpunit/tests/rest-api/rest-plugins-controller.php
Normal file
1127
tests/phpunit/tests/rest-api/rest-plugins-controller.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -126,6 +126,9 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
|
||||
'/wp/v2/block-types/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9_-]+)',
|
||||
'/wp/v2/settings',
|
||||
'/wp/v2/themes',
|
||||
'/wp/v2/plugins',
|
||||
'/wp/v2/plugins/(?P<plugin>[^.\/]+(?:\/[^.\/]+)?)',
|
||||
'/wp/v2/block-directory/search',
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected_routes, $routes );
|
||||
|
||||
Reference in New Issue
Block a user