wpTearDownAfterClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Unit tests covering WP_REST_Users_Controller functionality.
4
 *
5
 * @package    WordPress
6
 * @subpackage REST API
7
 */
8
9
/**
10
 * @group restapi
11
 */
12
class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase
13
{
14
    protected static $superadmin;
15
    protected static $user;
16
    protected static $editor;
17
    protected static $site;
18
19
    public static function wpSetUpBeforeClass( $factory ) 
0 ignored issues
show
Coding Style introduced by
The function name wpSetUpBeforeClass is in camel caps, but expected wp_set_up_before_class instead as per the coding standard.
Loading history...
20
    {
21
        self::$superadmin = $factory->user->create(
22
            array(
23
            'role'       => 'administrator',
24
            'user_login' => 'superadmin',
25
            ) 
26
        );
27
        self::$user = $factory->user->create(
28
            array(
29
            'role' => 'administrator',
30
            ) 
31
        );
32
        self::$editor = $factory->user->create(
33
            array(
34
            'role'       => 'editor',
35
            'user_email' => '[email protected]',
36
            ) 
37
        );
38
39
        if (is_multisite() ) {
40
            self::$site = $factory->blog->create(array( 'domain' => 'rest.wordpress.org', 'path' => '/' ));
41
            update_site_option('site_admins', array( 'superadmin' ));
42
        }
43
    }
44
45
    public static function wpTearDownAfterClass() 
0 ignored issues
show
Coding Style introduced by
The function name wpTearDownAfterClass is in camel caps, but expected wp_tear_down_after_class instead as per the coding standard.
Loading history...
46
    {
47
        self::delete_user(self::$user);
48
        self::delete_user(self::$editor);
49
50
        if (is_multisite() ) {
51
            wpmu_delete_blog(self::$site, true);
52
        }
53
    }
54
55
    /**
56
     * This function is run before each method
57
     */
58
    public function setUp() 
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
59
    {
60
        parent::setUp();
61
        $this->endpoint = new WP_REST_Users_Controller();
62
    }
63
64 View Code Duplication
    public function test_register_routes() 
65
    {
66
        $routes = $this->server->get_routes();
67
68
        $this->assertArrayHasKey('/wp/v2/users', $routes);
69
        $this->assertCount(2, $routes['/wp/v2/users']);
70
        $this->assertArrayHasKey('/wp/v2/users/(?P<id>[\d]+)', $routes);
71
        $this->assertCount(3, $routes['/wp/v2/users/(?P<id>[\d]+)']);
72
        $this->assertArrayHasKey('/wp/v2/users/me', $routes);
73
    }
74
75 View Code Duplication
    public function test_context_param() 
76
    {
77
        // Collection
78
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/users');
79
        $response = $this->server->dispatch($request);
80
        $data = $response->get_data();
81
        $this->assertEquals('view', $data['endpoints'][0]['args']['context']['default']);
82
        $this->assertEquals(array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum']);
83
        // Single
84
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/users/' . self::$user);
85
        $response = $this->server->dispatch($request);
86
        $data = $response->get_data();
87
        $this->assertEquals('view', $data['endpoints'][0]['args']['context']['default']);
88
        $this->assertEquals(array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum']);
89
    }
90
91 View Code Duplication
    public function test_registered_query_params() 
92
    {
93
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/users');
94
        $response = $this->server->dispatch($request);
95
        $data = $response->get_data();
96
        $keys = array_keys($data['endpoints'][0]['args']);
97
        sort($keys);
98
        $this->assertEquals(
99
            array(
100
            'context',
101
            'exclude',
102
            'include',
103
            'offset',
104
            'order',
105
            'orderby',
106
            'page',
107
            'per_page',
108
            'roles',
109
            'search',
110
            'slug',
111
            ), $keys 
112
        );
113
    }
114
115 View Code Duplication
    public function test_get_items() 
116
    {
117
        wp_set_current_user(self::$user);
118
119
        $request = new WP_REST_Request('GET', '/wp/v2/users');
120
        $request->set_param('context', 'view');
121
        $response = $this->server->dispatch($request);
122
123
        $this->assertEquals(200, $response->get_status());
124
125
        $all_data = $response->get_data();
126
        $data = $all_data[0];
127
        $userdata = get_userdata($data['id']);
128
        $this->check_user_data($userdata, $data, 'view', $data['_links']);
129
    }
130
131 View Code Duplication
    public function test_get_items_with_edit_context() 
132
    {
133
        wp_set_current_user(self::$user);
134
135
        $request = new WP_REST_Request('GET', '/wp/v2/users');
136
        $request->set_param('context', 'edit');
137
        $response = $this->server->dispatch($request);
138
139
        $this->assertEquals(200, $response->get_status());
140
141
        $all_data = $response->get_data();
142
        $data = $all_data[0];
143
        $userdata = get_userdata($data['id']);
144
        $this->check_user_data($userdata, $data, 'edit', $data['_links']);
145
    }
146
147 View Code Duplication
    public function test_get_items_with_edit_context_without_permission() 
148
    {
149
        //test with a user not logged in
150
        $request = new WP_REST_Request('GET', '/wp/v2/users');
151
        $request->set_param('context', 'edit');
152
        $response = $this->server->dispatch($request);
153
154
        $this->assertEquals(401, $response->get_status());
155
156
        //test with a user logged in but without sufficient capabilities; capability in question: 'list_users'
157
        wp_set_current_user(self::$editor);
158
        $request = new WP_REST_Request('GET', '/wp/v2/users');
159
        $request->set_param('context', 'edit');
160
        $response = $this->server->dispatch($request);
161
162
        $this->assertEquals(403, $response->get_status());
163
    }
164
165
    public function test_get_items_unauthenticated_only_shows_public_users() 
166
    {
167
        $request = new WP_REST_Request('GET', '/wp/v2/users');
168
        $response = $this->server->dispatch($request);
169
170
        $this->assertEquals(array(), $response->get_data());
171
172
        $this->factory->post->create(array( 'post_author' => self::$editor ));
173
        $this->factory->post->create(array( 'post_author' => self::$user, 'post_status' => 'draft' ));
174
175
        $request = new WP_REST_Request('GET', '/wp/v2/users');
176
        $response = $this->server->dispatch($request);
177
        $users = $response->get_data();
178
179
        foreach ( $users as $user ) {
180
            $this->assertTrue(count_user_posts($user['id']) > 0);
181
182
            // Ensure we don't expose non-public data
183
            $this->assertArrayNotHasKey('capabilities', $user);
184
            $this->assertArrayNotHasKey('email', $user);
185
            $this->assertArrayNotHasKey('roles', $user);
186
        }
187
    }
188
189
    /**
190
     * @group test
191
     */
192 View Code Duplication
    public function test_get_items_pagination_headers() 
193
    {
194
        wp_set_current_user(self::$user);
195
        // Start of the index, including the three existing users
196
        for ( $i = 0; $i < 47; $i++ ) {
197
            $this->factory->user->create(
198
                array(
199
                'name'   => "User {$i}",
200
                ) 
201
            );
202
        }
203
        $request = new WP_REST_Request('GET', '/wp/v2/users');
204
        $response = $this->server->dispatch($request);
205
        $headers = $response->get_headers();
206
        $this->assertEquals(51, $headers['X-WP-Total']);
207
        $this->assertEquals(6, $headers['X-WP-TotalPages']);
208
        $next_link = add_query_arg(
209
            array(
210
            'page'    => 2,
211
            ), rest_url('wp/v2/users') 
212
        );
213
        $this->assertFalse(stripos($headers['Link'], 'rel="prev"'));
214
        $this->assertContains('<' . $next_link . '>; rel="next"', $headers['Link']);
215
        // 3rd page
216
        $this->factory->user->create(
217
            array(
218
            'name'   => 'User 51',
219
            ) 
220
        );
221
        $request = new WP_REST_Request('GET', '/wp/v2/users');
222
        $request->set_param('page', 3);
223
        $response = $this->server->dispatch($request);
224
        $headers = $response->get_headers();
225
        $this->assertEquals(52, $headers['X-WP-Total']);
226
        $this->assertEquals(6, $headers['X-WP-TotalPages']);
227
        $prev_link = add_query_arg(
228
            array(
229
            'page'    => 2,
230
            ), rest_url('wp/v2/users') 
231
        );
232
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
233
        $next_link = add_query_arg(
234
            array(
235
            'page'    => 4,
236
            ), rest_url('wp/v2/users') 
237
        );
238
        $this->assertContains('<' . $next_link . '>; rel="next"', $headers['Link']);
239
        // Last page
240
        $request = new WP_REST_Request('GET', '/wp/v2/users');
241
        $request->set_param('page', 6);
242
        $response = $this->server->dispatch($request);
243
        $headers = $response->get_headers();
244
        $this->assertEquals(52, $headers['X-WP-Total']);
245
        $this->assertEquals(6, $headers['X-WP-TotalPages']);
246
        $prev_link = add_query_arg(
247
            array(
248
            'page'    => 5,
249
            ), rest_url('wp/v2/users') 
250
        );
251
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
252
        $this->assertFalse(stripos($headers['Link'], 'rel="next"'));
253
        // Out of bounds
254
        $request = new WP_REST_Request('GET', '/wp/v2/users');
255
        $request->set_param('page', 8);
256
        $response = $this->server->dispatch($request);
257
        $headers = $response->get_headers();
258
        $this->assertEquals(52, $headers['X-WP-Total']);
259
        $this->assertEquals(6, $headers['X-WP-TotalPages']);
260
        $prev_link = add_query_arg(
261
            array(
262
            'page'    => 6,
263
            ), rest_url('wp/v2/users') 
264
        );
265
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
266
        $this->assertFalse(stripos($headers['Link'], 'rel="next"'));
267
    }
268
269
    public function test_get_items_per_page() 
270
    {
271
        wp_set_current_user(self::$user);
272
        for ( $i = 0; $i < 20; $i++ ) {
273
            $this->factory->user->create(array( 'display_name' => "User {$i}" ));
274
        }
275
        $request = new WP_REST_Request('GET', '/wp/v2/users');
276
        $response = $this->server->dispatch($request);
277
        $this->assertEquals(10, count($response->get_data()));
278
        $request = new WP_REST_Request('GET', '/wp/v2/users');
279
        $request->set_param('per_page', 5);
280
        $response = $this->server->dispatch($request);
281
        $this->assertEquals(5, count($response->get_data()));
282
    }
283
284
    public function test_get_items_page() 
285
    {
286
        wp_set_current_user(self::$user);
287
        for ( $i = 0; $i < 20; $i++ ) {
288
            $this->factory->user->create(array( 'display_name' => "User {$i}" ));
289
        }
290
        $request = new WP_REST_Request('GET', '/wp/v2/users');
291
        $request->set_param('per_page', 5);
292
        $request->set_param('page', 2);
293
        $response = $this->server->dispatch($request);
294
        $this->assertEquals(5, count($response->get_data()));
295
        $prev_link = add_query_arg(
296
            array(
297
            'per_page'  => 5,
298
            'page'      => 1,
299
            ), rest_url('wp/v2/users') 
300
        );
301
        $headers = $response->get_headers();
302
        $this->assertContains('<' . $prev_link . '>; rel="prev"', $headers['Link']);
303
    }
304
305
    public function test_get_items_orderby_name() 
306
    {
307
        wp_set_current_user(self::$user);
308
        $low_id = $this->factory->user->create(array( 'display_name' => 'AAAAA' ));
309
        $mid_id = $this->factory->user->create(array( 'display_name' => 'NNNNN' ));
310
        $high_id = $this->factory->user->create(array( 'display_name' => 'ZZZZ' ));
311
        $request = new WP_REST_Request('GET', '/wp/v2/users');
312
        $request->set_param('orderby', 'name');
313
        $request->set_param('order', 'desc');
314
        $request->set_param('per_page', 1);
315
        $response = $this->server->dispatch($request);
316
        $data = $response->get_data();
317
        $this->assertEquals($high_id, $data[0]['id']);
318
        $request = new WP_REST_Request('GET', '/wp/v2/users');
319
        $request->set_param('orderby', 'name');
320
        $request->set_param('order', 'asc');
321
        $request->set_param('per_page', 1);
322
        $response = $this->server->dispatch($request);
323
        $data = $response->get_data();
324
        $this->assertEquals($low_id, $data[0]['id']);
325
    }
326
327 View Code Duplication
    public function test_get_items_orderby_url() 
328
    {
329
        wp_set_current_user(self::$user);
330
331
        $low_id = $this->factory->user->create(array( 'user_url' => 'http://a.com' ));
332
        $high_id = $this->factory->user->create(array( 'user_url' => 'http://b.com' ));
333
334
        $request = new WP_REST_Request('GET', '/wp/v2/users');
335
        $request->set_param('orderby', 'url');
336
        $request->set_param('order', 'desc');
337
        $request->set_param('per_page', 1);
338
        $request->set_param('include', array( $low_id, $high_id ));
339
        $response = $this->server->dispatch($request);
340
        $data = $response->get_data();
341
342
        $this->assertEquals($high_id, $data[0]['id']);
343
344
        $request = new WP_REST_Request('GET', '/wp/v2/users');
345
        $request->set_param('orderby', 'url');
346
        $request->set_param('order', 'asc');
347
        $request->set_param('per_page', 1);
348
        $request->set_param('include', array( $low_id, $high_id ));
349
        $response = $this->server->dispatch($request);
350
        $data = $response->get_data();
351
        $this->assertEquals($low_id, $data[0]['id']);
352
    }
353
354 View Code Duplication
    public function test_get_items_orderby_slug() 
355
    {
356
        wp_set_current_user(self::$user);
357
358
        $high_id = $this->factory->user->create(array( 'user_nicename' => 'blogin' ));
359
        $low_id = $this->factory->user->create(array( 'user_nicename' => 'alogin' ));
360
361
        $request = new WP_REST_Request('GET', '/wp/v2/users');
362
        $request->set_param('orderby', 'slug');
363
        $request->set_param('order', 'desc');
364
        $request->set_param('per_page', 1);
365
        $request->set_param('include', array( $low_id, $high_id ));
366
        $response = $this->server->dispatch($request);
367
        $data = $response->get_data();
368
369
        $this->assertEquals($high_id, $data[0]['id']);
370
371
        $request = new WP_REST_Request('GET', '/wp/v2/users');
372
        $request->set_param('orderby', 'slug');
373
        $request->set_param('order', 'asc');
374
        $request->set_param('per_page', 1);
375
        $request->set_param('include', array( $low_id, $high_id ));
376
        $response = $this->server->dispatch($request);
377
        $data = $response->get_data();
378
        $this->assertEquals($low_id, $data[0]['id']);
379
    }
380
381 View Code Duplication
    public function test_get_items_orderby_email() 
382
    {
383
        wp_set_current_user(self::$user);
384
385
        $high_id = $this->factory->user->create(array( 'user_email' => '[email protected]' ));
386
        $low_id = $this->factory->user->create(array( 'user_email' => '[email protected]' ));
387
388
        $request = new WP_REST_Request('GET', '/wp/v2/users');
389
        $request->set_param('orderby', 'email');
390
        $request->set_param('order', 'desc');
391
        $request->set_param('per_page', 1);
392
        $request->set_param('include', array( $low_id, $high_id ));
393
        $response = $this->server->dispatch($request);
394
        $data = $response->get_data();
395
        $this->assertEquals($high_id, $data[0]['id']);
396
397
        $request = new WP_REST_Request('GET', '/wp/v2/users');
398
        $request->set_param('orderby', 'email');
399
        $request->set_param('order', 'asc');
400
        $request->set_param('per_page', 1);
401
        $request->set_param('include', array( $low_id, $high_id ));
402
        $response = $this->server->dispatch($request);
403
        $data = $response->get_data();
404
        $this->assertEquals($low_id, $data[0]['id']);
405
    }
406
407
    public function test_get_items_orderby_email_unauthenticated() 
408
    {
409
        $request = new WP_REST_Request('GET', '/wp/v2/users');
410
        $request->set_param('orderby', 'email');
411
        $request->set_param('order', 'desc');
412
        $response = $this->server->dispatch($request);
413
        $this->assertErrorResponse('rest_forbidden_orderby', $response, 401);
414
    }
415
416
    public function test_get_items_orderby_registered_date_unauthenticated() 
417
    {
418
        $request = new WP_REST_Request('GET', '/wp/v2/users');
419
        $request->set_param('orderby', 'registered_date');
420
        $request->set_param('order', 'desc');
421
        $response = $this->server->dispatch($request);
422
        $this->assertErrorResponse('rest_forbidden_orderby', $response, 401);
423
    }
424
425 View Code Duplication
    public function test_get_items_invalid_order() 
426
    {
427
        $request = new WP_REST_Request('GET', '/wp/v2/users');
428
        $request->set_param('order', 'asc,id');
429
        $response = $this->server->dispatch($request);
430
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
431
    }
432
433 View Code Duplication
    public function test_get_items_invalid_orderby() 
434
    {
435
        $request = new WP_REST_Request('GET', '/wp/v2/users');
436
        $request->set_param('orderby', 'invalid');
437
        $response = $this->server->dispatch($request);
438
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
439
    }
440
441
    public function test_get_items_offset() 
442
    {
443
        wp_set_current_user(self::$user);
444
        // 2 users created in __construct(), plus default user
445
        $this->factory->user->create();
446
        $request = new WP_REST_Request('GET', '/wp/v2/users');
447
        $request->set_param('offset', 1);
448
        $response = $this->server->dispatch($request);
449
        $this->assertCount(4, $response->get_data());
450
        // 'offset' works with 'per_page'
451
        $request->set_param('per_page', 2);
452
        $response = $this->server->dispatch($request);
453
        $this->assertCount(2, $response->get_data());
454
        // 'offset' takes priority over 'page'
455
        $request->set_param('page', 3);
456
        $response = $this->server->dispatch($request);
457
        $this->assertCount(2, $response->get_data());
458
        // 'offset' invalid value should error
459
        $request->set_param('offset', 'moreplease');
460
        $response = $this->server->dispatch($request);
461
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
462
    }
463
464
    public function test_get_items_include_query() 
465
    {
466
        wp_set_current_user(self::$user);
467
        $id1 = $this->factory->user->create();
468
        $id2 = $this->factory->user->create();
469
        $id3 = $this->factory->user->create();
470
        $request = new WP_REST_Request('GET', '/wp/v2/users');
471
        // Orderby=>asc
472
        $request->set_param('include', array( $id3, $id1 ));
473
        $response = $this->server->dispatch($request);
474
        $data = $response->get_data();
475
        $this->assertEquals(2, count($data));
476
        $this->assertEquals($id1, $data[0]['id']);
477
        // Orderby=>include
478
        $request->set_param('orderby', 'include');
479
        $response = $this->server->dispatch($request);
480
        $data = $response->get_data();
481
        $this->assertEquals(2, count($data));
482
        $this->assertEquals($id3, $data[0]['id']);
483
        // Invalid include should fail
484
        $request->set_param('include', 'invalid');
485
        $response = $this->server->dispatch($request);
486
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
487
        // No privileges
488
        $request->set_param('include', array( $id3, $id1 ));
489
        wp_set_current_user(0);
490
        $response = $this->server->dispatch($request);
491
        $data = $response->get_data();
492
        $this->assertEquals(0, count($data));
493
494
    }
495
496 View Code Duplication
    public function test_get_items_exclude_query() 
497
    {
498
        wp_set_current_user(self::$user);
499
        $id1 = $this->factory->user->create();
500
        $id2 = $this->factory->user->create();
501
        $request = new WP_REST_Request('GET', '/wp/v2/users');
502
        $response = $this->server->dispatch($request);
503
        $data = $response->get_data();
504
        $this->assertTrue(in_array($id1, wp_list_pluck($data, 'id'), true));
505
        $this->assertTrue(in_array($id2, wp_list_pluck($data, 'id'), true));
506
        $request->set_param('exclude', array( $id2 ));
507
        $response = $this->server->dispatch($request);
508
        $data = $response->get_data();
509
        $this->assertTrue(in_array($id1, wp_list_pluck($data, 'id'), true));
510
        $this->assertFalse(in_array($id2, wp_list_pluck($data, 'id'), true));
511
        // Invalid exlude value should error.
512
        $request->set_param('exclude', 'none-of-those-please');
513
        $response = $this->server->dispatch($request);
514
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
515
    }
516
517
    public function test_get_items_search() 
518
    {
519
        wp_set_current_user(self::$user);
520
        $request = new WP_REST_Request('GET', '/wp/v2/users');
521
        $request->set_param('search', 'yololololo');
522
        $response = $this->server->dispatch($request);
523
        $this->assertEquals(0, count($response->get_data()));
524
        $yolo_id = $this->factory->user->create(array( 'display_name' => 'yololololo' ));
525
        $request = new WP_REST_Request('GET', '/wp/v2/users');
526
        $request->set_param('search', (string) $yolo_id);
527
        $response = $this->server->dispatch($request);
528
        $this->assertEquals(1, count($response->get_data()));
529
        // default to wildcard search
530
        $adam_id = $this->factory->user->create(
531
            array(
532
            'role'          => 'author',
533
            'user_nicename' => 'adam',
534
            ) 
535
        );
536
        $request = new WP_REST_Request('GET', '/wp/v2/users');
537
        $request->set_param('search', 'ada');
538
        $response = $this->server->dispatch($request);
539
        $data = $response->get_data();
540
        $this->assertEquals(1, count($data));
541
        $this->assertEquals($adam_id, $data[0]['id']);
542
    }
543
544 View Code Duplication
    public function test_get_items_slug_query() 
545
    {
546
        wp_set_current_user(self::$user);
547
        $this->factory->user->create(array( 'display_name' => 'foo', 'user_login' => 'bar' ));
548
        $id2 = $this->factory->user->create(array( 'display_name' => 'Moo', 'user_login' => 'foo' ));
549
        $request = new WP_REST_Request('GET', '/wp/v2/users');
550
        $request->set_param('slug', 'foo');
551
        $response = $this->server->dispatch($request);
552
        $data = $response->get_data();
553
        $this->assertEquals(1, count($data));
554
        $this->assertEquals($id2, $data[0]['id']);
555
    }
556
557
    // Note: Do not test using editor role as there is an editor role created in testing and it makes it hard to test this functionality.
558
    public function test_get_items_roles() 
559
    {
560
        wp_set_current_user(self::$user);
561
        $tango = $this->factory->user->create(array( 'display_name' => 'tango', 'role' => 'subscriber' ));
562
        $yolo  = $this->factory->user->create(array( 'display_name' => 'yolo', 'role' => 'author' ));
563
        $request = new WP_REST_Request('GET', '/wp/v2/users');
564
        $request->set_param('roles', 'author,subscriber');
565
        $response = $this->server->dispatch($request);
566
        $data = $response->get_data();
567
        $this->assertEquals(2, count($data));
568
        $this->assertEquals($tango, $data[0]['id']);
569
        $this->assertEquals($yolo, $data[1]['id']);
570
        $request->set_param('roles', 'author');
571
        $response = $this->server->dispatch($request);
572
        $data = $response->get_data();
573
        $this->assertEquals(1, count($data));
574
        $this->assertEquals($yolo, $data[0]['id']);
575
        wp_set_current_user(0);
576
        $request->set_param('roles', 'author');
577
        $response = $this->server->dispatch($request);
578
        $this->assertErrorResponse('rest_user_cannot_view', $response, 401);
579
        wp_set_current_user(self::$editor);
580
        $request->set_param('roles', 'author');
581
        $response = $this->server->dispatch($request);
582
        $this->assertErrorResponse('rest_user_cannot_view', $response, 403);
583
    }
584
585
    public function test_get_items_invalid_roles() 
586
    {
587
        wp_set_current_user(self::$user);
588
        $lolz = $this->factory->user->create(array( 'display_name' => 'lolz', 'role' => 'author' ));
589
        $request = new WP_REST_Request('GET', '/wp/v2/users');
590
        $request->set_param('roles', 'ilovesteak,author');
591
        $response = $this->server->dispatch($request);
592
        $data = $response->get_data();
593
        $this->assertEquals(1, count($data));
594
        $this->assertEquals($lolz, $data[0]['id']);
595
        $request = new WP_REST_Request('GET', '/wp/v2/users');
596
        $request->set_param('roles', 'steakisgood');
597
        $response = $this->server->dispatch($request);
598
        $data = $response->get_data();
599
        $this->assertEquals(0, count($data));
600
        $this->assertEquals(array(), $data);
601
    }
602
603 View Code Duplication
    public function test_get_item() 
604
    {
605
        $user_id = $this->factory->user->create();
606
        wp_set_current_user(self::$user);
607
608
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/users/%d', $user_id));
609
610
        $response = $this->server->dispatch($request);
611
        $this->check_get_user_response($response, 'embed');
612
    }
613
614
    public function test_prepare_item() 
615
    {
616
        wp_set_current_user(self::$user);
617
        $request = new WP_REST_Request;
618
        $request->set_param('context', 'edit');
619
        $user = get_user_by('id', get_current_user_id());
620
        $data = $this->endpoint->prepare_item_for_response($user, $request);
621
        $this->check_get_user_response($data, 'edit');
622
    }
623
624 View Code Duplication
    public function test_get_user_avatar_urls() 
625
    {
626
        wp_set_current_user(self::$user);
627
628
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/users/%d', self::$editor));
629
630
        $response = $this->server->dispatch($request);
631
632
        $data = $response->get_data();
633
        $this->assertArrayHasKey(24,  $data['avatar_urls']);
634
        $this->assertArrayHasKey(48,  $data['avatar_urls']);
635
        $this->assertArrayHasKey(96,  $data['avatar_urls']);
636
637
        $user = get_user_by('id', self::$editor);
638
        /**
639
         * Ignore the subdomain, since 'get_avatar_url randomly sets the Gravatar
640
         * server when building the url string.
641
         */
642
        $this->assertEquals(substr(get_avatar_url($user->user_email), 9), substr($data['avatar_urls'][96], 9));
643
    }
644
645 View Code Duplication
    public function test_get_user_invalid_id() 
646
    {
647
        wp_set_current_user(self::$user);
648
        $request = new WP_REST_Request('GET', '/wp/v2/users/100');
649
        $response = $this->server->dispatch($request);
650
651
        $this->assertErrorResponse('rest_user_invalid_id', $response, 404);
652
    }
653
654
    public function test_get_user_empty_capabilities() 
655
    {
656
        wp_set_current_user(self::$user);
657
        $this->allow_user_to_manage_multisite();
658
659
        $lolz = $this->factory->user->create(array( 'display_name' => 'lolz', 'roles' => '' ));
660
        delete_user_option($lolz, 'capabilities');
661
        delete_user_option($lolz, 'user_level');
662
        $request = new WP_REST_Request('GET', '/wp/v2/users/' . $lolz);
663
        $request->set_param('context', 'edit');
664
        $response = $this->server->dispatch($request);
665
        $data = $response->get_data();
666
667
        $this->assertEquals($data['capabilities'], new stdClass());
668
        $this->assertEquals($data['extra_capabilities'], new stdClass());
669
    }
670
671
    public function test_get_item_without_permission() 
672
    {
673
        wp_set_current_user(self::$editor);
674
675
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/users/%d', self::$user));
676
        $response = $this->server->dispatch($request);
677
678
        $this->assertErrorResponse('rest_user_cannot_view', $response, 403);
679
    }
680
681
    public function test_get_item_published_author_post() 
682
    {
683
        $this->author_id = $this->factory->user->create(
684
            array(
685
            'role' => 'author',
686
            ) 
687
        );
688
        $this->post_id = $this->factory->post->create(
689
            array(
690
            'post_author' => $this->author_id,
691
            )
692
        );
693
        wp_set_current_user(0);
694
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/users/%d', $this->author_id));
695
        $response = $this->server->dispatch($request);
696
        $this->check_get_user_response($response, 'embed');
697
    }
698
699
    public function test_get_item_published_author_pages() 
700
    {
701
        $this->author_id = $this->factory->user->create(
702
            array(
703
            'role' => 'author',
704
            ) 
705
        );
706
        wp_set_current_user(0);
707
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/users/%d', $this->author_id));
708
        $response = $this->server->dispatch($request);
709
        $this->assertEquals(401, $response->get_status());
710
        $this->post_id = $this->factory->post->create(
711
            array(
712
            'post_author' => $this->author_id,
713
            'post_type'   => 'page',
714
            )
715
        );
716
        $response = $this->server->dispatch($request);
717
        $this->check_get_user_response($response, 'embed');
718
    }
719
720
    public function test_get_user_with_edit_context() 
721
    {
722
        $user_id = $this->factory->user->create();
723
        $this->allow_user_to_manage_multisite();
724
725
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/users/%d', $user_id));
726
        $request->set_param('context', 'edit');
727
728
        $response = $this->server->dispatch($request);
729
        $this->check_get_user_response($response, 'edit');
730
    }
731
732
    public function test_get_item_published_author_wrong_context() 
733
    {
734
        $this->author_id = $this->factory->user->create(
735
            array(
736
            'role' => 'author',
737
            ) 
738
        );
739
        $this->post_id = $this->factory->post->create(
740
            array(
741
            'post_author' => $this->author_id,
742
            )
743
        );
744
        wp_set_current_user(0);
745
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/users/%d', $this->author_id));
746
        $request->set_param('context', 'edit');
747
        $response = $this->server->dispatch($request);
748
        $this->assertErrorResponse('rest_user_cannot_view', $response, 401);
749
    }
750
751 View Code Duplication
    public function test_get_current_user() 
752
    {
753
        wp_set_current_user(self::$user);
754
755
        $request = new WP_REST_Request('GET', '/wp/v2/users/me');
756
757
        $response = $this->server->dispatch($request);
758
        $this->assertEquals(200, $response->get_status());
759
        $this->check_get_user_response($response, 'view');
760
761
        $headers = $response->get_headers();
762
        $this->assertArrayNotHasKey('Location', $headers);
763
764
        $links = $response->get_links();
765
        $this->assertEquals(rest_url('wp/v2/users/' . self::$user), $links['self'][0]['href']);
766
    }
767
768 View Code Duplication
    public function test_get_current_user_without_permission() 
769
    {
770
        wp_set_current_user(0);
771
        $request = new WP_REST_Request('GET', '/wp/v2/users/me');
772
        $response = $this->server->dispatch($request);
773
774
        $this->assertErrorResponse('rest_not_logged_in', $response, 401);
775
    }
776
777
    public function test_create_item() 
778
    {
779
        $this->allow_user_to_manage_multisite();
780
        wp_set_current_user(self::$user);
781
782
        $params = array(
783
         'username'    => 'testuser',
784
         'password'    => 'testpassword',
785
         'email'       => '[email protected]',
786
         'name'        => 'Test User',
787
         'nickname'    => 'testuser',
788
         'slug'        => 'test-user',
789
         'roles'       => array( 'editor' ),
790
         'description' => 'New API User',
791
         'url'         => 'http://example.com',
792
        );
793
794
        $request = new WP_REST_Request('POST', '/wp/v2/users');
795
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
796
        $request->set_body_params($params);
797
798
        $response = $this->server->dispatch($request);
799
        $data = $response->get_data();
800
        $this->assertEquals('http://example.com', $data['url']);
801
        $this->assertEquals(array( 'editor' ), $data['roles']);
802
        $this->check_add_edit_user_response($response);
803
    }
804
805
    public function test_create_item_invalid_username() 
806
    {
807
        $this->allow_user_to_manage_multisite();
808
        wp_set_current_user(self::$user);
809
810
        $params = array(
811
         'username'    => '¯\_(ツ)_/¯',
812
         'password'    => 'testpassword',
813
         'email'       => '[email protected]',
814
         'name'        => 'Test User',
815
         'nickname'    => 'testuser',
816
         'slug'        => 'test-user',
817
         'roles'       => array( 'editor' ),
818
         'description' => 'New API User',
819
         'url'         => 'http://example.com',
820
        );
821
822
        // Username rules are different (more strict) for multisite; see `wpmu_validate_user_signup`
823
        if (is_multisite() ) {
824
            $params['username'] = 'no-dashes-allowed';
825
        }
826
827
        $request = new WP_REST_Request('POST', '/wp/v2/users');
828
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
829
        $request->set_body_params($params);
830
831
        $response = $this->server->dispatch($request);
832
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
833
834
        $data = $response->get_data();
835
        if (is_multisite() ) {
836
            $this->assertInternalType('array', $data['additional_errors']);
837
            $this->assertCount(1, $data['additional_errors']);
838
            $error = $data['additional_errors'][0];
839
            $this->assertEquals('user_name', $error['code']);
840
            $this->assertEquals('Usernames can only contain lowercase letters (a-z) and numbers.', $error['message']);
841
        } else {
842
            $this->assertInternalType('array', $data['data']['params']);
843
            $errors = $data['data']['params'];
844
            $this->assertInternalType('string', $errors['username']);
845
            $this->assertEquals('Username contains invalid characters.', $errors['username']);
846
        }
847
    }
848
849
    function get_illegal_user_logins() 
850
    {
851
        return array( 'nope' );
852
    }
853
854
    public function test_create_item_illegal_username() 
855
    {
856
        $this->allow_user_to_manage_multisite();
857
        wp_set_current_user(self::$user);
858
859
        add_filter('illegal_user_logins', array( $this, 'get_illegal_user_logins' ));
860
861
        $params = array(
862
         'username'    => 'nope',
863
         'password'    => 'testpassword',
864
         'email'       => '[email protected]',
865
         'name'        => 'Test User',
866
         'nickname'    => 'testuser',
867
         'slug'        => 'test-user',
868
         'roles'       => array( 'editor' ),
869
         'description' => 'New API User',
870
         'url'         => 'http://example.com',
871
        );
872
873
        $request = new WP_REST_Request('POST', '/wp/v2/users');
874
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
875
        $request->set_body_params($params);
876
877
        $response = $this->server->dispatch($request);
878
879
        remove_filter('illegal_user_logins', array( $this, 'get_illegal_user_logins' ));
880
881
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
882
883
        $data = $response->get_data();
884
        $this->assertInternalType('array', $data['data']['params']);
885
        $errors = $data['data']['params'];
886
        $this->assertInternalType('string', $errors['username']);
887
        $this->assertEquals('Sorry, that username is not allowed.', $errors['username']);
888
    }
889
890
    public function test_create_new_network_user_on_site_does_not_add_user_to_sub_site() 
891
    {
892
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
893
            $this->markTestSkipped('Test requires multisite.');
894
        }
895
896
        $this->allow_user_to_manage_multisite();
897
898
        $params = array(
899
         'username' => 'testuser123',
900
         'password' => 'testpassword',
901
         'email'    => '[email protected]',
902
         'name'     => 'Test User 123',
903
         'roles'    => array( 'editor' ),
904
        );
905
906
        $request = new WP_REST_Request('POST', '/wp/v2/users');
907
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
908
        $request->set_body_params($params);
909
        $response = $this->server->dispatch($request);
910
        $data = $response->get_data();
911
        $user_id = $data['id'];
912
913
        $user_is_member = is_user_member_of_blog($user_id, self::$site);
914
915
        wpmu_delete_user($user_id);
916
917
        $this->assertFalse($user_is_member);
918
    }
919
920
    public function test_create_new_network_user_on_sub_site_adds_user_to_site() 
921
    {
922
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
923
            $this->markTestSkipped('Test requires multisite.');
924
        }
925
926
        $this->allow_user_to_manage_multisite();
927
928
        $params = array(
929
         'username' => 'testuser123',
930
         'password' => 'testpassword',
931
         'email'    => '[email protected]',
932
         'name'     => 'Test User 123',
933
         'roles'    => array( 'editor' ),
934
        );
935
936
        switch_to_blog(self::$site);
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
937
938
        $request = new WP_REST_Request('POST', '/wp/v2/users');
939
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
940
        $request->set_body_params($params);
941
        $response = $this->server->dispatch($request);
942
        $data = $response->get_data();
943
        $user_id = $data['id'];
944
945
        restore_current_blog();
946
947
        $user_is_member = is_user_member_of_blog($user_id, self::$site);
948
949
        wpmu_delete_user($user_id);
950
951
        $this->assertTrue($user_is_member);
952
    }
953
954
    public function test_create_existing_network_user_on_sub_site_has_error() 
955
    {
956
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
957
            $this->markTestSkipped('Test requires multisite.');
958
        }
959
960
        $this->allow_user_to_manage_multisite();
961
962
        $params = array(
963
         'username' => 'testuser123',
964
         'password' => 'testpassword',
965
         'email'    => '[email protected]',
966
         'name'     => 'Test User 123',
967
         'roles'    => array( 'editor' ),
968
        );
969
970
        $request = new WP_REST_Request('POST', '/wp/v2/users');
971
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
972
        $request->set_body_params($params);
973
        $response = $this->server->dispatch($request);
974
        $data = $response->get_data();
975
        $user_id = $data['id'];
976
977
        switch_to_blog(self::$site);
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
978
979
        $request = new WP_REST_Request('POST', '/wp/v2/users');
980
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
981
        $request->set_body_params($params);
982
        $switched_response = $this->server->dispatch($request);
983
984
        restore_current_blog();
985
986
        wpmu_delete_user($user_id);
987
988
        $this->assertErrorResponse('rest_invalid_param', $switched_response, 400);
989
        $data = $switched_response->get_data();
990
        $this->assertInternalType('array', $data['additional_errors']);
991
        $this->assertCount(2, $data['additional_errors']);
992
        $errors = $data['additional_errors'];
993
        foreach ( $errors as $error ) {
994
            // Check the code matches one we know.
995
            $this->assertContains($error['code'], array( 'user_name', 'user_email' ));
996
            if ('user_name' === $error['code'] ) {
997
                $this->assertEquals('Sorry, that username already exists!', $error['message']);
998
            } else {
999
                $this->assertEquals('Sorry, that email address is already used!', $error['message']);
1000
            }
1001
        }
1002
    }
1003
1004
    public function test_update_existing_network_user_on_sub_site_adds_user_to_site() 
1005
    {
1006
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1007
            $this->markTestSkipped('Test requires multisite.');
1008
        }
1009
1010
        $this->allow_user_to_manage_multisite();
1011
1012
        $params = array(
1013
         'username' => 'testuser123',
1014
         'password' => 'testpassword',
1015
         'email'    => '[email protected]',
1016
         'name'     => 'Test User 123',
1017
         'roles'    => array( 'editor' ),
1018
        );
1019
1020
        $request = new WP_REST_Request('POST', '/wp/v2/users');
1021
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1022
        $request->set_body_params($params);
1023
        $response = $this->server->dispatch($request);
1024
        $data = $response->get_data();
1025
        $user_id = $data['id'];
1026
1027
        switch_to_blog(self::$site);
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
1028
1029
        $request = new WP_REST_Request('PUT', '/wp/v2/users/' . $user_id);
1030
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1031
        $request->set_body_params($params);
1032
        $this->server->dispatch($request);
1033
1034
        restore_current_blog();
1035
1036
        $user_is_member = is_user_member_of_blog($user_id, self::$site);
1037
1038
        wpmu_delete_user($user_id);
1039
1040
        $this->assertTrue($user_is_member);
1041
    }
1042
1043
    public function test_json_create_user() 
1044
    {
1045
        $this->allow_user_to_manage_multisite();
1046
        wp_set_current_user(self::$user);
1047
1048
        $params = array(
1049
         'username' => 'testjsonuser',
1050
         'password' => 'testjsonpassword',
1051
         'email'    => '[email protected]',
1052
        );
1053
1054
        $request = new WP_REST_Request('POST', '/wp/v2/users');
1055
        $request->add_header('content-type', 'application/json');
1056
        $request->set_body(wp_json_encode($params));
1057
1058
        $response = $this->server->dispatch($request);
1059
        $this->check_add_edit_user_response($response);
1060
    }
1061
1062
    public function test_create_user_without_permission() 
1063
    {
1064
        wp_set_current_user(self::$editor);
1065
1066
        $params = array(
1067
         'username' => 'homersimpson',
1068
         'password' => 'stupidsexyflanders',
1069
         'email'    => '[email protected]',
1070
        );
1071
1072
        $request = new WP_REST_Request('POST', '/wp/v2/users');
1073
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1074
        $request->set_body_params($params);
1075
        $response = $this->server->dispatch($request);
1076
1077
        $this->assertErrorResponse('rest_cannot_create_user', $response, 403);
1078
    }
1079
1080 View Code Duplication
    public function test_create_user_invalid_id() 
1081
    {
1082
        $this->allow_user_to_manage_multisite();
1083
        wp_set_current_user(self::$user);
1084
1085
        $params = array(
1086
         'id'       => '156',
1087
         'username' => 'lisasimpson',
1088
         'password' => 'DavidHasselhoff',
1089
         'email'    => '[email protected]',
1090
        );
1091
1092
        $request = new WP_REST_Request('POST', '/wp/v2/users');
1093
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1094
        $request->set_body_params($params);
1095
        $response = $this->server->dispatch($request);
1096
1097
        $this->assertErrorResponse('rest_user_exists', $response, 400);
1098
    }
1099
1100 View Code Duplication
    public function test_create_user_invalid_email() 
1101
    {
1102
        $this->allow_user_to_manage_multisite();
1103
        wp_set_current_user(self::$user);
1104
1105
        $params = array(
1106
         'username' => 'lisasimpson',
1107
         'password' => 'DavidHasselhoff',
1108
         'email'    => 'something',
1109
        );
1110
1111
        $request = new WP_REST_Request('POST', '/wp/v2/users');
1112
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1113
        $request->set_body_params($params);
1114
        $response = $this->server->dispatch($request);
1115
1116
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1117
    }
1118
1119 View Code Duplication
    public function test_create_user_invalid_role() 
1120
    {
1121
        $this->allow_user_to_manage_multisite();
1122
        wp_set_current_user(self::$user);
1123
1124
        $params = array(
1125
         'username' => 'maggiesimpson',
1126
         'password' => 'i_shot_mrburns',
1127
         'email'    => '[email protected]',
1128
         'roles'    => array( 'baby' ),
1129
        );
1130
1131
        $request = new WP_REST_Request('POST', '/wp/v2/users');
1132
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1133
        $request->set_body_params($params);
1134
        $response = $this->server->dispatch($request);
1135
1136
        $this->assertErrorResponse('rest_user_invalid_role', $response, 400);
1137
    }
1138
1139
    public function test_update_item() 
1140
    {
1141
        $user_id = $this->factory->user->create(
1142
            array(
1143
            'user_email' => '[email protected]',
1144
            'user_pass' => 'sjflsfls',
1145
            'user_login' => 'test_update',
1146
            'first_name' => 'Old Name',
1147
            'user_url' => 'http://apple.com',
1148
            'locale' => 'en_US',
1149
            )
1150
        );
1151
        $this->allow_user_to_manage_multisite();
1152
        wp_set_current_user(self::$user);
1153
1154
        $userdata = get_userdata($user_id);
1155
        $pw_before = $userdata->user_pass;
1156
1157
        $_POST['email'] = $userdata->user_email;
1158
        $_POST['username'] = $userdata->user_login;
1159
        $_POST['first_name'] = 'New Name';
1160
        $_POST['url'] = 'http://google.com';
1161
        $_POST['locale'] = 'de_DE';
1162
1163
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', $user_id));
1164
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1165
        $request->set_body_params($_POST);
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
1166
1167
        $response = $this->server->dispatch($request);
1168
        $this->check_add_edit_user_response($response, true);
1169
1170
        // Check that the name has been updated correctly
1171
        $new_data = $response->get_data();
1172
        $this->assertEquals('New Name', $new_data['first_name']);
1173
        $user = get_userdata($user_id);
1174
        $this->assertEquals('New Name', $user->first_name);
1175
1176
        $this->assertEquals('http://google.com', $new_data['url']);
1177
        $this->assertEquals('http://google.com', $user->user_url);
1178
        $this->assertEquals('de_DE', $user->locale);
1179
1180
        // Check that we haven't inadvertently changed the user's password,
1181
        // as per https://core.trac.wordpress.org/ticket/21429
1182
        $this->assertEquals($pw_before, $user->user_pass);
1183
    }
1184
1185
    public function test_update_item_no_change() 
1186
    {
1187
        $this->allow_user_to_manage_multisite();
1188
        wp_set_current_user(self::$user);
1189
        $user = get_userdata(self::$editor);
1190
1191
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', self::$editor));
1192
        $request->set_param('slug', $user->user_nicename);
1193
1194
        // Run twice to make sure that the update still succeeds even if no DB
1195
        // rows are updated.
1196
        $response = $this->server->dispatch($request);
1197
        $this->assertEquals(200, $response->get_status());
1198
1199
        $response = $this->server->dispatch($request);
1200
        $this->assertEquals(200, $response->get_status());
1201
    }
1202
1203 View Code Duplication
    public function test_update_item_existing_email() 
1204
    {
1205
        $user1 = $this->factory->user->create(array( 'user_login' => 'test_json_user', 'user_email' => '[email protected]' ));
1206
        $user2 = $this->factory->user->create(array( 'user_login' => 'test_json_user2', 'user_email' => '[email protected]' ));
1207
        $this->allow_user_to_manage_multisite();
1208
        wp_set_current_user(self::$user);
1209
1210
        $request = new WP_REST_Request('PUT', '/wp/v2/users/' . $user2);
1211
        $request->set_param('email', '[email protected]');
1212
        $response = $this->server->dispatch($request);
1213
        $this->assertInstanceOf('WP_Error', $response->as_error());
1214
        $this->assertEquals('rest_user_invalid_email', $response->as_error()->get_error_code());
1215
    }
1216
1217
    public function test_update_item_invalid_locale() 
1218
    {
1219
        $user1 = $this->factory->user->create(array( 'user_login' => 'test_json_user', 'user_email' => '[email protected]' ));
1220
        $this->allow_user_to_manage_multisite();
1221
        wp_set_current_user(self::$user);
1222
1223
        $request = new WP_REST_Request('PUT', '/wp/v2/users/' . $user1);
1224
        $request->set_param('locale', 'klingon');
1225
        $response = $this->server->dispatch($request);
1226
        $this->assertInstanceOf('WP_Error', $response->as_error());
1227
        $this->assertEquals('rest_invalid_param', $response->as_error()->get_error_code());
1228
    }
1229
1230
    public function test_update_item_en_US_locale() 
0 ignored issues
show
Coding Style introduced by
The function name test_update_item_en_US_locale is in camel caps, but expected test_update_item_en_u_s_locale instead as per the coding standard.
Loading history...
1231
    {
1232
        $user_id = $this->factory->user->create(array( 'user_login' => 'test_json_user', 'user_email' => '[email protected]' ));
1233
        $this->allow_user_to_manage_multisite();
1234
        wp_set_current_user(self::$user);
1235
1236
        $request = new WP_REST_Request('PUT', '/wp/v2/users/' . $user_id);
1237
        $request->set_param('locale', 'en_US');
1238
        $response = $this->server->dispatch($request);
1239
        $this->check_add_edit_user_response($response, true);
1240
1241
        $user = get_userdata($user_id);
1242
        $this->assertEquals('en_US', $user->locale);
1243
    }
1244
1245
    /**
1246
     * @ticket 38632
1247
     */
1248
    public function test_update_item_empty_locale() 
1249
    {
1250
        $user_id = $this->factory->user->create(array( 'user_login' => 'test_json_user', 'user_email' => '[email protected]', 'locale' => 'de_DE' ));
1251
        $this->allow_user_to_manage_multisite();
1252
        wp_set_current_user(self::$user);
1253
1254
        $request = new WP_REST_Request('PUT', '/wp/v2/users/' . $user_id);
1255
        $request->set_param('locale', '');
1256
        $response = $this->server->dispatch($request);
1257
        $this->check_add_edit_user_response($response, true);
1258
1259
        $data = $response->get_data();
1260
        $this->assertEquals(get_locale(), $data['locale']);
1261
        $user = get_userdata($user_id);
1262
        $this->assertEquals('', $user->locale);
1263
    }
1264
1265 View Code Duplication
    public function test_update_item_username_attempt() 
1266
    {
1267
        $user1 = $this->factory->user->create(array( 'user_login' => 'test_json_user', 'user_email' => '[email protected]' ));
1268
        $user2 = $this->factory->user->create(array( 'user_login' => 'test_json_user2', 'user_email' => '[email protected]' ));
1269
        $this->allow_user_to_manage_multisite();
1270
        wp_set_current_user(self::$user);
1271
1272
        $request = new WP_REST_Request('PUT', '/wp/v2/users/' . $user2);
1273
        $request->set_param('username', 'test_json_user');
1274
        $response = $this->server->dispatch($request);
1275
        $this->assertInstanceOf('WP_Error', $response->as_error());
1276
        $this->assertEquals('rest_user_invalid_argument', $response->as_error()->get_error_code());
1277
    }
1278
1279 View Code Duplication
    public function test_update_item_existing_nicename() 
1280
    {
1281
        $user1 = $this->factory->user->create(array( 'user_login' => 'test_json_user', 'user_email' => '[email protected]' ));
1282
        $user2 = $this->factory->user->create(array( 'user_login' => 'test_json_user2', 'user_email' => '[email protected]' ));
1283
        $this->allow_user_to_manage_multisite();
1284
        wp_set_current_user(self::$user);
1285
1286
        $request = new WP_REST_Request('PUT', '/wp/v2/users/' . $user2);
1287
        $request->set_param('slug', 'test_json_user');
1288
        $response = $this->server->dispatch($request);
1289
        $this->assertInstanceOf('WP_Error', $response->as_error());
1290
        $this->assertEquals('rest_user_invalid_slug', $response->as_error()->get_error_code());
1291
    }
1292
1293
    public function test_json_update_user() 
1294
    {
1295
        $user_id = $this->factory->user->create(
1296
            array(
1297
            'user_email' => '[email protected]',
1298
            'user_pass'  => 'sjflsfl3sdjls',
1299
            'user_login' => 'test_json_update',
1300
            'first_name' => 'Old Name',
1301
            'last_name'  => 'Original Last',
1302
            )
1303
        );
1304
        $this->allow_user_to_manage_multisite();
1305
        wp_set_current_user(self::$user);
1306
1307
        $params = array(
1308
         'username'   => 'test_json_update',
1309
         'email'      => '[email protected]',
1310
         'first_name' => 'JSON Name',
1311
         'last_name'  => 'New Last',
1312
        );
1313
1314
        $userdata = get_userdata($user_id);
1315
        $pw_before = $userdata->user_pass;
1316
1317
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', $user_id));
1318
        $request->add_header('content-type', 'application/json');
1319
        $request->set_body(wp_json_encode($params));
1320
1321
        $response = $this->server->dispatch($request);
1322
        $this->check_add_edit_user_response($response, true);
1323
1324
        // Check that the name has been updated correctly
1325
        $new_data = $response->get_data();
1326
        $this->assertEquals('JSON Name', $new_data['first_name']);
1327
        $this->assertEquals('New Last', $new_data['last_name']);
1328
        $user = get_userdata($user_id);
1329
        $this->assertEquals('JSON Name', $user->first_name);
1330
        $this->assertEquals('New Last', $user->last_name);
1331
1332
        // Check that we haven't inadvertently changed the user's password,
1333
        // as per https://core.trac.wordpress.org/ticket/21429
1334
        $this->assertEquals($pw_before, $user->user_pass);
1335
    }
1336
1337
    public function test_update_user_role() 
1338
    {
1339
        $user_id = $this->factory->user->create(array( 'role' => 'administrator' ));
1340
1341
        wp_set_current_user(self::$user);
1342
        $this->allow_user_to_manage_multisite();
1343
1344
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', $user_id));
1345
        $request->set_param('roles', array( 'editor' ));
1346
        $response = $this->server->dispatch($request);
1347
1348
        $new_data = $response->get_data();
1349
1350
        $this->assertEquals('editor', $new_data['roles'][0]);
1351
        $this->assertNotEquals('administrator', $new_data['roles'][0]);
1352
1353
        $user = get_userdata($user_id);
1354
        $this->assertArrayHasKey('editor', $user->caps);
1355
        $this->assertArrayNotHasKey('administrator', $user->caps);
1356
    }
1357
1358
    public function test_update_user_multiple_roles() 
1359
    {
1360
        $user_id = $this->factory->user->create(array( 'role' => 'administrator' ));
1361
1362
        wp_set_current_user(self::$user);
1363
        $this->allow_user_to_manage_multisite();
1364
1365
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', $user_id));
1366
        $request->set_param('roles', 'author,editor');
1367
        $response = $this->server->dispatch($request);
1368
1369
        $new_data = $response->get_data();
1370
1371
        $this->assertEquals(array( 'author', 'editor' ), $new_data['roles']);
1372
1373
        $user = get_userdata($user_id);
1374
        $this->assertArrayHasKey('author', $user->caps);
1375
        $this->assertArrayHasKey('editor', $user->caps);
1376
        $this->assertArrayNotHasKey('administrator', $user->caps);
1377
    }
1378
1379 View Code Duplication
    public function test_update_user_role_invalid_privilege_escalation() 
1380
    {
1381
        wp_set_current_user(self::$editor);
1382
1383
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', self::$editor));
1384
        $request->set_param('roles', array( 'administrator' ));
1385
        $response = $this->server->dispatch($request);
1386
1387
        $this->assertErrorResponse('rest_cannot_edit_roles', $response, 403);
1388
        $user = get_userdata(self::$editor);
1389
        $this->assertArrayHasKey('editor', $user->caps);
1390
        $this->assertArrayNotHasKey('administrator', $user->caps);
1391
1392
        $request = new WP_REST_Request('PUT', '/wp/v2/users/me');
1393
        $request->set_param('roles', array( 'administrator' ));
1394
        $response = $this->server->dispatch($request);
1395
1396
        $this->assertErrorResponse('rest_cannot_edit_roles', $response, 403);
1397
        $user = get_userdata(self::$editor);
1398
        $this->assertArrayHasKey('editor', $user->caps);
1399
        $this->assertArrayNotHasKey('administrator', $user->caps);
1400
    }
1401
1402
    public function test_update_user_role_invalid_privilege_deescalation() 
1403
    {
1404
        if (is_multisite() ) {
1405
            return $this->markTestSkipped('Test only intended for single site.');
1406
        }
1407
1408
        $user_id = $this->factory->user->create(array( 'role' => 'administrator' ));
1409
1410
        wp_set_current_user($user_id);
1411
1412
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', $user_id));
1413
        $request->set_param('roles', array( 'editor' ));
1414
        $response = $this->server->dispatch($request);
1415
1416
        $this->assertErrorResponse('rest_user_invalid_role', $response, 403);
1417
1418
        $user = get_userdata($user_id);
1419
        $this->assertArrayHasKey('administrator', $user->caps);
1420
        $this->assertArrayNotHasKey('editor', $user->caps);
1421
1422
        $request = new WP_REST_Request('PUT', '/wp/v2/users/me');
1423
        $request->set_param('roles', array( 'editor' ));
1424
        $response = $this->server->dispatch($request);
1425
1426
        $this->assertErrorResponse('rest_user_invalid_role', $response, 403);
1427
1428
        $user = get_userdata($user_id);
1429
        $this->assertArrayHasKey('administrator', $user->caps);
1430
        $this->assertArrayNotHasKey('editor', $user->caps);
1431
    }
1432
1433
    public function test_update_user_role_privilege_deescalation_multisite() 
1434
    {
1435
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1436
            return $this->markTestSkipped('Test only intended for multisite.');
1437
        }
1438
1439
        $user_id = $this->factory->user->create(array( 'role' => 'administrator' ));
1440
1441
        wp_set_current_user($user_id);
1442
        $user = wp_get_current_user();
1443
        update_site_option('site_admins', array( $user->user_login ));
1444
1445
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', $user_id));
1446
        $request->set_param('roles', array( 'editor' ));
1447
        $response = $this->server->dispatch($request);
1448
1449
        $new_data = $response->get_data();
1450
        $this->assertEquals('editor', $new_data['roles'][0]);
1451
        $this->assertNotEquals('administrator', $new_data['roles'][0]);
1452
1453
        $user_id = $this->factory->user->create(array( 'role' => 'administrator' ));
1454
1455
        wp_set_current_user($user_id);
1456
        $user = wp_get_current_user();
1457
        update_site_option('site_admins', array( $user->user_login ));
1458
1459
        $request = new WP_REST_Request('PUT', '/wp/v2/users/me');
1460
        $request->set_param('roles', array( 'editor' ));
1461
        $response = $this->server->dispatch($request);
1462
1463
        $new_data = $response->get_data();
1464
        $this->assertEquals('editor', $new_data['roles'][0]);
1465
        $this->assertNotEquals('administrator', $new_data['roles'][0]);
1466
    }
1467
1468
1469 View Code Duplication
    public function test_update_user_role_invalid_role() 
1470
    {
1471
        wp_set_current_user(self::$user);
1472
        $this->allow_user_to_manage_multisite();
1473
1474
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', self::$editor));
1475
        $request->set_param('roles', array( 'BeSharp' ));
1476
        $response = $this->server->dispatch($request);
1477
1478
        $this->assertErrorResponse('rest_user_invalid_role', $response, 400);
1479
1480
        $user = get_userdata(self::$editor);
1481
        $this->assertArrayHasKey('editor', $user->caps);
1482
        $this->assertArrayNotHasKey('BeSharp', $user->caps);
1483
1484
        $request = new WP_REST_Request('PUT', '/wp/v2/users/me');
1485
        $request->set_param('roles', array( 'BeSharp' ));
1486
        $response = $this->server->dispatch($request);
1487
1488
        $this->assertErrorResponse('rest_user_invalid_role', $response, 400);
1489
1490
        $user = get_userdata(self::$editor);
1491
        $this->assertArrayHasKey('editor', $user->caps);
1492
        $this->assertArrayNotHasKey('BeSharp', $user->caps);
1493
    }
1494
1495 View Code Duplication
    public function test_update_user_without_permission() 
1496
    {
1497
        wp_set_current_user(self::$editor);
1498
1499
        $params = array(
1500
         'username' => 'homersimpson',
1501
         'password' => 'stupidsexyflanders',
1502
         'email'    => '[email protected]',
1503
        );
1504
1505
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', self::$user));
1506
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1507
        $request->set_body_params($params);
1508
        $response = $this->server->dispatch($request);
1509
1510
        $this->assertErrorResponse('rest_cannot_edit', $response, 403);
1511
1512
        $request = new WP_REST_Request('PUT', '/wp/v2/users/me');
1513
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1514
        $request->set_body_params($params);
1515
        $response = $this->server->dispatch($request);
1516
1517
        $this->assertErrorResponse('rest_user_invalid_argument', $response, 400);
1518
    }
1519
1520 View Code Duplication
    public function test_update_user_invalid_id() 
1521
    {
1522
        $this->allow_user_to_manage_multisite();
1523
        wp_set_current_user(self::$user);
1524
1525
        $params = array(
1526
         'id'       => '156',
1527
         'username' => 'lisasimpson',
1528
         'password' => 'DavidHasselhoff',
1529
         'email'    => '[email protected]',
1530
        );
1531
1532
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', self::$editor));
1533
        $request->add_header('content-type', 'application/x-www-form-urlencoded');
1534
        $request->set_body_params($params);
1535
        $response = $this->server->dispatch($request);
1536
1537
        $this->assertErrorResponse('rest_user_invalid_id', $response, 404);
1538
    }
1539
1540 View Code Duplication
    public function test_update_item_invalid_password() 
1541
    {
1542
        $this->allow_user_to_manage_multisite();
1543
        wp_set_current_user(self::$user);
1544
1545
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', self::$editor));
1546
1547
        $request->set_param('password', 'no\\backslashes\\allowed');
1548
        $response = $this->server->dispatch($request);
1549
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1550
1551
        $request->set_param('password', '');
1552
        $response = $this->server->dispatch($request);
1553
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1554
    }
1555
1556
    public function verify_user_roundtrip( $input = array(), $expected_output = array() ) 
1557
    {
1558
        if (isset($input['id']) ) {
1559
            // Existing user; don't try to create one
1560
            $user_id = $input['id'];
1561
        } else {
1562
            // Create a new user
1563
            $request = new WP_REST_Request('POST', '/wp/v2/users');
1564
            foreach ( $input as $name => $value ) {
1565
                $request->set_param($name, $value);
1566
            }
1567
            $request->set_param('email', '[email protected]');
1568
            $response = $this->server->dispatch($request);
1569
            $this->assertEquals(201, $response->get_status());
1570
            $actual_output = $response->get_data();
1571
1572
            // Compare expected API output to actual API output
1573
            $this->assertEquals($expected_output['username'], $actual_output['username']);
1574
            $this->assertEquals($expected_output['name'], $actual_output['name']);
1575
            $this->assertEquals($expected_output['first_name'], $actual_output['first_name']);
1576
            $this->assertEquals($expected_output['last_name'], $actual_output['last_name']);
1577
            $this->assertEquals($expected_output['url'], $actual_output['url']);
1578
            $this->assertEquals($expected_output['description'], $actual_output['description']);
1579
            $this->assertEquals($expected_output['nickname'], $actual_output['nickname']);
1580
1581
            // Compare expected API output to WP internal values
1582
            $user = get_userdata($actual_output['id']);
1583
            $this->assertEquals($expected_output['username'], $user->user_login);
1584
            $this->assertEquals($expected_output['name'], $user->display_name);
1585
            $this->assertEquals($expected_output['first_name'], $user->first_name);
1586
            $this->assertEquals($expected_output['last_name'], $user->last_name);
1587
            $this->assertEquals($expected_output['url'], $user->user_url);
1588
            $this->assertEquals($expected_output['description'], $user->description);
1589
            $this->assertEquals($expected_output['nickname'], $user->nickname);
1590
            $this->assertTrue(wp_check_password(addslashes($expected_output['password']), $user->user_pass));
1591
1592
            $user_id = $actual_output['id'];
1593
        }
1594
1595
        // Update the user
1596
        $request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', $user_id));
1597
        foreach ( $input as $name => $value ) {
1598
            if ('username' !== $name ) {
1599
                $request->set_param($name, $value);
1600
            }
1601
        }
1602
        $response = $this->server->dispatch($request);
1603
        $this->assertEquals(200, $response->get_status());
1604
        $actual_output = $response->get_data();
1605
1606
        // Compare expected API output to actual API output
1607
        if (isset($expected_output['username']) ) {
1608
            $this->assertEquals($expected_output['username'], $actual_output['username']);
1609
        }
1610
        $this->assertEquals($expected_output['name'], $actual_output['name']);
1611
        $this->assertEquals($expected_output['first_name'], $actual_output['first_name']);
1612
        $this->assertEquals($expected_output['last_name'], $actual_output['last_name']);
1613
        $this->assertEquals($expected_output['url'], $actual_output['url']);
1614
        $this->assertEquals($expected_output['description'], $actual_output['description']);
1615
        $this->assertEquals($expected_output['nickname'], $actual_output['nickname']);
1616
1617
        // Compare expected API output to WP internal values
1618
        $user = get_userdata($actual_output['id']);
1619
        if (isset($expected_output['username']) ) {
1620
            $this->assertEquals($expected_output['username'], $user->user_login);
1621
        }
1622
        $this->assertEquals($expected_output['name'], $user->display_name);
1623
        $this->assertEquals($expected_output['first_name'], $user->first_name);
1624
        $this->assertEquals($expected_output['last_name'], $user->last_name);
1625
        $this->assertEquals($expected_output['url'], $user->user_url);
1626
        $this->assertEquals($expected_output['description'], $user->description);
1627
        $this->assertEquals($expected_output['nickname'], $user->nickname);
1628
        $this->assertTrue(wp_check_password(addslashes($expected_output['password']), $user->user_pass));
1629
    }
1630
1631
    public function test_user_roundtrip_as_editor() 
1632
    {
1633
        wp_set_current_user(self::$editor);
1634
        $this->assertEquals(! is_multisite(), current_user_can('unfiltered_html'));
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1635
        $this->verify_user_roundtrip(
1636
            array(
1637
            'id'          => self::$editor,
1638
            'name'        => '\o/ ¯\_(ツ)_/¯',
1639
            'first_name'  => '\o/ ¯\_(ツ)_/¯',
1640
            'last_name'   => '\o/ ¯\_(ツ)_/¯',
1641
            'url'         => '\o/ ¯\_(ツ)_/¯',
1642
            'description' => '\o/ ¯\_(ツ)_/¯',
1643
            'nickname'    => '\o/ ¯\_(ツ)_/¯',
1644
            'password'    => 'o/ ¯_(ツ)_/¯ \'"',
1645
            ), array(
1646
            'name'        => '\o/ ¯\_(ツ)_/¯',
1647
            'first_name'  => '\o/ ¯\_(ツ)_/¯',
1648
            'last_name'   => '\o/ ¯\_(ツ)_/¯',
1649
            'url'         => 'http://o/%20¯_(ツ)_/¯',
1650
            'description' => '\o/ ¯\_(ツ)_/¯',
1651
            'nickname'    => '\o/ ¯\_(ツ)_/¯',
1652
            'password'    => 'o/ ¯_(ツ)_/¯ \'"',
1653
            ) 
1654
        );
1655
    }
1656
1657
    public function test_user_roundtrip_as_editor_html() 
1658
    {
1659
        wp_set_current_user(self::$editor);
1660
        if (is_multisite() ) {
1661
            $this->assertFalse(current_user_can('unfiltered_html'));
1662
            $this->verify_user_roundtrip(
1663
                array(
1664
                'id'          => self::$editor,
1665
                'name'        => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1666
                'first_name'  => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1667
                'last_name'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1668
                'url'         => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1669
                'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1670
                'nickname'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1671
                'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1672
                ), array(
1673
                'name'        => 'div strong',
1674
                'first_name'  => 'div strong',
1675
                'last_name'   => 'div strong',
1676
                'url'         => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script',
1677
                'description' => 'div <strong>strong</strong> oh noes',
1678
                'nickname'    => 'div strong',
1679
                'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1680
                ) 
1681
            );
1682
        } else {
1683
            $this->assertTrue(current_user_can('unfiltered_html'));
1684
            $this->verify_user_roundtrip(
1685
                array(
1686
                'id'          => self::$editor,
1687
                'name'        => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1688
                'first_name'  => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1689
                'last_name'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1690
                'url'         => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1691
                'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1692
                'nickname'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1693
                'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1694
                ), array(
1695
                'name'        => 'div strong',
1696
                'first_name'  => 'div strong',
1697
                'last_name'   => 'div strong',
1698
                'url'         => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script',
1699
                'description' => 'div <strong>strong</strong> oh noes',
1700
                'nickname'    => 'div strong',
1701
                'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1702
                ) 
1703
            );
1704
        }
1705
    }
1706
1707 View Code Duplication
    public function test_user_roundtrip_as_superadmin() 
1708
    {
1709
        wp_set_current_user(self::$superadmin);
1710
        $this->assertTrue(current_user_can('unfiltered_html'));
1711
        $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here';
1712
        $this->verify_user_roundtrip(
1713
            array(
1714
            'username'    => $valid_username,
1715
            'name'        => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
1716
            'first_name'  => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
1717
            'last_name'   => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
1718
            'url'         => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
1719
            'description' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
1720
            'nickname'    => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
1721
            'password'    => '& &amp; &invalid; < &lt; &amp;lt;',
1722
            ), array(
1723
            'username'    => $valid_username,
1724
            'name'        => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
1725
            'first_name'  => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
1726
            'last_name'   => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
1727
            'url'         => 'http://&amp;%20&amp;%20&amp;invalid;%20%20&lt;%20&amp;lt;',
1728
            'description' => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
1729
            'nickname'    => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
1730
            'password'    => '& &amp; &invalid; < &lt; &amp;lt;',
1731
            ) 
1732
        );
1733
    }
1734
1735 View Code Duplication
    public function test_user_roundtrip_as_superadmin_html() 
1736
    {
1737
        wp_set_current_user(self::$superadmin);
1738
        $this->assertTrue(current_user_can('unfiltered_html'));
1739
        $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here';
1740
        $this->verify_user_roundtrip(
1741
            array(
1742
            'username'    => $valid_username,
1743
            'name'        => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1744
            'first_name'  => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1745
            'last_name'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1746
            'url'         => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1747
            'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1748
            'nickname'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1749
            'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1750
            ), array(
1751
            'username'    => $valid_username,
1752
            'name'        => 'div strong',
1753
            'first_name'  => 'div strong',
1754
            'last_name'   => 'div strong',
1755
            'url'         => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script',
1756
            'description' => 'div <strong>strong</strong> oh noes',
1757
            'nickname'    => 'div strong',
1758
            'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
1759
            ) 
1760
        );
1761
    }
1762
1763
    public function test_delete_item() 
1764
    {
1765
        $user_id = $this->factory->user->create(array( 'display_name' => 'Deleted User' ));
1766
1767
        $this->allow_user_to_manage_multisite();
1768
        wp_set_current_user(self::$user);
1769
1770
        $userdata = get_userdata($user_id); // cache for later
1771
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/users/%d', $user_id));
1772
        $request->set_param('force', true);
1773
        $request->set_param('reassign', false);
1774
        $response = $this->server->dispatch($request);
1775
1776
        // Not implemented in multisite.
1777
        if (is_multisite() ) {
1778
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
1779
            return;
1780
        }
1781
1782
        $this->assertEquals(200, $response->get_status());
1783
        $data = $response->get_data();
1784
        $this->assertTrue($data['deleted']);
1785
        $this->assertEquals('Deleted User', $data['previous']['name']);
1786
    }
1787
1788
    public function test_delete_item_no_trash() 
1789
    {
1790
        $user_id = $this->factory->user->create(array( 'display_name' => 'Deleted User' ));
1791
1792
        $this->allow_user_to_manage_multisite();
1793
        wp_set_current_user(self::$user);
1794
1795
        $userdata = get_userdata($user_id); // cache for later
1796
1797
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/users/%d', $user_id));
1798
        $request->set_param('reassign', false);
1799
        $response = $this->server->dispatch($request);
1800
1801
        // Not implemented in multisite.
1802
        if (is_multisite() ) {
1803
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
1804
            return;
1805
        }
1806
1807
        $this->assertErrorResponse('rest_trash_not_supported', $response, 501);
1808
1809
        $request->set_param('force', 'false');
1810
        $response = $this->server->dispatch($request);
1811
        $this->assertErrorResponse('rest_trash_not_supported', $response, 501);
1812
1813
        // Ensure the user still exists
1814
        $user = get_user_by('id', $user_id);
1815
        $this->assertNotEmpty($user);
1816
    }
1817
1818
    public function test_delete_current_item() 
1819
    {
1820
        $user_id = $this->factory->user->create(array( 'role' => 'administrator', 'display_name' => 'Deleted User' ));
1821
1822
        wp_set_current_user($user_id);
1823
        $user = wp_get_current_user();
1824
        update_site_option('site_admins', array( $user->user_login ));
1825
1826
        $request = new WP_REST_Request('DELETE', '/wp/v2/users/me');
1827
        $request['force'] = true;
1828
        $request->set_param('reassign', false);
1829
        $response = $this->server->dispatch($request);
1830
1831
        // Not implemented in multisite.
1832
        if (is_multisite() ) {
1833
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
1834
            return;
1835
        }
1836
1837
        $this->assertEquals(200, $response->get_status());
1838
        $data = $response->get_data();
1839
        $this->assertTrue($data['deleted']);
1840
        $this->assertEquals('Deleted User', $data['previous']['name']);
1841
    }
1842
1843
    public function test_delete_current_item_no_trash() 
1844
    {
1845
        $user_id = $this->factory->user->create(array( 'role' => 'administrator', 'display_name' => 'Deleted User' ));
1846
1847
        wp_set_current_user($user_id);
1848
        $user = wp_get_current_user();
1849
        update_site_option('site_admins', array( $user->user_login ));
1850
1851
        $request = new WP_REST_Request('DELETE', '/wp/v2/users/me');
1852
        $request->set_param('reassign', false);
1853
        $response = $this->server->dispatch($request);
1854
1855
        // Not implemented in multisite.
1856
        if (is_multisite() ) {
1857
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
1858
            return;
1859
        }
1860
1861
        $this->assertErrorResponse('rest_trash_not_supported', $response, 501);
1862
1863
        $request->set_param('force', 'false');
1864
        $response = $this->server->dispatch($request);
1865
        $this->assertErrorResponse('rest_trash_not_supported', $response, 501);
1866
1867
        // Ensure the user still exists
1868
        $user = get_user_by('id', $user_id);
1869
        $this->assertNotEmpty($user);
1870
    }
1871
1872
    public function test_delete_user_without_permission() 
1873
    {
1874
        $user_id = $this->factory->user->create();
1875
1876
        $this->allow_user_to_manage_multisite();
1877
        wp_set_current_user(self::$editor);
1878
1879
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/users/%d', $user_id));
1880
        $request['force'] = true;
1881
        $request->set_param('reassign', false);
1882
        $response = $this->server->dispatch($request);
1883
1884
        $this->assertErrorResponse('rest_user_cannot_delete', $response, 403);
1885
1886
        $request = new WP_REST_Request('DELETE', '/wp/v2/users/me');
1887
        $request['force'] = true;
1888
        $request->set_param('reassign', false);
1889
        $response = $this->server->dispatch($request);
1890
1891
        $this->assertErrorResponse('rest_user_cannot_delete', $response, 403);
1892
    }
1893
1894 View Code Duplication
    public function test_delete_user_invalid_id() 
1895
    {
1896
        $this->allow_user_to_manage_multisite();
1897
        wp_set_current_user(self::$user);
1898
1899
        $request = new WP_REST_Request('DELETE', '/wp/v2/users/100');
1900
        $request['force'] = true;
1901
        $request->set_param('reassign', false);
1902
        $response = $this->server->dispatch($request);
1903
1904
        // Not implemented in multisite.
1905
        if (is_multisite() ) {
1906
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
1907
            return;
1908
        }
1909
1910
        $this->assertErrorResponse('rest_user_invalid_id', $response, 404);
1911
    }
1912
1913
    public function test_delete_user_reassign() 
1914
    {
1915
        $this->allow_user_to_manage_multisite();
1916
1917
        // Test with a new user, to avoid any complications
1918
        $user_id = $this->factory->user->create();
1919
        $reassign_id = $this->factory->user->create();
1920
        $test_post = $this->factory->post->create(
1921
            array(
1922
            'post_author' => $user_id,
1923
            )
1924
        );
1925
1926
        // Sanity check to ensure the factory created the post correctly
1927
        $post = get_post($test_post);
1928
        $this->assertEquals($user_id, $post->post_author);
1929
1930
        // Delete our test user, and reassign to the new author
1931
        wp_set_current_user(self::$user);
1932
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/users/%d', $user_id));
1933
        $request['force'] = true;
1934
        $request->set_param('reassign', $reassign_id);
1935
        $response = $this->server->dispatch($request);
1936
1937
        // Not implemented in multisite.
1938
        if (is_multisite() ) {
1939
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
1940
            return;
1941
        }
1942
1943
        $this->assertEquals(200, $response->get_status());
1944
1945
        // Check that the post has been updated correctly
1946
        $post = get_post($test_post);
1947
        $this->assertEquals($reassign_id, $post->post_author);
1948
    }
1949
1950
    public function test_delete_user_invalid_reassign_id() 
1951
    {
1952
        $user_id = $this->factory->user->create();
1953
1954
        $this->allow_user_to_manage_multisite();
1955
        wp_set_current_user(self::$user);
1956
1957
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/users/%d', $user_id));
1958
        $request['force'] = true;
1959
        $request->set_param('reassign', 100);
1960
        $response = $this->server->dispatch($request);
1961
1962
        // Not implemented in multisite.
1963
        if (is_multisite() ) {
1964
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
1965
            return;
1966
        }
1967
1968
        $this->assertErrorResponse('rest_user_invalid_reassign', $response, 400);
1969
    }
1970
1971
    public function test_delete_user_invalid_reassign_passed_as_string() 
1972
    {
1973
        $user_id = $this->factory->user->create();
1974
1975
        $this->allow_user_to_manage_multisite();
1976
        wp_set_current_user(self::$user);
1977
1978
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/users/%d', $user_id));
1979
        $request['force'] = true;
1980
        $request->set_param('reassign', 'null');
1981
        $response = $this->server->dispatch($request);
1982
1983
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
1984
    }
1985
1986 View Code Duplication
    public function test_delete_user_reassign_passed_as_boolean_false_trashes_post() 
1987
    {
1988
        $user_id = $this->factory->user->create();
1989
1990
        $this->allow_user_to_manage_multisite();
1991
        wp_set_current_user(self::$user);
1992
1993
        $test_post = $this->factory->post->create(
1994
            array(
1995
            'post_author' => $user_id,
1996
            )
1997
        );
1998
1999
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/users/%d', $user_id));
2000
        $request['force'] = true;
2001
        $request->set_param('reassign', false);
2002
        $response = $this->server->dispatch($request);
2003
2004
        // Not implemented in multisite.
2005
        if (is_multisite() ) {
2006
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
2007
            return;
2008
        }
2009
2010
        $test_post = get_post($test_post);
2011
        $this->assertEquals('trash', $test_post->post_status);
2012
    }
2013
2014 View Code Duplication
    public function test_delete_user_reassign_passed_as_string_false_trashes_post() 
2015
    {
2016
        $user_id = $this->factory->user->create();
2017
2018
        $this->allow_user_to_manage_multisite();
2019
        wp_set_current_user(self::$user);
2020
2021
        $test_post = $this->factory->post->create(
2022
            array(
2023
            'post_author' => $user_id,
2024
            )
2025
        );
2026
2027
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/users/%d', $user_id));
2028
        $request['force'] = true;
2029
        $request->set_param('reassign', 'false');
2030
        $response = $this->server->dispatch($request);
2031
2032
        // Not implemented in multisite.
2033
        if (is_multisite() ) {
2034
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
2035
            return;
2036
        }
2037
2038
        $test_post = get_post($test_post);
2039
        $this->assertEquals('trash', $test_post->post_status);
2040
    }
2041
2042 View Code Duplication
    public function test_delete_user_reassign_passed_as_empty_string_trashes_post() 
2043
    {
2044
        $user_id = $this->factory->user->create();
2045
2046
        $this->allow_user_to_manage_multisite();
2047
        wp_set_current_user(self::$user);
2048
2049
        $test_post = $this->factory->post->create(
2050
            array(
2051
            'post_author' => $user_id,
2052
            )
2053
        );
2054
2055
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/users/%d', $user_id));
2056
        $request['force'] = true;
2057
        $request->set_param('reassign', '');
2058
        $response = $this->server->dispatch($request);
2059
2060
        // Not implemented in multisite.
2061
        if (is_multisite() ) {
2062
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
2063
            return;
2064
        }
2065
2066
        $test_post = get_post($test_post);
2067
        $this->assertEquals('trash', $test_post->post_status);
2068
    }
2069
2070 View Code Duplication
    public function test_delete_user_reassign_passed_as_0_reassigns_author() 
2071
    {
2072
        $user_id = $this->factory->user->create();
2073
2074
        $this->allow_user_to_manage_multisite();
2075
        wp_set_current_user(self::$user);
2076
2077
        $test_post = $this->factory->post->create(
2078
            array(
2079
            'post_author' => $user_id,
2080
            )
2081
        );
2082
2083
        $request = new WP_REST_Request('DELETE', sprintf('/wp/v2/users/%d', $user_id));
2084
        $request['force'] = true;
2085
        $request->set_param('reassign', 0);
2086
        $response = $this->server->dispatch($request);
2087
2088
        // Not implemented in multisite.
2089
        if (is_multisite() ) {
2090
            $this->assertErrorResponse('rest_cannot_delete', $response, 501);
2091
            return;
2092
        }
2093
2094
        $test_post = get_post($test_post);
2095
        $this->assertEquals(0, $test_post->post_author);
2096
    }
2097
2098
    public function test_get_item_schema() 
2099
    {
2100
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/users');
2101
        $response = $this->server->dispatch($request);
2102
        $data = $response->get_data();
2103
        $properties = $data['schema']['properties'];
2104
2105
        $this->assertEquals(19, count($properties));
2106
        $this->assertArrayHasKey('avatar_urls', $properties);
2107
        $this->assertArrayHasKey('capabilities', $properties);
2108
        $this->assertArrayHasKey('description', $properties);
2109
        $this->assertArrayHasKey('email', $properties);
2110
        $this->assertArrayHasKey('extra_capabilities', $properties);
2111
        $this->assertArrayHasKey('first_name', $properties);
2112
        $this->assertArrayHasKey('id', $properties);
2113
        $this->assertArrayHasKey('last_name', $properties);
2114
        $this->assertArrayHasKey('link', $properties);
2115
        $this->assertArrayHasKey('locale', $properties);
2116
        $this->assertArrayHasKey('meta', $properties);
2117
        $this->assertArrayHasKey('name', $properties);
2118
        $this->assertArrayHasKey('nickname', $properties);
2119
        $this->assertArrayHasKey('registered_date', $properties);
2120
        $this->assertArrayHasKey('slug', $properties);
2121
        $this->assertArrayHasKey('password', $properties);
2122
        $this->assertArrayHasKey('url', $properties);
2123
        $this->assertArrayHasKey('username', $properties);
2124
        $this->assertArrayHasKey('roles', $properties);
2125
2126
    }
2127
2128 View Code Duplication
    public function test_get_item_schema_show_avatar() 
2129
    {
2130
        update_option('show_avatars', false);
2131
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/users');
2132
        $response = $this->server->dispatch($request);
2133
        $data = $response->get_data();
2134
        $properties = $data['schema']['properties'];
2135
2136
        $this->assertArrayNotHasKey('avatar_urls', $properties);
2137
    }
2138
2139
    public function test_get_additional_field_registration() 
2140
    {
2141
2142
        $schema = array(
2143
         'type'        => 'integer',
2144
         'description' => 'Some integer of mine',
2145
         'enum'        => array( 1, 2, 3, 4 ),
2146
         'context'     => array( 'embed', 'view', 'edit' ),
2147
        );
2148
2149
        register_rest_field(
2150
            'user', 'my_custom_int', array(
2151
            'schema'          => $schema,
2152
            'get_callback'    => array( $this, 'additional_field_get_callback' ),
2153
            'update_callback' => array( $this, 'additional_field_update_callback' ),
2154
            ) 
2155
        );
2156
2157
        $request = new WP_REST_Request('OPTIONS', '/wp/v2/users');
2158
2159
        $response = $this->server->dispatch($request);
2160
        $data = $response->get_data();
2161
2162
        $this->assertArrayHasKey('my_custom_int', $data['schema']['properties']);
2163
        $this->assertEquals($schema, $data['schema']['properties']['my_custom_int']);
2164
2165
        wp_set_current_user(1);
2166
        if (is_multisite() ) {
2167
            $current_user = wp_get_current_user(1);
2168
            update_site_option('site_admins', array( $current_user->user_login ));
2169
        }
2170
2171
        $request = new WP_REST_Request('GET', '/wp/v2/users/1');
2172
2173
        $response = $this->server->dispatch($request);
2174
        $this->assertArrayHasKey('my_custom_int', $response->data);
2175
2176
        $request = new WP_REST_Request('POST', '/wp/v2/users/1');
2177
        $request->set_body_params(
2178
            array(
2179
            'my_custom_int' => 123,
2180
            )
2181
        );
2182
2183
        $response = $this->server->dispatch($request);
2184
        $this->assertEquals(123, get_user_meta(1, 'my_custom_int', true));
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
2185
2186
        $request = new WP_REST_Request('POST', '/wp/v2/users');
2187
        $request->set_body_params(
2188
            array(
2189
            'my_custom_int' => 123,
2190
            'email' => '[email protected]',
2191
            'username' => 'abc123',
2192
            'password' => 'hello',
2193
            )
2194
        );
2195
2196
        $response = $this->server->dispatch($request);
2197
2198
        $this->assertEquals(123, $response->data['my_custom_int']);
2199
2200
        global $wp_rest_additional_fields;
2201
        $wp_rest_additional_fields = array();
2202
    }
2203
2204
    public function test_additional_field_update_errors() 
2205
    {
2206
        $schema = array(
2207
         'type'        => 'integer',
2208
         'description' => 'Some integer of mine',
2209
         'enum'        => array( 1, 2, 3, 4 ),
2210
         'context'     => array( 'view', 'edit' ),
2211
        );
2212
2213
        register_rest_field(
2214
            'user', 'my_custom_int', array(
2215
            'schema'          => $schema,
2216
            'get_callback'    => array( $this, 'additional_field_get_callback' ),
2217
            'update_callback' => array( $this, 'additional_field_update_callback' ),
2218
            ) 
2219
        );
2220
2221
        wp_set_current_user(1);
2222
        if (is_multisite() ) {
2223
            $current_user = wp_get_current_user(1);
2224
            update_site_option('site_admins', array( $current_user->user_login ));
2225
        }
2226
2227
        // Check for error on update.
2228
        $request = new WP_REST_Request('POST', sprintf('/wp/v2/users/%d', self::$user));
2229
        $request->set_body_params(
2230
            array(
2231
            'my_custom_int' => 'returnError',
2232
            ) 
2233
        );
2234
2235
        $response = $this->server->dispatch($request);
2236
2237
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
2238
2239
        global $wp_rest_additional_fields;
2240
        $wp_rest_additional_fields = array();
2241
    }
2242
2243
    public function additional_field_get_callback( $object ) 
2244
    {
2245
        return get_user_meta($object['id'], 'my_custom_int', true);
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
2246
    }
2247
2248 View Code Duplication
    public function additional_field_update_callback( $value, $user ) 
2249
    {
2250
        if ('returnError' === $value ) {
2251
            return new WP_Error('rest_invalid_param', 'Testing an error.', array( 'status' => 400 ));
2252
        }
2253
        update_user_meta($user->ID, 'my_custom_int', $value);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
2254
    }
2255
2256
    public function tearDown() 
0 ignored issues
show
Coding Style introduced by
The function name tearDown is in camel caps, but expected tear_down instead as per the coding standard.
Loading history...
2257
    {
2258
        parent::tearDown();
2259
    }
2260
2261
    protected function check_user_data( $user, $data, $context, $links ) 
2262
    {
2263
        $this->assertEquals($user->ID, $data['id']);
2264
        $this->assertEquals($user->display_name, $data['name']);
2265
        $this->assertEquals($user->user_url, $data['url']);
2266
        $this->assertEquals($user->description, $data['description']);
2267
        $this->assertEquals(get_author_posts_url($user->ID), $data['link']);
2268
        $this->assertArrayHasKey('avatar_urls', $data);
2269
        $this->assertEquals($user->user_nicename, $data['slug']);
2270
2271
        if ('edit' === $context ) {
2272
            $this->assertEquals($user->first_name, $data['first_name']);
2273
            $this->assertEquals($user->last_name, $data['last_name']);
2274
            $this->assertEquals($user->nickname, $data['nickname']);
2275
            $this->assertEquals($user->user_email, $data['email']);
2276
            $this->assertEquals((object) $user->allcaps, $data['capabilities']);
0 ignored issues
show
introduced by
No space before opening casting parenthesis is prohibited
Loading history...
2277
            $this->assertEquals((object) $user->caps, $data['extra_capabilities']);
0 ignored issues
show
introduced by
No space before opening casting parenthesis is prohibited
Loading history...
2278
            $this->assertEquals(date('c', strtotime($user->user_registered)), $data['registered_date']);
2279
            $this->assertEquals($user->user_login, $data['username']);
2280
            $this->assertEquals($user->roles, $data['roles']);
2281
            $this->assertEquals(get_user_locale($user), $data['locale']);
2282
        }
2283
2284
        if ('edit' !== $context ) {
2285
            $this->assertArrayNotHasKey('roles', $data);
2286
            $this->assertArrayNotHasKey('capabilities', $data);
2287
            $this->assertArrayNotHasKey('registered', $data);
2288
            $this->assertArrayNotHasKey('first_name', $data);
2289
            $this->assertArrayNotHasKey('last_name', $data);
2290
            $this->assertArrayNotHasKey('nickname', $data);
2291
            $this->assertArrayNotHasKey('extra_capabilities', $data);
2292
            $this->assertArrayNotHasKey('username', $data);
2293
        }
2294
2295
        $this->assertEqualSets(
2296
            array(
2297
            'self',
2298
            'collection',
2299
            ), array_keys($links) 
2300
        );
2301
2302
        $this->assertArrayNotHasKey('password', $data);
2303
    }
2304
2305 View Code Duplication
    protected function check_get_user_response( $response, $context = 'view' ) 
2306
    {
2307
        $this->assertEquals(200, $response->get_status());
2308
2309
        $data = $response->get_data();
2310
        $userdata = get_userdata($data['id']);
2311
        $this->check_user_data($userdata, $data, $context, $response->get_links());
2312
    }
2313
2314
    protected function check_add_edit_user_response( $response, $update = false ) 
2315
    {
2316
        if ($update ) {
2317
            $this->assertEquals(200, $response->get_status());
2318
        } else {
2319
            $this->assertEquals(201, $response->get_status());
2320
        }
2321
2322
        $data = $response->get_data();
2323
        $userdata = get_userdata($data['id']);
2324
        $this->check_user_data($userdata, $data, 'edit', $response->get_links());
2325
    }
2326
2327
    protected function allow_user_to_manage_multisite() 
2328
    {
2329
        wp_set_current_user(self::$user);
2330
        $user = wp_get_current_user();
2331
2332
        if (is_multisite() ) {
2333
            update_site_option('site_admins', array( $user->user_login ));
2334
        }
2335
2336
        return;
2337
    }
2338
}
2339