TYPO3 CMS  TYPO3_6-2
ConditionMatcherTest.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $rootline;
28 
32  protected $matchCondition;
33 
37  protected $testTableName;
38 
39  public function setUp() {
40  $this->testTableName = 'conditionMatcherTestTable';
41  $this->testGlobalNamespace = $this->getUniqueId('TEST');
42  $GLOBALS['TCA'][$this->testTableName] = array('ctrl' => array());
43  $GLOBALS[$this->testGlobalNamespace] = array();
44  $this->setUpBackend();
45  $this->matchCondition = $this->getMock('TYPO3\\CMS\\Backend\\Configuration\\TypoScript\\ConditionMatching\\ConditionMatcher', array('determineRootline'), array(), '', FALSE);
46  }
47 
48  private function setUpBackend() {
49  $this->rootline = array(
50  2 => array('uid' => 121, 'pid' => 111),
51  1 => array('uid' => 111, 'pid' => 101),
52  0 => array('uid' => 101, 'pid' => 0)
53  );
54  $GLOBALS['BE_USER'] = $this->getMock('beUserAuth', array(), array(), '', FALSE);
55  }
56 
58  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('exec_SELECTquery', 'sql_fetch_assoc', 'sql_free_result'));
59  $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTquery')->will($this->returnCallback(array($this, 'determinePageIdByRecordDatabaseExecuteCallback')));
60  $GLOBALS['TYPO3_DB']->expects($this->any())->method('sql_fetch_assoc')->will($this->returnCallback(array($this, 'determinePageIdByRecordDatabaseFetchCallback')));
61  }
62 
69  $this->matchCondition->matchAll = FALSE;
70  $this->assertFalse($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
71  }
72 
79  $this->matchCondition->setSimulateMatchResult(TRUE);
80  $this->assertTrue($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
81  }
82 
89  $testCondition = '[' . $this->getUniqueId('test') . ' = Any condition to simulate a positive match]';
90  $this->matchCondition->setSimulateMatchConditions(array($testCondition));
91  $this->assertTrue($this->matchCondition->match($testCondition));
92  }
93 
101  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)';
102  $result = $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
103  $this->assertTrue($result);
104  }
105 
113  $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.25 (Windows NT 6.0; U; en)';
114  $result = $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
115  $this->assertFalse($result);
116  }
117 
124  $_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';
125  $result = $this->matchCondition->match('[system = iOS]');
126  $this->assertTrue($result);
127  }
128 
135  $_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';
136  $result = $this->matchCondition->match('[system = mac]');
137  $this->assertTrue($result);
138  }
139 
146  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; SV1)';
147  $result = $this->matchCondition->match('[system = win2k]');
148  $this->assertTrue($result);
149  }
150 
157  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; SV1)';
158  $result = $this->matchCondition->match('[system = winNT]');
159  $this->assertTrue($result);
160  }
161 
168  $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)';
169  $result = $this->matchCondition->match('[system = winNT]');
170  $this->assertTrue($result);
171  }
172 
179  $_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';
180  $result = $this->matchCondition->match('[system = android]');
181  $this->assertTrue($result);
182  }
183 
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  $_SERVER['HTTP_USER_AGENT'] = 'Googlebot/2.1 (+http://www.google.com/bot.html)';
202  $result = $this->matchCondition->match('[device = robot]');
203  $this->assertTrue($result);
204  }
205 
212  $_SERVER['HTTP_USER_AGENT'] = md5('Some strange user agent');
213  $result = $this->matchCondition->match('[device = robot]');
214  $this->assertFalse($result);
215  }
216 
223  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
224  $this->assertTrue($this->matchCondition->match('[language = *de*]'));
225  $this->assertTrue($this->matchCondition->match('[language = *de-de*]'));
226  }
227 
234  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
235  $this->assertTrue($this->matchCondition->match('[language = *en*,*de*]'));
236  $this->assertTrue($this->matchCondition->match('[language = *en-us*,*de-de*]'));
237  }
238 
245  $this->markTestSkipped('This comparison seems to be incomplete in \TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher.');
246  $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
247  $this->assertTrue($this->matchCondition->match('[language = de-de,de;q=0.8]'));
248  }
249 
256  $GLOBALS['BE_USER']->groupList = '13,14,15';
257  $this->assertTrue($this->matchCondition->match('[usergroup = 13]'));
258  }
259 
266  $GLOBALS['BE_USER']->groupList = '13,14,15';
267  $this->assertTrue($this->matchCondition->match('[usergroup = 999,15,14,13]'));
268  }
269 
276  $GLOBALS['BE_USER']->user['uid'] = 13;
277  $this->assertTrue($this->matchCondition->match('[loginUser = *]'));
278  }
279 
286  $GLOBALS['BE_USER']->user['uid'] = 13;
287  $this->assertTrue($this->matchCondition->match('[loginUser = 13]'));
288  }
289 
296  $GLOBALS['BE_USER']->user['uid'] = 13;
297  $this->assertFalse($this->matchCondition->match('[loginUser = 999]'));
298  }
299 
306  $GLOBALS['BE_USER']->user['uid'] = 13;
307  $this->assertTrue($this->matchCondition->match('[loginUser = 999,13]'));
308  }
309 
316  $GLOBALS['BE_USER']->user['uid'] = 13;
317  $GLOBALS['BE_USER']->user['admin'] = 1;
318  $this->assertTrue($this->matchCondition->match('[adminUser = 1]'));
319  }
320 
327  $GLOBALS['BE_USER']->user['uid'] = 14;
328  $GLOBALS['BE_USER']->user['admin'] = 0;
329  $this->assertTrue($this->matchCondition->match('[adminUser = 0]'));
330  }
331 
338  $GLOBALS['BE_USER']->user['uid'] = 14;
339  $GLOBALS['BE_USER']->user['admin'] = 0;
340  $this->assertFalse($this->matchCondition->match('[adminUser = 1]'));
341  }
342 
349  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10]'), '1');
350  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1]'), '2');
351  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10]'), '3');
352  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1]'), '4');
353  }
354 
361  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10|20|30]'));
362  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1|20.2|30.3]'));
363  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 = 10|20|30]'));
364  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 = 10.1|20.2|30.3]'));
365  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10|20|30]'));
366  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1|20.2|30.3]'));
367  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 == 10|20|30]'));
368  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 == 10.1|20.2|30.3]'));
369  }
370 
377  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
378  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
379  }
380 
387  $this->assertFalse($this->matchCondition->match('[globalVar = LIT:10 != 10]'));
388  }
389 
396  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20|30]'));
397  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2|20.3]'));
398  }
399 
406  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
407  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));
408  }
409 
416  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 10]'));
417  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 20]'));
418  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.1]'));
419  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.2]'));
420  }
421 
428  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 > 10]'));
429  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 > 10.1]'));
430  }
431 
438  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 >= 10]'));
439  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 >= 10]'));
440  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 >= 10.1]'));
441  $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 >= 10.1]'));
442  }
443 
450  $testKey = $this->getUniqueId('test');
451  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
452  $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
453  }
454 
461  $testKey = $this->getUniqueId('test');
462  $_GET = array();
463  $_POST = array($testKey => 0);
464  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
465  $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
466  }
467 
474  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.Test.Condition]'));
475  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3]'));
476  }
477 
484  $testKey = $this->getUniqueId('test');
485  $_GET = array();
486  $_POST = array($testKey => '');
487  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . '=]'));
488  $this->assertTrue($this->matchCondition->match('[globalString = GP:' . $testKey . ' = ]'));
489  }
490 
497  $this->assertTrue($this->matchCondition->match('[globalString = LIT:=]'));
498  $this->assertTrue($this->matchCondition->match('[globalString = LIT: = ]'));
499  }
500 
507  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?Test?Condition]'));
508  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.T*t.Condition]'));
509  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?T*t?Condition]'));
510  }
511 
518  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^[A-Za-z3.]+$/]'));
519  $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^TYPO3\\..+Condition$/]'));
520  $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^FALSE/]'));
521  }
522 
529  $testKey = $this->getUniqueId('test');
530  $_SERVER[$testKey] = '';
531  $this->assertTrue($this->matchCondition->match('[globalString = _SERVER|' . $testKey . ' = /^$/]'));
532  }
533 
540  $this->matchCondition->setRootline($this->rootline);
541  $this->assertTrue($this->matchCondition->match('[treeLevel = 2]'));
542  }
543 
550  $this->matchCondition->setRootline($this->rootline);
551  $this->assertTrue($this->matchCondition->match('[treeLevel = 999,998,2]'));
552  }
553 
560  $this->matchCondition->setRootline($this->rootline);
561  $this->assertFalse($this->matchCondition->match('[treeLevel = 999]'));
562  }
563 
570  $GLOBALS['SOBE'] = $this->getMock('TYPO3\\CMS\\Backend\\Controller\\EditDocumentController', array(), array(), '', FALSE);
571  $GLOBALS['SOBE']->elementsData = array(
572  array(
573  'table' => 'pages',
574  'uid' => 'NEW4adc6021e37e7',
575  'pid' => 121,
576  'cmd' => 'new',
577  'deleteAccess' => 0
578  )
579  );
580  $GLOBALS['SOBE']->data = array();
581  $this->matchCondition->setRootline($this->rootline);
582  $this->matchCondition->setPageId(121);
583  $this->assertTrue($this->matchCondition->match('[treeLevel = 3]'));
584  }
585 
592  $GLOBALS['SOBE'] = $this->getMock('TYPO3\\CMS\\Backend\\Controller\\EditDocumentController', array(), array(), '', FALSE);
593  $GLOBALS['SOBE']->elementsData = array(
594  array(
595  'table' => 'pages',
597  'uid' => 999,
598  'pid' => 121,
599  'cmd' => 'edit',
600  'deleteAccess' => 1
601  )
602  );
603  $GLOBALS['SOBE']->data = array(
604  'pages' => array(
605  'NEW4adc6021e37e7' => array(
606  'pid' => 121
607  )
608  )
609  );
610  $this->matchCondition->setRootline($this->rootline);
611  $this->matchCondition->setPageId(121);
612  $this->assertTrue($this->matchCondition->match('[treeLevel = 3]'));
613  }
614 
621  $this->matchCondition->setRootline($this->rootline);
622  $this->matchCondition->setPageId(121);
623  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 111]'));
624  }
625 
632  $this->matchCondition->setRootline($this->rootline);
633  $this->matchCondition->setPageId(121);
634  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 999,111,101]'));
635  }
636 
643  $this->matchCondition->setRootline($this->rootline);
644  $this->matchCondition->setPageId(121);
645  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 999]'));
646  }
647 
654  $this->matchCondition->setRootline($this->rootline);
655  $this->matchCondition->setPageId(121);
656  $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 121]'));
657  }
658 
665  $GLOBALS['SOBE'] = $this->getMock('TYPO3\\CMS\\Backend\\Controller\\EditDocumentController', array(), array(), '', FALSE);
666  $GLOBALS['SOBE']->elementsData = array(
667  array(
668  'table' => 'pages',
669  'uid' => 'NEW4adc6021e37e7',
670  'pid' => 121,
671  'cmd' => 'new',
672  'deleteAccess' => 0
673  )
674  );
675  $GLOBALS['SOBE']->data = array();
676  $this->matchCondition->setRootline($this->rootline);
677  $this->matchCondition->setPageId(121);
678  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 121]'));
679  }
680 
687  $GLOBALS['SOBE'] = $this->getMock('TYPO3\\CMS\\Backend\\Controller\\EditDocumentController', array(), array(), '', FALSE);
688  $GLOBALS['SOBE']->elementsData = array(
689  array(
690  'table' => 'pages',
692  'uid' => 999,
693  'pid' => 121,
694  'cmd' => 'edit',
695  'deleteAccess' => 1
696  )
697  );
698  $GLOBALS['SOBE']->data = array(
699  'pages' => array(
700  'NEW4adc6021e37e7' => array(
701  'pid' => 121
702  )
703  )
704  );
705  $this->matchCondition->setRootline($this->rootline);
706  $this->matchCondition->setPageId(121);
707  $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 121]'));
708  }
709 
716  $this->matchCondition->setRootline($this->rootline);
717  $this->matchCondition->setPageId(121);
718  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 111]'));
719  }
720 
727  $this->matchCondition->setRootline($this->rootline);
728  $this->matchCondition->setPageId(121);
729  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 999,111,101]'));
730  }
731 
738  $this->matchCondition->setRootline($this->rootline);
739  $this->matchCondition->setPageId(121);
740  $this->assertTrue($this->matchCondition->match('[PIDinRootline = 121]'));
741  }
742 
749  $this->matchCondition->setRootline($this->rootline);
750  $this->matchCondition->setPageId(121);
751  $this->assertFalse($this->matchCondition->match('[PIDinRootline = 999]'));
752  }
753 
761  $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
762  $this->assertTrue($this->matchCondition->match('[compatVersion = 4.0]'));
763  }
764 
772  $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
773  $this->assertTrue($this->matchCondition->match('[compatVersion = 4.9]'));
774  }
775 
783  $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
784  $this->assertFalse($this->matchCondition->match('[compatVersion = 5.0]'));
785  }
786 
793  $_GET = array('testGet' => 'getTest');
794  $_POST = array('testPost' => 'postTest');
795  $this->assertTrue($this->matchCondition->match('[globalString = GP:testGet = getTest]'));
796  $this->assertTrue($this->matchCondition->match('[globalString = GP:testPost = postTest]'));
797  }
798 
806  $GLOBALS['TSFE'] = new \stdClass();
807  $GLOBALS['TSFE']->id = 1234567;
808  $this->assertFalse($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
809  }
810 
817  $testKey = $this->getUniqueId('test');
818  putenv($testKey . '=testValue');
819  $this->assertTrue($this->matchCondition->match('[globalString = ENV:' . $testKey . ' = testValue]'));
820  }
821 
828  $_SERVER['HTTP_HOST'] = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY') . ':1234567';
829  $this->assertTrue($this->matchCondition->match('[globalString = IENV:TYPO3_PORT = 1234567]'));
830  }
831 
838  $GLOBALS[$this->testGlobalNamespace] = array(
839  'first' => 'testFirst',
840  'second' => array('third' => 'testThird')
841  );
842  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|first = testFirst]'));
843  $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|second|third = testThird]'));
844  }
845 
852  $_GET['id'] = 999;
853  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
854  $this->assertEquals(999, $this->matchCondition->getPageId());
855  }
856 
863  $_GET['edit']['pages'][999] = 'edit';
864  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
865  $this->assertEquals(999, $this->matchCondition->getPageId());
866  }
867 
875  $_GET['edit'][$this->testTableName][13] = 'edit';
876  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
877  $this->assertEquals(999, $this->matchCondition->getPageId());
878  }
879 
886  $_GET['edit']['pages'][999] = 'new';
887  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
888  $this->assertEquals(999, $this->matchCondition->getPageId());
889  }
890 
898  $_GET['edit'][$this->testTableName][-13] = 'new';
899  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
900  $this->assertEquals(999, $this->matchCondition->getPageId());
901  }
902 
909  $_GET['cmd']['pages'][999]['delete'] = 1;
910  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
911  $this->assertEquals(999, $this->matchCondition->getPageId());
912  }
913 
920  $_GET['cmd']['pages'][121]['copy'] = 999;
921  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
922  $this->assertEquals(999, $this->matchCondition->getPageId());
923  }
924 
932  $_GET['cmd'][$this->testTableName][121]['copy'] = -13;
933  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
934  $this->assertEquals(999, $this->matchCondition->getPageId());
935  }
936 
943  $_GET['cmd']['pages'][121]['move'] = 999;
944  $this->matchCondition->match('[globalVar = LIT:10 = 10]');
945  $this->assertEquals(999, $this->matchCondition->getPageId());
946  }
947 
957  public function determinePageIdByRecordDatabaseExecuteCallback($fields, $table, $where) {
958  if ($table === $this->testTableName) {
959  return array(
960  'scope' => $this->testTableName,
961  'data' => array(
962  'pid' => 999
963  )
964  );
965  } else {
966  return FALSE;
967  }
968  }
969 
977  public function determinePageIdByRecordDatabaseFetchCallback($resource) {
978  if (is_array($resource) && $resource['scope'] === $this->testTableName) {
979  return $resource['data'];
980  } else {
981  return FALSE;
982  }
983  }
984 
985 }
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]