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