[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Configuration / TypoScript / ConditionMatching / AbstractConditionMatcherTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Configuration\TypoScript\ConditionMatching;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher;
18 use TYPO3\CMS\Core\Core\ApplicationContext;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Test cases
23 */
24 class AbstractConditionMatcherTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
25 {
26 /**
27 * @var \TYPO3\CMS\Core\Core\ApplicationContext
28 */
29 protected $backupApplicationContext = null;
30
31 /**
32 * @var AbstractConditionMatcher|\PHPUnit_Framework_MockObject_MockObject
33 */
34 protected $conditionMatcher;
35
36 /**
37 * @var \ReflectionMethod
38 */
39 protected $evaluateConditionCommonMethod;
40
41 /**
42 * Set up
43 */
44 protected function setUp()
45 {
46 require_once('Fixtures/ConditionMatcherUserFuncs.php');
47
48 GeneralUtility::flushInternalRuntimeCaches();
49
50 $this->backupApplicationContext = GeneralUtility::getApplicationContext();
51 $this->conditionMatcher = $this->getMockForAbstractClass(AbstractConditionMatcher::class);
52 $this->evaluateConditionCommonMethod = new \ReflectionMethod(AbstractConditionMatcher::class, 'evaluateConditionCommon');
53 $this->evaluateConditionCommonMethod->setAccessible(true);
54 }
55
56 /**
57 * Tear down
58 */
59 protected function tearDown()
60 {
61 Fixtures\GeneralUtilityFixture::setApplicationContext($this->backupApplicationContext);
62 parent::tearDown();
63 }
64
65 /**
66 * Data provider with matching applicationContext conditions.
67 *
68 * @return array[]
69 */
70 public function matchingApplicationContextConditionsDataProvider()
71 {
72 return [
73 ['Production*'],
74 ['Production/Staging/*'],
75 ['Production/Staging/Server2'],
76 ['/^Production.*$/'],
77 ['/^Production\\/.+\\/Server\\d+$/'],
78 ];
79 }
80
81 /**
82 * @test
83 * @dataProvider matchingApplicationContextConditionsDataProvider
84 */
85 public function evaluateConditionCommonReturnsTrueForMatchingContexts($matchingContextCondition)
86 {
87 /** @var \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext */
88 $applicationContext = new ApplicationContext('Production/Staging/Server2');
89 Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
90
91 $this->assertTrue(
92 $this->evaluateConditionCommonMethod->invokeArgs($this->conditionMatcher, ['applicationContext', $matchingContextCondition])
93 );
94 }
95
96 /**
97 * Data provider with not matching applicationContext conditions.
98 *
99 * @return array[]
100 */
101 public function notMatchingApplicationContextConditionsDataProvider()
102 {
103 return [
104 ['Production'],
105 ['Testing*'],
106 ['Development/Profiling, Testing/Unit'],
107 ['Testing/Staging/Server2'],
108 ['/^Testing.*$/'],
109 ['/^Production\\/.+\\/Host\\d+$/'],
110 ];
111 }
112
113 /**
114 * @test
115 * @dataProvider notMatchingApplicationContextConditionsDataProvider
116 */
117 public function evaluateConditionCommonReturnsNullForNotMatchingApplicationContexts($notMatchingApplicationContextCondition)
118 {
119 /** @var \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext */
120 $applicationContext = new ApplicationContext('Production/Staging/Server2');
121 Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
122
123 $this->assertFalse(
124 $this->evaluateConditionCommonMethod->invokeArgs($this->conditionMatcher, ['applicationContext', $notMatchingApplicationContextCondition])
125 );
126 }
127
128 /**
129 * Data provider for evaluateConditionCommonEvaluatesIpAddressesCorrectly
130 *
131 * @return array[]
132 */
133 public function evaluateConditionCommonDevIpMaskDataProvider()
134 {
135 return [
136 // [0] $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']
137 // [1] Actual IP
138 // [2] Expected condition result
139 'IP matches' => [
140 '127.0.0.1',
141 '127.0.0.1',
142 true,
143 ],
144 'ipv4 wildcard subnet' => [
145 '127.0.0.1/24',
146 '127.0.0.2',
147 true,
148 ],
149 'ipv6 wildcard subnet' => [
150 '0:0::1/128',
151 '::1',
152 true,
153 ],
154 'List of addresses matches' => [
155 '1.2.3.4, 5.6.7.8',
156 '5.6.7.8',
157 true,
158 ],
159 'IP does not match' => [
160 '127.0.0.1',
161 '127.0.0.2',
162 false,
163 ],
164 'ipv4 subnet does not match' => [
165 '127.0.0.1/8',
166 '126.0.0.1',
167 false,
168 ],
169 'ipv6 subnet does not match' => [
170 '::1/127',
171 '::2',
172 false
173 ],
174 'List of addresses does not match' => [
175 '127.0.0.1, ::1',
176 '::2',
177 false,
178 ],
179 ];
180 }
181
182 /**
183 * @test
184 * @dataProvider evaluateConditionCommonDevIpMaskDataProvider
185 */
186 public function evaluateConditionCommonEvaluatesIpAddressesCorrectly($devIpMask, $actualIp, $expectedResult)
187 {
188 // Do not trigger proxy stuff of GeneralUtility::getIndPEnv
189 unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP']);
190
191 $_SERVER['REMOTE_ADDR'] = $actualIp;
192 $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = $devIpMask;
193
194 $actualResult = $this->evaluateConditionCommonMethod->invokeArgs($this->conditionMatcher, ['IP', 'devIP']);
195 $this->assertSame($expectedResult, $actualResult);
196 }
197
198 /**
199 * @test
200 */
201 public function testUserFuncIsCalled()
202 {
203 $this->assertTrue(
204 $this->evaluateConditionCommonMethod->invokeArgs(
205 $this->conditionMatcher,
206 ['userFunc', 'user_testFunction']
207 )
208 );
209 }
210
211 /**
212 * @test
213 */
214 public function testUserFuncWithSingleArgument()
215 {
216 $this->assertTrue(
217 $this->evaluateConditionCommonMethod->invokeArgs(
218 $this->conditionMatcher,
219 ['userFunc', 'user_testFunctionWithSingleArgument(x)']
220 )
221 );
222 }
223
224 /**
225 * @test
226 */
227 public function testUserFuncWithIntegerZeroArgument()
228 {
229 $this->assertTrue(
230 $this->evaluateConditionCommonMethod->invokeArgs(
231 $this->conditionMatcher,
232 ['userFunc', 'user_testFunctionWithSingleArgument(0)']
233 )
234 );
235 }
236
237 /**
238 * @test
239 */
240 public function testUserFuncWithWhitespaceArgument()
241 {
242 $this->assertTrue(
243 $this->evaluateConditionCommonMethod->invokeArgs(
244 $this->conditionMatcher,
245 ['userFunc', 'user_testFunctionWithNoArgument( )']
246 )
247 );
248 }
249
250 /**
251 * @test
252 */
253 public function testUserFuncWithMultipleArguments()
254 {
255 $this->assertTrue(
256 $this->evaluateConditionCommonMethod->invokeArgs(
257 $this->conditionMatcher,
258 ['userFunc', 'user_testFunctionWithThreeArguments(1,2,3)']
259 )
260 );
261 }
262
263 /**
264 * @test
265 */
266 public function testUserFuncWithMultipleDifferentArgumentsNullBoolString()
267 {
268 $this->assertTrue(
269 $this->evaluateConditionCommonMethod->invokeArgs(
270 $this->conditionMatcher,
271 ['userFunc', 'user_testFunctionWithThreeArguments(0,true,"foo")']
272 )
273 );
274 }
275
276 /**
277 * @test
278 */
279 public function testUserFuncWithMultipleDifferentArgumentsNullStringBool()
280 {
281 $this->assertTrue(
282 $this->evaluateConditionCommonMethod->invokeArgs(
283 $this->conditionMatcher,
284 ['userFunc', 'user_testFunctionWithThreeArguments(0,"foo",true)']
285 )
286 );
287 }
288
289 /**
290 * @test
291 */
292 public function testUserFuncWithMultipleDifferentArgumentsStringBoolNull()
293 {
294 $this->assertTrue(
295 $this->evaluateConditionCommonMethod->invokeArgs(
296 $this->conditionMatcher,
297 ['userFunc', 'user_testFunctionWithThreeArguments("foo",true,0)']
298 )
299 );
300 }
301
302 /**
303 * @test
304 */
305 public function testUserFuncWithMultipleDifferentArgumentsStringNullBool()
306 {
307 $this->assertTrue(
308 $this->evaluateConditionCommonMethod->invokeArgs(
309 $this->conditionMatcher,
310 ['userFunc', 'user_testFunctionWithThreeArguments("foo",0,true)']
311 )
312 );
313 }
314
315 /**
316 * @test
317 */
318 public function testUserFuncWithMultipleDifferentArgumentsBoolNullString()
319 {
320 $this->assertTrue(
321 $this->evaluateConditionCommonMethod->invokeArgs(
322 $this->conditionMatcher,
323 ['userFunc', 'user_testFunctionWithThreeArguments(true,0,"foo")']
324 )
325 );
326 }
327
328 /**
329 * @test
330 */
331 public function testUserFuncWithMultipleDifferentArgumentsBoolStringNull()
332 {
333 $this->assertTrue(
334 $this->evaluateConditionCommonMethod->invokeArgs(
335 $this->conditionMatcher,
336 ['userFunc', 'user_testFunctionWithThreeArguments(true,"foo",0)']
337 )
338 );
339 }
340
341 /**
342 * @test
343 */
344 public function testUserFuncWithMultipleDifferentArgumentsNullBoolStringSingleQuotes()
345 {
346 $this->assertTrue(
347 $this->evaluateConditionCommonMethod->invokeArgs(
348 $this->conditionMatcher,
349 ['userFunc', "user_testFunctionWithThreeArguments(0,true,'foo')"]
350 )
351 );
352 }
353
354 /**
355 * @test
356 */
357 public function testUserFuncWithMultipleDifferentArgumentsNullStringBoolSingleQuotes()
358 {
359 $this->assertTrue(
360 $this->evaluateConditionCommonMethod->invokeArgs(
361 $this->conditionMatcher,
362 ['userFunc', "user_testFunctionWithThreeArguments(0,'foo',true)"]
363 )
364 );
365 }
366
367 /**
368 * @test
369 */
370 public function testUserFuncWithMultipleDifferentArgumentsStringBoolNullSingleQuotes()
371 {
372 $this->assertTrue(
373 $this->evaluateConditionCommonMethod->invokeArgs(
374 $this->conditionMatcher,
375 ['userFunc', "user_testFunctionWithThreeArguments('foo',true,0)"]
376 )
377 );
378 }
379
380 /**
381 * @test
382 */
383 public function testUserFuncWithMultipleDifferentArgumentsStringNullBoolSingleQuotes()
384 {
385 $this->assertTrue(
386 $this->evaluateConditionCommonMethod->invokeArgs(
387 $this->conditionMatcher,
388 ['userFunc', "user_testFunctionWithThreeArguments('foo',0,true)"]
389 )
390 );
391 }
392
393 /**
394 * @test
395 */
396 public function testUserFuncWithMultipleDifferentArgumentsBoolNullStringSingleQuotes()
397 {
398 $this->assertTrue(
399 $this->evaluateConditionCommonMethod->invokeArgs(
400 $this->conditionMatcher,
401 ['userFunc', "user_testFunctionWithThreeArguments(true,0,'foo')"]
402 )
403 );
404 }
405
406 /**
407 * @test
408 */
409 public function testUserFuncWithMultipleDifferentArgumentsBoolStringNullSingleQuotes()
410 {
411 $this->assertTrue(
412 $this->evaluateConditionCommonMethod->invokeArgs(
413 $this->conditionMatcher,
414 ['userFunc', "user_testFunctionWithThreeArguments(true,'foo',0)"]
415 )
416 );
417 }
418
419 /**
420 * @test
421 */
422 public function testUserFuncWithMultipleSingleQuotedArguments()
423 {
424 $this->assertTrue(
425 $this->evaluateConditionCommonMethod->invokeArgs(
426 $this->conditionMatcher,
427 ['userFunc', "user_testFunctionWithThreeArguments('foo','bar', 'baz')"]
428 )
429 );
430 }
431
432 /**
433 * @test
434 */
435 public function testUserFuncWithMultipleSoubleQuotedArguments()
436 {
437 $this->assertTrue(
438 $this->evaluateConditionCommonMethod->invokeArgs(
439 $this->conditionMatcher,
440 ['userFunc', 'user_testFunctionWithThreeArguments("foo","bar","baz")']
441 )
442 );
443 }
444
445 /**
446 * @test
447 */
448 public function testUserFuncReturnsFalse()
449 {
450 $this->assertFalse(
451 $this->evaluateConditionCommonMethod->invokeArgs(
452 $this->conditionMatcher,
453 ['userFunc', 'user_testFunctionFalse']
454 )
455 );
456 }
457
458 /**
459 * @test
460 */
461 public function testUserFuncWithMultipleArgumentsAndQuotes()
462 {
463 $this->assertTrue(
464 $this->evaluateConditionCommonMethod->invokeArgs(
465 $this->conditionMatcher,
466 ['userFunc', 'user_testFunctionWithThreeArguments(1,2,"3,4,5,6")']
467 )
468 );
469 }
470
471 /**
472 * @test
473 */
474 public function testUserFuncWithMultipleArgumentsAndQuotesAndSpaces()
475 {
476 $this->assertTrue(
477 $this->evaluateConditionCommonMethod->invokeArgs(
478 $this->conditionMatcher,
479 ['userFunc', 'user_testFunctionWithThreeArguments ( 1 , 2, "3, 4, 5, 6" )']
480 )
481 );
482 }
483
484 /**
485 * @test
486 */
487 public function testUserFuncWithMultipleArgumentsAndQuotesAndSpacesStripped()
488 {
489 $this->assertTrue(
490 $this->evaluateConditionCommonMethod->invokeArgs(
491 $this->conditionMatcher,
492 ['userFunc', 'user_testFunctionWithThreeArgumentsSpaces ( 1 , 2, "3, 4, 5, 6" )']
493 )
494 );
495 }
496
497 /**
498 * @test
499 */
500 public function testUserFuncWithSpacesInQuotes()
501 {
502 $this->assertTrue(
503 $this->evaluateConditionCommonMethod->invokeArgs(
504 $this->conditionMatcher,
505 ['userFunc', 'user_testFunctionWithSpaces(" 3, 4, 5, 6 ")']
506 )
507 );
508 }
509
510 /**
511 * @test
512 */
513 public function testUserFuncWithMultipleArgumentsAndQuotesAndSpacesStrippedAndEscapes()
514 {
515 $this->assertTrue(
516 $this->evaluateConditionCommonMethod->invokeArgs(
517 $this->conditionMatcher,
518 ['userFunc', 'user_testFunctionWithThreeArgumentsSpaces ( 1 , 2, "3, \"4, 5\", 6" )']
519 )
520 );
521 }
522
523 /**
524 * @test
525 */
526 public function testUserFuncWithQuoteMissing()
527 {
528 $this->assertTrue(
529 $this->evaluateConditionCommonMethod->invokeArgs(
530 $this->conditionMatcher,
531 ['userFunc', 'user_testFunctionWithQuoteMissing ("value \")']
532 )
533 );
534 }
535
536 /**
537 * @test
538 */
539 public function testUserFuncWithQuotesInside()
540 {
541 $this->assertTrue(
542 $this->evaluateConditionCommonMethod->invokeArgs(
543 $this->conditionMatcher,
544 ['userFunc', 'user_testQuotes("1 \" 2")']
545 )
546 );
547 }
548
549 /**
550 * @test
551 */
552 public function testUserFuncWithClassMethodCall()
553 {
554 $this->assertTrue(
555 $this->evaluateConditionCommonMethod->invokeArgs(
556 $this->conditionMatcher,
557 ['userFunc', 'ConditionMatcherUserFunctions::isTrue(1)']
558 )
559 );
560 }
561 }