‪TYPO3CMS  ‪main
GifBuilderTest.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;
31 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
32 
33 final class ‪GifBuilderTest extends FunctionalTestCase
34 {
35  private function ‪setupFullTestEnvironment(): void
36  {
37  $this->importCSVDataSet(__DIR__ . '/../Fixtures/sys_file_storage.csv');
38  $this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users.csv');
39  $this->setUpBackendUser(1);
40  ‪$GLOBALS['TYPO3_REQUEST'] = (new ‪ServerRequest('https://www.example.com/'))
41  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
42  ‪$GLOBALS['TSFE'] = $this->createMock(TypoScriptFrontendController::class);
44  }
45 
49  private function ‪simulateCliRequestInComposerMode(): void
50  {
53  true,
54  true,
56  ‪Environment::getPublicPath() . '/public',
60  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
61  );
62  }
63 
64  public static function ‪fileExtensionDataProvider(): array
65  {
66  return [
67  'jpg' => ['jpg'],
68  'png' => ['png'],
69  'gif' => ['gif'],
70  'webp' => ['webp'],
71  ];
72  }
73 
74  #[DataProvider('fileExtensionDataProvider')]
75  #[Test]
76  public function ‪buildSimpleGifBuilderImageInComposerMode(string $fileExtension): void
77  {
79 
80  $conf = [
81  'XY' => '10,10',
82  'format' => $fileExtension,
83  ];
84 
85  $gifBuilder = new ‪GifBuilder();
86  $gifBuilder->start($conf, []);
87  $imageResource = $gifBuilder->gifBuild();
88 
89  self::assertFileDoesNotExist(‪Environment::getProjectPath() . '/' . $imageResource->getPublicUrl());
90  self::assertFileExists(‪Environment::getPublicPath() . '/' . $imageResource->getPublicUrl());
91  self::assertEquals($fileExtension, $imageResource->getExtension());
92  }
93 
94  #[Test]
96  {
99 
100  copy(
101  __DIR__ . '/../Fixtures/Images/kasper-skarhoj1.jpg',
102  ‪Environment::getPublicPath() . '/fileadmin/kasper-skarhoj-gifbuilder.jpg'
103  );
104 
105  $contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
106  $result = $contentObjectRenderer->cObjGetSingle(
107  'IMAGE',
108  [
109  'file' => 'GIFBUILDER',
110  'file.' => [
111  'XY' => '[10.w],[10.h]',
112  'format' => 'jpg',
113 
114  '10' => 'IMAGE',
115  '10.' => [
116  'file' => 'fileadmin/kasper-skarhoj-gifbuilder.jpg',
117  ],
118  ],
119  ]
120  );
121  self::assertStringStartsWith('<img src="typo3temp/assets/images/csm_kasper-skarhoj-gifbuilder_', $result);
122  }
123 
124  #[Test]
126  {
129 
130  copy(
131  __DIR__ . '/../Fixtures/Images/kasper-skarhoj1.jpg',
132  ‪Environment::getPublicPath() . '/fileadmin/kasper-skarhoj-gifbuilder-imageresource.jpg'
133  );
134 
135  $contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
136  $result = $contentObjectRenderer->cObjGetSingle(
137  'IMG_RESOURCE',
138  [
139  'file' => 'GIFBUILDER',
140  'file.' => [
141  'XY' => '[10.w],[10.h]',
142  'format' => 'jpg',
143 
144  '10' => 'IMAGE',
145  '10.' => [
146  'file' => 'fileadmin/kasper-skarhoj-gifbuilder-imageresource.jpg',
147  ],
148  ],
149  ]
150  );
151  self::assertStringStartsWith('typo3temp/assets/images/csm_kasper-skarhoj-gifbuilder-imageresource_', $result);
152  }
153 
154  #[Test]
156  {
159 
160  copy(
161  __DIR__ . '/../Fixtures/Images/kasper-skarhoj1.jpg',
162  ‪Environment::getPublicPath() . '/fileadmin/kasper-skarhoj-gifbuilder.jpg'
163  );
164 
165  $contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
166  $result = $contentObjectRenderer->cObjGetSingle(
167  'IMAGE',
168  [
169  'file' => 'GIFBUILDER',
170  'file.' => [
171  'XY' => '[10.w],[10.h]',
172  'format' => 'jpg',
173 
174  '10' => 'IMAGE',
175  '10.' => [
176  'file' => 'fileadmin/kasper-skarhoj-gifbuilder.jpg',
177  ],
178  '20' => 'IMAGE',
179  '20.' => [
180  'offset' => '0,500',
181  'XY' => '[mask.w],40',
182 
183  'file' => 'GIFBUILDER',
184  'file.' => [
185  'XY' => '400,60',
186  'backColor' => '#cccccc',
187  ],
188 
189  'mask' => 'GIFBUILDER',
190  'mask.' => [
191  'XY' => '[10.w]+55,60',
192  'backColor' => '#cccccc',
193 
194  '10' => 'TEXT',
195  '10.' => [
196  'text' => 'Kasper Skårhøj',
197  'fontColor' => '#111111',
198  'fontSize' => '20',
199  'offset' => '20,40',
200  ],
201  ],
202  ],
203  ],
204  ]
205  );
206  self::assertStringStartsWith('<img src="typo3temp/', $result);
207  }
208 
212  #[Test]
213  public function ‪overlayImagesHasStableHash(): void
214  {
216 
217  copy(
218  __DIR__ . '/../Fixtures/Images/kasper-skarhoj1.jpg',
219  ‪Environment::getPublicPath() . '/fileadmin/kasper-skarhoj1.jpg'
220  );
221 
222  $storageRepository = GeneralUtility::makeInstance(StorageRepository::class)->findByUid(1);
223  $file = $storageRepository->getFile('kasper-skarhoj1.jpg');
224 
225  self::assertInstanceOf(File::class, $file);
226  self::assertFalse($file->isMissing());
227 
228  $conf = [
229  'XY' => '[10.w],[10.h]',
230  'format' => 'jpg',
231  'quality' => 88,
232  '10' => 'IMAGE',
233  '10.' => [
234  'file' => $file,
235  'file.' => [
236  'width' => 300,
237  ],
238  ],
239  '30' => 'IMAGE',
240  '30.' => [
241  'file' => $file,
242  'file.' => [
243  'align' => 'l,t',
244  'width' => 100,
245  ],
246  ],
247  ];
248 
249  $gifBuilder = new ‪GifBuilder();
250  $gifBuilder->start($conf, []);
251  $setup1 = $gifBuilder->setup;
252  $imageResource1 = $gifBuilder->gifBuild();
253 
254  // Recreate a fresh GifBuilder instance, to catch inconsistencies in hashing for different instances
255  $gifBuilder = new ‪GifBuilder();
256  $gifBuilder->start($conf, []);
257  $setup2 = $gifBuilder->setup;
258  $imageResource2 = $gifBuilder->gifBuild();
259 
260  self::assertSame($setup1, $setup2, 'The Setup resulting from two equal configurations must be equal');
261  self::assertSame($imageResource1->getPublicUrl(), $imageResource2->getPublicUrl());
262  }
263 }
‪TYPO3\CMS\Frontend\Tests\Functional\Imaging\GifBuilderTest\setupFullTestEnvironment
‪setupFullTestEnvironment()
Definition: GifBuilderTest.php:35
‪TYPO3\CMS\Frontend\Tests\Functional\Imaging\GifBuilderTest\simulateCliRequestInComposerMode
‪simulateCliRequestInComposerMode()
Definition: GifBuilderTest.php:49
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Frontend\Tests\Functional\Imaging\GifBuilderTest\buildImageInCommandLineInterfaceAndComposerMode
‪buildImageInCommandLineInterfaceAndComposerMode()
Definition: GifBuilderTest.php:95
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Frontend\Tests\Functional\Imaging\GifBuilderTest\getImageResourceInCommandLineInterfaceAndComposerMode
‪getImageResourceInCommandLineInterfaceAndComposerMode()
Definition: GifBuilderTest.php:125
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static getCurrentScript()
Definition: Environment.php:220
‪TYPO3\CMS\Frontend\Tests\Functional\Imaging
Definition: GifBuilderTest.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep(string $directory)
Definition: GeneralUtility.php:1654
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static getConfigPath()
Definition: Environment.php:212
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:160
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Frontend\Tests\Functional\Imaging\GifBuilderTest\overlayImagesHasStableHash
‪overlayImagesHasStableHash()
Definition: GifBuilderTest.php:213
‪TYPO3\CMS\Core\Core\Environment\initialize
‪static initialize(ApplicationContext $context, bool $cli, bool $composerMode, string $projectPath, string $publicPath, string $varPath, string $configPath, string $currentScript, string $os)
Definition: Environment.php:100
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Frontend\Tests\Functional\Imaging\GifBuilderTest\fileExtensionDataProvider
‪static fileExtensionDataProvider()
Definition: GifBuilderTest.php:64
‪TYPO3\CMS\Frontend\Imaging\GifBuilder
Definition: GifBuilder.php:58
‪TYPO3\CMS\Frontend\Tests\Functional\Imaging\GifBuilderTest\buildImageWithMaskInCommandLineInterfaceAndComposerMode
‪buildImageWithMaskInCommandLineInterfaceAndComposerMode()
Definition: GifBuilderTest.php:155
‪TYPO3\CMS\Frontend\Tests\Functional\Imaging\GifBuilderTest\buildSimpleGifBuilderImageInComposerMode
‪buildSimpleGifBuilderImageInComposerMode(string $fileExtension)
Definition: GifBuilderTest.php:76
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:58
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Tests\Functional\Imaging\GifBuilderTest
Definition: GifBuilderTest.php:34
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static getContext()
Definition: Environment.php:128
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static isWindows()
Definition: Environment.php:276