‪TYPO3CMS  11.5
UriPrefixRenderingTest.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\InternalRequest;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 class ‪UriPrefixRenderingTest extends FunctionalTestCase
27 {
29 
33  private ‪$definedResources = [
34  'absoluteCSS' => '/typo3/sysext/backend/Resources/Public/Css/backend.css',
35  'relativeCSS' => 'typo3/sysext/backend/Resources/Public/Css/backend.css',
36  'extensionCSS' => 'EXT:rte_ckeditor/Resources/Public/Css/contents.css',
37  'externalCSS' => 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
38  'absoluteJS' => '/typo3/sysext/backend/Resources/Public/JavaScript/backend.js',
39  'relativeJS' => 'typo3/sysext/core/Resources/Public/JavaScript/Contrib/autosize.js',
40  'extensionJS' => 'EXT:core/Resources/Public/JavaScript/Contrib/jquery.autocomplete.js',
41  'externalJS' => 'https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.min.js',
42  'localImage' => 'typo3/sysext/frontend/Resources/Public/Icons/Extension.svg',
43  ];
44 
48  private ‪$resolvedResources = [
49  'relativeCSS' => 'typo3/sysext/backend/Resources/Public/Css/backend.css',
50  'extensionCSS' => 'typo3/sysext/rte_ckeditor/Resources/Public/Css/contents.css',
51  'externalCSS' => 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
52  'relativeJS' => 'typo3/sysext/core/Resources/Public/JavaScript/Contrib/autosize.js',
53  'extensionJS' => 'typo3/sysext/core/Resources/Public/JavaScript/Contrib/jquery.autocomplete.js',
54  'externalJS' => 'https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.min.js',
55  'localImage' => 'typo3/sysext/frontend/Resources/Public/Icons/Extension.svg',
56  ];
57 
58  protected ‪$coreExtensionsToLoad = ['rte_ckeditor'];
59 
61  'FE' => [
62  'cacheHash' => [
63  'excludedParameters' => ['testAbsRefPrefix', 'testCompressor'],
64  ],
65  ],
66  'SC_OPTIONS' => [
67  'Core/TypoScript/TemplateService' => [
68  'runThroughTemplatesPostProcessing' => [
69  'FunctionalTest' => \TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Hook\TypoScriptInstructionModifier::class . '->apply',
70  ],
71  ],
72  ],
73  ];
74 
75  protected const ‪LANGUAGE_PRESETS = [
76  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8', 'iso' => 'en', 'hrefLang' => 'en-US'],
77  ];
78 
79  protected function ‪setUp(): void
80  {
81  parent::setUp();
82  $this->importCsvDataSet(__DIR__ . '/../../../../core/Tests/Functional/Fixtures/pages.csv');
84  'test',
85  $this->‪buildSiteConfiguration(1, '/'),
86  [
87  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
88  ],
89  $this->‪buildErrorHandlingConfiguration('Fluid', [404]),
90  );
91  $this->setUpFrontendRootPage(
92  1,
93  ['EXT:frontend/Tests/Functional/Rendering/Fixtures/UriPrefixRenderingTest.typoscript']
94  );
96  1,
97  $this->‪compileTypoScriptConstants($this->definedResources)
98  );
99  }
100 
101  public function ‪urisAreRenderedUsingAbsRefPrefixDataProvider(): array
102  {
103  return [
104  // no compression settings
105  'none - none' => [
106  'none', 'none',
107  [
108  'absolute' => '"{{CANDIDATE}}"',
109  'local' => '"{{CANDIDATE}}"',
110  'relative' => '"{{CANDIDATE}}\?\d+"',
111  'extension' => '"{{CANDIDATE}}\?\d+"',
112  'external' => '"{{CANDIDATE}}"',
113  ],
114  ],
115  'auto - none' => [
116  'auto', 'none',
117  [
118  'absolute' => '"{{CANDIDATE}}"',
119  'local' => '"/{{CANDIDATE}}"',
120  'relative' => '"/{{CANDIDATE}}\?\d+"',
121  'extension' => '"/{{CANDIDATE}}\?\d+"',
122  'external' => '"{{CANDIDATE}}"',
123  ],
124  ],
125  'absolute-with-host - none' => [
126  'absolute-with-host', 'none',
127  [
128  'absolute' => '"{{CANDIDATE}}"',
129  'local' => '"http://localhost/{{CANDIDATE}}"',
130  'relative' => '"http://localhost/{{CANDIDATE}}\?\d+"',
131  'extension' => '"http://localhost/{{CANDIDATE}}\?\d+"',
132  'external' => '"{{CANDIDATE}}"',
133  ],
134  ],
135  'absolute-without-host - none' => [
136  'absolute-without-host', 'none',
137  [
138  'absolute' => '"{{CANDIDATE}}"',
139  'local' => '"/{{CANDIDATE}}"',
140  'relative' => '"/{{CANDIDATE}}\?\d+"',
141  'extension' => '"/{{CANDIDATE}}\?\d+"',
142  'external' => '"{{CANDIDATE}}"',
143  ],
144  ],
145  // concatenation
146  'none - concatenate' => [
147  'none', 'concatenate',
148  [
149  '!absolute' => '{{CANDIDATE}}',
150  '!relative' => '{{CANDIDATE}}',
151  '!extension' => '{{CANDIDATE}}',
152  'absolute' => '"typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
153  'local' => '"{{CANDIDATE}}"',
154  'relative' => '"typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
155  'extension' => '"typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
156  'external' => '"{{CANDIDATE}}"',
157  ],
158  ],
159  'auto - concatenate' => [
160  'auto', 'concatenate',
161  [
162  '!absolute' => '{{CANDIDATE}}',
163  '!relative' => '{{CANDIDATE}}',
164  '!extension' => '{{CANDIDATE}}',
165  'absolute' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
166  'local' => '"/{{CANDIDATE}}"',
167  'relative' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
168  'extension' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
169  'external' => '"{{CANDIDATE}}"',
170  ],
171  ],
172  'absolute-with-host - concatenate' => [
173  'absolute-with-host', 'concatenate',
174  [
175  '!absolute' => '{{CANDIDATE}}',
176  '!relative' => '{{CANDIDATE}}',
177  '!extension' => '{{CANDIDATE}}',
178  'absolute' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
179  'local' => '"http://localhost/{{CANDIDATE}}"',
180  'relative' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
181  'extension' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
182  'external' => '"{{CANDIDATE}}"',
183  ],
184  ],
185  'absolute-without-host - concatenate' => [
186  'absolute-without-host', 'concatenate',
187  [
188  '!absolute' => '{{CANDIDATE}}',
189  '!relative' => '{{CANDIDATE}}',
190  '!extension' => '{{CANDIDATE}}',
191  'absolute' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
192  'local' => '"/{{CANDIDATE}}"',
193  'relative' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
194  'extension' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
195  'external' => '"{{CANDIDATE}}"',
196  ],
197  ],
198  // compression
199  'none - compress' => [
200  'none', 'compress',
201  [
202  '!absolute' => '{{CANDIDATE}}',
203  '!relative' => '{{CANDIDATE}}',
204  '!extension' => '{{CANDIDATE}}',
205  'absolute' => '"typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
206  'local' => '"{{CANDIDATE}}"',
207  'relative' => '"typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
208  'extension' => '"typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
209  'external' => '"{{CANDIDATE}}"',
210  ],
211  ],
212  'auto - compress' => [
213  'auto', 'compress',
214  [
215  '!absolute' => '{{CANDIDATE}}',
216  '!relative' => '{{CANDIDATE}}',
217  '!extension' => '{{CANDIDATE}}',
218  'absolute' => '"/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
219  'local' => '"/{{CANDIDATE}}"',
220  'relative' => '"/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
221  'extension' => '"/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
222  'external' => '"{{CANDIDATE}}"',
223  ],
224  ],
225  'absolute-with-host - compress' => [
226  'absolute-with-host', 'compress',
227  [
228  '!absolute' => '{{CANDIDATE}}',
229  '!relative' => '{{CANDIDATE}}',
230  '!extension' => '{{CANDIDATE}}',
231  'absolute' => '"http://localhost/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
232  'local' => '"http://localhost/{{CANDIDATE}}"',
233  'relative' => '"http://localhost/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
234  'extension' => '"http://localhost/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
235  'external' => '"{{CANDIDATE}}"',
236  ],
237  ],
238  'absolute-without-host - compress' => [
239  'absolute-without-host', 'compress',
240  [
241  '!absolute' => '{{CANDIDATE}}',
242  '!relative' => '{{CANDIDATE}}',
243  '!extension' => '{{CANDIDATE}}',
244  'absolute' => '"/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
245  'local' => '"/{{CANDIDATE}}"',
246  'relative' => '"/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
247  'extension' => '"/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
248  'external' => '"{{CANDIDATE}}"',
249  ],
250  ],
251  // concatenation & compression
252  'none - concatenate-and-compress' => [
253  'none', 'concatenate-and-compress',
254  [
255  '!absolute' => '{{CANDIDATE}}',
256  '!relative' => '{{CANDIDATE}}',
257  '!extension' => '{{CANDIDATE}}',
258  'absolute' => '"typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
259  'local' => '"{{CANDIDATE}}"',
260  'relative' => '"typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
261  'extension' => '"typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
262  'external' => '"{{CANDIDATE}}"',
263  ],
264  ],
265  'auto - concatenate-and-compress' => [
266  'auto', 'concatenate-and-compress',
267  [
268  '!absolute' => '{{CANDIDATE}}',
269  '!relative' => '{{CANDIDATE}}',
270  '!extension' => '{{CANDIDATE}}',
271  'absolute' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
272  'local' => '"/{{CANDIDATE}}"',
273  'relative' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
274  'extension' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
275  'external' => '"{{CANDIDATE}}"',
276  ],
277  ],
278  'absolute-with-host - concatenate-and-compress' => [
279  'absolute-with-host', 'concatenate-and-compress',
280  [
281  '!absolute' => '{{CANDIDATE}}',
282  '!relative' => '{{CANDIDATE}}',
283  '!extension' => '{{CANDIDATE}}',
284  'absolute' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
285  'local' => '"http://localhost/{{CANDIDATE}}"',
286  'relative' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
287  'extension' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
288  'external' => '"{{CANDIDATE}}"',
289  ],
290  ],
291  'absolute-without-host - concatenate-and-compress' => [
292  'absolute-without-host', 'concatenate-and-compress',
293  [
294  '!absolute' => '{{CANDIDATE}}',
295  '!relative' => '{{CANDIDATE}}',
296  '!extension' => '{{CANDIDATE}}',
297  'absolute' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
298  'local' => '"/{{CANDIDATE}}"',
299  'relative' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
300  'extension' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
301  'external' => '"{{CANDIDATE}}"',
302  ],
303  ],
304  ];
305  }
306 
314  public function ‪urisAreRenderedUsingAbsRefPrefix(string $absRefPrefixAspect, string $compressorAspect, array $expectations): void
315  {
316  $response = $this->executeFrontendSubRequest(
317  (new InternalRequest())->withQueryParameters([
318  'id' => 1,
319  'testAbsRefPrefix' => $absRefPrefixAspect,
320  'testCompressor' => $compressorAspect,
321  ])
322  );
323  $content = (string)$response->getBody();
324 
325  foreach ($expectations as $type => $expectation) {
326  $shallExist = true;
327  if (strpos($type, '!') === 0) {
328  $shallExist = false;
329  $type = substr($type, 1);
330  }
331  $candidates = array_map(
332  function (string $candidateKey) {
333  return $this->resolvedResources[$candidateKey];
334  },
335  array_filter(
336  array_keys($this->resolvedResources),
337  static function (string $candidateKey) use ($type) {
338  return strpos($candidateKey, $type) === 0;
339  }
340  )
341  );
342  foreach ($candidates as $candidate) {
343  $pathInfo = pathinfo($candidate);
344  $pattern = str_replace(
345  [
346  '{{CANDIDATE}}',
347  '{{CANDIDATE-FILENAME}}',
348  '{{CANDIDATE-EXTENSION}}',
349  ],
350  [
351  preg_quote($candidate, '#'),
352  preg_quote($pathInfo['filename'], '#'),
353  preg_quote($pathInfo['extension'] ?? '', '#'),
354  ],
355  $expectation
356  );
357 
358  if ($shallExist) {
359  self::assertMatchesRegularExpression(
360  '#' . $pattern . '#',
361  $content
362  );
363  } else {
364  self::assertDoesNotMatchRegularExpression(
365  '#' . $pattern . '#',
366  $content
367  );
368  }
369  }
370  }
371  }
372 
380  protected function ‪setTypoScriptConstantsToTemplateRecord(int $pageId, string $constants, bool $append = false): void
381  {
382  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_template');
383 
384  $template = $connection->select(['uid', 'constants'], 'sys_template', ['pid' => $pageId, 'root' => 1])->fetchAssociative();
385  if (empty($template)) {
386  self::fail('Cannot find root template on page with id: "' . $pageId . '"');
387  }
388  $updateFields = [];
389  $updateFields['constants'] = ($append ? $template['constants'] . LF : '') . $constants;
390  $connection->update(
391  'sys_template',
392  $updateFields,
393  ['uid' => $template['uid']]
394  );
395  }
396 
401  protected function ‪compileTypoScriptConstants(array $constants): string
402  {
403  $lines = [];
404  foreach ($constants as $constantName => $constantValue) {
405  $lines[] = $constantName . ' = ' . $constantValue;
406  }
407  return implode(PHP_EOL, $lines);
408  }
409 }
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildErrorHandlingConfiguration
‪array buildErrorHandlingConfiguration(string $handler, array $codes)
Definition: SiteBasedTestTrait.php:186
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest\$resolvedResources
‪string[] $resolvedResources
Definition: UriPrefixRenderingTest.php:45
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:36
‪TYPO3
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:58
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest\$configurationToUseInTestInstance
‪$configurationToUseInTestInstance
Definition: UriPrefixRenderingTest.php:57
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest\$definedResources
‪string[] $definedResources
Definition: UriPrefixRenderingTest.php:31
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest\urisAreRenderedUsingAbsRefPrefixDataProvider
‪urisAreRenderedUsingAbsRefPrefixDataProvider()
Definition: UriPrefixRenderingTest.php:98
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪array buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:126
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest\compileTypoScriptConstants
‪string compileTypoScriptConstants(array $constants)
Definition: UriPrefixRenderingTest.php:398
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest\setUp
‪setUp()
Definition: UriPrefixRenderingTest.php:76
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest\$coreExtensionsToLoad
‪$coreExtensionsToLoad
Definition: UriPrefixRenderingTest.php:55
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: UriPrefixRenderingTest.php:72
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest
Definition: UriPrefixRenderingTest.php:27
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest\setTypoScriptConstantsToTemplateRecord
‪setTypoScriptConstantsToTemplateRecord(int $pageId, string $constants, bool $append=false)
Definition: UriPrefixRenderingTest.php:377
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪array buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:111
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\UriPrefixRenderingTest\urisAreRenderedUsingAbsRefPrefix
‪urisAreRenderedUsingAbsRefPrefix(string $absRefPrefixAspect, string $compressorAspect, array $expectations)
Definition: UriPrefixRenderingTest.php:311
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering
Definition: LocalizedSiteContentRenderingTest.php:18