‪TYPO3CMS  11.5
SiteBasedTestTrait.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
23 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\AbstractInstruction;
24 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\ArrayValueInstruction;
25 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\TypoScriptInstruction;
26 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
27 
36 {
40  protected static function ‪failIfArrayIsNotEmpty(array $items): void
41  {
42  if (empty($items)) {
43  return;
44  }
45 
46  static::fail(
47  'Array was not empty as expected, but contained these items:' . LF
48  . '* ' . implode(LF . '* ', $items)
49  );
50  }
51 
58  protected function ‪writeSiteConfiguration(
59  string $identifier,
60  array $site = [],
61  array $languages = [],
62  array $errorHandling = []
63  ): void {
64  $configuration = $site;
65  if (!empty($languages)) {
66  $configuration['languages'] = $languages;
67  }
68  if (!empty($errorHandling)) {
69  $configuration['errorHandling'] = $errorHandling;
70  }
71  $siteConfiguration = new ‪SiteConfiguration(
72  $this->instancePath . '/typo3conf/sites/',
73  $this->get('cache.core')
74  );
75 
76  try {
77  // ensure no previous site configuration influences the test
78  ‪GeneralUtility::rmdir($this->instancePath . '/typo3conf/sites/' . $identifier, true);
79  $siteConfiguration->write($identifier, $configuration);
80  } catch (\‪Exception $exception) {
81  $this->markTestSkipped($exception->getMessage());
82  }
83  }
84 
89  protected function ‪mergeSiteConfiguration(
90  string $identifier,
91  array $overrides
92  ): void {
93  $siteConfiguration = new ‪SiteConfiguration(
94  $this->instancePath . '/typo3conf/sites/',
95  $this->get('cache.core')
96  );
97  $configuration = $siteConfiguration->load($identifier);
98  $configuration = array_merge($configuration, $overrides);
99  try {
100  $siteConfiguration->write($identifier, $configuration);
101  } catch (\‪Exception $exception) {
102  $this->markTestSkipped($exception->getMessage());
103  }
104  }
105 
111  protected function ‪buildSiteConfiguration(
112  int $rootPageId,
113  string $base = ''
114  ): array {
115  return [
116  'rootPageId' => $rootPageId,
117  'base' => $base,
118  ];
119  }
120 
127  string $identifier,
128  string $base
129  ): array {
130  $configuration = $this->buildLanguageConfiguration($identifier, $base);
131  $configuration['typo3Language'] = 'default';
132  $configuration['flag'] = 'global';
133  unset($configuration['fallbackType'], $configuration['fallbacks']);
134  return $configuration;
135  }
136 
144  protected function ‪buildLanguageConfiguration(
145  string $identifier,
146  string $base,
147  array $fallbackIdentifiers = [],
148  string $fallbackType = null
149  ): array {
150  $preset = $this->resolveLanguagePreset($identifier);
151 
152  $configuration = [
153  'languageId' => $preset['id'],
154  'title' => $preset['title'],
155  'navigationTitle' => $preset['title'],
156  'base' => $base,
157  'locale' => $preset['locale'],
158  'iso-639-1' => $preset['iso'] ?? '',
159  'hreflang' => $preset['hrefLang'] ?? '',
160  'direction' => $preset['direction'] ?? '',
161  'typo3Language' => $preset['iso'] ?? '',
162  'flag' => $preset['iso'] ?? '',
163  'fallbackType' => $fallbackType ?? (empty($fallbackIdentifiers) ? 'strict' : 'fallback'),
164  ];
165 
166  if (!empty($fallbackIdentifiers)) {
167  $fallbackIds = array_map(
168  function (string $fallbackIdentifier) {
169  $preset = $this->resolveLanguagePreset($fallbackIdentifier);
170  return $preset['id'];
171  },
172  $fallbackIdentifiers
173  );
174  $configuration['fallbackType'] = $fallbackType ?? 'fallback';
175  $configuration['fallbacks'] = implode(',', $fallbackIds);
176  }
177 
178  return $configuration;
179  }
180 
187  string $handler,
188  array $codes
189  ): array {
190  if ($handler === 'Page') {
191  // This implies you cannot test both 404 and 403 in the same test.
192  // Fixing that requires much deeper changes to the testing harness,
193  // as the structure here is only a portion of the config array structure.
194  if (in_array(404, $codes, true)) {
195  $baseConfiguration = [
196  'errorContentSource' => 't3://page?uid=404',
197  ];
198  } elseif (in_array(403, $codes, true)) {
199  $baseConfiguration = [
200  'errorContentSource' => 't3://page?uid=403',
201  ];
202  }
203  } elseif ($handler === 'Fluid') {
204  $baseConfiguration = [
205  'errorFluidTemplate' => 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/FluidError.html',
206  'errorFluidTemplatesRootPath' => '',
207  'errorFluidLayoutsRootPath' => '',
208  'errorFluidPartialsRootPath' => '',
209  ];
210  } elseif ($handler === 'PHP') {
211  $baseConfiguration = [
212  'errorPhpClassFQCN' => PhpError::class,
213  ];
214  } else {
215  throw new \LogicException(
216  sprintf('Invalid handler "%s"', $handler),
217  1533894782
218  );
219  }
220 
221  $baseConfiguration['errorHandler'] = $handler;
222 
223  return array_map(
224  static function (int $code) use ($baseConfiguration) {
225  $baseConfiguration['errorCode'] = $code;
226  return $baseConfiguration;
227  },
228  $codes
229  );
230  }
231 
236  protected function ‪resolveLanguagePreset(string $identifier)
237  {
238  if (!isset(static::LANGUAGE_PRESETS[$identifier])) {
239  throw new \LogicException(
240  sprintf('Undefined preset identifier "%s"', $identifier),
241  1533893665
242  );
243  }
244  return static::LANGUAGE_PRESETS[$identifier];
245  }
246 
254  protected function ‪applyInstructions(InternalRequest $request, AbstractInstruction ...$instructions): InternalRequest
255  {
256  $modifiedInstructions = [];
257 
258  foreach ($instructions as $instruction) {
259  $identifier = $instruction->getIdentifier();
260  if (isset($modifiedInstructions[$identifier]) || $request->getInstruction($identifier) !== null) {
261  $modifiedInstructions[$identifier] = $this->mergeInstruction(
262  $modifiedInstructions[$identifier] ?? $request->getInstruction($identifier),
263  $instruction
264  );
265  } else {
266  $modifiedInstructions[$identifier] = $instruction;
267  }
268  }
269 
270  return $request->withInstructions($modifiedInstructions);
271  }
272 
278  protected function ‪mergeInstruction(AbstractInstruction $current, AbstractInstruction $other): AbstractInstruction
279  {
280  if (get_class($current) !== get_class($other)) {
281  throw new \LogicException('Cannot merge different instruction types', 1565863174);
282  }
283 
284  if ($current instanceof TypoScriptInstruction) {
286  $typoScript = array_replace_recursive(
287  $current->getTypoScript() ?? [],
288  $other->getTypoScript() ?? []
289  );
290  $constants = array_replace_recursive(
291  $current->getConstants() ?? [],
292  $other->getConstants() ?? []
293  );
294  if ($typoScript !== []) {
295  $current = $current->withTypoScript($typoScript);
296  }
297  if ($constants !== []) {
298  $current = $current->withConstants($constants);
299  }
300  return $current;
301  }
302 
303  if ($current instanceof ArrayValueInstruction) {
305  $array = array_merge_recursive($current->getArray(), $other->getArray());
306  return $current->withArray($array);
307  }
308 
309  return $current;
310  }
311 }
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\failIfArrayIsNotEmpty
‪static failIfArrayIsNotEmpty(array $items)
Definition: SiteBasedTestTrait.php:40
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildErrorHandlingConfiguration
‪array buildErrorHandlingConfiguration(string $handler, array $codes)
Definition: SiteBasedTestTrait.php:186
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling
Definition: SiteBasedTestTrait.php:18
‪TYPO3\CMS\Core\Tests\Functional\Fixtures\Frontend\PhpError
Definition: PhpError.php:29
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:36
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildLanguageConfiguration
‪array buildLanguageConfiguration(string $identifier, string $base, array $fallbackIdentifiers=[], string $fallbackType=null)
Definition: SiteBasedTestTrait.php:144
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:58
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:43
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪array buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:126
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\resolveLanguagePreset
‪mixed resolveLanguagePreset(string $identifier)
Definition: SiteBasedTestTrait.php:236
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\mergeInstruction
‪AbstractInstruction mergeInstruction(AbstractInstruction $current, AbstractInstruction $other)
Definition: SiteBasedTestTrait.php:278
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\applyInstructions
‪InternalRequest applyInstructions(InternalRequest $request, AbstractInstruction ... $instructions)
Definition: SiteBasedTestTrait.php:254
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪array buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:111
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir($path, $removeNonEmpty=false)
Definition: GeneralUtility.php:1961
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\mergeSiteConfiguration
‪mergeSiteConfiguration(string $identifier, array $overrides)
Definition: SiteBasedTestTrait.php:89