-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This introduce a `plugins` REST API route for managing and installing plugins. The block directory routes have been updated to wrap this API using inner rest requests. Co-authored-by: tellyworth <[email protected]>
- Loading branch information
1 parent
c711a16
commit 26363cf
Showing
10 changed files
with
2,553 additions
and
164 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
phpunit/class-wp-rest-block-directory-controller-test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
<?php | ||
|
||
/** | ||
* Test WP_REST_Block_Directory_Controller_Test() | ||
* | ||
* @package Gutenberg | ||
* phpcs:disable | ||
*/ | ||
class WP_REST_Block_Directory_Controller_Test extends WP_Test_REST_Controller_Testcase { | ||
protected static $admin_id; | ||
|
||
public static function wpSetUpBeforeClass( $factory ) { | ||
self::$admin_id = $factory->user->create( | ||
array( | ||
'role' => 'administrator', | ||
) | ||
); | ||
|
||
if ( is_multisite() ) { | ||
grant_super_admin( self::$admin_id ); | ||
} | ||
|
||
if ( ! defined( 'FS_METHOD' ) ) { | ||
define( 'FS_METHOD', 'direct' ); | ||
} | ||
} | ||
|
||
public static function wpTearDownAfterClass() { | ||
self::delete_user( self::$admin_id ); | ||
} | ||
|
||
public function test_register_routes() { | ||
$routes = rest_get_server()->get_routes(); | ||
|
||
$this->assertArrayHasKey( '/__experimental/block-directory/search', $routes ); | ||
$this->assertArrayHasKey( '/__experimental/block-directory/install', $routes ); | ||
$this->assertArrayHasKey( '/__experimental/block-directory/uninstall', $routes ); | ||
} | ||
|
||
public function test_context_param() { | ||
// Collection. | ||
$request = new WP_REST_Request( 'OPTIONS', '/__experimental/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'] ); | ||
} | ||
|
||
public function test_get_items() { | ||
wp_set_current_user( self::$admin_id ); | ||
|
||
$request = new WP_REST_Request( 'GET', '/__experimental/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 ); | ||
} | ||
|
||
public function test_get_items_wdotorg_unavailable() { | ||
wp_set_current_user( self::$admin_id ); | ||
|
||
$request = new WP_REST_Request( 'GET', '/__experimental/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 ); | ||
} | ||
|
||
public function test_get_items_logged_out() { | ||
$request = new WP_REST_Request( 'GET', '/__experimental/block-directory/search' ); | ||
$request->set_query_params( array( 'term' => 'foo' ) ); | ||
$response = rest_do_request( $request ); | ||
$this->assertErrorResponse( 'rest_block_directory_cannot_view', $response ); | ||
} | ||
|
||
public function test_get_items_no_results() { | ||
wp_set_current_user( self::$admin_id ); | ||
|
||
$request = new WP_REST_Request( 'GET', '/__experimental/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() { | ||
if ( isset( get_plugins()['hello-dolly/hello.php'] ) ) { | ||
delete_plugins( array( 'hello-dolly/hello.php' ) ); | ||
} | ||
|
||
wp_set_current_user( self::$admin_id ); | ||
|
||
$request = new WP_REST_Request( 'POST', '/__experimental/block-directory/install' ); | ||
$request->set_body_params( array( 'slug' => 'hello-dolly' ) ); | ||
|
||
$response = rest_do_request( $request ); | ||
$this->skip_on_filesystem_error( $response ); | ||
$this->assertNotWPError( $response->as_error() ); | ||
$this->assertEquals( 201, $response->get_status() ); | ||
$this->assertEquals( 'Hello Dolly', $response->get_data()['name'] ); | ||
} | ||
|
||
public function test_update_item() { | ||
$this->markTestSkipped( 'Controller does not have update_item route.' ); | ||
} | ||
|
||
public function test_delete_item() { | ||
$this->markTestSkipped( 'Covered by Plugins controller tests.' ); | ||
} | ||
|
||
public function test_prepare_item() { | ||
wp_set_current_user( self::$admin_id ); | ||
|
||
// This will hit the live API. We're searching for `block` which should definitely return at least one result. | ||
$request = new WP_REST_Request( 'GET', '/__experimental/block-directory/search' ); | ||
$request->set_query_params( array( 'term' => 'block' ) ); | ||
$response = rest_do_request( $request ); | ||
$data = $response->get_data(); | ||
|
||
$this->assertEquals( 200, $response->status ); | ||
// At least one result | ||
$this->assertGreaterThanOrEqual( 1, count( $data ) ); | ||
// Each result should be an object with important attributes set | ||
foreach ( $data as $plugin ) { | ||
$this->assertArrayHasKey( 'name', $plugin ); | ||
$this->assertArrayHasKey( 'title', $plugin ); | ||
$this->assertArrayHasKey( 'id', $plugin ); | ||
$this->assertArrayHasKey( 'author_block_rating', $plugin ); | ||
$this->assertArrayHasKey( 'assets', $plugin ); | ||
$this->assertArrayHasKey( 'humanized_updated', $plugin ); | ||
} | ||
} | ||
|
||
public function test_get_item_schema() { | ||
wp_set_current_user( self::$admin_id ); | ||
|
||
$request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-directory/search' ); | ||
$request->set_query_params( array( 'term' => 'foo' ) ); | ||
$response = rest_do_request( $request ); | ||
$data = $response->get_data(); | ||
|
||
// Check endpoints | ||
$this->assertEquals( [ 'GET' ], $data['endpoints'][0]['methods'] ); | ||
$this->assertTrue( $data['endpoints'][0]['args']['term'][ 'required'] ); | ||
|
||
$properties = $data['schema']['properties']; | ||
|
||
$this->assertCount( 13, $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( 'humanized_updated', $properties ); | ||
$this->assertArrayHasKey( 'assets', $properties ); | ||
} | ||
|
||
/** | ||
* Skips the test if the response is an error due to the filesystem being unavailable. | ||
* | ||
* @since 5.5.0 | ||
* | ||
* @param WP_REST_Response $response The response object to inspect. | ||
*/ | ||
protected function skip_on_filesystem_error( WP_REST_Response $response ) { | ||
if ( ! $response->is_error() ) { | ||
return; | ||
} | ||
|
||
$code = $response->as_error()->get_error_code(); | ||
|
||
if ( 'fs_unavailable' === $code || false !== strpos( $code, 'mkdir_failed' ) ) { | ||
$this->markTestSkipped( 'Filesystem is unavailable.' ); | ||
} | ||
} | ||
|
||
/** | ||
* Simulate a network failure on outbound http requests to a given hostname. | ||
* | ||
* @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 | ||
); | ||
} | ||
} |
Oops, something went wrong.