[FEATURE] TypoScript: Allow easy comparison against multiple values
[Packages/TYPO3.CMS.git] / tests / Unit / t3lib / matchcondition / class.t3lib_matchcondition_backendTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 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_frontend.
28 *
29 * @author Oliver Hader <oliver@typo3.org>
30 * @package TYPO3
31 * @subpackage t3lib
32 */
33 class t3lib_matchCondition_backendTest 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_backend
46 */
47 private $matchCondition;
48
49 /**
50 * @var string
51 */
52 private $testTableName;
53
54 public function setUp() {
55 $this->backupGlobalVariables = array(
56 '_ENV' => $_ENV,
57 '_GET' => $_GET,
58 '_POST' => $_POST,
59 '_SERVER' => $_SERVER,
60 'TCA' => $GLOBALS['TCA'],
61 'TYPO3_DB' => $GLOBALS['TYPO3_DB'],
62 'TYPO3_CONF_VARS' => $GLOBALS['TYPO3_CONF_VARS'],
63 'T3_VAR' => $GLOBALS['T3_VAR'],
64 'BE_USER' => $GLOBALS['BE_USER'],
65 'SOBE' => $GLOBALS['SOBE'],
66 );
67
68 $this->testTableName = 't3lib_matchCondition_backend_testTable';
69 $this->testGlobalNamespace = uniqid('TEST');
70
71 $GLOBALS['TCA'][$this->testTableName] = array('ctrl' => array());
72 $GLOBALS[$this->testGlobalNamespace] = array();
73
74 $this->setUpBackend();
75
76 $this->matchCondition = t3lib_div::makeInstance('t3lib_matchCondition_backend');
77 }
78
79 public function tearDown() {
80 foreach ($this->backupGlobalVariables as $key => $data) {
81 $GLOBALS[$key] = $data;
82 }
83
84 unset($this->matchCondition);
85 unset($this->backupGlobalVariables);
86 unset($GLOBALS[$this->testGlobalNamespace]);
87 }
88
89 private function setUpBackend() {
90 $this->rootline = array(
91 2 => array('uid' => 121, 'pid' => 111),
92 1 => array('uid' => 111, 'pid' => 101,),
93 0 => array('uid' => 101, 'pid' => 0,),
94 );
95
96 $GLOBALS['BE_USER'] = $this->getMock('beUserAuth', array(), array(), '', FALSE);
97 }
98
99 private function setUpDatabaseMockForDeterminePageId() {
100 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('exec_SELECTquery', 'sql_fetch_assoc', 'sql_free_result'));
101 $GLOBALS['TYPO3_DB']->expects($this->any())->method('exec_SELECTquery')->will(
102 $this->returnCallback(array($this, 'determinePageIdByRecordDatabaseExecuteCallback'))
103 );
104 $GLOBALS['TYPO3_DB']->expects($this->any())->method('sql_fetch_assoc')->will(
105 $this->returnCallback(array($this, 'determinePageIdByRecordDatabaseFetchCallback'))
106 );
107 }
108
109 /**
110 * Tests whether a faulty expression fails.
111 * @test
112 */
113 public function simulateDisabledMatchAllConditionsFailsOnFaultyExpression() {
114 $this->matchCondition->matchAll = FALSE;
115 $this->assertFalse($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
116 }
117
118 /**
119 * Tests whether simulating positive matches for all conditions succeeds.
120 * @test
121 */
122 public function simulateEnabledMatchAllConditionsSucceeds() {
123 $this->matchCondition->setSimulateMatchResult(TRUE);
124 $this->assertTrue($this->matchCondition->match('[nullCondition = This expression would return FALSE in general]'));
125 }
126
127 /**
128 * Tests whether simulating positive matches for specific conditions succeeds.
129 * @test
130 */
131 public function simulateEnabledMatchSpecificConditionsSucceeds() {
132 $testCondition = '[' . uniqid('test') . ' = Any condition to simulate a positive match]';
133 $this->matchCondition->setSimulateMatchConditions(array($testCondition));
134 $this->assertTrue($this->matchCondition->match($testCondition));
135 }
136
137 /**
138 * Tests whether a condition matches Internet Explorer 7 on Windows.
139 *
140 * @return void
141 * @test
142 */
143 public function conditionMatchesInternetExplorer7Windows() {
144 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)';
145 $result = $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
146 $this->assertTrue($result);
147 }
148
149 /**
150 * Tests whether a condition does not match Internet Explorer 7 on Windows.
151 *
152 * @return void
153 * @test
154 */
155 public function conditionDoesNotMatchInternetExplorer7Windows() {
156 $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.25 (Windows NT 6.0; U; en)';
157 $result = $this->matchCondition->match('[browser = msie] && [version = 7] && [system = winNT]');
158 $this->assertFalse($result);
159 }
160
161
162 /**
163 * Tests whether a condition does match the iOS with the correct and more recent 'iOS'
164 * @test
165 */
166 public function conditionDoesMatchIosWithCorrectSystemKey() {
167 $_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';
168 $result = $this->matchCondition->match('[system = iOS]');
169 $this->assertTrue($result);
170 }
171
172 /**
173 * Tests whether a condition does match the iOS with the old 'mac'
174 * @test
175 */
176 public function conditionDoesMatchIosWithOldSystemKey() {
177 $_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';
178 $result = $this->matchCondition->match('[system = mac]');
179 $this->assertTrue($result);
180 }
181
182 /**
183 * Tests whether a condition does match Windows 2000 with the correct and more recent 'win2k'
184 * @test
185 */
186 public function conditionDoesMatchWindows2kWithNewSystemKey() {
187 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; SV1)';
188 $result = $this->matchCondition->match('[system = win2k]');
189 $this->assertTrue($result);
190 }
191
192 /**
193 * Tests whether a condition does match Windows 2000 with the old 'winNT'
194 * @test
195 */
196 public function conditionDoesMatchWindows2kWithOldSystemKey() {
197 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; SV1)';
198 $result = $this->matchCondition->match('[system = winNT]');
199 $this->assertTrue($result);
200 }
201
202 /**
203 * Tests whether a condition does match Windows NT with 'winNT'
204 * @test
205 */
206 public function conditionDoesMatchWindowsNtWithSystemKey() {
207 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)';
208 $result = $this->matchCondition->match('[system = winNT]');
209 $this->assertTrue($result);
210 }
211
212
213 /**
214 * Tests whether a condition does match Android with the correct and more recent 'android'
215 * @test
216 */
217 public function conditionDoesMatchAndroidWithNewSystemKey() {
218 $_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';
219 $result = $this->matchCondition->match('[system = android]');
220 $this->assertTrue($result);
221 }
222
223 /**
224 * Tests whether a condition does match Android with the old 'linux'
225 * @test
226 */
227 public function conditionDoesMatchAndroidWithOldSystemKey() {
228 $_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';
229 $result = $this->matchCondition->match('[system = linux]');
230 $this->assertTrue($result);
231 }
232
233 /**
234 * Tests whether a device type condition matches a crawler.
235 * @test
236 */
237 public function deviceConditionMatchesRobot() {
238 $_SERVER['HTTP_USER_AGENT'] = 'Googlebot/2.1 (+http://www.google.com/bot.html)';
239 $result = $this->matchCondition->match('[device = robot]');
240 $this->assertTrue($result);
241 }
242
243 /**
244 * Tests whether a device type condition does not match a crawler.
245 * @test
246 */
247 public function deviceConditionDoesNotMatchRobot() {
248 $_SERVER['HTTP_USER_AGENT'] = md5('Some strange user agent');
249 $result = $this->matchCondition->match('[device = robot]');
250 $this->assertFalse($result);
251 }
252
253 /**
254 * Tests whether the language comparison matches.
255 * @test
256 */
257 public function languageConditionMatchesSingleLanguageExpression() {
258 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
259 $this->assertTrue($this->matchCondition->match('[language = *de*]'));
260 $this->assertTrue($this->matchCondition->match('[language = *de-de*]'));
261 }
262
263 /**
264 * Tests whether the language comparison matches.
265 * @test
266 */
267 public function languageConditionMatchesMultipleLanguagesExpression() {
268 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
269 $this->assertTrue($this->matchCondition->match('[language = *en*,*de*]'));
270 $this->assertTrue($this->matchCondition->match('[language = *en-us*,*de-de*]'));
271 }
272
273 /**
274 * Tests whether the language comparison matches.
275 * @test
276 */
277 public function languageConditionMatchesCompleteLanguagesExpression() {
278 $this->markTestSkipped('This comparison seems to be incomplete in t3lib_matchCondition.');
279
280 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
281 $this->assertTrue($this->matchCondition->match('[language = de-de,de;q=0.8]'));
282 }
283
284 /**
285 * Tests whether usergroup comparison matches.
286 * @test
287 */
288 public function usergroupConditionMatchesSingleGroupId() {
289 $GLOBALS['BE_USER']->groupList = '13,14,15';
290 $this->assertTrue($this->matchCondition->match('[usergroup = 13]'));
291 }
292
293 /**
294 * Tests whether usergroup comparison matches.
295 * @test
296 */
297 public function usergroupConditionMatchesMultipleUserGroupId() {
298 $GLOBALS['BE_USER']->groupList = '13,14,15';
299 $this->assertTrue($this->matchCondition->match('[usergroup = 999,15,14,13]'));
300 }
301
302 /**
303 * Tests whether user comparison matches.
304 * @test
305 */
306 public function loginUserConditionMatchesAnyLoggedInUser() {
307 $GLOBALS['BE_USER']->user['uid'] = 13;
308 $this->assertTrue($this->matchCondition->match('[loginUser = *]'));
309 }
310
311 /**
312 * Tests whether user comparison matches.
313 * @test
314 */
315 public function loginUserConditionMatchesSingleLoggedInUser() {
316 $GLOBALS['BE_USER']->user['uid'] = 13;
317 $this->assertTrue($this->matchCondition->match('[loginUser = 13]'));
318 }
319
320 /**
321 * Tests whether user comparison matches.
322 * @test
323 */
324 public function loginUserConditionDoesNotMatchSingleLoggedInUser() {
325 $GLOBALS['BE_USER']->user['uid'] = 13;
326 $this->assertFalse($this->matchCondition->match('[loginUser = 999]'));
327 }
328
329 /**
330 * Tests whether user comparison matches.
331 * @test
332 */
333 public function loginUserConditionMatchesMultipleLoggedInUsers() {
334 $GLOBALS['BE_USER']->user['uid'] = 13;
335 $this->assertTrue($this->matchCondition->match('[loginUser = 999,13]'));
336 }
337
338 /**
339 * Tests whether checkinf for an admin user matches
340 * @test
341 */
342 public function adminUserConditionMatchesAdminUser() {
343 $GLOBALS['BE_USER']->user['uid'] = 13;
344 $GLOBALS['BE_USER']->user['admin'] = 1;
345 $this->assertTrue($this->matchCondition->match('[adminUser = 1]'));
346 }
347
348 /**
349 * Tests whether checkinf for an admin user matches
350 * @test
351 */
352 public function adminUserConditionMatchesRegularUser() {
353 $GLOBALS['BE_USER']->user['uid'] = 14;
354 $GLOBALS['BE_USER']->user['admin'] = 0;
355 $this->assertTrue($this->matchCondition->match('[adminUser = 0]'));
356 }
357
358 /**
359 * Tests whether checkinf for an admin user matches
360 * @test
361 */
362 public function adminUserConditionDoesNotMatchRegularUser() {
363 $GLOBALS['BE_USER']->user['uid'] = 14;
364 $GLOBALS['BE_USER']->user['admin'] = 0;
365 $this->assertFalse($this->matchCondition->match('[adminUser = 1]'));
366 }
367
368 /**
369 * Tests whether numerical comparison matches.
370 * @test
371 */
372 public function globalVarConditionMatchesOnEqualExpression() {
373 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10]'));
374 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1]'));
375
376 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10]'));
377 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1]'));
378 }
379
380 /**
381 * Tests whether numerical comparison matches.
382 * @test
383 */
384 public function globalVarConditionMatchesOnEqualExpressionWithMultipleValues() {
385 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10|20|30]'));
386 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1|20.2|30.3]'));
387 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 = 10|20|30]'));
388 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 = 10.1|20.2|30.3]'));
389
390 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10|20|30]'));
391 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1|20.2|30.3]'));
392 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 == 10|20|30]'));
393 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 == 10.1|20.2|30.3]'));
394 }
395
396 /**
397 * Tests whether numerical comparison matches.
398 * @test
399 */
400 public function globalVarConditionMatchesOnNotEqualExpression() {
401 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
402 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
403 }
404
405 /**
406 * Tests whether numerical comparison matches.
407 * @test
408 */
409 public function globalVarConditionMatchesOnNotEqualExpressionWithMultipleValues() {
410 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20|30]'));
411 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2|20.3]'));
412 }
413
414 /**
415 * Tests whether numerical comparison matches.
416 * @test
417 */
418 public function globalVarConditionMatchesOnLowerThanExpression() {
419 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
420 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));
421 }
422
423 /**
424 * Tests whether numerical comparison matches.
425 * @test
426 */
427 public function globalVarConditionMatchesOnLowerThanOrEqualExpression() {
428 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 10]'));
429 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 <= 20]'));
430
431 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.1]'));
432 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 <= 10.2]'));
433 }
434
435 /**
436 * Tests whether numerical comparison matches.
437 * @test
438 */
439 public function globalVarConditionMatchesOnGreaterThanExpression() {
440 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 > 10]'));
441 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 > 10.1]'));
442 }
443
444 /**
445 * Tests whether numerical comparison matches.
446 * @test
447 */
448 public function globalVarConditionMatchesOnGreaterThanOrEqualExpression() {
449 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 >= 10]'));
450 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 >= 10]'));
451
452 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 >= 10.1]'));
453 $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.2 >= 10.1]'));
454 }
455
456 /**
457 * Tests whether numerical comparison matches.
458 * @test
459 */
460 public function globalVarConditionMatchesOnEmptyExpressionWithNoValueSet() {
461 $testKey = uniqid('test');
462 $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
463 $this->assertTrue($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
464 }
465
466 /**
467 * Tests whether numerical comparison matches.
468 * @test
469 */
470 public function globalVarConditionDoesNotMatchOnEmptyExpressionWithValueSetToZero() {
471 $testKey = uniqid('test');
472
473 $_GET = array();
474 $_POST = array($testKey => 0);
475
476 $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . '=]'));
477 $this->assertFalse($this->matchCondition->match('[globalVar = GP:' . $testKey . ' = ]'));
478 }
479
480 /**
481 * Tests whether string comparison matches.
482 * @test
483 */
484 public function globalStringConditionMatchesOnEqualExpression() {
485 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.Test.Condition]'));
486 $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3]'));
487 }
488
489 /**
490 * Tests whether string comparison matches.
491 * @test
492 */
493 public function globalStringConditionMatchesWildcardExpression() {
494 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?Test?Condition]'));
495 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3.T*t.Condition]'));
496 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = TYPO3?T*t?Condition]'));
497 }
498
499 /**
500 * Tests whether string comparison matches.
501 * @test
502 */
503 public function globalStringConditionMatchesRegularExpression() {
504 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^[A-Za-z3.]+$/]'));
505 $this->assertTrue($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^TYPO3\..+Condition$/]'));
506 $this->assertFalse($this->matchCondition->match('[globalString = LIT:TYPO3.Test.Condition = /^FALSE/]'));
507 }
508
509 /**
510 * Tests whether string comparison matches.
511 * @test
512 */
513 public function globalStringConditionMatchesEmptyRegularExpression() {
514 $testKey = uniqid('test');
515 $_SERVER[$testKey] = '';
516 $this->assertTrue($this->matchCondition->match('[globalString = _SERVER|' . $testKey . ' = /^$/]'));
517 }
518
519 /**
520 * Tests whether treeLevel comparison matches.
521 * @test
522 */
523 public function treeLevelConditionMatchesSingleValue() {
524 $this->matchCondition->setRootline($this->rootline);
525 $this->assertTrue($this->matchCondition->match('[treeLevel = 2]'));
526 }
527
528 /**
529 * Tests whether treeLevel comparison matches.
530 * @test
531 */
532 public function treeLevelConditionMatchesMultipleValues() {
533 $this->matchCondition->setRootline($this->rootline);
534 $this->assertTrue($this->matchCondition->match('[treeLevel = 999,998,2]'));
535 }
536
537 /**
538 * Tests whether treeLevel comparison matches.
539 * @test
540 */
541 public function treeLevelConditionDoesNotMatchFaultyValue() {
542 $this->matchCondition->setRootline($this->rootline);
543 $this->assertFalse($this->matchCondition->match('[treeLevel = 999]'));
544 }
545
546 /**
547 * Tests whether treeLevel comparison matches when creating new pages.
548 * @test
549 */
550 public function treeLevelConditionMatchesCurrentPageIdWhileEditingNewPage() {
551 $GLOBALS['SOBE'] = $this->getMock('SC_alt_doc', array());
552 $GLOBALS['SOBE']->elementsData = array(
553 array(
554 'table' => 'pages',
555 'uid' => 'NEW4adc6021e37e7',
556 'pid' => 121,
557 'cmd' => 'new',
558 'deleteAccess' => 0,
559 ),
560 );
561 $GLOBALS['SOBE']->data = array();
562
563 $this->matchCondition->setRootline($this->rootline);
564 $this->matchCondition->setPageId(121);
565 $this->assertTrue($this->matchCondition->match('[treeLevel = 3]'));
566 }
567
568 /**
569 * Tests whether treeLevel comparison matches when creating new pages.
570 * @test
571 */
572 public function treeLevelConditionMatchesCurrentPageIdWhileSavingNewPage() {
573 $GLOBALS['SOBE'] = $this->getMock('SC_alt_doc', array());
574 $GLOBALS['SOBE']->elementsData = array(
575 array(
576 'table' => 'pages',
577 /// 999 is the uid of the page that was just created
578 'uid' => 999,
579 'pid' => 121,
580 'cmd' => 'edit',
581 'deleteAccess' => 1,
582 ),
583 );
584 $GLOBALS['SOBE']->data = array(
585 'pages' => array(
586 'NEW4adc6021e37e7' => array(
587 'pid' => 121,
588 ),
589 ),
590 );
591
592 $this->matchCondition->setRootline($this->rootline);
593 $this->matchCondition->setPageId(121);
594 $this->assertTrue($this->matchCondition->match('[treeLevel = 3]'));
595 }
596
597 /**
598 * Tests whether a page Id is found in the previous rootline entries.
599 * @test
600 */
601 public function PIDupinRootlineConditionMatchesSinglePageIdInRootline() {
602 $this->matchCondition->setRootline($this->rootline);
603 $this->matchCondition->setPageId(121);
604 $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 111]'));
605 }
606
607 /**
608 * Tests whether a page Id is found in the previous rootline entries.
609 * @test
610 */
611 public function PIDupinRootlineConditionMatchesMultiplePageIdsInRootline() {
612 $this->matchCondition->setRootline($this->rootline);
613 $this->matchCondition->setPageId(121);
614 $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 999,111,101]'));
615 }
616
617 /**
618 * Tests whether a page Id is found in the previous rootline entries.
619 * @test
620 */
621 public function PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline() {
622 $this->matchCondition->setRootline($this->rootline);
623 $this->matchCondition->setPageId(121);
624 $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 999]'));
625 }
626
627 /**
628 * Tests whether a page Id is found in the previous rootline entries.
629 * @test
630 */
631 public function PIDupinRootlineConditionDoesNotMatchLastPageIdInRootline() {
632 $this->matchCondition->setRootline($this->rootline);
633 $this->matchCondition->setPageId(121);
634 $this->assertFalse($this->matchCondition->match('[PIDupinRootline = 121]'));
635 }
636
637 /**
638 * Tests whether a page Id is found in the previous rootline entries.
639 * @test
640 */
641 public function PIDupinRootlineConditionMatchesCurrentPageIdWhileEditingNewPage() {
642 $GLOBALS['SOBE'] = $this->getMock('SC_alt_doc', array());
643 $GLOBALS['SOBE']->elementsData = array(
644 array(
645 'table' => 'pages',
646 'uid' => 'NEW4adc6021e37e7',
647 'pid' => 121,
648 'cmd' => 'new',
649 'deleteAccess' => 0,
650 ),
651 );
652 $GLOBALS['SOBE']->data = array();
653
654 $this->matchCondition->setRootline($this->rootline);
655 $this->matchCondition->setPageId(121);
656 $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 121]'));
657 }
658
659 /**
660 * Tests whether a page Id is found in the previous rootline entries.
661 * @test
662 */
663 public function PIDupinRootlineConditionMatchesCurrentPageIdWhileSavingNewPage() {
664 $GLOBALS['SOBE'] = $this->getMock('SC_alt_doc', array());
665 $GLOBALS['SOBE']->elementsData = array(
666 array(
667 'table' => 'pages',
668 /// 999 is the uid of the page that was just created
669 'uid' => 999,
670 'pid' => 121,
671 'cmd' => 'edit',
672 'deleteAccess' => 1,
673 ),
674 );
675 $GLOBALS['SOBE']->data = array(
676 'pages' => array(
677 'NEW4adc6021e37e7' => array(
678 'pid' => 121,
679 ),
680 ),
681 );
682
683 $this->matchCondition->setRootline($this->rootline);
684 $this->matchCondition->setPageId(121);
685 $this->assertTrue($this->matchCondition->match('[PIDupinRootline = 121]'));
686 }
687
688 /**
689 * Tests whether a page Id is found in all rootline entries.
690 * @test
691 */
692 public function PIDinRootlineConditionMatchesSinglePageIdInRootline() {
693 $this->matchCondition->setRootline($this->rootline);
694 $this->matchCondition->setPageId(121);
695 $this->assertTrue($this->matchCondition->match('[PIDinRootline = 111]'));
696 }
697
698 /**
699 * Tests whether a page Id is found in all rootline entries.
700 * @test
701 */
702 public function PIDinRootlineConditionMatchesMultiplePageIdsInRootline() {
703 $this->matchCondition->setRootline($this->rootline);
704 $this->matchCondition->setPageId(121);
705 $this->assertTrue($this->matchCondition->match('[PIDinRootline = 999,111,101]'));
706 }
707
708 /**
709 * Tests whether a page Id is found in all rootline entries.
710 * @test
711 */
712 public function PIDinRootlineConditionMatchesLastPageIdInRootline() {
713 $this->matchCondition->setRootline($this->rootline);
714 $this->matchCondition->setPageId(121);
715 $this->assertTrue($this->matchCondition->match('[PIDinRootline = 121]'));
716 }
717
718 /**
719 * Tests whether a page Id is found in all rootline entries.
720 * @test
721 */
722 public function PIDinRootlineConditionDoesNotMatchPageIdNotInRootline() {
723 $this->matchCondition->setRootline($this->rootline);
724 $this->matchCondition->setPageId(121);
725 $this->assertFalse($this->matchCondition->match('[PIDinRootline = 999]'));
726 }
727
728 /**
729 * Tests whether the compatibility version can be evaluated.
730 * (e.g. 4.9 is compatible to 4.0 but not to 5.0)
731 * @test
732 */
733 public function compatVersionConditionMatchesOlderRelease() {
734 $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
735 $this->assertTrue($this->matchCondition->match('[compatVersion = 4.0]'));
736 }
737
738 /**
739 * Tests whether the compatibility version can be evaluated.
740 * (e.g. 4.9 is compatible to 4.0 but not to 5.0)
741 * @test
742 */
743 public function compatVersionConditionMatchesSameRelease() {
744 $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
745 $this->assertTrue($this->matchCondition->match('[compatVersion = 4.9]'));
746 }
747
748 /**
749 * Tests whether the compatibility version can be evaluated.
750 * (e.g. 4.9 is compatible to 4.0 but not to 5.0)
751 * @test
752 */
753 public function compatVersionConditionDoesNotMatchNewerRelease() {
754 $GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version'] = '4.9';
755 $this->assertFalse($this->matchCondition->match('[compatVersion = 5.0]'));
756 }
757
758 /**
759 * Tests whether the generic fetching of variables works with the namespace 'GP'.
760 * @test
761 */
762 public function genericGetVariablesSucceedsWithNamespaceGP() {
763 $_GET = array('testGet' => 'getTest');
764 $_POST = array('testPost' => 'postTest');
765
766 $this->assertTrue($this->matchCondition->match('[globalString = GP:testGet = getTest]'));
767 $this->assertTrue($this->matchCondition->match('[globalString = GP:testPost = postTest]'));
768 }
769
770 /**
771 * Tests whether the generic fetching of variables does not work with the namespace 'TSFE',
772 * since we are in the backend context here.
773 * @test
774 */
775 public function genericGetVariablesFailsWithNamespaceTSFE() {
776 $GLOBALS['TSFE'] = new stdClass();
777 $GLOBALS['TSFE']->id = 1234567;
778
779 $this->assertFalse($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
780 }
781
782 /**
783 * Tests whether the generic fetching of variables works with the namespace 'ENV'.
784 * @test
785 */
786 public function genericGetVariablesSucceedsWithNamespaceENV() {
787 $testKey = uniqid('test');
788 putenv($testKey .'=testValue');
789
790 $this->assertTrue($this->matchCondition->match('[globalString = ENV:' . $testKey . ' = testValue]'));
791 }
792
793 /**
794 * Tests whether the generic fetching of variables works with the namespace 'IENV'.
795 * @test
796 */
797 public function genericGetVariablesSucceedsWithNamespaceIENV() {
798 $_SERVER['HTTP_HOST'] = t3lib_div::getIndpEnv('TYPO3_HOST_ONLY') . ':1234567';
799 $this->assertTrue($this->matchCondition->match('[globalString = IENV:TYPO3_PORT = 1234567]'));
800 }
801
802 /**
803 * Tests whether the generic fetching of variables works with any global namespace.
804 * @test
805 */
806 public function genericGetVariablesSucceedsWithAnyGlobalNamespace() {
807 $GLOBALS[$this->testGlobalNamespace] = array(
808 'first' => 'testFirst',
809 'second' => array('third' => 'testThird'),
810 );
811
812 $this->assertTrue($this->matchCondition->match(
813 '[globalString = ' . $this->testGlobalNamespace . '|first = testFirst]'
814 ));
815 $this->assertTrue($this->matchCondition->match(
816 '[globalString = ' . $this->testGlobalNamespace . '|second|third = testThird]'
817 ));
818 }
819
820 /**
821 * Tests whether determining a pageId works.
822 * @test
823 */
824 public function pageIdCanBeDeterminedWhileCallingModuleWithPageTree() {
825 $_GET['id'] = 999;
826
827 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
828 $this->assertEquals(999, $this->matchCondition->getPageId());
829 }
830
831 /**
832 * Tests whether determining a pageId works.
833 * @test
834 */
835 public function pageIdCanBeDeterminedWhileEditingAPageRecord() {
836 $_GET['edit']['pages'][999] = 'edit';
837
838 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
839 $this->assertEquals(999, $this->matchCondition->getPageId());
840 }
841
842 /**
843 * Tests whether determining a pageId works.
844 * @test
845 */
846 public function pageIdCanBeDeterminedWhileEditingARegularRecord() {
847 $this->setUpDatabaseMockForDeterminePageId();
848
849 $_GET['edit'][$this->testTableName][13] = 'edit';
850
851 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
852 $this->assertEquals(999, $this->matchCondition->getPageId());
853 }
854
855 /**
856 * Tests whether determining a pageId works.
857 * @test
858 */
859 public function pageIdCanBeDeterminedWhileCreatingARecord() {
860 $_GET['edit']['pages'][999] = 'new';
861
862 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
863 $this->assertEquals(999, $this->matchCondition->getPageId());
864 }
865
866 /**
867 * Tests whether determining a pageId works.
868 * @test
869 */
870 public function pageIdCanBeDeterminedWhileCreatingARecordAfterAnExistingRecord() {
871 $this->setUpDatabaseMockForDeterminePageId();
872
873 $_GET['edit'][$this->testTableName][-13] = 'new';
874
875 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
876 $this->assertEquals(999, $this->matchCondition->getPageId());
877 }
878
879 /**
880 * Tests whether determining a pageId works.
881 * @test
882 */
883 public function pageIdCanBeDeterminedWhileDeletingAPageRecord() {
884 $_GET['cmd']['pages'][999]['delete'] = 1;
885
886 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
887 $this->assertEquals(999, $this->matchCondition->getPageId());
888 }
889
890 /**
891 * Tests whether determining a pageId works.
892 * @test
893 */
894 public function pageIdCanBeDeterminedWhileCopyingARecordToAnotherPage() {
895 $_GET['cmd']['pages'][121]['copy'] = 999;
896
897 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
898 $this->assertEquals(999, $this->matchCondition->getPageId());
899 }
900
901 /**
902 * Tests whether determining a pageId works.
903 * @test
904 */
905 public function pageIdCanBeDeterminedWhileCopyingARecordAfterAnExistingRecord() {
906 $this->setUpDatabaseMockForDeterminePageId();
907
908 $_GET['cmd'][$this->testTableName][121]['copy'] = -13;
909
910 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
911 $this->assertEquals(999, $this->matchCondition->getPageId());
912 }
913
914 /**
915 * Tests whether determining a pageId works.
916 * @test
917 */
918 public function pageIdCanBeDeterminedWhileMovingARecordToAnotherPage() {
919 $_GET['cmd']['pages'][121]['move'] = 999;
920
921 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
922 $this->assertEquals(999, $this->matchCondition->getPageId());
923 }
924
925 /**
926 * Callback method for pageIdCanBeDetermined test cases.
927 * Simulates TYPO3_DB->exec_SELECTquery().
928 *
929 * @param string $fields
930 * @param string $table
931 * @param string $where
932 * @return mixed
933 */
934 public function determinePageIdByRecordDatabaseExecuteCallback($fields, $table, $where) {
935 if ($table === $this->testTableName) {
936 return array(
937 'scope' => $this->testTableName,
938 'data' => array(
939 'pid' => 999,
940 ),
941 );
942 } else {
943 return FALSE;
944 }
945 }
946
947 /**
948 * Callback method for pageIdCanBeDetermined test cases.
949 * Simulates TYPO3_DB->sql_fetch_assoc().
950 *
951 * @param mixed $resource
952 * @return mixed
953 */
954 public function determinePageIdByRecordDatabaseFetchCallback($resource) {
955 if (is_array($resource) && ($resource['scope'] === $this->testTableName)) {
956 return $resource['data'];
957 } else {
958 return FALSE;
959 }
960 }
961 }
962 ?>