Skip to content

Commit 0d584b7

Browse files
committed
Add tests for od_url_metric_storage_validity filter
1 parent 42005e6 commit 0d584b7

File tree

2 files changed

+182
-52
lines changed

2 files changed

+182
-52
lines changed

plugins/optimization-detective/storage/rest-api.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,14 @@ function od_handle_rest_request( WP_REST_Request $request ) {
236236
*/
237237
$validity = apply_filters( 'od_url_metric_storage_validity', true, $url_metric, $url_metric->jsonSerialize() );
238238
} catch ( Exception $e ) {
239-
$error_data = null;
239+
$error_data = array(
240+
'status' => 500,
241+
);
240242
if ( WP_DEBUG ) {
241-
$error_data = array(
242-
'exception_class' => get_class( $e ),
243-
'exception_message' => $e->getMessage(),
244-
'exception_code' => $e->getCode(),
243+
$error_data['exception'] = array(
244+
'class' => get_class( $e ),
245+
'message' => $e->getMessage(),
246+
'code' => $e->getCode(),
245247
);
246248
}
247249
$validity = new WP_Error(
@@ -254,12 +256,13 @@ function od_handle_rest_request( WP_REST_Request $request ) {
254256
$error_data
255257
);
256258
}
257-
if ( false === $validity || ( $validity instanceof WP_Error && $validity->has_errors() ) ) {
259+
if ( false === (bool) $validity || ( $validity instanceof WP_Error && $validity->has_errors() ) ) {
258260
if ( false === $validity ) {
259261
$validity = new WP_Error( 'invalid_url_metric', __( 'Validity of URL Metric was rejected by filter.', 'optimization-detective' ) );
260262
}
261-
if ( ! isset( $validity->error_data['status'] ) ) {
262-
$validity->error_data['status'] = 400;
263+
$error_code = $validity->get_error_code();
264+
if ( ! isset( $validity->error_data[ $error_code ]['status'] ) ) {
265+
$validity->error_data[ $error_code ]['status'] = 400;
263266
}
264267
return $validity;
265268
}
@@ -269,9 +272,19 @@ function od_handle_rest_request( WP_REST_Request $request ) {
269272
$request->get_param( 'slug' ),
270273
$url_metric
271274
);
272-
273275
if ( $result instanceof WP_Error ) {
274-
return $result;
276+
$error_data = array(
277+
'status' => 500,
278+
);
279+
if ( WP_DEBUG ) {
280+
$error_data['code'] = $result->get_error_code();
281+
$error_data['message'] = $result->get_error_message();
282+
}
283+
return new WP_Error(
284+
'unable_to_store_url_metric',
285+
__( 'Unable to store URL Metric.', 'optimization-detective' ),
286+
$error_data
287+
);
275288
}
276289
$post_id = $result;
277290

plugins/optimization-detective/tests/storage/test-rest-api.php

Lines changed: 159 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,124 @@ public function test_od_register_endpoint_hooked(): void {
2828
* @return array<string, mixed>
2929
*/
3030
public function data_provider_to_test_rest_request_good_params(): array {
31+
$add_root_extra_property = static function ( string $property_name ): void {
32+
add_filter(
33+
'od_url_metric_schema_root_additional_properties',
34+
static function ( array $properties ) use ( $property_name ): array {
35+
$properties[ $property_name ] = array(
36+
'type' => 'string',
37+
);
38+
return $properties;
39+
}
40+
);
41+
};
42+
3143
return array(
32-
'not_extended' => array(
33-
'set_up' => function (): array {
44+
'not_extended' => array(
45+
'set_up' => function (): array {
3446
return $this->get_valid_params();
3547
},
48+
'expect_stored' => true,
49+
'expected_status' => 200,
3650
),
37-
'extended' => array(
38-
'set_up' => function (): array {
51+
'extended' => array(
52+
'set_up' => function () use ( $add_root_extra_property ): array {
53+
$add_root_extra_property( 'extra' );
54+
$params = $this->get_valid_params();
55+
$params['extra'] = 'foo';
56+
return $params;
57+
},
58+
'expect_stored' => true,
59+
'expected_status' => 200,
60+
),
61+
'extended_but_unset' => array(
62+
'set_up' => function () use ( $add_root_extra_property ): array {
63+
$add_root_extra_property( 'unset_prop' );
3964
add_filter(
40-
'od_url_metric_schema_root_additional_properties',
41-
static function ( array $properties ): array {
42-
$properties['extra'] = array(
43-
'type' => 'string',
44-
);
45-
return $properties;
65+
'od_url_metric_storage_validity',
66+
static function ( $validity, OD_URL_Metric $url_metric ) {
67+
$url_metric->unset( 'extra' );
68+
return $validity;
69+
},
70+
10,
71+
2
72+
);
73+
$params = $this->get_valid_params();
74+
$params['unset_prop'] = 'bar';
75+
return $params;
76+
},
77+
'expect_stored' => true,
78+
'expected_status' => 200,
79+
),
80+
'extended_and_rejected_by_returning_false' => array(
81+
'set_up' => function () use ( $add_root_extra_property ): array {
82+
$add_root_extra_property( 'extra' );
83+
add_filter( 'od_url_metric_storage_validity', '__return_false' );
84+
85+
$params = $this->get_valid_params();
86+
$params['extra'] = 'foo';
87+
return $params;
88+
},
89+
'expect_stored' => false,
90+
'expected_status' => 400,
91+
),
92+
'extended_and_rejected_by_returning_wp_error' => array(
93+
'set_up' => function () use ( $add_root_extra_property ): array {
94+
$add_root_extra_property( 'extra' );
95+
add_filter(
96+
'od_url_metric_storage_validity',
97+
static function () {
98+
return new WP_Error( 'rejected', 'Rejected!' );
4699
}
47100
);
48101

49102
$params = $this->get_valid_params();
50103
$params['extra'] = 'foo';
51104
return $params;
52105
},
106+
'expect_stored' => false,
107+
'expected_status' => 400,
53108
),
54-
'with_cache_purge_post_id' => array(
55-
'set_up' => function (): array {
109+
'rejected_by_returning_wp_error_with_forbidden_status' => array(
110+
'set_up' => function (): array {
111+
add_filter(
112+
'od_url_metric_storage_validity',
113+
static function () {
114+
return new WP_Error( 'forbidden', 'Forbidden!', array( 'status' => 403 ) );
115+
}
116+
);
117+
return $this->get_valid_params();
118+
},
119+
'expect_stored' => false,
120+
'expected_status' => 403,
121+
),
122+
'rejected_by_exception' => array(
123+
'set_up' => function (): array {
124+
add_filter(
125+
'od_url_metric_storage_validity',
126+
static function ( $validity, OD_URL_Metric $url_metric ) {
127+
$url_metric->unset( 'viewport' );
128+
return $validity;
129+
},
130+
10,
131+
2
132+
);
133+
return $this->get_valid_params();
134+
},
135+
'expect_stored' => false,
136+
'expected_status' => 500,
137+
),
138+
'with_cache_purge_post_id' => array(
139+
'set_up' => function (): array {
56140
$params = $this->get_valid_params();
57141
$params['cache_purge_post_id'] = self::factory()->post->create();
58142
$params['url'] = get_permalink( $params['cache_purge_post_id'] );
59143
$params['slug'] = od_get_url_metrics_slug( array( 'p' => $params['cache_purge_post_id'] ) );
60144
$params['hmac'] = od_get_url_metrics_storage_hmac( $params['slug'], $params['current_etag'], $params['url'], $params['cache_purge_post_id'] );
61145
return $params;
62146
},
147+
'expect_stored' => true,
148+
'expected_status' => 200,
63149
),
64150
);
65151
}
@@ -73,7 +159,25 @@ static function ( array $properties ): array {
73159
* @covers ::od_handle_rest_request
74160
* @covers ::od_trigger_page_cache_invalidation
75161
*/
76-
public function test_rest_request_good_params( Closure $set_up ): void {
162+
public function test_rest_request_good_params( Closure $set_up, bool $expect_stored, int $expected_status ): void {
163+
$filtered_url_metric_obj = null;
164+
$filtered_url_metric_data = null;
165+
$filter_called = 0;
166+
add_filter(
167+
'od_url_metric_storage_validity',
168+
function ( $validity, $url_metric, $url_metric_data ) use ( &$filter_called, &$filtered_url_metric_obj, &$filtered_url_metric_data ) {
169+
$this->assertTrue( $validity );
170+
$this->assertInstanceOf( OD_URL_Metric::class, $url_metric );
171+
$this->assertIsArray( $url_metric_data );
172+
$filtered_url_metric_obj = $url_metric;
173+
$filtered_url_metric_data = $url_metric_data;
174+
$filter_called++;
175+
return $validity;
176+
},
177+
0,
178+
3
179+
);
180+
77181
$stored_context = null;
78182
add_action(
79183
'od_url_metric_stored',
@@ -96,38 +200,51 @@ function ( OD_URL_Metric_Store_Request_Context $context ) use ( &$stored_context
96200
$this->assertCount( 0, get_posts( array( 'post_type' => OD_URL_Metrics_Post_Type::SLUG ) ) );
97201
$request = $this->create_request( $valid_params );
98202
$response = rest_get_server()->dispatch( $request );
99-
$this->assertSame( 200, $response->get_status(), 'Response: ' . wp_json_encode( $response ) );
100-
101-
$data = $response->get_data();
102-
$this->assertTrue( $data['success'] );
103-
104-
$this->assertCount( 1, get_posts( array( 'post_type' => OD_URL_Metrics_Post_Type::SLUG ) ) );
105-
$post = OD_URL_Metrics_Post_Type::get_post( $valid_params['slug'] );
106-
$this->assertInstanceOf( WP_Post::class, $post );
107-
108-
$url_metrics = OD_URL_Metrics_Post_Type::get_url_metrics_from_post( $post );
109-
$this->assertCount( 1, $url_metrics, 'Expected number of URL Metrics stored.' );
110-
$this->assertSame( $valid_params['elements'], $this->get_array_json_data( $url_metrics[0]->get( 'elements' ) ) );
111-
$this->assertSame( $valid_params['viewport']['width'], $url_metrics[0]->get_viewport_width() );
112-
113-
$expected_data = $valid_params;
114-
unset( $expected_data['hmac'], $expected_data['slug'], $expected_data['current_etag'], $expected_data['cache_purge_post_id'] );
115-
$this->assertSame(
116-
$expected_data,
117-
wp_array_slice_assoc( $url_metrics[0]->jsonSerialize(), array_keys( $expected_data ) )
118-
);
119-
$this->assertSame( 1, did_action( 'od_url_metric_stored' ) );
120203

121-
$this->assertInstanceOf( OD_URL_Metric_Store_Request_Context::class, $stored_context );
204+
$this->assertSame( 1, $filter_called );
205+
$this->assertSame( $expect_stored ? 1 : 0, did_action( 'od_url_metric_stored' ) );
122206

123-
// Now check that od_trigger_page_cache_invalidation() cleaned caches as expected.
124-
$this->assertSame( $url_metrics[0]->jsonSerialize(), $stored_context->url_metric->jsonSerialize() );
125-
$cache_purge_post_id = $stored_context->request->get_param( 'cache_purge_post_id' );
207+
$this->assertSame( $expected_status, $response->get_status(), 'Response: ' . wp_json_encode( $response ) );
208+
$data = $response->get_data();
209+
$this->assertCount( $expect_stored ? 1 : 0, get_posts( array( 'post_type' => OD_URL_Metrics_Post_Type::SLUG ) ) );
210+
211+
if ( ! $expect_stored ) {
212+
$this->assertArrayHasKey( 'code', $data );
213+
$this->assertArrayHasKey( 'message', $data );
214+
$this->assertNull( $stored_context );
215+
$this->assertNull( OD_URL_Metrics_Post_Type::get_post( $valid_params['slug'] ) );
216+
} else {
217+
$this->assertTrue( $data['success'] );
218+
219+
$post = OD_URL_Metrics_Post_Type::get_post( $valid_params['slug'] );
220+
$this->assertInstanceOf( WP_Post::class, $post );
221+
222+
$url_metrics = OD_URL_Metrics_Post_Type::get_url_metrics_from_post( $post );
223+
$this->assertCount( 1, $url_metrics, 'Expected number of URL Metrics stored.' );
224+
$this->assertSame( $valid_params['elements'], $this->get_array_json_data( $url_metrics[0]->get( 'elements' ) ) );
225+
$this->assertSame( $valid_params['viewport']['width'], $url_metrics[0]->get_viewport_width() );
226+
227+
$expected_data = $valid_params;
228+
unset( $expected_data['hmac'], $expected_data['slug'], $expected_data['current_etag'], $expected_data['cache_purge_post_id'] );
229+
unset( $expected_data['unset_prop'] );
230+
$this->assertSame(
231+
$expected_data,
232+
wp_array_slice_assoc( $url_metrics[0]->jsonSerialize(), array_keys( $expected_data ) )
233+
);
126234

127-
if ( isset( $valid_params['cache_purge_post_id'] ) ) {
128-
$scheduled = wp_next_scheduled( 'od_trigger_page_cache_invalidation', array( $valid_params['cache_purge_post_id'] ) );
129-
$this->assertIsInt( $scheduled );
130-
$this->assertGreaterThan( time(), $scheduled );
235+
$this->assertInstanceOf( OD_URL_Metric_Store_Request_Context::class, $stored_context );
236+
$this->assertSame( $stored_context->url_metric, $filtered_url_metric_obj );
237+
$this->assertSame( $stored_context->url_metric->jsonSerialize(), $filtered_url_metric_data );
238+
239+
// Now check that od_trigger_page_cache_invalidation() cleaned caches as expected.
240+
$this->assertSame( $url_metrics[0]->jsonSerialize(), $stored_context->url_metric->jsonSerialize() );
241+
if ( isset( $valid_params['cache_purge_post_id'] ) ) {
242+
$cache_purge_post_id = $stored_context->request->get_param( 'cache_purge_post_id' );
243+
$this->assertSame( $valid_params['cache_purge_post_id'], $cache_purge_post_id );
244+
$scheduled = wp_next_scheduled( 'od_trigger_page_cache_invalidation', array( $cache_purge_post_id ) );
245+
$this->assertIsInt( $scheduled );
246+
$this->assertGreaterThan( time(), $scheduled );
247+
}
131248
}
132249
}
133250

0 commit comments

Comments
 (0)