[BUGFIX] Change order of tablename and alias in restriction container
[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\Database\Query\Restriction\FrontendRestrictionContainer;
19 use TYPO3\CMS\Frontend\Page\PageRepository;
20
21 class FrontendRestrictionContainerTest extends AbstractRestrictionTestCase
22 {
23 /**
24 */
25 protected function setUp()
26 {
27 parent::setUp();
28 }
29
30 public function frontendStatesDataProvider()
31 {
32 return [
33 'Live, no preview' => [
34 'tableName' => 'aTable',
35 'tableAlias' => 'aTable',
36 'workspaceId' => 0,
37 'workspacePreview' => false,
38 'hiddenPagePreview' => false,
39 'hiddenRecordPreview' => false,
40 'feGroupList' => '0,-1',
41 '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")))'
42 ],
43 'Live, with hidden record preview' => [
44 'tableName' => 'aTable',
45 'tableAlias' => 'aTable',
46 'workspaceId' => 0,
47 'workspacePreview' => false,
48 'hiddenPagePreview' => true,
49 'hiddenRecordPreview' => true,
50 'feGroupList' => '0,-1',
51 '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")))'
52 ],
53 'Workspace, with WS preview' => [
54 'tableName' => 'aTable',
55 'tableAlias' => 'aTable',
56 'workspaceId' => 1,
57 'workspacePreview' => true,
58 'hiddenPagePreview' => false,
59 'hiddenRecordPreview' => false,
60 'feGroupList' => '0,-1',
61 'expectedSQL' => '("aTable"."deleted" = 0) AND ((("aTable"."t3ver_wsid" = 0) OR ("aTable"."t3ver_wsid" = 1)) 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")))'
62 ],
63 'Workspace, with WS preview and hidden record preview' => [
64 'tableName' => 'aTable',
65 'tableAlias' => 'aTable',
66 'workspaceId' => 1,
67 'workspacePreview' => true,
68 'hiddenPagePreview' => true,
69 'hiddenRecordPreview' => true,
70 'feGroupList' => '0,-1',
71 'expectedSQL' => '("aTable"."deleted" = 0) AND ((("aTable"."t3ver_wsid" = 0) OR ("aTable"."t3ver_wsid" = 1)) 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")))'
72 ],
73 'Live page, no preview' => [
74 'tableName' => 'pages',
75 'tableAlias' => 'pages',
76 'workspaceId' => 0,
77 'workspacePreview' => false,
78 'hiddenPagePreview' => false,
79 'hiddenRecordPreview' => false,
80 'feGroupList' => '0,-1',
81 '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")))'
82 ],
83 'Live page, with hidden page preview' => [
84 'tableName' => 'pages',
85 'tableAlias' => 'pages',
86 'workspaceId' => 0,
87 'workspacePreview' => false,
88 'hiddenPagePreview' => true,
89 'hiddenRecordPreview' => true,
90 'feGroupList' => '0,-1',
91 '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")))'
92 ],
93 'Workspace page, with WS preview' => [
94 'tableName' => 'pages',
95 'tableAlias' => 'pages',
96 'workspaceId' => 1,
97 'workspacePreview' => true,
98 'hiddenPagePreview' => false,
99 'hiddenRecordPreview' => false,
100 'feGroupList' => '0,-1',
101 'expectedSQL' => '("pages"."deleted" = 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")))'
102 ],
103 'Workspace page, with WS preview and hidden pages preview' => [
104 'tableName' => 'pages',
105 'tableAlias' => 'pages',
106 'workspaceId' => 1,
107 'workspacePreview' => true,
108 'hiddenPagePreview' => true,
109 'hiddenRecordPreview' => true,
110 'feGroupList' => '0,-1',
111 'expectedSQL' => '("pages"."deleted" = 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")))'
112 ],
113 'Live, no preview with alias' => [
114 'tableName' => 'aTable',
115 'tableAlias' => 'a',
116 'workspaceId' => 0,
117 'workspacePreview' => false,
118 'hiddenPagePreview' => false,
119 'hiddenRecordPreview' => false,
120 'feGroupList' => '0,-1',
121 '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")))'
122 ],
123 ];
124 }
125
126 /**
127 * @param string $tableName
128 * @param string $tableAlias
129 * @param int $workspaceId
130 * @param bool $workspacePreview
131 * @param bool $hiddenPagePreview
132 * @param bool $hiddenRecordPreview
133 * @param string $feGroupList
134 * @param string $expectedSQL
135 *
136 * @test
137 * @dataProvider frontendStatesDataProvider
138 */
139 public function buildExpressionAddsCorrectClause(
140 string $tableName,
141 string $tableAlias,
142 int $workspaceId,
143 bool $workspacePreview,
144 bool $hiddenPagePreview,
145 bool $hiddenRecordPreview,
146 string $feGroupList,
147 string $expectedSQL
148 ) {
149 $GLOBALS['TCA'] = [
150 'aTable' => [
151 'ctrl' => [
152 'versioningWS' => 2,
153 'delete' => 'deleted',
154 'enablecolumns' => [
155 'disabled' => 'myHiddenField',
156 'starttime' => 'myStartTimeField',
157 'endtime' => 'myEndTimeField',
158 'fe_group' => 'myGroupField',
159 ],
160 ],
161 ],
162 'pages' => [
163 'ctrl' => [
164 'label' => 'title',
165 'tstamp' => 'tstamp',
166 'sortby' => 'sorting',
167 'type' => 'doktype',
168 'versioningWS' => true,
169 'origUid' => 't3_origuid',
170 'delete' => 'deleted',
171 'enablecolumns' => [
172 'disabled' => 'hidden',
173 'starttime' => 'starttime',
174 'endtime' => 'endtime',
175 'fe_group' => 'fe_group'
176 ],
177 ],
178 'columns' => []
179 ]
180 ];
181
182 $pageRepository = $this->createMock(PageRepository::class);
183 $pageRepository->versioningWorkspaceId = $workspaceId;
184 $pageRepository->versioningPreview = $workspacePreview;
185
186 $typoScriptFrontendController = new \stdClass();
187 $typoScriptFrontendController->showHiddenPage = $hiddenPagePreview;
188 $typoScriptFrontendController->showHiddenRecords = $hiddenRecordPreview;
189 $typoScriptFrontendController->gr_list = $feGroupList;
190 $typoScriptFrontendController->sys_page = $pageRepository;
191
192 $GLOBALS['TSFE'] = $typoScriptFrontendController;
193 $GLOBALS['SIM_ACCESS_TIME'] = 42;
194
195 $subject = new FrontendRestrictionContainer();
196 $expression = $subject->buildExpression([$tableAlias => $tableName], $this->expressionBuilder);
197 $this->assertSame($expectedSQL, (string)$expression);
198 }
199 }