‪TYPO3CMS  ‪main
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 Psr\Log\NullLogger;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 final class ‪ImageInfoTest extends UnitTestCase
29 {
30  protected bool ‪$resetSingletonInstances = true;
31 
35  public function ‪classImageInfoCanBeInstantiated(): void
36  {
37  $className = ImageInfo::class;
38  $classInstance = new ‪ImageInfo('FooFileName');
39  self::assertInstanceOf($className, $classInstance);
40  }
41 
46  {
47  $testFile = __DIR__ . '/../Fixture/html_file_with_pdf_ending.pdf';
48 
49  $exceptionIsLogged = function (array $context) {
50  self::assertEquals(
51  'Unsupported file html_file_with_pdf_ending.pdf (text/html)',
52  $context['exception']->getMessage()
53  );
54  return true;
55  };
56 
57  $loggerMock = $this->createMock(Logger::class);
58  $loggerMock->expects(self::once())->method('error')->with(self::isType('string'), self::callback($exceptionIsLogged));
59  $loggerMock->expects(self::once())->method('warning')
60  ->with('I could not retrieve the image size for file {file}', ['file' => $testFile]);
61 
62  $imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $testFile);
63  $imageInfo->setLogger($loggerMock);
64  self::assertEquals(0, $imageInfo->getHeight());
65  self::assertEquals(0, $imageInfo->getWidth());
66  }
67 
69  {
70  return [
71  ['Invalid XML.', 0, 0],
72  [
73  '<?xml version="1.0" encoding="utf-8"?>
74  <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [
75  <!ENTITY ns_a "http://ns.typo3.com/test/a/1.0/">
76  <!ENTITY ns_b "http://ns.typo3.com/test/b/1.0/">
77  ]>
78  <svg version="1.0"
79  xmlns:x="&ns_a;"
80  xmlns="http://www.w3.org/2000/svg"
81  xmlns:xlink="http://www.w3.org/1999/xlink"
82  xml:space="preserve"
83  x="0px" y="0px" viewBox="0 0 436 177">
84  <metadata>
85  <sfw xmlns="&ns_b;">
86  <slices></slices>
87  </sfw>
88  </metadata>
89  </svg>',
90  436,
91  177,
92  ],
93  ];
94  }
95 
100  public function ‪doesNotBreakOnImageInfoWithInvalidSvg(string $svg, int $width, int $height): void
101  {
102  $testDirectory = ‪Environment::getVarPath() . '/ImageTest';
103  $this->testFilesToDelete[] = $testDirectory;
104  $testFile = $testDirectory . '/test.svg';
105  if (!is_dir($testDirectory)) {
106  mkdir($testDirectory);
107  }
108  touch($testFile);
109  file_put_contents($testFile, $svg);
110 
111  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'] = [
112  'svg' => 'image/svg+xml',
113  'youtube' => 'video/youtube',
114  'vimeo' => 'video/vimeo',
115  ];
116 
117  $graphicalFunctionsMock = $this->createMock(GraphicalFunctions::class);
118  $graphicalFunctionsMock->method('imageMagickIdentify')->with($testFile)->willReturn(null);
119  GeneralUtility::addInstance(GraphicalFunctions::class, $graphicalFunctionsMock);
120 
121  $imageInfo = new ‪ImageInfo($testFile);
122  $imageInfo->setLogger(new NullLogger());
123 
124  self::assertSame($width, $imageInfo->getWidth());
125  self::assertSame($height, $imageInfo->getHeight());
126 
127  GeneralUtility::makeInstance(GraphicalFunctions::class);
128  }
129 
130  public static function ‪canDetectImageSizesDataProvider(): array
131  {
132  return [
133  'svg' => ['test.svg', 80, 80],
134  'jpg' => ['test.jpg', 600, 388],
135  'png' => ['test.png', 600, 388],
136  ];
137  }
138 
143  public function ‪canDetectImageSizes(string $file, int $width, int $height): void
144  {
145  $imageInfo = new ‪ImageInfo(__DIR__ . '/../Fixture/' . $file);
146  $imageInfo->setLogger(new NullLogger());
147 
148  self::assertSame($width, $imageInfo->getWidth());
149  self::assertSame($height, $imageInfo->getHeight());
150  }
151 }
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\canDetectImageSizes
‪canDetectImageSizes(string $file, int $width, int $height)
Definition: ImageInfoTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\doesNotBreakOnImageInfoWithInvalidSvgDataProvider
‪static doesNotBreakOnImageInfoWithInvalidSvgDataProvider()
Definition: ImageInfoTest.php:68
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ImageInfoTest.php:30
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\doesNotBreakOnImageInfoWithInvalidSvg
‪doesNotBreakOnImageInfoWithInvalidSvg(string $svg, int $width, int $height)
Definition: ImageInfoTest.php:100
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest
Definition: ImageInfoTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Type\File
Definition: ImageInfoTest.php:18
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions
Definition: GraphicalFunctions.php:33
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\doesNotBreakOnFileWithInvalidEnding
‪doesNotBreakOnFileWithInvalidEnding()
Definition: ImageInfoTest.php:45
‪TYPO3\CMS\Core\Type\File\ImageInfo
Definition: ImageInfo.php:28
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\classImageInfoCanBeInstantiated
‪classImageInfoCanBeInstantiated()
Definition: ImageInfoTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Type\File\ImageInfoTest\canDetectImageSizesDataProvider
‪static canDetectImageSizesDataProvider()
Definition: ImageInfoTest.php:130
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51