Optimize system status tests (#39363)

This commit is contained in:
Leif Singer 2023-07-27 22:29:59 +02:00 committed by Kyle Nel
parent fccc62d91a
commit 144e873393
No known key found for this signature in database
3 changed files with 118 additions and 93 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: dev
Optimized the System Status Report unit tests.

View File

@ -17,18 +17,52 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
use ArraySubsetAsserts;
/**
* Setup our test server.
* User variable.
*
* @var int
*/
public function setUp(): void {
parent::setUp();
$this->endpoint = new WC_REST_System_Status_Controller();
$this->user = $this->factory->user->create(
protected static int $administrator_user;
/**
* User variable.
*
* @var int
*/
protected static int $no_user = 0;
/**
* Setup once before running tests.
*
* @param object $factory Factory object.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$administrator_user = $factory->user->create(
array(
'role' => 'administrator',
)
);
}
/**
* Fetches the System Status Report data and caches it.
* @param int $user The ID of a WordPress user to switch to before fetching the data.
* @return Array An array of the data returned by the System Status Report endpoint.
*/
private function fetch_or_get_system_status_data_for_user( int $user ) {
if ( $user < 0 ) {
return null;
}
static $system_status_data = array();
if ( ! isset( $system_status_data[ 'user' . $user ] ) ) {
wp_set_current_user( $user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
$data = $response->get_data();
$system_status_data[ 'user' . $user ] = $data;
}
return $system_status_data[ 'user' . $user ];
}
/**
* Test route registration.
*/
@ -45,7 +79,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
* @since 3.0.0
*/
public function test_get_system_status_info_without_permission() {
wp_set_current_user( 0 );
wp_set_current_user( self::$no_user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
$this->assertEquals( 401, $response->get_status() );
}
@ -58,18 +92,14 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
*/
public function test_get_system_status_info_returns_root_properties() {
$this->skip_on_php_8_1();
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
$data = $response->get_data();
$this->assertArrayHasKey( 'environment', $data );
$this->assertArrayHasKey( 'database', $data );
$this->assertArrayHasKey( 'active_plugins', $data );
$this->assertArrayHasKey( 'theme', $data );
$this->assertArrayHasKey( 'settings', $data );
$this->assertArrayHasKey( 'security', $data );
$this->assertArrayHasKey( 'pages', $data );
$system_status_data = $this->fetch_or_get_system_status_data_for_user( self::$administrator_user );
$this->assertArrayHasKey( 'environment', $system_status_data );
$this->assertArrayHasKey( 'database', $system_status_data );
$this->assertArrayHasKey( 'active_plugins', $system_status_data );
$this->assertArrayHasKey( 'theme', $system_status_data );
$this->assertArrayHasKey( 'settings', $system_status_data );
$this->assertArrayHasKey( 'security', $system_status_data );
$this->assertArrayHasKey( 'pages', $system_status_data );
}
/**
@ -80,10 +110,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
public function test_get_system_status_info_environment() {
$this->skip_on_php_8_1();
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
$data = $response->get_data();
$environment = (array) $data['environment'];
$environment = (array) $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['environment'];
// Make sure all expected data is present.
$this->assertEquals( 32, count( $environment ) );
@ -103,10 +130,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
$this->skip_on_php_8_1();
global $wpdb;
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
$data = $response->get_data();
$database = (array) $data['database'];
$database = (array) $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['database'];
$this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] );
$this->assertEquals( $wpdb->prefix, $database['database_prefix'] );
@ -124,7 +148,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
public function test_get_system_status_info_active_plugins() {
$this->skip_on_php_8_1();
wp_set_current_user( $this->user );
wp_set_current_user( self::$administrator_user );
$actual_plugins = array( 'hello.php' );
update_option( 'active_plugins', $actual_plugins );
@ -145,13 +169,8 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
*/
public function test_get_system_status_info_theme() {
$this->skip_on_php_8_1();
wp_set_current_user( $this->user );
$active_theme = wp_get_theme();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
$data = $response->get_data();
$theme = (array) $data['theme'];
$theme = (array) $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['theme'];
$this->assertEquals( 13, count( $theme ) );
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
@ -166,17 +185,13 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
public function test_get_system_status_info_settings() {
$this->skip_on_php_8_1();
wp_set_current_user( $this->user );
$term_response = array();
$terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
foreach ( $terms as $term ) {
$term_response[ $term->slug ] = strtolower( $term->name );
}
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
$data = $response->get_data();
$settings = (array) $data['settings'];
$settings = (array) $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['settings'];
$this->assertEquals( 17, count( $settings ) );
$this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] );
@ -192,11 +207,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
public function test_get_system_status_info_security() {
$this->skip_on_php_8_1();
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
$data = $response->get_data();
$settings = (array) $data['security'];
$settings = (array) $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['security'];
$this->assertEquals( 2, count( $settings ) );
$this->assertEquals( 'https' === substr( wc_get_page_permalink( 'shop' ), 0, 5 ), $settings['secure_connection'] );
@ -211,10 +222,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
public function test_get_system_status_info_pages() {
$this->skip_on_php_8_1();
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
$data = $response->get_data();
$pages = $data['pages'];
$pages = $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['pages'];
$this->assertEquals( 5, count( $pages ) );
}
@ -244,7 +252,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
* @since 3.0.0
*/
public function test_get_system_tools() {
wp_set_current_user( $this->user );
wp_set_current_user( self::$administrator_user );
$tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools();
@ -290,7 +298,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
* @since 3.0.0
*/
public function test_get_system_status_tools_without_permission() {
wp_set_current_user( 0 );
wp_set_current_user( self::$no_user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) );
$this->assertEquals( 401, $response->get_status() );
}
@ -301,7 +309,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
* @since 3.0.0
*/
public function test_get_system_tool() {
wp_set_current_user( $this->user );
wp_set_current_user( self::$administrator_user );
$tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools();
@ -324,7 +332,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
* @since 3.0.0
*/
public function test_get_system_status_tool_without_permission() {
wp_set_current_user( 0 );
wp_set_current_user( self::$no_user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) );
$this->assertEquals( 401, $response->get_status() );
}
@ -335,7 +343,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
* @since 3.0.0
*/
public function test_execute_system_tool() {
wp_set_current_user( $this->user );
wp_set_current_user( self::$administrator_user );
$tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools();
@ -361,7 +369,7 @@ class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
* @since 3.0.0
*/
public function test_execute_system_status_tool_without_permission() {
wp_set_current_user( 0 );
wp_set_current_user( self::$no_user );
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) );
$this->assertEquals( 401, $response->get_status() );
}

View File

@ -19,9 +19,16 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
/**
* User variable.
*
* @var WP_User
* @var int
*/
protected static $user;
protected static int $administrator_user;
/**
* User variable.
*
* @var int
*/
protected static int $no_user = 0;
/**
* Setup once before running tests.
@ -29,7 +36,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @param object $factory Factory object.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$user = $factory->user->create(
self::$administrator_user = $factory->user->create(
array(
'role' => 'administrator',
)
@ -42,14 +49,30 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
public function setUp(): void {
parent::setUp();
wp_set_current_user( self::$user );
$this->endpoint = new WC_REST_System_Status_Controller();
// Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response.
$this->http_responder = array( $this, 'mock_http_responses' );
}
/**
* Fetches the System Status Report data and caches it.
* @param int $user The ID of a WordPress user to switch to before fetching the data.
* @return Array An array of the data returned by the System Status Report endpoint.
*/
private function fetch_or_get_system_status_data_for_user( int $user ) {
if ( $user < 0 ) {
return null;
}
static $system_status_data = array();
if ( ! isset( $system_status_data[ 'user' . $user ] ) ) {
wp_set_current_user( $user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
$data = $response->get_data();
$system_status_data[ 'user' . $user ] = $data;
}
return $system_status_data[ 'user' . $user ];
}
/**
* Test route registration.
*/
@ -66,7 +89,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_without_permission() {
wp_set_current_user( 0 );
wp_set_current_user( self::$no_user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$this->assertEquals( 401, $response->get_status() );
}
@ -78,16 +101,14 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_returns_root_properties() {
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$this->assertArrayHasKey( 'environment', $data );
$this->assertArrayHasKey( 'database', $data );
$this->assertArrayHasKey( 'active_plugins', $data );
$this->assertArrayHasKey( 'theme', $data );
$this->assertArrayHasKey( 'settings', $data );
$this->assertArrayHasKey( 'security', $data );
$this->assertArrayHasKey( 'pages', $data );
$system_status_data = $this->fetch_or_get_system_status_data_for_user( self::$administrator_user );
$this->assertArrayHasKey( 'environment', $system_status_data );
$this->assertArrayHasKey( 'database', $system_status_data );
$this->assertArrayHasKey( 'active_plugins', $system_status_data );
$this->assertArrayHasKey( 'theme', $system_status_data );
$this->assertArrayHasKey( 'settings', $system_status_data );
$this->assertArrayHasKey( 'security', $system_status_data );
$this->assertArrayHasKey( 'pages', $system_status_data );
}
/**
@ -96,9 +117,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_environment() {
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$environment = (array) $data['environment'];
$environment = (array) $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['environment'];
// Make sure all expected data is present.
$this->assertEquals( 32, count( $environment ) );
@ -123,6 +142,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
'version' => WC()->version,
),
);
wp_set_current_user( self::$administrator_user );
$request = new WP_REST_Request( 'GET', '/wc/v3/system_status' );
$request->set_query_params( array( '_fields' => 'environment.version' ) );
@ -140,9 +160,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
*/
public function test_get_system_status_info_database() {
global $wpdb;
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$database = (array) $data['database'];
$database = (array) $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['database'];
$this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] );
$this->assertEquals( $wpdb->prefix, $database['database_prefix'] );
@ -156,6 +174,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_active_plugins() {
wp_set_current_user( self::$administrator_user );
$actual_plugins = array( 'hello.php' );
update_option( 'active_plugins', $actual_plugins );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
@ -175,10 +194,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
*/
public function test_get_system_status_info_theme() {
$active_theme = wp_get_theme();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$theme = (array) $data['theme'];
$theme = (array) $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['theme'];
$this->assertEquals( 13, count( $theme ) );
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
@ -197,9 +213,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
$term_response[ $term->slug ] = strtolower( $term->name );
}
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$settings = (array) $data['settings'];
$settings = (array) $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['settings'];
$this->assertEquals( 17, count( $settings ) );
$this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] );
@ -213,9 +227,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_security() {
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$settings = (array) $data['security'];
$settings = (array) $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['security'];
$this->assertEquals( 2, count( $settings ) );
$this->assertEquals( 'https' === substr( wc_get_page_permalink( 'shop' ), 0, 5 ), $settings['secure_connection'] );
@ -228,9 +240,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_pages() {
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$pages = $data['pages'];
$pages = $this->fetch_or_get_system_status_data_for_user( self::$administrator_user )['pages'];
$this->assertEquals( 5, count( $pages ) );
}
@ -260,6 +270,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_tools() {
wp_set_current_user( self::$administrator_user );
$tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools();
@ -346,7 +357,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_tools_without_permission() {
wp_set_current_user( 0 );
wp_set_current_user( self::$no_user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' ) );
$this->assertEquals( 401, $response->get_status() );
}
@ -357,6 +368,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_tool() {
wp_set_current_user( self::$administrator_user );
$tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools();
$raw_tool = $raw_tools['recount_terms'];
@ -400,7 +412,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_tool_without_permission() {
wp_set_current_user( 0 );
wp_set_current_user( self::$no_user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' ) );
$this->assertEquals( 401, $response->get_status() );
}
@ -411,6 +423,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_execute_system_tool() {
wp_set_current_user( self::$administrator_user );
$tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools();
$raw_tool = $raw_tools['recount_terms'];
@ -461,7 +474,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_execute_system_status_tool_without_permission() {
wp_set_current_user( 0 );
wp_set_current_user( self::$no_user );
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/recount_terms' ) );
$this->assertEquals( 401, $response->get_status() );
}