Site Health, REST API: Move async tests to REST API endpoints.

This provides more flexibility when writing tests and benefits from running in a front-end context which is necessary for some tests like checking that updates are supported. Additionally, this provides a more robust interface for developers who want to integrate with Site Health tests.

Because the `wp/v2` endpoint is reserved for modeling core entities, site health is registered in its own `wp-site-health/v1` namespace.

The existing ajax actions have been maintained for backward compatibility.

Props Clorith, chrisvanpatten, afragen, pokhriyal, TimothyBlynJacobs.
Fixes #48105.


git-svn-id: https://develop.svn.wordpress.org/trunk@49154 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs
2020-10-15 01:58:28 +00:00
parent 50fa352cbb
commit efe06cdcea
12 changed files with 682 additions and 91 deletions

View File

@@ -132,6 +132,11 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
'/wp/v2/plugins',
'/wp/v2/plugins/(?P<plugin>[^.\/]+(?:\/[^.\/]+)?)',
'/wp/v2/block-directory/search',
'/wp-site-health/v1',
'/wp-site-health/v1/tests/background-updates',
'/wp-site-health/v1/tests/loopback-requests',
'/wp-site-health/v1/tests/dotorg-communication',
'/wp-site-health/v1/directory-sizes',
);
$this->assertSameSets( $expected_routes, $routes );
@@ -141,7 +146,8 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
return (
'/' === $route ||
preg_match( '#^/oembed/1\.0(/.+)?$#', $route ) ||
preg_match( '#^/wp/v2(/.+)?$#', $route )
preg_match( '#^/wp/v2(/.+)?$#', $route ) ||
preg_match( '#^/wp-site-health/v1(/.+)?$#', $route )
);
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* Unit tests covering the site health controller.
*
* Also generates the fixture data used by the wp-api.js QUnit tests.
*
* @package WordPress
* @subpackage REST API
* @since 5.6.0
*/
/**
* @group restapi
*/
class WP_Test_REST_Site_Health_Controller extends WP_Test_REST_TestCase {
/**
* Subscriber user ID.
*
* @since 5.6.0
*
* @var int
*/
private static $subscriber;
/**
* Administrator user id.
*
* @since 5.6.0
*
* @var int
*/
private static $admin;
/**
* Set up class test fixtures.
*
* @since 5.6.0
*
* @param WP_UnitTest_Factory $factory WordPress unit test factory.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$subscriber = $factory->user->create(
array(
'role' => 'subscriber',
)
);
self::$admin = $factory->user->create(
array(
'role' => 'administrator',
)
);
}
/**
* Clean up test fixtures.
*
* @since 5.6.0
*/
public static function wpTearDownAfterClass() {
self::delete_user( self::$subscriber );
self::delete_user( self::$admin );
}
public function test_logged_out() {
$response = rest_do_request( '/wp-site-health/v1/tests/dotorg-communication' );
$this->assertErrorResponse( 'rest_forbidden', $response, 401 );
}
public function test_insufficient_caps() {
wp_set_current_user( self::$subscriber );
$response = rest_do_request( '/wp-site-health/v1/tests/dotorg-communication' );
$this->assertErrorResponse( 'rest_forbidden', $response, 403 );
}
public function test_custom_capability() {
wp_set_current_user( self::$admin );
add_filter(
'site_health_test_rest_capability_dotorg_communication',
static function () {
return 'a_custom_capability';
}
);
$response = rest_do_request( '/wp-site-health/v1/tests/dotorg-communication' );
$this->assertErrorResponse( 'rest_forbidden', $response, 403 );
}
public function test() {
wp_set_current_user( self::$admin );
$response = rest_do_request( '/wp-site-health/v1/tests/dotorg-communication' );
$this->assertEquals( 'dotorg_communication', $response->get_data()['test'] );
}
}