‪TYPO3CMS  ‪main
FrontendUserAuthenticationTest.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 
20 use PHPUnit\Framework\Attributes\Test;
21 use Psr\Log\NullLogger;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
29 final class ‪FrontendUserAuthenticationTest extends UnitTestCase
30 {
31  protected bool ‪$resetSingletonInstances = true;
32 
37  #[Test]
38  public function ‪canSetAndUnsetSessionKey(): void
39  {
40  $uniqueSessionId = ‪StringUtility::getUniqueId('test');
41 
42  $sessionRecord = [
43  'ses_id' => $uniqueSessionId . '--not-checked--',
44  'ses_data' => serialize(['foo' => 'bar']),
45  'ses_userid' => 0,
46  'ses_iplock' => '[DISABLED]',
47  ];
48  $userSession = ‪UserSession::createFromRecord($sessionRecord['ses_id'], $sessionRecord);
49 
50  // Main session backend setup
51  $userSessionManagerMock = $this->createMock(UserSessionManager::class);
52  $userSessionManagerMock->method('createFromRequestOrAnonymous')->with(self::anything())->willReturn($userSession);
53  // Verify new session id is generated
54  $userSessionManagerMock->method('createAnonymousSession')->willReturn(‪UserSession::createNonFixated('newSessionId'));
55  // set() and update() shouldn't be called since no session cookie is set
56  // remove() should be called with given session id
57  $userSessionManagerMock->expects(self::once())->method('isSessionPersisted')->with(self::anything())->willReturn(true);
58  $userSessionManagerMock->expects(self::once())->method('removeSession')->with(self::anything());
59 
60  // set() and update() shouldn't be called since no session cookie is set
61  $userSessionManagerMock->expects(self::never())->method('elevateToFixatedUserSession')->with(self::anything());
62  $userSessionManagerMock->expects(self::never())->method('updateSession')->with(self::anything());
63 
64  $subject = new ‪FrontendUserAuthentication();
65  $subject->initializeUserSessionManager($userSessionManagerMock);
66  $subject->setLogger(new NullLogger());
67  $subject->start(new ‪ServerRequest());
68  $subject->setSessionData('foo', 'bar');
69  $subject->removeSessionData();
70  self::assertNull($subject->getSessionData('someKey'));
71  }
72 
76  #[Test]
77  public function ‪canSetSessionDataForAnonymousUser(): void
78  {
79  $uniqueSessionId = ‪StringUtility::getUniqueId('test');
80  $currentTime = ‪$GLOBALS['EXEC_TIME'];
81 
82  // Main session backend setup
83  $userSession = ‪UserSession::createNonFixated($uniqueSessionId);
84  $userSessionManagerMock = $this->createMock(UserSessionManager::class);
85  $userSessionManagerMock->method('createFromRequestOrAnonymous')->withAnyParameters()->willReturn($userSession);
86  $userSessionManagerMock->method('createAnonymousSession')->withAnyParameters()->willReturn($userSession);
87  // Verify new session id is generated
88  // set() and update() shouldn't be called since no session cookie is set
89  // remove() should be called with given session id
90  $userSessionManagerMock->expects(self::once())->method('isSessionPersisted')->with(self::anything())->willReturn(true);
91  $userSessionManagerMock->expects(self::never())->method('removeSession')->with(self::anything());
92 
93  // set() and update() shouldn't be called since no session cookie is set
94  $userSessionManagerMock->expects(self::never())->method('elevateToFixatedUserSession')->with(self::anything());
95  $userSessionManagerMock->expects(self::once())->method('updateSession')->with(self::anything());
96 
97  // new session should be written
98  $sessionRecord = [
99  'ses_id' => 'newSessionId',
100  'ses_iplock' => '',
101  'ses_userid' => 0,
102  'ses_tstamp' => $currentTime,
103  'ses_data' => serialize(['foo' => 'bar']),
104  'ses_permanent' => 0,
105  ];
106  $userSessionToBePersisted = ‪UserSession::createFromRecord($uniqueSessionId, $sessionRecord, true);
107  $userSessionToBePersisted->set('foo', 'bar');
108  $userSessionManagerMock->expects(self::once())->method('updateSession')->with($userSessionToBePersisted);
109 
110  $subject = new ‪FrontendUserAuthentication();
111  $subject->initializeUserSessionManager($userSessionManagerMock);
112  $subject->setLogger(new NullLogger());
113  $subject->start(new ‪ServerRequest());
114  self::assertEmpty($subject->getSessionData($uniqueSessionId));
115  self::assertEmpty($subject->user);
116  $subject->setSessionData('foo', 'bar');
117  self::assertNotNull($subject->getSessionData('foo'));
118 
119  // Suppress "headers already sent" errors - phpunit does that internally already
120  $prev = error_reporting(0);
121  $subject->storeSessionData();
122  error_reporting($prev);
123  }
124 }
‪TYPO3\CMS\Frontend\Tests\Unit\Authentication\FrontendUserAuthenticationTest\canSetAndUnsetSessionKey
‪canSetAndUnsetSessionKey()
Definition: FrontendUserAuthenticationTest.php:38
‪TYPO3\CMS\Core\Session\UserSession\createNonFixated
‪static createNonFixated(string $identifier)
Definition: UserSession.php:243
‪TYPO3\CMS\Core\Session\UserSession
Definition: UserSession.php:45
‪TYPO3\CMS\Core\Session\UserSession\createFromRecord
‪static createFromRecord(string $id, array $record, bool $markAsNew=false)
Definition: UserSession.php:223
‪TYPO3\CMS\Frontend\Tests\Unit\Authentication
Definition: FrontendUserAuthenticationTest.php:18
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Frontend\Tests\Unit\Authentication\FrontendUserAuthenticationTest\canSetSessionDataForAnonymousUser
‪canSetSessionDataForAnonymousUser()
Definition: FrontendUserAuthenticationTest.php:77
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:33
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Frontend\Tests\Unit\Authentication\FrontendUserAuthenticationTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FrontendUserAuthenticationTest.php:31
‪TYPO3\CMS\Core\Session\UserSessionManager
Definition: UserSessionManager.php:46
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Frontend\Tests\Unit\Authentication\FrontendUserAuthenticationTest
Definition: FrontendUserAuthenticationTest.php:30