Skip to content

Commit af51967

Browse files
committed
Tests: Add coverage for wp_roles().
Verify initialization, reuse of an existing `WP_Roles` instance, and replacement of invalid global values. Cover the behavior introduced in [63490]. Developed in: #14257 Props huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64360 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 15bd12c commit af51967

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/**
4+
* Tests for wp_roles().
5+
*
6+
* @group user
7+
* @group capabilities
8+
*
9+
* @covers ::wp_roles
10+
*/
11+
class Tests_User_wpRoles extends WP_UnitTestCase {
12+
13+
/**
14+
* Original roles object.
15+
*
16+
* @var WP_Roles|null
17+
*/
18+
private $original_wp_roles;
19+
20+
public function set_up() {
21+
parent::set_up();
22+
23+
$this->original_wp_roles = $GLOBALS['wp_roles'] ?? null;
24+
}
25+
26+
public function tear_down() {
27+
$GLOBALS['wp_roles'] = $this->original_wp_roles;
28+
29+
parent::tear_down();
30+
}
31+
32+
/**
33+
* Tests that the roles object is instantiated when the global is not set.
34+
*
35+
* @ticket 65819
36+
*/
37+
public function test_should_instantiate_roles_when_global_is_not_set() {
38+
unset( $GLOBALS['wp_roles'] );
39+
40+
$roles = wp_roles();
41+
42+
$this->assertInstanceOf( WP_Roles::class, $roles, 'A WP_Roles instance should be returned.' );
43+
$this->assertSame( $roles, $GLOBALS['wp_roles'], 'The instance should be stored in the global.' );
44+
}
45+
46+
/**
47+
* Tests that an existing roles object is returned as is.
48+
*
49+
* @ticket 65819
50+
*/
51+
public function test_should_return_existing_roles_instance() {
52+
$roles = new WP_Roles();
53+
$GLOBALS['wp_roles'] = $roles;
54+
55+
$this->assertSame( $roles, wp_roles(), 'The existing instance should be returned.' );
56+
$this->assertSame( $roles, $GLOBALS['wp_roles'], 'The global should not be replaced.' );
57+
}
58+
59+
/**
60+
* Tests that a global which is not a roles object is replaced.
61+
*
62+
* @ticket 65819
63+
*
64+
* @dataProvider data_values_other_than_roles_instance
65+
*
66+
* @param mixed $value Value the global was overwritten with.
67+
*/
68+
public function test_should_replace_global_which_is_not_roles_instance( $value ) {
69+
$GLOBALS['wp_roles'] = $value;
70+
71+
$roles = wp_roles();
72+
73+
$this->assertInstanceOf( WP_Roles::class, $roles, 'A WP_Roles instance should be returned.' );
74+
$this->assertSame( $roles, $GLOBALS['wp_roles'], 'The instance should be stored in the global.' );
75+
}
76+
77+
/**
78+
* Data provider.
79+
*
80+
* @return array[]
81+
*/
82+
public static function data_values_other_than_roles_instance() {
83+
return array(
84+
'null' => array( null ),
85+
'false' => array( false ),
86+
'true' => array( true ),
87+
'zero' => array( 0 ),
88+
'an empty string' => array( '' ),
89+
'a role name' => array( 'administrator' ),
90+
'an empty array' => array( array() ),
91+
'an array' => array( array( 'administrator' => array() ) ),
92+
'another object' => array( new stdClass() ),
93+
);
94+
}
95+
}

0 commit comments

Comments
 (0)