@@ -28,38 +28,124 @@ public function test_od_register_endpoint_hooked(): void {
28
28
* @return array<string, mixed>
29
29
*/
30
30
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
+
31
43
return array (
32
- 'not_extended ' => array (
33
- 'set_up ' => function (): array {
44
+ 'not_extended ' => array (
45
+ 'set_up ' => function (): array {
34
46
return $ this ->get_valid_params ();
35
47
},
48
+ 'expect_stored ' => true ,
49
+ 'expected_status ' => 200 ,
36
50
),
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 ' );
39
64
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! ' );
46
99
}
47
100
);
48
101
49
102
$ params = $ this ->get_valid_params ();
50
103
$ params ['extra ' ] = 'foo ' ;
51
104
return $ params ;
52
105
},
106
+ 'expect_stored ' => false ,
107
+ 'expected_status ' => 400 ,
53
108
),
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 {
56
140
$ params = $ this ->get_valid_params ();
57
141
$ params ['cache_purge_post_id ' ] = self ::factory ()->post ->create ();
58
142
$ params ['url ' ] = get_permalink ( $ params ['cache_purge_post_id ' ] );
59
143
$ params ['slug ' ] = od_get_url_metrics_slug ( array ( 'p ' => $ params ['cache_purge_post_id ' ] ) );
60
144
$ params ['hmac ' ] = od_get_url_metrics_storage_hmac ( $ params ['slug ' ], $ params ['current_etag ' ], $ params ['url ' ], $ params ['cache_purge_post_id ' ] );
61
145
return $ params ;
62
146
},
147
+ 'expect_stored ' => true ,
148
+ 'expected_status ' => 200 ,
63
149
),
64
150
);
65
151
}
@@ -73,7 +159,25 @@ static function ( array $properties ): array {
73
159
* @covers ::od_handle_rest_request
74
160
* @covers ::od_trigger_page_cache_invalidation
75
161
*/
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
+
77
181
$ stored_context = null ;
78
182
add_action (
79
183
'od_url_metric_stored ' ,
@@ -96,38 +200,51 @@ function ( OD_URL_Metric_Store_Request_Context $context ) use ( &$stored_context
96
200
$ this ->assertCount ( 0 , get_posts ( array ( 'post_type ' => OD_URL_Metrics_Post_Type::SLUG ) ) );
97
201
$ request = $ this ->create_request ( $ valid_params );
98
202
$ 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 ' ) );
120
203
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 ' ) );
122
206
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
+ );
126
234
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
+ }
131
248
}
132
249
}
133
250
0 commit comments