-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathclass-wp-rest-pattern-directory-controller-test.php
More file actions
321 lines (277 loc) · 8.34 KB
/
class-wp-rest-pattern-directory-controller-test.php
File metadata and controls
321 lines (277 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* Unit tests covering WP_REST_Pattern_Directory_Controller functionality.
*
* @package WordPress
* @subpackage REST API
*/
/**
* @group restapi
* @group pattern-directory
*/
class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_Testcase {
/**
* Contributor user id.
*
* @since 5.8.0
*
* @var int
*/
protected static $contributor_id;
/**
* An instance of WP_REST_Pattern_Directory_Controller class.
*
* @since 6.0.0
*
* @var WP_REST_Pattern_Directory_Controller
*/
private static $controller;
/**
* List of URLs captured.
*
* @since 6.2.0
*
* @var string[]
*/
protected static $http_request_urls;
/**
* Set up class test fixtures.
*
* @param WP_UnitTest_Factory $factory WordPress unit test factory.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$contributor_id = $factory->user->create(
array(
'role' => 'contributor',
)
);
self::$http_request_urls = array();
static::$controller = new Gutenberg_REST_Pattern_Directory_Controller_6_3();
}
public static function wpTearDownAfterClass() {
self::delete_user( self::$contributor_id );
}
/**
* Clear the captured request URLs after each test.
*/
public function tear_down() {
self::$http_request_urls = array();
parent::tear_down();
}
/**
* @covers WP_REST_Pattern_Directory_Controller::register_routes
*
* @since 5.8.0
* @since 6.2.0 Added pattern directory categories endpoint.
*/
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/wp/v2/pattern-directory/patterns', $routes );
$this->assertArrayHasKey( '/wp/v2/pattern-directory/categories', $routes );
}
/**
* @covers WP_REST_Pattern_Directory_Controller::prepare_pattern_category_for_response
*
* @since 6.2.0
*/
public function test_prepare_pattern_category_for_response() {
$raw_categories = array(
(object) array(
'id' => 3,
'name' => 'Columns',
'slug' => 'columns',
'description' => 'A description',
),
);
$prepared_category = static::$controller->prepare_response_for_collection(
static::$controller->prepare_pattern_category_for_response( $raw_categories[0], new WP_REST_Request() )
);
$this->assertSame(
array(
'id' => 3,
'name' => 'Columns',
'slug' => 'columns',
),
$prepared_category
);
}
/**
* Tests if the provided query args are passed through to the wp.org API.
*
* @dataProvider data_get_items_query_args
*
* @covers WP_REST_Pattern_Directory_Controller::get_items
*
* @since 6.2.0
*
* @param string $param Query parameter name (ex, page).
* @param mixed $value Query value to test.
* @param bool $is_error Whether this value should error or not.
* @param mixed $expected Expected value (or expected error code).
*/
public function test_get_items_query_args( $param, $value, $is_error, $expected ) {
wp_set_current_user( self::$contributor_id );
self::capture_http_urls();
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
if ( $value ) {
$request->set_query_params( array( $param => $value ) );
}
$response = rest_do_request( $request );
$data = $response->get_data();
if ( $is_error ) {
$this->assertSame( $expected, $data['code'] );
$this->assertStringContainsString( $param, $data['message'] );
} else {
$this->assertCount( 1, self::$http_request_urls );
$this->assertStringContainsString( $param . '=' . $expected, self::$http_request_urls[0] );
}
}
/**
* @since 6.2.0
*/
public function data_get_items_query_args() {
return array(
'per_page default' => array( 'per_page', false, false, 100 ),
'per_page custom-1' => array( 'per_page', 5, false, 5 ),
'per_page custom-2' => array( 'per_page', 50, false, 50 ),
'per_page invalid-1' => array( 'per_page', 200, true, 'rest_invalid_param' ),
'per_page invalid-2' => array( 'per_page', 'abc', true, 'rest_invalid_param' ),
'page default' => array( 'page', false, false, 1 ),
'page custom' => array( 'page', 5, false, 5 ),
'page invalid' => array( 'page', 'abc', true, 'rest_invalid_param' ),
'offset custom' => array( 'offset', 5, false, 5 ),
'offset invalid-1' => array( 'offset', 'abc', true, 'rest_invalid_param' ),
'order default' => array( 'order', false, false, 'desc' ),
'order custom' => array( 'order', 'asc', false, 'asc' ),
'order invalid-1' => array( 'order', 10, true, 'rest_invalid_param' ),
'order invalid-2' => array( 'order', 'fake', true, 'rest_invalid_param' ),
'orderby default' => array( 'orderby', false, false, 'date' ),
'orderby custom-1' => array( 'orderby', 'title', false, 'title' ),
'orderby custom-2' => array( 'orderby', 'date', false, 'date' ),
'orderby custom-3' => array( 'orderby', 'favorite_count', false, 'favorite_count' ),
'orderby invalid-1' => array( 'orderby', 10, true, 'rest_invalid_param' ),
'orderby invalid-2' => array( 'orderby', 'fake', true, 'rest_invalid_param' ),
);
}
/**
* Attach a filter to capture requested wp.org URL.
*
* @since 6.2.0
*/
private static function capture_http_urls() {
add_filter(
'pre_http_request',
function ( $preempt, $args, $url ) {
if ( 'api.wordpress.org' !== wp_parse_url( $url, PHP_URL_HOST ) ) {
return $preempt;
}
self::$http_request_urls[] = $url;
// Return a response to prevent external API request.
$response = array(
'headers' => array(),
'response' => array(
'code' => 200,
'message' => 'OK',
),
'body' => '[]',
'cookies' => array(),
'filename' => null,
);
return $response;
},
10,
3
);
}
/**
* @covers WP_REST_Pattern_Directory_Controller::prepare_item_for_response
*
* @since 5.8.0
* @since 6.2.0 Added `block_types` property.
*/
public function test_prepare_item() {
$raw_patterns = json_decode( self::get_raw_response( 'browse-all' ) );
$raw_patterns[0]->extra_field = 'this should be removed';
$prepared_pattern = static::$controller->prepare_response_for_collection(
static::$controller->prepare_item_for_response( $raw_patterns[0], new WP_REST_Request() )
);
$this->assertPatternMatchesSchema( $prepared_pattern );
$this->assertArrayNotHasKey( 'extra_field', $prepared_pattern );
}
/**
* Asserts that the pattern matches the expected response schema.
*
* @param WP_REST_Response[] $pattern An individual pattern from the REST API response.
*/
public function assertPatternMatchesSchema( $pattern ) {
$schema = static::$controller->get_item_schema();
$pattern_id = isset( $pattern->id ) ? $pattern->id : '{pattern ID is missing}';
$this->assertTrue(
rest_validate_value_from_schema( $pattern, $schema ),
"Pattern ID `$pattern_id` doesn't match the response schema."
);
$this->assertSame(
array_keys( $schema['properties'] ),
array_keys( $pattern ),
"Pattern ID `$pattern_id` doesn't contain all of the fields expected from the schema."
);
}
/**
* Get a mocked raw response from api.wordpress.org.
*
* @return string
*/
private static function get_raw_response( $action ) {
$fixtures_dir = __DIR__ . '/fixtures/pattern-directory';
switch ( $action ) {
default:
case 'browse-all':
// Response from https://api.wordpress.org/patterns/1.0/.
$response = file_get_contents( $fixtures_dir . '/browse-all.json' );
break;
}
return $response;
}
/**
* @doesNotPerformAssertions
*/
public function test_context_param() {
// Covered by the core test.
}
/**
* @doesNotPerformAssertions
*/
public function test_get_items() {
// Covered by the core test.
}
/**
* @doesNotPerformAssertions
*/
public function test_get_item() {
// Controller does not implement get_item().
}
/**
* @doesNotPerformAssertions
*/
public function test_create_item() {
// Controller does not implement create_item().
}
/**
* @doesNotPerformAssertions
*/
public function test_update_item() {
// Controller does not implement update_item().
}
/**
* @doesNotPerformAssertions
*/
public function test_delete_item() {
// Controller does not implement delete_item().
}
/**
* @doesNotPerformAssertions
*/
public function test_get_item_schema() {
// The controller's schema is hardcoded, so tests would not be meaningful.
}
}