‪TYPO3CMS  ‪main
SvgImageRenderingTest.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 ‪SvgImageRenderingTest extends FunctionalTestCase
29 {
31 
35  private array ‪$definedResources = [
36  'localImage1' => 'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest1.svg',
37  'localImage2' => 'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest2.svg',
38  'localImage3' => 'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest3.svg',
39  'localImage4' => 'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest4.svg',
40 
41  'localImage1Uid' => '1',
42  'localImage2Uid' => '2',
43  'localImage3Uid' => '3',
44  'localImage4Uid' => '4',
45 
46  'localImage1UidUncrop' => '6',
47  'localImage2UidUncrop' => '7',
48  'localImage3UidUncrop' => '8',
49  'localImage4UidUncrop' => '9',
50  ];
51 
52  protected array ‪$pathsToProvideInTestInstance = [
53  'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest1.svg' => 'fileadmin/user_upload/FALImageViewHelperTest1.svg',
54  'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest2.svg' => 'fileadmin/user_upload/FALImageViewHelperTest2.svg',
55  'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest3.svg' => 'fileadmin/user_upload/FALImageViewHelperTest3.svg',
56  'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest4.svg' => 'fileadmin/user_upload/FALImageViewHelperTest4.svg',
57  'typo3/sysext/fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest5.svg' => 'fileadmin/user_upload/FALImageViewHelperTest5.svg',
58  ];
59 
60  protected array ‪$additionalFoldersToCreate = [
61  '/fileadmin/user_upload',
62  ];
63 
64  protected array ‪$coreExtensionsToLoad = ['rte_ckeditor'];
65 
66  protected const ‪LANGUAGE_PRESETS = [
67  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8', 'iso' => 'en'],
68  ];
69 
70  protected function ‪setUp(): void
71  {
72  parent::setUp();
73  $this->importCsvDataSet(__DIR__ . '/../../../../core/Tests/Functional/Fixtures/pages.csv');
74  $this->importCSVDataSet(__DIR__ . '/../../../../fluid/Tests/Functional/Fixtures/crops.csv');
75 
77  'test',
78  $this->‪buildSiteConfiguration(1, '/'),
79  [
80  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
81  ],
82  $this->‪buildErrorHandlingConfiguration('Fluid', [404]),
83  );
84  $this->setUpFrontendRootPage(
85  1,
86  ['EXT:frontend/Tests/Functional/Rendering/Fixtures/SvgImageRenderingTest.typoscript']
87  );
89  1,
90  $this->‪compileTypoScriptConstants($this->definedResources)
91  );
92  }
93 
94  public static function ‪svgsAreRenderedWithTyposcriptDataProvider(): array
95  {
96  return [
97  'rendered svg assets contains' => [
98  [
99  '@<img src="/typo3temp/assets/_processed_/[0-9a-f]/[0-9a-f]/csm_ImageViewHelperTest1_.*\.svg" width="500" height="500"\s+alt=""\s+/?>@U',
100  '@<img src="/typo3temp/assets/_processed_/[0-9a-f]/[0-9a-f]/csm_ImageViewHelperTest2_.*\.svg" width="500" height="500"\s+alt=""\s+/?>@U',
101  '@<img src="/typo3temp/assets/_processed_/[0-9a-f]/[0-9a-f]/csm_ImageViewHelperTest3_.*\.svg" width="500" height="500"\s+alt=""\s+/?>@U',
102  '@<img src="/typo3temp/assets/_processed_/[0-9a-f]/[0-9a-f]/csm_ImageViewHelperTest4_.*\.svg" width="500" height="500"\s+alt=""\s+/?>@U',
103 
104  '@<img src="/typo3temp/assets/_processed_/[0-9a-f]/[0-9a-f]/csm_ImageViewHelperTest1_.*\.png" width="500" height="500"\s+alt=""\s+/?>@U',
105  // @todo should be 273x273 (or 274x274)
106  '@<img src="/typo3temp/assets/_processed_/[0-9a-f]/[0-9a-f]/csm_ImageViewHelperTest2_.*\.png" width="500" height="500"\s+alt=""\s+/?>@U',
107  '@<img src="/typo3temp/assets/_processed_/[0-9a-f]/[0-9a-f]/csm_ImageViewHelperTest3_.*\.png" width="500" height="500"\s+alt=""\s+/?>@U',
108  '@<img src="/typo3temp/assets/_processed_/[0-9a-f]/[0-9a-f]/csm_ImageViewHelperTest4_.*\.png" width="500" height="500"\s+alt=""\s+/?>@U',
109 
110  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest1_.*\.svg" width="231" height="238"\s+alt=""\s+/?>@U',
111  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest2_.*\.svg" width="241" height="60"\s+alt=""\s+/?>@U',
112  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest3_.*\.svg" width="176" height="320"\s+alt=""\s+/?>@U',
113  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest4_.*\.svg" width="114" height="131"\s+alt=""\s+/?>@U',
114 
115  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest1_.*\.png" width="231" height="238"\s+alt=""\s+/?>@U',
116  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest2_.*\.png" width="241" height="60"\s+alt=""\s+/?>@U',
117  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest3_.*\.png" width="176" height="320"\s+alt=""\s+/?>@U',
118  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest4_.*\.png" width="114" height="131"\s+alt=""\s+/?>@U',
119 
120  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest1_.*\.svg" width="500" height="500"\s+alt=""\s+/?>@U',
121  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest2_.*\.svg" width="500" height="500"\s+alt=""\s+/?>@U',
122  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest3_.*\.svg" width="500" height="500"\s+alt=""\s+/?>@U',
123  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest4_.*\.svg" width="500" height="500"\s+alt=""\s+/?>@U',
124 
125  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest1_.*\.png" width="500" height="500"\s+alt=""\s+/?>@U',
126  // @todo should be 273x273 (or 274x274)
127  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest2_.*\.png" width="500" height="500"\s+alt=""\s+/?>@U',
128  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest3_.*\.png" width="500" height="500"\s+alt=""\s+/?>@U',
129  '@<img src="/fileadmin/_processed_/[0-9a-f]/[0-9a-f]/csm_FALImageViewHelperTest4_.*\.png" width="500" height="500"\s+alt=""\s+/?>@U',
130  ],
131  ],
132  ];
133  }
134 
135  #[DataProvider('svgsAreRenderedWithTyposcriptDataProvider')]
136  #[Test]
137  public function ‪svgsAreRenderedWithTyposcript(array $expectedAssets): void
138  {
139  $response = $this->executeFrontendSubRequest(
140  (new InternalRequest())->withQueryParameters([
141  'id' => 1,
142  ])
143  );
144  $content = (string)$response->getBody();
145 
146  preg_match('@<body>(.+)</body>@imsU', $content, $bodyContent);
147  self::assertIsArray($bodyContent);
148 
149  foreach ($expectedAssets as $expectedAsset) {
150  self::assertMatchesRegularExpression($expectedAsset, $bodyContent[1]);
151  }
152  }
153 
157  protected function ‪setTypoScriptConstantsToTemplateRecord(int $pageId, string $constants, bool $append = false): void
158  {
159  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_template');
160 
161  $template = $connection->select(['uid', 'constants'], 'sys_template', ['pid' => $pageId, 'root' => 1])->fetchAssociative();
162  if (empty($template)) {
163  self::fail('Cannot find root template on page with id: "' . $pageId . '"');
164  }
165  $updateFields = [];
166  $updateFields['constants'] = ($append ? $template['constants'] . LF : '') . $constants;
167  $connection->update(
168  'sys_template',
169  $updateFields,
170  ['uid' => $template['uid']]
171  );
172  }
173 
174  protected function ‪compileTypoScriptConstants(array $constants): string
175  {
176  $lines = [];
177  foreach ($constants as $constantName => $constantValue) {
178  $lines[] = $constantName . ' = ' . $constantValue;
179  }
180  return implode(PHP_EOL, $lines);
181  }
182 }
‪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\SvgImageRenderingTest\setTypoScriptConstantsToTemplateRecord
‪setTypoScriptConstantsToTemplateRecord(int $pageId, string $constants, bool $append=false)
Definition: SvgImageRenderingTest.php:156
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:88
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\SvgImageRenderingTest\$additionalFoldersToCreate
‪array $additionalFoldersToCreate
Definition: SvgImageRenderingTest.php:59
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\SvgImageRenderingTest\svgsAreRenderedWithTyposcriptDataProvider
‪static svgsAreRenderedWithTyposcriptDataProvider()
Definition: SvgImageRenderingTest.php:93
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\SvgImageRenderingTest\svgsAreRenderedWithTyposcript
‪svgsAreRenderedWithTyposcript(array $expectedAssets)
Definition: SvgImageRenderingTest.php:136
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildErrorHandlingConfiguration
‪buildErrorHandlingConfiguration(string $handler, array $codes)
Definition: SiteBasedTestTrait.php:142
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\SvgImageRenderingTest\$pathsToProvideInTestInstance
‪array $pathsToProvideInTestInstance
Definition: SvgImageRenderingTest.php:51
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\SvgImageRenderingTest
Definition: SvgImageRenderingTest.php:29
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\SvgImageRenderingTest\compileTypoScriptConstants
‪compileTypoScriptConstants(array $constants)
Definition: SvgImageRenderingTest.php:173
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\SvgImageRenderingTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: SvgImageRenderingTest.php:65
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\SvgImageRenderingTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: SvgImageRenderingTest.php:63
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:98
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Frontend\Tests\Functional\Rendering\SvgImageRenderingTest\$definedResources
‪array $definedResources
Definition: SvgImageRenderingTest.php:34
‪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\SvgImageRenderingTest\setUp
‪setUp()
Definition: SvgImageRenderingTest.php:69