[TASK] Prepare Frontend Tests for additional site handling tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / SiteHandling / SiteBasedTestTrait.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\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\Configuration\SiteConfiguration;
19 use TYPO3\CMS\Core\Tests\Functional\Fixtures\Frontend\PhpError;
20
21 /**
22 * Trait used for test classes that want to set up (= write) site configuration files.
23 *
24 * Mainly used when testing Site-related tests in Frontend requests.
25 *
26 * Be sure to set the LANGUAGE_PRESETS const in your class.
27 */
28 trait SiteBasedTestTrait
29 {
30 /**
31 * @param string $identifier
32 * @param array $site
33 * @param array $languages
34 * @param array $errorHandling
35 */
36 protected function writeSiteConfiguration(
37 string $identifier,
38 array $site = [],
39 array $languages = [],
40 array $errorHandling = []
41 ) {
42 $configuration = $site;
43 if (!empty($languages)) {
44 $configuration['languages'] = $languages;
45 }
46 if (!empty($errorHandling)) {
47 $configuration['errorHandling'] = $errorHandling;
48 }
49
50 $siteConfiguration = new SiteConfiguration(
51 $this->instancePath . '/typo3conf/sites/'
52 );
53
54 try {
55 $siteConfiguration->write($identifier, $configuration);
56 } catch (\Exception $exception) {
57 $this->markTestSkipped($exception->getMessage());
58 }
59 }
60
61 /**
62 * @param string $identifier
63 * @param array $overrides
64 */
65 protected function mergeSiteConfiguration(
66 string $identifier,
67 array $overrides
68 ) {
69 $siteConfiguration = new SiteConfiguration(
70 $this->instancePath . '/typo3conf/sites/'
71 );
72 $configuration = $siteConfiguration->load($identifier);
73 $configuration = array_merge($configuration, $overrides);
74 try {
75 $siteConfiguration->write($identifier, $configuration);
76 } catch (\Exception $exception) {
77 $this->markTestSkipped($exception->getMessage());
78 }
79 }
80
81 /**
82 * @param int $rootPageId
83 * @param string $base
84 * @return array
85 */
86 protected function buildSiteConfiguration(
87 int $rootPageId,
88 string $base = ''
89 ): array {
90 return [
91 'rootPageId' => $rootPageId,
92 'base' => $base,
93 ];
94 }
95
96 /**
97 * @param string $identifier
98 * @param string $base
99 * @return array
100 */
101 protected function buildDefaultLanguageConfiguration(
102 string $identifier,
103 string $base
104 ): array {
105 $configuration = $this->buildLanguageConfiguration($identifier, $base);
106 $configuration['typo3Language'] = 'default';
107 $configuration['flag'] = 'global';
108 unset($configuration['fallbackType'], $configuration['fallbacks']);
109 return $configuration;
110 }
111
112 /**
113 * @param string $identifier
114 * @param string $base
115 * @param array $fallbackIdentifiers
116 * @param string $fallbackType
117 * @return array
118 */
119 protected function buildLanguageConfiguration(
120 string $identifier,
121 string $base,
122 array $fallbackIdentifiers = [],
123 string $fallbackType = null
124 ): array {
125 $preset = $this->resolveLanguagePreset($identifier);
126
127 $configuration = [
128 'languageId' => $preset['id'],
129 'title' => $preset['title'],
130 'navigationTitle' => $preset['title'],
131 'base' => $base,
132 'locale' => $preset['locale'],
133 'iso-639-1' => $preset['iso'],
134 'hreflang' => $preset['hrefLang'],
135 'direction' => $preset['direction'],
136 'typo3Language' => $preset['iso'],
137 'flag' => $preset['iso'],
138 'fallbackType' => $fallbackType ?? (empty($fallbackIdentifiers) ? 'strict' : 'fallback'),
139 ];
140
141 if (!empty($fallbackIdentifiers)) {
142 $fallbackIds = array_map(
143 function (string $fallbackIdentifier) {
144 $preset = $this->resolveLanguagePreset($fallbackIdentifier);
145 return $preset['id'];
146 },
147 $fallbackIdentifiers
148 );
149 $configuration['fallbackType'] = $fallbackType ?? 'fallback';
150 $configuration['fallbacks'] = implode(',', $fallbackIds);
151 }
152
153 return $configuration;
154 }
155
156 /**
157 * @param string $handler
158 * @param array $codes
159 * @return array
160 */
161 protected function buildErrorHandlingConfiguration(
162 string $handler,
163 array $codes
164 ): array {
165 if ($handler === 'Page') {
166 $baseConfiguration = [
167 'errorContentSource' => '404',
168 ];
169 } elseif ($handler === 'Fluid') {
170 $baseConfiguration = [
171 'errorFluidTemplate' => 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/FluidError.html',
172 'errorFluidTemplatesRootPath' => '',
173 'errorFluidLayoutsRootPath' => '',
174 'errorFluidPartialsRootPath' => '',
175 ];
176 } elseif ($handler === 'PHP') {
177 $baseConfiguration = [
178 'errorPhpClassFQCN' => PhpError::class,
179 ];
180 } else {
181 throw new \LogicException(
182 sprintf('Invalid handler "%s"', $handler),
183 1533894782
184 );
185 }
186
187 $baseConfiguration['errorHandler'] = $handler;
188
189 return array_map(
190 function (int $code) use ($baseConfiguration) {
191 $baseConfiguration['errorCode'] = $code;
192 return $baseConfiguration;
193 },
194 $codes
195 );
196 }
197
198 /**
199 * @param string $identifier
200 * @return mixed
201 */
202 protected function resolveLanguagePreset(string $identifier)
203 {
204 if (!isset(static::LANGUAGE_PRESETS[$identifier])) {
205 throw new \LogicException(
206 sprintf('Undefined preset identifier "%s"', $identifier),
207 1533893665
208 );
209 }
210 return static::LANGUAGE_PRESETS[$identifier];
211 }
212 }