‪TYPO3CMS  11.5
ImageInfoTest.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 org\bovigo\vfs\vfsStream;
21 use Prophecy\Argument;
22 use Prophecy\PhpUnit\ProphecyTrait;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 class ‪ImageInfoTest extends UnitTestCase
33 {
34  use ProphecyTrait;
35 
39  public function ‪classImageInfoCanBeInstantiated(): void
40  {
41  $className = ImageInfo::class;
42  $classInstance = new ‪ImageInfo('FooFileName');
43  self::assertInstanceOf($className, $classInstance);
44  }
45 
49  public function ‪doesNotBreakOnFileWithInvalidEnding(): void
50  {
51  $this->resetSingletonInstances = true;
52 
53  $testFile = __DIR__ . '/../Fixture/html_file_with_pdf_ending.pdf';
54 
55  $exceptionIsLogged = function (array $context) {
56  self::assertEquals(
57  'Unsupported file html_file_with_pdf_ending.pdf (text/html)',
58  $context['exception']->getMessage()
59  );
60  return true;
61  };
62 
63  $loggerProphecy = $this->prophesize(Logger::class);
64  $loggerProphecy->error(Argument::type('string'), Argument::that($exceptionIsLogged))->shouldBeCalledOnce();
65  $loggerProphecy->warning('I could not retrieve the image size for file {file}', ['file' => $testFile])
66  ->shouldBeCalledOnce();
67 
68  $imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $testFile);
69  $imageInfo->setLogger($loggerProphecy->reveal());
70  self::assertEquals(0, $imageInfo->getHeight());
71  self::assertEquals(0, $imageInfo->getWidth());
72  }
73 
78  {
79  return [
80  ['Invalid XML.', 0, 0],
81  [
82  '<?xml version="1.0" encoding="utf-8"?>
83  <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [
84  <!ENTITY ns_a "http://ns.typo3.com/test/a/1.0/">
85  <!ENTITY ns_b "http://ns.typo3.com/test/b/1.0/">
86  ]>
87  <svg version="1.0"
88  xmlns:x="&ns_a;"
89  xmlns="http://www.w3.org/2000/svg"
90  xmlns:xlink="http://www.w3.org/1999/xlink"
91  xml:space="preserve"
92  x="0px" y="0px" viewBox="0 0 436 177">
93  <metadata>
94  <sfw xmlns="&ns_b;">
95  <slices></slices>
96  </sfw>
97  </metadata>
98  </svg>',
99  436,
100  177,
101  ],
102  ];
103  }
104 
112  public function ‪doesNotBreakOnImageInfoWithInvalidSvg(string $svg, int $width, int $height): void
113  {
114  $this->resetSingletonInstances = true;
115 
116  $root = vfsStream::setup('root');
117  $testFile = 'test.svg';
118  vfsStream::newFile($testFile)->at($root)->setContent($svg);
119 
120  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'] = [
121  'svg' => 'image/svg+xml',
122  'youtube' => 'video/youtube',
123  'vimeo' => 'video/vimeo',
124  ];
125 
126  $graphicalFunctionsProphecy = $this->prophesize(GraphicalFunctions::class);
127  $graphicalFunctionsProphecy->imageMagickIdentify($root->url() . '/' . $testFile)->willReturn(null);
128  GeneralUtility::addInstance(GraphicalFunctions::class, $graphicalFunctionsProphecy->reveal());
129 
130  $loggerProphecy = $this->prophesize(Logger::class);
131 
132  $imageInfo = new ‪ImageInfo($root->url() . '/' . $testFile);
133  $imageInfo->setLogger($loggerProphecy->reveal());
134 
135  self::assertSame($width, $imageInfo->getWidth());
136  self::assertSame($height, $imageInfo->getHeight());
137 
138  GeneralUtility::makeInstance(GraphicalFunctions::class);
139  }
140 
144  public function ‪canDetectImageSizesDataProvider(): array
145  {
146  return [
147  'svg' => ['test.svg', 80, 80],
148  'jpg' => ['test.jpg', 600, 388],
149  'png' => ['test.png', 600, 388],
150  ];
151  }
152 
160  public function ‪canDetectImageSizes(string $file, int $width, int $height): void
161  {
162  $logger = $this->prophesize(Logger::class)->reveal();
163  $imageInfo = new ‪ImageInfo(__DIR__ . '/../Fixture/' . $file);
164  $imageInfo->setLogger($logger);
165 
166  self::assertSame($width, $imageInfo->getWidth());
167  self::assertSame($height, $imageInfo->getHeight());
168  }
169 }
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\canDetectImageSizes
‪canDetectImageSizes(string $file, int $width, int $height)
Definition: ImageInfoTest.php:159
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\doesNotBreakOnImageInfoWithInvalidSvg
‪doesNotBreakOnImageInfoWithInvalidSvg(string $svg, int $width, int $height)
Definition: ImageInfoTest.php:111
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest
Definition: ImageInfoTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Type\File
Definition: ImageInfoTest.php:18
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions
Definition: GraphicalFunctions.php:37
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\doesNotBreakOnImageInfoWithInvalidSvgDataProvider
‪array doesNotBreakOnImageInfoWithInvalidSvgDataProvider()
Definition: ImageInfoTest.php:76
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\doesNotBreakOnFileWithInvalidEnding
‪doesNotBreakOnFileWithInvalidEnding()
Definition: ImageInfoTest.php:48
‪TYPO3\CMS\Core\Type\File\ImageInfo
Definition: ImageInfo.php:28
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\classImageInfoCanBeInstantiated
‪classImageInfoCanBeInstantiated()
Definition: ImageInfoTest.php:38
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\canDetectImageSizesDataProvider
‪array canDetectImageSizesDataProvider()
Definition: ImageInfoTest.php:143