TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
frontend/Tests/Unit/Configuration/TypoScript/ConditionMatching/ConditionMatcherTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Frontend\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 
22 
27 {
32 
36  protected $matchCondition;
37 
38  protected function setUp()
39  {
40  $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  $this->matchCondition = GeneralUtility::makeInstance(ConditionMatcher::class);
51  }
52 
59  {
60  $this->matchCondition->matchAll = false;
61  $this->assertFalse($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
62  }
63 
70  {
71  $this->matchCondition->setSimulateMatchResult(true);
72  $this->assertTrue($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
73  }
74 
81  {
82  $testCondition = '[' . $this->getUniqueId('test') . ' = Any condition to simulate a positive match]';
83  $this->matchCondition->setSimulateMatchConditions([$testCondition]);
84  $this->assertTrue($this->matchCondition->match($testCondition));
85  }
86 
93  {
94  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
95  $this->assertTrue($this->matchCondition->match('[language = *de*]'));
96  $this->assertTrue($this->matchCondition->match('[language = *de-de*]'));
97  }
98 
105  {
106  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
107  $this->assertTrue($this->matchCondition->match('[language = *en*,*de*]'));
108  $this->assertTrue($this->matchCondition->match('[language = *en-us*,*de-de*]'));
109  }
110 
117  {
118  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
119  $this->assertTrue($this->matchCondition->match('[language = de-de,de;q=0.8,en-us;q=0.5,en;q=0.3]'));
120  }
121 
128  {
129  $GLOBALS['TSFE']->gr_list = '13,14,15';
130  $this->assertTrue($this->matchCondition->match('[usergroup = 13]'));
131  }
132 
139  {
140  $GLOBALS['TSFE']->gr_list = '13,14,15';
141  $this->assertTrue($this->matchCondition->match('[usergroup = 999,15,14,13]'));
142  }
143 
150  {
151  $GLOBALS['TSFE']->gr_list = '0,-1';
152  $this->assertFalse($this->matchCondition->match('[usergroup = 0,-1]'));
153  }
154 
161  {
162  $GLOBALS['TSFE']->loginUser = true;
163  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
164  $this->assertTrue($this->matchCondition->match('[loginUser = *]'));
165  }
166 
173  {
174  $GLOBALS['TSFE']->loginUser = true;
175  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
176  $this->assertTrue($this->matchCondition->match('[loginUser = 13]'));
177  }
178 
185  {
186  $GLOBALS['TSFE']->loginUser = true;
187  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
188  $this->assertTrue($this->matchCondition->match('[loginUser = 999,13]'));
189  }
190 
197  {
198  $GLOBALS['TSFE']->loginUser = false;
199  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
200  $this->assertFalse($this->matchCondition->match('[loginUser = *]'));
201  $this->assertFalse($this->matchCondition->match('[loginUser = 13]'));
202  }
203 
210  {
211  $GLOBALS['TSFE']->loginUser = false;
212  $this->assertTrue($this->matchCondition->match('[loginUser = ]'));
213  }
214 
221  {
222  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10]'));
223  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1]'));
224  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10]'));
225  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1]'));
226  }
227 
234  {
235  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10|20|30]'));
236  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1|20.2|30.3]'));
237  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 = 10|20|30]'));
238  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 = 10.1|20.2|30.3]'));
239  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10|20|30]'));
240  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1|20.2|30.3]'));
241  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 == 10|20|30]'));
242  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 == 10.1|20.2|30.3]'));
243  }
244 
251  {
252  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
253  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
254  }
255 
262  {
263  $this->assertFalse($this->matchCondition->match('[globalVar = LIT:10 != 10]'));
264  }
265 
272  {
273  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20|30]'));
274  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2|20.3]'));
275  }
276 
283  {
284  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
285  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));
286  }
287 
294  {
295  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 10]'));
296  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 20]'));
297  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.1]'));
298  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.2]'));
299  }
300 
307  {
308  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 > 10]'));
309  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 > 10.1]'));
310  }
311 
318  {
319  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 >= 10]'));
320  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 >= 10]'));
321  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 >= 10.1]'));
322  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 >= 10.1]'));
323  }
324 
331  {
332  $testKey = $this->getUniqueId('test');
333  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
334  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
335  }
336 
343  {
344  $testKey = $this->getUniqueId('test');
345  $_GET = [];
346  $_POST = [$testKey => 0];
347  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
348  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
349  }
350 
357  {
358  $testKey = $this->getUniqueId('test');
359  $testValue = '1';
360  $_GET = [];
361  $_POST = [$testKey => ['0' => $testValue]];
362  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . '|0=' . $testValue . ']'));
363  }
364 
371  {
372  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.Test.Condition]'));
373  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3]'));
374  }
375 
382  {
383  $testKey = $this->getUniqueId('test');
384  $_GET = [];
385  $_POST = [$testKey => ''];
386  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . '=]'));
387  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . ' = ]'));
388  }
389 
396  {
397  $this->assertTrue($this->matchCondition->match('[globalString = LIT:=]'));
398  $this->assertTrue($this->matchCondition->match('[globalString = LIT: = ]'));
399  }
400 
407  {
408  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?Test?Condition]'));
409  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.T*t.Condition]'));
410  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?T*t?Condition]'));
411  }
412 
419  {
420  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^[A-Za-z3.]+$/]'));
421  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^TYPO3\\..+Condition$/]'));
422  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^FALSE/]'));
423  }
424 
431  {
432  $testKey = $this->getUniqueId('test');
433  $_SERVER[$testKey] = '';
434  $this->assertTrue($this->matchCondition->match('[globalString = _SERVER|' . $testKey . ' = /^$/]'));
435  }
436 
443  {
444  $this->assertTrue($this->matchCondition->match('[treeLevel = 2]'));
445  }
446 
453  {
454  $this->assertTrue($this->matchCondition->match('[treeLevel = 999,998,2]'));
455  }
456 
463  {
464  $this->assertFalse($this->matchCondition->match('[treeLevel = 999]'));
465  }
466 
473  {
474  $GLOBALS['TSFE']->id = 121;
475  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 111]'));
476  }
477 
484  {
485  $GLOBALS['TSFE']->id = 121;
486  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 999,111,101]'));
487  }
488 
495  {
496  $GLOBALS['TSFE']->id = 121;
497  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 999]'));
498  }
499 
506  {
507  $GLOBALS['TSFE']->id = 121;
508  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 121]'));
509  }
510 
517  {
518  $GLOBALS['TSFE']->id = 121;
519  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 111]'));
520  }
521 
528  {
529  $GLOBALS['TSFE']->id = 121;
530  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 999,111,101]'));
531  }
532 
539  {
540  $GLOBALS['TSFE']->id = 121;
541  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 121]'));
542  }
543 
550  {
551  $GLOBALS['TSFE']->id = 121;
552  $this->assertFalse($this->matchCondition->match('[PIDinRootline = 999]'));
553  }
554 
562  {
563  $this->assertTrue($this->matchCondition->match('[compatVersion = 7.0]'));
564  }
565 
573  {
574  $this->assertTrue($this->matchCondition->match('[compatVersion = ' . TYPO3_branch . ']'));
575  }
576 
584  {
585  $this->assertFalse($this->matchCondition->match('[compatVersion = 15.0]'));
586  }
587 
594  {
595  $_GET = ['testGet' => 'getTest'];
596  $_POST = ['testPost' => 'postTest'];
597  $this->assertTrue($this->matchCondition->match('[globalString = GP:testGet = getTest]'));
598  $this->assertTrue($this->matchCondition->match('[globalString = GP:testPost = postTest]'));
599  }
600 
607  {
608  $GLOBALS['TSFE']->id = 1234567;
609  $GLOBALS['TSFE']->testSimpleObject = new \stdClass();
610  $GLOBALS['TSFE']->testSimpleObject->testSimpleVariable = 'testValue';
611  $this->assertTrue($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
612  $this->assertTrue($this->matchCondition->match('[globalString = TSFE:testSimpleObject|testSimpleVariable = testValue]'));
613  }
614 
621  {
622  $testKey = $this->getUniqueId('test');
623  putenv($testKey . '=testValue');
624  $this->assertTrue($this->matchCondition->match('[globalString = ENV:' . $testKey . ' = testValue]'));
625  }
626 
633  {
634  $_SERVER['HTTP_HOST'] = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY') . ':1234567';
635  $this->assertTrue($this->matchCondition->match('[globalString = IENV:TYPO3_PORT = 1234567]'));
636  }
637 
644  {
646  'first' => 'testFirst',
647  'second' => ['third' => 'testThird']
648  ];
649  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|first = testFirst]'));
650  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|second|third = testThird]'));
651  }
652 
657  {
658  $this->expectException(InvalidTypoScriptConditionException::class);
659  $this->expectExceptionCode(1410286153);
660  $this->matchCondition->match('[stdClass = foo]');
661  }
662 
667  {
668  $this->expectException(TestConditionException::class);
669  $this->expectExceptionCode(1411581139);
670  $this->matchCondition->match('[TYPO3\\CMS\\Frontend\\Tests\\Unit\\Configuration\\TypoScript\\ConditionMatching\\Fixtures\\TestCondition = 7, != 6]');
671  }
672 }
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)