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