‪TYPO3CMS  10.4
LoginControllerTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Prophecy\Argument;
19 use Prophecy\Prophecy\MethodProphecy;
20 use Prophecy\Prophecy\ObjectProphecy;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪LoginControllerTest extends UnitTestCase
35 {
39  protected ‪$resetSingletonInstances = true;
40 
44  protected ‪$loginControllerMock;
45 
50  protected static ‪$alreadySetUp = false;
51 
55  protected function ‪setUp(): void
56  {
57  $this->loginControllerMock = $this->getAccessibleMock(LoginController::class, ['dummy'], [], '', false);
58  }
59 
64  {
65  $this->expectException(\RuntimeException::class);
66  $this->expectExceptionCode(1433417281);
67  unset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders']);
68  $this->loginControllerMock->_call('validateAndSortLoginProviders');
69  }
70 
75  {
76  $this->expectException(\RuntimeException::class);
77  $this->expectExceptionCode(1433417281);
78  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = 'foo';
79  $this->loginControllerMock->_call('validateAndSortLoginProviders');
80  }
81 
86  {
87  $this->expectException(\RuntimeException::class);
88  $this->expectExceptionCode(1433417281);
89  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [];
90  $this->loginControllerMock->_call('validateAndSortLoginProviders');
91  }
92 
97  {
98  $this->expectException(\RuntimeException::class);
99  $this->expectExceptionCode(1433416043);
100  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [
101  1433419736 => [],
102  ];
103  $this->loginControllerMock->_call('validateAndSortLoginProviders');
104  }
105 
110  {
111  $this->expectException(\RuntimeException::class);
112  $this->expectExceptionCode(1460977275);
113  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [
114  1433419736 => [
115  'provider' => \stdClass::class,
116  ],
117  ];
118  $this->loginControllerMock->_call('validateAndSortLoginProviders');
119  }
120 
125  {
126  $this->expectException(\RuntimeException::class);
127  $this->expectExceptionCode(1433416044);
128  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [
129  1433419736 => [
130  'provider' => UsernamePasswordLoginProvider::class,
131  'sorting' => 30,
132  'icon-class' => 'foo',
133  ],
134  ];
135  $this->loginControllerMock->_call('validateAndSortLoginProviders');
136  }
137 
142  {
143  $this->expectException(\RuntimeException::class);
144  $this->expectExceptionCode(1433416045);
145  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [
146  1433419736 => [
147  'provider' => UsernamePasswordLoginProvider::class,
148  'sorting' => 30,
149  'label' => 'foo',
150  ],
151  ];
152  $this->loginControllerMock->_call('validateAndSortLoginProviders');
153  }
154 
159  {
160  $this->expectException(\RuntimeException::class);
161  $this->expectExceptionCode(1433416046);
162  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [
163  1433419736 => [
164  'provider' => UsernamePasswordLoginProvider::class,
165  'label' => 'foo',
166  'icon-class' => 'foo',
167  ],
168  ];
169  $this->loginControllerMock->_call('validateAndSortLoginProviders');
170  }
171 
176  {
177  ‪$GLOBALS['LANG'] = ($this->prophesize(LanguageService::class))->reveal();
178  $authenticationProphecy = $this->prophesize(BackendUserAuthentication::class);
179  $authenticationProphecy->getTSConfig()->willReturn([
180  'auth.' => [
181  'BE.' => [
182  'redirectToURL' => 'http://example.com'
183  ]
184  ]
185  ]);
186  $authenticationProphecy->writeUC()->willReturn();
187  $authenticationProphecy->getSessionData('formProtectionSessionToken')->willReturn('foo');
188  ‪$GLOBALS['BE_USER'] = $authenticationProphecy->reveal();
190 
191  $this->loginControllerMock = $this->getAccessibleMock(
192  LoginController::class,
193  ['isLoginInProgress', 'redirectToUrl'],
194  [],
195  '',
196  false
197  );
198 
199  ‪$GLOBALS['BE_USER']->user['uid'] = 1;
200  $this->loginControllerMock->method('isLoginInProgress')->willReturn(true);
201  $this->loginControllerMock->_set('loginRefresh', false);
202 
203  $this->loginControllerMock->expects(self::once())->method('redirectToUrl');
204  $this->loginControllerMock->_call(
205  'checkRedirect',
206  $this->prophesize(ServerRequest::class)->reveal(),
207  $this->prophesize(PageRenderer::class)->reveal()
208  );
209  }
210 
215  {
216  ‪$GLOBALS['LANG'] = $this->prophesize(LanguageService::class)->reveal();
217  $authenticationProphecy = $this->prophesize(BackendUserAuthentication::class);
218  $authenticationProphecy->getTSConfig()->willReturn([
219  'auth.' => [
220  'BE.' => [
221  'redirectToURL' => 'http://example.com'
222  ]
223  ]
224  ]);
225  $authenticationProphecy->writeUC()->willReturn();
227  ‪$GLOBALS['BE_USER'] = $authenticationProphecy->reveal();
228 
229  $this->loginControllerMock = $this->getAccessibleMock(
230  LoginController::class,
231  ['isLoginInProgress', 'redirectToUrl'],
232  [],
233  '',
234  false
235  );
236 
237  ‪$GLOBALS['BE_USER']->user['uid'] = 1;
238  $this->loginControllerMock->method('isLoginInProgress')->willReturn(false);
239  $this->loginControllerMock->_set('loginRefresh', true);
241  $pageRendererProphecy = $this->prophesize(PageRenderer::class);
243  $inlineCodeProphecy = $pageRendererProphecy->addJsInlineCode('loginRefresh', Argument::cetera());
244  $this->loginControllerMock->_set('pageRenderer', $pageRendererProphecy->reveal());
245 
246  $this->loginControllerMock->_call(
247  'checkRedirect',
248  $this->prophesize(ServerRequest::class)->reveal()
249  );
250 
251  $inlineCodeProphecy->shouldHaveBeenCalledTimes(1);
252  }
253 
258  {
259  ‪$GLOBALS['LANG'] = $this->prophesize(LanguageService::class)->reveal();
260  $authenticationProphecy = $this->prophesize(BackendUserAuthentication::class);
261  $authenticationProphecy->getTSConfig()->willReturn([
262  'auth.' => [
263  'BE.' => [
264  'redirectToURL' => 'http://example.com'
265  ]
266  ]
267  ]);
268  $authenticationProphecy->writeUC()->willReturn();
269  ‪$GLOBALS['BE_USER'] = $authenticationProphecy->reveal();
271 
272  $this->loginControllerMock = $this->getAccessibleMock(
273  LoginController::class,
274  ['isLoginInProgress', 'redirectToUrl'],
275  [],
276  '',
277  false
278  );
279 
280  ‪$GLOBALS['BE_USER']->user['uid'] = 1;
281  $this->loginControllerMock->method('isLoginInProgress')->willReturn(true);
282  $this->loginControllerMock->_set('loginRefresh', true);
284  $pageRendererProphecy = $this->prophesize(PageRenderer::class);
286  $inlineCodeProphecy = $pageRendererProphecy->addJsInlineCode('loginRefresh', Argument::cetera());
287  $this->loginControllerMock->_set('pageRenderer', $pageRendererProphecy->reveal());
288 
289  $this->loginControllerMock->_call(
290  'checkRedirect',
291  $this->prophesize(ServerRequest::class)->reveal()
292  );
293 
294  $inlineCodeProphecy->shouldHaveBeenCalledTimes(1);
295  }
296 
300  public function ‪checkRedirectDoesNotRedirectIfNoUserIsFound(): void
301  {
302  ‪$GLOBALS['BE_USER'] = $this->prophesize(BackendUserAuthentication::class)->reveal();
303  $this->loginControllerMock = $this->getAccessibleMock(
304  LoginController::class,
305  ['redirectToUrl'],
306  [],
307  '',
308  false
309  );
310 
311  ‪$GLOBALS['BE_USER']->user['uid'] = null;
312 
313  $this->loginControllerMock->expects(self::never())->method('redirectToUrl');
314  $this->loginControllerMock->_call(
315  'checkRedirect',
316  $this->prophesize(ServerRequest::class)->reveal(),
317  $this->prophesize(PageRenderer::class)->reveal()
318  );
319  }
320 
324  protected function ‪prophesizeFormProtection(): void
325  {
326  if (!self::$alreadySetUp) {
327  $formProtectionProphecy = $this->prophesize(BackendFormProtection::class);
328  GeneralUtility::addInstance(BackendFormProtection::class, $formProtectionProphecy->reveal());
329  self::$alreadySetUp = true;
330  }
331  }
332 }
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\$loginControllerMock
‪LoginController PHPUnit Framework MockObject MockObject TYPO3 TestingFramework Core AccessibleObjectInterface $loginControllerMock
Definition: LoginControllerTest.php:42
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\checkRedirectAddsJavaScriptForCaseLoginRefreshWhileLoginIsInProgress
‪checkRedirectAddsJavaScriptForCaseLoginRefreshWhileLoginIsInProgress()
Definition: LoginControllerTest.php:254
‪TYPO3\CMS\Core\FormProtection\BackendFormProtection
Definition: BackendFormProtection.php:75
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsMissingConfigurationForProvider
‪validateAndSortLoginProvidersDetectsMissingConfigurationForProvider()
Definition: LoginControllerTest.php:93
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: LoginControllerTest.php:38
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsIfNoProviderIsRegistered
‪validateAndSortLoginProvidersDetectsIfNoProviderIsRegistered()
Definition: LoginControllerTest.php:82
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsMissingIconClass
‪validateAndSortLoginProvidersDetectsMissingIconClass()
Definition: LoginControllerTest.php:138
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\$alreadySetUp
‪static bool $alreadySetUp
Definition: LoginControllerTest.php:47
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:42
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsNonArrayProviderConfiguration
‪validateAndSortLoginProvidersDetectsNonArrayProviderConfiguration()
Definition: LoginControllerTest.php:71
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsMissingProviderConfiguration
‪validateAndSortLoginProvidersDetectsMissingProviderConfiguration()
Definition: LoginControllerTest.php:60
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\checkRedirectDoesNotRedirectIfNoUserIsFound
‪checkRedirectDoesNotRedirectIfNoUserIsFound()
Definition: LoginControllerTest.php:297
‪TYPO3\CMS\Backend\LoginProvider\UsernamePasswordLoginProvider
Definition: UsernamePasswordLoginProvider.php:29
‪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:35
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\setUp
‪setUp()
Definition: LoginControllerTest.php:52
‪TYPO3\CMS\Backend\Tests\Unit\Controller
Definition: EditDocumentControllerTest.php:16
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsWrongProvider
‪validateAndSortLoginProvidersDetectsWrongProvider()
Definition: LoginControllerTest.php:106
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsMissingLabel
‪validateAndSortLoginProvidersDetectsMissingLabel()
Definition: LoginControllerTest.php:121
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\checkRedirectAddsJavaScriptForCaseLoginRefresh
‪checkRedirectAddsJavaScriptForCaseLoginRefresh()
Definition: LoginControllerTest.php:211
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\prophesizeFormProtection
‪prophesizeFormProtection()
Definition: LoginControllerTest.php:321
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsMissingSorting
‪validateAndSortLoginProvidersDetectsMissingSorting()
Definition: LoginControllerTest.php:155
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Controller\LoginController
Definition: LoginController.php:54
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\checkRedirectRedirectsIfLoginIsInProgressAndUserWasFound
‪checkRedirectRedirectsIfLoginIsInProgressAndUserWasFound()
Definition: LoginControllerTest.php:172