‪TYPO3CMS  ‪main
AbsoluteUriPrefixRenderingTest.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 PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
25 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
26 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
27 
28 final class ‪AbsoluteUriPrefixRenderingTest extends FunctionalTestCase
29 {
31 
32  protected array ‪$configurationToUseInTestInstance = [
33  'FE' => [
34  'cacheHash' => [
35  'excludedParameters' => ['useAbsoluteUrls', 'testCompressor'],
36  ],
37  ],
38  ];
39 
43  private array ‪$definedResources = [
44  'absoluteCSS' => '/typo3/sysext/backend/Resources/Public/Css/backend.css',
45  'relativeCSS' => 'typo3/sysext/backend/Resources/Public/Css/backend.css',
46  'extensionCSS' => 'EXT:rte_ckeditor/Resources/Public/Css/contents.css',
47  'externalCSS' => 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
48  'absoluteJS' => '/typo3/sysext/backend/Resources/Public/JavaScript/backend.js',
49  'relativeJS' => 'typo3/sysext/core/Resources/Public/JavaScript/Contrib/autosize.js',
50  'extensionJS' => 'EXT:core/Resources/Public/JavaScript/Contrib/jquery.js',
51  'externalJS' => 'https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.min.js',
52  'localImage' => 'typo3/sysext/frontend/Resources/Public/Icons/Extension.svg',
53  ];
54 
58  private array ‪$resolvedResources = [
59  'relativeCSS' => 'typo3/sysext/backend/Resources/Public/Css/backend.css',
60  'extensionCSS' => 'typo3/sysext/rte_ckeditor/Resources/Public/Css/contents.css',
61  'externalCSS' => 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
62  'relativeJS' => 'typo3/sysext/core/Resources/Public/JavaScript/Contrib/autosize.js',
63  'extensionJS' => 'typo3/sysext/core/Resources/Public/JavaScript/Contrib/jquery.js',
64  'externalJS' => 'https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.min.js',
65  'localImage' => 'typo3/sysext/frontend/Resources/Public/Icons/Extension.svg',
66  'link' => '/en/dummy-1-4-10',
67  ];
68 
69  protected array ‪$coreExtensionsToLoad = ['rte_ckeditor'];
70 
71  protected const ‪LANGUAGE_PRESETS = [
72  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8', 'iso' => 'en'],
73  ];
74 
75  protected function ‪setUp(): void
76  {
77  parent::setUp();
78  $this->importCsvDataSet(__DIR__ . '/../../../../core/Tests/Functional/Fixtures/pages.csv');
80  'test',
81  $this->‪buildSiteConfiguration(1, 'http://localhost/'),
82  [
83  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
84  ],
85  $this->‪buildErrorHandlingConfiguration('Fluid', [404]),
86  );
87  $this->setUpFrontendRootPage(
88  1,
89  ['EXT:frontend/Tests/Functional/Rendering/Fixtures/AbsoluteUriPrefixRenderingTest.typoscript']
90  );
92  1,
93  $this->‪compileTypoScriptConstants($this->definedResources)
94  );
95  }
96 
97  public static function ‪urisAreRenderedUsingForceAbsoluteUrlsDataProvider(): \Generator
98  {
99  // no compression settings
100  yield 'none - none' => [
101  'none', 'none',
102  [
103  'absolute' => '"/{{CANDIDATE}}"',
104  'local' => '"/{{CANDIDATE}}"',
105  'relative' => '"/{{CANDIDATE}}\?\d+"',
106  'extension' => '"/{{CANDIDATE}}\?\d+"',
107  'external' => '"{{CANDIDATE}}"',
108  'link' => 'href="{{CANDIDATE}}"',
109  ],
110  ];
111  yield 'with-prefix - none' => [
112  '1', 'none',
113  [
114  'absolute' => '"http://localhost/{{CANDIDATE}}"',
115  'local' => '"http://localhost/{{CANDIDATE}}"',
116  'relative' => '"http://localhost/{{CANDIDATE}}\?\d+"',
117  'extension' => '"http://localhost/{{CANDIDATE}}\?\d+"',
118  'external' => '"{{CANDIDATE}}"',
119  'link' => 'href="http://localhost{{CANDIDATE}}"',
120  ],
121  ];
122  // concatenation
123  yield 'none - concatenate' => [
124  '0', 'concatenate',
125  [
126  '!absolute' => '{{CANDIDATE}}',
127  '!relative' => '{{CANDIDATE}}',
128  '!extension' => '{{CANDIDATE}}',
129  'absolute' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
130  'local' => '"/{{CANDIDATE}}"',
131  'relative' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
132  'extension' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
133  'external' => '"{{CANDIDATE}}"',
134  'link' => 'href="{{CANDIDATE}}"',
135  ],
136  ];
137  yield 'with-prefix - concatenate' => [
138  '1', 'concatenate',
139  [
140  '!absolute' => 'http://localhost/{{CANDIDATE}}',
141  '!relative' => 'http://localhost/{{CANDIDATE}}',
142  '!extension' => 'http://localhost/{{CANDIDATE}}',
143  'absolute' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
144  'local' => '"http://localhost/{{CANDIDATE}}"',
145  'relative' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
146  'extension' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
147  'external' => '"{{CANDIDATE}}"',
148  'link' => 'href="http://localhost{{CANDIDATE}}"',
149  ],
150  ];
151  // compression
152  yield 'none - compress' => [
153  '0', 'compress',
154  [
155  '!absolute' => '{{CANDIDATE}}',
156  '!relative' => '/{{CANDIDATE}}',
157  '!extension' => '/{{CANDIDATE}}',
158  'absolute' => '"/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
159  'local' => '"/{{CANDIDATE}}"',
160  'relative' => '"/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
161  'extension' => '"/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
162  'external' => '"{{CANDIDATE}}"',
163  'link' => 'href="{{CANDIDATE}}"',
164  ],
165  ];
166  yield 'with-prefix - compress' => [
167  '1', 'compress',
168  [
169  '!absolute' => 'http://localhost/{{CANDIDATE}}',
170  '!relative' => 'http://localhost/{{CANDIDATE}}',
171  '!extension' => 'http://localhost/{{CANDIDATE}}',
172  'absolute' => '"http://localhost/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
173  'local' => '"http://localhost/{{CANDIDATE}}"',
174  'relative' => '"http://localhost/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
175  'extension' => '"http://localhost/typo3temp/assets/compressed/{{CANDIDATE-FILENAME}}-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
176  'external' => '"{{CANDIDATE}}"',
177  'link' => 'href="http://localhost{{CANDIDATE}}"',
178  ],
179  ];
180  // concatenation & compression
181  yield 'no prefix - concatenate-and-compress' => [
182  '0', 'concatenate-and-compress',
183  [
184  '!absolute' => '{{CANDIDATE}}',
185  '!relative' => '/{{CANDIDATE}}',
186  '!extension' => '/{{CANDIDATE}}',
187  'absolute' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
188  'local' => '"/{{CANDIDATE}}"',
189  'relative' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
190  'extension' => '"/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
191  'external' => '"{{CANDIDATE}}"',
192  'link' => 'href="{{CANDIDATE}}"',
193  ],
194  ];
195  yield 'with prefix - concatenate-and-compress' => [
196  '1', 'concatenate-and-compress',
197  [
198  '!absolute' => 'http://localhost/{{CANDIDATE}}',
199  '!relative' => 'http://localhost/{{CANDIDATE}}',
200  '!extension' => 'http://localhost/{{CANDIDATE}}',
201  'absolute' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
202  'local' => '"http://localhost/{{CANDIDATE}}"',
203  'relative' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
204  'extension' => '"http://localhost/typo3temp/assets/compressed/merged-[a-z0-9]+-[a-z0-9]+\.{{CANDIDATE-EXTENSION}}\?\d+"',
205  'external' => '"{{CANDIDATE}}"',
206  'link' => 'href="http://localhost{{CANDIDATE}}"',
207  ],
208  ];
209  }
210 
211  #[DataProvider('urisAreRenderedUsingForceAbsoluteUrlsDataProvider')]
212  #[Test]
213  public function ‪urisAreRenderedUsingAbsRefPrefix(string $useAbsoluteUrls, string $compressorAspect, array $expectations): void
214  {
215  $response = $this->executeFrontendSubRequest(
216  (new InternalRequest())->withQueryParameters([
217  'id' => 1,
218  'useAbsoluteUrls' => $useAbsoluteUrls,
219  'testCompressor' => $compressorAspect,
220  ])
221  );
222  $content = (string)$response->getBody();
223 
224  foreach ($expectations as $type => $expectation) {
225  $shallExist = true;
226  if (str_starts_with($type, '!')) {
227  $shallExist = false;
228  $type = substr($type, 1);
229  }
230  $candidates = array_map(
231  function (string $candidateKey) {
232  return $this->resolvedResources[$candidateKey];
233  },
234  array_filter(
235  array_keys($this->resolvedResources),
236  static function (string $candidateKey) use ($type) {
237  return str_starts_with($candidateKey, $type);
238  }
239  )
240  );
241  foreach ($candidates as $candidate) {
242  $pathInfo = pathinfo($candidate);
243  $pattern = str_replace(
244  [
245  '{{CANDIDATE}}',
246  '{{CANDIDATE-FILENAME}}',
247  '{{CANDIDATE-EXTENSION}}',
248  ],
249  [
250  preg_quote($candidate, '#'),
251  preg_quote($pathInfo['filename'], '#'),
252  preg_quote($pathInfo['extension'] ?? '', '#'),
253  ],
254  $expectation
255  );
256 
257  if ($shallExist) {
258  self::assertMatchesRegularExpression(
259  '#' . $pattern . '#',
260  $content
261  );
262  } else {
263  self::assertDoesNotMatchRegularExpression(
264  '#' . $pattern . '#',
265  $content
266  );
267  }
268  }
269  }
270  }
271 
275  protected function ‪setTypoScriptConstantsToTemplateRecord(int $pageId, string $constants, bool $append = false): void
276  {
277  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_template');
278 
279  $template = $connection->select(['uid', 'constants'], 'sys_template', ['pid' => $pageId, 'root' => 1])->fetchAssociative();
280  if (empty($template)) {
281  self::fail('Cannot find root template on page with id: "' . $pageId . '"');
282  }
283  $updateFields = [];
284  $updateFields['constants'] = ($append ? $template['constants'] . LF : '') . $constants;
285  $connection->update(
286  'sys_template',
287  $updateFields,
288  ['uid' => $template['uid']]
289  );
290  }
291 
292  protected function ‪compileTypoScriptConstants(array $constants): string
293  {
294  $lines = [];
295  foreach ($constants as $constantName => $constantValue) {
296  $lines[] = $constantName . ' = ' . $constantValue;
297  }
298  return implode(PHP_EOL, $lines);
299  }
300 }
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: AbsoluteUriPrefixRenderingTest.php:70
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest\setUp
‪setUp()
Definition: AbsoluteUriPrefixRenderingTest.php:74
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: AbsoluteUriPrefixRenderingTest.php:68
‪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\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest\urisAreRenderedUsingForceAbsoluteUrlsDataProvider
‪static urisAreRenderedUsingForceAbsoluteUrlsDataProvider()
Definition: AbsoluteUriPrefixRenderingTest.php:96
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:88
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest\$configurationToUseInTestInstance
‪array $configurationToUseInTestInstance
Definition: AbsoluteUriPrefixRenderingTest.php:31
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildErrorHandlingConfiguration
‪buildErrorHandlingConfiguration(string $handler, array $codes)
Definition: SiteBasedTestTrait.php:142
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest\$resolvedResources
‪array $resolvedResources
Definition: AbsoluteUriPrefixRenderingTest.php:57
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest
Definition: AbsoluteUriPrefixRenderingTest.php:29
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest\compileTypoScriptConstants
‪compileTypoScriptConstants(array $constants)
Definition: AbsoluteUriPrefixRenderingTest.php:291
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:98
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest\$definedResources
‪array $definedResources
Definition: AbsoluteUriPrefixRenderingTest.php:42
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering
Definition: AbsoluteUriPrefixRenderingTest.php:18
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest\setTypoScriptConstantsToTemplateRecord
‪setTypoScriptConstantsToTemplateRecord(int $pageId, string $constants, bool $append=false)
Definition: AbsoluteUriPrefixRenderingTest.php:274
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\AbsoluteUriPrefixRenderingTest\urisAreRenderedUsingAbsRefPrefix
‪urisAreRenderedUsingAbsRefPrefix(string $useAbsoluteUrls, string $compressorAspect, array $expectations)
Definition: AbsoluteUriPrefixRenderingTest.php:212