‪TYPO3CMS  9.5
ConditionMatcherTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 use Prophecy\Argument;
26 use TYPO3\CMS\Core\Package\PackageManager;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
37 class ‪ConditionMatcherTest extends UnitTestCase
38 {
42  protected ‪$subject;
43 
47  protected ‪$testGlobalNamespace;
48 
52  protected ‪$resetSingletonInstances = true;
53 
54  protected function ‪setUp(): void
55  {
56  ‪$GLOBALS['TYPO3_REQUEST'] = new ‪ServerRequest();
57  $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
58  $cacheFrontendProphecy->has(Argument::any())->willReturn(false);
59  $cacheFrontendProphecy->set(Argument::any(), Argument::any())->willReturn(null);
60  $cacheManagerProphecy = $this->prophesize(CacheManager::class);
61  $cacheManagerProphecy->getCache('cache_core')->willReturn($cacheFrontendProphecy->reveal());
62  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
63 
64  $packageManagerProphecy = $this->prophesize(PackageManager::class);
65  $corePackageProphecy = $this->prophesize(PackageInterface::class);
66  $corePackageProphecy->getPackagePath()->willReturn(__DIR__ . '/../../../../../../../sysext/core/');
67  $packageManagerProphecy->getActivePackages()->willReturn([
68  $corePackageProphecy->reveal()
69  ]);
70  GeneralUtility::setSingletonInstance(PackageManager::class, $packageManagerProphecy->reveal());
71 
72  $this->testGlobalNamespace = $this->getUniqueId('TEST');
74  ‪$GLOBALS['TSFE'] = $this->prophesize(TypoScriptFrontendController::class)->reveal();
75  ‪$GLOBALS['TSFE']->page = [];
76  ‪$GLOBALS['TSFE']->tmpl = new \stdClass();
77  ‪$GLOBALS['TSFE']->tmpl->rootLine = [
78  2 => ['uid' => 121, 'pid' => 111],
79  1 => ['uid' => 111, 'pid' => 101],
80  0 => ['uid' => 101, 'pid' => 0]
81  ];
82 
83  $frontedUserAuthentication = $this->getMockBuilder(FrontendUserAuthentication::class)
84  ->setMethods(['dummy'])
85  ->getMock();
86 
87  $frontedUserAuthentication->user['uid'] = 13;
88  $frontedUserAuthentication->groupData['uid'] = [14];
89  ‪$GLOBALS['TSFE']->fe_user = $frontedUserAuthentication;
91  }
92 
93  protected function ‪getFreshConditionMatcher()
94  {
95  $this->subject = new ‪ConditionMatcher(new ‪Context([
96  'frontend.user' => new ‪UserAspect(‪$GLOBALS['TSFE']->fe_user)
97  ]));
98  $this->subject->setLogger($this->prophesize(Logger::class)->reveal());
99  }
100 
106  public function ‪usergroupConditionMatchesSingleGroupId(): void
107  {
108  ‪$subject = new ConditionMatcher(new Context([
109  'frontend.user' => new UserAspect(new FrontendUserAuthentication(), [13, 14, 15])
110  ]));
111  $loggerProphecy = $this->prophesize(Logger::class);
112  ‪$subject->setLogger($loggerProphecy->reveal());
113  $this->assertTrue(‪$subject->‪match('[usergroup(13)]'));
114  $this->assertTrue(‪$subject->‪match('[usergroup("13")]'));
115  $this->assertTrue(‪$subject->‪match('[usergroup(\'13\')]'));
116  }
117 
124  {
125  ‪$subject = new ConditionMatcher(new Context([
126  'frontend.user' => new UserAspect(new FrontendUserAuthentication(), [13, 14, 15])
127  ]));
128  $loggerProphecy = $this->prophesize(Logger::class);
129  ‪$subject->setLogger($loggerProphecy->reveal());
130  $this->assertFalse(‪$subject->‪match('[usergroup(999,15,14,13)]'));
131  $this->assertTrue(‪$subject->‪match('[usergroup("999,15,14,13")]'));
132  $this->assertTrue(‪$subject->‪match('[usergroup(\'999,15,14,13\')]'));
133  }
134 
141  {
142  ‪$subject = new ConditionMatcher(new Context([
143  'frontend.user' => new UserAspect(new FrontendUserAuthentication(), [0, -1])
144  ]));
145  $loggerProphecy = $this->prophesize(Logger::class);
146  ‪$subject->setLogger($loggerProphecy->reveal());
147  $this->assertFalse(‪$subject->‪match('[usergroup("0,-1")]'));
148  $this->assertFalse(‪$subject->‪match('[usergroup(\'0,-1\')]'));
149  }
150 
156  public function ‪loginUserConditionMatchesAnyLoggedInUser(): void
157  {
159  $this->assertTrue($this->subject->match('[loginUser("*")]'));
160  $this->assertTrue($this->subject->match('[loginUser(\'*\')]'));
161  }
162 
168  public function ‪loginUserConditionMatchesSingleLoggedInUser(): void
169  {
171  $this->assertTrue($this->subject->match('[loginUser(13)]'));
172  $this->assertTrue($this->subject->match('[loginUser("13")]'));
173  $this->assertTrue($this->subject->match('[loginUser(\'13\')]'));
174  }
175 
182  {
184  $this->assertTrue($this->subject->match('[loginUser("999,13")]'));
185  $this->assertTrue($this->subject->match('[loginUser(\'999,13\')]'));
186  }
187 
194  {
195  $user = new FrontendUserAuthentication();
196  $user->user['uid'] = 13;
197  ‪$subject = new ConditionMatcher(new Context([
198  'frontend.user' => new UserAspect($user)
199  ]));
200  $loggerProphecy = $this->prophesize(Logger::class);
201  ‪$subject->setLogger($loggerProphecy->reveal());
202  $this->assertFalse(‪$subject->‪match('[loginUser("*")]'));
203  $this->assertTrue(‪$subject->‪match('[loginUser("*") == false]'));
204  $this->assertFalse(‪$subject->‪match('[loginUser("13")]'));
205  $this->assertFalse(‪$subject->‪match('[loginUser(\'*\')]'));
206  $this->assertFalse(‪$subject->‪match('[loginUser(\'13\')]'));
207  }
208 
214  public function ‪loginUserConditionMatchIfUserIsNotLoggedIn(): void
215  {
216  $user = new FrontendUserAuthentication();
217  ‪$subject = new ConditionMatcher(new Context([
218  'frontend.user' => new UserAspect($user)
219  ]));
220  $loggerProphecy = $this->prophesize(Logger::class);
221  ‪$subject->setLogger($loggerProphecy->reveal());
222  $this->assertTrue(‪$subject->‪match('[loginUser(\'*\') == false]'));
223  $this->assertTrue(‪$subject->‪match('[loginUser("*") == false]'));
224  }
225 
231  public function ‪treeLevelConditionMatchesSingleValue(): void
232  {
233  $this->assertTrue($this->subject->match('[tree.level == 2]'));
234  }
235 
241  public function ‪treeLevelConditionMatchesMultipleValues(): void
242  {
243  $this->assertTrue($this->subject->match('[tree.level in [999,998,2]]'));
244  }
245 
251  public function ‪treeLevelConditionDoesNotMatchFaultyValue(): void
252  {
253  $this->assertFalse($this->subject->match('[tree.level == 999]'));
254  }
255 
262  {
263  ‪$GLOBALS['TSFE']->id = 121;
265  $this->assertTrue($this->subject->match('[111 in tree.rootLineIds]'));
266  $this->assertTrue($this->subject->match('["111" in tree.rootLineIds]'));
267  $this->assertTrue($this->subject->match('[\'111\' in tree.rootLineIds]'));
268  }
269 
276  {
277  ‪$GLOBALS['TSFE']->id = 121;
279  $this->assertFalse($this->subject->match('[999 in tree.rootLineIds]'));
280  }
281 
288  {
289  ‪$GLOBALS['TSFE']->id = 121;
291  $this->assertTrue($this->subject->match('[111 in tree.rootLineIds]'));
292  }
293 
300  {
301  ‪$GLOBALS['TSFE']->id = 121;
303  $this->assertTrue($this->subject->match('[121 in tree.rootLineIds]'));
304  }
305 
312  {
313  ‪$GLOBALS['TSFE']->id = 121;
314  $this->assertFalse($this->subject->match('[999 in tree.rootLineIds]'));
315  }
316 
323  public function ‪compatVersionConditionMatchesOlderRelease(): void
324  {
325  $this->assertTrue($this->subject->match('[compatVersion(7.0)]'));
326  $this->assertTrue($this->subject->match('[compatVersion("7.0")]'));
327  $this->assertTrue($this->subject->match('[compatVersion(\'7.0\')]'));
328  }
329 
336  public function ‪compatVersionConditionMatchesSameRelease(): void
337  {
338  $this->assertTrue($this->subject->match('[compatVersion(' . TYPO3_branch . ')]'));
339  }
340 
348  {
349  $this->assertFalse($this->subject->match('[compatVersion(15.0)]'));
350  $this->assertFalse($this->subject->match('[compatVersion("15.0")]'));
351  $this->assertFalse($this->subject->match('[compatVersion(\'15.0\')]'));
352  }
353 
360  {
361  ‪$GLOBALS['TSFE']->id = 1234567;
362  ‪$GLOBALS['TSFE']->testSimpleObject = new \stdClass();
363  ‪$GLOBALS['TSFE']->testSimpleObject->testSimpleVariable = 'testValue';
364 
366  $this->assertTrue($this->subject->match('[getTSFE().id == 1234567]'));
367  $this->assertTrue($this->subject->match('[getTSFE().testSimpleObject.testSimpleVariable == "testValue"]'));
368  }
369 
376  {
377  $prophecy = $this->prophesize(FrontendUserAuthentication::class);
378  $prophecy->getSessionData(Argument::exact('foo'))->willReturn(['bar' => 1234567]);
379  ‪$GLOBALS['TSFE']->fe_user = $prophecy->reveal();
380 
382  $this->assertTrue($this->subject->match('[session("foo|bar") == 1234567]'));
383  }
384 
390  public function ‪genericGetVariablesSucceedsWithNamespaceENV(): void
391  {
392  $testKey = $this->getUniqueId('test');
393  putenv($testKey . '=testValue');
395  $this->assertTrue($this->subject->match('[getenv("' . $testKey . '") == "testValue"]'));
396  }
397 
403  public function ‪siteLanguageMatchesCondition(): void
404  {
405  $site = new Site('angelo', 13, [
406  'languages' => [
407  [
408  'languageId' => 0,
409  'title' => 'United States',
410  'locale' => 'en_US.UTF-8',
411  ],
412  [
413  'languageId' => 2,
414  'title' => 'UK',
415  'locale' => 'en_UK.UTF-8',
416  ]
417  ]
418  ]);
419  ‪$GLOBALS['TYPO3_REQUEST'] = ‪$GLOBALS['TYPO3_REQUEST']->withAttribute('language', $site->getLanguageById(0));
421  $this->assertTrue($this->subject->match('[siteLanguage("locale") == "en_US.UTF-8"]'));
422  $this->assertTrue($this->subject->match('[siteLanguage("locale") in ["de_DE", "en_US.UTF-8"]]'));
423  }
424 
430  public function ‪siteLanguageDoesNotMatchCondition(): void
431  {
432  $site = new Site('angelo', 13, [
433  'languages' => [
434  [
435  'languageId' => 0,
436  'title' => 'United States',
437  'locale' => 'en_US.UTF-8',
438  ],
439  [
440  'languageId' => 2,
441  'title' => 'UK',
442  'locale' => 'en_UK.UTF-8',
443  ]
444  ]
445  ]);
446  ‪$GLOBALS['TYPO3_REQUEST'] = ‪$GLOBALS['TYPO3_REQUEST']->withAttribute('language', $site->getLanguageById(0));
448  $this->assertFalse($this->subject->match('[siteLanguage("locale") == "en_UK.UTF-8"]'));
449  $this->assertFalse($this->subject->match('[siteLanguage("locale") == "de_DE" && siteLanguage("title") == "UK"]'));
450  }
451 
457  public function ‪siteMatchesCondition(): void
458  {
459  $site = new Site('angelo', 13, ['languages' => [], 'base' => 'https://typo3.org/']);
460  ‪$GLOBALS['TYPO3_REQUEST'] = ‪$GLOBALS['TYPO3_REQUEST']->withAttribute('site', $site);
462  $this->assertTrue($this->subject->match('[site("identifier") == "angelo"]'));
463  $this->assertTrue($this->subject->match('[site("rootPageId") == 13]'));
464  $this->assertTrue($this->subject->match('[site("base") == "https://typo3.org/"]'));
465  }
466 
472  public function ‪siteDoesNotMatchCondition(): void
473  {
474  $site = new Site('angelo', 13, [
475  'languages' => [
476  [
477  'languageId' => 0,
478  'title' => 'United States',
479  'locale' => 'en_US.UTF-8',
480  ],
481  [
482  'languageId' => 2,
483  'title' => 'UK',
484  'locale' => 'en_UK.UTF-8',
485  ]
486  ]
487  ]);
488  ‪$GLOBALS['TYPO3_REQUEST'] = ‪$GLOBALS['TYPO3_REQUEST']->withAttribute('site', $site);
490  $this->assertFalse($this->subject->match('[site("identifier") == "berta"]'));
491  $this->assertFalse($this->subject->match('[site("rootPageId") == 14 && site("rootPageId") == 23]'));
492  }
493 }
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\compatVersionConditionDoesNotMatchNewerRelease
‪compatVersionConditionDoesNotMatchNewerRelease()
Definition: ConditionMatcherTest.php:344
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionMatchesAnyLoggedInUser
‪loginUserConditionMatchesAnyLoggedInUser()
Definition: ConditionMatcherTest.php:153
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\genericGetVariablesSucceedsWithNamespaceSession
‪genericGetVariablesSucceedsWithNamespaceSession()
Definition: ConditionMatcherTest.php:372
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\$testGlobalNamespace
‪string $testGlobalNamespace
Definition: ConditionMatcherTest.php:45
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\getFreshConditionMatcher
‪getFreshConditionMatcher()
Definition: ConditionMatcherTest.php:90
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\match
‪bool match($expression)
Definition: AbstractConditionMatcher.php:226
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\genericGetVariablesSucceedsWithNamespaceTSFE
‪genericGetVariablesSucceedsWithNamespaceTSFE()
Definition: ConditionMatcherTest.php:356
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\treeLevelConditionDoesNotMatchFaultyValue
‪treeLevelConditionDoesNotMatchFaultyValue()
Definition: ConditionMatcherTest.php:248
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\$subject
‪ConditionMatcher $subject
Definition: ConditionMatcherTest.php:41
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline
‪PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline()
Definition: ConditionMatcherTest.php:272
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\treeLevelConditionMatchesSingleValue
‪treeLevelConditionMatchesSingleValue()
Definition: ConditionMatcherTest.php:228
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:21
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:39
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\siteLanguageDoesNotMatchCondition
‪siteLanguageDoesNotMatchCondition()
Definition: ConditionMatcherTest.php:427
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\siteDoesNotMatchCondition
‪siteDoesNotMatchCondition()
Definition: ConditionMatcherTest.php:469
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest
Definition: ConditionMatcherTest.php:38
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\usergroupConditionDoesNotMatchDefaulUserGroupIds
‪usergroupConditionDoesNotMatchDefaulUserGroupIds()
Definition: ConditionMatcherTest.php:137
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionMatchesMultipleLoggedInUsers
‪loginUserConditionMatchesMultipleLoggedInUsers()
Definition: ConditionMatcherTest.php:178
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\usergroupConditionMatchesMultipleUserGroupId
‪usergroupConditionMatchesMultipleUserGroupId()
Definition: ConditionMatcherTest.php:120
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching
Definition: ConditionMatcherTest.php:3
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\setUp
‪setUp()
Definition: ConditionMatcherTest.php:51
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\compatVersionConditionMatchesOlderRelease
‪compatVersionConditionMatchesOlderRelease()
Definition: ConditionMatcherTest.php:320
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:35
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionMatchesSingleLoggedInUser
‪loginUserConditionMatchesSingleLoggedInUser()
Definition: ConditionMatcherTest.php:165
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\usergroupConditionMatchesSingleGroupId
‪usergroupConditionMatchesSingleGroupId()
Definition: ConditionMatcherTest.php:103
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\siteMatchesCondition
‪siteMatchesCondition()
Definition: ConditionMatcherTest.php:454
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDinRootlineConditionDoesNotMatchPageIdNotInRootline
‪PIDinRootlineConditionDoesNotMatchPageIdNotInRootline()
Definition: ConditionMatcherTest.php:308
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\compatVersionConditionMatchesSameRelease
‪compatVersionConditionMatchesSameRelease()
Definition: ConditionMatcherTest.php:333
‪TYPO3\CMS\Frontend\Configuration\TypoScript\ConditionMatching\ConditionMatcher
Definition: ConditionMatcher.php:33
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDinRootlineConditionMatchesLastPageIdInRootline
‪PIDinRootlineConditionMatchesLastPageIdInRootline()
Definition: ConditionMatcherTest.php:296
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:23
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDupinRootlineConditionMatchesSinglePageIdInRootline
‪PIDupinRootlineConditionMatchesSinglePageIdInRootline()
Definition: ConditionMatcherTest.php:258
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDinRootlineConditionMatchesSinglePageIdInRootline
‪PIDinRootlineConditionMatchesSinglePageIdInRootline()
Definition: ConditionMatcherTest.php:284
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionMatchIfUserIsNotLoggedIn
‪loginUserConditionMatchIfUserIsNotLoggedIn()
Definition: ConditionMatcherTest.php:211
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ConditionMatcherTest.php:49
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\siteLanguageMatchesCondition
‪siteLanguageMatchesCondition()
Definition: ConditionMatcherTest.php:400
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\treeLevelConditionMatchesMultipleValues
‪treeLevelConditionMatchesMultipleValues()
Definition: ConditionMatcherTest.php:238
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionDoesNotMatchIfNotUserIsLoggedId
‪loginUserConditionDoesNotMatchIfNotUserIsLoggedId()
Definition: ConditionMatcherTest.php:190
‪TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\genericGetVariablesSucceedsWithNamespaceENV
‪genericGetVariablesSucceedsWithNamespaceENV()
Definition: ConditionMatcherTest.php:387
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:36