TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
backend/Tests/Unit/Configuration/TypoScript/ConditionMatching/ConditionMatcherTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\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 
20 
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' => []];
56  $this->setUpBackend();
57  $this->matchCondition = $this->getMockBuilder(\TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher::class)
58  ->setMethods(['determineRootline'])
59  ->disableOriginalConstructor()
60  ->getMock();
61  }
62 
66  private function setUpBackend()
67  {
68  $this->rootline = [
69  2 => ['uid' => 121, 'pid' => 111],
70  1 => ['uid' => 111, 'pid' => 101],
71  0 => ['uid' => 101, 'pid' => 0]
72  ];
73  $GLOBALS['BE_USER'] = $this->getMockBuilder(BackendUserAuthentication::class)
74  ->setMethods(['dummy'])
75  ->disableOriginalConstructor()
76  ->getMock();
77  }
78 
83  {
84  $this->matchCondition = $this->getMockBuilder(\TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher::class)
85  ->setMethods(['determineRootline', 'determinePageId'])
86  ->disableOriginalConstructor()
87  ->getMock();
88 
89  $this->matchCondition->expects($this->once())->method('determineRootline');
90  $this->matchCondition->expects($this->once())->method('determinePageId')->willReturn(999);
91  }
92 
99  {
100  $this->assertFalse($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
101  }
102 
109  {
110  $this->matchCondition->setSimulateMatchResult(true);
111  $this->assertTrue($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
112  }
113 
120  {
121  $testCondition = '[' . $this->getUniqueId('test') . ' = Any condition to simulate a positive match]';
122  $this->matchCondition->setSimulateMatchConditions([$testCondition]);
123  $this->assertTrue($this->matchCondition->match($testCondition));
124  }
125 
132  {
133  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
134  $this->assertTrue($this->matchCondition->match('[language = *de*]'));
135  $this->assertTrue($this->matchCondition->match('[language = *de-de*]'));
136  }
137 
144  {
145  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
146  $this->assertTrue($this->matchCondition->match('[language = *en*,*de*]'));
147  $this->assertTrue($this->matchCondition->match('[language = *en-us*,*de-de*]'));
148  }
149 
156  {
157  $this->markTestSkipped('This comparison seems to be incomplete in \TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher.');
158  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
159  $this->assertTrue($this->matchCondition->match('[language = de-de,de;q=0.8]'));
160  }
161 
168  {
169  $GLOBALS['BE_USER']->groupList = '13,14,15';
170  $this->assertTrue($this->matchCondition->match('[usergroup = 13]'));
171  }
172 
179  {
180  $GLOBALS['BE_USER']->groupList = '13,14,15';
181  $this->assertTrue($this->matchCondition->match('[usergroup = 999,15,14,13]'));
182  }
183 
190  {
191  $GLOBALS['BE_USER']->user['uid'] = 13;
192  $this->assertTrue($this->matchCondition->match('[loginUser = *]'));
193  }
194 
201  {
202  $GLOBALS['BE_USER']->user['uid'] = 13;
203  $this->assertTrue($this->matchCondition->match('[loginUser = 13]'));
204  }
205 
212  {
213  $GLOBALS['BE_USER']->user['uid'] = 13;
214  $this->assertFalse($this->matchCondition->match('[loginUser = 999]'));
215  }
216 
223  {
224  $GLOBALS['BE_USER']->user['uid'] = 13;
225  $this->assertTrue($this->matchCondition->match('[loginUser = 999,13]'));
226  }
227 
234  {
235  $GLOBALS['BE_USER']->user['uid'] = 13;
236  $GLOBALS['BE_USER']->user['admin'] = 1;
237  $this->assertTrue($this->matchCondition->match('[adminUser = 1]'));
238  }
239 
246  {
247  $GLOBALS['BE_USER']->user['uid'] = 14;
248  $GLOBALS['BE_USER']->user['admin'] = 0;
249  $this->assertTrue($this->matchCondition->match('[adminUser = 0]'));
250  }
251 
258  {
259  $GLOBALS['BE_USER']->user['uid'] = 14;
260  $GLOBALS['BE_USER']->user['admin'] = 0;
261  $this->assertFalse($this->matchCondition->match('[adminUser = 1]'));
262  }
263 
270  {
271  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10]'), '1');
272  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1]'), '2');
273  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10]'), '3');
274  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1]'), '4');
275  }
276 
283  {
284  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10|20|30]'));
285  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1|20.2|30.3]'));
286  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 = 10|20|30]'));
287  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 = 10.1|20.2|30.3]'));
288  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10|20|30]'));
289  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1|20.2|30.3]'));
290  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 == 10|20|30]'));
291  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 == 10.1|20.2|30.3]'));
292  }
293 
300  {
301  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
302  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
303  }
304 
311  {
312  $this->assertFalse($this->matchCondition->match('[globalVar = LIT:10 != 10]'));
313  }
314 
321  {
322  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20|30]'));
323  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2|20.3]'));
324  }
325 
332  {
333  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
334  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));
335  }
336 
343  {
344  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 10]'));
345  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 20]'));
346  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.1]'));
347  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.2]'));
348  }
349 
356  {
357  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 > 10]'));
358  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 > 10.1]'));
359  }
360 
367  {
368  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 >= 10]'));
369  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 >= 10]'));
370  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 >= 10.1]'));
371  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 >= 10.1]'));
372  }
373 
380  {
381  $testKey = $this->getUniqueId('test');
382  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
383  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
384  }
385 
392  {
393  $testKey = $this->getUniqueId('test');
394  $_GET = [];
395  $_POST = [$testKey => 0];
396  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
397  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
398  }
399 
406  {
407  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.Test.Condition]'));
408  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3]'));
409  }
410 
417  {
418  $testKey = $this->getUniqueId('test');
419  $_GET = [];
420  $_POST = [$testKey => ''];
421  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . '=]'));
422  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . ' = ]'));
423  }
424 
431  {
432  $this->assertTrue($this->matchCondition->match('[globalString = LIT:=]'));
433  $this->assertTrue($this->matchCondition->match('[globalString = LIT: = ]'));
434  }
435 
442  {
443  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?Test?Condition]'));
444  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.T*t.Condition]'));
445  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?T*t?Condition]'));
446  }
447 
454  {
455  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^[A-Za-z3.]+$/]'));
456  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^TYPO3\\..+Condition$/]'));
457  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^FALSE/]'));
458  }
459 
466  {
467  $testKey = $this->getUniqueId('test');
468  $_SERVER[$testKey] = '';
469  $this->assertTrue($this->matchCondition->match('[globalString = _SERVER|' . $testKey . ' = /^$/]'));
470  }
471 
478  {
479  $this->matchCondition->setRootline($this->rootline);
480  $this->assertTrue($this->matchCondition->match('[treeLevel = 2]'));
481  }
482 
489  {
490  $this->matchCondition->setRootline($this->rootline);
491  $this->assertTrue($this->matchCondition->match('[treeLevel = 999,998,2]'));
492  }
493 
500  {
501  $this->matchCondition->setRootline($this->rootline);
502  $this->assertFalse($this->matchCondition->match('[treeLevel = 999]'));
503  }
504 
511  {
512  $GLOBALS['SOBE'] = $this->createMock(\TYPO3\CMS\Backend\Controller\EditDocumentController::class);
513  $GLOBALS['SOBE']->elementsData = [
514  [
515  'table' => 'pages',
516  'uid' => 'NEW4adc6021e37e7',
517  'pid' => 121,
518  'cmd' => 'new',
519  'deleteAccess' => 0
520  ]
521  ];
522  $GLOBALS['SOBE']->data = [];
523  $this->matchCondition->setRootline($this->rootline);
524  $this->matchCondition->setPageId(121);
525  $this->assertTrue($this->matchCondition->match('[treeLevel = 3]'));
526  }
527 
534  {
535  $GLOBALS['SOBE'] = $this->createMock(\TYPO3\CMS\Backend\Controller\EditDocumentController::class);
536  $GLOBALS['SOBE']->elementsData = [
537  [
538  'table' => 'pages',
540  'uid' => 999,
541  'pid' => 121,
542  'cmd' => 'edit',
543  'deleteAccess' => 1
544  ]
545  ];
546  $GLOBALS['SOBE']->data = [
547  'pages' => [
548  'NEW4adc6021e37e7' => [
549  'pid' => 121
550  ]
551  ]
552  ];
553  $this->matchCondition->setRootline($this->rootline);
554  $this->matchCondition->setPageId(121);
555  $this->assertTrue($this->matchCondition->match('[treeLevel = 3]'));
556  }
557 
564  {
565  $this->matchCondition->setRootline($this->rootline);
566  $this->matchCondition->setPageId(121);
567  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 111]'));
568  }
569 
576  {
577  $this->matchCondition->setRootline($this->rootline);
578  $this->matchCondition->setPageId(121);
579  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 999,111,101]'));
580  }
581 
588  {
589  $this->matchCondition->setRootline($this->rootline);
590  $this->matchCondition->setPageId(121);
591  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 999]'));
592  }
593 
600  {
601  $this->matchCondition->setRootline($this->rootline);
602  $this->matchCondition->setPageId(121);
603  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 121]'));
604  }
605 
612  {
613  $GLOBALS['SOBE'] = $this->createMock(\TYPO3\CMS\Backend\Controller\EditDocumentController::class);
614  $GLOBALS['SOBE']->elementsData = [
615  [
616  'table' => 'pages',
617  'uid' => 'NEW4adc6021e37e7',
618  'pid' => 121,
619  'cmd' => 'new',
620  'deleteAccess' => 0
621  ]
622  ];
623  $GLOBALS['SOBE']->data = [];
624  $this->matchCondition->setRootline($this->rootline);
625  $this->matchCondition->setPageId(121);
626  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 121]'));
627  }
628 
635  {
636  $GLOBALS['SOBE'] = $this->createMock(\TYPO3\CMS\Backend\Controller\EditDocumentController::class);
637  $GLOBALS['SOBE']->elementsData = [
638  [
639  'table' => 'pages',
641  'uid' => 999,
642  'pid' => 121,
643  'cmd' => 'edit',
644  'deleteAccess' => 1
645  ]
646  ];
647  $GLOBALS['SOBE']->data = [
648  'pages' => [
649  'NEW4adc6021e37e7' => [
650  'pid' => 121
651  ]
652  ]
653  ];
654  $this->matchCondition->setRootline($this->rootline);
655  $this->matchCondition->setPageId(121);
656  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 121]'));
657  }
658 
665  {
666  $this->matchCondition->setRootline($this->rootline);
667  $this->matchCondition->setPageId(121);
668  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 111]'));
669  }
670 
677  {
678  $this->matchCondition->setRootline($this->rootline);
679  $this->matchCondition->setPageId(121);
680  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 999,111,101]'));
681  }
682 
689  {
690  $this->matchCondition->setRootline($this->rootline);
691  $this->matchCondition->setPageId(121);
692  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 121]'));
693  }
694 
701  {
702  $this->matchCondition->setRootline($this->rootline);
703  $this->matchCondition->setPageId(121);
704  $this->assertFalse($this->matchCondition->match('[PIDinRootline = 999]'));
705  }
706 
714  {
715  $this->assertTrue($this->matchCondition->match('[compatVersion = 7.0]'));
716  }
717 
725  {
726  $this->assertTrue($this->matchCondition->match('[compatVersion = ' . TYPO3_branch . ']'));
727  }
728 
736  {
737  $this->assertFalse($this->matchCondition->match('[compatVersion = 15.0]'));
738  }
739 
746  {
747  $_GET = ['testGet' => 'getTest'];
748  $_POST = ['testPost' => 'postTest'];
749  $this->assertTrue($this->matchCondition->match('[globalString = GP:testGet = getTest]'));
750  $this->assertTrue($this->matchCondition->match('[globalString = GP:testPost = postTest]'));
751  }
752 
760  {
761  $GLOBALS['TSFE'] = new \stdClass();
762  $GLOBALS['TSFE']->id = 1234567;
763  $this->assertFalse($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
764  }
765 
772  {
773  $testKey = $this->getUniqueId('test');
774  putenv($testKey . '=testValue');
775  $this->assertTrue($this->matchCondition->match('[globalString = ENV:' . $testKey . ' = testValue]'));
776  }
777 
784  {
785  $_SERVER['HTTP_HOST'] = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY') . ':1234567';
786  $this->assertTrue($this->matchCondition->match('[globalString = IENV:TYPO3_PORT = 1234567]'));
787  }
788 
795  {
797  'first' => 'testFirst',
798  'second' => ['third' => 'testThird']
799  ];
800  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|first = testFirst]'));
801  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|second|third = testThird]'));
802  }
803 
810  {
811  $_GET['id'] = 999;
812  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
813  $this->assertEquals(999, $this->matchCondition->getPageId());
814  }
815 
822  {
823  $_GET['edit']['pages'][999] = 'edit';
824  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
825  $this->assertEquals(999, $this->matchCondition->getPageId());
826  }
827 
834  {
836  $_GET['edit'][$this->testTableName][13] = 'edit';
837  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
838  $this->assertEquals(999, $this->matchCondition->getPageId());
839  }
840 
847  {
848  $_GET['edit']['pages'][999] = 'new';
849  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
850  $this->assertEquals(999, $this->matchCondition->getPageId());
851  }
852 
859  {
861  $_GET['edit'][$this->testTableName][-13] = 'new';
862  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
863  $this->assertEquals(999, $this->matchCondition->getPageId());
864  }
865 
872  {
873  $_GET['cmd']['pages'][999]['delete'] = 1;
874  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
875  $this->assertEquals(999, $this->matchCondition->getPageId());
876  }
877 
884  {
885  $_GET['cmd']['pages'][121]['copy'] = 999;
886  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
887  $this->assertEquals(999, $this->matchCondition->getPageId());
888  }
889 
896  {
898  $_GET['cmd'][$this->testTableName][121]['copy'] = -13;
899  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
900  $this->assertEquals(999, $this->matchCondition->getPageId());
901  }
902 
909  {
910  $_GET['cmd']['pages'][121]['move'] = 999;
911  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
912  $this->assertEquals(999, $this->matchCondition->getPageId());
913  }
914 
919  {
920  $this->expectException(TestConditionException::class);
921  $this->expectExceptionCode(1476109533);
922  $this->matchCondition->match('[TYPO3\\CMS\\Backend\\Tests\\Unit\\Configuration\\TypoScript\\ConditionMatching\\Fixtures\\TestCondition = 7, != 6]');
923  }
924 }
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']