ac701d9c32d5593e4085a8750c78c74a046589ed
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Functional / SiteHandling / AbstractTestCase.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Frontend\Tests\Functional\SiteHandling;
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\Tests\Functional\SiteHandling\SiteBasedTestTrait;
19 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Fixtures\LinkHandlingController;
20 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\ArrayValueInstruction;
21 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
22
23 /**
24 * Abstract test case for frontend requests
25 */
26 abstract class AbstractTestCase extends FunctionalTestCase
27 {
28 use SiteBasedTestTrait;
29
30 protected const ENCRYPTION_KEY = '4408d27a916d51e624b69af3554f516dbab61037a9f7b9fd6f81b4d3bedeccb6';
31
32 protected const TYPO3_CONF_VARS = [
33 'SYS' => [
34 'encryptionKey' => self::ENCRYPTION_KEY,
35 ],
36 'FE' => [
37 'cacheHash' => [
38 'requireCacheHashPresenceParameters' => ['testing[value]']
39 ],
40 ]
41 ];
42
43 protected const LANGUAGE_PRESETS = [
44 'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8', 'iso' => 'en', 'hrefLang' => 'en-US', 'direction' => ''],
45 'FR' => ['id' => 1, 'title' => 'French', 'locale' => 'fr_FR.UTF8', 'iso' => 'fr', 'hrefLang' => 'fr-FR', 'direction' => ''],
46 'FR-CA' => ['id' => 2, 'title' => 'Franco-Canadian', 'locale' => 'fr_CA.UTF8', 'iso' => 'fr', 'hrefLang' => 'fr-CA', 'direction' => ''],
47 ];
48
49 /**
50 * @var string[]
51 */
52 protected $coreExtensionsToLoad = ['frontend', 'workspaces'];
53
54 /**
55 * @var string[]
56 */
57 protected $pathsToLinkInTestInstance = [
58 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/AdditionalConfiguration.php' => 'typo3conf/AdditionalConfiguration.php',
59 ];
60
61 /**
62 * Combines string values of multiple array as cross-product into flat items.
63 *
64 * Example:
65 * + meltStrings(['a','b'], ['c','e'], ['f','g'])
66 * + results into ['acf', 'acg', 'aef', 'aeg', 'bcf', 'bcg', 'bef', 'beg']
67 *
68 * @param array $arrays Distinct array that should be melted
69 * @param callable $finalCallback Callback being executed on last multiplier
70 * @param string $prefix Prefix containing concatenated previous values
71 * @return array
72 */
73 protected function meltStrings(array $arrays, callable $finalCallback = null, string $prefix = ''): array
74 {
75 $results = [[]];
76 $array = array_shift($arrays);
77 foreach ($array as $item) {
78 $resultItem = $prefix . $item;
79 if (count($arrays) > 0) {
80 $results[] = $this->meltStrings($arrays, $finalCallback, $resultItem);
81 continue;
82 }
83 if ($finalCallback !== null) {
84 $resultItem = call_user_func($finalCallback, $resultItem);
85 }
86 $results[] = [$resultItem];
87 }
88 return array_merge(...$results);
89 }
90
91 /**
92 * @param array $array
93 * @return array
94 */
95 protected function wrapInArray(array $array): array
96 {
97 return array_map(
98 function ($item) {
99 return [$item];
100 },
101 $array
102 );
103 }
104
105 /**
106 * @param string[] $array
107 * @return array
108 */
109 protected function keysFromValues(array $array): array
110 {
111 return array_combine($array, $array);
112 }
113
114 /**
115 * Generates key names based on a template and array items as arguments.
116 *
117 * + keysFromTemplate([[1, 2, 3], [11, 22, 33]], '%1$d->%2$d (user:%3$d)')
118 * + returns the following array with generated keys
119 * [
120 * '1->2 (user:3)' => [1, 2, 3],
121 * '11->22 (user:33)' => [11, 22, 33],
122 * ]
123 *
124 * @param array $array
125 * @param string $template
126 * @param callable|null $callback
127 * @return array
128 */
129 protected function keysFromTemplate(array $array, string $template, callable $callback = null): array
130 {
131 $keys = array_unique(
132 array_map(
133 function (array $values) use ($template, $callback) {
134 if ($callback !== null) {
135 $values = call_user_func($callback, $values);
136 }
137 return vsprintf($template, $values);
138 },
139 $array
140 )
141 );
142
143 if (count($keys) !== count($array)) {
144 throw new \LogicException(
145 'Amount of generated keys does not match to item count.',
146 1534682840
147 );
148 }
149
150 return array_combine($keys, $array);
151 }
152
153 /**
154 * @param array $typoScript
155 * @return ArrayValueInstruction
156 */
157 protected function createTypoLinkUrlInstruction(array $typoScript): ArrayValueInstruction
158 {
159 return (new ArrayValueInstruction(LinkHandlingController::class))
160 ->withArray([
161 '10' => 'TEXT',
162 '10.' => [
163 'typolink.' => array_merge(
164 $typoScript,
165 ['returnLast' => 'url']
166 )
167 ]
168 ]);
169 }
170
171 /**
172 * @param array $typoScript
173 * @return ArrayValueInstruction
174 */
175 protected function createHierarchicalMenuProcessorInstruction(array $typoScript): ArrayValueInstruction
176 {
177 return (new ArrayValueInstruction(LinkHandlingController::class))
178 ->withArray([
179 '10' => 'FLUIDTEMPLATE',
180 '10.' => [
181 'file' => 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/FluidJson.html',
182 'dataProcessing.' => [
183 '1' => 'TYPO3\\CMS\\Frontend\\DataProcessing\\MenuProcessor',
184 '1.' => array_merge(
185 $typoScript,
186 ['as' => 'results']
187 )
188 ],
189 ],
190 ]);
191 }
192
193 /**
194 * @param array $typoScript
195 * @return ArrayValueInstruction
196 */
197 protected function createLanguageMenuProcessorInstruction(array $typoScript): ArrayValueInstruction
198 {
199 return (new ArrayValueInstruction(LinkHandlingController::class))
200 ->withArray([
201 '10' => 'FLUIDTEMPLATE',
202 '10.' => [
203 'file' => 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/FluidJson.html',
204 'dataProcessing.' => [
205 '1' => 'TYPO3\\CMS\\Frontend\\DataProcessing\\LanguageMenuProcessor',
206 '1.' => array_merge(
207 $typoScript,
208 ['as' => 'results']
209 )
210 ],
211 ],
212 ]);
213 }
214
215 /**
216 * Filters and keeps only desired names.
217 *
218 * @param array $menu
219 * @param array $keepNames
220 * @return array
221 */
222 protected function filterMenu(
223 array $menu,
224 array $keepNames = ['title', 'link']
225 ): array {
226 if (!in_array('children', $keepNames)) {
227 $keepNames[] = 'children';
228 }
229 return array_map(
230 function (array $menuItem) use ($keepNames) {
231 $menuItem = array_filter(
232 $menuItem,
233 function (string $name) use ($keepNames) {
234 return in_array($name, $keepNames);
235 },
236 ARRAY_FILTER_USE_KEY
237 );
238 if (is_array($menuItem['children'] ?? null)) {
239 $menuItem['children'] = $this->filterMenu(
240 $menuItem['children'],
241 $keepNames
242 );
243 }
244 return $menuItem;
245 },
246 $menu
247 );
248 }
249
250 /**
251 * @param array $keys
252 * @param mixed $payload
253 * @return array
254 */
255 protected function populateToKeys(array $keys, $payload): array
256 {
257 $result = [];
258 foreach ($keys as $key) {
259 $result[$key] = $payload;
260 }
261 return $result;
262 }
263 }