[TASK] Shorten the extend class in all unit tests
[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 Prophecy\Argument;
18 use TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Core\Cache\CacheManager;
21 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
22 use TYPO3\CMS\Core\Context\Context;
23 use TYPO3\CMS\Core\Context\UserAspect;
24 use TYPO3\CMS\Core\Http\ServerRequest;
25 use TYPO3\CMS\Core\Log\Logger;
26 use TYPO3\CMS\Core\Package\PackageInterface;
27 use TYPO3\CMS\Core\Package\PackageManager;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30
31 /**
32 * Testcase for class \TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher.
33 */
34 class ConditionMatcherTest extends UnitTestCase
35 {
36 /**
37 * @var array
38 */
39 protected $rootline;
40
41 /**
42 * @var \TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher
43 */
44 protected $matchCondition;
45
46 /**
47 * @var string
48 */
49 protected $testGlobalNamespace;
50
51 /**
52 * @var string
53 */
54 protected $testTableName;
55
56 /**
57 * @var bool Reset singletons
58 */
59 protected $resetSingletonInstances = true;
60
61 /**
62 * Set up tests
63 */
64 protected function setUp()
65 {
66 $GLOBALS['TYPO3_REQUEST'] = new ServerRequest();
67 $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
68 $cacheFrontendProphecy->has(Argument::any())->willReturn(false);
69 $cacheFrontendProphecy->set(Argument::any(), Argument::any())->willReturn(null);
70 $cacheManagerProphecy = $this->prophesize(CacheManager::class);
71 $cacheManagerProphecy->getCache('cache_core')->willReturn($cacheFrontendProphecy->reveal());
72 GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
73
74 $packageManagerProphecy = $this->prophesize(PackageManager::class);
75 $corePackageProphecy = $this->prophesize(PackageInterface::class);
76 $corePackageProphecy->getPackagePath()->willReturn(__DIR__ . '/../../../../../../../sysext/core/');
77 $packageManagerProphecy->getActivePackages()->willReturn([
78 $corePackageProphecy->reveal()
79 ]);
80 GeneralUtility::setSingletonInstance(PackageManager::class, $packageManagerProphecy->reveal());
81
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 usergroup comparison matches.
140 *
141 * @test
142 */
143 public function usergroupConditionMatchesSingleGroupId()
144 {
145 $this->assertTrue($this->matchCondition->match('[usergroup(13)]'));
146 $this->assertTrue($this->matchCondition->match('[usergroup("13")]'));
147 $this->assertTrue($this->matchCondition->match('[usergroup(\'13\')]'));
148 }
149
150 /**
151 * Tests whether usergroup comparison matches.
152 *
153 * @test
154 */
155 public function usergroupConditionMatchesMultipleUserGroupId()
156 {
157 $this->assertTrue($this->matchCondition->match('[usergroup("999,15,14,13")]'));
158 $this->assertTrue($this->matchCondition->match('[usergroup(\'999,15,14,13\')]'));
159 }
160
161 /**
162 * Tests whether user comparison matches.
163 *
164 * @test
165 */
166 public function loginUserConditionMatchesAnyLoggedInUser()
167 {
168 $this->assertTrue($this->matchCondition->match('[loginUser("*")]'));
169 $this->assertTrue($this->matchCondition->match('[loginUser(\'*\')]'));
170 }
171
172 /**
173 * Tests whether user comparison matches.
174 *
175 * @test
176 */
177 public function loginUserConditionMatchesSingleLoggedInUser()
178 {
179 $this->assertTrue($this->matchCondition->match('[loginUser(13)]'));
180 $this->assertTrue($this->matchCondition->match('[loginUser("13")]'));
181 $this->assertTrue($this->matchCondition->match('[loginUser(\'13\')]'));
182 }
183
184 /**
185 * Tests whether user comparison matches.
186 *
187 * @test
188 */
189 public function loginUserConditionDoesNotMatchSingleLoggedInUser()
190 {
191 $GLOBALS['BE_USER']->user['uid'] = 13;
192 $this->assertFalse($this->matchCondition->match('[loginUser(999)]'));
193 $this->assertFalse($this->matchCondition->match('[loginUser("999")]'));
194 $this->assertFalse($this->matchCondition->match('[loginUser(\'999\')]'));
195 }
196
197 /**
198 * Tests whether user comparison matches.
199 *
200 * @test
201 */
202 public function loginUserConditionMatchesMultipleLoggedInUsers()
203 {
204 $this->assertTrue($this->matchCondition->match('[loginUser("999,13")]'));
205 $this->assertTrue($this->matchCondition->match('[loginUser(\'999,13\')]'));
206 }
207
208 /**
209 * Tests whether checkinf for an admin user matches
210 *
211 * @test
212 */
213 public function adminUserConditionMatchesAdminUser()
214 {
215 $this->assertTrue($this->matchCondition->match('[backend.user.isAdmin == true]'));
216 $this->assertTrue($this->matchCondition->match('[backend.user.isAdmin != false]'));
217 $this->assertTrue($this->matchCondition->match('[backend.user.isAdmin]'));
218 }
219
220 /**
221 * Tests whether treeLevel comparison matches.
222 *
223 * @test
224 */
225 public function treeLevelConditionMatchesSingleValue()
226 {
227 $this->matchCondition->setRootline($this->rootline);
228 $this->assertTrue($this->matchCondition->match('[tree.level == 2]'));
229 }
230
231 /**
232 * Tests whether treeLevel comparison matches.
233 *
234 * @test
235 */
236 public function treeLevelConditionMatchesMultipleValues()
237 {
238 $this->matchCondition->setRootline($this->rootline);
239 $this->assertTrue($this->matchCondition->match('[tree.level in [999,998,2]]'));
240 }
241
242 /**
243 * Tests whether treeLevel comparison matches.
244 *
245 * @test
246 */
247 public function treeLevelConditionDoesNotMatchFaultyValue()
248 {
249 $this->matchCondition->setRootline($this->rootline);
250 $this->assertFalse($this->matchCondition->match('[tree.level == 999]'));
251 }
252
253 /**
254 * Tests whether a page Id is found in the previous rootline entries.
255 *
256 * @test
257 */
258 public function PIDupinRootlineConditionMatchesSinglePageIdInRootline()
259 {
260 $this->matchCondition->setRootline($this->rootline);
261 $this->matchCondition->setPageId(121);
262 $this->assertTrue($this->matchCondition->match('[111 in tree.rootLineIds]'));
263 $this->assertTrue($this->matchCondition->match('["111" in tree.rootLineIds]'));
264 $this->assertTrue($this->matchCondition->match('[\'111\' in tree.rootLineIds]'));
265 }
266
267 /**
268 * Tests whether a page Id is found in the previous rootline entries.
269 *
270 * @test
271 */
272 public function PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline()
273 {
274 $this->matchCondition->setRootline($this->rootline);
275 $this->matchCondition->setPageId(121);
276 $this->assertFalse($this->matchCondition->match('[999 in tree.rootLineIds]'));
277 }
278
279 /**
280 * Tests whether a page Id is found in all rootline entries.
281 *
282 * @test
283 */
284 public function PIDinRootlineConditionMatchesSinglePageIdInRootline()
285 {
286 $this->matchCondition->setRootline($this->rootline);
287 $this->matchCondition->setPageId(121);
288 $this->assertTrue($this->matchCondition->match('[111 in tree.rootLineIds]'));
289 }
290
291 /**
292 * Tests whether a page Id is found in all rootline entries.
293 *
294 * @test
295 */
296 public function PIDinRootlineConditionMatchesLastPageIdInRootline()
297 {
298 $this->matchCondition->setRootline($this->rootline);
299 $this->matchCondition->setPageId(121);
300 $this->assertTrue($this->matchCondition->match('[121 in tree.rootLineIds]'));
301 }
302
303 /**
304 * Tests whether a page Id is found in all rootline entries.
305 *
306 * @test
307 */
308 public function PIDinRootlineConditionDoesNotMatchPageIdNotInRootline()
309 {
310 $this->matchCondition->setRootline($this->rootline);
311 $this->matchCondition->setPageId(121);
312 $this->assertFalse($this->matchCondition->match('[999 in tree.rootLineIds]'));
313 }
314
315 /**
316 * Tests whether the compatibility version can be evaluated.
317 * (e.g. 7.9 is compatible to 7.0 but not to 15.0)
318 *
319 * @test
320 */
321 public function compatVersionConditionMatchesOlderRelease()
322 {
323 $this->assertTrue($this->matchCondition->match('[compatVersion(7.0)]'));
324 $this->assertTrue($this->matchCondition->match('[compatVersion("7.0")]'));
325 $this->assertTrue($this->matchCondition->match('[compatVersion(\'7.0\')]'));
326 }
327
328 /**
329 * Tests whether the compatibility version can be evaluated.
330 * (e.g. 7.9 is compatible to 7.0 but not to 15.0)
331 *
332 * @test
333 */
334 public function compatVersionConditionMatchesSameRelease()
335 {
336 $this->assertTrue($this->matchCondition->match('[compatVersion(' . TYPO3_branch . ')]'));
337 $this->assertTrue($this->matchCondition->match('[compatVersion("' . TYPO3_branch . '")]'));
338 $this->assertTrue($this->matchCondition->match('[compatVersion(\'' . TYPO3_branch . '\')]'));
339 }
340
341 /**
342 * Tests whether the compatibility version can be evaluated.
343 * (e.g. 7.9 is compatible to 7.0 but not to 15.0)
344 *
345 * @test
346 */
347 public function compatVersionConditionDoesNotMatchNewerRelease()
348 {
349 $this->assertFalse($this->matchCondition->match('[compatVersion(15.0)]'));
350 $this->assertFalse($this->matchCondition->match('[compatVersion("15.0")]'));
351 $this->assertFalse($this->matchCondition->match('[compatVersion(\'15.0\')]'));
352 }
353
354 /**
355 * Tests whether the generic fetching of variables works with the namespace 'ENV'.
356 *
357 * @test
358 */
359 public function genericGetVariablesSucceedsWithNamespaceENV()
360 {
361 $testKey = $this->getUniqueId('test');
362 putenv($testKey . '=testValue');
363 $this->assertTrue($this->matchCondition->match('[getenv("' . $testKey . '") == "testValue"]'));
364 }
365 }