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