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