‪TYPO3CMS  10.4
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 
28 class ‪DatabaseSessionBackendTest extends FunctionalTestCase
29 {
33  protected ‪$subject;
34 
38  protected ‪$testSessionRecord = [
39  // DatabaseSessionBackend::hash('randomSessionId') with encryption key 12345
40  'ses_id' => '76898588caa1baee7984f4dc8adfed3b',
41  'ses_userid' => 1,
42  // serialize(['foo' => 'bar', 'boo' => 'far'])
43  'ses_data' => 'a:2:{s:3:"foo";s:3:"bar";s:3:"boo";s:3:"far";}',
44  ];
45 
49  protected function ‪setUp(): void
50  {
51  parent::setUp();
52  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';
53 
54  $this->subject = new ‪DatabaseSessionBackend();
55  $this->subject->initialize('default', [
56  'table' => 'fe_sessions',
57  'has_anonymous' => true,
58  ]);
59  }
60 
64  public function ‪canValidateSessionBackend()
65  {
66  $this->subject->validateConfiguration();
67  }
68 
73  public function ‪sessionDataIsStoredProperly()
74  {
75  $record = $this->subject->set('randomSessionId', $this->testSessionRecord);
76 
77  $expected = array_merge($this->testSessionRecord, ['ses_tstamp' => ‪$GLOBALS['EXEC_TIME']]);
78 
79  self::assertEquals($record, $expected);
80  self::assertSame($expected['ses_data'], $this->subject->get('randomSessionId')['ses_data']);
81  self::assertSame($expected['ses_userid'], (int)$this->subject->get('randomSessionId')['ses_userid']);
82  }
83 
88  {
89  $record = $this->subject->set('randomSessionId', array_merge($this->testSessionRecord, ['ses_anonymous' => 1]));
90 
91  $expected = array_merge($this->testSessionRecord, ['ses_anonymous' => 1, 'ses_tstamp' => ‪$GLOBALS['EXEC_TIME']]);
92 
93  self::assertEquals($record, $expected);
94  self::assertSame($expected['ses_data'], $this->subject->get('randomSessionId')['ses_data']);
95  self::assertSame($expected['ses_userid'], (int)$this->subject->get('randomSessionId')['ses_userid']);
96  }
97 
103  {
104  $this->expectException(SessionNotFoundException::class);
105  $this->expectExceptionCode(1481885483);
106  $this->subject->get('IDoNotExist');
107  }
108 
113  public function ‪mergeSessionDataWithNewData()
114  {
115  $this->subject->set('randomSessionId', $this->testSessionRecord);
116 
117  $updateData = [
118  'ses_data' => serialize(['foo' => 'baz', 'idontwantto' => 'set the world on fire']),
119  'ses_tstamp' => ‪$GLOBALS['EXEC_TIME']
120  ];
121  $expectedMergedData = array_merge($this->testSessionRecord, $updateData);
122  $this->subject->update('randomSessionId', $updateData);
123  $fetchedRecord = $this->subject->get('randomSessionId');
124  self::assertSame($expectedMergedData['ses_data'], $fetchedRecord['ses_data']);
125  self::assertSame($expectedMergedData['ses_userid'], (int)$fetchedRecord['ses_userid']);
126  }
127 
132  public function ‪existingSessionMustNotBeOverridden()
133  {
134  $this->expectException(SessionNotCreatedException::class);
135  $this->expectExceptionCode(1481895005);
136 
137  $this->subject->set('randomSessionId', $this->testSessionRecord);
138 
139  $newData = array_merge($this->testSessionRecord, ['ses_data' => serialize(['foo' => 'baz', 'idontwantto' => 'set the world on fire'])]);
140  $this->subject->set('randomSessionId', $newData);
141  }
142 
147  public function ‪cannotChangeSessionId()
148  {
149  $this->subject->set('randomSessionId', $this->testSessionRecord);
150 
151  $newSessionId = 'newRandomSessionId';
152  $newData = ‪$this->testSessionRecord;
153  $newData['ses_id'] = $newSessionId;
154 
155  // old session id has to exist, no exception must be thrown at this point
156  $this->subject->get('randomSessionId');
157 
158  // Change session id
159  $this->subject->update('randomSessionId', $newData);
160 
161  // no session with key newRandomSessionId should exist
162  $this->expectException(SessionNotFoundException::class);
163  $this->expectExceptionCode(1481885483);
164  $this->subject->get('newRandomSessionId');
165  }
166 
171  public function ‪sessionGetsDestroyed()
172  {
173  $this->subject->set('randomSessionId', $this->testSessionRecord);
174 
175  // Remove session
176  self::assertTrue($this->subject->remove('randomSessionId'));
177 
178  // Check if session was really removed
179  $this->expectException(SessionNotFoundException::class);
180  $this->expectExceptionCode(1481885483);
181  $this->subject->get('randomSessionId');
182  }
183 
188  public function ‪canLoadAllSessions()
189  {
190  $this->subject->set('randomSessionId', $this->testSessionRecord);
191  $this->subject->set('randomSessionId2', $this->testSessionRecord);
192 
193  // Check if session was really removed
194  self::assertEquals(2, count($this->subject->getAll()));
195  }
196 
200  public function ‪canCollectGarbage()
201  {
202  ‪$GLOBALS['EXEC_TIME'] = 150;
203  $authenticatedSession = array_merge($this->testSessionRecord, ['ses_id' => 'authenticatedSession']);
204  $anonymousSession = array_merge($this->testSessionRecord, ['ses_id' => 'anonymousSession', 'ses_anonymous' => 1]);
205 
206  $this->subject->set('authenticatedSession', $authenticatedSession);
207  $this->subject->set('anonymousSession', $anonymousSession);
208 
209  // Assert that we set authenticated session correctly
210  self::assertSame(
211  $authenticatedSession['ses_data'],
212  $this->subject->get('authenticatedSession')['ses_data']
213  );
214  self::assertSame(
215  $authenticatedSession['ses_userid'],
216  (int)$this->subject->get('authenticatedSession')['ses_userid']
217  );
218 
219  // assert that we set anonymous session correctly
220  self::assertSame(
221  $anonymousSession['ses_data'],
222  $this->subject->get('anonymousSession')['ses_data']
223  );
224 
225  // Run the garbage collection
226  ‪$GLOBALS['EXEC_TIME'] = 200;
227  // 150 + 10 < 200 but 150 + 60 >= 200
228  $this->subject->collectGarbage(60, 10);
229 
230  // Authenticated session should still be there
231  self::assertSame(
232  $authenticatedSession['ses_data'],
233  $this->subject->get('authenticatedSession')['ses_data']
234  );
235  self::assertSame(
236  $authenticatedSession['ses_userid'],
237  (int)$this->subject->get('authenticatedSession')['ses_userid']
238  );
239 
240  // Non-authenticated session should be removed
241  $this->expectException(SessionNotFoundException::class);
242  $this->expectExceptionCode(1481885483);
243  $this->subject->get('anonymousSession');
244  }
245 
249  public function ‪canPartiallyUpdateAfterGet()
250  {
251  $updatedRecord = array_merge(
252  $this->testSessionRecord,
253  ['ses_tstamp' => ‪$GLOBALS['EXEC_TIME']]
254  );
255  $sessionId = 'randomSessionId';
256  $this->subject->set($sessionId, $this->testSessionRecord);
257  $this->subject->update($sessionId, []);
258  self::assertSame($updatedRecord['ses_data'], $this->subject->get($sessionId)['ses_data']);
259  }
260 }
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\throwExceptionOnNonExistingSessionId
‪throwExceptionOnNonExistingSessionId()
Definition: DatabaseSessionBackendTest.php:100
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\setUp
‪setUp()
Definition: DatabaseSessionBackendTest.php:47
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\canPartiallyUpdateAfterGet
‪canPartiallyUpdateAfterGet()
Definition: DatabaseSessionBackendTest.php:247
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\mergeSessionDataWithNewData
‪mergeSessionDataWithNewData()
Definition: DatabaseSessionBackendTest.php:111
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\sessionGetsDestroyed
‪sessionGetsDestroyed()
Definition: DatabaseSessionBackendTest.php:169
‪TYPO3\CMS\Core\Session\Backend\Exception\SessionNotCreatedException
Definition: SessionNotCreatedException.php:24
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\anonymousSessionDataIsStoredProperly
‪anonymousSessionDataIsStoredProperly()
Definition: DatabaseSessionBackendTest.php:85
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\$subject
‪DatabaseSessionBackend $subject
Definition: DatabaseSessionBackendTest.php:32
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\canCollectGarbage
‪canCollectGarbage()
Definition: DatabaseSessionBackendTest.php:198
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\canLoadAllSessions
‪canLoadAllSessions()
Definition: DatabaseSessionBackendTest.php:186
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest
Definition: DatabaseSessionBackendTest.php:29
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\cannotChangeSessionId
‪cannotChangeSessionId()
Definition: DatabaseSessionBackendTest.php:145
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\$testSessionRecord
‪array $testSessionRecord
Definition: DatabaseSessionBackendTest.php:36
‪TYPO3\CMS\Core\Session\Backend\DatabaseSessionBackend
Definition: DatabaseSessionBackend.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\canValidateSessionBackend
‪canValidateSessionBackend()
Definition: DatabaseSessionBackendTest.php:62
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend
Definition: DatabaseSessionBackendTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\existingSessionMustNotBeOverridden
‪existingSessionMustNotBeOverridden()
Definition: DatabaseSessionBackendTest.php:130
‪TYPO3\CMS\Core\Tests\Functional\Session\Backend\DatabaseSessionBackendTest\sessionDataIsStoredProperly
‪sessionDataIsStoredProperly()
Definition: DatabaseSessionBackendTest.php:71
‪TYPO3\CMS\Core\Session\Backend\Exception\SessionNotFoundException
Definition: SessionNotFoundException.php:24