fd16c6501ec1d665d65c9692692cfcceacac7908
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_matchcondition_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Oliver Hader <oliver@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * Testcase for class t3lib_matchCondition.
28 *
29 * @author Oliver Hader <oliver@typo3.org>
30 * @package TYPO3
31 * @subpackage t3lib
32 */
33 class t3lib_matchCondition_testcase extends tx_phpunit_testcase {
34 /**
35 * @var array
36 */
37 private $backupGlobalVariables;
38
39 /**
40 * @var array
41 */
42 private $rootline;
43
44 /**
45 * @var t3lib_matchCondition
46 */
47 private $matchCondition;
48
49 public function setUp() {
50 $this->backupGlobalVariables = array(
51 '_ENV' => $_ENV,
52 '_GET' => $_GET,
53 '_POST' => $_POST,
54 '_SERVER' => $_SERVER,
55 'TYPO3_CONF_VARS' => $GLOBALS['TYPO3_CONF_VARS'],
56 );
57
58 $this->testGlobalNamespace = uniqid('TEST');
59 $GLOBALS[$this->testGlobalNamespace] = array();
60
61 $this->setUpTSFE();
62 $this->matchCondition = t3lib_div::makeInstance('t3lib_matchCondition');
63 }
64
65 public function tearDown() {
66 foreach ($this->backupGlobalVariables as $key => $data) {
67 $GLOBALS[$key] = $data;
68 }
69
70 unset($this->matchCondition);
71 unset($this->backupGlobalVariables);
72 unset($GLOBALS[$this->testGlobalNamespace]);
73 }
74
75 private function setUpTSFE() {
76 $this->rootline = array(
77 2 => array('uid' => 121, 'pid' => 111),
78 1 => array('uid' => 111, 'pid' => 101,),
79 0 => array('uid' => 101, 'pid' => 0,),
80 );
81
82 $GLOBALS['TSFE'] = $this->getMock('tslib_fe', array(), array(), '', FALSE);
83 $GLOBALS['TSFE']->tmpl = $this->getMock('t3lib_TStemplate');
84 }
85
86 /**
87 * Tests whether a faulty expression fails.
88 * @test
89 */
90 public function simulateDisabledMatchAllConditionsFailsOnFaultyExpression() {
91 $this->matchCondition->matchAll = false;
92 $this->assertFalse($this->matchCondition->match('[nullCondition = This expression would return false in general]'));
93 }
94
95 /**
96 * Tests whether simulating positive matches for all conditions succeeds.
97 * @test
98 */
99 public function simulateEnabledMatchAllConditionsSucceeds() {
100 $this->matchCondition->matchAll = true;
101 $this->assertTrue($this->matchCondition->match('[nullCondition = This expression would return false in general]'));
102 }
103
104 /**
105 * Tests whether simulating positive matches for specific conditions succeeds.
106 * @test
107 */
108 public function simulateEnabledMatchSpecificConditionsSucceeds() {
109 $testCondition = '[' . uniqid('test') . ' = Any condition to simulate a positive match]';
110 $this->matchCondition->matchAlternative = array($testCondition);
111 $this->assertTrue($this->matchCondition->match($testCondition));
112 }
113
114 /**
115 * Tests whether a condition matches Internet Explorer 7 on Windows.
116 *
117 * @return void
118 * @test
119 */
120 public function conditionMatchesInternetExplorer7Windows() {
121 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)';
122 $result = $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
123 $this->assertTrue($result);
124 }
125
126 /**
127 * Tests whether a condition does not match Internet Explorer 7 on Windows.
128 *
129 * @return void
130 * @test
131 */
132 public function conditionDoesNotMatchInternetExplorer7Windows() {
133 $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.25 (Windows NT 6.0; U; en)';
134 $result = $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
135 $this->assertFalse($result);
136 }
137
138 /**
139 * Tests whether the browserInfo hook is called.
140 *
141 * @return void
142 * @test
143 */
144 public function browserInfoHookIsCalled() {
145 $browserInfoHookMock = $this->getMock(uniqid('tx_browserInfoHook'), array('browserInfo'));
146 $browserInfoHookMock->expects($this->atLeastOnce())->method('browserInfo');
147 $this->matchCondition->hookObjectsArr = array($browserInfoHookMock);
148
149 $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
150 }
151
152 /**
153 * Tests whether the language comparison matches.
154 * @test
155 */
156 public function languageConditionMatchesSingleLanguageExpression() {
157 $_SERVER[] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
158 $this->assertTrue($this->matchCondition->match('[language = *de*]'));
159 $this->assertTrue($this->matchCondition->match('[language = *de-de*]'));
160 }
161
162 /**
163 * Tests whether the language comparison matches.
164 * @test
165 */
166 public function languageConditionMatchesMultipleLanguagesExpression() {
167 $_SERVER[] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
168 $this->assertTrue($this->matchCondition->match('[language = *en*,*de*]'));
169 $this->assertTrue($this->matchCondition->match('[language = *en-us*,*de-de*]'));
170 }
171
172 /**
173 * Tests whether the language comparison matches.
174 * @test
175 */
176 public function languageConditionMatchesCompleteLanguagesExpression() {
177 $this->markTestSkipped('This comparison seems to be incomplete in t3lib_matchCondition.');
178
179 $_SERVER[] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
180 $this->assertTrue($this->matchCondition->match('[language = de-de,de;q=0.8]'));
181 }
182
183 /**
184 * Tests whether usergroup comparison matches.
185 * @test
186 */
187 public function usergroupConditionMatchesSingleGroupId() {
188 $GLOBALS['TSFE']->gr_list = '13,14,15';
189 $this->assertTrue($this->matchCondition->match('[usergroup = 13]'));
190 }
191
192 /**
193 * Tests whether usergroup comparison matches.
194 * @test
195 */
196 public function usergroupConditionMatchesMultipleUserGroupId() {
197 $GLOBALS['TSFE']->gr_list = '13,14,15';
198 $this->assertTrue($this->matchCondition->match('[usergroup = 999,15,14,13]'));
199 }
200
201 /**
202 * Tests whether usergroup comparison matches.
203 * @test
204 */
205 public function usergroupConditionDoesNotMatchDefaulUserGroupIds() {
206 $GLOBALS['TSFE']->gr_list = '0,-1';
207 $this->assertFalse($this->matchCondition->match('[usergroup = 0,-1]'));
208 }
209
210 /**
211 * Tests whether user comparison matches.
212 * @test
213 */
214 public function loginUserConditionMatchesAnyLoggedInUser() {
215 $GLOBALS['TSFE']->loginUser = TRUE;
216 $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
217 $this->assertTrue($this->matchCondition->match('[loginUser = *]'));
218 }
219
220 /**
221 * Tests whether user comparison matches.
222 * @test
223 */
224 public function loginUserConditionMatchesSingleLoggedInUser() {
225 $GLOBALS['TSFE']->loginUser = TRUE;
226 $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
227 $this->assertTrue($this->matchCondition->match('[loginUser = 13]'));
228 }
229
230 /**
231 * Tests whether user comparison matches.
232 * @test
233 */
234 public function loginUserConditionMatchesMultipleLoggedInUsers() {
235 $GLOBALS['TSFE']->loginUser = TRUE;
236 $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
237 $this->assertTrue($this->matchCondition->match('[loginUser = 999,13]'));
238 }
239
240 /**
241 * Tests whether user comparison matches.
242 * @test
243 */
244 public function loginUserConditionDoesNotMatchIfNotUserIsLoggedId() {
245 $GLOBALS['TSFE']->loginUser = FALSE;
246 $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
247 $this->assertFalse($this->matchCondition->match('[loginUser = *]'));
248 $this->assertFalse($this->matchCondition->match('[loginUser = 13]'));
249 }
250
251 /**
252 * Tests whether numerical comparison matches.
253 * @test
254 */
255 public function globalVarConditionMatchesOnEqualExpression() {
256 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10]'));
257 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1]'));
258
259 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10]'));
260 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1]'));
261 }
262
263 /**
264 * Tests whether numerical comparison matches.
265 * @test
266 */
267 public function globalVarConditionMatchesOnNotEqualExpression() {
268 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
269 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
270 }
271
272 /**
273 * Tests whether numerical comparison matches.
274 * @test
275 */
276 public function globalVarConditionMatchesOnLowerThanExpression() {
277 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
278 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));
279 }
280
281 /**
282 * Tests whether numerical comparison matches.
283 * @test
284 */
285 public function globalVarConditionMatchesOnLowerThanOrEqualExpression() {
286 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 10]'));
287 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 20]'));
288
289 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.1]'));
290 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.2]'));
291 }
292
293 /**
294 * Tests whether numerical comparison matches.
295 * @test
296 */
297 public function globalVarConditionMatchesOnGreaterThanExpression() {
298 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 > 10]'));
299 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 > 10.1]'));
300 }
301
302 /**
303 * Tests whether numerical comparison matches.
304 * @test
305 */
306 public function globalVarConditionMatchesOnGreaterThanOrEqualExpression() {
307 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 >= 10]'));
308 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 >= 10]'));
309
310 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 >= 10.1]'));
311 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 >= 10.1]'));
312 }
313
314 /**
315 * Tests whether string comparison matches.
316 * @test
317 */
318 public function globalStringConditionMatchesOnEqualExpression() {
319 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.Test.Condition]'));
320 $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3]'));
321 }
322
323 /**
324 * Tests whether string comparison matches.
325 * @test
326 */
327 public function globalStringConditionMatchesWildcardExpression() {
328 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?Test?Condition]'));
329 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.T*t.Condition]'));
330 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?T*t?Condition]'));
331 }
332
333 /**
334 * Tests whether string comparison matches.
335 * @test
336 */
337 public function globalStringConditionMatchesRegularExpression() {
338 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^[A-Za-z3.]+$/]'));
339 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^TYPO3\..+Condition$/]'));
340 $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^FALSE/]'));
341 }
342
343 /**
344 * Tests whether string comparison matches.
345 * @test
346 */
347 public function globalStringConditionMatchesEmptyRegularExpression() {
348 $testKey = uniqid('test');
349 $_SERVER[$testKey] = '';
350 $this->assertTrue($this->matchCondition->match('[globalString = _SERVER|' . $testKey . ' = /^$/]'));
351 }
352
353 /**
354 * Tests whether treeLevel comparison matches.
355 * @test
356 */
357 public function treeLevelConditionMatchesSingleValue() {
358 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
359 $this->assertTrue($this->matchCondition->match('[treeLevel = 2]'));
360 }
361
362 /**
363 * Tests whether treeLevel comparison matches.
364 * @test
365 */
366 public function treeLevelConditionMatchesMultipleValues() {
367 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
368 $this->assertTrue($this->matchCondition->match('[treeLevel = 999,998,2]'));
369 }
370
371 /**
372 * Tests whether treeLevel comparison matches.
373 * @test
374 */
375 public function treeLevelConditionDoesNotMatchFaultyValue() {
376 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
377 $this->assertFalse($this->matchCondition->match('[treeLevel = 999]'));
378 }
379
380 /**
381 * Tests whether a page Id is found in the previous rootline entries.
382 * @test
383 */
384 public function PIDupinRootlineConditionMatchesSinglePageIdInRootline() {
385 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
386 $GLOBALS['TSFE']->id = 121;
387 $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 111]'));
388 }
389
390 /**
391 * Tests whether a page Id is found in the previous rootline entries.
392 * @test
393 */
394 public function PIDupinRootlineConditionMatchesMultiplePageIdsInRootline() {
395 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
396 $GLOBALS['TSFE']->id = 121;
397 $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 999,111,101]'));
398 }
399
400 /**
401 * Tests whether a page Id is found in the previous rootline entries.
402 * @test
403 */
404 public function PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline() {
405 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
406 $GLOBALS['TSFE']->id = 121;
407 $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 999]'));
408 }
409
410 /**
411 * Tests whether a page Id is found in the previous rootline entries.
412 * @test
413 */
414 public function PIDupinRootlineConditionDoesNotMatchLastPageIdInRootline() {
415 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
416 $GLOBALS['TSFE']->id = 121;
417 $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 121]'));
418 }
419
420 /**
421 * Tests whether a page Id is found in all rootline entries.
422 * @test
423 */
424 public function PIDinRootlineConditionMatchesSinglePageIdInRootline() {
425 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
426 $GLOBALS['TSFE']->id = 121;
427 $this->assertTrue($this->matchCondition->match('[PIDinRootline = 111]'));
428 }
429
430 /**
431 * Tests whether a page Id is found in all rootline entries.
432 * @test
433 */
434 public function PIDinRootlineConditionMatchesMultiplePageIdsInRootline() {
435 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
436 $GLOBALS['TSFE']->id = 121;
437 $this->assertTrue($this->matchCondition->match('[PIDinRootline = 999,111,101]'));
438 }
439
440 /**
441 * Tests whether a page Id is found in all rootline entries.
442 * @test
443 */
444 public function PIDinRootlineConditionMatchesLastPageIdInRootline() {
445 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
446 $GLOBALS['TSFE']->id = 121;
447 $this->assertTrue($this->matchCondition->match('[PIDinRootline = 121]'));
448 }
449
450 /**
451 * Tests whether a page Id is found in all rootline entries.
452 * @test
453 */
454 public function PIDinRootlineConditionDoesNotMatchPageIdNotInRootline() {
455 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
456 $GLOBALS['TSFE']->id = 121;
457 $this->assertFalse($this->matchCondition->match('[PIDinRootline = 999]'));
458 }
459
460 /**
461 * Tests whether the compatibility version can be evaluated.
462 * (e.g. 4.9 is compatible to 4.0 but not to 5.0)
463 * @test
464 */
465 public function compatVersionConditionMatchesOlderRelease() {
466 $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
467 $this->assertTrue($this->matchCondition->match('[compatVersion = 4.0]'));
468 }
469
470 /**
471 * Tests whether the compatibility version can be evaluated.
472 * (e.g. 4.9 is compatible to 4.0 but not to 5.0)
473 * @test
474 */
475 public function compatVersionConditionMatchesSameRelease() {
476 $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
477 $this->assertTrue($this->matchCondition->match('[compatVersion = 4.9]'));
478 }
479
480 /**
481 * Tests whether the compatibility version can be evaluated.
482 * (e.g. 4.9 is compatible to 4.0 but not to 5.0)
483 * @test
484 */
485 public function compatVersionConditionDoesNotMatchNewerRelease() {
486 $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
487 $this->assertFalse($this->matchCondition->match('[compatVersion = 5.0]'));
488 }
489
490 /**
491 * Tests whether the gneric fetching of variables works with the namespace 'GP'.
492 * @test
493 */
494 public function genericGetVariablesSucceedsWithNamespaceGP() {
495 $_GET = array('testGet' => 'getTest');
496 $_POST = array('testPost' => 'postTest');
497
498 $this->assertTrue($this->matchCondition->match('[globalString = GP:testGet = getTest]'));
499 $this->assertTrue($this->matchCondition->match('[globalString = GP:testPost = postTest]'));
500 }
501
502 /**
503 * Tests whether the gneric fetching of variables works with the namespace 'TSFE'.
504 * @test
505 */
506 public function genericGetVariablesSucceedsWithNamespaceTSFE() {
507 $GLOBALS['TSFE']->id = 1234567;
508 $GLOBALS['TSFE']->testSimpleObject = new stdClass();
509 $GLOBALS['TSFE']->testSimpleObject->testSimpleVariable = 'testValue';
510
511 $this->assertTrue($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
512 $this->assertTrue($this->matchCondition->match('[globalString = TSFE:testSimpleObject|testSimpleVariable = testValue]'));
513 }
514
515 /**
516 * Tests whether the gneric fetching of variables works with the namespace 'ENV'.
517 * @test
518 */
519 public function genericGetVariablesSucceedsWithNamespaceENV() {
520 $testKey = uniqid('test');
521 putenv($testKey .'=testValue');
522
523 $this->assertTrue($this->matchCondition->match('[globalString = ENV:' . $testKey . ' = testValue]'));
524 }
525
526 /**
527 * Tests whether the gneric fetching of variables works with the namespace 'IENV'.
528 * @test
529 */
530 public function genericGetVariablesSucceedsWithNamespaceIENV() {
531 $_SERVER['HTTP_HOST'] = t3lib_div::getIndpEnv('TYPO3_HOST_ONLY') . ':1234567';
532 $this->assertTrue($this->matchCondition->match('[globalString = IENV:TYPO3_PORT = 1234567]'));
533 }
534
535 /**
536 * Tests whether the gneric fetching of variables works with any global namespace.
537 * @test
538 */
539 public function genericGetVariablesSucceedsWithAnyGlobalNamespace() {
540 $GLOBALS[$this->testGlobalNamespace] = array(
541 'first' => 'testFirst',
542 'second' => array('third' => 'testThird'),
543 );
544
545 $this->assertTrue($this->matchCondition->match(
546 '[globalString = ' . $this->testGlobalNamespace . '|first = testFirst]'
547 ));
548 $this->assertTrue($this->matchCondition->match(
549 '[globalString = ' . $this->testGlobalNamespace . '|second|third = testThird]'
550 ));
551 }
552 }
553 ?>