‪TYPO3CMS  ‪main
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 
20 use Psr\EventDispatcher\EventDispatcherInterface;
24 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\AbstractInstruction;
25 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\ArrayValueInstruction;
26 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\TypoScriptInstruction;
27 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
28 
37 {
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 
50  protected function ‪writeSiteConfiguration(
51  string ‪$identifier,
52  array $site = [],
53  array ‪$languages = [],
54  array $errorHandling = []
55  ): void {
56  $configuration = $site;
57  if (!empty(‪$languages)) {
58  $configuration['languages'] = ‪$languages;
59  }
60  if (!empty($errorHandling)) {
61  $configuration['errorHandling'] = $errorHandling;
62  }
63  $siteConfiguration = new ‪SiteConfiguration(
64  $this->instancePath . '/typo3conf/sites/',
65  $this->get(EventDispatcherInterface::class),
66  $this->get('cache.core')
67  );
68 
69  try {
70  // ensure no previous site configuration influences the test
71  ‪GeneralUtility::rmdir($this->instancePath . '/typo3conf/sites/' . ‪$identifier, true);
72  $siteConfiguration->write(‪$identifier, $configuration);
73  } catch (\‪Exception $exception) {
74  $this->markTestSkipped($exception->getMessage());
75  }
76  }
77 
78  protected function ‪mergeSiteConfiguration(
79  string ‪$identifier,
80  array $overrides
81  ): void {
82  $siteConfiguration = new ‪SiteConfiguration(
83  $this->instancePath . '/typo3conf/sites/',
84  $this->get(EventDispatcherInterface::class),
85  $this->get('cache.core')
86  );
87  $configuration = $siteConfiguration->load(‪$identifier);
88  $configuration = array_merge($configuration, $overrides);
89  try {
90  $siteConfiguration->write(‪$identifier, $configuration);
91  } catch (\‪Exception $exception) {
92  $this->markTestSkipped($exception->getMessage());
93  }
94  }
95 
96  protected function ‪buildSiteConfiguration(
97  int $rootPageId,
98  string $base = ''
99  ): array {
100  return [
101  'rootPageId' => $rootPageId,
102  'base' => $base,
103  ];
104  }
105 
107  string ‪$identifier,
108  string $base
109  ): array {
110  $configuration = $this->buildLanguageConfiguration(‪$identifier, $base);
111  $configuration['flag'] = 'global';
112  unset($configuration['fallbackType'], $configuration['fallbacks']);
113  return $configuration;
114  }
115 
116  protected function ‪buildLanguageConfiguration(
117  string ‪$identifier,
118  string $base,
119  array $fallbackIdentifiers = [],
120  string $fallbackType = null
121  ): array {
122  $preset = $this->resolveLanguagePreset(‪$identifier);
123 
124  $configuration = [
125  'languageId' => $preset['id'],
126  'title' => $preset['title'],
127  'navigationTitle' => $preset['title'],
128  'websiteTitle' => $preset['websiteTitle'] ?? '',
129  'base' => $base,
130  'locale' => $preset['locale'],
131  'flag' => $preset['iso'] ?? '',
132  'fallbackType' => $fallbackType ?? (empty($fallbackIdentifiers) ? 'strict' : 'fallback'),
133  ];
134 
135  if (!empty($fallbackIdentifiers)) {
136  $fallbackIds = array_map(
137  function (string $fallbackIdentifier) {
138  $preset = $this->resolveLanguagePreset($fallbackIdentifier);
139  return $preset['id'];
140  },
141  $fallbackIdentifiers
142  );
143  $configuration['fallbackType'] = $fallbackType ?? 'fallback';
144  $configuration['fallbacks'] = implode(',', $fallbackIds);
145  }
146 
147  return $configuration;
148  }
149 
151  string $handler,
152  array $codes
153  ): array {
154  if ($handler === 'Page') {
155  // This implies you cannot test both 404 and 403 in the same test.
156  // Fixing that requires much deeper changes to the testing harness,
157  // as the structure here is only a portion of the config array structure.
158  if (in_array(404, $codes, true)) {
159  $baseConfiguration = [
160  'errorContentSource' => 't3://page?uid=404',
161  ];
162  } elseif (in_array(403, $codes, true)) {
163  $baseConfiguration = [
164  'errorContentSource' => 't3://page?uid=403',
165  ];
166  }
167  } elseif ($handler === 'Fluid') {
168  $baseConfiguration = [
169  'errorFluidTemplate' => 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/FluidError.html',
170  'errorFluidTemplatesRootPath' => '',
171  'errorFluidLayoutsRootPath' => '',
172  'errorFluidPartialsRootPath' => '',
173  ];
174  } elseif ($handler === 'PHP') {
175  $baseConfiguration = [
176  'errorPhpClassFQCN' => PhpError::class,
177  ];
178  } else {
179  throw new \LogicException(
180  sprintf('Invalid handler "%s"', $handler),
181  1533894782
182  );
183  }
184 
185  $baseConfiguration['errorHandler'] = $handler;
186 
187  return array_map(
188  static function (int $code) use ($baseConfiguration) {
189  $baseConfiguration['errorCode'] = $code;
190  return $baseConfiguration;
191  },
192  $codes
193  );
194  }
195 
199  protected function ‪resolveLanguagePreset(string ‪$identifier)
200  {
201  if (!isset(static::LANGUAGE_PRESETS[‪$identifier])) {
202  throw new \LogicException(
203  sprintf('Undefined preset identifier "%s"', ‪$identifier),
204  1533893665
205  );
206  }
207  return static::LANGUAGE_PRESETS[‪$identifier];
208  }
209 
213  protected function ‪applyInstructions(InternalRequest $request, AbstractInstruction ...$instructions): InternalRequest
214  {
215  $modifiedInstructions = [];
216 
217  foreach ($instructions as $instruction) {
218  ‪$identifier = $instruction->getIdentifier();
219  if (isset($modifiedInstructions[‪$identifier]) || $request->getInstruction(‪$identifier) !== null) {
220  $modifiedInstructions[‪$identifier] = $this->mergeInstruction(
221  $modifiedInstructions[‪$identifier] ?? $request->getInstruction(‪$identifier),
222  $instruction
223  );
224  } else {
225  $modifiedInstructions[‪$identifier] = $instruction;
226  }
227  }
228 
229  return $request->withInstructions($modifiedInstructions);
230  }
231 
232  protected function ‪mergeInstruction(AbstractInstruction $current, AbstractInstruction $other): AbstractInstruction
233  {
234  if (get_class($current) !== get_class($other)) {
235  throw new \LogicException('Cannot merge different instruction types', 1565863174);
236  }
237 
238  if ($current instanceof TypoScriptInstruction) {
240  $typoScript = array_replace_recursive(
241  $current->getTypoScript() ?? [],
242  $other->getTypoScript() ?? []
243  );
244  $constants = array_replace_recursive(
245  $current->getConstants() ?? [],
246  $other->getConstants() ?? []
247  );
248  if ($typoScript !== []) {
249  $current = $current->withTypoScript($typoScript);
250  }
251  if ($constants !== []) {
252  $current = $current->withConstants($constants);
253  }
254  return $current;
255  }
256 
257  if ($current instanceof ArrayValueInstruction) {
259  $array = array_merge_recursive($current->getArray(), $other->getArray());
260  return $current->withArray($array);
261  }
262 
263  return $current;
264  }
265 }
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\failIfArrayIsNotEmpty
‪static failIfArrayIsNotEmpty(array $items)
Definition: SiteBasedTestTrait.php:38
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildLanguageConfiguration
‪buildLanguageConfiguration(string $identifier, string $base, array $fallbackIdentifiers=[], string $fallbackType=null)
Definition: SiteBasedTestTrait.php:116
‪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
‪$languages
‪$languages
Definition: updateIsoDatabase.php:104
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:37
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:50
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:96
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\mergeInstruction
‪mergeInstruction(AbstractInstruction $current, AbstractInstruction $other)
Definition: SiteBasedTestTrait.php:232
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\applyInstructions
‪applyInstructions(InternalRequest $request, AbstractInstruction ... $instructions)
Definition: SiteBasedTestTrait.php:213
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:47
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildErrorHandlingConfiguration
‪buildErrorHandlingConfiguration(string $handler, array $codes)
Definition: SiteBasedTestTrait.php:150
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\resolveLanguagePreset
‪mixed resolveLanguagePreset(string $identifier)
Definition: SiteBasedTestTrait.php:199
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir(string $path, bool $removeNonEmpty=false)
Definition: GeneralUtility.php:1698
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:106
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\mergeSiteConfiguration
‪mergeSiteConfiguration(string $identifier, array $overrides)
Definition: SiteBasedTestTrait.php:78
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37