‪TYPO3CMS  ‪main
DatabaseSessionBackendTest.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 
23 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
24 
25 final class ‪DatabaseSessionBackendTest extends FunctionalTestCase
26 {
30  protected ‪$subject;
31 
35  protected ‪$testSessionRecord = [
36  // DatabaseSessionBackend::hash('randomSessionId') with encryption key 12345
37  'ses_id' => '21c0e911565a67315cdc384889c470fd291feafbfa62e31ecf7409430640bc7a',
38  'ses_userid' => 1,
39  // serialize(['foo' => 'bar', 'boo' => 'far'])
40  'ses_data' => 'a:2:{s:3:"foo";s:3:"bar";s:3:"boo";s:3:"far";}',
41  ];
42 
46  protected function ‪setUp(): void
47  {
48  parent::setUp();
49  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';
50 
51  $this->subject = new ‪DatabaseSessionBackend();
52  $this->subject->initialize('default', [
53  'table' => 'fe_sessions',
54  'has_anonymous' => true,
55  ]);
56  }
57 
61  public function ‪canValidateSessionBackend(): void
62  {
63  $this->subject->validateConfiguration();
64  }
65 
69  public function ‪sessionDataIsStoredProperly(): void
70  {
71  ‪$record = $this->subject->set('randomSessionId', $this->testSessionRecord);
72 
73  $expected = array_merge($this->testSessionRecord, ['ses_tstamp' => ‪$GLOBALS['EXEC_TIME']]);
74 
75  self::assertEquals($expected, ‪$record);
76  self::assertSame($expected['ses_data'], $this->subject->get('randomSessionId')['ses_data']);
77  self::assertSame($expected['ses_userid'], (int)$this->subject->get('randomSessionId')['ses_userid']);
78  }
79 
83  public function ‪anonymousSessionDataIsStoredProperly(): void
84  {
85  ‪$record = $this->subject->set('randomSessionId', array_merge($this->testSessionRecord, ['ses_userid' => 0]));
86 
87  $expected = array_merge($this->testSessionRecord, ['ses_userid' => 0, 'ses_tstamp' => ‪$GLOBALS['EXEC_TIME']]);
88 
89  self::assertEquals($expected, ‪$record);
90  self::assertSame($expected['ses_data'], $this->subject->get('randomSessionId')['ses_data']);
91  self::assertSame($expected['ses_userid'], (int)$this->subject->get('randomSessionId')['ses_userid']);
92  }
93 
97  public function ‪throwExceptionOnNonExistingSessionId(): void
98  {
99  $this->expectException(SessionNotFoundException::class);
100  $this->expectExceptionCode(1481885483);
101  $this->subject->get('IDoNotExist');
102  }
103 
107  public function ‪mergeSessionDataWithNewData(): void
108  {
109  $this->subject->set('randomSessionId', $this->testSessionRecord);
110 
111  $updateData = [
112  'ses_data' => serialize(['foo' => 'baz', 'idontwantto' => 'set the world on fire']),
113  'ses_tstamp' => ‪$GLOBALS['EXEC_TIME'],
114  ];
115  $expectedMergedData = array_merge($this->testSessionRecord, $updateData);
116  $this->subject->update('randomSessionId', $updateData);
117  $fetchedRecord = $this->subject->get('randomSessionId');
118  self::assertSame($expectedMergedData['ses_data'], $fetchedRecord['ses_data']);
119  self::assertSame($expectedMergedData['ses_userid'], (int)$fetchedRecord['ses_userid']);
120  }
121 
125  public function ‪existingSessionMustNotBeOverridden(): void
126  {
127  $this->expectException(SessionNotCreatedException::class);
128  $this->expectExceptionCode(1481895005);
129 
130  $this->subject->set('randomSessionId', $this->testSessionRecord);
131 
132  $newData = array_merge($this->testSessionRecord, ['ses_data' => serialize(['foo' => 'baz', 'idontwantto' => 'set the world on fire'])]);
133  $this->subject->set('randomSessionId', $newData);
134  }
135 
139  public function ‪cannotChangeSessionId(): void
140  {
141  $this->subject->set('randomSessionId', $this->testSessionRecord);
142 
143  $newSessionId = 'newRandomSessionId';
144  $newData = ‪$this->testSessionRecord;
145  $newData['ses_id'] = $newSessionId;
146 
147  // old session id has to exist, no exception must be thrown at this point
148  $this->subject->get('randomSessionId');
149 
150  // Change session id
151  $this->subject->update('randomSessionId', $newData);
152 
153  // no session with key newRandomSessionId should exist
154  $this->expectException(SessionNotFoundException::class);
155  $this->expectExceptionCode(1481885483);
156  $this->subject->get('newRandomSessionId');
157  }
158 
162  public function ‪sessionGetsDestroyed(): void
163  {
164  $this->subject->set('randomSessionId', $this->testSessionRecord);
165 
166  // Remove session
167  self::assertTrue($this->subject->remove('randomSessionId'));
168 
169  // Check if session was really removed
170  $this->expectException(SessionNotFoundException::class);
171  $this->expectExceptionCode(1481885483);
172  $this->subject->get('randomSessionId');
173  }
174 
178  public function ‪canLoadAllSessions(): void
179  {
180  $this->subject->set('randomSessionId', $this->testSessionRecord);
181  $this->subject->set('randomSessionId2', $this->testSessionRecord);
182 
183  // Check if session was really removed
184  self::assertCount(2, $this->subject->getAll());
185  }
186 
190  public function ‪canCollectGarbage(): void
191  {
192  ‪$GLOBALS['EXEC_TIME'] = 150;
193  $authenticatedSession = array_merge($this->testSessionRecord, ['ses_id' => 'authenticatedSession']);
194  $anonymousSession = array_merge($this->testSessionRecord, ['ses_id' => 'anonymousSession', 'ses_userid' => 0]);
195 
196  $this->subject->set('authenticatedSession', $authenticatedSession);
197  $this->subject->set('anonymousSession', $anonymousSession);
198 
199  // Assert that we set authenticated session correctly
200  self::assertSame(
201  $authenticatedSession['ses_data'],
202  $this->subject->get('authenticatedSession')['ses_data']
203  );
204  self::assertSame(
205  $authenticatedSession['ses_userid'],
206  (int)$this->subject->get('authenticatedSession')['ses_userid']
207  );
208 
209  // assert that we set anonymous session correctly
210  self::assertSame(
211  $anonymousSession['ses_data'],
212  $this->subject->get('anonymousSession')['ses_data']
213  );
214 
215  // Run the garbage collection
216  ‪$GLOBALS['EXEC_TIME'] = 200;
217  // 150 + 10 < 200 but 150 + 60 >= 200
218  $this->subject->collectGarbage(60, 10);
219 
220  // Authenticated session should still be there
221  self::assertSame(
222  $authenticatedSession['ses_data'],
223  $this->subject->get('authenticatedSession')['ses_data']
224  );
225  self::assertSame(
226  $authenticatedSession['ses_userid'],
227  (int)$this->subject->get('authenticatedSession')['ses_userid']
228  );
229 
230  // Non-authenticated session should be removed
231  $this->expectException(SessionNotFoundException::class);
232  $this->expectExceptionCode(1481885483);
233  $this->subject->get('anonymousSession');
234  }
235 
239  public function ‪canPartiallyUpdateAfterGet(): void
240  {
241  $updatedRecord = array_merge(
242  $this->testSessionRecord,
243  ['ses_tstamp' => ‪$GLOBALS['EXEC_TIME']]
244  );
245  $sessionId = 'randomSessionId';
246  $this->subject->set($sessionId, $this->testSessionRecord);
247  $this->subject->update($sessionId, []);
248  self::assertSame($updatedRecord['ses_data'], $this->subject->get($sessionId)['ses_data']);
249  }
250 }
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\throwExceptionOnNonExistingSessionId
‪throwExceptionOnNonExistingSessionId()
Definition: DatabaseSessionBackendTest.php:95
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\setUp
‪setUp()
Definition: DatabaseSessionBackendTest.php:44
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\canPartiallyUpdateAfterGet
‪canPartiallyUpdateAfterGet()
Definition: DatabaseSessionBackendTest.php:237
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\mergeSessionDataWithNewData
‪mergeSessionDataWithNewData()
Definition: DatabaseSessionBackendTest.php:105
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\sessionGetsDestroyed
‪sessionGetsDestroyed()
Definition: DatabaseSessionBackendTest.php:160
‪TYPO3\CMS\Core\Session\Backend\Exception\SessionNotCreatedException
Definition: SessionNotCreatedException.php:23
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\anonymousSessionDataIsStoredProperly
‪anonymousSessionDataIsStoredProperly()
Definition: DatabaseSessionBackendTest.php:81
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\$subject
‪DatabaseSessionBackend $subject
Definition: DatabaseSessionBackendTest.php:29
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\canCollectGarbage
‪canCollectGarbage()
Definition: DatabaseSessionBackendTest.php:188
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\canLoadAllSessions
‪canLoadAllSessions()
Definition: DatabaseSessionBackendTest.php:176
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest
Definition: DatabaseSessionBackendTest.php:26
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\cannotChangeSessionId
‪cannotChangeSessionId()
Definition: DatabaseSessionBackendTest.php:137
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\$testSessionRecord
‪array $testSessionRecord
Definition: DatabaseSessionBackendTest.php:33
‪TYPO3\CMS\Core\Session\Backend\DatabaseSessionBackend
Definition: DatabaseSessionBackend.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\canValidateSessionBackend
‪canValidateSessionBackend()
Definition: DatabaseSessionBackendTest.php:59
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend
Definition: DatabaseSessionBackendTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\existingSessionMustNotBeOverridden
‪existingSessionMustNotBeOverridden()
Definition: DatabaseSessionBackendTest.php:123
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\sessionDataIsStoredProperly
‪sessionDataIsStoredProperly()
Definition: DatabaseSessionBackendTest.php:67
‪TYPO3\CMS\Core\Session\Backend\Exception\SessionNotFoundException
Definition: SessionNotFoundException.php:23