‪TYPO3CMS  9.5
SiteBasedTestTrait.php
Go to the documentation of this file.
1 <?php
2 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 
21 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\AbstractInstruction;
22 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\ArrayValueInstruction;
23 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\TypoScriptInstruction;
24 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
25 
34 {
38  protected static function ‪failIfArrayIsNotEmpty(array $items): void
39  {
40  if (empty($items)) {
41  return;
42  }
43 
44  static::fail(
45  'Array was not empty as expected, but contained these items:' . LF
46  . '* ' . implode(LF . '* ', $items)
47  );
48  }
49 
56  protected function ‪writeSiteConfiguration(
57  string $identifier,
58  array $site = [],
59  array $languages = [],
60  array $errorHandling = []
61  ) {
62  $configuration = $site;
63  if (!empty($languages)) {
64  $configuration['languages'] = $languages;
65  }
66  if (!empty($errorHandling)) {
67  $configuration['errorHandling'] = $errorHandling;
68  }
69  $siteConfiguration = new ‪SiteConfiguration(
70  $this->instancePath . '/typo3conf/sites/'
71  );
72 
73  try {
74  // ensure no previous site configuration influences the test
75  GeneralUtility::rmdir($this->instancePath . '/typo3conf/sites/' . $identifier, true);
76  $siteConfiguration->write($identifier, $configuration);
77  } catch (\‪Exception $exception) {
78  $this->markTestSkipped($exception->getMessage());
79  }
80  }
81 
86  protected function ‪mergeSiteConfiguration(
87  string $identifier,
88  array $overrides
89  ) {
90  $siteConfiguration = new ‪SiteConfiguration(
91  $this->instancePath . '/typo3conf/sites/'
92  );
93  $configuration = $siteConfiguration->load($identifier);
94  $configuration = array_merge($configuration, $overrides);
95  try {
96  $siteConfiguration->write($identifier, $configuration);
97  } catch (\‪Exception $exception) {
98  $this->markTestSkipped($exception->getMessage());
99  }
100  }
101 
107  protected function ‪buildSiteConfiguration(
108  int $rootPageId,
109  string $base = ''
110  ): array {
111  return [
112  'rootPageId' => $rootPageId,
113  'base' => $base,
114  ];
115  }
116 
123  string $identifier,
124  string $base
125  ): array {
126  $configuration = $this->buildLanguageConfiguration($identifier, $base);
127  $configuration['typo3Language'] = 'default';
128  $configuration['flag'] = 'global';
129  unset($configuration['fallbackType'], $configuration['fallbacks']);
130  return $configuration;
131  }
132 
140  protected function ‪buildLanguageConfiguration(
141  string $identifier,
142  string $base,
143  array $fallbackIdentifiers = [],
144  string $fallbackType = null
145  ): array {
146  $preset = $this->resolveLanguagePreset($identifier);
147 
148  $configuration = [
149  'languageId' => $preset['id'],
150  'title' => $preset['title'],
151  'navigationTitle' => $preset['title'],
152  'base' => $base,
153  'locale' => $preset['locale'],
154  'iso-639-1' => $preset['iso'],
155  'hreflang' => $preset['hrefLang'],
156  'direction' => $preset['direction'],
157  'typo3Language' => $preset['iso'],
158  'flag' => $preset['iso'],
159  'fallbackType' => $fallbackType ?? (empty($fallbackIdentifiers) ? 'strict' : 'fallback'),
160  ];
161 
162  if (!empty($fallbackIdentifiers)) {
163  $fallbackIds = array_map(
164  function (string $fallbackIdentifier) {
165  $preset = $this->resolveLanguagePreset($fallbackIdentifier);
166  return $preset['id'];
167  },
168  $fallbackIdentifiers
169  );
170  $configuration['fallbackType'] = $fallbackType ?? 'fallback';
171  $configuration['fallbacks'] = implode(',', $fallbackIds);
172  }
173 
174  return $configuration;
175  }
176 
183  string $handler,
184  array $codes
185  ): array {
186  if ($handler === 'Page') {
187  $baseConfiguration = [
188  'errorContentSource' => '404',
189  ];
190  } elseif ($handler === 'Fluid') {
191  $baseConfiguration = [
192  'errorFluidTemplate' => 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/FluidError.html',
193  'errorFluidTemplatesRootPath' => '',
194  'errorFluidLayoutsRootPath' => '',
195  'errorFluidPartialsRootPath' => '',
196  ];
197  } elseif ($handler === 'PHP') {
198  $baseConfiguration = [
199  'errorPhpClassFQCN' => PhpError::class,
200  ];
201  } else {
202  throw new \LogicException(
203  sprintf('Invalid handler "%s"', $handler),
204  1533894782
205  );
206  }
207 
208  $baseConfiguration['errorHandler'] = $handler;
209 
210  return array_map(
211  function (int $code) use ($baseConfiguration) {
212  $baseConfiguration['errorCode'] = $code;
213  return $baseConfiguration;
214  },
215  $codes
216  );
217  }
218 
223  protected function ‪resolveLanguagePreset(string $identifier)
224  {
225  if (!isset(static::LANGUAGE_PRESETS[$identifier])) {
226  throw new \LogicException(
227  sprintf('Undefined preset identifier "%s"', $identifier),
228  1533893665
229  );
230  }
231  return static::LANGUAGE_PRESETS[$identifier];
232  }
233 
241  protected function ‪applyInstructions(InternalRequest $request, AbstractInstruction ...$instructions): InternalRequest
242  {
243  $modifiedInstructions = [];
244 
245  foreach ($instructions as $instruction) {
246  $identifier = $instruction->getIdentifier();
247  if (isset($modifiedInstructions[$identifier]) || $request->getInstruction($identifier) !== null) {
248  $modifiedInstructions[$identifier] = $this->mergeInstruction(
249  $modifiedInstructions[$identifier] ?? $request->getInstruction($identifier),
250  $instruction
251  );
252  } else {
253  $modifiedInstructions[$identifier] = $instruction;
254  }
255  }
256 
257  return $request->withInstructions($modifiedInstructions);
258  }
259 
265  protected function ‪mergeInstruction(AbstractInstruction $current, AbstractInstruction $other): AbstractInstruction
266  {
267  if (get_class($current) !== get_class($other)) {
268  throw new \LogicException('Cannot merge different instruction types', 1565863174);
269  }
270 
271  if ($current instanceof TypoScriptInstruction) {
273  $typoScript = array_replace_recursive(
274  $current->getTypoScript() ?? [],
275  $other->getTypoScript() ?? []
276  );
277  $constants = array_replace_recursive(
278  $current->getConstants() ?? [],
279  $other->getConstants() ?? []
280  );
281  if ($typoScript !== []) {
282  $current = $current->withTypoScript($typoScript);
283  }
284  if ($constants !== []) {
285  $current = $current->withConstants($constants);
286  }
287  return $current;
288  }
289 
290  if ($current instanceof ArrayValueInstruction) {
292  $array = array_merge_recursive($current->getArray(), $other->getArray());
293  return $current->withArray($array);
294  }
295 
296  return $current;
297  }
298 }
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\failIfArrayIsNotEmpty
‪static failIfArrayIsNotEmpty(array $items)
Definition: SiteBasedTestTrait.php:38
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildErrorHandlingConfiguration
‪array buildErrorHandlingConfiguration(string $handler, array $codes)
Definition: SiteBasedTestTrait.php:182
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling
Definition: SiteBasedTestTrait.php:3
‪TYPO3\CMS\Core\Tests\Functional\Fixtures\Frontend\PhpError
Definition: PhpError.php:27
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:34
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildLanguageConfiguration
‪array buildLanguageConfiguration(string $identifier, string $base, array $fallbackIdentifiers=[], string $fallbackType=null)
Definition: SiteBasedTestTrait.php:140
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:56
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:38
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪array buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:122
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\resolveLanguagePreset
‪mixed resolveLanguagePreset(string $identifier)
Definition: SiteBasedTestTrait.php:223
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\mergeInstruction
‪AbstractInstruction mergeInstruction(AbstractInstruction $current, AbstractInstruction $other)
Definition: SiteBasedTestTrait.php:265
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\applyInstructions
‪InternalRequest applyInstructions(InternalRequest $request, AbstractInstruction ... $instructions)
Definition: SiteBasedTestTrait.php:241
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪array buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:107
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\mergeSiteConfiguration
‪mergeSiteConfiguration(string $identifier, array $overrides)
Definition: SiteBasedTestTrait.php:86