‪TYPO3CMS  11.5
LoginControllerTest.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 
20 use PHPUnit\Framework\MockObject\MockObject;
21 use Prophecy\PhpUnit\ProphecyTrait;
27 use TYPO3\CMS\Core\Page\PageRenderer;
29 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
35 class ‪LoginControllerTest extends UnitTestCase
36 {
37  use ProphecyTrait;
38 
42  protected ‪$resetSingletonInstances = true;
43 
47  protected ‪$loginControllerMock;
48 
53  protected static bool ‪$alreadySetUp = false;
54 
58  protected function ‪setUp(): void
59  {
60  parent::setUp();
61  $this->loginControllerMock = $this->getAccessibleMock(LoginController::class, ['dummy'], [], '', false);
62  }
63 
68  {
69  ‪$GLOBALS['LANG'] = ($this->prophesize(LanguageService::class))->reveal();
70  $authenticationProphecy = $this->prophesize(BackendUserAuthentication::class);
71  $authenticationProphecy->getTSConfig()->willReturn([
72  'auth.' => [
73  'BE.' => [
74  'redirectToURL' => 'http://example.com',
75  ],
76  ],
77  ]);
78  $authenticationProphecy->writeUC()->willReturn();
79  $authenticationProphecy->getSessionData('formProtectionSessionToken')->willReturn('foo');
80  ‪$GLOBALS['BE_USER'] = $authenticationProphecy->reveal();
82 
83  $this->loginControllerMock = $this->getAccessibleMock(
84  LoginController::class,
85  ['isLoginInProgress', 'redirectToUrl'],
86  [],
87  '',
88  false
89  );
90 
91  ‪$GLOBALS['BE_USER']->user['uid'] = 1;
92  $this->loginControllerMock->method('isLoginInProgress')->willReturn(true);
93  $this->loginControllerMock->_set('loginRefresh', false);
94 
95  $this->loginControllerMock->expects(self::once())->method('redirectToUrl');
96  $this->loginControllerMock->_call(
97  'checkRedirect',
98  $this->prophesize(ServerRequest::class)->reveal(),
99  $this->prophesize(PageRenderer::class)->reveal()
100  );
101  }
102 
106  public function ‪checkRedirectDoesNotRedirectIfNoUserIsFound(): void
107  {
108  ‪$GLOBALS['BE_USER'] = $this->prophesize(BackendUserAuthentication::class)->reveal();
109  $this->loginControllerMock = $this->getAccessibleMock(
110  LoginController::class,
111  ['redirectToUrl'],
112  [],
113  '',
114  false
115  );
116 
117  ‪$GLOBALS['BE_USER']->user['uid'] = null;
118 
119  $this->loginControllerMock->expects(self::never())->method('redirectToUrl');
120  $this->loginControllerMock->_call(
121  'checkRedirect',
122  $this->prophesize(ServerRequest::class)->reveal(),
123  $this->prophesize(PageRenderer::class)->reveal()
124  );
125  }
126 
130  protected function ‪prophesizeFormProtection(): void
131  {
132  if (!self::$alreadySetUp) {
133  $formProtectionProphecy = $this->prophesize(BackendFormProtection::class);
134  GeneralUtility::addInstance(BackendFormProtection::class, $formProtectionProphecy->reveal());
135  self::$alreadySetUp = true;
136  }
137  }
138 }
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\$loginControllerMock
‪LoginController MockObject AccessibleObjectInterface $loginControllerMock
Definition: LoginControllerTest.php:44
‪TYPO3\CMS\Core\FormProtection\BackendFormProtection
Definition: BackendFormProtection.php:75
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: LoginControllerTest.php:40
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\$alreadySetUp
‪static bool $alreadySetUp
Definition: LoginControllerTest.php:50
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\checkRedirectDoesNotRedirectIfNoUserIsFound
‪checkRedirectDoesNotRedirectIfNoUserIsFound()
Definition: LoginControllerTest.php:103
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest
Definition: LoginControllerTest.php:36
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\setUp
‪setUp()
Definition: LoginControllerTest.php:55
‪TYPO3\CMS\Backend\Tests\Unit\Controller
Definition: EditDocumentControllerTest.php:18
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\prophesizeFormProtection
‪prophesizeFormProtection()
Definition: LoginControllerTest.php:127
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Controller\LoginController
Definition: LoginController.php:54
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\checkRedirectRedirectsIfLoginIsInProgressAndUserWasFound
‪checkRedirectRedirectsIfLoginIsInProgressAndUserWasFound()
Definition: LoginControllerTest.php:64