1293b1d5713a9cb92a6dd54c95dc0ada66560f8d
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Context / UserAspectTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Context;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException;
20 use TYPO3\CMS\Core\Context\UserAspect;
21 use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 class UserAspectTest extends UnitTestCase
25 {
26 /**
27 * @test
28 */
29 public function getterReturnsProperDefaultValues()
30 {
31 $subject = new UserAspect(null, null);
32 $this->assertEquals(0, $subject->get('id'));
33 $this->assertEquals('', $subject->get('username'));
34 $this->assertFalse($subject->get('isLoggedIn'));
35 $this->assertEquals([], $subject->get('groupIds'));
36 $this->assertEquals([], $subject->get('groupNames'));
37 }
38
39 /**
40 * @test
41 */
42 public function getterReturnsValidUserId()
43 {
44 $user = new FrontendUserAuthentication();
45 $user->user = [
46 'uid' => 13
47 ];
48 $subject = new UserAspect($user);
49 $this->assertEquals(13, $subject->get('id'));
50 }
51
52 /**
53 * @test
54 */
55 public function getterReturnsValidUsername()
56 {
57 $user = new FrontendUserAuthentication();
58 $user->user = [
59 'uid' => 13,
60 'username' => 'Teddy'
61 ];
62 $subject = new UserAspect($user);
63 $this->assertEquals('Teddy', $subject->get('username'));
64 }
65
66 /**
67 * @test
68 */
69 public function isLoggedInReturnsFalseOnFrontendUserWithoutUserGroup()
70 {
71 $user = new FrontendUserAuthentication();
72 $user->user = [
73 'uid' => 13
74 ];
75 $subject = new UserAspect($user);
76 $this->assertFalse($subject->isLoggedIn());
77 }
78
79 /**
80 * @test
81 */
82 public function isLoggedInReturnsTrueOnFrontendUserWithUserGroup()
83 {
84 $user = new FrontendUserAuthentication();
85 $user->user = [
86 'uid' => 13
87 ];
88 $user->groupData['uid'] = [1, 5, 7];
89 $subject = new UserAspect($user);
90 $this->assertTrue($subject->isLoggedIn());
91 }
92
93 /**
94 * @test
95 */
96 public function isLoggedInReturnsTrueOnBackendUserWithId()
97 {
98 $user = new BackendUserAuthentication();
99 $user->user = [
100 'uid' => 13
101 ];
102 $subject = new UserAspect($user);
103 $this->assertTrue($subject->isLoggedIn());
104 }
105
106 /**
107 * @test
108 */
109 public function getGroupIdsReturnsFrontendUserGroups()
110 {
111 $user = new FrontendUserAuthentication();
112 $user->user = [
113 'uid' => 13
114 ];
115 $user->groupData['uid'] = [23, 54];
116 $subject = new UserAspect($user);
117 $this->assertEquals([0, -2, 23, 54], $subject->getGroupIds());
118 }
119
120 /**
121 * @test
122 */
123 public function getGroupIdsReturnsOverriddenGroups()
124 {
125 $user = new FrontendUserAuthentication();
126 // Not used, because overridden with 33
127 $user->groupData['uid'] = [23, 54];
128 $subject = new UserAspect($user, [33]);
129 $this->assertEquals([33], $subject->getGroupIds());
130 }
131
132 public function isUserOrGroupSetDataProvider()
133 {
134 return [
135 'Not logged in: no id or group set' => [
136 0,
137 null,
138 null,
139 false
140 ],
141 'only valid user id' => [
142 13,
143 null,
144 null,
145 true
146 ],
147 'valid user and overridden group' => [
148 13,
149 null,
150 [33],
151 true
152 ],
153 'no user and overridden group' => [
154 0,
155 null,
156 [33],
157 true
158 ],
159 'valid user, default groups and overridden group' => [
160 13,
161 [23],
162 [33],
163 true
164 ],
165 'no user, default groups and overridden group' => [
166 0,
167 [23],
168 [33],
169 true
170 ],
171 'Not logged in: no user, and classic group structure' => [
172 0,
173 null,
174 [0, -1],
175 false
176 ],
177 ];
178 }
179
180 /**
181 * @test
182 * @dataProvider isUserOrGroupSetDataProvider
183 * @param $userId
184 * @param $userGroups
185 * @param $overriddenGroups
186 * @param bool $expectedResult
187 */
188 public function isUserOrGroupSetChecksForValidUser($userId, $userGroups, $overriddenGroups, $expectedResult)
189 {
190 $user = new FrontendUserAuthentication();
191 if ($userId) {
192 $user->user['uid'] = $userId;
193 }
194 $user->groupData['uid'] = $userGroups;
195 $subject = new UserAspect($user, $overriddenGroups);
196 $this->assertEquals($expectedResult, $subject->isUserOrGroupSet());
197 }
198
199 /**
200 * @test
201 */
202 public function getThrowsExceptionOnInvalidArgument()
203 {
204 $this->expectException(AspectPropertyNotFoundException::class);
205 $subject = new UserAspect();
206 $subject->get('football');
207 }
208 }