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 
20 
24 class ConditionMatcherTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
29  protected $rootline;
30 
34  protected $matchCondition;
35 
40 
44  protected $testTableName;
45 
49  protected function setUp()
50  {
51  $this->testTableName = 'conditionMatcherTestTable';
52  $this->testGlobalNamespace = $this->getUniqueId('TEST');
53  $GLOBALS['TCA'][$this->testTableName] = ['ctrl' => []];
55  $this->setUpBackend();
56  $this->matchCondition = $this->getMockBuilder(\TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher::class)
57  ->setMethods(['determineRootline'])
58  ->disableOriginalConstructor()
59  ->getMock();
60  }
61 
65  private function setUpBackend()
66  {
67  $this->rootline = [
68  2 => ['uid' => 121, 'pid' => 111],
69  1 => ['uid' => 111, 'pid' => 101],
70  0 => ['uid' => 101, 'pid' => 0]
71  ];
72  $GLOBALS['BE_USER'] = $this->getMockBuilder(BackendUserAuthentication::class)
73  ->setMethods(['dummy'])
74  ->disableOriginalConstructor()
75  ->getMock();
76  }
77 
82  {
83  $this->matchCondition = $this->getMockBuilder(\TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher::class)
84  ->setMethods(['determineRootline', 'determinePageId'])
85  ->disableOriginalConstructor()
86  ->getMock();
87 
88  $this->matchCondition->expects($this->once())->method('determineRootline');
89  $this->matchCondition->expects($this->once())->method('determinePageId')->willReturn(999);
90  }
91 
98  {
99  $this->assertFalse($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
100  }
101 
108  {
109  $this->matchCondition->setSimulateMatchResult(true);
110  $this->assertTrue($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
111  }
112 
119  {
120  $testCondition = '[' . $this->getUniqueId('test') . ' = Any condition to simulate a positive match]';
121  $this->matchCondition->setSimulateMatchConditions([$testCondition]);
122  $this->assertTrue($this->matchCondition->match($testCondition));
123  }
124 
131  {
132  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
133  $this->assertTrue($this->matchCondition->match('[language = *de*]'));
134  $this->assertTrue($this->matchCondition->match('[language = *de-de*]'));
135  }
136 
143  {
144  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
145  $this->assertTrue($this->matchCondition->match('[language = *en*,*de*]'));
146  $this->assertTrue($this->matchCondition->match('[language = *en-us*,*de-de*]'));
147  }
148 
155  {
156  $this->markTestSkipped('This comparison seems to be incomplete in \TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher.');
157  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
158  $this->assertTrue($this->matchCondition->match('[language = de-de,de;q=0.8]'));
159  }
160 
167  {
168  $GLOBALS['BE_USER']->groupList = '13,14,15';
169  $this->assertTrue($this->matchCondition->match('[usergroup = 13]'));
170  }
171 
178  {
179  $GLOBALS['BE_USER']->groupList = '13,14,15';
180  $this->assertTrue($this->matchCondition->match('[usergroup = 999,15,14,13]'));
181  }
182 
189  {
190  $GLOBALS['BE_USER']->user['uid'] = 13;
191  $this->assertTrue($this->matchCondition->match('[loginUser = *]'));
192  }
193 
200  {
201  $GLOBALS['BE_USER']->user['uid'] = 13;
202  $this->assertTrue($this->matchCondition->match('[loginUser = 13]'));
203  }
204 
211  {
212  $GLOBALS['BE_USER']->user['uid'] = 13;
213  $this->assertFalse($this->matchCondition->match('[loginUser = 999]'));
214  }
215 
222  {
223  $GLOBALS['BE_USER']->user['uid'] = 13;
224  $this->assertTrue($this->matchCondition->match('[loginUser = 999,13]'));
225  }
226 
233  {
234  $GLOBALS['BE_USER']->user['uid'] = 13;
235  $GLOBALS['BE_USER']->user['admin'] = 1;
236  $this->assertTrue($this->matchCondition->match('[adminUser = 1]'));
237  }
238 
245  {
246  $GLOBALS['BE_USER']->user['uid'] = 14;
247  $GLOBALS['BE_USER']->user['admin'] = 0;
248  $this->assertTrue($this->matchCondition->match('[adminUser = 0]'));
249  }
250 
257  {
258  $GLOBALS['BE_USER']->user['uid'] = 14;
259  $GLOBALS['BE_USER']->user['admin'] = 0;
260  $this->assertFalse($this->matchCondition->match('[adminUser = 1]'));
261  }
262 
269  {
270  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10]'), '1');
271  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1]'), '2');
272  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10]'), '3');
273  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1]'), '4');
274  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:0 = 0]'), '5');
275  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:0 == 0]'), '6');
276  }
277 
284  {
285  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10|20|30]'));
286  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1|20.2|30.3]'));
287  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 = 10|20|30]'));
288  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 = 10.1|20.2|30.3]'));
289  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10|20|30]'));
290  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1|20.2|30.3]'));
291  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 == 10|20|30]'));
292  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 == 10.1|20.2|30.3]'));
293  }
294 
301  {
302  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
303  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
304  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:0 != 1]'));
305  }
306 
313  {
314  $this->assertFalse($this->matchCondition->match('[globalVar = LIT:10 != 10]'));
315  }
316 
323  {
324  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20|30]'));
325  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2|20.3]'));
326  }
327 
334  {
335  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
336  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));
337  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:0 < 1]'));
338  }
339 
346  {
347  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 10]'));
348  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 20]'));
349  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.1]'));
350  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.2]'));
351  }
352 
359  {
360  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 > 10]'));
361  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 > 10.1]'));
362  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:1 > 0]'));
363  }
364 
371  {
372  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 >= 10]'));
373  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 >= 10]'));
374  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 >= 10.1]'));
375  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 >= 10.1]'));
376  }
377 
384  {
385  $testKey = $this->getUniqueId('test');
386  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
387  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
388  }
389 
396  {
397  $testKey = $this->getUniqueId('test');
398  $_GET = [];
399  $_POST = [$testKey => 0];
400  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
401  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
402  }
403 
410  {
411  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.Test.Condition]'));
412  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3]'));
413  }
414 
421  {
422  $testKey = $this->getUniqueId('test');
423  $_GET = [];
424  $_POST = [$testKey => ''];
425  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . '=]'));
426  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . ' = ]'));
427  }
428 
435  {
436  $this->assertTrue($this->matchCondition->match('[globalString = LIT:=]'));
437  $this->assertTrue($this->matchCondition->match('[globalString = LIT: = ]'));
438  }
439 
446  {
447  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?Test?Condition]'));
448  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.T*t.Condition]'));
449  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?T*t?Condition]'));
450  }
451 
458  {
459  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^[A-Za-z3.]+$/]'));
460  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^TYPO3\\..+Condition$/]'));
461  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^FALSE/]'));
462  }
463 
470  {
471  $testKey = $this->getUniqueId('test');
472  $_SERVER[$testKey] = '';
473  $this->assertTrue($this->matchCondition->match('[globalString = _SERVER|' . $testKey . ' = /^$/]'));
474  }
475 
482  {
483  $this->matchCondition->setRootline($this->rootline);
484  $this->assertTrue($this->matchCondition->match('[treeLevel = 2]'));
485  }
486 
493  {
494  $this->matchCondition->setRootline($this->rootline);
495  $this->assertTrue($this->matchCondition->match('[treeLevel = 999,998,2]'));
496  }
497 
504  {
505  $this->matchCondition->setRootline($this->rootline);
506  $this->assertFalse($this->matchCondition->match('[treeLevel = 999]'));
507  }
508 
515  {
516  $GLOBALS['SOBE'] = $this->createMock(\TYPO3\CMS\Backend\Controller\EditDocumentController::class);
517  $GLOBALS['SOBE']->elementsData = [
518  [
519  'table' => 'pages',
520  'uid' => 'NEW4adc6021e37e7',
521  'pid' => 121,
522  'cmd' => 'new',
523  'deleteAccess' => 0
524  ]
525  ];
526  $GLOBALS['SOBE']->data = [];
527  $this->matchCondition->setRootline($this->rootline);
528  $this->matchCondition->setPageId(121);
529  $this->assertTrue($this->matchCondition->match('[treeLevel = 3]'));
530  }
531 
538  {
539  $GLOBALS['SOBE'] = $this->createMock(\TYPO3\CMS\Backend\Controller\EditDocumentController::class);
540  $GLOBALS['SOBE']->elementsData = [
541  [
542  'table' => 'pages',
544  'uid' => 999,
545  'pid' => 121,
546  'cmd' => 'edit',
547  'deleteAccess' => 1
548  ]
549  ];
550  $GLOBALS['SOBE']->data = [
551  'pages' => [
552  'NEW4adc6021e37e7' => [
553  'pid' => 121
554  ]
555  ]
556  ];
557  $this->matchCondition->setRootline($this->rootline);
558  $this->matchCondition->setPageId(121);
559  $this->assertTrue($this->matchCondition->match('[treeLevel = 3]'));
560  }
561 
568  {
569  $this->matchCondition->setRootline($this->rootline);
570  $this->matchCondition->setPageId(121);
571  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 111]'));
572  }
573 
580  {
581  $this->matchCondition->setRootline($this->rootline);
582  $this->matchCondition->setPageId(121);
583  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 999,111,101]'));
584  }
585 
592  {
593  $this->matchCondition->setRootline($this->rootline);
594  $this->matchCondition->setPageId(121);
595  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 999]'));
596  }
597 
604  {
605  $this->matchCondition->setRootline($this->rootline);
606  $this->matchCondition->setPageId(121);
607  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 121]'));
608  }
609 
616  {
617  $GLOBALS['SOBE'] = $this->createMock(\TYPO3\CMS\Backend\Controller\EditDocumentController::class);
618  $GLOBALS['SOBE']->elementsData = [
619  [
620  'table' => 'pages',
621  'uid' => 'NEW4adc6021e37e7',
622  'pid' => 121,
623  'cmd' => 'new',
624  'deleteAccess' => 0
625  ]
626  ];
627  $GLOBALS['SOBE']->data = [];
628  $this->matchCondition->setRootline($this->rootline);
629  $this->matchCondition->setPageId(121);
630  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 121]'));
631  }
632 
639  {
640  $GLOBALS['SOBE'] = $this->createMock(\TYPO3\CMS\Backend\Controller\EditDocumentController::class);
641  $GLOBALS['SOBE']->elementsData = [
642  [
643  'table' => 'pages',
645  'uid' => 999,
646  'pid' => 121,
647  'cmd' => 'edit',
648  'deleteAccess' => 1
649  ]
650  ];
651  $GLOBALS['SOBE']->data = [
652  'pages' => [
653  'NEW4adc6021e37e7' => [
654  'pid' => 121
655  ]
656  ]
657  ];
658  $this->matchCondition->setRootline($this->rootline);
659  $this->matchCondition->setPageId(121);
660  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 121]'));
661  }
662 
669  {
670  $this->matchCondition->setRootline($this->rootline);
671  $this->matchCondition->setPageId(121);
672  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 111]'));
673  }
674 
681  {
682  $this->matchCondition->setRootline($this->rootline);
683  $this->matchCondition->setPageId(121);
684  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 999,111,101]'));
685  }
686 
693  {
694  $this->matchCondition->setRootline($this->rootline);
695  $this->matchCondition->setPageId(121);
696  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 121]'));
697  }
698 
705  {
706  $this->matchCondition->setRootline($this->rootline);
707  $this->matchCondition->setPageId(121);
708  $this->assertFalse($this->matchCondition->match('[PIDinRootline = 999]'));
709  }
710 
718  {
719  $this->assertTrue($this->matchCondition->match('[compatVersion = 7.0]'));
720  }
721 
729  {
730  $this->assertTrue($this->matchCondition->match('[compatVersion = ' . TYPO3_branch . ']'));
731  }
732 
740  {
741  $this->assertFalse($this->matchCondition->match('[compatVersion = 15.0]'));
742  }
743 
750  {
751  $_GET = ['testGet' => 'getTest'];
752  $_POST = ['testPost' => 'postTest'];
753  $this->assertTrue($this->matchCondition->match('[globalString = GP:testGet = getTest]'));
754  $this->assertTrue($this->matchCondition->match('[globalString = GP:testPost = postTest]'));
755  }
756 
764  {
765  $GLOBALS['TSFE'] = new \stdClass();
766  $GLOBALS['TSFE']->id = 1234567;
767  $this->assertFalse($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
768  }
769 
776  {
777  $testKey = $this->getUniqueId('test');
778  putenv($testKey . '=testValue');
779  $this->assertTrue($this->matchCondition->match('[globalString = ENV:' . $testKey . ' = testValue]'));
780  }
781 
788  {
789  $_SERVER['HTTP_HOST'] = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY') . ':1234567';
791  $this->assertTrue($this->matchCondition->match('[globalString = IENV:TYPO3_PORT = 1234567]'));
792  }
793 
800  {
802  'first' => 'testFirst',
803  'second' => ['third' => 'testThird']
804  ];
805  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|first = testFirst]'));
806  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|second|third = testThird]'));
807  }
808 
815  {
816  $_GET['id'] = 999;
817  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
818  $this->assertEquals(999, $this->matchCondition->getPageId());
819  }
820 
827  {
828  $_GET['edit']['pages'][999] = 'edit';
829  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
830  $this->assertEquals(999, $this->matchCondition->getPageId());
831  }
832 
839  {
841  $_GET['edit'][$this->testTableName][13] = 'edit';
842  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
843  $this->assertEquals(999, $this->matchCondition->getPageId());
844  }
845 
852  {
853  $_GET['edit']['pages'][999] = 'new';
854  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
855  $this->assertEquals(999, $this->matchCondition->getPageId());
856  }
857 
864  {
866  $_GET['edit'][$this->testTableName][-13] = 'new';
867  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
868  $this->assertEquals(999, $this->matchCondition->getPageId());
869  }
870 
877  {
878  $_GET['cmd']['pages'][999]['delete'] = 1;
879  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
880  $this->assertEquals(999, $this->matchCondition->getPageId());
881  }
882 
889  {
890  $_GET['cmd']['pages'][121]['copy'] = 999;
891  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
892  $this->assertEquals(999, $this->matchCondition->getPageId());
893  }
894 
901  {
903  $_GET['cmd'][$this->testTableName][121]['copy'] = -13;
904  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
905  $this->assertEquals(999, $this->matchCondition->getPageId());
906  }
907 
914  {
915  $_GET['cmd']['pages'][121]['move'] = 999;
916  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
917  $this->assertEquals(999, $this->matchCondition->getPageId());
918  }
919 
924  {
925  $this->expectException(TestConditionException::class);
926  $this->expectExceptionCode(1476109533);
927  $this->matchCondition->match('[TYPO3\\CMS\\Backend\\Tests\\Unit\\Configuration\\TypoScript\\ConditionMatching\\Fixtures\\TestCondition = 7, != 6]');
928  }
929 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']