mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
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:
@@ -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 )
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
95
tests/phpunit/tests/rest-api/rest-site-health-controller.php
Normal file
95
tests/phpunit/tests/rest-api/rest-site-health-controller.php
Normal 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'] );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user