‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪ImageScriptServiceTest extends UnitTestCase
32 {
33  protected bool ‪$resetSingletonInstances = true;
34 
36 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43  $resourceFactory = $this->createMock(ResourceFactory::class);
44  $this->subject = new ‪ImageService($resourceFactory);
45  ‪$_SERVER['HTTP_HOST'] = 'foo.bar';
46  }
47 
48  #[Test]
50  {
51  $reference = $this->getMockBuilder(FileReference::class)->disableOriginalConstructor()->getMock();
52  $file = $this->createMock(File::class);
53  $processedFile = $this->createMock(ProcessedFile::class);
54  $file->expects(self::once())->method('process')->willReturn($processedFile);
55  $reference->expects(self::once())->method('getOriginalFile')->willReturn($file);
56  $processedFile->expects(self::once())->method('getOriginalFile')->willReturn($file);
57  $processedFile->expects(self::atLeastOnce())->method('getPublicUrl')->willReturn('https://example.com/foo.png');
58 
59  $this->subject->applyProcessingInstructions($reference, []);
60  }
61 
63  {
64  return [
65  'with scheme' => ['http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
66  'scheme relative' => ['//foo.bar/img.jpg', '//foo.bar/img.jpg'],
67  'without scheme' => ['/prefix/foo.bar/img.jpg', '/prefix/foo.bar/img.jpg'],
68  ];
69  }
70 
71  #[DataProvider('prefixIsCorrectlyAppliedToGetImageUriDataProvider')]
72  #[Test]
73  public function ‪prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected): void
74  {
75  ‪$GLOBALS['TYPO3_REQUEST'] = (new ‪ServerRequest())
76  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
77  ‪$GLOBALS['TSFE'] = new \stdClass();
78  ‪$GLOBALS['TSFE']->absRefPrefix = '/prefix/';
79 
80  $file = $this->createMock(File::class);
81  $file->expects(self::once())->method('getPublicUrl')->willReturn($imageUri);
82 
83  self::assertSame($expected, $this->subject->getImageUri($file));
84  }
85 
87  {
88  return [
89  'with scheme' => ['http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
90  'scheme relative' => ['//foo.bar/img.jpg', '//foo.bar/img.jpg'],
91  'without scheme' => ['/prefix/foo.bar/img.jpg', 'http://foo.bar/prefix/foo.bar/img.jpg'],
92  ];
93  }
94 
95  #[DataProvider('prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider')]
96  #[Test]
97  public function ‪prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl($imageUri, $expected): void
98  {
99  ‪$GLOBALS['TYPO3_REQUEST'] = (new ‪ServerRequest())
100  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
101  ‪$GLOBALS['TSFE'] = new \stdClass();
102  ‪$GLOBALS['TSFE']->absRefPrefix = '/prefix/';
103 
104  $file = $this->createMock(File::class);
105  $file->expects(self::once())->method('getPublicUrl')->willReturn($imageUri);
106 
107  self::assertSame($expected, $this->subject->getImageUri($file, true));
108  }
109 }
‪TYPO3\CMS\Extbase\Tests\Unit\Service
Definition: CacheServiceTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest
Definition: ImageScriptServiceTest.php:32
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider
‪static prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider()
Definition: ImageScriptServiceTest.php:86
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ImageScriptServiceTest.php:33
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUri
‪prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected)
Definition: ImageScriptServiceTest.php:73
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\setUp
‪setUp()
Definition: ImageScriptServiceTest.php:40
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUriDataProvider
‪static prefixIsCorrectlyAppliedToGetImageUriDataProvider()
Definition: ImageScriptServiceTest.php:62
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\fileIsUnwrappedFromReferenceForProcessing
‪fileIsUnwrappedFromReferenceForProcessing()
Definition: ImageScriptServiceTest.php:49
‪$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\prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl
‪prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl($imageUri, $expected)
Definition: ImageScriptServiceTest.php:97
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\$subject
‪ImageService $subject
Definition: ImageScriptServiceTest.php:35