forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-rest-nav-menus-controller-test.php
554 lines (494 loc) · 16.4 KB
/
class-rest-nav-menus-controller-test.php
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<?php
/**
* REST API: REST_Nav_Menus_Controller_Test class
*
* @package WordPress
* @subpackage REST_API
*/
/**
* Tests for REST API for Menus.
*
* @see WP_Test_REST_Controller_Testcase
*/
class REST_Nav_Menus_Controller_Test extends WP_Test_REST_Controller_Testcase {
/**
* @var int
*/
public $menu_id;
/**
* @var int
*/
protected static $admin_id;
/**
* @var int
*/
protected static $subscriber_id;
/**
*
*/
const TAXONOMY = 'nav_menu';
/**
* @var int
*/
protected static $per_page = 50;
/**
* Create fake data before our tests run.
*
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
self::$subscriber_id = $factory->user->create(
array(
'role' => 'subscriber',
)
);
}
/**
*
*/
public function setUp() {
parent::setUp();
// Unregister all nav menu locations.
foreach ( array_keys( get_registered_nav_menus() ) as $location ) {
unregister_nav_menu( $location );
}
$orig_args = array(
'name' => 'Original Name',
'description' => 'Original Description',
'slug' => 'original-slug',
'taxonomy' => 'nav_menu',
);
$this->menu_id = $this->factory->term->create( $orig_args );
register_meta(
'term',
'test_single_menu',
array(
'object_subtype' => self::TAXONOMY,
'show_in_rest' => true,
'single' => true,
'type' => 'string',
)
);
}
/**
* Register nav menu locations.
*
* @param array $locations Location slugs.
*/
public function register_nav_menu_locations( $locations ) {
foreach ( $locations as $location ) {
register_nav_menu( $location, ucfirst( $location ) );
}
}
/**
*
*/
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/__experimental/menus', $routes );
$this->assertArrayHasKey( '/__experimental/menus/(?P<id>[\d]+)', $routes );
}
/**
*
*/
public function test_context_param() {
// Collection.
$request = new WP_REST_Request( 'OPTIONS', '/__experimental/menus' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertEqualSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
// Single.
$tag1 = $this->factory->tag->create( array( 'name' => 'Season 5' ) );
$request = new WP_REST_Request( 'OPTIONS', '/__experimental/menus/' . $tag1 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertEqualSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}
/**
*
*/
public function test_registered_query_params() {
$request = new WP_REST_Request( 'OPTIONS', '/__experimental/menus' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$keys = array_keys( $data['endpoints'][0]['args'] );
sort( $keys );
$this->assertEquals(
array(
'context',
'exclude',
'hide_empty',
'include',
'offset',
'order',
'orderby',
'page',
'per_page',
'post',
'search',
'slug',
),
$keys
);
}
/**
*
*/
public function test_get_items() {
wp_set_current_user( self::$admin_id );
wp_update_nav_menu_object(
0,
array(
'description' => 'Test get',
'menu-name' => 'test Name get',
)
);
$request = new WP_REST_Request( 'GET', '/__experimental/menus' );
$request->set_param( 'per_page', self::$per_page );
$response = rest_get_server()->dispatch( $request );
$this->check_get_taxonomy_terms_response( $response );
}
/**
*
*/
public function test_get_item() {
wp_set_current_user( self::$admin_id );
$nav_menu_id = wp_update_nav_menu_object(
0,
array(
'description' => 'Test menu',
'menu-name' => 'test Name',
)
);
$request = new WP_REST_Request( 'GET', '/__experimental/menus/' . $nav_menu_id );
$response = rest_get_server()->dispatch( $request );
$this->check_get_taxonomy_term_response( $response, $nav_menu_id );
}
/**
*
*/
public function test_create_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/__experimental/menus' );
$request->set_param( 'name', 'My Awesome menus' );
$request->set_param( 'description', 'This menu is so awesome.' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 201, $response->get_status() );
$headers = $response->get_headers();
$data = $response->get_data();
$this->assertContains( '/__experimental/menus/' . $data['id'], $headers['Location'] );
$this->assertEquals( 'My Awesome menus', $data['name'] );
$this->assertEquals( 'This menu is so awesome.', $data['description'] );
$this->assertEquals( 'my-awesome-menus', $data['slug'] );
}
/**
*
*/
public function test_update_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/__experimental/menus/' . $this->menu_id );
$request->set_param( 'name', 'New Name' );
$request->set_param( 'description', 'New Description' );
$request->set_param(
'meta',
array(
'test_single_menu' => 'just meta',
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'New Name', $data['name'] );
$this->assertEquals( 'New Description', $data['description'] );
$this->assertEquals( 'new-name', $data['slug'] );
$this->assertEquals( 'just meta', $data['meta']['test_single_menu'] );
$this->assertFalse( isset( $data['meta']['test_cat_meta'] ) );
}
/**
*
*/
public function test_delete_item() {
wp_set_current_user( self::$admin_id );
$nav_menu_id = wp_update_nav_menu_object(
0,
array(
'description' => 'Deleted Menu',
'menu-name' => 'Deleted Menu',
)
);
$term = get_term_by( 'id', $nav_menu_id, self::TAXONOMY );
$request = new WP_REST_Request( 'DELETE', '/__experimental/menus/' . $term->term_id );
$request->set_param( 'force', true );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertTrue( $data['deleted'] );
$this->assertEquals( 'Deleted Menu', $data['previous']['name'] );
}
/**
*
*/
public function test_prepare_item() {
$nav_menu_id = wp_update_nav_menu_object(
0,
array(
'description' => 'Foo Menu',
'menu-name' => 'Foo Menu',
)
);
$term = get_term_by( 'id', $nav_menu_id, self::TAXONOMY );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/__experimental/menus/' . $term->term_id );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->check_taxonomy_term( $term, $data, $response->get_links() );
}
/**
*
*/
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/__experimental/menus' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 6, count( $properties ) );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'meta', $properties );
$this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'slug', $properties );
$this->assertArrayHasKey( 'locations', $properties );
}
/**
*
*/
public function test_create_item_with_location_permission_correct() {
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/__experimental/menus' );
$request->set_param( 'name', 'My Awesome Term' );
$request->set_param( 'slug', 'so-awesome' );
$request->set_param( 'locations', 'primary' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 201, $response->get_status() );
$data = $response->get_data();
$term_id = $data['id'];
$locations = get_nav_menu_locations();
$this->assertEquals( $locations['primary'], $term_id );
}
/**
*
*/
public function test_create_item_with_location_permission_incorrect() {
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'POST', '/__experimental/menus' );
$request->set_param( 'name', 'My Awesome Term' );
$request->set_param( 'slug', 'so-awesome' );
$request->set_param( 'locations', 'primary' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( rest_authorization_required_code(), $response->get_status() );
$this->assertErrorResponse( 'rest_cannot_assign_location', $response, rest_authorization_required_code() );
}
/**
*
*/
public function test_create_item_with_location_permission_no_location() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/__experimental/menus' );
$request->set_param( 'name', 'My Awesome Term' );
$request->set_param( 'slug', 'so-awesome' );
$request->set_param( 'locations', 'bar' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
$this->assertErrorResponse( 'rest_menu_location_invalid', $response, 400 );
}
/**
*
*/
public function test_update_item_with_no_location() {
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/__experimental/menus/' . $this->menu_id );
$request->set_param( 'name', 'New Name' );
$request->set_param( 'description', 'New Description' );
$request->set_param( 'slug', 'new-slug' );
$request->set_param( 'locations', 'bar' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
}
/**
*
*/
public function test_update_item_with_location_permission_correct() {
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/__experimental/menus/' . $this->menu_id );
$request->set_param( 'name', 'New Name' );
$request->set_param( 'description', 'New Description' );
$request->set_param( 'slug', 'new-slug' );
$request->set_param( 'locations', 'primary' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$locations = get_nav_menu_locations();
$this->assertEquals( $locations['primary'], $this->menu_id );
}
/**
*
*/
public function test_update_item_with_location_permission_incorrect() {
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'POST', '/__experimental/menus/' . $this->menu_id );
$request->set_param( 'name', 'New Name' );
$request->set_param( 'description', 'New Description' );
$request->set_param( 'slug', 'new-slug' );
$request->set_param( 'locations', 'primary' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( rest_authorization_required_code(), $response->get_status() );
}
/**
*
*/
public function test_get_item_links() {
wp_set_current_user( self::$admin_id );
$nav_menu_id = wp_update_nav_menu_object(
0,
array(
'description' => 'Foo Menu',
'menu-name' => 'Foo Menu',
)
);
register_nav_menu( 'foo', 'Bar' );
set_theme_mod( 'nav_menu_locations', array( 'foo' => $nav_menu_id ) );
$request = new WP_REST_Request( 'GET', sprintf( '/__experimental/menus/%d', $nav_menu_id ) );
$response = rest_get_server()->dispatch( $request );
$links = $response->get_links();
$this->assertArrayHasKey( 'https://api.w.org/menu-location', $links );
$location_url = rest_url( '/__experimental/menu-locations/foo' );
$this->assertEquals( $location_url, $links['https://api.w.org/menu-location'][0]['href'] );
}
/**
*
*/
public function test_change_menu_location() {
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
$secondary_id = self::factory()->term->create(
array(
'name' => 'Secondary Name',
'description' => 'Secondary Description',
'slug' => 'secondary-slug',
'taxonomy' => 'nav_menu',
)
);
$locations = get_nav_menu_locations();
$locations['primary'] = $this->menu_id;
$locations['secondary'] = $secondary_id;
set_theme_mod( 'nav_menu_locations', $locations );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/__experimental/menus/' . $this->menu_id );
$request->set_body_params(
array(
'locations' => array( 'secondary' ),
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$locations = get_nav_menu_locations();
$this->assertArrayNotHasKey( 'primary', $locations );
$this->assertArrayHasKey( 'secondary', $locations );
$this->assertEquals( $this->menu_id, $locations['secondary'] );
}
/**
*
*/
public function test_get_items_no_permission() {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', '/__experimental/menus' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
}
/**
*
*/
public function test_get_item_no_permission() {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', '/__experimental/menus/' . $this->menu_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
}
/**
*
*/
public function test_get_items_wrong_permission() {
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'GET', '/__experimental/menus' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
}
/**
*
*/
public function test_get_item_wrong_permission() {
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'GET', '/__experimental/menus/' . $this->menu_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
}
/**
* @param WP_REST_Response $response Response Class.
*/
protected function check_get_taxonomy_terms_response( $response ) {
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$args = array(
'hide_empty' => false,
);
$tags = get_terms( self::TAXONOMY, $args );
$this->assertEquals( count( $tags ), count( $data ) );
$this->assertEquals( $tags[0]->term_id, $data[0]['id'] );
$this->assertEquals( $tags[0]->name, $data[0]['name'] );
$this->assertEquals( $tags[0]->slug, $data[0]['slug'] );
$this->assertEquals( $tags[0]->description, $data[0]['description'] );
}
/**
* @param WP_REST_Response $response Response Class.
* @param int $id Term ID.
*/
protected function check_get_taxonomy_term_response( $response, $id ) {
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$menu = get_term( $id, self::TAXONOMY );
$this->check_taxonomy_term( $menu, $data, $response->get_links() );
}
/**
* @param WP_Term $term WP_Term object.
* @param array $data Data from REST API.
* @param array $links Array of links.
*/
protected function check_taxonomy_term( $term, $data, $links ) {
$this->assertEquals( $term->term_id, $data['id'] );
$this->assertEquals( $term->name, $data['name'] );
$this->assertEquals( $term->slug, $data['slug'] );
$this->assertEquals( $term->description, $data['description'] );
$this->assertFalse( isset( $data['parent'] ) );
$relations = array(
'self',
'collection',
'about',
'https://api.w.org/post_type',
);
if ( ! empty( $data['parent'] ) ) {
$relations[] = 'up';
}
$this->assertEqualSets( $relations, array_keys( $links ) );
$this->assertContains( 'wp/v2/taxonomies/' . $term->taxonomy, $links['about'][0]['href'] );
$this->assertEquals( add_query_arg( 'menus', $term->term_id, rest_url( 'wp/v2/menu-items' ) ), $links['https://api.w.org/post_type'][0]['href'] );
}
}