‪TYPO3CMS  ‪main
ConditionMatcherTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
20 use Psr\Log\NullLogger;
29 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
30 
31 final class ‪ConditionMatcherTest extends FunctionalTestCase
32 {
33  protected function ‪setUp(): void
34  {
35  parent::setUp();
36 
37  $this->importCSVDataSet(__DIR__ . '/../../../Fixtures/pages.csv');
38 
39  $backendUser = new ‪BackendUserAuthentication();
40  $backendUser->user['uid'] = 13;
41  $backendUser->user['admin'] = true;
42  $backendUser->userGroupsUID = [13, 14, 15];
43  GeneralUtility::makeInstance(Context::class)->setAspect('backend.user', new ‪UserAspect($backendUser));
44  }
45 
52  {
53  $subject = $this->‪getConditionMatcher();
54  self::assertTrue($subject->match('[usergroup(13)]'));
55  self::assertTrue($subject->match('[usergroup("13")]'));
56  self::assertTrue($subject->match('[usergroup(\'13\')]'));
57  }
58 
65  {
66  $subject = $this->‪getConditionMatcher();
67  self::assertTrue($subject->match('[usergroup("999,15,14,13")]'));
68  self::assertTrue($subject->match('[usergroup(\'999,15,14,13\')]'));
69  }
70 
77  {
78  $subject = $this->‪getConditionMatcher();
79  self::assertTrue($subject->match('[14 in backend.user.userGroupIds]'));
80  }
81 
88  {
89  $subject = $this->‪getConditionMatcher();
90  self::assertTrue($subject->match('[loginUser("*")]'));
91  self::assertTrue($subject->match('[loginUser(\'*\')]'));
92  }
93 
100  {
101  $subject = $this->‪getConditionMatcher();
102  self::assertTrue($subject->match('[loginUser(13)]'));
103  self::assertTrue($subject->match('[loginUser("13")]'));
104  self::assertTrue($subject->match('[loginUser(\'13\')]'));
105  }
106 
113  {
114  $subject = $this->‪getConditionMatcher();
115  self::assertFalse($subject->match('[loginUser(999)]'));
116  self::assertFalse($subject->match('[loginUser("999")]'));
117  self::assertFalse($subject->match('[loginUser(\'999\')]'));
118  }
119 
126  {
127  $subject = $this->‪getConditionMatcher();
128  self::assertTrue($subject->match('[loginUser("999,13")]'));
129  self::assertTrue($subject->match('[loginUser(\'999,13\')]'));
130  }
131 
138  {
139  $subject = $this->‪getConditionMatcher();
140  self::assertTrue($subject->match('[backend.user.isAdmin == true]'));
141  self::assertTrue($subject->match('[backend.user.isAdmin != false]'));
142  self::assertTrue($subject->match('[backend.user.isAdmin]'));
143  }
144 
151  {
152  $this->‪setUpWorkspaceAspect(0);
153  $subject = $this->‪getConditionMatcher();
154  self::assertTrue($subject->match('[workspace.workspaceId === 0]'));
155  self::assertTrue($subject->match('[workspace.workspaceId == 0]'));
156  self::assertTrue($subject->match('[workspace.workspaceId == "0"]'));
157  self::assertTrue($subject->match('[workspace.workspaceId == \'0\']'));
158  }
159 
166  {
167  $this->‪setUpWorkspaceAspect(1);
168  $subject = $this->‪getConditionMatcher();
169  self::assertFalse($subject->match('[workspace.isLive]'));
170  self::assertFalse($subject->match('[workspace.isLive === true]'));
171  self::assertFalse($subject->match('[workspace.isLive == true]'));
172  self::assertFalse($subject->match('[workspace.isLive !== false]'));
173  self::assertFalse($subject->match('[workspace.isLive != false]'));
174  }
175 
182  {
183  $this->‪setUpWorkspaceAspect(1);
184  $subject = $this->‪getConditionMatcher();
185  self::assertTrue($subject->match('[workspace.isOffline]'));
186  self::assertTrue($subject->match('[workspace.isOffline === true]'));
187  self::assertTrue($subject->match('[workspace.isOffline == true]'));
188  self::assertTrue($subject->match('[workspace.isOffline !== false]'));
189  self::assertTrue($subject->match('[workspace.isOffline != false]'));
190  }
191 
198  {
199  $subject = $this->‪getConditionMatcher(2);
200  self::assertTrue($subject->match('[tree.level == 2]'));
201  }
202 
209  {
210  $subject = $this->‪getConditionMatcher(2);
211  self::assertTrue($subject->match('[tree.level in [999,998,2]]'));
212  }
213 
220  {
221  $subject = $this->‪getConditionMatcher();
222  self::assertFalse($subject->match('[tree.level == 999]'));
223  }
224 
231  {
232  $subject = $this->‪getConditionMatcher(3);
233  self::assertTrue($subject->match('[2 in tree.rootLineParentIds]'));
234  self::assertTrue($subject->match('["2" in tree.rootLineParentIds]'));
235  self::assertTrue($subject->match('[\'2\' in tree.rootLineParentIds]'));
236  }
237 
244  {
245  $subject = $this->‪getConditionMatcher(3);
246  self::assertFalse($subject->match('[3 in tree.rootLineParentIds]'));
247  }
248 
255  {
256  $subject = $this->‪getConditionMatcher(3);
257  self::assertFalse($subject->match('[999 in tree.rootLineParentIds]'));
258  }
259 
266  {
267  $subject = $this->‪getConditionMatcher(3);
268  self::assertTrue($subject->match('[2 in tree.rootLineIds]'));
269  }
270 
277  {
278  $subject = $this->‪getConditionMatcher(3);
279  self::assertTrue($subject->match('[3 in tree.rootLineIds]'));
280  }
281 
288  {
289  $subject = $this->‪getConditionMatcher(3);
290  self::assertFalse($subject->match('[999 in tree.rootLineIds]'));
291  }
292 
300  {
301  $subject = $this->‪getConditionMatcher();
302  self::assertTrue($subject->match('[compatVersion(7.0)]'));
303  self::assertTrue($subject->match('[compatVersion("7.0")]'));
304  self::assertTrue($subject->match('[compatVersion(\'7.0\')]'));
305  }
306 
314  {
315  $typo3Version = new ‪Typo3Version();
316  $subject = $this->‪getConditionMatcher();
317  self::assertTrue($subject->match('[compatVersion(' . $typo3Version->getBranch() . ')]'));
318  self::assertTrue($subject->match('[compatVersion("' . $typo3Version->getBranch() . '")]'));
319  self::assertTrue($subject->match('[compatVersion(\'' . $typo3Version->getBranch() . '\')]'));
320  }
321 
328  public function compatVersionConditionDoesNotMatchNewerRelease(): void
329  {
330  $subject = $this->getConditionMatcher();
331  self::assertFalse($subject->match('[compatVersion(15.0)]'));
332  self::assertFalse($subject->match('[compatVersion("15.0")]'));
333  self::assertFalse($subject->match('[compatVersion(\'15.0\')]'));
334  }
335 
342  {
343  $testKey = ‪StringUtility::getUniqueId('test');
344  putenv($testKey . '=testValue');
345  $subject = $this->‪getConditionMatcher();
346  self::assertTrue($subject->match('[getenv("' . $testKey . '") == "testValue"]'));
347  }
348 
353  {
354  $subject = $this->‪getConditionMatcher();
355  self::assertFalse($subject->match('[getTSFE().id == 1]'));
356  }
357 
359  {
360  return [
361  'Page ID from "id" parameter' => [
362  ['id' => 6],
363  6,
364  ],
365  'Page ID from "edit" parameter' => [
366  [
367  'edit' => [
368  'pages' => [
369  6 => 'edit',
370  ],
371  ],
372  ],
373  6,
374  ],
375  'Page ID from "new" parameter' => [
376  [
377  'edit' => [
378  'pages' => [
379  -6 => 'new',
380  ],
381  ],
382  ],
383  5,
384  ],
385  'Page ID from "copy" parameter' => [
386  [
387  'cmd' => [
388  'pages' => [
389  5 => [
390  'copy' => 6,
391  ],
392  ],
393  ],
394  ],
395  6,
396  ],
397  'Page ID from "move" target parameter' => [
398  [
399  'cmd' => [
400  'pages' => [
401  5 => [
402  'move' => [
403  'target' => 6,
404  ],
405  ],
406  ],
407  ],
408  ],
409  6,
410  ],
411  ];
412  }
413 
418  public function ‪determinePageIdFindIdFromQueryParameters($queryParameters, $resultPageId): void
419  {
420  $_GET = $queryParameters;
421  $subject = GeneralUtility::makeInstance(ConditionMatcher::class);
422  self::assertEquals($resultPageId, $subject->getPageId());
423  }
424 
428  protected function ‪getConditionMatcher(int $pageId = null): ‪ConditionMatcher
429  {
430  $conditionMatcher = new ‪ConditionMatcher(null, $pageId);
431  $conditionMatcher->setLogger(new NullLogger());
432 
433  return $conditionMatcher;
434  }
435 
439  protected function ‪setUpWorkspaceAspect(int $workspaceId): void
440  {
441  GeneralUtility::makeInstance(Context::class)->setAspect('workspace', new ‪WorkspaceAspect($workspaceId));
442  }
443 }
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionDoesNotMatchSingleLoggedInUser
‪loginUserConditionDoesNotMatchSingleLoggedInUser()
Definition: ConditionMatcherTest.php:112
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline
‪PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline()
Definition: ConditionMatcherTest.php:254
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\treeLevelConditionDoesNotMatchFaultyValue
‪treeLevelConditionDoesNotMatchFaultyValue()
Definition: ConditionMatcherTest.php:219
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDupinRootlineConditionDoesNotMatchLastPageIdInRootline
‪PIDupinRootlineConditionDoesNotMatchLastPageIdInRootline()
Definition: ConditionMatcherTest.php:243
‪TYPO3\CMS\Core\Information\Typo3Version
Definition: Typo3Version.php:21
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\genericGetVariablesSucceedsWithNamespaceENV
‪genericGetVariablesSucceedsWithNamespaceENV()
Definition: ConditionMatcherTest.php:341
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\setUp
‪setUp()
Definition: ConditionMatcherTest.php:33
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher
Definition: ConditionMatcher.php:34
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\getConditionMatcher
‪getConditionMatcher(int $pageId=null)
Definition: ConditionMatcherTest.php:428
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\setUpWorkspaceAspect
‪setUpWorkspaceAspect(int $workspaceId)
Definition: ConditionMatcherTest.php:439
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest
Definition: ConditionMatcherTest.php:32
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\treeLevelConditionMatchesSingleValue
‪treeLevelConditionMatchesSingleValue()
Definition: ConditionMatcherTest.php:197
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\usergroupConditionMatchesMultipleUserGroupId
‪usergroupConditionMatchesMultipleUserGroupId()
Definition: ConditionMatcherTest.php:64
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\treeLevelConditionMatchesMultipleValues
‪treeLevelConditionMatchesMultipleValues()
Definition: ConditionMatcherTest.php:208
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:55
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching
Definition: ConditionMatcherTest.php:18
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\workspaceIdConditionMatchesCurrentWorkspaceId
‪workspaceIdConditionMatchesCurrentWorkspaceId()
Definition: ConditionMatcherTest.php:150
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\determinePageIdFindIdFromQueryParametersDataProvider
‪static determinePageIdFindIdFromQueryParametersDataProvider()
Definition: ConditionMatcherTest.php:358
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDinRootlineConditionMatchesLastPageIdInRootline
‪PIDinRootlineConditionMatchesLastPageIdInRootline()
Definition: ConditionMatcherTest.php:276
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\workspaceIsOfflineMatchesCorrectWorkspaceState
‪workspaceIsOfflineMatchesCorrectWorkspaceState()
Definition: ConditionMatcherTest.php:181
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDupinRootlineConditionMatchesSinglePageIdInRootline
‪PIDupinRootlineConditionMatchesSinglePageIdInRootline()
Definition: ConditionMatcherTest.php:230
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionMatchesMultipleLoggedInUsers
‪loginUserConditionMatchesMultipleLoggedInUsers()
Definition: ConditionMatcherTest.php:125
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionMatchesSingleLoggedInUser
‪loginUserConditionMatchesSingleLoggedInUser()
Definition: ConditionMatcherTest.php:99
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDinRootlineConditionDoesNotMatchPageIdNotInRootline
‪PIDinRootlineConditionDoesNotMatchPageIdNotInRootline()
Definition: ConditionMatcherTest.php:287
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:66
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\usergroupConditionMatchesSingleGroupId
‪usergroupConditionMatchesSingleGroupId()
Definition: ConditionMatcherTest.php:51
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionMatchesAnyLoggedInUser
‪loginUserConditionMatchesAnyLoggedInUser()
Definition: ConditionMatcherTest.php:87
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\usingTSFEInATestInBeContextIsAlwaysFalse
‪usingTSFEInATestInBeContextIsAlwaysFalse()
Definition: ConditionMatcherTest.php:352
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\adminUserConditionMatchesAdminUser
‪adminUserConditionMatchesAdminUser()
Definition: ConditionMatcherTest.php:137
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\workspaceIsLiveMatchesCorrectWorkspaceState
‪workspaceIsLiveMatchesCorrectWorkspaceState()
Definition: ConditionMatcherTest.php:165
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\compatVersionConditionMatchesOlderRelease
‪compatVersionConditionMatchesOlderRelease()
Definition: ConditionMatcherTest.php:299
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\userGroupInOperatorConditionMatchesGroupId
‪userGroupInOperatorConditionMatchesGroupId()
Definition: ConditionMatcherTest.php:76
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\determinePageIdFindIdFromQueryParameters
‪determinePageIdFindIdFromQueryParameters($queryParameters, $resultPageId)
Definition: ConditionMatcherTest.php:418
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:37
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDinRootlineConditionMatchesSinglePageIdInRootline
‪PIDinRootlineConditionMatchesSinglePageIdInRootline()
Definition: ConditionMatcherTest.php:265
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:29
‪TYPO3\CMS\Backend\Tests\FunctionalDeprecated\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\compatVersionConditionMatchesSameRelease
‪compatVersionConditionMatchesSameRelease()
Definition: ConditionMatcherTest.php:313