Repository navigation
Expand file tree
/
Copy pathclass-wp-rest-icon-controller-test.php
More file actions
487 lines (400 loc) · 15.1 KB
/
Copy pathclass-wp-rest-icon-controller-test.php
File metadata and controls
487 lines (400 loc) · 15.1 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
* Unit tests covering WP_REST_Icons_Controller functionality.
*
* @package gutenberg
*/
class WP_Test_REST_Icons_Controller extends WP_Test_REST_TestCase {
protected static $admin_id;
protected static $editor_id;
protected static $contributor_id;
protected static $subscriber_id;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
self::$contributor_id = $factory->user->create( array( 'role' => 'contributor' ) );
self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) );
}
public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );
self::delete_user( self::$editor_id );
self::delete_user( self::$contributor_id );
self::delete_user( self::$subscriber_id );
}
public function set_up() {
parent::set_up();
$collections = array(
'test-public' => true,
'test-private' => false,
);
foreach ( $collections as $slug => $is_public ) {
wp_register_icon_collection(
$slug,
array(
'label' => $slug,
'public' => $is_public,
)
);
wp_register_icon(
$slug . '/visibility-icon',
array(
'label' => 'Visibility Icon',
'content' => '<svg viewBox="0 0 24 24"><path d="M0 0h24v24H0z" /></svg>',
)
);
}
}
public function tear_down() {
wp_unregister_icon_collection( 'test-public' );
wp_unregister_icon_collection( 'test-private' );
parent::tear_down();
}
/**
* Test that GET /wp/v2/icons returns a list of icons for users with edit_posts capability.
*/
public function test_get_items_returns_icons_list() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
$this->assertIsArray( $data );
$this->assertNotEmpty( $data, 'Icon registry should contain at least one icon' );
// Check structure of first icon
$icon = $data[0];
$this->assertArrayHasKey( 'name', $icon );
$this->assertArrayHasKey( 'content', $icon );
$this->assertIsString( $icon['name'] );
$this->assertIsString( $icon['content'] );
$this->assertStringStartsWith( '<svg ', $icon['content'], 'Icon content should be valid SVG markup' );
}
/**
* Test that GET /wp/v2/icons requires proper permissions.
*/
public function test_get_items_requires_edit_posts_capability() {
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
}
/**
* Test that administrators can access icons.
*/
public function test_get_items_admin_has_access() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
}
/**
* Test that contributors can access icons.
*/
public function test_get_items_contributor_has_access() {
wp_set_current_user( self::$contributor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
}
/**
* Test that GET /wp/v2/icons/core/arrow-left returns specific icon data.
*/
public function test_get_item_returns_specific_icon() {
wp_set_current_user( self::$editor_id );
/*
* Intentionally avoid mocks or class reflection to register fake
* icons. Yes, this blurs the line between unit and integration
* testing, but as of now WP_Icons_Registry is closed for registration
* and really MUST contain our core icons.
*/
$request = new WP_REST_Request( 'GET', '/wp/v2/icons/core/arrow-left' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
$this->assertArrayHasKey( 'name', $data );
$this->assertArrayHasKey( 'label', $data );
$this->assertArrayHasKey( 'content', $data );
$this->assertSame( 'core/arrow-left', $data['name'] );
$this->assertSame( 'Arrow Left', $data['label'] );
$this->assertNotEmpty( $data['content'] );
$this->assertStringStartsWith(
'<svg ',
$data['content'],
'Icon content should start with an SVG element'
);
$this->assertStringContainsString(
'xmlns="http://www.w3.org/2000/svg"',
$data['content'],
'Icon should include the xmlns attribute'
);
}
/**
* Test that GET /wp/v2/icons/core/invalid returns 404 for non-existent icons.
*/
public function test_get_item_returns_404_for_invalid_icon() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons/core/invalid-icon-name' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_icon_not_found', $response, 404 );
}
/**
* Test that GET /wp/v2/icons/?search=arrow returns filtered results.
*/
public function test_get_items_search_filters_results() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$request->set_param( 'search', 'arrow' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
$this->assertIsArray( $data );
// All returned icons should contain "arrow" in their name
foreach ( $data as $icon ) {
$this->assertStringContainsStringIgnoringCase( 'arrow', $icon['name'] );
}
// Assert that 'core/arrow-left' is specifically included in the results
$icon_names = array_column( $data, 'name' );
$this->assertContains( 'core/arrow-left', $icon_names, 'Search results should include core/arrow-left icon' );
}
/**
* Test that GET /wp/v2/icons/?search=%s searches icon labels too.
*/
public function test_get_items_search_includes_label() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
// The '@' character is only found in the *label* for core/at-symbol
$request->set_param( 'search', '@' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
$this->assertSame( array( 'core/at-symbol' ), array_column( $data, 'name' ) );
}
/**
* Registers an icon carrying keywords, so the keyword tests do not depend on
* the terms any bundled icon happens to ship with.
*
* @return string The registered icon name.
*/
private function register_keyword_icon() {
$icon_name = 'core/keyword-icon';
wp_register_icon(
$icon_name,
array(
'label' => 'Keyword Icon',
'content' => '<svg></svg>',
'keywords' => array( 'hamburger' ),
)
);
return $icon_name;
}
/**
* Test that GET /wp/v2/icons/?search=%s searches icon keywords too.
*/
public function test_get_items_search_includes_keywords() {
wp_set_current_user( self::$editor_id );
$icon_name = $this->register_keyword_icon();
try {
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
// 'hamburger' is in neither the name nor the label, only the keywords.
$request->set_param( 'search', 'hamburger' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
$this->assertSame( array( $icon_name ), array_column( $data, 'name' ) );
} finally {
wp_unregister_icon( $icon_name );
}
}
/**
* Test that the response exposes an icon's keywords, so that clients which
* filter icons locally can match against them.
*/
public function test_get_items_response_includes_keywords() {
wp_set_current_user( self::$editor_id );
$icon_name = $this->register_keyword_icon();
try {
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$request->set_param( 'search', $icon_name );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
$this->assertCount( 1, $data );
$this->assertArrayHasKey( 'keywords', $data[0] );
$this->assertContains( 'hamburger', $data[0]['keywords'] );
} finally {
wp_unregister_icon( $icon_name );
}
}
/**
* Test that icons registered without keywords still expose an empty array,
* so consumers do not have to handle a missing field.
*/
public function test_get_items_response_keywords_defaults_to_empty_array() {
wp_set_current_user( self::$editor_id );
wp_register_icon(
'core/no-keywords',
array(
'label' => 'No Keywords',
'content' => '<svg></svg>',
)
);
try {
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$request->set_param( 'search', 'core/no-keywords' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
$this->assertCount( 1, $data );
$this->assertSame( array(), $data[0]['keywords'] );
} finally {
wp_unregister_icon( 'core/no-keywords' );
}
}
/**
* Test that search is case-insensitive.
*/
public function test_get_items_search_case_insensitive() {
wp_set_current_user( self::$editor_id );
// Test with uppercase search term
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$request->set_param( 'search', 'ARROW' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
// All returned icons should contain "arrow" in their name (case insensitive)
foreach ( $data as $icon ) {
$this->assertStringContainsStringIgnoringCase( 'arrow', $icon['name'] );
}
}
/**
* Test that search with no matches returns empty array.
*/
public function test_get_items_search_no_matches() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$request->set_param( 'search', 'nonexistenticon12345' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
$this->assertIsArray( $data );
$this->assertEmpty( $data );
}
/**
* Test that _fields parameter filters response fields.
*/
public function test_get_items_fields_parameter() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$request->set_param( '_fields', 'name' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
// Each icon should only have the 'name' field
foreach ( $data as $icon ) {
$this->assertArrayHasKey( 'name', $icon );
$this->assertArrayNotHasKey( 'content', $icon );
}
}
/**
* Test permissions for getting a specific icon.
*/
public function test_get_item_requires_permissions() {
// Get a valid icon name first with proper permissions
wp_set_current_user( self::$editor_id );
$list_request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$list_response = rest_get_server()->dispatch( $list_request );
// Icons endpoint must be available
$this->assertSame( 200, $list_response->get_status(), 'Icons endpoint should be available and return 200' );
$all_icons = $list_response->get_data();
// Registry should contain at least our test icon
$this->assertIsArray( $all_icons, 'Icons endpoint should return an array' );
$this->assertNotEmpty( $all_icons, 'Icon registry should contain at least one icon' );
$this->assertArrayHasKey( 'name', $all_icons[0], 'Icons should have a name field' );
$test_icon_name = $all_icons[0]['name'];
// Now test with subscriber (no permissions)
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons/' . $test_icon_name );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
}
/**
* Test that unauthenticated users cannot access icons.
*/
public function test_get_items_requires_authentication() {
wp_set_current_user( 0 ); // No user
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
}
/**
* Test that unauthenticated users cannot access specific icons.
*/
public function test_get_item_requires_authentication() {
wp_set_current_user( 0 ); // No user
$request = new WP_REST_Request( 'GET', '/wp/v2/icons/core/some-icon' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
}
/**
* Test that icons in non-public collections are omitted from lists and search results.
*
* @dataProvider data_icon_visibility_searches
*
* @param string $search Icon search term.
*/
public function test_get_items_omits_non_public_collections( $search ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons' );
$request->set_param( 'search', $search );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$names = wp_list_pluck( $response->get_data(), 'name' );
$this->assertContains( 'test-public/visibility-icon', $names );
$this->assertNotContains( 'test-private/visibility-icon', $names );
}
/**
* Provides icon searches that match both public and non-public collections.
*
* @return array[]
*/
public function data_icon_visibility_searches() {
return array(
'all icons' => array( '' ),
'search icons' => array( 'visibility' ),
);
}
/**
* Test that an icon in a non-public collection is not readable by name.
*/
public function test_get_item_returns_404_for_non_public_collection() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/icons/test-private/visibility-icon' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_icon_not_found', $response, 404 );
}
/**
* Test that non-public collections cannot be requested by URL or query parameter.
*
* @dataProvider data_non_public_collection_requests
*
* @param string $route REST API route.
* @param array $query REST API query parameters.
*/
public function test_get_collection_icons_returns_404_for_non_public_collection( $route, $query ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', $route );
$request->set_query_params( $query );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_icon_collection_not_found', $response, 404 );
}
/**
* Provides requests scoped to a non-public collection.
*
* @return array[]
*/
public function data_non_public_collection_requests() {
return array(
'collection route' => array( '/wp/v2/icons/test-private', array() ),
'collection query' => array( '/wp/v2/icons', array( 'collection' => 'test-private' ) ),
);
}
}