‪TYPO3CMS  9.5
LoginControllerTest.php
Go to the documentation of this file.
1 <?php
2 
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 
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪LoginControllerTest extends UnitTestCase
32 {
36  protected ‪$resetSingletonInstances = true;
37 
41  protected ‪$loginControllerMock;
42 
47  protected static ‪$alreadySetUp = false;
48 
52  protected function ‪setUp()
53  {
54  $this->loginControllerMock = $this->getAccessibleMock(LoginController::class, ['dummy'], [], '', false);
55  }
56 
61  {
62  $this->expectException(\RuntimeException::class);
63  $this->expectExceptionCode(1433417281);
64  unset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders']);
65  $this->loginControllerMock->_call('validateAndSortLoginProviders');
66  }
67 
72  {
73  $this->expectException(\RuntimeException::class);
74  $this->expectExceptionCode(1433417281);
75  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = 'foo';
76  $this->loginControllerMock->_call('validateAndSortLoginProviders');
77  }
78 
83  {
84  $this->expectException(\RuntimeException::class);
85  $this->expectExceptionCode(1433417281);
86  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [];
87  $this->loginControllerMock->_call('validateAndSortLoginProviders');
88  }
89 
94  {
95  $this->expectException(\RuntimeException::class);
96  $this->expectExceptionCode(1433416043);
97  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [
98  1433419736 => [],
99  ];
100  $this->loginControllerMock->_call('validateAndSortLoginProviders');
101  }
102 
107  {
108  $this->expectException(\RuntimeException::class);
109  $this->expectExceptionCode(1460977275);
110  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [
111  1433419736 => [
112  'provider' => \stdClass::class,
113  ],
114  ];
115  $this->loginControllerMock->_call('validateAndSortLoginProviders');
116  }
117 
122  {
123  $this->expectException(\RuntimeException::class);
124  $this->expectExceptionCode(1433416044);
125  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [
126  1433419736 => [
127  'provider' => UsernamePasswordLoginProvider::class,
128  'sorting' => 30,
129  'icon-class' => 'foo',
130  ],
131  ];
132  $this->loginControllerMock->_call('validateAndSortLoginProviders');
133  }
134 
139  {
140  $this->expectException(\RuntimeException::class);
141  $this->expectExceptionCode(1433416045);
142  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [
143  1433419736 => [
144  'provider' => UsernamePasswordLoginProvider::class,
145  'sorting' => 30,
146  'label' => 'foo',
147  ],
148  ];
149  $this->loginControllerMock->_call('validateAndSortLoginProviders');
150  }
151 
156  {
157  $this->expectException(\RuntimeException::class);
158  $this->expectExceptionCode(1433416046);
159  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] = [
160  1433419736 => [
161  'provider' => UsernamePasswordLoginProvider::class,
162  'label' => 'foo',
163  'icon-class' => 'foo',
164  ],
165  ];
166  $this->loginControllerMock->_call('validateAndSortLoginProviders');
167  }
168 
173  {
174  ‪$GLOBALS['LANG'] = ($this->prophesize(LanguageService::class))->reveal();
175  $authenticationProphecy = $this->prophesize(BackendUserAuthentication::class);
176  $authenticationProphecy->getTSConfig()->willReturn([
177  'auth.' => [
178  'BE.' => [
179  'redirectToURL' => 'http://example.com'
180  ]
181  ]
182  ]);
183  $authenticationProphecy->writeUC()->willReturn();
184  $authenticationProphecy->getSessionData('formProtectionSessionToken')->willReturn('foo');
185  ‪$GLOBALS['BE_USER'] = $authenticationProphecy->reveal();
187 
188  $this->loginControllerMock = $this->getAccessibleMock(
189  LoginController::class,
190  ['isLoginInProgress', 'redirectToUrl'],
191  [],
192  '',
193  false
194  );
195 
196  ‪$GLOBALS['BE_USER']->user['uid'] = 1;
197  $this->loginControllerMock->method('isLoginInProgress')->willReturn(true);
198  $this->loginControllerMock->_set('loginRefresh', false);
199 
200  $this->loginControllerMock->expects($this->once())->method('redirectToUrl');
201  $this->loginControllerMock->_call('checkRedirect', $this->prophesize(ServerRequest::class)->reveal());
202  }
203 
208  {
209  ‪$GLOBALS['LANG'] = $this->prophesize(LanguageService::class)->reveal();
210  $authenticationProphecy = $this->prophesize(BackendUserAuthentication::class);
211  $authenticationProphecy->getTSConfig()->willReturn([
212  'auth.' => [
213  'BE.' => [
214  'redirectToURL' => 'http://example.com'
215  ]
216  ]
217  ]);
218  $authenticationProphecy->writeUC()->willReturn();
220  ‪$GLOBALS['BE_USER'] = $authenticationProphecy->reveal();
221  $documentTemplateProphecy = $this->prophesize(DocumentTemplate::class);
222  ‪$GLOBALS['TBE_TEMPLATE'] = $documentTemplateProphecy->reveal();
223 
224  $this->loginControllerMock = $this->getAccessibleMock(
225  LoginController::class,
226  ['isLoginInProgress', 'redirectToUrl'],
227  [],
228  '',
229  false
230  );
231 
232  ‪$GLOBALS['BE_USER']->user['uid'] = 1;
233  $this->loginControllerMock->method('isLoginInProgress')->willReturn(false);
234  $this->loginControllerMock->_set('loginRefresh', true);
235 
236  $this->loginControllerMock->_call('checkRedirect', $this->prophesize(ServerRequest::class)->reveal());
237 
238  self::assertContains('window.opener.TYPO3.LoginRefresh.startTask();', $documentTemplateProphecy->JScode);
239  }
240 
245  {
246  ‪$GLOBALS['LANG'] = $this->prophesize(LanguageService::class)->reveal();
247  $authenticationProphecy = $this->prophesize(BackendUserAuthentication::class);
248  $authenticationProphecy->getTSConfig()->willReturn([
249  'auth.' => [
250  'BE.' => [
251  'redirectToURL' => 'http://example.com'
252  ]
253  ]
254  ]);
255  $authenticationProphecy->writeUC()->willReturn();
256  ‪$GLOBALS['BE_USER'] = $authenticationProphecy->reveal();
258  $documentTemplateProphecy = $this->prophesize(DocumentTemplate::class);
259  ‪$GLOBALS['TBE_TEMPLATE'] = $documentTemplateProphecy->reveal();
260 
261  $this->loginControllerMock = $this->getAccessibleMock(
262  LoginController::class,
263  ['isLoginInProgress', 'redirectToUrl'],
264  [],
265  '',
266  false
267  );
268 
269  ‪$GLOBALS['BE_USER']->user['uid'] = 1;
270  $this->loginControllerMock->method('isLoginInProgress')->willReturn(true);
271  $this->loginControllerMock->_set('loginRefresh', true);
272 
273  $this->loginControllerMock->_call('checkRedirect', $this->prophesize(ServerRequest::class)->reveal());
274 
275  self::assertContains('window.opener.TYPO3.LoginRefresh.startTask();', $documentTemplateProphecy->JScode);
276  }
277 
281  public function ‪checkRedirectDoesNotRedirectIfNoUserIsFound(): void
282  {
283  ‪$GLOBALS['BE_USER'] = $this->prophesize(BackendUserAuthentication::class)->reveal();
284  $this->loginControllerMock = $this->getAccessibleMock(
285  LoginController::class,
286  ['redirectToUrl'],
287  [],
288  '',
289  false
290  );
291 
292  ‪$GLOBALS['BE_USER']->user['uid'] = null;
293 
294  $this->loginControllerMock->expects($this->never())->method('redirectToUrl');
295  $this->loginControllerMock->_call('checkRedirect', $this->prophesize(ServerRequest::class)->reveal());
296  }
297 
301  protected function ‪prophesizeFormProtection(): void
302  {
303  if (!self::$alreadySetUp) {
304  $formProtectionProphecy = $this->prophesize(BackendFormProtection::class);
305  GeneralUtility::addInstance(BackendFormProtection::class, $formProtectionProphecy->reveal());
306  self::$alreadySetUp = true;
307  }
308  }
309 }
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\checkRedirectAddsJavaScriptForCaseLoginRefreshWhileLoginIsInProgress
‪checkRedirectAddsJavaScriptForCaseLoginRefreshWhileLoginIsInProgress()
Definition: LoginControllerTest.php:241
‪TYPO3\CMS\Core\FormProtection\BackendFormProtection
Definition: BackendFormProtection.php:73
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsMissingConfigurationForProvider
‪validateAndSortLoginProvidersDetectsMissingConfigurationForProvider()
Definition: LoginControllerTest.php:90
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: LoginControllerTest.php:35
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsIfNoProviderIsRegistered
‪validateAndSortLoginProvidersDetectsIfNoProviderIsRegistered()
Definition: LoginControllerTest.php:79
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsMissingIconClass
‪validateAndSortLoginProvidersDetectsMissingIconClass()
Definition: LoginControllerTest.php:135
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\$alreadySetUp
‪static bool $alreadySetUp
Definition: LoginControllerTest.php:44
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsNonArrayProviderConfiguration
‪validateAndSortLoginProvidersDetectsNonArrayProviderConfiguration()
Definition: LoginControllerTest.php:68
‪TYPO3\CMS\Backend\Template\DocumentTemplate
Definition: DocumentTemplate.php:48
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsMissingProviderConfiguration
‪validateAndSortLoginProvidersDetectsMissingProviderConfiguration()
Definition: LoginControllerTest.php:57
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\checkRedirectDoesNotRedirectIfNoUserIsFound
‪checkRedirectDoesNotRedirectIfNoUserIsFound()
Definition: LoginControllerTest.php:278
‪TYPO3\CMS\Backend\LoginProvider\UsernamePasswordLoginProvider
Definition: UsernamePasswordLoginProvider.php:29
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:35
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest
Definition: LoginControllerTest.php:32
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\setUp
‪setUp()
Definition: LoginControllerTest.php:49
‪TYPO3\CMS\Backend\Tests\Unit\Controller
Definition: EditDocumentControllerTest.php:3
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsWrongProvider
‪validateAndSortLoginProvidersDetectsWrongProvider()
Definition: LoginControllerTest.php:103
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsMissingLabel
‪validateAndSortLoginProvidersDetectsMissingLabel()
Definition: LoginControllerTest.php:118
‪$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:204
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\$loginControllerMock
‪LoginController PHPUnit_Framework_MockObject_MockObject TYPO3 TestingFramework Core AccessibleObjectInterface $loginControllerMock
Definition: LoginControllerTest.php:39
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\prophesizeFormProtection
‪prophesizeFormProtection()
Definition: LoginControllerTest.php:298
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\validateAndSortLoginProvidersDetectsMissingSorting
‪validateAndSortLoginProvidersDetectsMissingSorting()
Definition: LoginControllerTest.php:152
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Controller\LoginController
Definition: LoginController.php:49
‪TYPO3\CMS\Backend\Tests\Unit\Controller\LoginControllerTest\checkRedirectRedirectsIfLoginIsInProgressAndUserWasFound
‪checkRedirectRedirectsIfLoginIsInProgressAndUserWasFound()
Definition: LoginControllerTest.php:169