-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
class-rest-search-controller-test.php
521 lines (461 loc) · 12.9 KB
/
class-rest-search-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
<?php
/**
* WP_REST_Search_Controller tests
*
* @package gutenberg
*/
/**
* Tests for WP_REST_Search_Controller.
*/
class REST_Search_Controller_Test extends WP_Test_REST_Controller_Testcase {
/**
* Posts with title 'my-footitle'.
*
* @var array
*/
private static $my_title_post_ids = array();
/**
* Pages with title 'my-footitle'.
*
* @var array
*/
private static $my_title_page_ids = array();
/**
* Posts with content 'my-foocontent'.
*
* @var array
*/
private static $my_content_post_ids = array();
/**
* 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::$my_title_post_ids = $factory->post->create_many(
4,
array(
'post_title' => 'my-footitle',
'post_type' => 'post',
)
);
self::$my_title_page_ids = $factory->post->create_many(
4,
array(
'post_title' => 'my-footitle',
'post_type' => 'page',
)
);
self::$my_content_post_ids = $factory->post->create_many(
6,
array(
'post_content' => 'my-foocontent',
)
);
}
/**
* Delete our fake data after our tests run.
*/
public static function wpTearDownAfterClass() {
$post_ids = array_merge(
self::$my_title_post_ids,
self::$my_title_page_ids,
self::$my_content_post_ids
);
foreach ( $post_ids as $post_id ) {
wp_delete_post( $post_id, true );
}
}
/**
* Check that our routes get set up properly.
*/
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/gutenberg/v1/search', $routes );
$this->assertCount( 1, $routes['/gutenberg/v1/search'] );
}
/**
* Check the context parameter.
*/
public function test_context_param() {
$response = $this->do_request_with_params( array(), 'OPTIONS' );
$data = $response->get_data();
$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertEquals( array( 'view', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
}
/**
* Search through all content.
*/
public function test_get_items() {
$response = $this->do_request_with_params(
array(
'per_page' => 100,
)
);
$this->assertEquals( 200, $response->get_status() );
$this->assertEqualSets(
array_merge(
self::$my_title_post_ids,
self::$my_title_page_ids,
self::$my_content_post_ids
),
wp_list_pluck( $response->get_data(), 'id' )
);
}
/**
* Search through all content with a low limit.
*/
public function test_get_items_with_limit() {
$response = $this->do_request_with_params(
array(
'per_page' => 3,
)
);
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 3, count( $response->get_data() ) );
}
/**
* Search through posts of any post type.
*/
public function test_get_items_search_type_post() {
$response = $this->do_request_with_params(
array(
'per_page' => 100,
'type' => 'post',
)
);
$this->assertEquals( 200, $response->get_status() );
$this->assertEqualSets(
array_merge(
self::$my_title_post_ids,
self::$my_title_page_ids,
self::$my_content_post_ids
),
wp_list_pluck( $response->get_data(), 'id' )
);
}
/**
* Search through posts of post type 'post'.
*/
public function test_get_items_search_type_post_subtype_post() {
$response = $this->do_request_with_params(
array(
'per_page' => 100,
'type' => 'post',
'subtype' => 'post',
)
);
$this->assertEquals( 200, $response->get_status() );
$this->assertEqualSets(
array_merge(
self::$my_title_post_ids,
self::$my_content_post_ids
),
wp_list_pluck( $response->get_data(), 'id' )
);
}
/**
* Search through posts of post type 'page'.
*/
public function test_get_items_search_type_post_subtype_page() {
$response = $this->do_request_with_params(
array(
'per_page' => 100,
'type' => 'post',
'subtype' => 'page',
)
);
$this->assertEquals( 200, $response->get_status() );
$this->assertEqualSets(
self::$my_title_page_ids,
wp_list_pluck( $response->get_data(), 'id' )
);
}
/**
* Search through an invalid type
*/
public function test_get_items_search_type_invalid() {
$response = $this->do_request_with_params(
array(
'per_page' => 100,
'type' => 'invalid',
)
);
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* Search through posts of an invalid post type.
*/
public function test_get_items_search_type_post_subtype_invalid() {
$response = $this->do_request_with_params(
array(
'per_page' => 100,
'type' => 'post',
'subtype' => 'invalid',
)
);
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* Search through posts and pages.
*/
public function test_get_items_search_posts_and_pages() {
$response = $this->do_request_with_params(
array(
'per_page' => 100,
'type' => 'post',
'subtype' => 'post,page',
)
);
$this->assertEquals( 200, $response->get_status() );
$this->assertEqualSets(
array_merge(
self::$my_title_post_ids,
self::$my_title_page_ids,
self::$my_content_post_ids
),
wp_list_pluck( $response->get_data(), 'id' )
);
}
/**
* Search through all that matches a 'footitle' search.
*/
public function test_get_items_search_for_footitle() {
$response = $this->do_request_with_params(
array(
'per_page' => 100,
'search' => 'footitle',
)
);
$this->assertEquals( 200, $response->get_status() );
$this->assertEqualSets(
array_merge(
self::$my_title_post_ids,
self::$my_title_page_ids
),
wp_list_pluck( $response->get_data(), 'id' )
);
}
/**
* Search through all that matches a 'foocontent' search.
*/
public function test_get_items_search_for_foocontent() {
$response = $this->do_request_with_params(
array(
'per_page' => 100,
'search' => 'foocontent',
)
);
$this->assertEquals( 200, $response->get_status() );
$this->assertEqualSets(
self::$my_content_post_ids,
wp_list_pluck( $response->get_data(), 'id' )
);
}
/**
* Test retrieving a single item isn't possible.
*/
public function test_get_item() {
/** The search controller does not allow getting individual item content */
$request = new WP_REST_Request( 'GET', '/gutenberg/v1/search' . self::$my_title_post_ids[0] );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test creating an item isn't possible.
*/
public function test_create_item() {
/** The search controller does not allow creating content */
$request = new WP_REST_Request( 'POST', '/gutenberg/v1/search' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test updating an item isn't possible.
*/
public function test_update_item() {
/** The search controller does not allow upading content */
$request = new WP_REST_Request( 'POST', '/gutenberg/v1/search' . self::$my_title_post_ids[0] );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test deleting an item isn't possible.
*/
public function test_delete_item() {
/** The search controller does not allow deleting content */
$request = new WP_REST_Request( 'DELETE', '/gutenberg/v1/search' . self::$my_title_post_ids[0] );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test preparing the data contains the correct fields.
*/
public function test_prepare_item() {
$response = $this->do_request_with_params();
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals(
array(
'id',
'title',
'url',
'type',
'subtype',
'_links',
),
array_keys( $data[0] )
);
}
/**
* Test preparing the data with limited fields contains the correct fields.
*/
public function test_prepare_item_limit_fields() {
if ( ! method_exists( 'WP_REST_Controller', 'get_fields_for_response' ) ) {
$this->markTestSkipped( 'Limiting fields requires the WP_REST_Controller::get_fields_for_response() method.' );
}
$response = $this->do_request_with_params(
array(
'_fields' => 'id,title',
)
);
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals(
array(
'id',
'title',
'_links',
),
array_keys( $data[0] )
);
}
/**
* Tests the item schema is correct.
*/
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/gutenberg/v1/search' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'url', $properties );
$this->assertArrayHasKey( 'type', $properties );
$this->assertArrayHasKey( 'subtype', $properties );
}
/**
* Tests that non-public post types are not allowed.
*/
public function test_non_public_post_type() {
$response = $this->do_request_with_params(
array(
'type' => 'post',
'subtype' => 'post,nav_menu_item',
)
);
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* Test getting items directly with a custom search handler.
*/
public function test_custom_search_handler_get_items() {
$controller = new WP_REST_Search_Controller( array( new WP_REST_Dummy_Search_Handler( 10 ) ) );
$request = $this->get_request(
array(
'page' => 1,
'per_page' => 10,
'type' => 'dummy',
'subtype' => array( WP_REST_Search_Controller::TYPE_ANY ),
)
);
$response = $controller->get_items( $request );
$this->assertEqualSets( range( 1, 10 ), wp_list_pluck( $response->get_data(), 'id' ) );
$request = $this->get_request(
array(
'page' => 1,
'per_page' => 10,
'type' => 'dummy',
'subtype' => array( 'dummy_first_type' ),
)
);
$response = $controller->get_items( $request );
$this->assertEqualSets( range( 1, 5 ), wp_list_pluck( $response->get_data(), 'id' ) );
}
/**
* Test preparing an item directly with a custom search handler.
*/
public function test_custom_search_handler_prepare_item() {
$controller = new WP_REST_Search_Controller( array( new WP_REST_Dummy_Search_Handler( 10 ) ) );
$request = $this->get_request(
array(
'type' => 'dummy',
'subtype' => array( WP_REST_Search_Controller::TYPE_ANY ),
)
);
$response = $controller->prepare_item_for_response( 1, $request );
$data = $response->get_data();
$this->assertEquals(
array(
'id',
'title',
'url',
'type',
'subtype',
),
array_keys( $data )
);
}
/**
* Test preparing an item directly with a custom search handler with limited fields.
*/
public function test_custom_search_handler_prepare_item_limit_fields() {
if ( ! method_exists( 'WP_REST_Controller', 'get_fields_for_response' ) ) {
$this->markTestSkipped( 'Limiting fields requires the WP_REST_Controller::get_fields_for_response() method.' );
}
$controller = new WP_REST_Search_Controller( array( new WP_REST_Dummy_Search_Handler( 10 ) ) );
$request = $this->get_request(
array(
'type' => 'dummy',
'subtype' => array( WP_REST_Search_Controller::TYPE_ANY ),
'_fields' => 'id,title',
)
);
$response = $controller->prepare_item_for_response( 1, $request );
$data = $response->get_data();
$this->assertEquals(
array(
'id',
'title',
),
array_keys( $data )
);
}
/**
* Test getting the collection params directly with a custom search handler.
*/
public function test_custom_search_handler_get_collection_params() {
$controller = new WP_REST_Search_Controller( array( new WP_REST_Dummy_Search_Handler( 10 ) ) );
$params = $controller->get_collection_params();
$this->assertEquals( 'dummy', $params[ WP_REST_Search_Controller::PROP_TYPE ]['default'] );
$this->assertEqualSets( array( 'dummy' ), $params[ WP_REST_Search_Controller::PROP_TYPE ]['enum'] );
$this->assertEqualSets( array( 'dummy_first_type', 'dummy_second_type', WP_REST_Search_Controller::TYPE_ANY ), $params[ WP_REST_Search_Controller::PROP_SUBTYPE ]['items']['enum'] );
}
/**
* Perform a REST request to our search endpoint with given parameters.
*/
private function do_request_with_params( $params = array(), $method = 'GET' ) {
$request = $this->get_request( $params, $method );
return rest_get_server()->dispatch( $request );
}
/**
* Get a REST request object for given parameters.
*/
private function get_request( $params = array(), $method = 'GET' ) {
$request = new WP_REST_Request( $method, '/gutenberg/v1/search' );
foreach ( $params as $param => $value ) {
$request->set_param( $param, $value );
}
return $request;
}
}