2 declare(strict_types = 1);
18 use Doctrine\DBAL\Statement;
19 use Prophecy\Argument;
20 use Psr\Log\NullLogger;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
55 $uniqueSessionId = $this->getUniqueId(
'test');
56 $_COOKIE[
'fe_typo_user'] = $uniqueSessionId;
59 $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
60 $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
61 $connectionPoolProphecy->getQueryBuilderForTable(
'fe_users')->willReturn($queryBuilderProphecy->reveal());
62 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
63 $expressionBuilderProphecy = $this->prophesize(ExpressionBuilder::class);
64 $queryBuilderProphecy->expr()->willReturn($expressionBuilderProphecy->reveal());
65 $compositeExpressionProphecy = $this->prophesize(CompositeExpression::class);
66 $expressionBuilderProphecy->andX(Argument::cetera())->willReturn($compositeExpressionProphecy->reveal());
67 $expressionBuilderProphecy->in(Argument::cetera())->willReturn(
'');
70 $sessionBackendProphecy = $this->prophesize(SessionBackendInterface::class);
73 'ses_data' => serialize([
'foo' =>
'bar']),
74 'ses_anonymous' =>
true,
75 'ses_iplock' =>
'[DISABLED]',
77 $sessionBackendProphecy->get($uniqueSessionId)->shouldBeCalled()->willReturn($sessionRecord);
78 $sessionManagerProphecy = $this->prophesize(SessionManager::class);
79 GeneralUtility::setSingletonInstance(SessionManager::class, $sessionManagerProphecy->reveal());
80 $sessionManagerProphecy->getSessionBackend(
'FE')->willReturn($sessionBackendProphecy->reveal());
83 $subject->setLogger(
new NullLogger());
84 $subject->gc_probability = -1;
87 $this->assertArrayNotHasKey(
'uid', $subject->user);
88 $this->assertEquals(
'bar', $subject->getSessionData(
'foo'));
89 $this->assertEquals($uniqueSessionId, $subject->id);
98 $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
99 $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
100 $connectionPoolProphecy->getQueryBuilderForTable(
'fe_users')->willReturn($queryBuilderProphecy->reveal());
101 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
102 $expressionBuilderProphecy = $this->prophesize(ExpressionBuilder::class);
103 $queryBuilderProphecy->expr()->willReturn($expressionBuilderProphecy->reveal());
104 $compositeExpressionProphecy = $this->prophesize(CompositeExpression::class);
105 $expressionBuilderProphecy->andX(Argument::cetera())->willReturn($compositeExpressionProphecy->reveal());
106 $expressionBuilderProphecy->in(Argument::cetera())->willReturn(
'');
109 $sessionBackendProphecy = $this->prophesize(SessionBackendInterface::class);
110 $sessionManagerProphecy = $this->prophesize(SessionManager::class);
111 GeneralUtility::setSingletonInstance(SessionManager::class, $sessionManagerProphecy->reveal());
112 $sessionManagerProphecy->getSessionBackend(
'FE')->willReturn($sessionBackendProphecy->reveal());
116 $randomProphecy = $this->prophesize(Random::class);
117 $randomProphecy->generateRandomHexString(32)->shouldBeCalled()->willReturn(
'newSessionId');
118 GeneralUtility::addInstance(Random::class, $randomProphecy->reveal());
121 $sessionBackendProphecy->set(Argument::cetera())->shouldNotBeCalled();
122 $sessionBackendProphecy->update(Argument::cetera())->shouldNotBeCalled();
125 $subject->setLogger(
new NullLogger());
126 $subject->gc_probability = -1;
128 $subject->storeSessionData();
139 $uniqueSessionId = $this->getUniqueId(
'test');
140 $_COOKIE[
'fe_typo_user'] = $uniqueSessionId;
143 $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
144 $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
145 $connectionPoolProphecy->getQueryBuilderForTable(
'fe_users')->willReturn($queryBuilderProphecy->reveal());
146 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
147 $expressionBuilderProphecy = $this->prophesize(ExpressionBuilder::class);
148 $queryBuilderProphecy->expr()->willReturn($expressionBuilderProphecy->reveal());
149 $compositeExpressionProphecy = $this->prophesize(CompositeExpression::class);
150 $expressionBuilderProphecy->andX(Argument::cetera())->willReturn($compositeExpressionProphecy->reveal());
151 $expressionBuilderProphecy->in(Argument::cetera())->willReturn(
'');
154 $sessionBackendProphecy = $this->prophesize(SessionBackendInterface::class);
157 'ses_data' => serialize([
'foo' =>
'bar']),
158 'ses_anonymous' =>
true,
159 'ses_iplock' =>
'[DISABLED]',
161 $sessionBackendProphecy->get($uniqueSessionId)->shouldBeCalled()->willReturn($sessionRecord);
162 $sessionManagerProphecy = $this->prophesize(SessionManager::class);
163 GeneralUtility::setSingletonInstance(SessionManager::class, $sessionManagerProphecy->reveal());
164 $sessionManagerProphecy->getSessionBackend(
'FE')->willReturn($sessionBackendProphecy->reveal());
167 $sessionBackendProphecy->set(Argument::cetera())->shouldNotBeCalled();
168 $sessionBackendProphecy->update(Argument::cetera())->shouldNotBeCalled();
170 $sessionBackendProphecy->remove($uniqueSessionId)->shouldBeCalled();
172 $subject =
new FrontendUserAuthentication();
173 $subject->setLogger(
new NullLogger());
174 $subject->gc_probability = -1;
176 $subject->setSessionData(
'foo',
'bar');
177 $subject->removeSessionData();
178 $this->assertAttributeEmpty(
'sessionData', $subject);
188 $uniqueSessionId = $this->getUniqueId(
'test');
189 $_COOKIE[
'fe_typo_user'] = $uniqueSessionId;
193 $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
194 $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
195 $connectionPoolProphecy->getQueryBuilderForTable(
'fe_users')->willReturn($queryBuilderProphecy->reveal());
196 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
197 $expressionBuilderProphecy = $this->prophesize(ExpressionBuilder::class);
198 $queryBuilderProphecy->expr()->willReturn($expressionBuilderProphecy->reveal());
199 $compositeExpressionProphecy = $this->prophesize(CompositeExpression::class);
200 $expressionBuilderProphecy->andX(Argument::cetera())->willReturn($compositeExpressionProphecy->reveal());
201 $expressionBuilderProphecy->in(Argument::cetera())->willReturn(
'');
204 $sessionBackendProphecy = $this->prophesize(SessionBackendInterface::class);
205 $sessionBackendProphecy->get($uniqueSessionId)->shouldBeCalled()->willThrow(
new SessionNotFoundException(
'testing', 1486676313));
206 $sessionManagerProphecy = $this->prophesize(SessionManager::class);
207 GeneralUtility::setSingletonInstance(SessionManager::class, $sessionManagerProphecy->reveal());
208 $sessionManagerProphecy->getSessionBackend(
'FE')->willReturn($sessionBackendProphecy->reveal());
211 $randomProphecy = $this->prophesize(Random::class);
212 $randomProphecy->generateRandomHexString(32)->shouldBeCalled()->willReturn(
'newSessionId');
213 GeneralUtility::addInstance(Random::class, $randomProphecy->reveal());
216 $sessionBackendProphecy->update(Argument::cetera())->shouldNotBeCalled();
217 $sessionBackendProphecy->get(
'newSessionId')->shouldBeCalled()->willThrow(
new SessionNotFoundException(
'testing', 1486676314));
220 $sessionBackendProphecy->set(
223 'ses_id' =>
'newSessionId',
224 'ses_iplock' =>
'[DISABLED]',
226 'ses_tstamp' => $currentTime,
227 'ses_data' => serialize([
'foo' =>
'bar']),
228 'ses_permanent' => 0,
233 $subject =
new FrontendUserAuthentication();
234 $subject->setLogger(
new NullLogger());
235 $subject->gc_probability = -1;
237 $subject->lockIP = 0;
238 $this->assertEmpty($subject->getSessionData($uniqueSessionId));
239 $this->assertEmpty($subject->user);
240 $subject->setSessionData(
'foo',
'bar');
241 $this->assertAttributeNotEmpty(
'sessionData', $subject);
244 $prev = error_reporting(0);
245 $subject->storeSessionData();
246 error_reporting($prev);
256 $uniqueSessionId = $this->getUniqueId(
'test');
257 $_COOKIE[
'fe_typo_user'] = $uniqueSessionId;
261 $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
262 $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
263 $connectionPoolProphecy->getQueryBuilderForTable(
'fe_users')->willReturn($queryBuilderProphecy->reveal());
264 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
265 $expressionBuilderProphecy = $this->prophesize(ExpressionBuilder::class);
266 $queryBuilderProphecy->expr()->willReturn($expressionBuilderProphecy->reveal());
267 $compositeExpressionProphecy = $this->prophesize(CompositeExpression::class);
268 $expressionBuilderProphecy->andX(Argument::cetera())->willReturn($compositeExpressionProphecy->reveal());
269 $expressionBuilderProphecy->in(Argument::cetera())->willReturn(
'');
272 $sessionBackendProphecy = $this->prophesize(SessionBackendInterface::class);
273 $sessionManagerProphecy = $this->prophesize(SessionManager::class);
274 GeneralUtility::setSingletonInstance(SessionManager::class, $sessionManagerProphecy->reveal());
275 $sessionManagerProphecy->getSessionBackend(
'FE')->willReturn($sessionBackendProphecy->reveal());
278 $sessionBackendProphecy->get($uniqueSessionId)->shouldBeCalled()->willReturn(
280 'ses_id' => $uniqueSessionId . self::NOT_CHECKED_INDICATOR,
282 'ses_iplock' =>
'[DISABLED]',
283 'ses_tstamp' => $currentTime,
284 'ses_data' => serialize([
'foo' =>
'bar']),
285 'ses_permanent' => 0,
291 $connectionPoolFeUserProphecy = $this->prophesize(ConnectionPool::class);
292 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolFeUserProphecy->reveal());
293 $queryBuilderFeUserProphecy = $this->prophesize(QueryBuilder::class);
294 $queryBuilderFeUserProphecyRevelation = $queryBuilderFeUserProphecy->reveal();
295 $connectionPoolFeUserProphecy->getQueryBuilderForTable(
'fe_users')->willReturn($queryBuilderFeUserProphecyRevelation);
296 $queryBuilderFeUserProphecy->select(
'*')->willReturn($queryBuilderFeUserProphecyRevelation);
297 $queryBuilderFeUserProphecy->setRestrictions(Argument::cetera())->shouldBeCalled();
298 $queryBuilderFeUserProphecy->from(
'fe_users')->shouldBeCalled()->willReturn($queryBuilderFeUserProphecyRevelation);
299 $expressionBuilderFeUserProphecy = $this->prophesize(ExpressionBuilder::class);
300 $queryBuilderFeUserProphecy->expr()->willReturn($expressionBuilderFeUserProphecy->reveal());
301 $queryBuilderFeUserProphecy->createNamedParameter(Argument::cetera())->willReturnArgument(0);
302 $expressionBuilderFeUserProphecy->eq(Argument::cetera())->willReturn(
'1=1');
303 $queryBuilderFeUserProphecy->where(Argument::cetera())->shouldBeCalled()->willReturn($queryBuilderFeUserProphecyRevelation);
304 $statementFeUserProphecy = $this->prophesize(Statement::class);
305 $queryBuilderFeUserProphecy->execute()->shouldBeCalled()->willReturn($statementFeUserProphecy->reveal());
306 $statementFeUserProphecy->fetch()->willReturn(
309 'username' =>
'existingUserName',
316 $subject =
new FrontendUserAuthentication();
317 $subject->setLogger(
new NullLogger());
318 $subject->gc_probability = -1;
321 $this->assertAttributeNotEmpty(
'user', $subject);
322 $this->assertEquals(
'existingUserName', $subject->user[
'username']);
332 $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
333 $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
334 $connectionPoolProphecy->getQueryBuilderForTable(
'fe_users')->willReturn($queryBuilderProphecy->reveal());
335 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
336 $expressionBuilderProphecy = $this->prophesize(ExpressionBuilder::class);
337 $queryBuilderProphecy->expr()->willReturn($expressionBuilderProphecy->reveal());
338 $compositeExpressionProphecy = $this->prophesize(CompositeExpression::class);
339 $expressionBuilderProphecy->andX(Argument::cetera())->willReturn($compositeExpressionProphecy->reveal());
340 $expressionBuilderProphecy->in(Argument::cetera())->willReturn(
'');
343 $sessionBackendProphecy = $this->prophesize(SessionBackendInterface::class);
344 $sessionManagerProphecy = $this->prophesize(SessionManager::class);
345 GeneralUtility::setSingletonInstance(SessionManager::class, $sessionManagerProphecy->reveal());
346 $sessionManagerProphecy->getSessionBackend(
'FE')->willReturn($sessionBackendProphecy->reveal());
349 $sessionBackendProphecy->get(
'newSessionId')->willThrow(
new SessionNotFoundException(
'testing', 1486676358));
350 $sessionBackendProphecy->remove(
'newSessionId')->shouldBeCalled();
353 $randomProphecy = $this->prophesize(Random::class);
354 $randomProphecy->generateRandomHexString(32)->shouldBeCalled()->willReturn(
'newSessionId');
355 GeneralUtility::addInstance(Random::class, $randomProphecy->reveal());
358 $subject = $this->getMockBuilder($this->buildAccessibleProxy(FrontendUserAuthentication::class))
366 $subject->setLogger(
new NullLogger());
367 $subject->gc_probability = -1;
370 $subject->method(
'getLoginFormData')->willReturn([
372 'uname' =>
'existingUserName',
376 $authServiceMock = $this->getMockBuilder(AuthenticationService::class)->getMock();
377 $authServiceMock->method(
'getUser')->willReturn([
379 'username' =>
'existingUserName'
382 $authServiceMock->method(
'authUser')->willReturn(
true);
384 $subject->method(
'getAuthServices')->willReturn(
new \ArrayIterator([$authServiceMock]));
386 $subject->method(
'createUserSession')->willReturn([
387 'ses_id' =>
'newSessionId'
390 $subject->method(
'getCookie')->willReturn(
null);
393 $this->assertFalse($subject->_get(
'loginFailure'));
394 $this->assertEquals(
'existingUserName', $subject->user[
'username']);
404 $this->markTestSkipped(
'Test is flaky, convert to a functional test');
406 $sessionBackend = $this->getMockBuilder(SessionBackendInterface::class)->getMock();
408 $oldSessionRecord = [
409 'ses_id' =>
'oldSessionId',
410 'ses_data' => serialize([
'foo' =>
'bar']),
411 'ses_anonymous' => 1,
416 $sessionBackend->method(
'get')->willReturn($oldSessionRecord);
418 $expectedSessionRecord = array_merge(
428 $sessionBackend->expects($this->once())->method(
'set')->with(
430 $this->equalTo($expectedSessionRecord)
431 )->willReturnArgument(1);
433 $this->subject->method(
'getSessionBackend')->willReturn($sessionBackend);
435 $this->subject->method(
'getCookie')->willReturn(
'oldSessionId');
436 $this->subject->method(
'createSessionId')->willReturn(
'newSessionId');
439 $this->subject->method(
'getLoginFormData')->willReturn([
441 'uname' =>
'existingUserName',
445 $authServiceMock = $this->getMockBuilder(AuthenticationService::class)->getMock();
446 $authServiceMock->method(
'getUser')->willReturn([
448 'username' =>
'existingUserName'
451 $authServiceMock->method(
'authUser')->willReturn(
true);
454 $this->subject->method(
'getAuthServices')->willReturn(
new \ArrayIterator([$authServiceMock]));
458 $this->subject->start();
460 $this->assertEquals(
'newSessionId', $this->subject->id);
461 $this->assertEquals($expectedUserId, $this->subject->user[
'uid']);
462 $this->subject->setSessionData(
'foobar',
'baz');
463 $this->assertArraySubset([
'foo' =>
'bar'], $this->subject->_get(
'sessionData'));
464 $this->assertTrue($this->subject->sesData_change);
474 $this->markTestSkipped(
'Test is flaky, convert to a functional test');
476 $sessionBackend = $this->getMockBuilder(SessionBackendInterface::class)->getMock();
477 $sessionBackend->method(
'get')->willReturn(
479 'ses_id' =>
'existingId',
481 'ses_data' => serialize([
'foo' =>
'bar']),
483 'ses_tstamp' => time() + 100
486 $this->subject->method(
'getSessionBackend')->willReturn($sessionBackend);
487 $this->subject->method(
'getCookie')->willReturn(
'existingId');
489 $this->subject->start();
491 $this->subject->removeSessionData();
492 $this->assertEmpty($this->subject->getSessionData(
'foo'));
493 $this->subject->storeSessionData();
494 $this->assertEmpty($this->subject->getSessionData(
'foo'));
504 $this->markTestSkipped(
'Test is flaky, convert to a functional test');
505 $sessionBackend = $this->getMockBuilder(SessionBackendInterface::class)->getMock();
507 $this->subject->method(
'getSessionBackend')->willReturn($sessionBackend);
509 $this->subject->method(
'createSessionId')->willReturn(
'newSessionId');
511 $expectedSessionRecord = [
512 'ses_anonymous' => 1,
513 'ses_data' => serialize([
'foo' =>
'bar'])
516 $sessionBackend->expects($this->at(0))->method(
'get')->willThrowException(
new SessionNotFoundException(
'testing', 1486045419));
517 $sessionBackend->expects($this->at(1))->method(
'get')->willThrowException(
new SessionNotFoundException(
'testing', 1486045420));
518 $sessionBackend->expects($this->at(2))->method(
'get')->willReturn(
520 'ses_id' =>
'newSessionId',
525 $sessionBackend->expects($this->once())
527 ->with(
'newSessionId',
new \PHPUnit_Framework_Constraint_ArraySubset($expectedSessionRecord))
529 'ses_id' =>
'newSessionId',
530 'ses_anonymous' => 1,
531 'ses_data' => serialize([
'foo' =>
'bar']),
535 $this->subject->start();
536 $this->assertEmpty($this->subject->_get(
'sessionData'));
537 $this->assertEmpty($this->subject->user);
538 $this->subject->setSessionData(
'foo',
'bar');
539 $this->assertAttributeNotEmpty(
'sessionData', $this->subject);
540 $this->subject->storeSessionData();
543 $this->subject->setSessionData(
'foo',
null);
544 $this->assertAttributeEmpty(
'sessionData', $this->subject);
545 $sessionBackend->expects($this->once())->method(
'remove')->with(
'newSessionId');
546 $sessionBackend->expects($this->never())->method(
'update');
548 $this->subject->storeSessionData();
557 $this->markTestSkipped(
'Test is flaky, convert to a functional test');
558 $sessionBackend = $this->getMockBuilder(SessionBackendInterface::class)->getMock();
559 $this->subject->method(
'getSessionBackend')->willReturn($sessionBackend);
560 $this->subject->method(
'createSessionId')->willReturn(
'newSessionId');
562 $sessionBackend->method(
'get')->willReturn(
564 'ses_id' =>
'existingId',
566 'ses_data' => serialize([
'foo' =>
'bar']),
568 'ses_tstamp' => time() + 100
571 $this->subject->method(
'getSessionBackend')->willReturn($sessionBackend);
572 $this->subject->method(
'getCookie')->willReturn(
'existingId');
574 $this->subject->method(
'getRawUserByUid')->willReturn([
580 $this->subject->method(
'getLoginFormData')->willReturn([
581 'status' =>
'logout',
585 $sessionBackend->expects($this->once())->method(
'set')->with(
'newSessionId', $this->anything())->willReturnArgument(1);
586 $sessionBackend->expects($this->once())->method(
'remove')->with(
'existingId');
589 $this->subject->start();
591 $this->assertNotEmpty($this->subject->user);
592 $this->assertEquals(1, (
int)$this->subject->user[
'ses_anonymous']);
593 $this->assertEquals([
'foo' =>
'bar'], $this->subject->_get(
'sessionData'));
595 $this->assertEquals(
'newSessionId', $this->subject->id);