TYPO3 CMS  TYPO3_7-6
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 
25 {
30 
34  protected $matchCondition;
35 
36  protected function setUp()
37  {
38  $this->testGlobalNamespace = $this->getUniqueId('TEST');
41  $GLOBALS['TSFE'] = new \stdClass();
42  $GLOBALS['TSFE']->tmpl = new \stdClass();
43  $GLOBALS['TSFE']->tmpl->rootLine = [
44  2 => ['uid' => 121, 'pid' => 111],
45  1 => ['uid' => 111, 'pid' => 101],
46  0 => ['uid' => 101, 'pid' => 0]
47  ];
48  $this->matchCondition = GeneralUtility::makeInstance(ConditionMatcher::class);
49  }
50 
57  {
58  $this->matchCondition->matchAll = false;
59  $this->assertFalse($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
60  }
61 
68  {
69  $this->matchCondition->setSimulateMatchResult(true);
70  $this->assertTrue($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
71  }
72 
79  {
80  $testCondition = '[' . $this->getUniqueId('test') . ' = Any condition to simulate a positive match]';
81  $this->matchCondition->setSimulateMatchConditions([$testCondition]);
82  $this->assertTrue($this->matchCondition->match($testCondition));
83  }
84 
92  {
93  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)';
94  $result = $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
95  $this->assertTrue($result);
96  }
97 
105  {
106  $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.25 (Windows NT 6.0; U; en)';
107  $result = $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
108  $this->assertFalse($result);
109  }
110 
117  {
118  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7W367a Safari/531.21.10';
119  $result = $this->matchCondition->match('[system = iOS]');
120  $this->assertTrue($result);
121  }
122 
129  {
130  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7W367a Safari/531.21.10';
131  $result = $this->matchCondition->match('[system = mac]');
132  $this->assertTrue($result);
133  }
134 
141  {
142  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; SV1)';
143  $result = $this->matchCondition->match('[system = win2k]');
144  $this->assertTrue($result);
145  }
146 
153  {
154  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; SV1)';
155  $result = $this->matchCondition->match('[system = winNT]');
156  $this->assertTrue($result);
157  }
158 
165  {
166  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)';
167  $result = $this->matchCondition->match('[system = winNT]');
168  $this->assertTrue($result);
169  }
170 
177  {
178  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Linux; U; Android 2.3; en-US; sdk Build/GRH55) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';
179  $result = $this->matchCondition->match('[system = android]');
180  $this->assertTrue($result);
181  }
182 
189  {
190  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Linux; U; Android 2.3; en-US; sdk Build/GRH55) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';
191  $result = $this->matchCondition->match('[system = linux]');
192  $this->assertTrue($result);
193  }
194 
200  public function deviceConditionMatchesRobot()
201  {
202  $_SERVER['HTTP_USER_AGENT'] = 'Googlebot/2.1 (+http://www.google.com/bot.html)';
203  $result = $this->matchCondition->match('[device = robot]');
204  $this->assertTrue($result);
205  }
206 
213  {
214  $_SERVER['HTTP_USER_AGENT'] = md5('Some strange user agent');
215  $result = $this->matchCondition->match('[device = robot]');
216  $this->assertFalse($result);
217  }
218 
225  {
226  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
227  $this->assertTrue($this->matchCondition->match('[language = *de*]'));
228  $this->assertTrue($this->matchCondition->match('[language = *de-de*]'));
229  }
230 
237  {
238  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
239  $this->assertTrue($this->matchCondition->match('[language = *en*,*de*]'));
240  $this->assertTrue($this->matchCondition->match('[language = *en-us*,*de-de*]'));
241  }
242 
249  {
250  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
251  $this->assertTrue($this->matchCondition->match('[language = de-de,de;q=0.8,en-us;q=0.5,en;q=0.3]'));
252  }
253 
260  {
261  $GLOBALS['TSFE']->gr_list = '13,14,15';
262  $this->assertTrue($this->matchCondition->match('[usergroup = 13]'));
263  }
264 
271  {
272  $GLOBALS['TSFE']->gr_list = '13,14,15';
273  $this->assertTrue($this->matchCondition->match('[usergroup = 999,15,14,13]'));
274  }
275 
282  {
283  $GLOBALS['TSFE']->gr_list = '0,-1';
284  $this->assertFalse($this->matchCondition->match('[usergroup = 0,-1]'));
285  }
286 
293  {
294  $GLOBALS['TSFE']->loginUser = true;
295  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
296  $this->assertTrue($this->matchCondition->match('[loginUser = *]'));
297  }
298 
305  {
306  $GLOBALS['TSFE']->loginUser = true;
307  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
308  $this->assertTrue($this->matchCondition->match('[loginUser = 13]'));
309  }
310 
317  {
318  $GLOBALS['TSFE']->loginUser = true;
319  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
320  $this->assertTrue($this->matchCondition->match('[loginUser = 999,13]'));
321  }
322 
329  {
330  $GLOBALS['TSFE']->loginUser = false;
331  $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
332  $this->assertFalse($this->matchCondition->match('[loginUser = *]'));
333  $this->assertFalse($this->matchCondition->match('[loginUser = 13]'));
334  }
335 
342  {
343  $GLOBALS['TSFE']->loginUser = false;
344  $this->assertTrue($this->matchCondition->match('[loginUser = ]'));
345  }
346 
353  {
354  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10]'));
355  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1]'));
356  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10]'));
357  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1]'));
358  }
359 
366  {
367  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10|20|30]'));
368  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1|20.2|30.3]'));
369  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 = 10|20|30]'));
370  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 = 10.1|20.2|30.3]'));
371  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10|20|30]'));
372  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1|20.2|30.3]'));
373  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 == 10|20|30]'));
374  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 == 10.1|20.2|30.3]'));
375  }
376 
383  {
384  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
385  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
386  }
387 
394  {
395  $this->assertFalse($this->matchCondition->match('[globalVar = LIT:10 != 10]'));
396  }
397 
404  {
405  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20|30]'));
406  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2|20.3]'));
407  }
408 
415  {
416  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
417  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));
418  }
419 
426  {
427  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 10]'));
428  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 20]'));
429  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.1]'));
430  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.2]'));
431  }
432 
439  {
440  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 > 10]'));
441  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 > 10.1]'));
442  }
443 
450  {
451  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 >= 10]'));
452  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 >= 10]'));
453  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 >= 10.1]'));
454  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 >= 10.1]'));
455  }
456 
463  {
464  $testKey = $this->getUniqueId('test');
465  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
466  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
467  }
468 
475  {
476  $testKey = $this->getUniqueId('test');
477  $_GET = [];
478  $_POST = [$testKey => 0];
479  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
480  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
481  }
482 
489  {
490  $testKey = $this->getUniqueId('test');
491  $testValue = '1';
492  $_GET = [];
493  $_POST = [$testKey => ['0' => $testValue]];
494  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . '|0=' . $testValue . ']'));
495  }
496 
503  {
504  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.Test.Condition]'));
505  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3]'));
506  }
507 
514  {
515  $testKey = $this->getUniqueId('test');
516  $_GET = [];
517  $_POST = [$testKey => ''];
518  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . '=]'));
519  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . ' = ]'));
520  }
521 
528  {
529  $this->assertTrue($this->matchCondition->match('[globalString = LIT:=]'));
530  $this->assertTrue($this->matchCondition->match('[globalString = LIT: = ]'));
531  }
532 
539  {
540  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?Test?Condition]'));
541  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.T*t.Condition]'));
542  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?T*t?Condition]'));
543  }
544 
551  {
552  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^[A-Za-z3.]+$/]'));
553  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^TYPO3\\..+Condition$/]'));
554  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^FALSE/]'));
555  }
556 
563  {
564  $testKey = $this->getUniqueId('test');
565  $_SERVER[$testKey] = '';
566  $this->assertTrue($this->matchCondition->match('[globalString = _SERVER|' . $testKey . ' = /^$/]'));
567  }
568 
575  {
576  $this->assertTrue($this->matchCondition->match('[treeLevel = 2]'));
577  }
578 
585  {
586  $this->assertTrue($this->matchCondition->match('[treeLevel = 999,998,2]'));
587  }
588 
595  {
596  $this->assertFalse($this->matchCondition->match('[treeLevel = 999]'));
597  }
598 
605  {
606  $GLOBALS['TSFE']->id = 121;
607  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 111]'));
608  }
609 
616  {
617  $GLOBALS['TSFE']->id = 121;
618  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 999,111,101]'));
619  }
620 
627  {
628  $GLOBALS['TSFE']->id = 121;
629  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 999]'));
630  }
631 
638  {
639  $GLOBALS['TSFE']->id = 121;
640  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 121]'));
641  }
642 
649  {
650  $GLOBALS['TSFE']->id = 121;
651  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 111]'));
652  }
653 
660  {
661  $GLOBALS['TSFE']->id = 121;
662  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 999,111,101]'));
663  }
664 
671  {
672  $GLOBALS['TSFE']->id = 121;
673  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 121]'));
674  }
675 
682  {
683  $GLOBALS['TSFE']->id = 121;
684  $this->assertFalse($this->matchCondition->match('[PIDinRootline = 999]'));
685  }
686 
694  {
695  $this->assertTrue($this->matchCondition->match('[compatVersion = 7.0]'));
696  }
697 
705  {
706  $this->assertTrue($this->matchCondition->match('[compatVersion = ' . TYPO3_branch . ']'));
707  }
708 
716  {
717  $this->assertFalse($this->matchCondition->match('[compatVersion = 15.0]'));
718  }
719 
726  {
727  $_GET = ['testGet' => 'getTest'];
728  $_POST = ['testPost' => 'postTest'];
729  $this->assertTrue($this->matchCondition->match('[globalString = GP:testGet = getTest]'));
730  $this->assertTrue($this->matchCondition->match('[globalString = GP:testPost = postTest]'));
731  }
732 
739  {
740  $GLOBALS['TSFE']->id = 1234567;
741  $GLOBALS['TSFE']->testSimpleObject = new \stdClass();
742  $GLOBALS['TSFE']->testSimpleObject->testSimpleVariable = 'testValue';
743  $this->assertTrue($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
744  $this->assertTrue($this->matchCondition->match('[globalString = TSFE:testSimpleObject|testSimpleVariable = testValue]'));
745  }
746 
753  {
754  $testKey = $this->getUniqueId('test');
755  putenv($testKey . '=testValue');
756  $this->assertTrue($this->matchCondition->match('[globalString = ENV:' . $testKey . ' = testValue]'));
757  }
758 
765  {
766  $_SERVER['HTTP_HOST'] = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY') . ':1234567';
767  $this->assertTrue($this->matchCondition->match('[globalString = IENV:TYPO3_PORT = 1234567]'));
768  }
769 
776  {
778  'first' => 'testFirst',
779  'second' => ['third' => 'testThird']
780  ];
781  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|first = testFirst]'));
782  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|second|third = testThird]'));
783  }
784 
790  {
791  $this->matchCondition->match('[stdClass = foo]');
792  }
793 
799  {
800  $this->matchCondition->match('[TYPO3\\CMS\\Frontend\\Tests\\Unit\\Configuration\\TypoScript\\ConditionMatching\\Fixtures\\TestCondition = 7, != 6]');
801  }
802 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']