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