‪TYPO3CMS  10.4
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  ) {
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  );
74 
75  try {
76  // ensure no previous site configuration influences the test
77  ‪GeneralUtility::rmdir($this->instancePath . '/typo3conf/sites/' . $identifier, true);
78  $siteConfiguration->write($identifier, $configuration);
79  } catch (\‪Exception $exception) {
80  $this->markTestSkipped($exception->getMessage());
81  }
82  }
83 
88  protected function ‪mergeSiteConfiguration(
89  string $identifier,
90  array $overrides
91  ) {
92  $siteConfiguration = new ‪SiteConfiguration(
93  $this->instancePath . '/typo3conf/sites/'
94  );
95  $configuration = $siteConfiguration->load($identifier);
96  $configuration = array_merge($configuration, $overrides);
97  try {
98  $siteConfiguration->write($identifier, $configuration);
99  } catch (\‪Exception $exception) {
100  $this->markTestSkipped($exception->getMessage());
101  }
102  }
103 
109  protected function ‪buildSiteConfiguration(
110  int $rootPageId,
111  string $base = ''
112  ): array {
113  return [
114  'rootPageId' => $rootPageId,
115  'base' => $base,
116  ];
117  }
118 
125  string $identifier,
126  string $base
127  ): array {
128  $configuration = $this->buildLanguageConfiguration($identifier, $base);
129  $configuration['typo3Language'] = 'default';
130  $configuration['flag'] = 'global';
131  unset($configuration['fallbackType'], $configuration['fallbacks']);
132  return $configuration;
133  }
134 
142  protected function ‪buildLanguageConfiguration(
143  string $identifier,
144  string $base,
145  array $fallbackIdentifiers = [],
146  string $fallbackType = null
147  ): array {
148  $preset = $this->resolveLanguagePreset($identifier);
149 
150  $configuration = [
151  'languageId' => $preset['id'],
152  'title' => $preset['title'],
153  'navigationTitle' => $preset['title'],
154  'base' => $base,
155  'locale' => $preset['locale'],
156  'iso-639-1' => $preset['iso'],
157  'hreflang' => $preset['hrefLang'],
158  'direction' => $preset['direction'],
159  'typo3Language' => $preset['iso'],
160  'flag' => $preset['iso'],
161  'fallbackType' => $fallbackType ?? (empty($fallbackIdentifiers) ? 'strict' : 'fallback'),
162  ];
163 
164  if (!empty($fallbackIdentifiers)) {
165  $fallbackIds = array_map(
166  function (string $fallbackIdentifier) {
167  $preset = $this->resolveLanguagePreset($fallbackIdentifier);
168  return $preset['id'];
169  },
170  $fallbackIdentifiers
171  );
172  $configuration['fallbackType'] = $fallbackType ?? 'fallback';
173  $configuration['fallbacks'] = implode(',', $fallbackIds);
174  }
175 
176  return $configuration;
177  }
178 
185  string $handler,
186  array $codes
187  ): array {
188  if ($handler === 'Page') {
189  $baseConfiguration = [
190  'errorContentSource' => '404',
191  ];
192  } elseif ($handler === 'Fluid') {
193  $baseConfiguration = [
194  'errorFluidTemplate' => 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/FluidError.html',
195  'errorFluidTemplatesRootPath' => '',
196  'errorFluidLayoutsRootPath' => '',
197  'errorFluidPartialsRootPath' => '',
198  ];
199  } elseif ($handler === 'PHP') {
200  $baseConfiguration = [
201  'errorPhpClassFQCN' => PhpError::class,
202  ];
203  } else {
204  throw new \LogicException(
205  sprintf('Invalid handler "%s"', $handler),
206  1533894782
207  );
208  }
209 
210  $baseConfiguration['errorHandler'] = $handler;
211 
212  return array_map(
213  function (int $code) use ($baseConfiguration) {
214  $baseConfiguration['errorCode'] = $code;
215  return $baseConfiguration;
216  },
217  $codes
218  );
219  }
220 
225  protected function ‪resolveLanguagePreset(string $identifier)
226  {
227  if (!isset(static::LANGUAGE_PRESETS[$identifier])) {
228  throw new \LogicException(
229  sprintf('Undefined preset identifier "%s"', $identifier),
230  1533893665
231  );
232  }
233  return static::LANGUAGE_PRESETS[$identifier];
234  }
235 
243  protected function ‪applyInstructions(InternalRequest $request, AbstractInstruction ...$instructions): InternalRequest
244  {
245  $modifiedInstructions = [];
246 
247  foreach ($instructions as $instruction) {
248  $identifier = $instruction->getIdentifier();
249  if (isset($modifiedInstructions[$identifier]) || $request->getInstruction($identifier) !== null) {
250  $modifiedInstructions[$identifier] = $this->mergeInstruction(
251  $modifiedInstructions[$identifier] ?? $request->getInstruction($identifier),
252  $instruction
253  );
254  } else {
255  $modifiedInstructions[$identifier] = $instruction;
256  }
257  }
258 
259  return $request->withInstructions($modifiedInstructions);
260  }
261 
267  protected function ‪mergeInstruction(AbstractInstruction $current, AbstractInstruction $other): AbstractInstruction
268  {
269  if (get_class($current) !== get_class($other)) {
270  throw new \LogicException('Cannot merge different instruction types', 1565863174);
271  }
272 
273  if ($current instanceof TypoScriptInstruction) {
275  $typoScript = array_replace_recursive(
276  $current->getTypoScript() ?? [],
277  $other->getTypoScript() ?? []
278  );
279  $constants = array_replace_recursive(
280  $current->getConstants() ?? [],
281  $other->getConstants() ?? []
282  );
283  if ($typoScript !== []) {
284  $current = $current->withTypoScript($typoScript);
285  }
286  if ($constants !== []) {
287  $current = $current->withConstants($constants);
288  }
289  return $current;
290  }
291 
292  if ($current instanceof ArrayValueInstruction) {
294  $array = array_merge_recursive($current->getArray(), $other->getArray());
295  return $current->withArray($array);
296  }
297 
298  return $current;
299  }
300 }
‪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:184
‪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:22
‪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:142
‪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:41
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪array buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:124
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\resolveLanguagePreset
‪mixed resolveLanguagePreset(string $identifier)
Definition: SiteBasedTestTrait.php:225
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\mergeInstruction
‪AbstractInstruction mergeInstruction(AbstractInstruction $current, AbstractInstruction $other)
Definition: SiteBasedTestTrait.php:267
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\applyInstructions
‪InternalRequest applyInstructions(InternalRequest $request, AbstractInstruction ... $instructions)
Definition: SiteBasedTestTrait.php:243
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪array buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:109
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir($path, $removeNonEmpty=false)
Definition: GeneralUtility.php:2075
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\mergeSiteConfiguration
‪mergeSiteConfiguration(string $identifier, array $overrides)
Definition: SiteBasedTestTrait.php:88