TYPO3 CMS  TYPO3_8-7
ConditionMatcherTest.php
Go to the documentation of this file.
1 <?php
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 
23 
27 class ConditionMatcherTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
28 {
33 
37  protected $matchCondition;
38 
39  protected function setUp()
40  {
41  $this->testGlobalNamespace = $this->getUniqueId('TEST');
43  $GLOBALS['TSFE'] = new \stdClass();
44  $GLOBALS['TSFE']->tmpl = new \stdClass();
45  $GLOBALS['TSFE']->tmpl->rootLine = [
46  2 => ['uid' => 121, 'pid' => 111],
47  1 => ['uid' => 111, 'pid' => 101],
48  0 => ['uid' => 101, 'pid' => 0]
49  ];
50  $GLOBALS['TSFE']->fe_user = new \stdClass();
51  $this->matchCondition = GeneralUtility::makeInstance(ConditionMatcher::class);
52  }
53 
60  {
61  $this->matchCondition->matchAll = false;
62  $this->assertFalse($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
63  }
64 
71  {
72  $this->matchCondition->setSimulateMatchResult(true);
73  $this->assertTrue($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
74  }
75 
82  {
83  $testCondition = '[' . $this->getUniqueId('test') . ' = Any condition to simulate a positive match]';
84  $this->matchCondition->setSimulateMatchConditions([$testCondition]);
85  $this->assertTrue($this->matchCondition->match($testCondition));
86  }
87 
94  {
95  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
96  $this->assertTrue($this->matchCondition->match('[language = *de*]'));
97  $this->assertTrue($this->matchCondition->match('[language = *de-de*]'));
98  }
99 
106  {
107  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
108  $this->assertTrue($this->matchCondition->match('[language = *en*,*de*]'));
109  $this->assertTrue($this->matchCondition->match('[language = *en-us*,*de-de*]'));
110  }
111 
118  {
119  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
120  $this->assertTrue($this->matchCondition->match('[language = de-de,de;q=0.8,en-us;q=0.5,en;q=0.3]'));
121  }
122 
129  {
130  $GLOBALS['TSFE']->gr_list = '13,14,15';
131  $this->assertTrue($this->matchCondition->match('[usergroup = 13]'));
132  }
133 
140  {
141  $GLOBALS['TSFE']->gr_list = '13,14,15';
142  $this->assertTrue($this->matchCondition->match('[usergroup = 999,15,14,13]'));
143  }
144 
151  {
152  $GLOBALS['TSFE']->gr_list = '0,-1';
153  $this->assertFalse($this->matchCondition->match('[usergroup = 0,-1]'));
154  }
155 
162  {
163  $GLOBALS['TSFE']->loginUser = true;
164  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
165  $this->assertTrue($this->matchCondition->match('[loginUser = *]'));
166  }
167 
174  {
175  $GLOBALS['TSFE']->loginUser = true;
176  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
177  $this->assertTrue($this->matchCondition->match('[loginUser = 13]'));
178  }
179 
186  {
187  $GLOBALS['TSFE']->loginUser = true;
188  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
189  $this->assertTrue($this->matchCondition->match('[loginUser = 999,13]'));
190  }
191 
198  {
199  $GLOBALS['TSFE']->loginUser = false;
200  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
201  $this->assertFalse($this->matchCondition->match('[loginUser = *]'));
202  $this->assertFalse($this->matchCondition->match('[loginUser = 13]'));
203  }
204 
211  {
212  $GLOBALS['TSFE']->loginUser = false;
213  $this->assertTrue($this->matchCondition->match('[loginUser = ]'));
214  }
215 
222  {
223  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10]'));
224  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1]'));
225  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10]'));
226  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1]'));
227  }
228 
235  {
236  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10|20|30]'));
237  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1|20.2|30.3]'));
238  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 = 10|20|30]'));
239  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 = 10.1|20.2|30.3]'));
240  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10|20|30]'));
241  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1|20.2|30.3]'));
242  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 == 10|20|30]'));
243  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 == 10.1|20.2|30.3]'));
244  }
245 
252  {
253  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
254  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
255  }
256 
263  {
264  $this->assertFalse($this->matchCondition->match('[globalVar = LIT:10 != 10]'));
265  }
266 
273  {
274  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20|30]'));
275  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2|20.3]'));
276  }
277 
284  {
285  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
286  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));
287  }
288 
295  {
296  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 10]'));
297  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 20]'));
298  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.1]'));
299  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.2]'));
300  }
301 
308  {
309  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 > 10]'));
310  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 > 10.1]'));
311  }
312 
319  {
320  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 >= 10]'));
321  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 >= 10]'));
322  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 >= 10.1]'));
323  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 >= 10.1]'));
324  }
325 
332  {
333  $testKey = $this->getUniqueId('test');
334  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
335  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
336  }
337 
344  {
345  $testKey = $this->getUniqueId('test');
346  $_GET = [];
347  $_POST = [$testKey => 0];
348  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
349  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
350  }
351 
358  {
359  $testKey = $this->getUniqueId('test');
360  $testValue = '1';
361  $_GET = [];
362  $_POST = [$testKey => ['0' => $testValue]];
363  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . '|0=' . $testValue . ']'));
364  }
365 
372  {
373  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.Test.Condition]'));
374  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3]'));
375  }
376 
383  {
384  $testKey = $this->getUniqueId('test');
385  $_GET = [];
386  $_POST = [$testKey => ''];
387  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . '=]'));
388  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . ' = ]'));
389  }
390 
397  {
398  $this->assertTrue($this->matchCondition->match('[globalString = LIT:=]'));
399  $this->assertTrue($this->matchCondition->match('[globalString = LIT: = ]'));
400  }
401 
408  {
409  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?Test?Condition]'));
410  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.T*t.Condition]'));
411  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?T*t?Condition]'));
412  }
413 
420  {
421  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^[A-Za-z3.]+$/]'));
422  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^TYPO3\\..+Condition$/]'));
423  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^FALSE/]'));
424  }
425 
432  {
433  $testKey = $this->getUniqueId('test');
434  $_SERVER[$testKey] = '';
435  $this->assertTrue($this->matchCondition->match('[globalString = _SERVER|' . $testKey . ' = /^$/]'));
436  }
437 
444  {
445  $this->assertTrue($this->matchCondition->match('[treeLevel = 2]'));
446  }
447 
454  {
455  $this->assertTrue($this->matchCondition->match('[treeLevel = 999,998,2]'));
456  }
457 
464  {
465  $this->assertFalse($this->matchCondition->match('[treeLevel = 999]'));
466  }
467 
474  {
475  $GLOBALS['TSFE']->id = 121;
476  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 111]'));
477  }
478 
485  {
486  $GLOBALS['TSFE']->id = 121;
487  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 999,111,101]'));
488  }
489 
496  {
497  $GLOBALS['TSFE']->id = 121;
498  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 999]'));
499  }
500 
507  {
508  $GLOBALS['TSFE']->id = 121;
509  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 121]'));
510  }
511 
518  {
519  $GLOBALS['TSFE']->id = 121;
520  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 111]'));
521  }
522 
529  {
530  $GLOBALS['TSFE']->id = 121;
531  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 999,111,101]'));
532  }
533 
540  {
541  $GLOBALS['TSFE']->id = 121;
542  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 121]'));
543  }
544 
551  {
552  $GLOBALS['TSFE']->id = 121;
553  $this->assertFalse($this->matchCondition->match('[PIDinRootline = 999]'));
554  }
555 
563  {
564  $this->assertTrue($this->matchCondition->match('[compatVersion = 7.0]'));
565  }
566 
574  {
575  $this->assertTrue($this->matchCondition->match('[compatVersion = ' . TYPO3_branch . ']'));
576  }
577 
585  {
586  $this->assertFalse($this->matchCondition->match('[compatVersion = 15.0]'));
587  }
588 
595  {
596  $_GET = ['testGet' => 'getTest'];
597  $_POST = ['testPost' => 'postTest'];
598  $this->assertTrue($this->matchCondition->match('[globalString = GP:testGet = getTest]'));
599  $this->assertTrue($this->matchCondition->match('[globalString = GP:testPost = postTest]'));
600  }
601 
608  {
609  $GLOBALS['TSFE']->id = 1234567;
610  $GLOBALS['TSFE']->testSimpleObject = new \stdClass();
611  $GLOBALS['TSFE']->testSimpleObject->testSimpleVariable = 'testValue';
612 
613  $prophecy = $this->prophesize(FrontendUserAuthentication::class);
614  $prophecy->getSessionData(Argument::exact('foo'))->willReturn(['bar' => 1234567]);
615  $GLOBALS['TSFE']->fe_user = $prophecy->reveal();
616 
617  $this->assertTrue($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
618  $this->assertTrue($this->matchCondition->match('[globalString = TSFE:testSimpleObject|testSimpleVariable = testValue]'));
619  $this->assertTrue($this->matchCondition->match('[globalString = TSFE:fe_user|sesData|foo|bar = 1234567]'));
620  }
621 
628  {
629  $testKey = $this->getUniqueId('test');
630  putenv($testKey . '=testValue');
631  $this->assertTrue($this->matchCondition->match('[globalString = ENV:' . $testKey . ' = testValue]'));
632  }
633 
640  {
641  $_SERVER['HTTP_HOST'] = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY') . ':1234567';
643  $this->assertTrue($this->matchCondition->match('[globalString = IENV:TYPO3_PORT = 1234567]'));
644  }
645 
652  {
654  'first' => 'testFirst',
655  'second' => ['third' => 'testThird']
656  ];
657  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|first = testFirst]'));
658  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|second|third = testThird]'));
659  }
660 
665  {
666  $this->expectException(InvalidTypoScriptConditionException::class);
667  $this->expectExceptionCode(1410286153);
668  $this->matchCondition->match('[stdClass = foo]');
669  }
670 
675  {
676  $this->expectException(TestConditionException::class);
677  $this->expectExceptionCode(1411581139);
678  $this->matchCondition->match('[TYPO3\\CMS\\Frontend\\Tests\\Unit\\Configuration\\TypoScript\\ConditionMatching\\Fixtures\\TestCondition = 7, != 6]');
679  }
680 }
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']