ConditionMatcherTest.php 11.8 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
19
namespace TYPO3\CMS\Backend\Tests\Functional\Configuration\TypoScript\ConditionMatching;

20
21
22
23
use TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\UserAspect;
24
25
use TYPO3\CMS\Core\Context\WorkspaceAspect;
use TYPO3\CMS\Core\Log\Logger;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Core\Utility\StringUtility;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

/**
 * Functional test for the ConditionMatcher of EXT:backend
 */
class ConditionMatcherTest extends FunctionalTestCase
{
    /**
     * Sets up this test case.
     */
    protected function setUp(): void
    {
        parent::setUp();

        $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');

        $backendUser = new BackendUserAuthentication();
        $backendUser->user['uid'] = 13;
        $backendUser->user['admin'] = true;
47
        $backendUser->userGroupsUID = [13, 14, 15];
48
49
50
51
52
53
54
55
56
57
        GeneralUtility::makeInstance(Context::class)->setAspect('backend.user', new UserAspect($backendUser));
    }

    /**
     * Tests whether usergroup comparison matches.
     *
     * @test
     */
    public function usergroupConditionMatchesSingleGroupId(): void
    {
58
59
60
61
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[usergroup(13)]'));
        self::assertTrue($subject->match('[usergroup("13")]'));
        self::assertTrue($subject->match('[usergroup(\'13\')]'));
62
63
64
65
66
67
68
69
70
    }

    /**
     * Tests whether usergroup comparison matches.
     *
     * @test
     */
    public function usergroupConditionMatchesMultipleUserGroupId(): void
    {
71
72
73
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[usergroup("999,15,14,13")]'));
        self::assertTrue($subject->match('[usergroup(\'999,15,14,13\')]'));
74
75
76
77
78
79
80
81
82
    }

    /**
     * Tests whether user comparison matches.
     *
     * @test
     */
    public function loginUserConditionMatchesAnyLoggedInUser(): void
    {
83
84
85
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[loginUser("*")]'));
        self::assertTrue($subject->match('[loginUser(\'*\')]'));
86
87
88
89
90
91
92
93
94
    }

    /**
     * Tests whether user comparison matches.
     *
     * @test
     */
    public function loginUserConditionMatchesSingleLoggedInUser(): void
    {
95
96
97
98
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[loginUser(13)]'));
        self::assertTrue($subject->match('[loginUser("13")]'));
        self::assertTrue($subject->match('[loginUser(\'13\')]'));
99
100
101
102
103
104
105
106
107
    }

    /**
     * Tests whether user comparison matches.
     *
     * @test
     */
    public function loginUserConditionDoesNotMatchSingleLoggedInUser(): void
    {
108
109
110
111
        $subject = $this->getConditionMatcher();
        self::assertFalse($subject->match('[loginUser(999)]'));
        self::assertFalse($subject->match('[loginUser("999")]'));
        self::assertFalse($subject->match('[loginUser(\'999\')]'));
112
113
114
115
116
117
118
119
120
    }

    /**
     * Tests whether user comparison matches.
     *
     * @test
     */
    public function loginUserConditionMatchesMultipleLoggedInUsers(): void
    {
121
122
123
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[loginUser("999,13")]'));
        self::assertTrue($subject->match('[loginUser(\'999,13\')]'));
124
125
126
    }

    /**
127
     * Tests whether checking for an admin user matches
128
129
130
131
132
     *
     * @test
     */
    public function adminUserConditionMatchesAdminUser(): void
    {
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[backend.user.isAdmin == true]'));
        self::assertTrue($subject->match('[backend.user.isAdmin != false]'));
        self::assertTrue($subject->match('[backend.user.isAdmin]'));
    }

    /**
     * Tests whether checking for workspace id matches current workspace id
     *
     * @test
     */
    public function workspaceIdConditionMatchesCurrentWorkspaceId(): void
    {
        $this->setUpWorkspaceAspect(0);
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[workspace.workspaceId === 0]'));
        self::assertTrue($subject->match('[workspace.workspaceId == 0]'));
        self::assertTrue($subject->match('[workspace.workspaceId == "0"]'));
        self::assertTrue($subject->match('[workspace.workspaceId == \'0\']'));
    }

    /**
     * Tests whether checking if workspace is live matches
     *
     * @test
     */
    public function workspaceIsLiveMatchesCorrectWorkspaceState(): void
    {
        $this->setUpWorkspaceAspect(1);
        $subject = $this->getConditionMatcher();
        self::assertFalse($subject->match('[workspace.isLive]'));
        self::assertFalse($subject->match('[workspace.isLive === true]'));
        self::assertFalse($subject->match('[workspace.isLive == true]'));
        self::assertFalse($subject->match('[workspace.isLive !== false]'));
        self::assertFalse($subject->match('[workspace.isLive != false]'));
    }

    /**
     * Tests whether checking if workspace is offline matches
     *
     * @test
     */
    public function workspaceIsOfflineMatchesCorrectWorkspaceState(): void
    {
        $this->setUpWorkspaceAspect(1);
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[workspace.isOffline]'));
        self::assertTrue($subject->match('[workspace.isOffline === true]'));
        self::assertTrue($subject->match('[workspace.isOffline == true]'));
        self::assertTrue($subject->match('[workspace.isOffline !== false]'));
        self::assertTrue($subject->match('[workspace.isOffline != false]'));
184
185
186
187
188
189
190
191
192
    }

    /**
     * Tests whether treeLevel comparison matches.
     *
     * @test
     */
    public function treeLevelConditionMatchesSingleValue(): void
    {
193
194
        $subject = $this->getConditionMatcher(2);
        self::assertTrue($subject->match('[tree.level == 2]'));
195
196
197
198
199
200
201
202
203
    }

    /**
     * Tests whether treeLevel comparison matches.
     *
     * @test
     */
    public function treeLevelConditionMatchesMultipleValues(): void
    {
204
205
        $subject = $this->getConditionMatcher(2);
        self::assertTrue($subject->match('[tree.level in [999,998,2]]'));
206
207
208
209
210
211
212
213
214
    }

    /**
     * Tests whether treeLevel comparison matches.
     *
     * @test
     */
    public function treeLevelConditionDoesNotMatchFaultyValue(): void
    {
215
216
        $subject = $this->getConditionMatcher();
        self::assertFalse($subject->match('[tree.level == 999]'));
217
218
219
220
221
222
223
224
225
    }

    /**
     * Tests whether a page Id is found in the previous rootline entries.
     *
     * @test
     */
    public function PIDupinRootlineConditionMatchesSinglePageIdInRootline(): void
    {
226
        $subject = $this->getConditionMatcher(3);
227
228
229
230
231
232
233
234
235
236
237
238
        self::assertTrue($subject->match('[2 in tree.rootLineParentIds]'));
        self::assertTrue($subject->match('["2" in tree.rootLineParentIds]'));
        self::assertTrue($subject->match('[\'2\' in tree.rootLineParentIds]'));
    }

    /**
     * Tests whether a page id is not found in the previous rootline entries.
     *
     * @test
     */
    public function PIDupinRootlineConditionDoesNotMatchLastPageIdInRootline(): void
    {
239
        $subject = $this->getConditionMatcher(3);
240
        self::assertFalse($subject->match('[3 in tree.rootLineParentIds]'));
241
242
243
244
245
246
247
248
249
    }

    /**
     * Tests whether a page Id is found in the previous rootline entries.
     *
     * @test
     */
    public function PIDupinRootlineConditionDoesNotMatchPageIdNotInRootline(): void
    {
250
        $subject = $this->getConditionMatcher(3);
251
        self::assertFalse($subject->match('[999 in tree.rootLineParentIds]'));
252
253
254
255
256
257
258
259
260
    }

    /**
     * Tests whether a page Id is found in all rootline entries.
     *
     * @test
     */
    public function PIDinRootlineConditionMatchesSinglePageIdInRootline(): void
    {
261
        $subject = $this->getConditionMatcher(3);
262
        self::assertTrue($subject->match('[2 in tree.rootLineIds]'));
263
264
265
266
267
268
269
270
271
    }

    /**
     * Tests whether a page Id is found in all rootline entries.
     *
     * @test
     */
    public function PIDinRootlineConditionMatchesLastPageIdInRootline(): void
    {
272
        $subject = $this->getConditionMatcher(3);
273
        self::assertTrue($subject->match('[3 in tree.rootLineIds]'));
274
275
276
277
278
279
280
281
282
    }

    /**
     * Tests whether a page Id is found in all rootline entries.
     *
     * @test
     */
    public function PIDinRootlineConditionDoesNotMatchPageIdNotInRootline(): void
    {
283
        $subject = $this->getConditionMatcher(3);
284
        self::assertFalse($subject->match('[999 in tree.rootLineIds]'));
285
286
287
288
289
290
291
292
293
294
    }

    /**
     * Tests whether the compatibility version can be evaluated.
     * (e.g. 7.9 is compatible to 7.0 but not to 15.0)
     *
     * @test
     */
    public function compatVersionConditionMatchesOlderRelease(): void
    {
295
296
297
298
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[compatVersion(7.0)]'));
        self::assertTrue($subject->match('[compatVersion("7.0")]'));
        self::assertTrue($subject->match('[compatVersion(\'7.0\')]'));
299
300
301
302
303
304
305
306
307
308
    }

    /**
     * Tests whether the compatibility version can be evaluated.
     * (e.g. 7.9 is compatible to 7.0 but not to 15.0)
     *
     * @test
     */
    public function compatVersionConditionMatchesSameRelease(): void
    {
309
310
311
312
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[compatVersion(' . TYPO3_branch . ')]'));
        self::assertTrue($subject->match('[compatVersion("' . TYPO3_branch . '")]'));
        self::assertTrue($subject->match('[compatVersion(\'' . TYPO3_branch . '\')]'));
313
314
315
316
317
318
319
320
321
322
    }

    /**
     * Tests whether the compatibility version can be evaluated.
     * (e.g. 7.9 is compatible to 7.0 but not to 15.0)
     *
     * @test
     */
    public function compatVersionConditionDoesNotMatchNewerRelease(): void
    {
323
324
325
326
        $subject = $this->getConditionMatcher();
        self::assertFalse($subject->match('[compatVersion(15.0)]'));
        self::assertFalse($subject->match('[compatVersion("15.0")]'));
        self::assertFalse($subject->match('[compatVersion(\'15.0\')]'));
327
328
329
330
331
332
333
334
335
    }

    /**
     * Tests whether the generic fetching of variables works with the namespace 'ENV'.
     *
     * @test
     */
    public function genericGetVariablesSucceedsWithNamespaceENV(): void
    {
336
        $testKey = StringUtility::getUniqueId('test');
337
        putenv($testKey . '=testValue');
338
339
        $subject = $this->getConditionMatcher();
        self::assertTrue($subject->match('[getenv("' . $testKey . '") == "testValue"]'));
340
    }
341
342
343
344
345
346

    /**
     * @test
     */
    public function usingTSFEInATestInBeContextIsAlwaysFalse(): void
    {
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
        $subject = $this->getConditionMatcher();
        self::assertFalse($subject->match('[getTSFE().id == 1]'));
    }

    /**
     * @param int|null $pageId
     * @return ConditionMatcher
     */
    protected function getConditionMatcher(int $pageId = null): ConditionMatcher
    {
        $conditionMatcher = new ConditionMatcher(null, $pageId);
        $conditionMatcher->setLogger($this->prophesize(Logger::class)->reveal());

        return $conditionMatcher;
    }

    /**
     * Set up workspace aspect.
     *
     * @param int $workspaceId
     */
    protected function setUpWorkspaceAspect(int $workspaceId): void
    {
        GeneralUtility::makeInstance(Context::class)->setAspect('workspace', new WorkspaceAspect($workspaceId));
371
    }
372
}