‪TYPO3CMS  10.4
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 
28 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 
33 class ‪ConditionMatcherTest extends FunctionalTestCase
34 {
38  protected function ‪setUp(): void
39  {
40  parent::setUp();
41 
42  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
43 
44  $backendUser = new ‪BackendUserAuthentication();
45  $backendUser->user['uid'] = 13;
46  $backendUser->user['admin'] = true;
47  $backendUser->groupList = '13,14,15';
48  GeneralUtility::makeInstance(Context::class)->setAspect('backend.user', new ‪UserAspect($backendUser));
49  }
50 
57  {
58  $subject = $this->‪getConditionMatcher();
59  self::assertTrue($subject->match('[usergroup(13)]'));
60  self::assertTrue($subject->match('[usergroup("13")]'));
61  self::assertTrue($subject->match('[usergroup(\'13\')]'));
62  }
63 
70  {
71  $subject = $this->‪getConditionMatcher();
72  self::assertTrue($subject->match('[usergroup("999,15,14,13")]'));
73  self::assertTrue($subject->match('[usergroup(\'999,15,14,13\')]'));
74  }
75 
82  {
83  $subject = $this->‪getConditionMatcher();
84  self::assertTrue($subject->match('[loginUser("*")]'));
85  self::assertTrue($subject->match('[loginUser(\'*\')]'));
86  }
87 
94  {
95  $subject = $this->‪getConditionMatcher();
96  self::assertTrue($subject->match('[loginUser(13)]'));
97  self::assertTrue($subject->match('[loginUser("13")]'));
98  self::assertTrue($subject->match('[loginUser(\'13\')]'));
99  }
100 
107  {
108  ‪$GLOBALS['BE_USER']->user['uid'] = 13;
109  $subject = $this->‪getConditionMatcher();
110  self::assertFalse($subject->match('[loginUser(999)]'));
111  self::assertFalse($subject->match('[loginUser("999")]'));
112  self::assertFalse($subject->match('[loginUser(\'999\')]'));
113  }
114 
121  {
122  $subject = $this->‪getConditionMatcher();
123  self::assertTrue($subject->match('[loginUser("999,13")]'));
124  self::assertTrue($subject->match('[loginUser(\'999,13\')]'));
125  }
126 
133  {
134  $subject = $this->‪getConditionMatcher();
135  self::assertTrue($subject->match('[backend.user.isAdmin == true]'));
136  self::assertTrue($subject->match('[backend.user.isAdmin != false]'));
137  self::assertTrue($subject->match('[backend.user.isAdmin]'));
138  }
139 
146  {
147  $this->‪setUpWorkspaceAspect(0);
148  $subject = $this->‪getConditionMatcher();
149  self::assertTrue($subject->match('[workspace.workspaceId === 0]'));
150  self::assertTrue($subject->match('[workspace.workspaceId == 0]'));
151  self::assertTrue($subject->match('[workspace.workspaceId == "0"]'));
152  self::assertTrue($subject->match('[workspace.workspaceId == \'0\']'));
153  }
154 
161  {
162  $this->‪setUpWorkspaceAspect(1);
163  $subject = $this->‪getConditionMatcher();
164  self::assertFalse($subject->match('[workspace.isLive]'));
165  self::assertFalse($subject->match('[workspace.isLive === true]'));
166  self::assertFalse($subject->match('[workspace.isLive == true]'));
167  self::assertFalse($subject->match('[workspace.isLive !== false]'));
168  self::assertFalse($subject->match('[workspace.isLive != false]'));
169  }
170 
177  {
178  $this->‪setUpWorkspaceAspect(1);
179  $subject = $this->‪getConditionMatcher();
180  self::assertTrue($subject->match('[workspace.isOffline]'));
181  self::assertTrue($subject->match('[workspace.isOffline === true]'));
182  self::assertTrue($subject->match('[workspace.isOffline == true]'));
183  self::assertTrue($subject->match('[workspace.isOffline !== false]'));
184  self::assertTrue($subject->match('[workspace.isOffline != false]'));
185  }
186 
193  {
194  $subject = $this->‪getConditionMatcher(2);
195  self::assertTrue($subject->match('[tree.level == 2]'));
196  }
197 
204  {
205  $subject = $this->‪getConditionMatcher(2);
206  self::assertTrue($subject->match('[tree.level in [999,998,2]]'));
207  }
208 
215  {
216  $subject = $this->‪getConditionMatcher();
217  self::assertFalse($subject->match('[tree.level == 999]'));
218  }
219 
226  {
227  $subject = $this->‪getConditionMatcher(3);
228  self::assertTrue($subject->match('[2 in tree.rootLineParentIds]'));
229  self::assertTrue($subject->match('["2" in tree.rootLineParentIds]'));
230  self::assertTrue($subject->match('[\'2\' in tree.rootLineParentIds]'));
231  }
232 
239  {
240  $subject = $this->‪getConditionMatcher(3);
241  self::assertFalse($subject->match('[3 in tree.rootLineParentIds]'));
242  }
243 
250  {
251  $subject = $this->‪getConditionMatcher(3);
252  self::assertFalse($subject->match('[999 in tree.rootLineParentIds]'));
253  }
254 
261  {
262  $subject = $this->‪getConditionMatcher(3);
263  self::assertTrue($subject->match('[2 in tree.rootLineIds]'));
264  }
265 
272  {
273  $subject = $this->‪getConditionMatcher(3);
274  self::assertTrue($subject->match('[3 in tree.rootLineIds]'));
275  }
276 
283  {
284  $subject = $this->‪getConditionMatcher(3);
285  self::assertFalse($subject->match('[999 in tree.rootLineIds]'));
286  }
287 
295  {
296  $subject = $this->‪getConditionMatcher();
297  self::assertTrue($subject->match('[compatVersion(7.0)]'));
298  self::assertTrue($subject->match('[compatVersion("7.0")]'));
299  self::assertTrue($subject->match('[compatVersion(\'7.0\')]'));
300  }
301 
309  {
310  $subject = $this->‪getConditionMatcher();
311  self::assertTrue($subject->match('[compatVersion(' . TYPO3_branch . ')]'));
312  self::assertTrue($subject->match('[compatVersion("' . TYPO3_branch . '")]'));
313  self::assertTrue($subject->match('[compatVersion(\'' . TYPO3_branch . '\')]'));
314  }
315 
322  public function compatVersionConditionDoesNotMatchNewerRelease(): void
323  {
324  $subject = $this->getConditionMatcher();
325  self::assertFalse($subject->match('[compatVersion(15.0)]'));
326  self::assertFalse($subject->match('[compatVersion("15.0")]'));
327  self::assertFalse($subject->match('[compatVersion(\'15.0\')]'));
328  }
329 
336  {
337  $testKey = ‪StringUtility::getUniqueId('test');
338  putenv($testKey . '=testValue');
339  $subject = $this->‪getConditionMatcher();
340  self::assertTrue($subject->match('[getenv("' . $testKey . '") == "testValue"]'));
341  }
342 
347  {
348  $subject = $this->‪getConditionMatcher();
349  self::assertFalse($subject->match('[getTSFE().id == 1]'));
350  }
351 
353  {
354  return [
355  'Page ID from "id" parameter' => [
356  ['id' => 6],
357  6,
358  ],
359  'Page ID from "edit" parameter' => [
360  [
361  'edit' => [
362  'pages' => [
363  6 => 'edit',
364  ],
365  ],
366  ],
367  6,
368  ],
369  'Page ID from "new" parameter' => [
370  [
371  'edit' => [
372  'pages' => [
373  -6 => 'new',
374  ],
375  ],
376  ],
377  5,
378  ],
379  'Page ID from "copy" parameter' => [
380  [
381  'cmd' => [
382  'pages' => [
383  5 => [
384  'copy' => 6,
385  ],
386  ],
387  ],
388  ],
389  6,
390  ],
391  'Page ID from "move" target parameter' => [
392  [
393  'cmd' => [
394  'pages' => [
395  5 => [
396  'move' => [
397  'target' => 6,
398  ],
399  ],
400  ],
401  ],
402  ],
403  6,
404  ],
405  ];
406  }
407 
412  public function ‪determinePageIdFindIdFromQueryParameters($queryParameters, $resultPageId): void
413  {
414  $_GET = $queryParameters;
415  $subject = GeneralUtility::makeInstance(ConditionMatcher::class);
416  self::assertEquals($resultPageId, $subject->getPageId());
417  }
418 
423  protected function ‪getConditionMatcher(int $pageId = null): ‪ConditionMatcher
424  {
425  $conditionMatcher = new ‪ConditionMatcher(null, $pageId);
426  $conditionMatcher->setLogger($this->prophesize(Logger::class)->reveal());
427 
428  return $conditionMatcher;
429  }
430 
436  protected function ‪setUpWorkspaceAspect(int $workspaceId): void
437  {
438  GeneralUtility::makeInstance(Context::class)->setAspect('workspace', new ‪WorkspaceAspect($workspaceId));
439  }
440 }
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\genericGetVariablesSucceedsWithNamespaceENV
‪genericGetVariablesSucceedsWithNamespaceENV()
Definition: ConditionMatcherTest.php:335
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\treeLevelConditionMatchesMultipleValues
‪treeLevelConditionMatchesMultipleValues()
Definition: ConditionMatcherTest.php:203
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\usingTSFEInATestInBeContextIsAlwaysFalse
‪usingTSFEInATestInBeContextIsAlwaysFalse()
Definition: ConditionMatcherTest.php:346
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\treeLevelConditionMatchesSingleValue
‪treeLevelConditionMatchesSingleValue()
Definition: ConditionMatcherTest.php:192
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionMatchesMultipleLoggedInUsers
‪loginUserConditionMatchesMultipleLoggedInUsers()
Definition: ConditionMatcherTest.php:120
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher
Definition: ConditionMatcher.php:30
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionDoesNotMatchSingleLoggedInUser
‪loginUserConditionDoesNotMatchSingleLoggedInUser()
Definition: ConditionMatcherTest.php:106
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\adminUserConditionMatchesAdminUser
‪adminUserConditionMatchesAdminUser()
Definition: ConditionMatcherTest.php:132
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\workspaceIdConditionMatchesCurrentWorkspaceId
‪workspaceIdConditionMatchesCurrentWorkspaceId()
Definition: ConditionMatcherTest.php:145
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\compatVersionConditionMatchesSameRelease
‪compatVersionConditionMatchesSameRelease()
Definition: ConditionMatcherTest.php:308
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\workspaceIsLiveMatchesCorrectWorkspaceState
‪workspaceIsLiveMatchesCorrectWorkspaceState()
Definition: ConditionMatcherTest.php:160
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionMatchesAnyLoggedInUser
‪loginUserConditionMatchesAnyLoggedInUser()
Definition: ConditionMatcherTest.php:81
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\setUp
‪setUp()
Definition: ConditionMatcherTest.php:38
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\getConditionMatcher
‪ConditionMatcher getConditionMatcher(int $pageId=null)
Definition: ConditionMatcherTest.php:423
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\usergroupConditionMatchesSingleGroupId
‪usergroupConditionMatchesSingleGroupId()
Definition: ConditionMatcherTest.php:56
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDinRootlineConditionDoesNotMatchPageIdNotInRootline
‪PIDinRootlineConditionDoesNotMatchPageIdNotInRootline()
Definition: ConditionMatcherTest.php:282
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDupinRootlineConditionMatchesSinglePageIdInRootline
‪PIDupinRootlineConditionMatchesSinglePageIdInRootline()
Definition: ConditionMatcherTest.php:225
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDinRootlineConditionMatchesSinglePageIdInRootline
‪PIDinRootlineConditionMatchesSinglePageIdInRootline()
Definition: ConditionMatcherTest.php:260
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\workspaceIsOfflineMatchesCorrectWorkspaceState
‪workspaceIsOfflineMatchesCorrectWorkspaceState()
Definition: ConditionMatcherTest.php:176
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\determinePageIdFindIdFromQueryParameters
‪determinePageIdFindIdFromQueryParameters($queryParameters, $resultPageId)
Definition: ConditionMatcherTest.php:412
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\usergroupConditionMatchesMultipleUserGroupId
‪usergroupConditionMatchesMultipleUserGroupId()
Definition: ConditionMatcherTest.php:69
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDinRootlineConditionMatchesLastPageIdInRootline
‪PIDinRootlineConditionMatchesLastPageIdInRootline()
Definition: ConditionMatcherTest.php:271
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\determinePageIdFindIdFromQueryParametersDataProvider
‪determinePageIdFindIdFromQueryParametersDataProvider()
Definition: ConditionMatcherTest.php:352
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\loginUserConditionMatchesSingleLoggedInUser
‪loginUserConditionMatchesSingleLoggedInUser()
Definition: ConditionMatcherTest.php:93
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline
‪PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline()
Definition: ConditionMatcherTest.php:249
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\setUpWorkspaceAspect
‪setUpWorkspaceAspect(int $workspaceId)
Definition: ConditionMatcherTest.php:436
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\PIDupinRootlineConditionDoesNotMatchLastPageIdInRootline
‪PIDupinRootlineConditionDoesNotMatchLastPageIdInRootline()
Definition: ConditionMatcherTest.php:238
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\compatVersionConditionMatchesOlderRelease
‪compatVersionConditionMatchesOlderRelease()
Definition: ConditionMatcherTest.php:294
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest\treeLevelConditionDoesNotMatchFaultyValue
‪treeLevelConditionDoesNotMatchFaultyValue()
Definition: ConditionMatcherTest.php:214
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching
Definition: ConditionMatcherTest.php:18
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:38
‪TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching\ConditionMatcherTest
Definition: ConditionMatcherTest.php:34