Added test #12153: Add more unit tests for t3lib_matchCondition
[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 condition matches Internet Explorer 7 on Windows.
88 *
89 * @return void
90 * @test
91 */
92 public function conditionMatchesInternetExplorer7Windows() {
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
98 /**
99 * Tests whether a condition does not match Internet Explorer 7 on Windows.
100 *
101 * @return void
102 * @test
103 */
104 public function conditionDoesNotMatchInternetExplorer7Windows() {
105 $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.25 (Windows NT 6.0; U; en)';
106 $result = $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
107 $this->assertFalse($result);
108 }
109
110 /**
111 * Tests whether the browserInfo hook is called.
112 *
113 * @return void
114 * @test
115 */
116 public function browserInfoHookIsCalled() {
117 $browserInfoHookMock = $this->getMock(uniqid('tx_browserInfoHook'), array('browserInfo'));
118 $browserInfoHookMock->expects($this->atLeastOnce())->method('browserInfo');
119 $this->matchCondition->hookObjectsArr = array($browserInfoHookMock);
120
121 $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
122 }
123
124 /**
125 * Tests whether the language comparison matches.
126 * @test
127 */
128 public function languageConditionMatchesSingleLanguageExpression() {
129 $_SERVER[] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
130 $this->assertTrue($this->matchCondition->match('[language = *de*]'));
131 $this->assertTrue($this->matchCondition->match('[language = *de-de*]'));
132 }
133
134 /**
135 * Tests whether the language comparison matches.
136 * @test
137 */
138 public function languageConditionMatchesMultipleLanguagesExpression() {
139 $_SERVER[] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
140 $this->assertTrue($this->matchCondition->match('[language = *en*,*de*]'));
141 $this->assertTrue($this->matchCondition->match('[language = *en-us*,*de-de*]'));
142 }
143
144 /**
145 * Tests whether the language comparison matches.
146 * @test
147 */
148 public function languageConditionMatchesCompleteLanguagesExpression() {
149 $this->markTestSkipped('This comparison seems to be incomplete in t3lib_matchCondition.');
150
151 $_SERVER[] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
152 $this->assertTrue($this->matchCondition->match('[language = de-de,de;q=0.8]'));
153 }
154
155 /**
156 * Tests whether usergroup comparison matches.
157 * @test
158 */
159 public function usergroupConditionMatchesSingleGroupId() {
160 $GLOBALS['TSFE']->gr_list = '13,14,15';
161 $this->assertTrue($this->matchCondition->match('[usergroup = 13]'));
162 }
163
164 /**
165 * Tests whether usergroup comparison matches.
166 * @test
167 */
168 public function usergroupConditionMatchesMultipleUserGroupId() {
169 $GLOBALS['TSFE']->gr_list = '13,14,15';
170 $this->assertTrue($this->matchCondition->match('[usergroup = 999,15,14,13]'));
171 }
172
173 /**
174 * Tests whether usergroup comparison matches.
175 * @test
176 */
177 public function usergroupConditionDoesNotMatchDefaulUserGroupIds() {
178 $GLOBALS['TSFE']->gr_list = '0,-1';
179 $this->assertFalse($this->matchCondition->match('[usergroup = 0,-1]'));
180 }
181
182 /**
183 * Tests whether user comparison matches.
184 * @test
185 */
186 public function loginUserConditionMatchesAnyLoggedInUser() {
187 $GLOBALS['TSFE']->loginUser = TRUE;
188 $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
189 $this->assertTrue($this->matchCondition->match('[loginUser = *]'));
190 }
191
192 /**
193 * Tests whether user comparison matches.
194 * @test
195 */
196 public function loginUserConditionMatchesSingleLoggedInUser() {
197 $GLOBALS['TSFE']->loginUser = TRUE;
198 $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
199 $this->assertTrue($this->matchCondition->match('[loginUser = 13]'));
200 }
201
202 /**
203 * Tests whether user comparison matches.
204 * @test
205 */
206 public function loginUserConditionMatchesMultipleLoggedInUsers() {
207 $GLOBALS['TSFE']->loginUser = TRUE;
208 $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
209 $this->assertTrue($this->matchCondition->match('[loginUser = 999,13]'));
210 }
211
212 /**
213 * Tests whether user comparison matches.
214 * @test
215 */
216 public function loginUserConditionDoesNotMatchIfNotUserIsLoggedId() {
217 $GLOBALS['TSFE']->loginUser = FALSE;
218 $GLOBALS['TSFE']->fe_user->user['uid'] = 13;
219 $this->assertFalse($this->matchCondition->match('[loginUser = *]'));
220 $this->assertFalse($this->matchCondition->match('[loginUser = 13]'));
221 }
222
223 /**
224 * Tests whether numerical comparison matches.
225 * @test
226 */
227 public function globalVarConditionMatchesOnEqualExpression() {
228 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10]'));
229 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1]'));
230
231 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10]'));
232 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1]'));
233 }
234
235 /**
236 * Tests whether numerical comparison matches.
237 * @test
238 */
239 public function globalVarConditionMatchesOnNotEqualExpression() {
240 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
241 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
242 }
243
244 /**
245 * Tests whether numerical comparison matches.
246 * @test
247 */
248 public function globalVarConditionMatchesOnLowerThanExpression() {
249 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
250 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));
251 }
252
253 /**
254 * Tests whether numerical comparison matches.
255 * @test
256 */
257 public function globalVarConditionMatchesOnLowerThanOrEqualExpression() {
258 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 10]'));
259 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 20]'));
260
261 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.1]'));
262 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.2]'));
263 }
264
265 /**
266 * Tests whether numerical comparison matches.
267 * @test
268 */
269 public function globalVarConditionMatchesOnGreaterThanExpression() {
270 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 > 10]'));
271 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 > 10.1]'));
272 }
273
274 /**
275 * Tests whether numerical comparison matches.
276 * @test
277 */
278 public function globalVarConditionMatchesOnGreaterThanOrEqualExpression() {
279 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 >= 10]'));
280 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 >= 10]'));
281
282 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 >= 10.1]'));
283 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 >= 10.1]'));
284 }
285
286 /**
287 * Tests whether string comparison matches.
288 * @test
289 */
290 public function globalStringConditionMatchesOnEqualExpression() {
291 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.Test.Condition]'));
292 $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3]'));
293 }
294
295 /**
296 * Tests whether string comparison matches.
297 * @test
298 */
299 public function globalStringConditionMatchesWildcardExpression() {
300 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?Test?Condition]'));
301 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.T*t.Condition]'));
302 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?T*t?Condition]'));
303 }
304
305 /**
306 * Tests whether string comparison matches.
307 * @test
308 */
309 public function globalStringConditionMatchesRegularExpression() {
310 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^[A-Za-z3.]+$/]'));
311 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^TYPO3\..+Condition$/]'));
312 $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^FALSE/]'));
313 }
314
315 /**
316 * Tests whether string comparison matches.
317 * @test
318 */
319 public function globalStringConditionMatchesEmptyRegularExpression() {
320 $testKey = uniqid('test');
321 $_SERVER[$testKey] = '';
322 $this->assertTrue($this->matchCondition->match('[globalString = _SERVER|' . $testKey . ' = /^$/]'));
323 }
324
325 /**
326 * Tests whether treeLevel comparison matches.
327 * @test
328 */
329 public function treeLevelConditionMatchesSingleValue() {
330 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
331 $this->assertTrue($this->matchCondition->match('[treeLevel = 2]'));
332 }
333
334 /**
335 * Tests whether treeLevel comparison matches.
336 * @test
337 */
338 public function treeLevelConditionMatchesMultipleValues() {
339 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
340 $this->assertTrue($this->matchCondition->match('[treeLevel = 999,998,2]'));
341 }
342
343 /**
344 * Tests whether treeLevel comparison matches.
345 * @test
346 */
347 public function treeLevelConditionDoesNotMatchFaultyValue() {
348 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
349 $this->assertFalse($this->matchCondition->match('[treeLevel = 999]'));
350 }
351
352 /**
353 * Tests whether a page Id is found in the previous rootline entries.
354 * @test
355 */
356 public function PIDupinRootlineConditionMatchesSinglePageIdInRootline() {
357 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
358 $GLOBALS['TSFE']->id = 121;
359 $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 111]'));
360 }
361
362 /**
363 * Tests whether a page Id is found in the previous rootline entries.
364 * @test
365 */
366 public function PIDupinRootlineConditionMatchesMultiplePageIdsInRootline() {
367 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
368 $GLOBALS['TSFE']->id = 121;
369 $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 999,111,101]'));
370 }
371
372 /**
373 * Tests whether a page Id is found in the previous rootline entries.
374 * @test
375 */
376 public function PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline() {
377 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
378 $GLOBALS['TSFE']->id = 121;
379 $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 999]'));
380 }
381
382 /**
383 * Tests whether a page Id is found in the previous rootline entries.
384 * @test
385 */
386 public function PIDupinRootlineConditionDoesNotMatchLastPageIdInRootline() {
387 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
388 $GLOBALS['TSFE']->id = 121;
389 $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 121]'));
390 }
391
392 /**
393 * Tests whether a page Id is found in all rootline entries.
394 * @test
395 */
396 public function PIDinRootlineConditionMatchesSinglePageIdInRootline() {
397 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
398 $GLOBALS['TSFE']->id = 121;
399 $this->assertTrue($this->matchCondition->match('[PIDinRootline = 111]'));
400 }
401
402 /**
403 * Tests whether a page Id is found in all rootline entries.
404 * @test
405 */
406 public function PIDinRootlineConditionMatchesMultiplePageIdsInRootline() {
407 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
408 $GLOBALS['TSFE']->id = 121;
409 $this->assertTrue($this->matchCondition->match('[PIDinRootline = 999,111,101]'));
410 }
411
412 /**
413 * Tests whether a page Id is found in all rootline entries.
414 * @test
415 */
416 public function PIDinRootlineConditionMatchesLastPageIdInRootline() {
417 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
418 $GLOBALS['TSFE']->id = 121;
419 $this->assertTrue($this->matchCondition->match('[PIDinRootline = 121]'));
420 }
421
422 /**
423 * Tests whether a page Id is found in all rootline entries.
424 * @test
425 */
426 public function PIDinRootlineConditionDoesNotMatchPageIdNotInRootline() {
427 $GLOBALS['TSFE']->tmpl->rootLine = $this->rootline;
428 $GLOBALS['TSFE']->id = 121;
429 $this->assertFalse($this->matchCondition->match('[PIDinRootline = 999]'));
430 }
431
432 /**
433 * Tests whether the compatibility version can be evaluated.
434 * (e.g. 4.9 is compatible to 4.0 but not to 5.0)
435 * @test
436 */
437 public function compatVersionConditionMatchesOlderRelease() {
438 $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
439 $this->assertTrue($this->matchCondition->match('[compatVersion = 4.0]'));
440 }
441
442 /**
443 * Tests whether the compatibility version can be evaluated.
444 * (e.g. 4.9 is compatible to 4.0 but not to 5.0)
445 * @test
446 */
447 public function compatVersionConditionMatchesSameRelease() {
448 $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
449 $this->assertTrue($this->matchCondition->match('[compatVersion = 4.9]'));
450 }
451
452 /**
453 * Tests whether the compatibility version can be evaluated.
454 * (e.g. 4.9 is compatible to 4.0 but not to 5.0)
455 * @test
456 */
457 public function compatVersionConditionDoesNotMatchNewerRelease() {
458 $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
459 $this->assertFalse($this->matchCondition->match('[compatVersion = 5.0]'));
460 }
461
462 /**
463 * Tests whether the gneric fetching of variables works with the namespace 'GP'.
464 * @test
465 */
466 public function genericGetVariablesSucceedsWithNamespaceGP() {
467 $_GET = array('testGet' => 'getTest');
468 $_POST = array('testPost' => 'postTest');
469
470 $this->assertTrue($this->matchCondition->match('[globalString = GP:testGet = getTest]'));
471 $this->assertTrue($this->matchCondition->match('[globalString = GP:testPost = postTest]'));
472 }
473
474 /**
475 * Tests whether the gneric fetching of variables works with the namespace 'TSFE'.
476 * @test
477 */
478 public function genericGetVariablesSucceedsWithNamespaceTSFE() {
479 $GLOBALS['TSFE']->id = 1234567;
480 $GLOBALS['TSFE']->testSimpleObject = new stdClass();
481 $GLOBALS['TSFE']->testSimpleObject->testSimpleVariable = 'testValue';
482
483 $this->assertTrue($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
484 $this->assertTrue($this->matchCondition->match('[globalString = TSFE:testSimpleObject|testSimpleVariable = testValue]'));
485 }
486
487 /**
488 * Tests whether the gneric fetching of variables works with the namespace 'ENV'.
489 * @test
490 */
491 public function genericGetVariablesSucceedsWithNamespaceENV() {
492 $testKey = uniqid('test');
493 putenv($testKey .'=testValue');
494
495 $this->assertTrue($this->matchCondition->match('[globalString = ENV:' . $testKey . ' = testValue]'));
496 }
497
498 /**
499 * Tests whether the gneric fetching of variables works with the namespace 'IENV'.
500 * @test
501 */
502 public function genericGetVariablesSucceedsWithNamespaceIENV() {
503 $_SERVER['HTTP_HOST'] = t3lib_div::getIndpEnv('TYPO3_HOST_ONLY') . ':1234567';
504 $this->assertTrue($this->matchCondition->match('[globalString = IENV:TYPO3_PORT = 1234567]'));
505 }
506
507 /**
508 * Tests whether the gneric fetching of variables works with any global namespace.
509 * @test
510 */
511 public function genericGetVariablesSucceedsWithAnyGlobalNamespace() {
512 $GLOBALS[$this->testGlobalNamespace] = array(
513 'first' => 'testFirst',
514 'second' => array('third' => 'testThird'),
515 );
516
517 $this->assertTrue($this->matchCondition->match(
518 '[globalString = ' . $this->testGlobalNamespace . '|first = testFirst]'
519 ));
520 $this->assertTrue($this->matchCondition->match(
521 '[globalString = ' . $this->testGlobalNamespace . '|second|third = testThird]'
522 ));
523 }
524 }
525 ?>