‪TYPO3CMS  ‪main
ShowImageControllerTest.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 Masterminds\HTML5;
21 use PHPUnit\Framework\Attributes\DataProvider;
22 use PHPUnit\Framework\Attributes\Test;
23 use PHPUnit\Framework\MockObject\MockObject;
24 use Psr\Http\Message\ServerRequestInterface;
32 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
33 
34 final class ‪ShowImageControllerTest extends FunctionalTestCase
35 {
36  private const ‪ENCRYPTION_KEY = '4408d27a916d51e624b69af3554f516dbab61037a9f7b9fd6f81b4d3bedeccb6';
38  private ‪ResourceStorage&MockObject ‪$storage;
39  private ‪ShowImageController&MockObject ‪$subject;
40 
41  protected function ‪setUp(): void
42  {
43  parent::setUp();
44 
45  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = ‪self::ENCRYPTION_KEY;
46  $this->resourceFactory = $this->getMockBuilder(ResourceFactory::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  $this->storage = $this->getMockBuilder(ResourceStorage::class)
50  ->disableOriginalConstructor()
51  ->getMock();
52  $this->subject = $this->getMockBuilder(ShowImageController::class)
53  ->onlyMethods(['processImage'])
54  ->getMock();
55  GeneralUtility::setSingletonInstance(ResourceFactory::class, $this->resourceFactory);
56  }
57 
58  protected function ‪tearDown(): void
59  {
60  GeneralUtility::removeSingletonInstance(ResourceFactory::class, $this->resourceFactory);
61  unset($this->resourceFactory, $this->storage, $this->subject);
62  parent::tearDown();
63  }
64 
65  public static function ‪contentIsGeneratedForLocalFilesDataProvider(): \Generator
66  {
67  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = ‪self::ENCRYPTION_KEY;
68  $fileId = 13;
69  $parameters = [];
70  $serializedParameters = serialize($parameters);
71  $jsonEncodedParameters = json_encode($parameters);
72  $hashService = GeneralUtility::makeInstance(HashService::class);
73  yield 'numeric fileId, json encoded' => [
74  $fileId,
75  [
76  'file' => $fileId,
77  'parameters' => [$jsonEncodedParameters],
78  'md5' => $hashService->hmac(implode('|', [$fileId, $jsonEncodedParameters]), 'tx_cms_showpic'),
79  ],
80  ];
81  yield 'numeric fileId, outdated (valid) PHP encoded' => [
82  $fileId,
83  [
84  'file' => $fileId,
85  'parameters' => [$serializedParameters],
86  'md5' => $hashService->hmac(implode('|', [$fileId, $serializedParameters]), 'tx_cms_showpic'),
87  ],
88  ];
89  }
90 
94  #[DataProvider('contentIsGeneratedForLocalFilesDataProvider')]
95  #[Test]
96  public function ‪contentIsGeneratedForLocalFiles(int $fileId, array $queryParams): void
97  {
98  $storageDriver = 'Local';
99 
100  $this->storage->expects(self::atLeastOnce())
101  ->method('getDriverType')
102  ->willReturn($storageDriver);
103  $file = $this->‪buildFile('/local-file/' . $fileId, $this->storage);
104  $processedFile = $this->‪buildProcessedFile('/fileadmin/local-file/' . $fileId);
105  $this->resourceFactory->expects(self::atLeastOnce())
106  ->method('getFileObject')
107  ->with($fileId)
108  ->willReturn($file);
109  $this->subject->expects(self::once())
110  ->method('processImage')
111  ->willReturn($processedFile);
112 
113  $request = $this->‪buildRequest($queryParams);
114  $response = $this->subject->processRequest($request);
115  $document = (new HTML5())->loadHTML((string)$response->getBody());
116 
117  $titles = $document->getElementsByTagName('title');
118  $images = $document->getElementsByTagName('img');
119  self::assertSame('fileProperty::title', $titles->item(0)->nodeValue);
120  self::assertSame('/fileadmin/local-file/13', $images->item(0)->getAttribute('src'));
121  self::assertSame('fileProperty::alternative', $images->item(0)->getAttribute('alt'));
122  self::assertSame('fileProperty::title', $images->item(0)->getAttribute('title'));
123  self::assertSame('processedProperty::width', $images->item(0)->getAttribute('width'));
124  self::assertSame('processedProperty::height', $images->item(0)->getAttribute('height'));
125  }
126 
130  private function ‪buildRequest(array $queryParams): ServerRequestInterface&MockObject
131  {
132  $request = $this->createMock(ServerRequestInterface::class);
133  $request->method('getQueryParams')
134  ->willReturn($queryParams);
135 
136  return $request;
137  }
138 
140  {
141  $file = $this->createMock(FileInterface::class);
142  $file->method('getStorage')
143  ->willReturn(‪$storage);
144  $file->method('getIdentifier')
145  ->willReturn(‪$identifier);
146  $file->method('getProperty')
147  ->willReturnCallback($this->‪buildRoundTripClosure('fileProperty'));
148 
149  return $file;
150  }
151 
152  private function ‪buildProcessedFile(string ‪$publicUrl): ‪ProcessedFile&MockObject
153  {
154  $processedFile = $this->getMockBuilder(ProcessedFile::class)
155  ->disableOriginalConstructor()
156  ->getMock();
157  $processedFile
158  ->method('getPublicUrl')
159  ->willReturn(‪$publicUrl);
160  $processedFile
161  ->method('getProperty')
162  ->with(self::isType('string'))
163  ->willReturnCallback($this->‪buildRoundTripClosure('processedProperty'));
164 
165  return $processedFile;
166  }
167 
168  private function ‪buildRoundTripClosure(string $prefix): \Closure
169  {
170  return static function (string ...$args) use ($prefix): string {
171  return sprintf('%s::%s', $prefix, implode(',', ‪$args));
172  };
173  }
174 }
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\$subject
‪ShowImageController &MockObject $subject
Definition: ShowImageControllerTest.php:39
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\tearDown
‪tearDown()
Definition: ShowImageControllerTest.php:58
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest
Definition: ShowImageControllerTest.php:35
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\$resourceFactory
‪ResourceFactory &MockObject $resourceFactory
Definition: ShowImageControllerTest.php:37
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\contentIsGeneratedForLocalFilesDataProvider
‪static contentIsGeneratedForLocalFilesDataProvider()
Definition: ShowImageControllerTest.php:65
‪TYPO3\CMS\Webhooks\Message\$publicUrl
‪identifier readonly string readonly string $publicUrl
Definition: FileUpdatedMessage.php:36
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Frontend\Tests\Functional\Controller
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\contentIsGeneratedForLocalFiles
‪contentIsGeneratedForLocalFiles(int $fileId, array $queryParams)
Definition: ShowImageControllerTest.php:96
‪$args
‪$args
Definition: validateRstFiles.php:258
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\buildFile
‪buildFile(string $identifier, ResourceStorage $storage)
Definition: ShowImageControllerTest.php:139
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\$storage
‪ResourceStorage &MockObject $storage
Definition: ShowImageControllerTest.php:38
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\buildRoundTripClosure
‪buildRoundTripClosure(string $prefix)
Definition: ShowImageControllerTest.php:168
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:129
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\setUp
‪setUp()
Definition: ShowImageControllerTest.php:41
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\buildProcessedFile
‪buildProcessedFile(string $publicUrl)
Definition: ShowImageControllerTest.php:152
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\ENCRYPTION_KEY
‪const ENCRYPTION_KEY
Definition: ShowImageControllerTest.php:36
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Crypto\HashService
Definition: HashService.php:27
‪TYPO3\CMS\Frontend\Tests\Functional\Controller\ShowImageControllerTest\buildRequest
‪buildRequest(array $queryParams)
Definition: ShowImageControllerTest.php:130
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Frontend\Controller\ShowImageController
Definition: ShowImageController.php:50