forked from buddypress/BP-REST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-controller.php
More file actions
412 lines (322 loc) · 11.1 KB
/
Copy pathtest-controller.php
File metadata and controls
412 lines (322 loc) · 11.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
<?php
/**
* Components Endpoint Tests.
*
* @package BuddyPress
* @subpackage BP_REST
* @group components
*/
class BP_Test_REST_Components_Endpoint extends WP_Test_REST_Controller_Testcase {
public function set_up() {
parent::set_up();
$this->bp_factory = new BP_UnitTest_Factory();
$this->endpoint = new BP_REST_Components_Endpoint();
$this->bp = new BP_UnitTestCase();
$this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/components';
$this->user = $this->factory->user->create( array(
'role' => 'administrator',
'user_email' => 'admin@example.com',
) );
if ( ! $this->server ) {
$this->server = rest_get_server();
}
}
public function test_register_routes() {
$routes = $this->server->get_routes();
// Main.
$this->assertArrayHasKey( $this->endpoint_url, $routes );
$this->assertCount( 2, $routes[ $this->endpoint_url ] );
}
/**
* @group get_items
*/
public function test_get_items() {
$this->bp->set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
$request->set_param( 'context', 'view' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$all_data = $response->get_data();
$this->assertNotEmpty( $all_data );
$this->assertTrue( 10 === count( $all_data ) );
$component = $this->endpoint->get_component_info( $all_data[0]['name'] );
$this->check_component_data( $component, $all_data[0] );
}
/**
* @group get_items
*/
public function test_get_items_paginated() {
$this->bp->set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
$request->set_query_params(
array(
'per_page' => 5,
)
);
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$headers = $response->get_headers();
$this->assertEquals( 10, $headers['X-WP-Total'] );
$this->assertEquals( 2, $headers['X-WP-TotalPages'] );
$all_data = $response->get_data();
$this->assertNotEmpty( $all_data );
$this->assertTrue( 10 === count( $all_data ) );
}
/**
* @group get_items
*/
public function test_get_items_invalid_status() {
$this->bp->set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
$request->set_query_params(
array(
'status' => 'another',
)
);
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* @group get_items
*/
public function test_get_items_user_is_not_logged_in() {
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 401 );
}
/**
* @group get_items
*/
public function test_get_items_without_permission() {
$u = $this->factory->user->create();
$this->bp->set_current_user( $u );
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
$request->set_param( 'context', 'view' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 );
}
/**
* @group get_items
*/
public function test_get_items_active_permission() {
$u = $this->factory->user->create(
array(
'role' => 'author',
)
);
$this->bp->set_current_user( $u );
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
$request->set_param( 'status', 'active' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$all_data = $response->get_data();
$this->assertNotEmpty( $all_data );
$all_features = wp_list_pluck( $all_data, 'features' );
$this->assertNotEmpty( array_filter( $all_features ) );
}
/**
* @group get_items
*/
public function test_get_items_active_component_features() {
$this->bp->set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
$request->set_param( 'context', 'view' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$all_data = $response->get_data();
$this->assertNotEmpty( $all_data );
$members_info = wp_list_filter( $all_data, array( 'name' => 'members' ) );
$members_info = reset( $members_info );
$this->assertTrue( $members_info['features']['avatar'] );
}
/**
* @group get_items
*/
public function test_get_items_inactive_component_features() {
$this->bp->set_current_user( $this->user );
add_filter( 'bp_is_messages_star_active', '__return_false' );
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
$request->set_param( 'context', 'view' );
$response = $this->server->dispatch( $request );
remove_filter( 'bp_is_messages_star_active', '__return_false' );
$this->assertEquals( 200, $response->get_status() );
$all_data = $response->get_data();
$this->assertNotEmpty( $all_data );
$messages_info = wp_list_filter( $all_data, array( 'name' => 'messages' ) );
$messages_info = reset( $messages_info );
$this->assertFalse( $messages_info['features']['star'] );
}
public function deactivate_activity_component( $retval, $component ) {
if ( 'activity' === $component ) {
$retval = false;
}
return $retval;
}
/**
* @group get_items
*/
public function test_get_items_inactive_component() {
$this->bp->set_current_user( $this->user );
add_filter( 'bp_is_active', array( $this, 'deactivate_activity_component' ), 10, 2 );
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
$request->set_param( 'context', 'view' );
$response = $this->server->dispatch( $request );
remove_filter( 'bp_is_active', array( $this, 'deactivate_activity_component' ), 10, 2 );
$this->assertEquals( 200, $response->get_status() );
$all_data = $response->get_data();
$this->assertNotEmpty( $all_data );
$components_info = wp_list_filter( $all_data, array( 'name' => 'messages', 'title' => 'Activity Streams' ), 'or' );
$features = wp_list_pluck( $components_info, 'features', 'name' );
$this->assertTrue( $features['messages']['star'] );
$this->assertEmpty( $features['activity'] );
}
/**
* @group get_item
*/
public function test_get_item() {
$this->markTestSkipped();
}
/**
* @group create_item
*/
public function test_create_item() {
$this->markTestSkipped();
}
/**
* @group update_item
*/
public function test_update_item() {
$this->bp->set_current_user( $this->user );
$request = new WP_REST_Request( 'PUT', $this->endpoint_url );
$request->set_query_params( array(
'name' => 'blogs',
'action' => 'deactivate',
) );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$all_data = $response->get_data();
$this->assertNotEmpty( $all_data );
$this->assertTrue( 'inactive' === $all_data[0]['status'] );
}
/**
* @group update_item
*/
public function test_update_item_nonexistent_component() {
$this->bp->set_current_user( $this->user );
$request = new WP_REST_Request( 'PUT', $this->endpoint_url );
$request->set_query_params( array(
'name' => 'blogssss',
'action' => 'deactivate',
) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'bp_rest_component_nonexistent', $response, 404 );
}
/**
* @group update_item
*/
public function test_update_item_empty_action() {
$this->bp->set_current_user( $this->user );
$request = new WP_REST_Request( 'PUT', $this->endpoint_url );
$request->set_query_params( array(
'name' => 'blogs',
'action' => '',
) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* @group update_item
*/
public function test_update_item_invalid_action() {
$this->bp->set_current_user( $this->user );
$request = new WP_REST_Request( 'PUT', $this->endpoint_url );
$request->set_query_params( array(
'name' => 'blogs',
'action' => 'update',
) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* @group update_item
*/
public function test_update_item_user_is_not_logged_in() {
$request = new WP_REST_Request( 'PUT', $this->endpoint_url );
$request->set_query_params( array(
'name' => 'core',
'action' => 'activate',
) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 401 );
}
/**
* @group update_item
*/
public function test_update_item_without_permission() {
$u = $this->factory->user->create();
$this->bp->set_current_user( $u );
$request = new WP_REST_Request( 'PUT', $this->endpoint_url );
$request->set_query_params( array(
'name' => 'core',
'action' => 'activate',
) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 );
}
/**
* @group update_item
*/
public function test_update_item_site_admin_only() {
$u = static::factory()->user->create(
array(
'role' => 'author',
)
);
$this->bp::set_current_user( $u );
// Snapshot so we can prove no component was toggled.
$before = bp_get_option( 'bp-active-components' );
$request = new WP_REST_Request( 'PUT', $this->endpoint_url );
$request->set_query_params( array(
'name' => 'friends',
'action' => 'deactivate',
) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 );
// The component toggle must not have executed.
$this->assertSame( $before, bp_get_option( 'bp-active-components' ) );
}
/**
* @group delete_item
*/
public function test_delete_item() {
$this->markTestSkipped();
}
public function test_prepare_item() {
$this->markTestSkipped();
}
protected function check_component_data( $component, $data ) {
$this->assertEquals( $component['name'], $data['name'] );
$this->assertEquals( $component['status'], $data['status'] );
$this->assertEquals( $component['title'], $data['title'] );
$this->assertEquals( $component['description'], $data['description'] );
}
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint_url );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 6, count( $properties ) );
$this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'status', $properties );
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'description', $properties );
}
public function test_context_param() {
// Collection.
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint_url );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertEquals( array( 'view', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}
}