[BUGFIX] Set constraints in `FrontendRestrictionContainer` depending on workspace...
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Query / Restriction / FrontendRestrictionContainerTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Database\Query\Restriction;
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
18 use TYPO3\CMS\Core\Context\Context;
19 use TYPO3\CMS\Core\Context\UserAspect;
20 use TYPO3\CMS\Core\Context\VisibilityAspect;
21 use TYPO3\CMS\Core\Context\WorkspaceAspect;
22 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
25
26 class FrontendRestrictionContainerTest extends AbstractRestrictionTestCase
27 {
28 protected $resetSingletonInstances = true;
29
30 public function frontendStatesDataProvider()
31 {
32 return [
33 'Live, no preview' => [
34 'tableName' => 'aTable',
35 'tableAlias' => 'aTable',
36 'workspaceId' => 0,
37 'hiddenPagePreview' => false,
38 'hiddenRecordPreview' => false,
39 'frontendUserGroups' => [0, -1],
40 'expectedSQL' => '("aTable"."deleted" = 0) AND (("aTable"."t3ver_state" <= 0) AND ("aTable"."pid" <> -1)) 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")))'
41 ],
42 'Live, with hidden record preview' => [
43 'tableName' => 'aTable',
44 'tableAlias' => 'aTable',
45 'workspaceId' => 0,
46 'hiddenPagePreview' => true,
47 'hiddenRecordPreview' => true,
48 'frontendUserGroups' => [0, -1],
49 'expectedSQL' => '("aTable"."deleted" = 0) AND (("aTable"."t3ver_state" <= 0) AND ("aTable"."pid" <> -1)) 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")))'
50 ],
51 'Workspace, with WS preview' => [
52 'tableName' => 'aTable',
53 'tableAlias' => 'aTable',
54 'workspaceId' => 1,
55 'hiddenPagePreview' => false,
56 'hiddenRecordPreview' => false,
57 'frontendUserGroups' => [0, -1],
58 'expectedSQL' => '("aTable"."deleted" = 0) AND ((("aTable"."t3ver_wsid" = 0) OR ("aTable"."t3ver_wsid" = 1)) AND ("aTable"."pid" <> -1))'
59 ],
60 'Workspace, with WS preview and hidden record preview' => [
61 'tableName' => 'aTable',
62 'tableAlias' => 'aTable',
63 'workspaceId' => 1,
64 'hiddenPagePreview' => true,
65 'hiddenRecordPreview' => true,
66 'frontendUserGroups' => [0, -1],
67 'expectedSQL' => '("aTable"."deleted" = 0) AND ((("aTable"."t3ver_wsid" = 0) OR ("aTable"."t3ver_wsid" = 1)) AND ("aTable"."pid" <> -1))'
68 ],
69 'Live page, no preview' => [
70 'tableName' => 'pages',
71 'tableAlias' => 'pages',
72 'workspaceId' => 0,
73 'hiddenPagePreview' => false,
74 'hiddenRecordPreview' => false,
75 'frontendUserGroups' => [0, -1],
76 'expectedSQL' => '("pages"."deleted" = 0) AND (("pages"."t3ver_state" <= 0) AND ("pages"."pid" <> -1)) 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")))'
77 ],
78 'Live page, with hidden page preview' => [
79 'tableName' => 'pages',
80 'tableAlias' => 'pages',
81 'workspaceId' => 0,
82 'hiddenPagePreview' => true,
83 'hiddenRecordPreview' => true,
84 'frontendUserGroups' => [0, -1],
85 'expectedSQL' => '("pages"."deleted" = 0) AND (("pages"."t3ver_state" <= 0) AND ("pages"."pid" <> -1)) 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")))'
86 ],
87 'Workspace page, with WS preview' => [
88 'tableName' => 'pages',
89 'tableAlias' => 'pages',
90 'workspaceId' => 1,
91 'hiddenPagePreview' => false,
92 'hiddenRecordPreview' => false,
93 'frontendUserGroups' => [0, -1],
94 'expectedSQL' => '("pages"."deleted" = 0) AND ("pages"."pid" <> -1)'
95 ],
96 'Workspace page, with WS preview and hidden pages preview' => [
97 'tableName' => 'pages',
98 'tableAlias' => 'pages',
99 'workspaceId' => 1,
100 'hiddenPagePreview' => true,
101 'hiddenRecordPreview' => true,
102 'frontendUserGroups' => [0, -1],
103 'expectedSQL' => '("pages"."deleted" = 0) AND ("pages"."pid" <> -1)'
104 ],
105 'Live, no preview with alias' => [
106 'tableName' => 'aTable',
107 'tableAlias' => 'a',
108 'workspaceId' => 0,
109 'hiddenPagePreview' => false,
110 'hiddenRecordPreview' => false,
111 'frontendUserGroups' => [0, -1],
112 'expectedSQL' => '("a"."deleted" = 0) AND (("a"."t3ver_state" <= 0) AND ("a"."pid" <> -1)) 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")))'
113 ],
114 ];
115 }
116
117 /**
118 * @param string $tableName
119 * @param string $tableAlias
120 * @param int $workspaceId
121 * @param bool $hiddenPagePreview
122 * @param bool $hiddenRecordPreview
123 * @param array $frontendUserGroups
124 * @param string $expectedSQL
125 *
126 * @test
127 * @dataProvider frontendStatesDataProvider
128 */
129 public function buildExpressionAddsCorrectClause(
130 string $tableName,
131 string $tableAlias,
132 int $workspaceId,
133 bool $hiddenPagePreview,
134 bool $hiddenRecordPreview,
135 array $frontendUserGroups,
136 string $expectedSQL
137 ) {
138 $GLOBALS['TCA'] = [
139 'aTable' => [
140 'ctrl' => [
141 'versioningWS' => 2,
142 'delete' => 'deleted',
143 'enablecolumns' => [
144 'disabled' => 'myHiddenField',
145 'starttime' => 'myStartTimeField',
146 'endtime' => 'myEndTimeField',
147 'fe_group' => 'myGroupField',
148 ],
149 ],
150 ],
151 'pages' => [
152 'ctrl' => [
153 'label' => 'title',
154 'tstamp' => 'tstamp',
155 'sortby' => 'sorting',
156 'type' => 'doktype',
157 'versioningWS' => true,
158 'origUid' => 't3_origuid',
159 'delete' => 'deleted',
160 'enablecolumns' => [
161 'disabled' => 'hidden',
162 'starttime' => 'starttime',
163 'endtime' => 'endtime',
164 'fe_group' => 'fe_group'
165 ],
166 ],
167 'columns' => []
168 ]
169 ];
170 $context = new Context([
171 'visibility' => new VisibilityAspect($hiddenPagePreview, $hiddenRecordPreview),
172 'frontend.user' => new UserAspect(new FrontendUserAuthentication(), $frontendUserGroups),
173 'workspace' => new WorkspaceAspect($workspaceId)
174 ]);
175 GeneralUtility::setSingletonInstance(Context::class, $context);
176
177 $GLOBALS['SIM_ACCESS_TIME'] = 42;
178
179 $subject = new FrontendRestrictionContainer($context);
180 $expression = $subject->buildExpression([$tableAlias => $tableName], $this->expressionBuilder);
181 $this->assertSame($expectedSQL, (string)$expression);
182 }
183 }