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