‪TYPO3CMS  ‪main
ThumbnailControllerTest.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\MockObject\MockObject;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 final class ‪ThumbnailControllerTest extends UnitTestCase
31 {
32  protected ‪ThumbnailController&MockObject ‪$subject;
33 
34  protected static array ‪$parameters = [
35  'fileId' => 123,
36  'configuration' => [
37  'width' => 64,
38  'height' => 64,
39  ],
40  ];
41 
42  protected function ‪setUp(): void
43  {
44  parent::setUp();
45  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
46  = '4408d27a916d51e624b69af3554f516dbab61037a9f7b9fd6f81b4d3bedeccb6';
47  $this->subject = $this->createPartialMock(
48  ThumbnailController::class,
49  ['generateThumbnail']
50  );
51  }
52 
59  public function ‪exceptionIsThrownOnInvalidHMAC(string $hmac = null): void
60  {
61  $this->expectException(\InvalidArgumentException::class);
62  $this->expectExceptionCode(1534484203);
63 
64  $queryParameters = [
65  'parameters' => json_encode(static::$parameters),
66  'hmac' => $hmac,
67  ];
68 
69  $request = (new ‪ServerRequest())
70  ->withQueryParams($queryParameters);
71  $this->subject->render($request);
72  }
73 
74  public static function ‪exceptionIsThrownOnInvalidHMACDataProvider(): array
75  {
76  return [
77  'null' => [null],
78  'empty' => [''],
79  'invalid' => ['invalid'],
80  ];
81  }
82 
89  public function ‪generateThumbnailIsInvoked(array ‪$parameters = null): void
90  {
91  $this->subject->expects(self::once())
92  ->method('generateThumbnail')
93  ->willReturn(new ‪Response());
94 
95  $queryParameters = [
96  'parameters' => json_encode(‪$parameters),
97  'hmac' => ‪GeneralUtility::hmac(
98  json_encode(‪$parameters),
99  ThumbnailController::class
100  ),
101  ];
102 
103  $request = (new ‪ServerRequest())
104  ->withQueryParams($queryParameters);
105  self::assertInstanceOf(
106  Response::class,
107  $this->subject->render($request)
108  );
109  }
110 
111  public static function ‪generateThumbnailIsInvokedDataProvider(): array
112  {
113  return [
114  'null' => [null],
115  'empty array' => [[]],
116  'parameters' => [static::$parameters],
117  ];
118  }
119 }
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File
Definition: FileControllerTest.php:18
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\ThumbnailControllerTest\$subject
‪ThumbnailController &MockObject $subject
Definition: ThumbnailControllerTest.php:32
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\ThumbnailControllerTest\exceptionIsThrownOnInvalidHMAC
‪exceptionIsThrownOnInvalidHMAC(string $hmac=null)
Definition: ThumbnailControllerTest.php:59
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\ThumbnailControllerTest\setUp
‪setUp()
Definition: ThumbnailControllerTest.php:42
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\ThumbnailControllerTest
Definition: ThumbnailControllerTest.php:31
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:32
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\ThumbnailControllerTest\generateThumbnailIsInvoked
‪generateThumbnailIsInvoked(array $parameters=null)
Definition: ThumbnailControllerTest.php:89
‪TYPO3\CMS\Core\Utility\GeneralUtility\hmac
‪static string hmac($input, $additionalSecret='')
Definition: GeneralUtility.php:475
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\ThumbnailControllerTest\$parameters
‪static array $parameters
Definition: ThumbnailControllerTest.php:34
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\ThumbnailControllerTest\generateThumbnailIsInvokedDataProvider
‪static generateThumbnailIsInvokedDataProvider()
Definition: ThumbnailControllerTest.php:111
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Backend\Controller\File\ThumbnailController
Definition: ThumbnailController.php:36
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\ThumbnailControllerTest\exceptionIsThrownOnInvalidHMACDataProvider
‪static exceptionIsThrownOnInvalidHMACDataProvider()
Definition: ThumbnailControllerTest.php:74