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