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