‪TYPO3CMS  10.4
UserAspectTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 class ‪UserAspectTest extends UnitTestCase
27 {
32  {
33  $subject = new ‪UserAspect(null, null);
34  self::assertEquals(0, $subject->get('id'));
35  self::assertEquals('', $subject->get('username'));
36  self::assertFalse($subject->get('isLoggedIn'));
37  self::assertEquals([], $subject->get('groupIds'));
38  self::assertEquals([], $subject->get('groupNames'));
39  }
40 
45  {
46  $subject = new ‪UserAspect(null, []);
47  self::assertEquals([], $subject->get('groupIds'));
48  $subject = new ‪UserAspect(null, [567]);
49  self::assertEquals([567], $subject->get('groupIds'));
50  }
51 
55  public function ‪getterReturnsValidUserId()
56  {
57  $user = new ‪FrontendUserAuthentication();
58  $user->user = [
59  'uid' => 13
60  ];
61  $subject = new ‪UserAspect($user);
62  self::assertEquals(13, $subject->get('id'));
63  }
64 
69  {
70  $user = new ‪FrontendUserAuthentication();
71  $user->user = [
72  'uid' => 13,
73  'username' => 'Teddy'
74  ];
75  $subject = new ‪UserAspect($user);
76  self::assertEquals('Teddy', $subject->get('username'));
77  }
78 
83  {
84  $user = new ‪FrontendUserAuthentication();
85  $user->user = [
86  'uid' => 13
87  ];
88  $subject = new ‪UserAspect($user);
89  self::assertTrue($subject->isLoggedIn());
90  }
91 
96  {
97  $user = new ‪FrontendUserAuthentication();
98  $user->user = [
99  'uid' => 13
100  ];
101  $user->groupData['uid'] = [1, 5, 7];
102  $subject = new ‪UserAspect($user);
103  self::assertTrue($subject->isLoggedIn());
104  }
105 
110  {
111  $user = new ‪BackendUserAuthentication();
112  $user->user = [
113  'uid' => 13
114  ];
115  $subject = new ‪UserAspect($user);
116  self::assertTrue($subject->isLoggedIn());
117  }
118 
123  {
124  $user = new ‪FrontendUserAuthentication();
125  $user->user = [
126  'uid' => 13
127  ];
128  $user->groupData['uid'] = [23, 54];
129  $subject = new ‪UserAspect($user);
130  self::assertEquals([0, -2, 23, 54], $subject->getGroupIds());
131  }
132 
137  {
138  $user = new ‪FrontendUserAuthentication();
139  // Not used, because overridden with 33
140  $user->groupData['uid'] = [23, 54];
141  $subject = new ‪UserAspect($user, [33]);
142  self::assertEquals([33], $subject->getGroupIds());
143  }
144 
146  {
147  return [
148  'Not logged in: no id or group set' => [
149  0,
150  null,
151  null,
152  false
153  ],
154  'only valid user id' => [
155  13,
156  null,
157  null,
158  true
159  ],
160  'valid user and overridden group' => [
161  13,
162  null,
163  [33],
164  true
165  ],
166  'no user and overridden group' => [
167  0,
168  null,
169  [33],
170  true
171  ],
172  'valid user, default groups and overridden group' => [
173  13,
174  [23],
175  [33],
176  true
177  ],
178  'no user, default groups and overridden group' => [
179  0,
180  [23],
181  [33],
182  true
183  ],
184  'Not logged in: no user, and classic group structure' => [
185  0,
186  null,
187  [0, -1],
188  false
189  ],
190  ];
191  }
192 
201  public function ‪isUserOrGroupSetChecksForValidUser($userId, $userGroups, $overriddenGroups, $expectedResult)
202  {
203  $user = new ‪FrontendUserAuthentication();
204  if ($userId) {
205  $user->user['uid'] = $userId;
206  }
207  $user->groupData['uid'] = $userGroups;
208  $subject = new ‪UserAspect($user, $overriddenGroups);
209  self::assertEquals($expectedResult, $subject->isUserOrGroupSet());
210  }
211 
216  {
217  $this->expectException(AspectPropertyNotFoundException::class);
218  $this->expectExceptionCode(1529996567);
219  $subject = new ‪UserAspect();
220  $subject->get('football');
221  }
222 }
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getGroupIdsReturnsFrontendUserGroups
‪getGroupIdsReturnsFrontendUserGroups()
Definition: UserAspectTest.php:122
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\isLoggedInReturnsTrueOnBackendUserWithId
‪isLoggedInReturnsTrueOnBackendUserWithId()
Definition: UserAspectTest.php:109
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest
Definition: UserAspectTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getterReturnsValidUserId
‪getterReturnsValidUserId()
Definition: UserAspectTest.php:55
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\isUserOrGroupSetChecksForValidUser
‪isUserOrGroupSetChecksForValidUser($userId, $userGroups, $overriddenGroups, $expectedResult)
Definition: UserAspectTest.php:201
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\alternativeGroupsAreAlwaysReturned
‪alternativeGroupsAreAlwaysReturned()
Definition: UserAspectTest.php:44
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\isLoggedInReturnsTrueOnFrontendUserWithoutUserGroup
‪isLoggedInReturnsTrueOnFrontendUserWithoutUserGroup()
Definition: UserAspectTest.php:82
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getGroupIdsReturnsOverriddenGroups
‪getGroupIdsReturnsOverriddenGroups()
Definition: UserAspectTest.php:136
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getterReturnsProperDefaultValues
‪getterReturnsProperDefaultValues()
Definition: UserAspectTest.php:31
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:30
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getterReturnsValidUsername
‪getterReturnsValidUsername()
Definition: UserAspectTest.php:68
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getThrowsExceptionOnInvalidArgument
‪getThrowsExceptionOnInvalidArgument()
Definition: UserAspectTest.php:215
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:38
‪TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException
Definition: AspectPropertyNotFoundException.php:26
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\isLoggedInReturnsTrueOnFrontendUserWithUserGroup
‪isLoggedInReturnsTrueOnFrontendUserWithUserGroup()
Definition: UserAspectTest.php:95
‪TYPO3\CMS\Core\Tests\Unit\Context
Definition: ContextTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\isUserOrGroupSetDataProvider
‪isUserOrGroupSetDataProvider()
Definition: UserAspectTest.php:145