Skip to content

Commit

Permalink
Add tests for fpm-server-requirement-status endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
techanvil committed Nov 18, 2024
1 parent f315307 commit 54f900c
Showing 1 changed file with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,135 @@ public function provider_wrong_settings_data() {
);
}

/**
* @dataProvider provider_fpm_server_requirement_status_data
*/
public function test_get_fpm_server_requirement_status( $data ) {
$endpoint_responses = $data['endpoint_responses'];
$expected_settings = $data['expected_settings'];

// Here we mock the `is_endpoint_healthy()` method of the controller. This is necessary because, although we could
// mock `file_get_contents()`, it's not possible to mock the `$http_response_header` variable used within the scope
// of the `is_endpoint_healthy()` method. The rest of the controller's behaviour remains unmocked.
$mock_controller = $this->getMockBuilder( REST_First_Party_Mode_Controller::class )
->setConstructorArgs( array( $this->settings ) )
->onlyMethods( array( 'is_endpoint_healthy' ) )
->getMock();

$expected_calls = array_map(
function ( $url ) use ( $endpoint_responses ) {
return array( $url => $endpoint_responses[ $url ] );
},
array_keys( $endpoint_responses ),
);

$call_count = 0;

$mock_controller->expects( $this->exactly( 2 ) )
->method( 'is_endpoint_healthy' )
->willReturnCallback(
function ( $url ) use ( &$call_count, $expected_calls ) {
// Verify the argument matches what we expect.
$expected_url = array_keys( $expected_calls[ $call_count ] )[0];
$this->assertEquals( $expected_url, $url, 'Call #' . ( $call_count + 1 ) . ' received unexpected URL' );

// Return the corresponding response.
$result = array_values( $expected_calls[ $call_count ] )[0];
$call_count++;
return $result;
}
);

remove_all_filters( 'googlesitekit_rest_routes' );
/**
* @var REST_First_Party_Mode_Controller $mock_controller
*/
$mock_controller->register();
$this->register_rest_routes();
// Set up the site and admin user to make a successful REST request.
$this->grant_manage_options_permission();

$this->settings->register();

$request = new WP_REST_Request( 'GET', '/' . REST_Routes::REST_ROOT . '/core/site/data/fpm-server-requirement-status' );
$response = rest_get_server()->dispatch( $request );

$this->assertEqualSetsWithIndex(
array(
'isEnabled' => null,
'isFPMHealthy' => $expected_settings['isFPMHealthy'],
'isScriptAccessEnabled' => $expected_settings['isScriptAccessEnabled'],
),
$response->get_data()
);
}

public function provider_fpm_server_requirement_status_data() {
return array(
'FPS service healthy, proxy script healthy' => array(
array(
'endpoint_responses' => array(
'https://g-1234.fps.goog/mpath/healthy' => true,
'http://example.org/wp-content/plugins/google-site-kit/fpm/measurement.php?healthCheck=1' => true,
),
'expected_settings' => array(
'isFPMHealthy' => true,
'isScriptAccessEnabled' => true,
),
),
),
'FPS service healthy, proxy script unhealthy' => array(
array(
'endpoint_responses' => array(
'https://g-1234.fps.goog/mpath/healthy' => true,
'http://example.org/wp-content/plugins/google-site-kit/fpm/measurement.php?healthCheck=1' => false,
),
'expected_settings' => array(
'isFPMHealthy' => true,
'isScriptAccessEnabled' => false,
),
),
),
'FPS service unhealthy, proxy script healthy' => array(
array(
'endpoint_responses' => array(
'https://g-1234.fps.goog/mpath/healthy' => false,
'http://example.org/wp-content/plugins/google-site-kit/fpm/measurement.php?healthCheck=1' => true,
),
'expected_settings' => array(
'isFPMHealthy' => false,
'isScriptAccessEnabled' => true,
),
),
),
'FPS service unhealthy, proxy script unhealthy' => array(
array(
'endpoint_responses' => array(
'https://g-1234.fps.goog/mpath/healthy' => false,
'http://example.org/wp-content/plugins/google-site-kit/fpm/measurement.php?healthCheck=1' => false,
),
'expected_settings' => array(
'isFPMHealthy' => false,
'isScriptAccessEnabled' => false,
),
),
),
);
}

public function test_get_fpm_server_requirement_status__requires_authenticated_admin() {
remove_all_filters( 'googlesitekit_rest_routes' );
$this->controller->register();
$this->register_rest_routes();

$request = new WP_REST_Request( 'GET', '/' . REST_Routes::REST_ROOT . '/core/site/data/fpm-server-requirement-status' );
$response = rest_get_server()->dispatch( $request );

// This request is made by a user who is not authenticated with dashboard
// view permissions and is therefore forbidden.
$this->assertEquals( 'rest_forbidden', $response->get_data()['code'] );
}

private function grant_manage_options_permission() {
// Setup SiteKit.
$this->fake_proxy_site_connection();
Expand Down

0 comments on commit 54f900c

Please sign in to comment.