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