85fe1bbf36dec068aff075ad6145d7c5f078ad9d
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Session / UserSessionTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Session;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Session\UserSession;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\user\RoleInterface;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\Session\UserSession
12  * @group Session
13  */
14 class UserSessionTest extends UnitTestCase {
15
16   /**
17    * The user sessions used in the test
18    *
19    * @var \Drupal\Core\Session\AccountInterface[]
20    */
21   protected $users = [];
22
23   /**
24    * Provides test data for getHasPermission().
25    *
26    * @return array
27    */
28   public function providerTestHasPermission() {
29     $data = [];
30     $data[] = ['example permission', ['user_one', 'user_two'], ['user_last']];
31     $data[] = ['another example permission', ['user_two'], ['user_one', 'user_last']];
32     $data[] = ['final example permission', [], ['user_one', 'user_two', 'user_last']];
33
34     return $data;
35   }
36
37   /**
38    * Setups a user session for the test.
39    *
40    * @param array $rids
41    *   The rids of the user.
42    * @param bool $authenticated
43    *   TRUE if it is an authenticated user.
44    *
45    * @return \Drupal\Core\Session\AccountInterface
46    *   The created user session.
47    */
48   protected function createUserSession(array $rids = [], $authenticated = FALSE) {
49     array_unshift($rids, $authenticated ? RoleInterface::AUTHENTICATED_ID : RoleInterface::ANONYMOUS_ID);
50     return new UserSession(['roles' => $rids]);
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   protected function setUp() {
57     parent::setUp();
58
59     $roles = [];
60     $roles['role_one'] = $this->getMockBuilder('Drupal\user\Entity\Role')
61       ->disableOriginalConstructor()
62       ->setMethods(['hasPermission'])
63       ->getMock();
64     $roles['role_one']->expects($this->any())
65       ->method('hasPermission')
66       ->will($this->returnValueMap([
67         ['example permission', TRUE],
68         ['another example permission', FALSE],
69         ['last example permission', FALSE],
70       ]));
71
72     $roles['role_two'] = $this->getMockBuilder('Drupal\user\Entity\Role')
73       ->disableOriginalConstructor()
74       ->setMethods(['hasPermission'])
75       ->getMock();
76     $roles['role_two']->expects($this->any())
77       ->method('hasPermission')
78       ->will($this->returnValueMap([
79         ['example permission', TRUE],
80         ['another example permission', TRUE],
81         ['last example permission', FALSE],
82       ]));
83
84     $roles['anonymous'] = $this->getMockBuilder('Drupal\user\Entity\Role')
85       ->disableOriginalConstructor()
86       ->setMethods(['hasPermission'])
87       ->getMock();
88     $roles['anonymous']->expects($this->any())
89       ->method('hasPermission')
90       ->will($this->returnValueMap([
91         ['example permission', FALSE],
92         ['another example permission', FALSE],
93         ['last example permission', FALSE],
94       ]));
95
96     $role_storage = $this->getMockBuilder('Drupal\user\RoleStorage')
97       ->disableOriginalConstructor()
98       ->setMethods(['loadMultiple'])
99       ->getMock();
100     $role_storage->expects($this->any())
101       ->method('loadMultiple')
102       ->will($this->returnValueMap([
103         [[], []],
104         [NULL, $roles],
105         [['anonymous'], [$roles['anonymous']]],
106         [['anonymous', 'role_one'], [$roles['role_one']]],
107         [['anonymous', 'role_two'], [$roles['role_two']]],
108         [['anonymous', 'role_one', 'role_two'], [$roles['role_one'], $roles['role_two']]],
109       ]));
110
111     $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
112     $entity_manager->expects($this->any())
113       ->method('getStorage')
114       ->with($this->equalTo('user_role'))
115       ->will($this->returnValue($role_storage));
116     $container = new ContainerBuilder();
117     $container->set('entity.manager', $entity_manager);
118     \Drupal::setContainer($container);
119
120     $this->users['user_one'] = $this->createUserSession(['role_one']);
121     $this->users['user_two'] = $this->createUserSession(['role_one', 'role_two']);
122     $this->users['user_three'] = $this->createUserSession(['role_two'], TRUE);
123     $this->users['user_last'] = $this->createUserSession();
124   }
125
126   /**
127    * Tests the has permission method.
128    *
129    * @param string $permission
130    *   The permission to check.
131    * @param \Drupal\Core\Session\AccountInterface[] $sessions_with_access
132    *   The users with access.
133    * @param \Drupal\Core\Session\AccountInterface[] $sessions_without_access
134    *   The users without access.
135    *
136    * @dataProvider providerTestHasPermission
137    *
138    * @see \Drupal\Core\Session\UserSession::hasPermission()
139    */
140   public function testHasPermission($permission, array $sessions_with_access, array $sessions_without_access) {
141     foreach ($sessions_with_access as $name) {
142       $this->assertTrue($this->users[$name]->hasPermission($permission));
143     }
144     foreach ($sessions_without_access as $name) {
145       $this->assertFalse($this->users[$name]->hasPermission($permission));
146     }
147   }
148
149   /**
150    * Tests the method getRoles exclude or include locked roles based in param.
151    *
152    * @covers ::getRoles
153    * @todo Move roles constants to a class/interface
154    */
155   public function testUserGetRoles() {
156     $this->assertEquals([RoleInterface::AUTHENTICATED_ID, 'role_two'], $this->users['user_three']->getRoles());
157     $this->assertEquals(['role_two'], $this->users['user_three']->getRoles(TRUE));
158   }
159
160 }