|
| 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