Completed
Pull Request — master (#2)
by Stephen
13:19
created

Tests_User_Query::test_include_array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
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
	protected static $author_ids;
9
	protected static $sub_ids;
10
	protected static $editor_ids;
11
	protected static $contrib_id;
12
	protected static $admin_ids;
13
14
	protected $user_id;
15
16
	public static function wpSetUpBeforeClass( $factory ) {
17
		self::$author_ids = $factory->user->create_many( 4, array(
18
			'role' => 'author'
19
		) );
20
21
		self::$sub_ids = $factory->user->create_many( 2, array(
22
			'role' => 'subscriber',
23
		) );
24
25
		self::$editor_ids = $factory->user->create_many( 3, array(
26
			'role' => 'editor',
27
		) );
28
29
		self::$contrib_id = $factory->user->create( array(
30
			'role' => 'contributor',
31
		) );
32
33
		self::$admin_ids = $factory->user->create_many( 2, array(
34
			'role' => 'administrator',
35
		) );
36
	}
37
38
	function test_get_and_set() {
39
		$users = new WP_User_Query();
40
41
		$this->assertEquals( '', $users->get( 'fields' ) );
42
		$this->assertEquals( '', @$users->query_vars['fields'] );
43
44
		$users->set( 'fields', 'all' );
45
46
		$this->assertEquals( 'all', $users->get( 'fields' ) );
47
		$this->assertEquals( 'all', $users->query_vars['fields'] );
48
49
		$users->set( 'fields', '' );
50
		$this->assertEquals( '', $users->get( 'fields' ) );
51
		$this->assertEquals( '', $users->query_vars['fields'] );
52
53
		$this->assertNull( $users->get( 'does-not-exist' ) );
54
	}
55
56
	public function test_include_single() {
57
		$q = new WP_User_Query( array(
58
			'fields' => '',
59
			'include' => self::$author_ids[0],
60
		) );
61
		$ids = $q->get_results();
62
63
		$this->assertEquals( array( self::$author_ids[0] ), $ids );
64
	}
65
66
	public function test_include_comma_separated() {
67
		$q = new WP_User_Query( array(
68
			'fields' => '',
69
			'include' => self::$author_ids[0] . ', ' . self::$author_ids[2],
70
		) );
71
		$ids = $q->get_results();
72
73
		$this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids );
74
	}
75
76
	public function test_include_array() {
77
		$q = new WP_User_Query( array(
78
			'fields' => '',
79
			'include' => array( self::$author_ids[0], self::$author_ids[2] ),
80
		) );
81
		$ids = $q->get_results();
82
83
		$this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids );
84
	}
85
86
	public function test_include_array_bad_values() {
87
		$q = new WP_User_Query( array(
88
			'fields' => '',
89
			'include' => array( self::$author_ids[0], 'foo', self::$author_ids[2] ),
90
		) );
91
		$ids = $q->get_results();
92
93
		$this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids );
94
	}
95
96
	public function test_exclude() {
97
		$q = new WP_User_Query( array(
98
			'fields' => '',
99
			'exclude' => self::$author_ids[1],
100
		) );
101
102
		$ids = $q->get_results();
103
104
		// Indirect test in order to ignore default user created during installation.
105
		$this->assertNotEmpty( $ids );
106
		$this->assertNotContains( self::$author_ids[1], $ids );
107
	}
108
109
	public function test_get_all() {
110
		$users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) );
111
		$users = $users->get_results();
112
113
		// +1 for the default user created during installation.
114
		$this->assertEquals( 13, count( $users ) );
115
		foreach ( $users as $user ) {
116
			$this->assertInstanceOf( 'WP_User', $user );
117
		}
118
119
		$users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'fields' => 'all_with_meta' ) );
120
		$users = $users->get_results();
121
		$this->assertEquals( 13, count( $users ) );
122
		foreach ( $users as $user ) {
123
			$this->assertInstanceOf( 'WP_User', $user );
124
		}
125
	}
126
127
	/**
128
	 * @ticket 39297
129
	 */
130
	public function test_get_total_is_int() {
131
		$users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) );
132
		$total_users = $users->get_total();
133
134
		$this->assertSame( 13, $total_users );
135
	}
136
137
	/**
138
	 * @dataProvider orderby_should_convert_non_prefixed_keys_data
139
	 */
140
	public function test_orderby_should_convert_non_prefixed_keys( $short_key, $full_key ) {
141
		$q = new WP_User_Query( array(
142
			'orderby' => $short_key,
143
		) );
144
145
		$this->assertContains( "ORDER BY $full_key", $q->query_orderby );
146
	}
147
148
	public function orderby_should_convert_non_prefixed_keys_data() {
149
		return array(
150
			array( 'nicename', 'user_nicename' ),
151
			array( 'email', 'user_email' ),
152
			array( 'url', 'user_url' ),
153
			array( 'registered', 'user_registered' ),
154
			array( 'name', 'display_name' ),
155
		);
156
	}
157
158
	public function test_orderby_meta_value() {
159
		update_user_meta( self::$author_ids[0], 'last_name', 'Jones' );
160
		update_user_meta( self::$author_ids[1], 'last_name', 'Albert' );
161
		update_user_meta( self::$author_ids[2], 'last_name', 'Zorro' );
162
163
		$q = new WP_User_Query( array(
164
			'include' => self::$author_ids,
165
			'meta_key' => 'last_name',
166
			'orderby' => 'meta_value',
167
			'fields' => 'ids'
168
		) );
169
170
		$expected = array( self::$author_ids[3], self::$author_ids[1], self::$author_ids[0], self::$author_ids[2] );
171
172
		$this->assertEquals( $expected, $q->get_results() );
173
	}
174
175
	/**
176
	 * @ticket 27887
177
	 */
178
	public function test_orderby_meta_value_num() {
179
		update_user_meta( self::$author_ids[0], 'user_age', '101' );
180
		update_user_meta( self::$author_ids[1], 'user_age', '20' );
181
		update_user_meta( self::$author_ids[2], 'user_age', '25' );
182
183
		$q = new WP_User_Query( array(
184
			'include' => self::$author_ids,
185
			'meta_key' => 'user_age',
186
			'orderby' => 'meta_value_num',
187
			'fields' => 'ids'
188
		) );
189
190
		$expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
191
192
		$this->assertEquals( $expected, $q->get_results() );
193
	}
194
195
	/**
196
	 * @ticket 31265
197
	 */
198
	public function test_orderby_somekey_where_meta_key_is_somekey() {
199
		update_user_meta( self::$author_ids[0], 'foo', 'zzz' );
200
		update_user_meta( self::$author_ids[1], 'foo', 'aaa' );
201
		update_user_meta( self::$author_ids[2], 'foo', 'jjj' );
202
203
		$q = new WP_User_Query( array(
204
			'include' => self::$author_ids,
205
			'meta_key' => 'foo',
206
			'orderby' => 'foo',
207
			'fields' => 'ids'
208
		) );
209
210
		$expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
211
212
		$this->assertEquals( $expected, $q->get_results() );
213
	}
214
215
	/**
216
	 * @ticket 31265
217
	 */
218
	public function test_orderby_clause_key() {
219
		add_user_meta( self::$author_ids[0], 'foo', 'aaa' );
220
		add_user_meta( self::$author_ids[1], 'foo', 'zzz' );
221
		add_user_meta( self::$author_ids[2], 'foo', 'jjj' );
222
223
		$q = new WP_User_Query( array(
224
			'fields' => 'ids',
225
			'meta_query' => array(
226
				'foo_key' => array(
227
					'key' => 'foo',
228
					'compare' => 'EXISTS',
229
				),
230
			),
231
			'orderby' => 'foo_key',
232
			'order' => 'DESC',
233
		) );
234
235
		$this->assertEquals( array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ), $q->results );
0 ignored issues
show
Documentation introduced by
The property $results is declared private in WP_User_Query. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
236
	}
237
238
	/**
239
	 * @ticket 31265
240
	 */
241
	public function test_orderby_clause_key_as_secondary_sort() {
242
		$u1 = self::factory()->user->create( array(
243
			'user_registered' => '2015-01-28 03:00:00',
244
		) );
245
		$u2 = self::factory()->user->create( array(
246
			'user_registered' => '2015-01-28 05:00:00',
247
		) );
248
		$u3 = self::factory()->user->create( array(
249
			'user_registered' => '2015-01-28 03:00:00',
250
		) );
251
252
		add_user_meta( $u1, 'foo', 'jjj' );
253
		add_user_meta( $u2, 'foo', 'zzz' );
254
		add_user_meta( $u3, 'foo', 'aaa' );
255
256
		$q = new WP_User_Query( array(
257
			'fields' => 'ids',
258
			'meta_query' => array(
259
				'foo_key' => array(
260
					'key' => 'foo',
261
					'compare' => 'EXISTS',
262
				),
263
			),
264
			'orderby' => array(
265
				'comment_date' => 'asc',
266
				'foo_key' => 'asc',
267
			),
268
		) );
269
270
		$this->assertEquals( array( $u3, $u1, $u2 ), $q->results );
0 ignored issues
show
Documentation introduced by
The property $results is declared private in WP_User_Query. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
271
	}
272
273
	/**
274
	 * @ticket 31265
275
	 */
276
	public function test_orderby_more_than_one_clause_key() {
277
		add_user_meta( self::$author_ids[0], 'foo', 'jjj' );
278
		add_user_meta( self::$author_ids[1], 'foo', 'zzz' );
279
		add_user_meta( self::$author_ids[2], 'foo', 'jjj' );
280
		add_user_meta( self::$author_ids[0], 'bar', 'aaa' );
281
		add_user_meta( self::$author_ids[1], 'bar', 'ccc' );
282
		add_user_meta( self::$author_ids[2], 'bar', 'bbb' );
283
284
		$q = new WP_User_Query( array(
285
			'fields' => 'ids',
286
			'meta_query' => array(
287
				'foo_key' => array(
288
					'key' => 'foo',
289
					'compare' => 'EXISTS',
290
				),
291
				'bar_key' => array(
292
					'key' => 'bar',
293
					'compare' => 'EXISTS',
294
				),
295
			),
296
			'orderby' => array(
297
				'foo_key' => 'asc',
298
				'bar_key' => 'desc',
299
			),
300
		) );
301
302
		$this->assertEquals( array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] ), $q->results );
0 ignored issues
show
Documentation introduced by
The property $results is declared private in WP_User_Query. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
303
	}
304
305
	/**
306
	 * @ticket 30064
307
	 */
308
	public function test_orderby_include_with_empty_include() {
309
		$q = new WP_User_Query( array(
310
			'orderby' => 'include',
311
		) );
312
313
		$this->assertContains( 'ORDER BY user_login', $q->query_orderby );
314
	}
315
316
	/**
317
	 * @ticket 30064
318
	 */
319
	public function test_orderby_include() {
320
		global $wpdb;
321
322
		$q = new WP_User_Query( array(
323
			'orderby' => 'include',
324
			'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ),
325
			'fields' => '',
326
		) );
327
328
		$expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )';
329
		$this->assertContains( $expected_orderby, $q->query_orderby );
330
331
		// assertEquals() respects order but ignores type (get_results() returns numeric strings).
332
		$this->assertEquals( array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results() );
333
	}
334
335
	/**
336
	 * @ticket 30064
337
	 */
338
	public function test_orderby_include_duplicate_values() {
339
		global $wpdb;
340
341
		$q = new WP_User_Query( array(
342
			'orderby' => 'include',
343
			'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[1], self::$author_ids[3] ),
344
			'fields' => '',
345
		) );
346
347
		$expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )';
348
		$this->assertContains( $expected_orderby, $q->query_orderby );
349
350
		// assertEquals() respects order but ignores type (get_results() returns numeric strings).
351
		$this->assertEquals( array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results() );
352
	}
353
354
	/**
355
	 * @ticket 31265
356
	 */
357
	public function test_orderby_space_separated() {
358
		$q = new WP_User_Query( array(
359
			'orderby' => 'login nicename',
360
			'order' => 'ASC',
361
		) );
362
363
		$this->assertContains( "ORDER BY user_login ASC, user_nicename ASC", $q->query_orderby );
364
	}
365
366
	/**
367
	 * @ticket 31265
368
	 */
369
	public function test_orderby_flat_array() {
370
		$q = new WP_User_Query( array(
371
			'orderby' => array( 'login', 'nicename' ),
372
		) );
373
374
		$this->assertContains( "ORDER BY user_login ASC, user_nicename ASC", $q->query_orderby );
375
	}
376
377
	/**
378
	 * @ticket 31265
379
	 */
380
	public function test_orderby_array_contains_invalid_item() {
381
		$q = new WP_User_Query( array(
382
			'orderby' => array( 'login', 'foo', 'nicename' ),
383
		) );
384
385
		$this->assertContains( "ORDER BY user_login ASC, user_nicename ASC", $q->query_orderby );
386
	}
387
388
	/**
389
	 * @ticket 31265
390
	 */
391
	public function test_orderby_array_contains_all_invalid_items() {
392
		$q = new WP_User_Query( array(
393
			'orderby' => array( 'foo', 'bar', 'baz' ),
394
		) );
395
396
		$this->assertContains( "ORDER BY user_login", $q->query_orderby );
397
	}
398
399
	/**
400
	 * @ticket 31265
401
	 */
402
	public function test_orderby_array() {
403
		$q = new WP_User_Query( array(
404
			'orderby' => array(
405
				'login' => 'DESC',
406
				'nicename' => 'ASC',
407
				'email' => 'DESC',
408
			),
409
		) );
410
411
		$this->assertContains( "ORDER BY user_login DESC, user_nicename ASC, user_email DESC", $q->query_orderby );
412
	}
413
414
	/**
415
	 * @ticket 31265
416
	 */
417
	public function test_orderby_array_should_discard_invalid_columns() {
418
		$q = new WP_User_Query( array(
419
			'orderby' => array(
420
				'login' => 'DESC',
421
				'foo' => 'ASC',
422
				'email' => 'ASC',
423
			),
424
		) );
425
426
		$this->assertContains( "ORDER BY user_login DESC, user_email ASC", $q->query_orderby );
427
	}
428
429
	/**
430
	 * @ticket 28631
431
	 */
432
	function test_number() {
433
		// +1 for the default user created by the test suite.
434
		$users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) );
435
		$users = $users->get_results();
436
		$this->assertEquals( 13, count( $users ) );
437
438
		$users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => 10 ) );
439
		$users = $users->get_results();
440
		$this->assertEquals( 10, count( $users ) );
441
442
		$users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => 2 ) );
443
		$users = $users->get_results();
444
		$this->assertEquals( 2, count( $users ) );
445
446
		$users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => -1 ) );
447
		$users = $users->get_results();
448
		$this->assertEquals( 13, count( $users ) );
449
	}
450
451
	/**
452
	 * @ticket 21119
453
	 */
454
	function test_prepare_query() {
455
		$query = new WP_User_Query();
456
		$this->assertEmpty( $query->query_fields );
457
		$this->assertEmpty( $query->query_from );
458
		$this->assertEmpty( $query->query_limit );
459
		$this->assertEmpty( $query->query_orderby );
460
		$this->assertEmpty( $query->query_where );
461
		$this->assertEmpty( $query->query_vars );
462
		$_query_vars = $query->query_vars;
463
464
		$query->prepare_query();
465
		$this->assertNotEmpty( $query->query_fields );
466
		$this->assertNotEmpty( $query->query_from );
467
		$this->assertEmpty( $query->query_limit );
468
		$this->assertNotEmpty( $query->query_orderby );
469
		$this->assertNotEmpty( $query->query_where );
470
		$this->assertNotEmpty( $query->query_vars );
471
		$this->assertNotEquals( $_query_vars, $query->query_vars );
472
473
		// All values get reset
474
		$query->prepare_query( array( 'number' => 8 ) );
475
		$this->assertNotEmpty( $query->query_limit );
476
		$this->assertEquals( 'LIMIT 0, 8', $query->query_limit );
477
478
		// All values get reset
479
		$query->prepare_query( array( 'fields' => 'all' ) );
480
		$this->assertEmpty( $query->query_limit );
481
		$this->assertEquals( '', $query->query_limit );
482
		$_query_vars = $query->query_vars;
483
484
		$query->prepare_query();
485
		$this->assertEquals( $_query_vars, $query->query_vars );
486
487
		$query->prepare_query( array( 'number' => -1 ) );
488
		$this->assertNotEquals( 'LIMIT -1', $query->query_limit );
489
		$this->assertEmpty( $query->query_limit );
490
	}
491
492
	public function test_meta_vars_should_be_converted_to_meta_query() {
493
		$q = new WP_User_Query( array(
494
			'meta_key' => 'foo',
495
			'meta_value' => '5',
496
			'meta_compare' => '>',
497
			'meta_type' => 'SIGNED',
498
		) );
499
500
		// Multisite adds a 'blog_id' clause, so we have to find the 'foo' clause.
501
		$mq_clauses = $q->meta_query->get_clauses();
502
		foreach ( $mq_clauses as $mq_clause ) {
503
			if ( 'foo' === $mq_clause['key'] ) {
504
				$clause = $mq_clause;
505
			}
506
		}
507
508
		$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...
509
		$this->assertSame( '5', $clause['value'] );
510
		$this->assertSame( '>', $clause['compare'] );
511
		$this->assertSame( 'SIGNED', $clause['type'] );
512
	}
513
514
	/**
515
	 * @ticket 23849
516
	 */
517
	function test_meta_query_with_role() {
518
		add_user_meta( self::$author_ids[0], 'foo', 'bar' );
519
		add_user_meta( self::$author_ids[1], 'foo', 'baz' );
520
521
		// Users with foo = bar or baz restricted to the author role.
522
		$query = new WP_User_Query( array(
523
			'fields' => '',
524
			'role' => 'author',
525
			'meta_query' => array(
526
				'relation' => 'OR',
527
				array(
528
					'key' => 'foo',
529
					'value' => 'bar',
530
				),
531
				array(
532
					'key' => 'foo',
533
					'value' => 'baz',
534
				),
535
			),
536
		) );
537
538
		$this->assertEquals( array( self::$author_ids[0], self::$author_ids[1] ), $query->get_results() );
539
	}
540
541
	public function test_roles_and_caps_should_be_populated_for_default_value_of_blog_id() {
542
		$query = new WP_User_Query( array(
543
			'include' => self::$author_ids[0],
544
		) );
545
546
		$found = $query->get_results();
547
548
		$this->assertNotEmpty( $found );
549
		$user = reset( $found );
550
		$this->assertSame( array( 'author' ), $user->roles );
551
		$this->assertSame( array( 'author' => true ), $user->caps );
552
	}
553
554
	/**
555
	 * @group ms-excluded
556
	 */
557
	public function test_roles_and_caps_should_be_populated_for_explicit_value_of_blog_id_on_nonms() {
558
		$query = new WP_User_Query( array(
559
			'include' => self::$author_ids[0],
560
			'blog_id' => get_current_blog_id(),
561
		) );
562
563
		$found = $query->get_results();
564
565
		$this->assertNotEmpty( $found );
566
		$user = reset( $found );
567
		$this->assertSame( array( 'author' ), $user->roles );
568
		$this->assertSame( array( 'author' => true ), $user->caps );
569
	}
570
571
	/**
572
	 * @group ms-required
573
	 */
574
	public function test_roles_and_caps_should_be_populated_for_explicit_value_of_current_blog_id_on_ms() {
575
		$query = new WP_User_Query( array(
576
			'include' => self::$author_ids[0],
577
			'blog_id' => get_current_blog_id(),
578
		) );
579
580
		$found = $query->get_results();
581
582
		$this->assertNotEmpty( $found );
583
		$user = reset( $found );
584
		$this->assertSame( array( 'author' ), $user->roles );
585
		$this->assertSame( array( 'author' => true ), $user->caps );
586
	}
587
588
	/**
589
	 * @group ms-required
590
	 */
591
	public function test_roles_and_caps_should_be_populated_for_explicit_value_of_different_blog_id_on_ms_when_fields_all_with_meta() {
592
		$b = self::factory()->blog->create();
593
594
		add_user_to_blog( $b, self::$author_ids[0], 'author' );
595
596
		$query = new WP_User_Query( array(
597
			'include' => self::$author_ids[0],
598
			'blog_id' => $b,
599
			'fields' => 'all_with_meta',
600
		) );
601
602
		$found = $query->get_results();
603
604
		$this->assertNotEmpty( $found );
605
		$user = reset( $found );
606
		$this->assertSame( array( 'author' ), $user->roles );
607
		$this->assertSame( array( 'author' => true ), $user->caps );
608
	}
609
610
	/**
611
	 * @ticket 31878
612
	 * @group ms-required
613
	 */
614
	public function test_roles_and_caps_should_be_populated_for_explicit_value_of_different_blog_id_on_ms_when_fields_all() {
615
		$b = self::factory()->blog->create();
616
		add_user_to_blog( $b, self::$author_ids[0], 'author' );
617
618
		$query = new WP_User_Query( array(
619
			'fields' => 'all',
620
			'include' => self::$author_ids[0],
621
			'blog_id' => $b,
622
		) );
623
624
		$found = $query->get_results();
625
626
		$this->assertNotEmpty( $found );
627
		$user = reset( $found );
628
		$this->assertSame( array( 'author' ), $user->roles );
629
		$this->assertSame( array( 'author' => true ), $user->caps );
630
	}
631
632
	/**
633
	 * @ticket 32019
634
	 * @group ms-required
635
	 */
636
	public function test_who_authors() {
637
		$b = self::factory()->blog->create();
638
639
		add_user_to_blog( $b, self::$author_ids[0], 'subscriber' );
640
		add_user_to_blog( $b, self::$author_ids[1], 'author' );
641
		add_user_to_blog( $b, self::$author_ids[2], 'editor' );
642
643
		$q = new WP_User_Query( array(
644
			'who' => 'authors',
645
			'blog_id' => $b,
646
		) );
647
648
		$found = wp_list_pluck( $q->get_results(), 'ID' );
649
650
		$this->assertNotContains( self::$author_ids[0], $found );
651
		$this->assertContains( self::$author_ids[1], $found );
652
		$this->assertContains( self::$author_ids[2], $found );
653
	}
654
655
	/**
656
	 * @ticket 32019
657
	 * @group ms-required
658
	 */
659
	public function test_who_authors_should_work_alongside_meta_query() {
660
		$b = self::factory()->blog->create();
661
662
		add_user_to_blog( $b, self::$author_ids[0], 'subscriber' );
663
		add_user_to_blog( $b, self::$author_ids[1], 'author' );
664
		add_user_to_blog( $b, self::$author_ids[2], 'editor' );
665
666
		add_user_meta( self::$author_ids[1], 'foo', 'bar' );
667
		add_user_meta( self::$author_ids[2], 'foo', 'baz' );
668
669
		$q = new WP_User_Query( array(
670
			'who' => 'authors',
671
			'blog_id' => $b,
672
			'meta_query' => array(
673
				array(
674
					'key' => 'foo',
675
					'value' => 'bar',
676
				)
677
			),
678
		) );
679
680
		$found = wp_list_pluck( $q->get_results(), 'ID' );
681
682
		$this->assertNotContains( self::$author_ids[0], $found );
683
		$this->assertContains( self::$author_ids[1], $found );
684
		$this->assertNotContains( self::$author_ids[2], $found );
685
	}
686
687
	/**
688
	 * @ticket 36724
689
	 * @group ms-required
690
	 */
691
	public function test_who_authors_should_work_alongside_meta_params() {
692
		$b = self::factory()->blog->create();
693
694
		add_user_to_blog( $b, self::$author_ids[0], 'subscriber' );
695
		add_user_to_blog( $b, self::$author_ids[1], 'author' );
696
		add_user_to_blog( $b, self::$author_ids[2], 'editor' );
697
698
		add_user_meta( self::$author_ids[1], 'foo', 'bar' );
699
		add_user_meta( self::$author_ids[2], 'foo', 'baz' );
700
701
		$q = new WP_User_Query( array(
702
			'who' => 'authors',
703
			'blog_id' => $b,
704
			'meta_key' => 'foo',
705
			'meta_value' => 'bar',
706
		) );
707
708
		$found = wp_list_pluck( $q->get_results(), 'ID' );
709
710
		$this->assertNotContains( self::$author_ids[0], $found );
711
		$this->assertContains( self::$author_ids[1], $found );
712
		$this->assertNotContains( self::$author_ids[2], $found );
713
	}
714
715
	/**
716
	 * @ticket 32250
717
	 */
718
	public function test_has_published_posts_with_value_true_should_show_authors_of_posts_in_public_post_types() {
719
		register_post_type( 'wptests_pt_public', array( 'public' => true ) );
720
		register_post_type( 'wptests_pt_private', array( 'public' => false ) );
721
722
		self::factory()->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) );
723
		self::factory()->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) );
724
725
		$q = new WP_User_Query( array(
726
			'has_published_posts' => true,
727
		) );
728
729
		$found = wp_list_pluck( $q->get_results(), 'ID' );
730
		$expected = array( self::$author_ids[0] );
731
732
		$this->assertEqualSets( $expected, $found );
733
	}
734
735
	/**
736
	 * @ticket 32250
737
	 */
738
	public function test_has_published_posts_should_obey_post_types() {
739
		register_post_type( 'wptests_pt_public', array( 'public' => true ) );
740
		register_post_type( 'wptests_pt_private', array( 'public' => false ) );
741
742
		self::factory()->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) );
743
		self::factory()->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) );
744
		self::factory()->post->create( array( 'post_author' => self::$author_ids[2], 'post_status' => 'publish', 'post_type' => 'post' ) );
745
746
		$q = new WP_User_Query( array(
747
			'has_published_posts' => array( 'wptests_pt_private', 'post' ),
748
		) );
749
750
		$found = wp_list_pluck( $q->get_results(), 'ID' );
751
		$expected = array( self::$author_ids[1], self::$author_ids[2] );
752
753
		$this->assertEqualSets( $expected, $found );
754
	}
755
756
	/**
757
	 * @ticket 32250
758
	 */
759
	public function test_has_published_posts_should_ignore_non_published_posts() {
760
		register_post_type( 'wptests_pt_public', array( 'public' => true ) );
761
		register_post_type( 'wptests_pt_private', array( 'public' => false ) );
762
763
		self::factory()->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'draft', 'post_type' => 'wptests_pt_public' ) );
764
		self::factory()->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'inherit', 'post_type' => 'wptests_pt_private' ) );
765
		self::factory()->post->create( array( 'post_author' => self::$author_ids[2], 'post_status' => 'publish', 'post_type' => 'post' ) );
766
767
		$q = new WP_User_Query( array(
768
			'has_published_posts' => array( 'wptests_pt_public', 'wptests_pt_private', 'post' ),
769
		) );
770
771
		$found = wp_list_pluck( $q->get_results(), 'ID' );
772
		$expected = array( self::$author_ids[2] );
773
774
		$this->assertEqualSets( $expected, $found );
775
	}
776
777
	/**
778
	 * @ticket 32250
779
	 * @group ms-required
780
	 */
781
	public function test_has_published_posts_should_respect_blog_id() {
782
		$blogs = self::factory()->blog->create_many( 2 );
783
784
		add_user_to_blog( $blogs[0], self::$author_ids[0], 'author' );
785
		add_user_to_blog( $blogs[0], self::$author_ids[1], 'author' );
786
		add_user_to_blog( $blogs[1], self::$author_ids[0], 'author' );
787
		add_user_to_blog( $blogs[1], self::$author_ids[1], 'author' );
788
789
		switch_to_blog( $blogs[0] );
790
		self::factory()->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'post' ) );
791
		restore_current_blog();
792
793
		switch_to_blog( $blogs[1] );
794
		self::factory()->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'post' ) );
795
		restore_current_blog();
796
797
		$q = new WP_User_Query( array(
798
			'has_published_posts' => array( 'post' ),
799
			'blog_id' => $blogs[1],
800
		) );
801
802
		$found = wp_list_pluck( $q->get_results(), 'ID' );
803
		$expected = array( self::$author_ids[1] );
804
805
		$this->assertEqualSets( $expected, $found );
806
	}
807
808
	/**
809
	 * @ticket 32592
810
	 */
811
	public function test_top_level_or_meta_query_should_eliminate_duplicate_matches() {
812
		add_user_meta( self::$author_ids[0], 'foo', 'bar' );
813
		add_user_meta( self::$author_ids[1], 'foo', 'bar' );
814
		add_user_meta( self::$author_ids[0], 'foo2', 'bar2' );
815
816
		$q = new WP_User_Query( array(
817
			'meta_query' => array(
818
				'relation' => 'OR',
819
				array(
820
					'key' => 'foo',
821
					'value' => 'bar',
822
				),
823
				array(
824
					'key' => 'foo2',
825
					'value' => 'bar2',
826
				),
827
			),
828
		) );
829
830
		$found = wp_list_pluck( $q->get_results(), 'ID' );
831
		$expected = array( self::$author_ids[0], self::$author_ids[1] );
832
833
		$this->assertEqualSets( $expected, $found );
834
	}
835
836
	/**
837
	 * @ticket 32592
838
	 */
839
	public function test_nested_or_meta_query_should_eliminate_duplicate_matches() {
840
		add_user_meta( self::$author_ids[0], 'foo', 'bar' );
841
		add_user_meta( self::$author_ids[1], 'foo', 'bar' );
842
		add_user_meta( self::$author_ids[0], 'foo2', 'bar2' );
843
		add_user_meta( self::$author_ids[1], 'foo3', 'bar3' );
844
845
		$q = new WP_User_Query( array(
846
			'meta_query' => array(
847
				'relation' => 'AND',
848
				array(
849
					'key' => 'foo',
850
					'value' => 'bar',
851
				),
852
				array(
853
					'relation' => 'OR',
854
					array(
855
						'key' => 'foo',
856
						'value' => 'bar',
857
					),
858
					array(
859
						'key' => 'foo2',
860
						'value' => 'bar2',
861
					),
862
				),
863
			),
864
		) );
865
866
		$found = wp_list_pluck( $q->get_results(), 'ID' );
867
		$expected = array( self::$author_ids[0], self::$author_ids[1] );
868
869
		$this->assertEqualSets( $expected, $found );
870
	}
871
872
	/**
873
	 * @ticket 36624
874
	 */
875
	public function test_nicename_returns_user_with_nicename() {
876
		wp_update_user( array(
877
			'ID' => self::$author_ids[0],
878
			'user_nicename' => 'peter'
879
		) );
880
881
		$q = new WP_User_Query( array (
882
			'nicename' => 'peter'
883
		) );
884
885
		$found = wp_list_pluck( $q->get_results(), 'ID' );
886
		$expected = array( self::$author_ids[0] );
887
888
		$this->assertContains( "AND user_nicename = 'peter'", $q->query_where);
889
		$this->assertEqualSets( $expected, $found);
890
	}
891
892
	/**
893
	 * @ticket 36624
894
	 */
895
	public function test_nicename__in_returns_users_with_included_nicenames() {
896
		wp_update_user( array(
897
			'ID' => self::$author_ids[0],
898
			'user_nicename' => 'peter'
899
		) );
900
901
		wp_update_user( array(
902
			'ID' => self::$author_ids[1],
903
			'user_nicename' => 'paul'
904
		) );
905
906
		wp_update_user( array(
907
			'ID' => self::$author_ids[2],
908
			'user_nicename' => 'mary'
909
		) );
910
911
		$q = new WP_User_Query( array (
912
			'nicename__in' => array( 'peter', 'paul', 'mary' )
913
		) );
914
915
		$found = wp_list_pluck( $q->get_results(), 'ID' );
916
		$expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
917
918
		$this->assertContains( "AND user_nicename IN ( 'peter','paul','mary' )", $q->query_where);
919
		$this->assertEqualSets( $expected, $found );
920
	}
921
922
	/**
923
	 * @ticket 36624
924
	 */
925
	public function test_nicename__not_in_returns_users_without_included_nicenames() {
926
		wp_update_user( array(
927
			'ID' => self::$author_ids[0],
928
			'user_nicename' => 'peter'
929
		) );
930
931
		wp_update_user( array(
932
			'ID' => self::$author_ids[1],
933
			'user_nicename' => 'paul'
934
		) );
935
936
		wp_update_user( array(
937
			'ID' => self::$author_ids[2],
938
			'user_nicename' => 'mary'
939
		) );
940
941
		$q = new WP_User_Query( array (
942
			'nicename__not_in' => array( 'peter', 'paul', 'mary' )
943
		) );
944
945
		$foundCount = count($q->get_results());
946
		$expectedCount = 10; // 13 total users minus 3 from query
947
948
		$this->assertContains( "AND user_nicename NOT IN ( 'peter','paul','mary' )", $q->query_where);
949
		$this->assertEquals( $expectedCount, $foundCount );
950
	}
951
952
	/**
953
	 * @ticket 36624
954
	 */
955
	public function test_orderby_nicename__in() {
956
		wp_update_user( array(
957
			'ID' => self::$author_ids[0],
958
			'user_nicename' => 'peter'
959
		) );
960
961
		wp_update_user( array(
962
			'ID' => self::$author_ids[1],
963
			'user_nicename' => 'paul'
964
		) );
965
966
		wp_update_user( array(
967
			'ID' => self::$author_ids[2],
968
			'user_nicename' => 'mary'
969
		) );
970
971
		$q = new WP_User_Query( array (
972
			'nicename__in' => array( 'mary', 'peter', 'paul' ),
973
			'orderby' => 'nicename__in'
974
		) );
975
976
		$found = wp_list_pluck( $q->get_results(), 'ID' );
977
		$expected = array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] );
978
979
		$this->assertContains( "FIELD( user_nicename, 'mary','peter','paul' )", $q->query_orderby);
980
		$this->assertSame( $expected, $found );
981
	}
982
983
	/**
984
	 * @ticket 36624
985
	 */
986
	public function test_login_returns_user_with_login() {
987
988
		$user_login = get_userdata( self::$author_ids[0] )->user_login;
989
990
		$q = new WP_User_Query( array (
991
			'login' => $user_login
992
		) );
993
994
		$found = wp_list_pluck( $q->get_results(), 'ID' );
995
		$expected = array( self::$author_ids[0] );
996
997
		$this->assertContains( "AND user_login = '$user_login'", $q->query_where);
998
		$this->assertEqualSets( $expected, $found);
999
	}
1000
1001
	/**
1002
	 * @ticket 36624
1003
	 */
1004
	public function test_login__in_returns_users_with_included_logins() {
1005
		$user_login1 = get_userdata( self::$author_ids[0] )->user_login;
1006
		$user_login2 = get_userdata( self::$author_ids[1] )->user_login;
1007
		$user_login3 = get_userdata( self::$author_ids[2] )->user_login;
1008
1009
		$q = new WP_User_Query( array (
1010
			'login__in' => array( $user_login1, $user_login2, $user_login3 )
1011
		) );
1012
1013
		$found = wp_list_pluck( $q->get_results(), 'ID' );
1014
		$expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
1015
1016
		$this->assertContains( "AND user_login IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where);
1017
		$this->assertEqualSets( $expected, $found );
1018
	}
1019
1020
	/**
1021
	 * @ticket 36624
1022
	 */
1023
	public function test_login__not_in_returns_users_without_included_logins() {
1024
		$user_login1 = get_userdata( self::$author_ids[0] )->user_login;
1025
		$user_login2 = get_userdata( self::$author_ids[1] )->user_login;
1026
		$user_login3 = get_userdata( self::$author_ids[2] )->user_login;
1027
1028
		$q = new WP_User_Query( array (
1029
			'login__not_in' => array( $user_login1, $user_login2, $user_login3 )
1030
		) );
1031
1032
		$foundCount = count($q->get_results());
1033
		$expectedCount = 10; // 13 total users minus 3 from query
1034
1035
		$this->assertContains( "AND user_login NOT IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where);
1036
		$this->assertEquals( $expectedCount, $foundCount );
1037
	}
1038
1039
	/**
1040
	 * @ticket 36624
1041
	 */
1042
	public function test_orderby_login__in() {
1043
		$user_login1 = get_userdata( self::$author_ids[0] )->user_login;
1044
		$user_login2 = get_userdata( self::$author_ids[1] )->user_login;
1045
		$user_login3 = get_userdata( self::$author_ids[2] )->user_login;
1046
1047
		$q = new WP_User_Query( array (
1048
			'login__in' => array( $user_login2, $user_login3, $user_login1 ),
1049
			'orderby' => 'login__in'
1050
		) );
1051
1052
		$found = wp_list_pluck( $q->get_results(), 'ID' );
1053
		$expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
1054
1055
		$this->assertContains( "FIELD( user_login, '$user_login2','$user_login3','$user_login1' )", $q->query_orderby);
1056
		$this->assertSame( $expected, $found );
1057
	}
1058
1059
	/**
1060
	 * @ticket 25145
1061
	 */
1062
	public function test_paged() {
1063
		$q = new WP_User_Query( array(
1064
			'number' => 2,
1065
			'paged' => 2,
1066
			'orderby' => 'ID',
1067
			'order' => 'DESC', // Avoid funkiness with user 1.
1068
			'fields' => 'ids',
1069
		) );
1070
1071
		$this->assertEquals( array( self::$contrib_id, self::$editor_ids[2] ), $q->results );
0 ignored issues
show
Documentation introduced by
The property $results is declared private in WP_User_Query. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1072
	}
1073
1074
	/**
1075
	 * @ticket 33449
1076
	 */
1077
	public function test_query_vars_should_be_filled_in_after_pre_get_users() {
1078
		$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' );
1079
1080
		add_action( 'pre_get_users', array( $this, 'filter_pre_get_users_args' ) );
1081
		$q = new WP_User_Query( array_fill_keys( $query_vars, '1' ) );
1082
		remove_action( 'pre_get_users', array( $this, 'filter_pre_get_users_args' ) );
1083
1084
		foreach ( $query_vars as $query_var ) {
1085
			$this->assertTrue( array_key_exists( $query_var, $q->query_vars ), "$query_var does not exist." );
1086
		}
1087
1088
	}
1089
1090
	public function filter_pre_get_users_args( $q ) {
1091
		foreach ( $q->query_vars as $k => $v ) {
1092
			unset( $q->query_vars[ $k ] );
1093
		}
1094
	}
1095
1096
	/**
1097
	 * @ticket 22212
1098
	 */
1099
	public function test_get_single_role_by_user_query() {
1100
		$wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) );
1101
		$users          = $wp_user_search->get_results();
1102
1103
		$this->assertEquals( 2, count( $users ) );
1104
	}
1105
1106
	/**
1107
	 * @ticket 22212
1108
	 */
1109
	public function test_get_multiple_roles_by_user_query() {
1110
		$wp_user_search = new WP_User_Query( array( 'role__in' => array( 'subscriber', 'editor' ) ) );
1111
		$users          = $wp_user_search->get_results();
1112
		$this->assertEquals( 5, count( $users ) );
1113
	}
1114
1115
	/**
1116
	 * @ticket 22212
1117
	 */
1118
	public function test_get_single_role_by_string() {
1119
		$users = get_users( array(
1120
			'role' => 'subscriber',
1121
		) );
1122
1123
		$this->assertEquals( 2, count( $users ) );
1124
	}
1125
1126
	/**
1127
	 * @ticket 22212
1128
	 */
1129
	public function test_get_single_role_by_string_which_is_similar() {
1130
		$another_editor = self::factory()->user->create( array(
0 ignored issues
show
Unused Code introduced by
$another_editor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1131
			'user_email' => 'another_editor@another_editor.com',
1132
			'user_login' => 'another_editor',
1133
			'role' => 'another-editor',
1134
		) );
1135
1136
		$users = get_users( array(
1137
			'role' => 'editor',
1138
			'fields' => 'ids',
1139
		) );
1140
1141
		$this->assertEqualSets( self::$editor_ids, $users );
1142
	}
1143
1144
1145
	/**
1146
	 * @ticket 22212
1147
	 */
1148
	public function test_get_single_role_by_array() {
1149
		$users = get_users( array(
1150
			'role' => array( 'subscriber' ),
1151
		) );
1152
1153
		$this->assertEquals( 2, count( $users ) );
1154
	}
1155
1156
	/**
1157
	 * @ticket 22212
1158
	 */
1159
	public function test_get_multiple_roles_should_only_match_users_who_have_each_role() {
1160
		$users = new WP_User_Query( array( 'role' => array( 'subscriber', 'editor' ) ) );
1161
		$users = $users->get_results();
1162
1163
		$this->assertEmpty( $users );
1164
1165
		foreach ( self::$sub_ids as $subscriber ) {
1166
			$subscriber = get_user_by( 'ID', $subscriber );
1167
			$subscriber->add_role( 'editor' );
1168
		}
1169
1170
		$users = new WP_User_Query( array( 'role' => array( 'subscriber', 'editor' ) ) );
1171
		$users = $users->get_results();
1172
1173
		$this->assertEquals( 2, count( $users ) );
1174
1175
		foreach ( $users as $user ) {
1176
			$this->assertInstanceOf( 'WP_User', $user );
1177
		}
1178
	}
1179
1180
	/**
1181
	 * @ticket 22212
1182
	 */
1183
	public function test_get_multiple_roles_or() {
1184
		$users = new WP_User_Query( array( 'role__in' => array( 'subscriber', 'editor', 'administrator' ) ) );
1185
		$users = $users->get_results();
1186
1187
		// +1 for the default user created during installation.
1188
		$this->assertEquals( 8, count( $users ) );
1189
		foreach ( $users as $user ) {
1190
			$this->assertInstanceOf( 'WP_User', $user );
1191
		}
1192
	}
1193
1194
	/**
1195
	 * @ticket 22212
1196
	 */
1197
	public function test_get_multiple_roles_by_comma_separated_list() {
1198
		$users = get_users( array(
1199
			'role' => 'subscriber, editor',
1200
		) );
1201
1202
		$this->assertEmpty( $users );
1203
1204
		foreach ( self::$sub_ids as $subscriber ) {
1205
			$subscriber = get_user_by( 'ID', $subscriber );
1206
			$subscriber->add_role( 'editor' );
1207
		}
1208
1209
		$users = get_users( array(
1210
			'role' => 'subscriber, editor',
1211
		) );
1212
1213
		$this->assertEquals( 2, count( $users ) );
1214
	}
1215
1216
	/**
1217
	 * @ticket 22212
1218
	 */
1219
	public function test_get_multiple_roles_with_meta() {
1220
		// Create administrator user + meta
1221
		update_user_meta( self::$admin_ids[0], 'mk1', 1 );
1222
		update_user_meta( self::$admin_ids[0], 'mk2', 1 );
1223
1224
		// Create editor user + meta
1225
		update_user_meta( self::$editor_ids[0], 'mk1', 1 );
1226
		update_user_meta( self::$editor_ids[0], 'mk2', 2 );
1227
1228
		// Create subscriber user + meta
1229
		update_user_meta( self::$sub_ids[0], 'mk1', 1 );
1230
		update_user_meta( self::$sub_ids[0], 'mk2', 1 );
1231
1232
		// Create contributor user + meta
1233
		update_user_meta( self::$contrib_id, 'mk1', 1 );
1234
		update_user_meta( self::$contrib_id, 'mk2', 2 );
1235
1236
		// Fetch users
1237
		$users = get_users( array(
1238
			'role__in'   => array( 'administrator', 'editor', 'subscriber' ),
1239
			'meta_query' => array(
1240
				'relation' => 'AND',
1241
				array(
1242
					'key'     => 'mk1',
1243
					'value'   => '1',
1244
					'compare' => "=",
1245
					'type'    => 'numeric',
1246
				),
1247
				array(
1248
					'key'     => 'mk2',
1249
					'value'   => '2',
1250
					'compare' => "=",
1251
					'type'    => 'numeric',
1252
				),
1253
			),
1254
		) );
1255
1256
		// Check results
1257
		$this->assertEquals( 1, count( $users ) );
1258
		$this->assertSame( self::$editor_ids[0], (int) $users[0]->ID );
1259
	}
1260
1261
	/**
1262
	 * @ticket 22212
1263
	 */
1264
	public function test_role_exclusion() {
1265
		$users = get_users( array(
1266
			'role__not_in' => 'subscriber',
1267
		) );
1268
1269
		// +1 for the default user created during installation.
1270
		$this->assertEquals( 11, count( $users ) );
1271
1272
		$users = get_users( array(
1273
			'role__not_in' => 'editor',
1274
		) );
1275
1276
		// +1 for the default user created during installation.
1277
		$this->assertEquals( 10, count( $users ) );
1278
	}
1279
1280
	/**
1281
	 * @ticket 22212
1282
	 */
1283
	public function test_role__in_role__not_in_combined() {
1284
		foreach ( self::$sub_ids as $subscriber ) {
1285
			$subscriber = get_user_by( 'ID', $subscriber );
1286
			$subscriber->add_role( 'editor' );
1287
		}
1288
1289
		$users = get_users( array(
1290
			'role__in'     => 'editor',
1291
		) );
1292
1293
		$this->assertEquals( 5, count( $users ) );
1294
1295
		$users = get_users( array(
1296
			'role__in'     => 'editor',
1297
			'role__not_in' => 'subscriber',
1298
		) );
1299
1300
		$this->assertEquals( 3, count( $users ) );
1301
	}
1302
1303
	/**
1304
	 * @ticket 22212
1305
	 */
1306
	public function test_role__not_in_role_combined() {
1307
		$subscriber = get_user_by( 'ID', self::$sub_ids[0] );
1308
		$subscriber->add_role( 'editor' );
1309
1310
		$users = get_users( array(
1311
			'role'         => 'subscriber',
1312
			'role__not_in' => array( 'editor' ),
1313
		) );
1314
1315
		$this->assertEquals( 1, count( $users ) );
1316
	}
1317
1318
	/**
1319
	 * @ticket 22212
1320
	 */
1321
	public function test_role__not_in_user_without_role() {
1322
		$user_without_rule = get_user_by( 'ID', self::$sub_ids[0] );
1323
1324
		$user_without_rule->remove_role( 'subscriber' );
1325
1326
		$users = get_users( array(
1327
			'role__not_in' => 'subscriber',
1328
		) );
1329
1330
		// +1 for the default user created during installation.
1331
		$this->assertEquals( 12, count( $users ) );
1332
1333
		$users = get_users( array(
1334
			'role__not_in' => 'editor',
1335
		) );
1336
1337
		// +1 for the default user created during installation.
1338
		$this->assertEquals( 10, count( $users ) );
1339
	}
1340
1341
	/**
1342
	 * @ticket 22212
1343
	 * @group ms-required
1344
	 */
1345
	public function test_blog_id_should_restrict_by_blog_without_requiring_a_named_role() {
1346
		$sites = self::factory()->blog->create_many( 2 );
1347
1348
		add_user_to_blog( $sites[0], self::$author_ids[0], 'author' );
1349
		add_user_to_blog( $sites[1], self::$author_ids[1], 'author' );
1350
1351
		$found = get_users( array(
1352
			'blog_id' => $sites[1],
1353
			'fields' => 'ID',
1354
		) );
1355
1356
		$this->assertEqualSets( array( self::$author_ids[1] ), $found );
1357
	}
1358
1359
	/**
1360
	 * @ticket 22212
1361
	 * @ticket 21119
1362
	 * @group ms-required
1363
	 */
1364
	public function test_calling_prepare_query_a_second_time_should_not_add_another_cap_query_on_multisite() {
1365
		$site_id = get_current_blog_id();
1366
		add_user_to_blog( $site_id, self::$author_ids[0], 'author' );
1367
1368
		$q = new WP_User_Query( array(
1369
			'include' => self::$author_ids[0],
1370
		) );
1371
1372
		$r1 = $q->request;
1373
1374
		$q->prepare_query( array(
1375
			'include' => self::$author_ids[0],
1376
		) );
1377
1378
		$r2 = $q->request;
1379
1380
		$q->prepare_query( array(
1381
			'include' => self::$author_ids[0],
1382
		) );
1383
1384
		$r3 = $q->request;
1385
1386
		$this->assertSame( $r1, $r2 );
1387
		$this->assertSame( $r1, $r3 );
1388
	}
1389
}
1390