Tests_User_Query::test_get_and_set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Test WP_User Query, in wp-includes/user.php
4
 *
5
 * @group user
6
 */
7
class Tests_User_Query extends WP_UnitTestCase
8
{
9
    protected static $author_ids;
10
    protected static $sub_ids;
11
    protected static $editor_ids;
12
    protected static $contrib_id;
13
    protected static $admin_ids;
14
15
    protected $user_id;
16
17
    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...
18
    {
19
        self::$author_ids = $factory->user->create_many(
20
            4, array(
21
            'role' => 'author'
22
            ) 
23
        );
24
25
        self::$sub_ids = $factory->user->create_many(
26
            2, array(
27
            'role' => 'subscriber',
28
            ) 
29
        );
30
31
        self::$editor_ids = $factory->user->create_many(
32
            3, array(
33
            'role' => 'editor',
34
            ) 
35
        );
36
37
        self::$contrib_id = $factory->user->create(
38
            array(
39
            'role' => 'contributor',
40
            ) 
41
        );
42
43
        self::$admin_ids = $factory->user->create_many(
44
            2, array(
45
            'role' => 'administrator',
46
            ) 
47
        );
48
    }
49
50
    function test_get_and_set() 
51
    {
52
        $users = new WP_User_Query();
53
54
        $this->assertEquals('', $users->get('fields'));
55
        $this->assertEquals('', @$users->query_vars['fields']);
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
56
57
        $users->set('fields', 'all');
58
59
        $this->assertEquals('all', $users->get('fields'));
60
        $this->assertEquals('all', $users->query_vars['fields']);
61
62
        $users->set('fields', '');
63
        $this->assertEquals('', $users->get('fields'));
64
        $this->assertEquals('', $users->query_vars['fields']);
65
66
        $this->assertNull($users->get('does-not-exist'));
67
    }
68
69
    public function test_include_single() 
70
    {
71
        $q = new WP_User_Query(
72
            array(
73
            'fields' => '',
74
            'include' => self::$author_ids[0],
75
            ) 
76
        );
77
        $ids = $q->get_results();
78
79
        $this->assertEquals(array( self::$author_ids[0] ), $ids);
80
    }
81
82
    public function test_include_comma_separated() 
83
    {
84
        $q = new WP_User_Query(
85
            array(
86
            'fields' => '',
87
            'include' => self::$author_ids[0] . ', ' . self::$author_ids[2],
88
            ) 
89
        );
90
        $ids = $q->get_results();
91
92
        $this->assertEqualSets(array( self::$author_ids[0], self::$author_ids[2] ), $ids);
93
    }
94
95 View Code Duplication
    public function test_include_array() 
96
    {
97
        $q = new WP_User_Query(
98
            array(
99
            'fields' => '',
100
            'include' => array( self::$author_ids[0], self::$author_ids[2] ),
101
            ) 
102
        );
103
        $ids = $q->get_results();
104
105
        $this->assertEqualSets(array( self::$author_ids[0], self::$author_ids[2] ), $ids);
106
    }
107
108 View Code Duplication
    public function test_include_array_bad_values() 
109
    {
110
        $q = new WP_User_Query(
111
            array(
112
            'fields' => '',
113
            'include' => array( self::$author_ids[0], 'foo', self::$author_ids[2] ),
114
            ) 
115
        );
116
        $ids = $q->get_results();
117
118
        $this->assertEqualSets(array( self::$author_ids[0], self::$author_ids[2] ), $ids);
119
    }
120
121
    public function test_exclude() 
122
    {
123
        $q = new WP_User_Query(
124
            array(
125
            'fields' => '',
126
            'exclude' => self::$author_ids[1],
127
            ) 
128
        );
129
130
        $ids = $q->get_results();
131
132
        // Indirect test in order to ignore default user created during installation.
133
        $this->assertNotEmpty($ids);
134
        $this->assertNotContains(self::$author_ids[1], $ids);
135
    }
136
137
    public function test_get_all() 
138
    {
139
        $users = new WP_User_Query(array( 'blog_id' => get_current_blog_id() ));
140
        $users = $users->get_results();
141
142
        // +1 for the default user created during installation.
143
        $this->assertEquals(13, count($users));
144
        foreach ( $users as $user ) {
145
            $this->assertInstanceOf('WP_User', $user);
146
        }
147
148
        $users = new WP_User_Query(array( 'blog_id' => get_current_blog_id(), 'fields' => 'all_with_meta' ));
149
        $users = $users->get_results();
150
        $this->assertEquals(13, count($users));
151
        foreach ( $users as $user ) {
152
            $this->assertInstanceOf('WP_User', $user);
153
        }
154
    }
155
156
    /**
157
     * @dataProvider orderby_should_convert_non_prefixed_keys_data
158
     */
159
    public function test_orderby_should_convert_non_prefixed_keys( $short_key, $full_key ) 
160
    {
161
        $q = new WP_User_Query(
162
            array(
163
            'orderby' => $short_key,
164
            ) 
165
        );
166
167
        $this->assertContains("ORDER BY $full_key", $q->query_orderby);
168
    }
169
170
    public function orderby_should_convert_non_prefixed_keys_data() 
171
    {
172
        return array(
173
         array( 'nicename', 'user_nicename' ),
174
         array( 'email', 'user_email' ),
175
         array( 'url', 'user_url' ),
176
         array( 'registered', 'user_registered' ),
177
         array( 'name', 'display_name' ),
178
        );
179
    }
180
181 View Code Duplication
    public function test_orderby_meta_value() 
182
    {
183
        update_user_meta(self::$author_ids[0], 'last_name', 'Jones');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
184
        update_user_meta(self::$author_ids[1], 'last_name', 'Albert');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
185
        update_user_meta(self::$author_ids[2], 'last_name', 'Zorro');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
186
187
        $q = new WP_User_Query(
188
            array(
189
            'include' => self::$author_ids,
190
            'meta_key' => 'last_name',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
191
            'orderby' => 'meta_value',
192
            'fields' => 'ids'
193
            ) 
194
        );
195
196
        $expected = array( self::$author_ids[3], self::$author_ids[1], self::$author_ids[0], self::$author_ids[2] );
197
198
        $this->assertEquals($expected, $q->get_results());
199
    }
200
201
    /**
202
     * @ticket 27887
203
     */
204 View Code Duplication
    public function test_orderby_meta_value_num() 
205
    {
206
        update_user_meta(self::$author_ids[0], 'user_age', '101');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
207
        update_user_meta(self::$author_ids[1], 'user_age', '20');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
208
        update_user_meta(self::$author_ids[2], 'user_age', '25');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
209
210
        $q = new WP_User_Query(
211
            array(
212
            'include' => self::$author_ids,
213
            'meta_key' => 'user_age',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
214
            'orderby' => 'meta_value_num',
215
            'fields' => 'ids'
216
            ) 
217
        );
218
219
        $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
220
221
        $this->assertEquals($expected, $q->get_results());
222
    }
223
224
    /**
225
     * @ticket 31265
226
     */
227 View Code Duplication
    public function test_orderby_somekey_where_meta_key_is_somekey() 
228
    {
229
        update_user_meta(self::$author_ids[0], 'foo', 'zzz');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
230
        update_user_meta(self::$author_ids[1], 'foo', 'aaa');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
231
        update_user_meta(self::$author_ids[2], 'foo', 'jjj');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
232
233
        $q = new WP_User_Query(
234
            array(
235
            'include' => self::$author_ids,
236
            'meta_key' => 'foo',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
237
            'orderby' => 'foo',
238
            'fields' => 'ids'
239
            ) 
240
        );
241
242
        $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
243
244
        $this->assertEquals($expected, $q->get_results());
245
    }
246
247
    /**
248
     * @ticket 31265
249
     */
250
    public function test_orderby_clause_key() 
251
    {
252
        add_user_meta(self::$author_ids[0], 'foo', 'aaa');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
253
        add_user_meta(self::$author_ids[1], 'foo', 'zzz');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
254
        add_user_meta(self::$author_ids[2], 'foo', 'jjj');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
255
256
        $q = new WP_User_Query(
257
            array(
258
            'fields' => 'ids',
259
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
260
            'foo_key' => array(
261
            'key' => 'foo',
262
            'compare' => 'EXISTS',
263
            ),
264
            ),
265
            'orderby' => 'foo_key',
266
            'order' => 'DESC',
267
            ) 
268
        );
269
270
        $this->assertEquals(array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ), $q->results);
271
    }
272
273
    /**
274
     * @ticket 31265
275
     */
276 View Code Duplication
    public function test_orderby_clause_key_as_secondary_sort() 
277
    {
278
        $u1 = self::factory()->user->create(
279
            array(
280
            'user_registered' => '2015-01-28 03:00:00',
281
            ) 
282
        );
283
        $u2 = self::factory()->user->create(
284
            array(
285
            'user_registered' => '2015-01-28 05:00:00',
286
            ) 
287
        );
288
        $u3 = self::factory()->user->create(
289
            array(
290
            'user_registered' => '2015-01-28 03:00:00',
291
            ) 
292
        );
293
294
        add_user_meta($u1, 'foo', 'jjj');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
295
        add_user_meta($u2, 'foo', 'zzz');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
296
        add_user_meta($u3, 'foo', 'aaa');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
297
298
        $q = new WP_User_Query(
299
            array(
300
            'fields' => 'ids',
301
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
302
            'foo_key' => array(
303
            'key' => 'foo',
304
            'compare' => 'EXISTS',
305
            ),
306
            ),
307
            'orderby' => array(
308
            'comment_date' => 'asc',
309
            'foo_key' => 'asc',
310
            ),
311
            ) 
312
        );
313
314
        $this->assertEquals(array( $u3, $u1, $u2 ), $q->results);
315
    }
316
317
    /**
318
     * @ticket 31265
319
     */
320
    public function test_orderby_more_than_one_clause_key() 
321
    {
322
        add_user_meta(self::$author_ids[0], 'foo', 'jjj');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
323
        add_user_meta(self::$author_ids[1], 'foo', 'zzz');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
324
        add_user_meta(self::$author_ids[2], 'foo', 'jjj');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
325
        add_user_meta(self::$author_ids[0], 'bar', 'aaa');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
326
        add_user_meta(self::$author_ids[1], 'bar', 'ccc');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
327
        add_user_meta(self::$author_ids[2], 'bar', 'bbb');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
328
329
        $q = new WP_User_Query(
330
            array(
331
            'fields' => 'ids',
332
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
333
            'foo_key' => array(
334
            'key' => 'foo',
335
            'compare' => 'EXISTS',
336
            ),
337
            'bar_key' => array(
338
            'key' => 'bar',
339
            'compare' => 'EXISTS',
340
            ),
341
            ),
342
            'orderby' => array(
343
            'foo_key' => 'asc',
344
            'bar_key' => 'desc',
345
            ),
346
            ) 
347
        );
348
349
        $this->assertEquals(array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] ), $q->results);
350
    }
351
352
    /**
353
     * @ticket 30064
354
     */
355
    public function test_orderby_include_with_empty_include() 
356
    {
357
        $q = new WP_User_Query(
358
            array(
359
            'orderby' => 'include',
360
            ) 
361
        );
362
363
        $this->assertContains('ORDER BY user_login', $q->query_orderby);
364
    }
365
366
    /**
367
     * @ticket 30064
368
     */
369 View Code Duplication
    public function test_orderby_include() 
370
    {
371
        global $wpdb;
372
373
        $q = new WP_User_Query(
374
            array(
375
            'orderby' => 'include',
376
            'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ),
377
            'fields' => '',
378
            ) 
379
        );
380
381
        $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )';
0 ignored issues
show
introduced by
Usage of users/usermeta tables is highly discouraged in VIP context, For storing user additional user metadata, you should look at User Attributes.
Loading history...
382
        $this->assertContains($expected_orderby, $q->query_orderby);
383
384
        // assertEquals() respects order but ignores type (get_results() returns numeric strings).
385
        $this->assertEquals(array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results());
386
    }
387
388
    /**
389
     * @ticket 30064
390
     */
391 View Code Duplication
    public function test_orderby_include_duplicate_values() 
392
    {
393
        global $wpdb;
394
395
        $q = new WP_User_Query(
396
            array(
397
            'orderby' => 'include',
398
            'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[1], self::$author_ids[3] ),
399
            'fields' => '',
400
            ) 
401
        );
402
403
        $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )';
0 ignored issues
show
introduced by
Usage of users/usermeta tables is highly discouraged in VIP context, For storing user additional user metadata, you should look at User Attributes.
Loading history...
404
        $this->assertContains($expected_orderby, $q->query_orderby);
405
406
        // assertEquals() respects order but ignores type (get_results() returns numeric strings).
407
        $this->assertEquals(array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results());
408
    }
409
410
    /**
411
     * @ticket 31265
412
     */
413 View Code Duplication
    public function test_orderby_space_separated() 
414
    {
415
        $q = new WP_User_Query(
416
            array(
417
            'orderby' => 'login nicename',
418
            'order' => 'ASC',
419
            ) 
420
        );
421
422
        $this->assertContains("ORDER BY user_login ASC, user_nicename ASC", $q->query_orderby);
423
    }
424
425
    /**
426
     * @ticket 31265
427
     */
428
    public function test_orderby_flat_array() 
429
    {
430
        $q = new WP_User_Query(
431
            array(
432
            'orderby' => array( 'login', 'nicename' ),
433
            ) 
434
        );
435
436
        $this->assertContains("ORDER BY user_login ASC, user_nicename ASC", $q->query_orderby);
437
    }
438
439
    /**
440
     * @ticket 31265
441
     */
442 View Code Duplication
    public function test_orderby_array_contains_invalid_item() 
443
    {
444
        $q = new WP_User_Query(
445
            array(
446
            'orderby' => array( 'login', 'foo', 'nicename' ),
447
            ) 
448
        );
449
450
        $this->assertContains("ORDER BY user_login ASC, user_nicename ASC", $q->query_orderby);
451
    }
452
453
    /**
454
     * @ticket 31265
455
     */
456 View Code Duplication
    public function test_orderby_array_contains_all_invalid_items() 
457
    {
458
        $q = new WP_User_Query(
459
            array(
460
            'orderby' => array( 'foo', 'bar', 'baz' ),
461
            ) 
462
        );
463
464
        $this->assertContains("ORDER BY user_login", $q->query_orderby);
465
    }
466
467
    /**
468
     * @ticket 31265
469
     */
470 View Code Duplication
    public function test_orderby_array() 
471
    {
472
        $q = new WP_User_Query(
473
            array(
474
            'orderby' => array(
475
            'login' => 'DESC',
476
            'nicename' => 'ASC',
477
            'email' => 'DESC',
478
            ),
479
            ) 
480
        );
481
482
        $this->assertContains("ORDER BY user_login DESC, user_nicename ASC, user_email DESC", $q->query_orderby);
483
    }
484
485
    /**
486
     * @ticket 31265
487
     */
488 View Code Duplication
    public function test_orderby_array_should_discard_invalid_columns() 
489
    {
490
        $q = new WP_User_Query(
491
            array(
492
            'orderby' => array(
493
            'login' => 'DESC',
494
            'foo' => 'ASC',
495
            'email' => 'ASC',
496
            ),
497
            ) 
498
        );
499
500
        $this->assertContains("ORDER BY user_login DESC, user_email ASC", $q->query_orderby);
501
    }
502
503
    /**
504
     * @ticket 28631
505
     */
506
    function test_number() 
507
    {
508
        // +1 for the default user created by the test suite.
509
        $users = new WP_User_Query(array( 'blog_id' => get_current_blog_id() ));
510
        $users = $users->get_results();
511
        $this->assertEquals(13, count($users));
512
513
        $users = new WP_User_Query(array( 'blog_id' => get_current_blog_id(), 'number' => 10 ));
514
        $users = $users->get_results();
515
        $this->assertEquals(10, count($users));
516
517
        $users = new WP_User_Query(array( 'blog_id' => get_current_blog_id(), 'number' => 2 ));
518
        $users = $users->get_results();
519
        $this->assertEquals(2, count($users));
520
521
        $users = new WP_User_Query(array( 'blog_id' => get_current_blog_id(), 'number' => -1 ));
522
        $users = $users->get_results();
523
        $this->assertEquals(13, count($users));
524
    }
525
526
    /**
527
     * @ticket 21119
528
     */
529
    function test_prepare_query() 
530
    {
531
        $query = new WP_User_Query();
532
        $this->assertEmpty($query->query_fields);
533
        $this->assertEmpty($query->query_from);
534
        $this->assertEmpty($query->query_limit);
535
        $this->assertEmpty($query->query_orderby);
536
        $this->assertEmpty($query->query_where);
537
        $this->assertEmpty($query->query_vars);
538
        $_query_vars = $query->query_vars;
539
540
        $query->prepare_query();
541
        $this->assertNotEmpty($query->query_fields);
542
        $this->assertNotEmpty($query->query_from);
543
        $this->assertEmpty($query->query_limit);
544
        $this->assertNotEmpty($query->query_orderby);
545
        $this->assertNotEmpty($query->query_where);
546
        $this->assertNotEmpty($query->query_vars);
547
        $this->assertNotEquals($_query_vars, $query->query_vars);
548
549
        // All values get reset
550
        $query->prepare_query(array( 'number' => 8 ));
551
        $this->assertNotEmpty($query->query_limit);
552
        $this->assertEquals('LIMIT 0, 8', $query->query_limit);
553
554
        // All values get reset
555
        $query->prepare_query(array( 'fields' => 'all' ));
556
        $this->assertEmpty($query->query_limit);
557
        $this->assertEquals('', $query->query_limit);
558
        $_query_vars = $query->query_vars;
559
560
        $query->prepare_query();
561
        $this->assertEquals($_query_vars, $query->query_vars);
562
563
        $query->prepare_query(array( 'number' => -1 ));
564
        $this->assertNotEquals('LIMIT -1', $query->query_limit);
565
        $this->assertEmpty($query->query_limit);
566
    }
567
568
    public function test_meta_vars_should_be_converted_to_meta_query() 
569
    {
570
        $q = new WP_User_Query(
571
            array(
572
            'meta_key' => 'foo',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
573
            'meta_value' => '5',
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
574
            'meta_compare' => '>',
575
            'meta_type' => 'SIGNED',
576
            ) 
577
        );
578
579
        // Multisite adds a 'blog_id' clause, so we have to find the 'foo' clause.
580
        $mq_clauses = $q->meta_query->get_clauses();
581
        foreach ( $mq_clauses as $mq_clause ) {
582
            if ('foo' === $mq_clause['key'] ) {
583
                $clause = $mq_clause;
584
            }
585
        }
586
587
        $this->assertSame('foo', $clause['key']);
0 ignored issues
show
Bug introduced by
The variable $clause does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
588
        $this->assertSame('5', $clause['value']);
589
        $this->assertSame('>', $clause['compare']);
590
        $this->assertSame('SIGNED', $clause['type']);
591
    }
592
593
    /**
594
     * @ticket 23849
595
     */
596
    function test_meta_query_with_role() 
597
    {
598
        add_user_meta(self::$author_ids[0], 'foo', 'bar');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
599
        add_user_meta(self::$author_ids[1], 'foo', 'baz');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
600
601
        // Users with foo = bar or baz restricted to the author role.
602
        $query = new WP_User_Query(
603
            array(
604
            'fields' => '',
605
            'role' => 'author',
606
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
607
            'relation' => 'OR',
608
            array(
609
            'key' => 'foo',
610
            'value' => 'bar',
611
            ),
612
            array(
613
            'key' => 'foo',
614
            'value' => 'baz',
615
            ),
616
            ),
617
            ) 
618
        );
619
620
        $this->assertEquals(array( self::$author_ids[0], self::$author_ids[1] ), $query->get_results());
621
    }
622
623
    public function test_roles_and_caps_should_be_populated_for_default_value_of_blog_id() 
624
    {
625
        $query = new WP_User_Query(
626
            array(
627
            'include' => self::$author_ids[0],
628
            ) 
629
        );
630
631
        $found = $query->get_results();
632
633
        $this->assertNotEmpty($found);
634
        $user = reset($found);
635
        $this->assertSame(array( 'author' ), $user->roles);
636
        $this->assertSame(array( 'author' => true ), $user->caps);
637
    }
638
639 View Code Duplication
    public function test_roles_and_caps_should_be_populated_for_explicit_value_of_blog_id_on_nonms() 
640
    {
641
        if (is_multisite() ) {
642
            $this->markTestSkipped(__METHOD__ . ' is a non-multisite-only test.');
643
        }
644
645
        $query = new WP_User_Query(
646
            array(
647
            'include' => self::$author_ids[0],
648
            'blog_id' => get_current_blog_id(),
649
            ) 
650
        );
651
652
        $found = $query->get_results();
653
654
        $this->assertNotEmpty($found);
655
        $user = reset($found);
656
        $this->assertSame(array( 'author' ), $user->roles);
657
        $this->assertSame(array( 'author' => true ), $user->caps);
658
    }
659
660 View Code Duplication
    public function test_roles_and_caps_should_be_populated_for_explicit_value_of_current_blog_id_on_ms() 
661
    {
662
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
663
            $this->markTestSkipped(__METHOD__ . ' is a multisite-only test.');
664
        }
665
666
        $query = new WP_User_Query(
667
            array(
668
            'include' => self::$author_ids[0],
669
            'blog_id' => get_current_blog_id(),
670
            ) 
671
        );
672
673
        $found = $query->get_results();
674
675
        $this->assertNotEmpty($found);
676
        $user = reset($found);
677
        $this->assertSame(array( 'author' ), $user->roles);
678
        $this->assertSame(array( 'author' => true ), $user->caps);
679
    }
680
681 View Code Duplication
    public function test_roles_and_caps_should_be_populated_for_explicit_value_of_different_blog_id_on_ms_when_fields_all_with_meta() 
682
    {
683
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
684
            $this->markTestSkipped(__METHOD__ . ' is a multisite-only test.');
685
        }
686
687
        $b = self::factory()->blog->create();
688
689
        add_user_to_blog($b, self::$author_ids[0], 'author');
690
691
        $query = new WP_User_Query(
692
            array(
693
            'include' => self::$author_ids[0],
694
            'blog_id' => $b,
695
            'fields' => 'all_with_meta',
696
            ) 
697
        );
698
699
        $found = $query->get_results();
700
701
        $this->assertNotEmpty($found);
702
        $user = reset($found);
703
        $this->assertSame(array( 'author' ), $user->roles);
704
        $this->assertSame(array( 'author' => true ), $user->caps);
705
    }
706
707
    /**
708
     * @ticket 31878
709
     */
710 View Code Duplication
    public function test_roles_and_caps_should_be_populated_for_explicit_value_of_different_blog_id_on_ms_when_fields_all() 
711
    {
712
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
713
            $this->markTestSkipped(__METHOD__ . ' is a multisite-only test.');
714
        }
715
716
        $b = self::factory()->blog->create();
717
        add_user_to_blog($b, self::$author_ids[0], 'author');
718
719
        $query = new WP_User_Query(
720
            array(
721
            'fields' => 'all',
722
            'include' => self::$author_ids[0],
723
            'blog_id' => $b,
724
            ) 
725
        );
726
727
        $found = $query->get_results();
728
729
        $this->assertNotEmpty($found);
730
        $user = reset($found);
731
        $this->assertSame(array( 'author' ), $user->roles);
732
        $this->assertSame(array( 'author' => true ), $user->caps);
733
    }
734
735
    /**
736
     * @ticket 32019
737
     */
738
    public function test_who_authors() 
739
    {
740
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
741
            $this->markTestSkipped(__METHOD__ . ' requires multisite.');
742
        }
743
744
        $b = self::factory()->blog->create();
745
746
        add_user_to_blog($b, self::$author_ids[0], 'subscriber');
747
        add_user_to_blog($b, self::$author_ids[1], 'author');
748
        add_user_to_blog($b, self::$author_ids[2], 'editor');
749
750
        $q = new WP_User_Query(
751
            array(
752
            'who' => 'authors',
753
            'blog_id' => $b,
754
            ) 
755
        );
756
757
        $found = wp_list_pluck($q->get_results(), 'ID');
758
759
        $this->assertNotContains(self::$author_ids[0], $found);
760
        $this->assertContains(self::$author_ids[1], $found);
761
        $this->assertContains(self::$author_ids[2], $found);
762
    }
763
764
    /**
765
     * @ticket 32019
766
     */
767 View Code Duplication
    public function test_who_authors_should_work_alongside_meta_query() 
768
    {
769
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
770
            $this->markTestSkipped(__METHOD__ . ' requires multisite.');
771
        }
772
773
        $b = self::factory()->blog->create();
774
775
        add_user_to_blog($b, self::$author_ids[0], 'subscriber');
776
        add_user_to_blog($b, self::$author_ids[1], 'author');
777
        add_user_to_blog($b, self::$author_ids[2], 'editor');
778
779
        add_user_meta(self::$author_ids[1], 'foo', 'bar');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
780
        add_user_meta(self::$author_ids[2], 'foo', 'baz');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
781
782
        $q = new WP_User_Query(
783
            array(
784
            'who' => 'authors',
785
            'blog_id' => $b,
786
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
787
            array(
788
            'key' => 'foo',
789
            'value' => 'bar',
790
            )
791
            ),
792
            ) 
793
        );
794
795
        $found = wp_list_pluck($q->get_results(), 'ID');
796
797
        $this->assertNotContains(self::$author_ids[0], $found);
798
        $this->assertContains(self::$author_ids[1], $found);
799
        $this->assertNotContains(self::$author_ids[2], $found);
800
    }
801
802
    /**
803
     * @ticket 36724
804
     */
805 View Code Duplication
    public function test_who_authors_should_work_alongside_meta_params() 
806
    {
807
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
808
            $this->markTestSkipped(__METHOD__ . ' requires multisite.');
809
        }
810
811
        $b = self::factory()->blog->create();
812
813
        add_user_to_blog($b, self::$author_ids[0], 'subscriber');
814
        add_user_to_blog($b, self::$author_ids[1], 'author');
815
        add_user_to_blog($b, self::$author_ids[2], 'editor');
816
817
        add_user_meta(self::$author_ids[1], 'foo', 'bar');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
818
        add_user_meta(self::$author_ids[2], 'foo', 'baz');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
819
820
        $q = new WP_User_Query(
821
            array(
822
            'who' => 'authors',
823
            'blog_id' => $b,
824
            'meta_key' => 'foo',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
825
            'meta_value' => 'bar',
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
826
            ) 
827
        );
828
829
        $found = wp_list_pluck($q->get_results(), 'ID');
830
831
        $this->assertNotContains(self::$author_ids[0], $found);
832
        $this->assertContains(self::$author_ids[1], $found);
833
        $this->assertNotContains(self::$author_ids[2], $found);
834
    }
835
836
    /**
837
     * @ticket 32250
838
     */
839
    public function test_has_published_posts_with_value_true_should_show_authors_of_posts_in_public_post_types() 
840
    {
841
        register_post_type('wptests_pt_public', array( 'public' => true ));
842
        register_post_type('wptests_pt_private', array( 'public' => false ));
843
844
        self::factory()->post->create(array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ));
845
        self::factory()->post->create(array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ));
846
847
        $q = new WP_User_Query(
848
            array(
849
            'has_published_posts' => true,
850
            ) 
851
        );
852
853
        $found = wp_list_pluck($q->get_results(), 'ID');
854
        $expected = array( self::$author_ids[0] );
855
856
        $this->assertEqualSets($expected, $found);
857
    }
858
859
    /**
860
     * @ticket 32250
861
     */
862 View Code Duplication
    public function test_has_published_posts_should_obey_post_types() 
863
    {
864
        register_post_type('wptests_pt_public', array( 'public' => true ));
865
        register_post_type('wptests_pt_private', array( 'public' => false ));
866
867
        self::factory()->post->create(array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ));
868
        self::factory()->post->create(array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ));
869
        self::factory()->post->create(array( 'post_author' => self::$author_ids[2], 'post_status' => 'publish', 'post_type' => 'post' ));
870
871
        $q = new WP_User_Query(
872
            array(
873
            'has_published_posts' => array( 'wptests_pt_private', 'post' ),
874
            ) 
875
        );
876
877
        $found = wp_list_pluck($q->get_results(), 'ID');
878
        $expected = array( self::$author_ids[1], self::$author_ids[2] );
879
880
        $this->assertEqualSets($expected, $found);
881
    }
882
883
    /**
884
     * @ticket 32250
885
     */
886 View Code Duplication
    public function test_has_published_posts_should_ignore_non_published_posts() 
887
    {
888
        register_post_type('wptests_pt_public', array( 'public' => true ));
889
        register_post_type('wptests_pt_private', array( 'public' => false ));
890
891
        self::factory()->post->create(array( 'post_author' => self::$author_ids[0], 'post_status' => 'draft', 'post_type' => 'wptests_pt_public' ));
892
        self::factory()->post->create(array( 'post_author' => self::$author_ids[1], 'post_status' => 'inherit', 'post_type' => 'wptests_pt_private' ));
893
        self::factory()->post->create(array( 'post_author' => self::$author_ids[2], 'post_status' => 'publish', 'post_type' => 'post' ));
894
895
        $q = new WP_User_Query(
896
            array(
897
            'has_published_posts' => array( 'wptests_pt_public', 'wptests_pt_private', 'post' ),
898
            ) 
899
        );
900
901
        $found = wp_list_pluck($q->get_results(), 'ID');
902
        $expected = array( self::$author_ids[2] );
903
904
        $this->assertEqualSets($expected, $found);
905
    }
906
907
    /**
908
     * @ticket 32250
909
     */
910
    public function test_has_published_posts_should_respect_blog_id() 
911
    {
912
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
913
            $this->markTestSkipped(__METHOD__ . ' requires multisite.');
914
        }
915
916
        $blogs = self::factory()->blog->create_many(2);
917
918
        add_user_to_blog($blogs[0], self::$author_ids[0], 'author');
919
        add_user_to_blog($blogs[0], self::$author_ids[1], 'author');
920
        add_user_to_blog($blogs[1], self::$author_ids[0], 'author');
921
        add_user_to_blog($blogs[1], self::$author_ids[1], 'author');
922
923
        switch_to_blog($blogs[0]);
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...
924
        self::factory()->post->create(array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'post' ));
925
        restore_current_blog();
926
927
        switch_to_blog($blogs[1]);
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...
928
        self::factory()->post->create(array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'post' ));
929
        restore_current_blog();
930
931
        $q = new WP_User_Query(
932
            array(
933
            'has_published_posts' => array( 'post' ),
934
            'blog_id' => $blogs[1],
935
            ) 
936
        );
937
938
        $found = wp_list_pluck($q->get_results(), 'ID');
939
        $expected = array( self::$author_ids[1] );
940
941
        $this->assertEqualSets($expected, $found);
942
    }
943
944
    /**
945
     * @ticket 32592
946
     */
947
    public function test_top_level_or_meta_query_should_eliminate_duplicate_matches() 
948
    {
949
        add_user_meta(self::$author_ids[0], 'foo', 'bar');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
950
        add_user_meta(self::$author_ids[1], 'foo', 'bar');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
951
        add_user_meta(self::$author_ids[0], 'foo2', 'bar2');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
952
953
        $q = new WP_User_Query(
954
            array(
955
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
956
            'relation' => 'OR',
957
            array(
958
            'key' => 'foo',
959
            'value' => 'bar',
960
            ),
961
            array(
962
            'key' => 'foo2',
963
            'value' => 'bar2',
964
            ),
965
            ),
966
            ) 
967
        );
968
969
        $found = wp_list_pluck($q->get_results(), 'ID');
970
        $expected = array( self::$author_ids[0], self::$author_ids[1] );
971
972
        $this->assertEqualSets($expected, $found);
973
    }
974
975
    /**
976
     * @ticket 32592
977
     */
978
    public function test_nested_or_meta_query_should_eliminate_duplicate_matches() 
979
    {
980
        add_user_meta(self::$author_ids[0], 'foo', 'bar');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
981
        add_user_meta(self::$author_ids[1], 'foo', 'bar');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
982
        add_user_meta(self::$author_ids[0], 'foo2', 'bar2');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
983
        add_user_meta(self::$author_ids[1], 'foo3', 'bar3');
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
984
985
        $q = new WP_User_Query(
986
            array(
987
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
988
            'relation' => 'AND',
989
            array(
990
            'key' => 'foo',
991
            'value' => 'bar',
992
            ),
993
            array(
994
            'relation' => 'OR',
995
            array(
996
            'key' => 'foo',
997
            'value' => 'bar',
998
            ),
999
            array(
1000
            'key' => 'foo2',
1001
            'value' => 'bar2',
1002
            ),
1003
            ),
1004
            ),
1005
            ) 
1006
        );
1007
1008
        $found = wp_list_pluck($q->get_results(), 'ID');
1009
        $expected = array( self::$author_ids[0], self::$author_ids[1] );
1010
1011
        $this->assertEqualSets($expected, $found);
1012
    }
1013
1014
    /**
1015
     * @ticket 36624
1016
     */
1017
    public function test_nicename_returns_user_with_nicename() 
1018
    {
1019
        wp_update_user(
1020
            array(
1021
            'ID' => self::$author_ids[0],
1022
            'user_nicename' => 'peter'
1023
            ) 
1024
        );
1025
1026
        $q = new WP_User_Query(
1027
            array (
0 ignored issues
show
introduced by
There must be no space between the Array keyword and the opening parenthesis
Loading history...
1028
            'nicename' => 'peter'
1029
            ) 
1030
        );
1031
1032
        $found = wp_list_pluck($q->get_results(), 'ID');
1033
        $expected = array( self::$author_ids[0] );
1034
1035
        $this->assertContains("AND user_nicename = 'peter'", $q->query_where);
1036
        $this->assertEqualSets($expected, $found);
1037
    }
1038
1039
    /**
1040
     * @ticket 36624
1041
     */
1042 View Code Duplication
    public function test_nicename__in_returns_users_with_included_nicenames() 
1043
    {
1044
        wp_update_user(
1045
            array(
1046
            'ID' => self::$author_ids[0],
1047
            'user_nicename' => 'peter'
1048
            ) 
1049
        );
1050
1051
        wp_update_user(
1052
            array(
1053
            'ID' => self::$author_ids[1],
1054
            'user_nicename' => 'paul'
1055
            ) 
1056
        );
1057
1058
        wp_update_user(
1059
            array(
1060
            'ID' => self::$author_ids[2],
1061
            'user_nicename' => 'mary'
1062
            ) 
1063
        );
1064
1065
        $q = new WP_User_Query(
1066
            array (
0 ignored issues
show
introduced by
There must be no space between the Array keyword and the opening parenthesis
Loading history...
1067
            'nicename__in' => array( 'peter', 'paul', 'mary' )
1068
            ) 
1069
        );
1070
1071
        $found = wp_list_pluck($q->get_results(), 'ID');
1072
        $expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
1073
1074
        $this->assertContains("AND user_nicename IN ( 'peter','paul','mary' )", $q->query_where);
1075
        $this->assertEqualSets($expected, $found);
1076
    }
1077
1078
    /**
1079
     * @ticket 36624
1080
     */
1081
    public function test_nicename__not_in_returns_users_without_included_nicenames() 
1082
    {
1083
        wp_update_user(
1084
            array(
1085
            'ID' => self::$author_ids[0],
1086
            'user_nicename' => 'peter'
1087
            ) 
1088
        );
1089
1090
        wp_update_user(
1091
            array(
1092
            'ID' => self::$author_ids[1],
1093
            'user_nicename' => 'paul'
1094
            ) 
1095
        );
1096
1097
        wp_update_user(
1098
            array(
1099
            'ID' => self::$author_ids[2],
1100
            'user_nicename' => 'mary'
1101
            ) 
1102
        );
1103
1104
        $q = new WP_User_Query(
1105
            array (
0 ignored issues
show
introduced by
There must be no space between the Array keyword and the opening parenthesis
Loading history...
1106
            'nicename__not_in' => array( 'peter', 'paul', 'mary' )
1107
            ) 
1108
        );
1109
1110
        $foundCount = count($q->get_results());
1111
        $expectedCount = 10; // 13 total users minus 3 from query
1112
1113
        $this->assertContains("AND user_nicename NOT IN ( 'peter','paul','mary' )", $q->query_where);
1114
        $this->assertEquals($expectedCount, $foundCount);
1115
    }
1116
1117
    /**
1118
     * @ticket 36624
1119
     */
1120 View Code Duplication
    public function test_orderby_nicename__in() 
1121
    {
1122
        wp_update_user(
1123
            array(
1124
            'ID' => self::$author_ids[0],
1125
            'user_nicename' => 'peter'
1126
            ) 
1127
        );
1128
1129
        wp_update_user(
1130
            array(
1131
            'ID' => self::$author_ids[1],
1132
            'user_nicename' => 'paul'
1133
            ) 
1134
        );
1135
1136
        wp_update_user(
1137
            array(
1138
            'ID' => self::$author_ids[2],
1139
            'user_nicename' => 'mary'
1140
            ) 
1141
        );
1142
1143
        $q = new WP_User_Query(
1144
            array (
0 ignored issues
show
introduced by
There must be no space between the Array keyword and the opening parenthesis
Loading history...
1145
            'nicename__in' => array( 'mary', 'peter', 'paul' ),
1146
            'orderby' => 'nicename__in'
1147
            ) 
1148
        );
1149
1150
        $found = wp_list_pluck($q->get_results(), 'ID');
1151
        $expected = array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] );
1152
1153
        $this->assertContains("FIELD( user_nicename, 'mary','peter','paul' )", $q->query_orderby);
1154
        $this->assertSame($expected, $found);
1155
    }
1156
1157
    /**
1158
     * @ticket 36624
1159
     */
1160
    public function test_login_returns_user_with_login() 
1161
    {
1162
1163
        $user_login = get_userdata(self::$author_ids[0])->user_login;
1164
1165
        $q = new WP_User_Query(
1166
            array (
0 ignored issues
show
introduced by
There must be no space between the Array keyword and the opening parenthesis
Loading history...
1167
            'login' => $user_login
1168
            ) 
1169
        );
1170
1171
        $found = wp_list_pluck($q->get_results(), 'ID');
1172
        $expected = array( self::$author_ids[0] );
1173
1174
        $this->assertContains("AND user_login = '$user_login'", $q->query_where);
1175
        $this->assertEqualSets($expected, $found);
1176
    }
1177
1178
    /**
1179
     * @ticket 36624
1180
     */
1181 View Code Duplication
    public function test_login__in_returns_users_with_included_logins() 
1182
    {
1183
        $user_login1 = get_userdata(self::$author_ids[0])->user_login;
1184
        $user_login2 = get_userdata(self::$author_ids[1])->user_login;
1185
        $user_login3 = get_userdata(self::$author_ids[2])->user_login;
1186
1187
        $q = new WP_User_Query(
1188
            array (
0 ignored issues
show
introduced by
There must be no space between the Array keyword and the opening parenthesis
Loading history...
1189
            'login__in' => array( $user_login1, $user_login2, $user_login3 )
1190
            ) 
1191
        );
1192
1193
        $found = wp_list_pluck($q->get_results(), 'ID');
1194
        $expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
1195
1196
        $this->assertContains("AND user_login IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where);
1197
        $this->assertEqualSets($expected, $found);
1198
    }
1199
1200
    /**
1201
     * @ticket 36624
1202
     */
1203
    public function test_login__not_in_returns_users_without_included_logins() 
1204
    {
1205
        $user_login1 = get_userdata(self::$author_ids[0])->user_login;
1206
        $user_login2 = get_userdata(self::$author_ids[1])->user_login;
1207
        $user_login3 = get_userdata(self::$author_ids[2])->user_login;
1208
1209
        $q = new WP_User_Query(
1210
            array (
0 ignored issues
show
introduced by
There must be no space between the Array keyword and the opening parenthesis
Loading history...
1211
            'login__not_in' => array( $user_login1, $user_login2, $user_login3 )
1212
            ) 
1213
        );
1214
1215
        $foundCount = count($q->get_results());
1216
        $expectedCount = 10; // 13 total users minus 3 from query
1217
1218
        $this->assertContains("AND user_login NOT IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where);
1219
        $this->assertEquals($expectedCount, $foundCount);
1220
    }
1221
1222
    /**
1223
     * @ticket 36624
1224
     */
1225 View Code Duplication
    public function test_orderby_login__in() 
1226
    {
1227
        $user_login1 = get_userdata(self::$author_ids[0])->user_login;
1228
        $user_login2 = get_userdata(self::$author_ids[1])->user_login;
1229
        $user_login3 = get_userdata(self::$author_ids[2])->user_login;
1230
1231
        $q = new WP_User_Query(
1232
            array (
0 ignored issues
show
introduced by
There must be no space between the Array keyword and the opening parenthesis
Loading history...
1233
            'login__in' => array( $user_login2, $user_login3, $user_login1 ),
1234
            'orderby' => 'login__in'
1235
            ) 
1236
        );
1237
1238
        $found = wp_list_pluck($q->get_results(), 'ID');
1239
        $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
1240
1241
        $this->assertContains("FIELD( user_login, '$user_login2','$user_login3','$user_login1' )", $q->query_orderby);
1242
        $this->assertSame($expected, $found);
1243
    }
1244
1245
    /**
1246
     * @ticket 25145
1247
     */
1248
    public function test_paged() 
1249
    {
1250
        $q = new WP_User_Query(
1251
            array(
1252
            'number' => 2,
1253
            'paged' => 2,
1254
            'orderby' => 'ID',
1255
            'order' => 'DESC', // Avoid funkiness with user 1.
1256
            'fields' => 'ids',
1257
            ) 
1258
        );
1259
1260
        $this->assertEquals(array( self::$contrib_id, self::$editor_ids[2] ), $q->results);
1261
    }
1262
1263
    /**
1264
     * @ticket 33449
1265
     */
1266
    public function test_query_vars_should_be_filled_in_after_pre_get_users() 
1267
    {
1268
        $query_vars = array( 'blog_id', 'role', 'meta_key', 'meta_value', 'meta_compare', 'include', 'exclude', 'search', 'search_columns', 'orderby', 'order', 'offset', 'number', 'paged', 'count_total', 'fields', 'who', 'has_published_posts' );
1269
1270
        add_action('pre_get_users', array( $this, 'filter_pre_get_users_args' ));
1271
        $q = new WP_User_Query(array_fill_keys($query_vars, '1'));
1272
        remove_action('pre_get_users', array( $this, 'filter_pre_get_users_args' ));
1273
1274
        foreach ( $query_vars as $query_var ) {
1275
            $this->assertTrue(array_key_exists($query_var, $q->query_vars), "$query_var does not exist.");
1276
        }
1277
1278
    }
1279
1280
    public function filter_pre_get_users_args( $q ) 
1281
    {
1282
        foreach ( $q->query_vars as $k => $v ) {
1283
            unset($q->query_vars[ $k ]);
1284
        }
1285
    }
1286
1287
    /**
1288
     * @ticket 22212
1289
     */
1290
    public function test_get_single_role_by_user_query() 
1291
    {
1292
        $wp_user_search = new WP_User_Query(array( 'role' => 'subscriber' ));
1293
        $users          = $wp_user_search->get_results();
1294
1295
        $this->assertEquals(2, count($users));
1296
    }
1297
1298
    /**
1299
     * @ticket 22212
1300
     */
1301
    public function test_get_multiple_roles_by_user_query() 
1302
    {
1303
        $wp_user_search = new WP_User_Query(array( 'role__in' => array( 'subscriber', 'editor' ) ));
1304
        $users          = $wp_user_search->get_results();
1305
        $this->assertEquals(5, count($users));
1306
    }
1307
1308
    /**
1309
     * @ticket 22212
1310
     */
1311
    public function test_get_single_role_by_string() 
1312
    {
1313
        $users = get_users(
1314
            array(
1315
            'role' => 'subscriber',
1316
            ) 
1317
        );
1318
1319
        $this->assertEquals(2, count($users));
1320
    }
1321
1322
    /**
1323
     * @ticket 22212
1324
     */
1325
    public function test_get_single_role_by_string_which_is_similar() 
1326
    {
1327
        $another_editor = self::factory()->user->create(
1328
            array(
1329
            'user_email' => 'another_editor@another_editor.com',
1330
            'user_login' => 'another_editor',
1331
            'role' => 'another-editor',
1332
            ) 
1333
        );
1334
1335
        $users = get_users(
1336
            array(
1337
            'role' => 'editor',
1338
            'fields' => 'ids',
1339
            ) 
1340
        );
1341
1342
        $this->assertEqualSets(self::$editor_ids, $users);
1343
    }
1344
1345
1346
    /**
1347
     * @ticket 22212
1348
     */
1349
    public function test_get_single_role_by_array() 
1350
    {
1351
        $users = get_users(
1352
            array(
1353
            'role' => array( 'subscriber' ),
1354
            ) 
1355
        );
1356
1357
        $this->assertEquals(2, count($users));
1358
    }
1359
1360
    /**
1361
     * @ticket 22212
1362
     */
1363
    public function test_get_multiple_roles_should_only_match_users_who_have_each_role() 
1364
    {
1365
        $users = new WP_User_Query(array( 'role' => array( 'subscriber', 'editor' ) ));
1366
        $users = $users->get_results();
1367
1368
        $this->assertEmpty($users);
1369
1370
        foreach ( self::$sub_ids as $subscriber ) {
1371
            $subscriber = get_user_by('ID', $subscriber);
1372
            $subscriber->add_role('editor');
1373
        }
1374
1375
        $users = new WP_User_Query(array( 'role' => array( 'subscriber', 'editor' ) ));
1376
        $users = $users->get_results();
1377
1378
        $this->assertEquals(2, count($users));
1379
1380
        foreach ( $users as $user ) {
1381
            $this->assertInstanceOf('WP_User', $user);
1382
        }
1383
    }
1384
1385
    /**
1386
     * @ticket 22212
1387
     */
1388
    public function test_get_multiple_roles_or() 
1389
    {
1390
        $users = new WP_User_Query(array( 'role__in' => array( 'subscriber', 'editor', 'administrator' ) ));
1391
        $users = $users->get_results();
1392
1393
        // +1 for the default user created during installation.
1394
        $this->assertEquals(8, count($users));
1395
        foreach ( $users as $user ) {
1396
            $this->assertInstanceOf('WP_User', $user);
1397
        }
1398
    }
1399
1400
    /**
1401
     * @ticket 22212
1402
     */
1403
    public function test_get_multiple_roles_by_comma_separated_list() 
1404
    {
1405
        $users = get_users(
1406
            array(
1407
            'role' => 'subscriber, editor',
1408
            ) 
1409
        );
1410
1411
        $this->assertEmpty($users);
1412
1413
        foreach ( self::$sub_ids as $subscriber ) {
1414
            $subscriber = get_user_by('ID', $subscriber);
1415
            $subscriber->add_role('editor');
1416
        }
1417
1418
        $users = get_users(
1419
            array(
1420
            'role' => 'subscriber, editor',
1421
            ) 
1422
        );
1423
1424
        $this->assertEquals(2, count($users));
1425
    }
1426
1427
    /**
1428
     * @ticket 22212
1429
     */
1430
    public function test_get_multiple_roles_with_meta() 
1431
    {
1432
        // Create administrator user + meta
1433
        update_user_meta(self::$admin_ids[0], 'mk1', 1);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1434
        update_user_meta(self::$admin_ids[0], 'mk2', 1);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1435
1436
        // Create editor user + meta
1437
        update_user_meta(self::$editor_ids[0], 'mk1', 1);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1438
        update_user_meta(self::$editor_ids[0], 'mk2', 2);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1439
1440
        // Create subscriber user + meta
1441
        update_user_meta(self::$sub_ids[0], 'mk1', 1);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1442
        update_user_meta(self::$sub_ids[0], 'mk2', 1);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1443
1444
        // Create contributor user + meta
1445
        update_user_meta(self::$contrib_id, 'mk1', 1);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1446
        update_user_meta(self::$contrib_id, 'mk2', 2);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
1447
1448
        // Fetch users
1449
        $users = get_users(
1450
            array(
1451
            'role__in'   => array( 'administrator', 'editor', 'subscriber' ),
1452
            'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
1453
            'relation' => 'AND',
1454
            array(
1455
            'key'     => 'mk1',
1456
            'value'   => '1',
1457
            'compare' => "=",
1458
            'type'    => 'numeric',
1459
            ),
1460
            array(
1461
            'key'     => 'mk2',
1462
            'value'   => '2',
1463
            'compare' => "=",
1464
            'type'    => 'numeric',
1465
            ),
1466
            ),
1467
            ) 
1468
        );
1469
1470
        // Check results
1471
        $this->assertEquals(1, count($users));
1472
        $this->assertSame(self::$editor_ids[0], (int) $users[0]->ID);
1473
    }
1474
1475
    /**
1476
     * @ticket 22212
1477
     */
1478
    public function test_role_exclusion() 
1479
    {
1480
        $users = get_users(
1481
            array(
1482
            'role__not_in' => 'subscriber',
1483
            ) 
1484
        );
1485
1486
        // +1 for the default user created during installation.
1487
        $this->assertEquals(11, count($users));
1488
1489
        $users = get_users(
1490
            array(
1491
            'role__not_in' => 'editor',
1492
            ) 
1493
        );
1494
1495
        // +1 for the default user created during installation.
1496
        $this->assertEquals(10, count($users));
1497
    }
1498
1499
    /**
1500
     * @ticket 22212
1501
     */
1502 View Code Duplication
    public function test_role__in_role__not_in_combined() 
1503
    {
1504
        foreach ( self::$sub_ids as $subscriber ) {
1505
            $subscriber = get_user_by('ID', $subscriber);
1506
            $subscriber->add_role('editor');
1507
        }
1508
1509
        $users = get_users(
1510
            array(
1511
            'role__in'     => 'editor',
1512
            ) 
1513
        );
1514
1515
        $this->assertEquals(5, count($users));
1516
1517
        $users = get_users(
1518
            array(
1519
            'role__in'     => 'editor',
1520
            'role__not_in' => 'subscriber',
1521
            ) 
1522
        );
1523
1524
        $this->assertEquals(3, count($users));
1525
    }
1526
1527
    /**
1528
     * @ticket 22212
1529
     */
1530
    public function test_role__not_in_role_combined() 
1531
    {
1532
        $subscriber = get_user_by('ID', self::$sub_ids[0]);
1533
        $subscriber->add_role('editor');
1534
1535
        $users = get_users(
1536
            array(
1537
            'role'         => 'subscriber',
1538
            'role__not_in' => array( 'editor' ),
1539
            ) 
1540
        );
1541
1542
        $this->assertEquals(1, count($users));
1543
    }
1544
1545
    /**
1546
     * @ticket 22212
1547
     */
1548 View Code Duplication
    public function test_role__not_in_user_without_role() 
1549
    {
1550
        $user_without_rule = get_user_by('ID', self::$sub_ids[0]);
1551
1552
        $user_without_rule->remove_role('subscriber');
1553
1554
        $users = get_users(
1555
            array(
1556
            'role__not_in' => 'subscriber',
1557
            ) 
1558
        );
1559
1560
        // +1 for the default user created during installation.
1561
        $this->assertEquals(12, count($users));
1562
1563
        $users = get_users(
1564
            array(
1565
            'role__not_in' => 'editor',
1566
            ) 
1567
        );
1568
1569
        // +1 for the default user created during installation.
1570
        $this->assertEquals(10, count($users));
1571
    }
1572
1573
    /**
1574
     * @ticket 22212
1575
     */
1576
    public function test_blog_id_should_restrict_by_blog_without_requiring_a_named_role() 
1577
    {
1578
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1579
            $this->markTestSkipped(__METHOD__ . ' requires multisite.');
1580
        }
1581
1582
        $sites = self::factory()->blog->create_many(2);
1583
1584
        add_user_to_blog($sites[0], self::$author_ids[0], 'author');
1585
        add_user_to_blog($sites[1], self::$author_ids[1], 'author');
1586
1587
        $found = get_users(
1588
            array(
1589
            'blog_id' => $sites[1],
1590
            'fields' => 'ID',
1591
            ) 
1592
        );
1593
1594
        $this->assertEqualSets(array( self::$author_ids[1] ), $found);
1595
    }
1596
1597
    /**
1598
     * @ticket 22212
1599
     * @ticket 21119
1600
     */
1601
    public function test_calling_prepare_query_a_second_time_should_not_add_another_cap_query_on_multisite() 
1602
    {
1603
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1604
            $this->markTestSkipped(__METHOD__ . ' requires Multisite.');
1605
        }
1606
1607
        $site_id = get_current_blog_id();
1608
        add_user_to_blog($site_id, self::$author_ids[0], 'author');
1609
1610
        $q = new WP_User_Query(
1611
            array(
1612
            'include' => self::$author_ids[0],
1613
            ) 
1614
        );
1615
1616
        $r1 = $q->request;
1617
1618
        $q->prepare_query(
1619
            array(
1620
            'include' => self::$author_ids[0],
1621
            ) 
1622
        );
1623
1624
        $r2 = $q->request;
1625
1626
        $q->prepare_query(
1627
            array(
1628
            'include' => self::$author_ids[0],
1629
            ) 
1630
        );
1631
1632
        $r3 = $q->request;
1633
1634
        $this->assertSame($r1, $r2);
1635
        $this->assertSame($r1, $r3);
1636
    }
1637
}
1638