‪TYPO3CMS  ‪main
FrontendRestrictionContainerTest.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 PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
29 
31 {
32  protected bool ‪$resetSingletonInstances = true;
33 
34  public static function ‪frontendStatesDataProvider(): array
35  {
36  return [
37  'Live, no preview' => [
38  'tableName' => 'aTable',
39  'tableAlias' => 'aTable',
40  'workspaceId' => 0,
41  'hiddenPagePreview' => false,
42  'hiddenRecordPreview' => false,
43  'frontendUserGroups' => [0, -1],
44  'localContextFrontendUserGroups' => null,
45  'expectedSQL' => '(("aTable"."deleted" = 0) AND ((("aTable"."t3ver_wsid" = 0) AND ((("aTable"."t3ver_oid" = 0) OR ("aTable"."t3ver_state" = 4))))) AND ("aTable"."myHiddenField" = 0) AND ("aTable"."myStartTimeField" <= 42) AND ((("aTable"."myEndTimeField" = 0) OR ("aTable"."myEndTimeField" > 42))) AND ((("aTable"."myGroupField" IS NULL) OR ("aTable"."myGroupField" = \'\') OR ("aTable"."myGroupField" = \'0\') OR (FIND_IN_SET(\'0\', "aTable"."myGroupField")) OR (FIND_IN_SET(\'-1\', "aTable"."myGroupField")))))',
46  ],
47  'Live, local context with differing user groups' => [
48  'tableName' => 'aTable',
49  'tableAlias' => 'aTable',
50  'workspaceId' => 0,
51  'hiddenPagePreview' => false,
52  'hiddenRecordPreview' => false,
53  'frontendUserGroups' => [0, -1],
54  'localContextFrontendUserGroups' => [3],
55  'expectedSQL' => '(("aTable"."deleted" = 0) AND ((("aTable"."t3ver_wsid" = 0) AND ((("aTable"."t3ver_oid" = 0) OR ("aTable"."t3ver_state" = 4))))) AND ("aTable"."myHiddenField" = 0) AND ("aTable"."myStartTimeField" <= 42) AND ((("aTable"."myEndTimeField" = 0) OR ("aTable"."myEndTimeField" > 42))) AND ((("aTable"."myGroupField" IS NULL) OR ("aTable"."myGroupField" = \'\') OR ("aTable"."myGroupField" = \'0\') OR (FIND_IN_SET(\'3\', "aTable"."myGroupField")))))',
56  ],
57  'Live, with hidden record preview' => [
58  'tableName' => 'aTable',
59  'tableAlias' => 'aTable',
60  'workspaceId' => 0,
61  'hiddenPagePreview' => true,
62  'hiddenRecordPreview' => true,
63  'frontendUserGroups' => [0, -1],
64  'localContextFrontendUserGroups' => null,
65  'expectedSQL' => '(("aTable"."deleted" = 0) AND ((("aTable"."t3ver_wsid" = 0) AND ((("aTable"."t3ver_oid" = 0) OR ("aTable"."t3ver_state" = 4))))) AND ("aTable"."myStartTimeField" <= 42) AND ((("aTable"."myEndTimeField" = 0) OR ("aTable"."myEndTimeField" > 42))) AND ((("aTable"."myGroupField" IS NULL) OR ("aTable"."myGroupField" = \'\') OR ("aTable"."myGroupField" = \'0\') OR (FIND_IN_SET(\'0\', "aTable"."myGroupField")) OR (FIND_IN_SET(\'-1\', "aTable"."myGroupField")))))',
66  ],
67  'Workspace, with WS preview' => [
68  'tableName' => 'aTable',
69  'tableAlias' => 'aTable',
70  'workspaceId' => 1,
71  'hiddenPagePreview' => false,
72  'hiddenRecordPreview' => false,
73  'frontendUserGroups' => [0, -1],
74  'localContextFrontendUserGroups' => null,
75  'expectedSQL' => '(("aTable"."deleted" = 0) AND ((("aTable"."t3ver_wsid" IN (0, 1)) AND ((("aTable"."t3ver_oid" = 0) OR ("aTable"."t3ver_state" = 4))))) AND ("aTable"."myHiddenField" = 0) AND ("aTable"."myStartTimeField" <= 42) AND ((("aTable"."myEndTimeField" = 0) OR ("aTable"."myEndTimeField" > 42))) AND ((("aTable"."myGroupField" IS NULL) OR ("aTable"."myGroupField" = \'\') OR ("aTable"."myGroupField" = \'0\') OR (FIND_IN_SET(\'0\', "aTable"."myGroupField")) OR (FIND_IN_SET(\'-1\', "aTable"."myGroupField")))))',
76  ],
77  'Workspace, with WS preview and hidden record preview' => [
78  'tableName' => 'aTable',
79  'tableAlias' => 'aTable',
80  'workspaceId' => 1,
81  'hiddenPagePreview' => true,
82  'hiddenRecordPreview' => true,
83  'frontendUserGroups' => [0, -1],
84  'localContextFrontendUserGroups' => null,
85  'expectedSQL' => '(("aTable"."deleted" = 0) AND ((("aTable"."t3ver_wsid" IN (0, 1)) AND ((("aTable"."t3ver_oid" = 0) OR ("aTable"."t3ver_state" = 4))))) AND ("aTable"."myStartTimeField" <= 42) AND ((("aTable"."myEndTimeField" = 0) OR ("aTable"."myEndTimeField" > 42))) AND ((("aTable"."myGroupField" IS NULL) OR ("aTable"."myGroupField" = \'\') OR ("aTable"."myGroupField" = \'0\') OR (FIND_IN_SET(\'0\', "aTable"."myGroupField")) OR (FIND_IN_SET(\'-1\', "aTable"."myGroupField")))))',
86  ],
87  'Live page, no preview' => [
88  'tableName' => 'pages',
89  'tableAlias' => 'pages',
90  'workspaceId' => 0,
91  'hiddenPagePreview' => false,
92  'hiddenRecordPreview' => false,
93  'frontendUserGroups' => [0, -1],
94  'localContextFrontendUserGroups' => null,
95  'expectedSQL' => '(("pages"."deleted" = 0) AND ((("pages"."t3ver_wsid" = 0) AND ((("pages"."t3ver_oid" = 0) OR ("pages"."t3ver_state" = 4))))) AND ("pages"."hidden" = 0) AND ("pages"."starttime" <= 42) AND ((("pages"."endtime" = 0) OR ("pages"."endtime" > 42))) AND ((("pages"."fe_group" IS NULL) OR ("pages"."fe_group" = \'\') OR ("pages"."fe_group" = \'0\') OR (FIND_IN_SET(\'0\', "pages"."fe_group")) OR (FIND_IN_SET(\'-1\', "pages"."fe_group")))))',
96  ],
97  'Live page, with hidden page preview' => [
98  'tableName' => 'pages',
99  'tableAlias' => 'pages',
100  'workspaceId' => 0,
101  'hiddenPagePreview' => true,
102  'hiddenRecordPreview' => true,
103  'frontendUserGroups' => [0, -1],
104  'localContextFrontendUserGroups' => null,
105  'expectedSQL' => '(("pages"."deleted" = 0) AND ((("pages"."t3ver_wsid" = 0) AND ((("pages"."t3ver_oid" = 0) OR ("pages"."t3ver_state" = 4))))) AND ("pages"."starttime" <= 42) AND ((("pages"."endtime" = 0) OR ("pages"."endtime" > 42))) AND ((("pages"."fe_group" IS NULL) OR ("pages"."fe_group" = \'\') OR ("pages"."fe_group" = \'0\') OR (FIND_IN_SET(\'0\', "pages"."fe_group")) OR (FIND_IN_SET(\'-1\', "pages"."fe_group")))))',
106  ],
107  'Workspace page, with WS preview' => [
108  'tableName' => 'pages',
109  'tableAlias' => 'pages',
110  'workspaceId' => 1,
111  'hiddenPagePreview' => false,
112  'hiddenRecordPreview' => false,
113  'frontendUserGroups' => [0, -1],
114  'localContextFrontendUserGroups' => null,
115  'expectedSQL' => '(("pages"."deleted" = 0) AND ((("pages"."t3ver_wsid" IN (0, 1)) AND ((("pages"."t3ver_oid" = 0) OR ("pages"."t3ver_state" = 4))))) AND ("pages"."hidden" = 0) AND ("pages"."starttime" <= 42) AND ((("pages"."endtime" = 0) OR ("pages"."endtime" > 42))) AND ((("pages"."fe_group" IS NULL) OR ("pages"."fe_group" = \'\') OR ("pages"."fe_group" = \'0\') OR (FIND_IN_SET(\'0\', "pages"."fe_group")) OR (FIND_IN_SET(\'-1\', "pages"."fe_group")))))',
116  ],
117  'Workspace page, with WS preview and hidden pages preview' => [
118  'tableName' => 'pages',
119  'tableAlias' => 'pages',
120  'workspaceId' => 1,
121  'hiddenPagePreview' => true,
122  'hiddenRecordPreview' => true,
123  'frontendUserGroups' => [0, -1],
124  'localContextFrontendUserGroups' => null,
125  'expectedSQL' => '(("pages"."deleted" = 0) AND ((("pages"."t3ver_wsid" IN (0, 1)) AND ((("pages"."t3ver_oid" = 0) OR ("pages"."t3ver_state" = 4))))) AND ("pages"."starttime" <= 42) AND ((("pages"."endtime" = 0) OR ("pages"."endtime" > 42))) AND ((("pages"."fe_group" IS NULL) OR ("pages"."fe_group" = \'\') OR ("pages"."fe_group" = \'0\') OR (FIND_IN_SET(\'0\', "pages"."fe_group")) OR (FIND_IN_SET(\'-1\', "pages"."fe_group")))))',
126  ],
127  'Live, no preview with alias' => [
128  'tableName' => 'aTable',
129  'tableAlias' => 'a',
130  'workspaceId' => 0,
131  'hiddenPagePreview' => false,
132  'hiddenRecordPreview' => false,
133  'frontendUserGroups' => [0, -1],
134  'localContextFrontendUserGroups' => null,
135  'expectedSQL' => '(("a"."deleted" = 0) AND ((("a"."t3ver_wsid" = 0) AND ((("a"."t3ver_oid" = 0) OR ("a"."t3ver_state" = 4))))) AND ("a"."myHiddenField" = 0) AND ("a"."myStartTimeField" <= 42) AND ((("a"."myEndTimeField" = 0) OR ("a"."myEndTimeField" > 42))) AND ((("a"."myGroupField" IS NULL) OR ("a"."myGroupField" = \'\') OR ("a"."myGroupField" = \'0\') OR (FIND_IN_SET(\'0\', "a"."myGroupField")) OR (FIND_IN_SET(\'-1\', "a"."myGroupField")))))',
136  ],
137  ];
138  }
139 
140  #[DataProvider('frontendStatesDataProvider')]
141  #[Test]
143  string $tableName,
144  string $tableAlias,
145  int $workspaceId,
146  bool $hiddenPagePreview,
147  bool $hiddenRecordPreview,
148  array $frontendUserGroups,
149  ?array $localContextFrontendUserGroups,
150  string $expectedSQL
151  ): void {
152  ‪$GLOBALS['TCA'] = [
153  'aTable' => [
154  'ctrl' => [
155  'versioningWS' => 2,
156  'delete' => 'deleted',
157  'enablecolumns' => [
158  'disabled' => 'myHiddenField',
159  'starttime' => 'myStartTimeField',
160  'endtime' => 'myEndTimeField',
161  'fe_group' => 'myGroupField',
162  ],
163  ],
164  ],
165  'pages' => [
166  'ctrl' => [
167  'label' => 'title',
168  'tstamp' => 'tstamp',
169  'sortby' => 'sorting',
170  'type' => 'doktype',
171  'versioningWS' => true,
172  'delete' => 'deleted',
173  'enablecolumns' => [
174  'disabled' => 'hidden',
175  'starttime' => 'starttime',
176  'endtime' => 'endtime',
177  'fe_group' => 'fe_group',
178  ],
179  ],
180  'columns' => [],
181  ],
182  ];
183  $context = new ‪Context();
184  $context->setAspect('visibility', new ‪VisibilityAspect($hiddenPagePreview, $hiddenRecordPreview));
185  $context->setAspect('frontend.user', new ‪UserAspect(new ‪FrontendUserAuthentication(), $frontendUserGroups));
186  $context->setAspect('workspace', new ‪WorkspaceAspect($workspaceId));
187  GeneralUtility::setSingletonInstance(Context::class, $context);
188 
189  ‪$GLOBALS['SIM_ACCESS_TIME'] = 42;
190 
191  $localContext = clone $context;
192  if ($localContextFrontendUserGroups !== null) {
193  $localContext->setAspect(
194  'frontend.user',
195  new ‪UserAspect(new ‪FrontendUserAuthentication(), $localContextFrontendUserGroups)
196  );
197  }
198 
199  $subject = new ‪FrontendRestrictionContainer($localContext);
200  $expression = $subject->buildExpression([$tableAlias => $tableName], $this->expressionBuilder);
201  self::assertSame($expectedSQL, (string)$expression);
202  }
203 }
‪TYPO3\CMS\Core\Context\VisibilityAspect
Definition: VisibilityAspect.php:31
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\Restriction\FrontendRestrictionContainerTest\buildExpressionAddsCorrectClause
‪buildExpressionAddsCorrectClause(string $tableName, string $tableAlias, int $workspaceId, bool $hiddenPagePreview, bool $hiddenRecordPreview, array $frontendUserGroups, ?array $localContextFrontendUserGroups, string $expectedSQL)
Definition: FrontendRestrictionContainerTest.php:142
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\Restriction\FrontendRestrictionContainerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FrontendRestrictionContainerTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\Restriction\FrontendRestrictionContainerTest
Definition: FrontendRestrictionContainerTest.php:31
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\Restriction
Definition: AbstractRestrictionContainerTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\Restriction\AbstractRestrictionTestCase
Definition: AbstractRestrictionTestCase.php:27
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\Restriction\FrontendRestrictionContainerTest\frontendStatesDataProvider
‪static frontendStatesDataProvider()
Definition: FrontendRestrictionContainerTest.php:34
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:30
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:37