‪TYPO3CMS  9.5
UserAspectTest.php
Go to the documentation of this file.
1 <?php
2 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 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
24 class ‪UserAspectTest extends UnitTestCase
25 {
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 
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 
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 
70  {
71  $user = new ‪FrontendUserAuthentication();
72  $user->user = [
73  'uid' => 13
74  ];
75  $subject = new ‪UserAspect($user);
76  $this->assertFalse($subject->isLoggedIn());
77  }
78 
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 
97  {
98  $user = new ‪BackendUserAuthentication();
99  $user->user = [
100  'uid' => 13
101  ];
102  $subject = new ‪UserAspect($user);
103  $this->assertTrue($subject->isLoggedIn());
104  }
105 
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 
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 
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 
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 
203  {
204  $this->expectException(AspectPropertyNotFoundException::class);
205  $this->expectExceptionCode(1529996567);
206  $subject = new ‪UserAspect();
207  $subject->get('football');
208  }
209 }
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getGroupIdsReturnsFrontendUserGroups
‪getGroupIdsReturnsFrontendUserGroups()
Definition: UserAspectTest.php:109
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\isLoggedInReturnsTrueOnBackendUserWithId
‪isLoggedInReturnsTrueOnBackendUserWithId()
Definition: UserAspectTest.php:96
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest
Definition: UserAspectTest.php:25
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getterReturnsValidUserId
‪getterReturnsValidUserId()
Definition: UserAspectTest.php:42
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\isLoggedInReturnsFalseOnFrontendUserWithoutUserGroup
‪isLoggedInReturnsFalseOnFrontendUserWithoutUserGroup()
Definition: UserAspectTest.php:69
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\isUserOrGroupSetChecksForValidUser
‪isUserOrGroupSetChecksForValidUser($userId, $userGroups, $overriddenGroups, $expectedResult)
Definition: UserAspectTest.php:188
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getGroupIdsReturnsOverriddenGroups
‪getGroupIdsReturnsOverriddenGroups()
Definition: UserAspectTest.php:123
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getterReturnsProperDefaultValues
‪getterReturnsProperDefaultValues()
Definition: UserAspectTest.php:29
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:28
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getterReturnsValidUsername
‪getterReturnsValidUsername()
Definition: UserAspectTest.php:55
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\getThrowsExceptionOnInvalidArgument
‪getThrowsExceptionOnInvalidArgument()
Definition: UserAspectTest.php:202
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:36
‪TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException
Definition: AspectPropertyNotFoundException.php:24
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\isLoggedInReturnsTrueOnFrontendUserWithUserGroup
‪isLoggedInReturnsTrueOnFrontendUserWithUserGroup()
Definition: UserAspectTest.php:82
‪TYPO3\CMS\Core\Tests\Unit\Context
Definition: ContextTest.php:3
‪TYPO3\CMS\Core\Tests\Unit\Context\UserAspectTest\isUserOrGroupSetDataProvider
‪isUserOrGroupSetDataProvider()
Definition: UserAspectTest.php:132