[!!!][TASK] Remove config.language_alt and config.language
[Packages/TYPO3.CMS.git] / typo3 / sysext / felogin / Tests / Unit / Controller / FrontendLoginControllerTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Felogin\Tests\Unit\Controller;
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
18 use TYPO3\CMS\Core\Authentication\LoginType;
19 use TYPO3\CMS\Core\Http\ServerRequest;
20 use TYPO3\CMS\Core\Http\Uri;
21 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
22 use TYPO3\CMS\Felogin\Controller\FrontendLoginController;
23 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
24 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26
27 /**
28 * Test case
29 */
30 class FrontendLoginControllerTest extends UnitTestCase
31 {
32 protected $resetSingletonInstances = true;
33
34 public function setUp(): void
35 {
36 $GLOBALS['TYPO3_REQUEST'] = (new ServerRequest())->withAttribute('language', new SiteLanguage(0, 'en_US', new Uri('/'), ['typo3Language' => 'en']));
37 $GLOBALS['TSFE'] = new \stdClass();
38 parent::setUp();
39 }
40
41 /*************************
42 * Test concerning getPreserveGetVars
43 *************************/
44
45 /**
46 * @return array
47 */
48 public function getPreserveGetVarsReturnsCorrectResultDataProvider()
49 {
50 return [
51 'special get var id is not preserved' => [
52 [
53 'id' => 42,
54 ],
55 '',
56 [],
57 ],
58 'simple additional parameter is not preserved if not specified in preservedGETvars' => [
59 [
60 'id' => 42,
61 'special' => 23,
62 ],
63 '',
64 [],
65 ],
66 'all params except ignored ones are preserved if preservedGETvars is set to "all"' => [
67 [
68 'id' => 42,
69 'special1' => 23,
70 'special2' => [
71 'foo' => 'bar',
72 ],
73 'tx_felogin_pi1' => [
74 'forgot' => 1,
75 ],
76 ],
77 'all',
78 [
79 'special1' => 23,
80 'special2' => [
81 'foo' => 'bar',
82 ],
83 ]
84 ],
85 'preserve single parameter' => [
86 [
87 'L' => 42,
88 ],
89 'L',
90 [
91 'L' => 42,
92 ],
93 ],
94 'preserve whole parameter array' => [
95 [
96 'L' => 3,
97 'tx_someext' => [
98 'foo' => 'simple',
99 'bar' => [
100 'baz' => 'simple',
101 ],
102 ],
103 ],
104 'L,tx_someext',
105 [
106 'L' => 3,
107 'tx_someext' => [
108 'foo' => 'simple',
109 'bar' => [
110 'baz' => 'simple',
111 ],
112 ],
113 ],
114 ],
115 'preserve part of sub array' => [
116 [
117 'L' => 3,
118 'tx_someext' => [
119 'foo' => 'simple',
120 'bar' => [
121 'baz' => 'simple',
122 ],
123 ],
124 ],
125 'L,tx_someext[bar]',
126 [
127 'L' => 3,
128 'tx_someext' => [
129 'bar' => [
130 'baz' => 'simple',
131 ],
132 ],
133 ],
134 ],
135 'preserve keys on different levels' => [
136 [
137 'L' => 3,
138 'no-preserve' => 'whatever',
139 'tx_ext2' => [
140 'foo' => 'simple',
141 ],
142 'tx_ext3' => [
143 'bar' => [
144 'baz' => 'simple',
145 ],
146 'go-away' => '',
147 ],
148 ],
149 'L,tx_ext2,tx_ext3[bar]',
150 [
151 'L' => 3,
152 'tx_ext2' => [
153 'foo' => 'simple',
154 ],
155 'tx_ext3' => [
156 'bar' => [
157 'baz' => 'simple',
158 ],
159 ],
160 ],
161 ],
162 'preserved value that does not exist in get' => [
163 [],
164 'L,foo%5Bbar%5D',
165 [],
166 ],
167 ];
168 }
169
170 /**
171 * @test
172 * @dataProvider getPreserveGetVarsReturnsCorrectResultDataProvider
173 * @param array $getArray
174 * @param string $preserveVars
175 * @param string $expected
176 */
177 public function getPreserveGetVarsReturnsCorrectResult(array $getArray, $preserveVars, $expected)
178 {
179 $_GET = $getArray;
180 $subject = $this->getAccessibleMock(FrontendLoginController::class, ['dummy'], ['_', $this->createMock(TypoScriptFrontendController::class)]);
181 $subject->cObj = $this->createMock(ContentObjectRenderer::class);
182 $subject->conf['preserveGETvars'] = $preserveVars;
183 $this->assertSame($expected, $subject->_call('getPreserveGetVars'));
184 }
185
186 /**
187 *
188 */
189 public function processUserFieldsRespectsDefaultConfigurationForStdWrapDataProvider()
190 {
191 return [
192 'Simple casing' => [
193 [
194 'username' => 'Holy',
195 'lastname' => 'Wood',
196 ],
197 [
198 'username.' => [
199 'case' => 'upper'
200 ]
201 ],
202 [
203 '###FEUSER_USERNAME###' => 'HOLY',
204 '###FEUSER_LASTNAME###' => 'Wood',
205 '###USER###' => 'HOLY'
206 ]
207 ],
208 'Default config applies' => [
209 [
210 'username' => 'Holy',
211 'lastname' => 'O" Mally',
212 ],
213 [
214 'username.' => [
215 'case' => 'upper'
216 ]
217 ],
218 [
219 '###FEUSER_USERNAME###' => 'HOLY',
220 '###FEUSER_LASTNAME###' => 'O&quot; Mally',
221 '###USER###' => 'HOLY'
222 ]
223 ],
224 'Specific config overrides default config' => [
225 [
226 'username' => 'Holy',
227 'lastname' => 'O" Mally',
228 ],
229 [
230 'username.' => [
231 'case' => 'upper'
232 ],
233 'lastname.' => [
234 'htmlSpecialChars' => '0'
235 ]
236 ],
237 [
238 '###FEUSER_USERNAME###' => 'HOLY',
239 '###FEUSER_LASTNAME###' => 'O" Mally',
240 '###USER###' => 'HOLY'
241 ]
242 ],
243 'No given user returns empty array' => [
244 null,
245 [
246 'username.' => [
247 'case' => 'upper'
248 ],
249 'lastname.' => [
250 'htmlSpecialChars' => '0'
251 ]
252 ],
253 []
254 ],
255 ];
256 }
257
258 /**
259 * @test
260 * @dataProvider processUserFieldsRespectsDefaultConfigurationForStdWrapDataProvider
261 */
262 public function processUserFieldsRespectsDefaultConfigurationForStdWrap($userRecord, $fieldConf, $expectedMarkers)
263 {
264 $tsfe = new \stdClass();
265 $tsfe->fe_user = new \stdClass();
266 $tsfe->fe_user->user = $userRecord;
267 $conf = ['userfields.' => $fieldConf];
268 $subject = $this->getAccessibleMock(FrontendLoginController::class, ['dummy']);
269 $subject->cObj = new ContentObjectRenderer();
270 $subject->_set('frontendController', $tsfe);
271 $subject->_set('conf', $conf);
272 $this->assertEquals($expectedMarkers, $subject->_call('getUserFieldMarkers'));
273 }
274
275 /**
276 * @test
277 */
278 public function processRedirectReferrerDomainsMatchesDomains()
279 {
280 $conf = [
281 'redirectMode' => 'refererDomains',
282 'domains' => 'example.com'
283 ];
284 $subject = $this->getAccessibleMock(FrontendLoginController::class, ['dummy']);
285 $subject->_set('conf', $conf);
286 $subject->_set('logintype', LoginType::LOGIN);
287 $subject->_set('referer', 'http://www.example.com/snafu');
288 $subject->_set('userIsLoggedIn', true);
289 $this->assertSame(['http://www.example.com/snafu'], $subject->_call('processRedirect'));
290 }
291 }