‪TYPO3CMS  9.5
AbstractTestCase.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 
22 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\ArrayValueInstruction;
23 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
24 
28 abstract class ‪AbstractTestCase extends FunctionalTestCase
29 {
31 
32  protected const ‪ENCRYPTION_KEY = '4408d27a916d51e624b69af3554f516dbab61037a9f7b9fd6f81b4d3bedeccb6';
33 
34  protected const TYPO3_CONF_VARS = [
35  'SYS' => [
36  'encryptionKey' => ‪self::ENCRYPTION_KEY,
37  ],
38  'FE' => [
39  'cacheHash' => [
40  'requireCacheHashPresenceParameters' => ['value', 'testing[value]', 'tx_testing_link[value]'],
41  'excludedParameters' => ['L', 'tx_testing_link[excludedValue]']
42  ],
43  ]
44  ];
45 
46  protected const LANGUAGE_PRESETS = [
47  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8', 'iso' => 'en', 'hrefLang' => 'en-US', 'direction' => ''],
48  'FR' => ['id' => 1, 'title' => 'French', 'locale' => 'fr_FR.UTF8', 'iso' => 'fr', 'hrefLang' => 'fr-FR', 'direction' => ''],
49  'FR-CA' => ['id' => 2, 'title' => 'Franco-Canadian', 'locale' => 'fr_CA.UTF8', 'iso' => 'fr', 'hrefLang' => 'fr-CA', 'direction' => ''],
50  'ES' => ['id' => 3, 'title' => 'Spanish', 'locale' => 'es_ES.UTF8', 'iso' => 'es', 'hrefLang' => 'es-ES', 'direction' => ''],
51  ];
52 
56  protected $coreExtensionsToLoad = ['frontend', 'workspaces'];
57 
61  protected $pathsToLinkInTestInstance = [
62  'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/AdditionalConfiguration.php' => 'typo3conf/AdditionalConfiguration.php',
63  ];
64 
69  protected function wrapInArray(array $array): array
70  {
71  return array_map(
72  function ($item) {
73  return [$item];
74  },
75  $array
76  );
77  }
78 
83  protected function keysFromValues(array $array): array
84  {
85  return array_combine($array, $array);
86  }
87 
103  protected function keysFromTemplate(array $array, string $template, callable $callback = null): array
104  {
105  $keys = array_unique(
106  array_map(
107  function (array $values) use ($template, $callback) {
108  if ($callback !== null) {
109  $values = call_user_func($callback, $values);
110  }
111  return vsprintf($template, $values);
112  },
113  $array
114  )
115  );
116 
117  if (count($keys) !== count($array)) {
118  throw new \LogicException(
119  'Amount of generated keys does not match to item count.',
120  1534682840
121  );
122  }
123 
124  return array_combine($keys, $array);
125  }
126 
130  protected static function ‪failIfArrayIsNotEmpty(array $items): void
131  {
132  if (empty($items)) {
133  return;
134  }
135 
136  static::fail(
137  'Array was not empty as expected, but contained these items:' . LF
138  . '* ' . implode(LF . '* ', $items)
139  );
140  }
141 
146  protected static function generateCacheHash(string $uri): string
147  {
148  if (!isset(‪$GLOBALS['TYPO3_CONF_VARS'])) {
149  ‪$GLOBALS['TYPO3_CONF_VARS'] = [];
150  }
151 
152  $configuration = ‪$GLOBALS['TYPO3_CONF_VARS'];
154  ‪$GLOBALS['TYPO3_CONF_VARS'],
155  static::TYPO3_CONF_VARS
156  );
157 
158  $calculator = new CacheHashCalculator();
159  $parameters = $calculator->getRelevantParameters(
160  parse_url($uri, PHP_URL_QUERY)
161  );
162  $cacheHash = $calculator->calculateCacheHash($parameters);
163 
164  ‪$GLOBALS['TYPO3_CONF_VARS'] = $configuration;
165  return $cacheHash;
166  }
167 
172  protected function createTypoLinkUrlInstruction(array $typoScript): ArrayValueInstruction
173  {
174  return (new ArrayValueInstruction(LinkHandlingController::class))
175  ->withArray([
176  '10' => 'TEXT',
177  '10.' => [
178  'typolink.' => array_merge(
179  $typoScript,
180  ['returnLast' => 'url']
181  )
182  ]
183  ]);
184  }
185 
190  protected function createHierarchicalMenuProcessorInstruction(array $typoScript): ArrayValueInstruction
191  {
192  return (new ArrayValueInstruction(LinkHandlingController::class))
193  ->withArray([
194  '10' => 'FLUIDTEMPLATE',
195  '10.' => [
196  'file' => 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/FluidJson.html',
197  'dataProcessing.' => [
198  '1' => 'TYPO3\\CMS\\Frontend\\DataProcessing\\MenuProcessor',
199  '1.' => array_merge(
200  $typoScript,
201  ['as' => 'results']
202  )
203  ],
204  ],
205  ]);
206  }
207 
212  protected function createLanguageMenuProcessorInstruction(array $typoScript): ArrayValueInstruction
213  {
214  return (new ArrayValueInstruction(LinkHandlingController::class))
215  ->withArray([
216  '10' => 'FLUIDTEMPLATE',
217  '10.' => [
218  'file' => 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/FluidJson.html',
219  'dataProcessing.' => [
220  '1' => 'TYPO3\\CMS\\Frontend\\DataProcessing\\LanguageMenuProcessor',
221  '1.' => array_merge(
222  $typoScript,
223  ['as' => 'results']
224  )
225  ],
226  ],
227  ]);
228  }
229 
237  protected function filterMenu(
238  array $menu,
239  array $keepNames = ['title', 'link']
240  ): array {
241  if (!in_array('children', $keepNames)) {
242  $keepNames[] = 'children';
243  }
244  return array_map(
245  function (array $menuItem) use ($keepNames) {
246  $menuItem = array_filter(
247  $menuItem,
248  function (string $name) use ($keepNames) {
249  return in_array($name, $keepNames);
250  },
251  ARRAY_FILTER_USE_KEY
252  );
253  if (is_array($menuItem['children'] ?? null)) {
254  $menuItem['children'] = $this->filterMenu(
255  $menuItem['children'],
256  $keepNames
257  );
258  }
259  return $menuItem;
260  },
261  $menu
262  );
263  }
264 }
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\failIfArrayIsNotEmpty
‪static failIfArrayIsNotEmpty(array $items)
Definition: SiteBasedTestTrait.php:38
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:34
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\AbstractTestCase\ENCRYPTION_KEY
‪const ENCRYPTION_KEY
Definition: AbstractTestCase.php:31
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\AbstractTestCase
Definition: AbstractTestCase.php:29
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:614
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling
Definition: AbstractTestCase.php:3
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator
Definition: CacheHashCalculator.php:24