‪TYPO3CMS  11.5
ImageScriptServiceTest.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 
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 class ‪ImageScriptServiceTest extends UnitTestCase
33 {
37  protected ‪$resetSingletonInstances = true;
38 
39  protected ‪ImageService ‪$subject;
40 
44  protected function ‪setUp(): void
45  {
46  parent::setUp();
47  $resourceFactory = $this->createMock(ResourceFactory::class);
48  $this->subject = new ‪ImageService($resourceFactory);
49  $_SERVER['HTTP_HOST'] = 'foo.bar';
50  }
51 
55  public function ‪fileIsUnwrappedFromReferenceForProcessing(): void
56  {
57  $reference = $this->getMockBuilder(FileReference::class)->disableOriginalConstructor()->getMock();
58  $file = $this->createMock(File::class);
59  $processedFile = $this->createMock(ProcessedFile::class);
60  $file->expects(self::once())->method('process')->willReturn($processedFile);
61  $reference->expects(self::once())->method('getOriginalFile')->willReturn($file);
62  $processedFile->expects(self::once())->method('getOriginalFile')->willReturn($file);
63  $processedFile->expects(self::atLeastOnce())->method('getPublicUrl')->willReturn('https://example.com/foo.png');
64 
65  $this->subject->applyProcessingInstructions($reference, []);
66  }
67 
72  {
73  return [
74  'with scheme' => ['http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
75  'scheme relative' => ['//foo.bar/img.jpg', '//foo.bar/img.jpg'],
76  'without scheme' => ['/prefix/foo.bar/img.jpg', '/prefix/foo.bar/img.jpg'],
77  ];
78  }
79 
84  public function ‪prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected): void
85  {
86  ‪$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest())
87  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
88  ‪$GLOBALS['TSFE'] = new \stdClass();
89  ‪$GLOBALS['TSFE']->absRefPrefix = '/prefix/';
90 
91  $file = $this->createMock(File::class);
92  $file->expects(self::once())->method('getPublicUrl')->willReturn($imageUri);
93 
94  self::assertSame($expected, $this->subject->getImageUri($file));
95  }
96 
101  {
102  return [
103  'with scheme' => ['http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
104  'scheme relative' => ['//foo.bar/img.jpg', '//foo.bar/img.jpg'],
105  'without scheme' => ['/prefix/foo.bar/img.jpg', 'http://foo.bar/prefix/foo.bar/img.jpg'],
106  ];
107  }
108 
113  public function ‪prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl($imageUri, $expected): void
114  {
115  ‪$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest())
116  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
117  ‪$GLOBALS['TSFE'] = new \stdClass();
118  ‪$GLOBALS['TSFE']->absRefPrefix = '/prefix/';
119 
120  $file = $this->createMock(File::class);
121  $file->expects(self::once())->method('getPublicUrl')->willReturn($imageUri);
122 
123  self::assertSame($expected, $this->subject->getImageUri($file, true));
124  }
125 }
‪TYPO3\CMS\Extbase\Tests\Unit\Service
Definition: CacheServiceTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUriDataProvider
‪array prefixIsCorrectlyAppliedToGetImageUriDataProvider()
Definition: ImageScriptServiceTest.php:70
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest
Definition: ImageScriptServiceTest.php:33
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ImageScriptServiceTest.php:36
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUri
‪prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected)
Definition: ImageScriptServiceTest.php:83
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\setUp
‪setUp()
Definition: ImageScriptServiceTest.php:43
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:45
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\fileIsUnwrappedFromReferenceForProcessing
‪fileIsUnwrappedFromReferenceForProcessing()
Definition: ImageScriptServiceTest.php:54
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_FE
‪const REQUESTTYPE_FE
Definition: SystemEnvironmentBuilder.php:43
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider
‪array prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider()
Definition: ImageScriptServiceTest.php:99
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl
‪prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl($imageUri, $expected)
Definition: ImageScriptServiceTest.php:112
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\$subject
‪ImageService $subject
Definition: ImageScriptServiceTest.php:38