2 declare(strict_types = 1);
22 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
46 'ses_id' =>
'21c0e911565a67315cdc384889c470fd291feafbfa62e31ecf7409430640bc7a',
49 'ses_data' =>
'a:2:{s:3:"foo";s:3:"bar";s:3:"boo";s:3:"far";}',
55 protected function setUp()
58 $GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'encryptionKey'] =
'12345';
60 if (!getenv(
'typo3TestingRedisHost')) {
61 $this->markTestSkipped(
'environment variable "typo3TestingRedisHost" must be set to run this test');
68 $redisHost = getenv(
'typo3TestingRedisHost');
70 $env = getenv(
'typo3TestingRedisPort');
71 $redisPort = is_string($env) ? (int)$env : 6379;
73 $this->redis = new \Redis();
74 $this->redis->connect($redisHost, $redisPort);
75 $this->redis->select(0);
77 $this->redis->flushDB();
80 $this->subject->initialize(
85 'hostname' => $redisHost
95 $this->expectException(SessionNotUpdatedException::class);
96 $this->expectExceptionCode(1484389971);
97 $this->subject->update(
'iSoNotExist', []);
105 $this->subject->validateConfiguration();
114 $record = $this->subject->set(
'randomSessionId', $this->testSessionRecord);
116 $expected = array_merge($this->testSessionRecord, [
'ses_tstamp' =>
$GLOBALS[
'EXEC_TIME']]);
118 $this->assertEquals($record, $expected);
119 $this->assertArraySubset($expected, $this->subject->get(
'randomSessionId'));
127 $record = $this->subject->set(
'randomSessionId', array_merge($this->testSessionRecord, [
'ses_anonymous' => 1]));
129 $expected = array_merge($this->testSessionRecord, [
'ses_anonymous' => 1,
'ses_tstamp' =>
$GLOBALS[
'EXEC_TIME']]);
131 $this->assertEquals($record, $expected);
132 $this->assertArraySubset($expected, $this->subject->get(
'randomSessionId'));
141 $this->expectException(SessionNotFoundException::class);
142 $this->expectExceptionCode(1481885583);
143 $this->subject->get(
'IDoNotExist');
152 $this->subject->set(
'randomSessionId', $this->testSessionRecord);
155 'ses_data' => serialize([
'foo' =>
'baz',
'idontwantto' =>
'set the world on fire']),
158 $expectedMergedData = array_merge($this->testSessionRecord, $updateData);
159 $this->subject->update(
'randomSessionId', $updateData);
160 $fetchedRecord = $this->subject->get(
'randomSessionId');
161 $this->assertArraySubset($expectedMergedData, $fetchedRecord);
174 'typo3_ses_default_' . sha1(
$GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'encryptionKey']) .
'_randomSessionId',
180 'ses_data' => serialize([
'foo' =>
'baz',
'idontwantto' =>
'set the world on fire']),
184 $this->subject->update(
'randomSessionId', $updateData);
185 $fetchedRecord = $this->subject->get(
'randomSessionId');
186 self::assertSame($expectedMergedData, $fetchedRecord);
195 $this->expectException(SessionNotCreatedException::class);
196 $this->expectExceptionCode(1481895647);
198 $this->subject->set(
'randomSessionId', $this->testSessionRecord);
200 $newData = array_merge($this->testSessionRecord, [
'ses_data' => serialize([
'foo' =>
'baz',
'idontwantto' =>
'set the world on fire'])]);
201 $this->subject->set(
'randomSessionId', $newData);
210 $this->subject->set(
'randomSessionId', $this->testSessionRecord);
212 $newSessionId =
'newRandomSessionId';
213 $newData = array_merge($this->testSessionRecord, [
'ses_id' => $newSessionId]);
216 $this->subject->get(
'randomSessionId');
219 $this->subject->update(
'randomSessionId', $newData);
222 $this->expectException(SessionNotFoundException::class);
223 $this->expectExceptionCode(1481885583);
224 $this->subject->get(
'newRandomSessionId');
233 $this->subject->set(
'randomSessionId', $this->testSessionRecord);
236 $this->assertTrue($this->subject->remove(
'randomSessionId'));
239 $this->expectException(SessionNotFoundException::class);
240 $this->expectExceptionCode(1481885583);
241 $this->subject->get(
'randomSessionId');
250 $this->subject->set(
'randomSessionId', $this->testSessionRecord);
251 $this->subject->set(
'randomSessionId2', $this->testSessionRecord);
254 $this->assertEquals(2, count($this->subject->getAll()));
263 $authenticatedSession = array_merge($this->testSessionRecord, [
'ses_id' =>
'authenticatedSession']);
264 $anonymousSession = array_merge($this->testSessionRecord, [
'ses_id' =>
'anonymousSession',
'ses_anonymous' => 1]);
266 $this->subject->set(
'authenticatedSession', $authenticatedSession);
267 $this->subject->set(
'anonymousSession', $anonymousSession);
271 $authenticatedSession[
'ses_data'],
272 $this->subject->get(
'authenticatedSession')[
'ses_data']
277 $anonymousSession[
'ses_data'],
278 $this->subject->get(
'anonymousSession')[
'ses_data']
284 $this->subject->collectGarbage(60, 10);
288 $authenticatedSession[
'ses_data'],
289 $this->subject->get(
'authenticatedSession')[
'ses_data']
293 $this->expectException(SessionNotFoundException::class);
294 $this->expectExceptionCode(1481885583);
295 $this->subject->get(
'anonymousSession');
303 $updatedRecord = array_merge(
304 $this->testSessionRecord,
307 $sessionId =
'randomSessionId';
308 $this->subject->set($sessionId, $this->testSessionRecord);
309 $this->subject->update($sessionId, []);
310 $this->assertArraySubset($updatedRecord, $this->subject->get($sessionId));