[TASK] Introduce runtime cache for getIndpEnv()
[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
17 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Testcase for class \TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher.
22 */
23 class ConditionMatcherTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
24
25 /**
26 * @var array
27 */
28 protected $rootline;
29
30 /**
31 * @var \TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher
32 */
33 protected $matchCondition;
34
35 /**
36 * @var string
37 */
38 protected $testGlobalNamespace;
39
40 /**
41 * @var string
42 */
43 protected $testTableName;
44
45 /**
46 * Set up tests
47 */
48 protected function setUp() {
49 $this->testTableName = 'conditionMatcherTestTable';
50 $this->testGlobalNamespace = $this->getUniqueId('TEST');
51 $GLOBALS['TCA'][$this->testTableName] = array('ctrl' => array());
52 $GLOBALS[$this->testGlobalNamespace] = array();
53 GeneralUtility::flushInternalRuntimeCaches();
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 = '[' . $this->getUniqueId('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 = $this->getUniqueId('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 = $this->getUniqueId('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 = $this->getUniqueId('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 = $this->getUniqueId('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. 7.9 is compatible to 7.0 but not to 15.0)
772 *
773 * @test
774 */
775 public function compatVersionConditionMatchesOlderRelease() {
776 $this->assertTrue($this->matchCondition->match('[compatVersion = 7.0]'));
777 }
778
779 /**
780 * Tests whether the compatibility version can be evaluated.
781 * (e.g. 7.9 is compatible to 7.0 but not to 15.0)
782 *
783 * @test
784 */
785 public function compatVersionConditionMatchesSameRelease() {
786 $this->assertTrue($this->matchCondition->match('[compatVersion = ' . TYPO3_branch . ']'));
787 }
788
789 /**
790 * Tests whether the compatibility version can be evaluated.
791 * (e.g. 7.9 is compatible to 7.0 but not to 15.0)
792 *
793 * @test
794 */
795 public function compatVersionConditionDoesNotMatchNewerRelease() {
796 $this->assertFalse($this->matchCondition->match('[compatVersion = 15.0]'));
797 }
798
799 /**
800 * Tests whether the generic fetching of variables works with the namespace 'GP'.
801 *
802 * @test
803 */
804 public function genericGetVariablesSucceedsWithNamespaceGP() {
805 $_GET = array('testGet' => 'getTest');
806 $_POST = array('testPost' => 'postTest');
807 $this->assertTrue($this->matchCondition->match('[globalString = GP:testGet = getTest]'));
808 $this->assertTrue($this->matchCondition->match('[globalString = GP:testPost = postTest]'));
809 }
810
811 /**
812 * Tests whether the generic fetching of variables does not work with the namespace 'TSFE',
813 * since we are in the backend context here.
814 *
815 * @test
816 */
817 public function genericGetVariablesFailsWithNamespaceTSFE() {
818 $GLOBALS['TSFE'] = new \stdClass();
819 $GLOBALS['TSFE']->id = 1234567;
820 $this->assertFalse($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
821 }
822
823 /**
824 * Tests whether the generic fetching of variables works with the namespace 'ENV'.
825 *
826 * @test
827 */
828 public function genericGetVariablesSucceedsWithNamespaceENV() {
829 $testKey = $this->getUniqueId('test');
830 putenv($testKey . '=testValue');
831 $this->assertTrue($this->matchCondition->match('[globalString = ENV:' . $testKey . ' = testValue]'));
832 }
833
834 /**
835 * Tests whether the generic fetching of variables works with the namespace 'IENV'.
836 *
837 * @test
838 */
839 public function genericGetVariablesSucceedsWithNamespaceIENV() {
840 $_SERVER['HTTP_HOST'] = GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY') . ':1234567';
841 $this->assertTrue($this->matchCondition->match('[globalString = IENV:TYPO3_PORT = 1234567]'));
842 }
843
844 /**
845 * Tests whether the generic fetching of variables works with any global namespace.
846 *
847 * @test
848 */
849 public function genericGetVariablesSucceedsWithAnyGlobalNamespace() {
850 $GLOBALS[$this->testGlobalNamespace] = array(
851 'first' => 'testFirst',
852 'second' => array('third' => 'testThird')
853 );
854 $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|first = testFirst]'));
855 $this->assertTrue($this->matchCondition->match('[globalString = ' . $this->testGlobalNamespace . '|second|third = testThird]'));
856 }
857
858 /**
859 * Tests whether determining a pageId works.
860 *
861 * @test
862 */
863 public function pageIdCanBeDeterminedWhileCallingModuleWithPageTree() {
864 $_GET['id'] = 999;
865 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
866 $this->assertEquals(999, $this->matchCondition->getPageId());
867 }
868
869 /**
870 * Tests whether determining a pageId works.
871 *
872 * @test
873 */
874 public function pageIdCanBeDeterminedWhileEditingAPageRecord() {
875 $_GET['edit']['pages'][999] = '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 pageIdCanBeDeterminedWhileEditingARegularRecord() {
886 $this->setUpDatabaseMockForDeterminePageId();
887 $_GET['edit'][$this->testTableName][13] = 'edit';
888 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
889 $this->assertEquals(999, $this->matchCondition->getPageId());
890 }
891
892 /**
893 * Tests whether determining a pageId works.
894 *
895 * @test
896 */
897 public function pageIdCanBeDeterminedWhileCreatingARecord() {
898 $_GET['edit']['pages'][999] = '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 pageIdCanBeDeterminedWhileCreatingARecordAfterAnExistingRecord() {
909 $this->setUpDatabaseMockForDeterminePageId();
910 $_GET['edit'][$this->testTableName][-13] = 'new';
911 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
912 $this->assertEquals(999, $this->matchCondition->getPageId());
913 }
914
915 /**
916 * Tests whether determining a pageId works.
917 *
918 * @test
919 */
920 public function pageIdCanBeDeterminedWhileDeletingAPageRecord() {
921 $_GET['cmd']['pages'][999]['delete'] = 1;
922 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
923 $this->assertEquals(999, $this->matchCondition->getPageId());
924 }
925
926 /**
927 * Tests whether determining a pageId works.
928 *
929 * @test
930 */
931 public function pageIdCanBeDeterminedWhileCopyingARecordToAnotherPage() {
932 $_GET['cmd']['pages'][121]['copy'] = 999;
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 pageIdCanBeDeterminedWhileCopyingARecordAfterAnExistingRecord() {
943 $this->setUpDatabaseMockForDeterminePageId();
944 $_GET['cmd'][$this->testTableName][121]['copy'] = -13;
945 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
946 $this->assertEquals(999, $this->matchCondition->getPageId());
947 }
948
949 /**
950 * Tests whether determining a pageId works.
951 *
952 * @test
953 */
954 public function pageIdCanBeDeterminedWhileMovingARecordToAnotherPage() {
955 $_GET['cmd']['pages'][121]['move'] = 999;
956 $this->matchCondition->match('[globalVar = LIT:10 = 10]');
957 $this->assertEquals(999, $this->matchCondition->getPageId());
958 }
959
960 /**
961 * Callback method for pageIdCanBeDetermined test cases.
962 * Simulates TYPO3_DB->exec_SELECTquery().
963 *
964 * @param string $fields
965 * @param string $table
966 * @param string $where
967 * @return mixed
968 */
969 public function determinePageIdByRecordDatabaseExecuteCallback($fields, $table, $where) {
970 if ($table === $this->testTableName) {
971 return array(
972 'scope' => $this->testTableName,
973 'data' => array(
974 'pid' => 999
975 )
976 );
977 } else {
978 return FALSE;
979 }
980 }
981
982 /**
983 * Callback method for pageIdCanBeDetermined test cases.
984 * Simulates TYPO3_DB->sql_fetch_assoc().
985 *
986 * @param mixed $resource
987 * @return mixed
988 */
989 public function determinePageIdByRecordDatabaseFetchCallback($resource) {
990 if (is_array($resource) && $resource['scope'] === $this->testTableName) {
991 return $resource['data'];
992 } else {
993 return FALSE;
994 }
995 }
996
997 /**
998 * @test
999 * @expectedException \TYPO3\CMS\Backend\Tests\Unit\Configuration\TypoScript\ConditionMatching\Fixtures\TestConditionException
1000 */
1001 public function matchCallsTestConditionAndHandsOverParameters() {
1002 $this->matchCondition->match('[TYPO3\\CMS\\Backend\\Tests\\Unit\\Configuration\\TypoScript\\ConditionMatching\\Fixtures\\TestCondition = 7, != 6]');
1003 }
1004 }