[FEATURE] Add Contexts for storing data access modes
[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 use TYPO3\CMS\Frontend\Page\PageRepository;
26
27 class FrontendRestrictionContainerTest extends AbstractRestrictionTestCase
28 {
29 protected $resetSingletonInstances = true;
30
31 public function frontendStatesDataProvider()
32 {
33 return [
34 'Live, no preview' => [
35 'tableName' => 'aTable',
36 'tableAlias' => 'aTable',
37 'workspaceId' => 0,
38 'hiddenPagePreview' => false,
39 'hiddenRecordPreview' => false,
40 'frontendUserGroups' => [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 'hiddenPagePreview' => true,
48 'hiddenRecordPreview' => true,
49 'frontendUserGroups' => [0, -1],
50 '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")))'
51 ],
52 'Workspace, with WS preview' => [
53 'tableName' => 'aTable',
54 'tableAlias' => 'aTable',
55 'workspaceId' => 1,
56 'hiddenPagePreview' => false,
57 'hiddenRecordPreview' => false,
58 'frontendUserGroups' => [0, -1],
59 '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")))'
60 ],
61 'Workspace, with WS preview and hidden record preview' => [
62 'tableName' => 'aTable',
63 'tableAlias' => 'aTable',
64 'workspaceId' => 1,
65 'hiddenPagePreview' => true,
66 'hiddenRecordPreview' => true,
67 'frontendUserGroups' => [0, -1],
68 '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")))'
69 ],
70 'Live page, no preview' => [
71 'tableName' => 'pages',
72 'tableAlias' => 'pages',
73 'workspaceId' => 0,
74 'hiddenPagePreview' => false,
75 'hiddenRecordPreview' => false,
76 'frontendUserGroups' => [0, -1],
77 '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")))'
78 ],
79 'Live page, with hidden page preview' => [
80 'tableName' => 'pages',
81 'tableAlias' => 'pages',
82 'workspaceId' => 0,
83 'hiddenPagePreview' => true,
84 'hiddenRecordPreview' => true,
85 'frontendUserGroups' => [0, -1],
86 '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")))'
87 ],
88 'Workspace page, with WS preview' => [
89 'tableName' => 'pages',
90 'tableAlias' => 'pages',
91 'workspaceId' => 1,
92 'hiddenPagePreview' => false,
93 'hiddenRecordPreview' => false,
94 'frontendUserGroups' => [0, -1],
95 '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")))'
96 ],
97 'Workspace page, with WS preview and hidden pages preview' => [
98 'tableName' => 'pages',
99 'tableAlias' => 'pages',
100 'workspaceId' => 1,
101 'hiddenPagePreview' => true,
102 'hiddenRecordPreview' => true,
103 'frontendUserGroups' => [0, -1],
104 '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")))'
105 ],
106 'Live, no preview with alias' => [
107 'tableName' => 'aTable',
108 'tableAlias' => 'a',
109 'workspaceId' => 0,
110 'hiddenPagePreview' => false,
111 'hiddenRecordPreview' => false,
112 'frontendUserGroups' => [0, -1],
113 '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")))'
114 ],
115 ];
116 }
117
118 /**
119 * @param string $tableName
120 * @param string $tableAlias
121 * @param int $workspaceId
122 * @param bool $hiddenPagePreview
123 * @param bool $hiddenRecordPreview
124 * @param array $frontendUserGroups
125 * @param string $expectedSQL
126 *
127 * @test
128 * @dataProvider frontendStatesDataProvider
129 */
130 public function buildExpressionAddsCorrectClause(
131 string $tableName,
132 string $tableAlias,
133 int $workspaceId,
134 bool $hiddenPagePreview,
135 bool $hiddenRecordPreview,
136 array $frontendUserGroups,
137 string $expectedSQL
138 ) {
139 $GLOBALS['TCA'] = [
140 'aTable' => [
141 'ctrl' => [
142 'versioningWS' => 2,
143 'delete' => 'deleted',
144 'enablecolumns' => [
145 'disabled' => 'myHiddenField',
146 'starttime' => 'myStartTimeField',
147 'endtime' => 'myEndTimeField',
148 'fe_group' => 'myGroupField',
149 ],
150 ],
151 ],
152 'pages' => [
153 'ctrl' => [
154 'label' => 'title',
155 'tstamp' => 'tstamp',
156 'sortby' => 'sorting',
157 'type' => 'doktype',
158 'versioningWS' => true,
159 'origUid' => 't3_origuid',
160 'delete' => 'deleted',
161 'enablecolumns' => [
162 'disabled' => 'hidden',
163 'starttime' => 'starttime',
164 'endtime' => 'endtime',
165 'fe_group' => 'fe_group'
166 ],
167 ],
168 'columns' => []
169 ]
170 ];
171 $context = new Context([
172 'visibility' => new VisibilityAspect($hiddenPagePreview, $hiddenRecordPreview),
173 'frontend.user' => new UserAspect(new FrontendUserAuthentication(), $frontendUserGroups),
174 'workspace' => new WorkspaceAspect($workspaceId)
175 ]);
176 GeneralUtility::setSingletonInstance(Context::class, $context);
177
178 $pageRepository = $this->createMock(PageRepository::class);
179 $pageRepository->__set('context', $context);
180 $pageRepository->versioningWorkspaceId = $workspaceId;
181
182 $GLOBALS['TSFE'] = new \stdClass();
183 $GLOBALS['TSFE']->sys_page = $pageRepository;
184 $GLOBALS['SIM_ACCESS_TIME'] = 42;
185
186 $subject = new FrontendRestrictionContainer($context);
187 $expression = $subject->buildExpression([$tableAlias => $tableName], $this->expressionBuilder);
188 $this->assertSame($expectedSQL, (string)$expression);
189 }
190 }