a5ac9b8bac7c984f4d6cdbd66a5a7661fd94a82f
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Service / ImageScriptServiceTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Unit\Service;
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
18 use TYPO3\CMS\Core\Resource\File;
19 use TYPO3\CMS\Core\Resource\FileReference;
20 use TYPO3\CMS\Core\Resource\ProcessedFile;
21 use TYPO3\CMS\Extbase\Service\EnvironmentService;
22 use TYPO3\CMS\Extbase\Service\ImageService;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24
25 /**
26 * Test case
27 */
28 class ImageScriptServiceTest extends UnitTestCase
29 {
30 /**
31 * @var bool Reset singletons created by subject
32 */
33 protected $resetSingletonInstances = true;
34
35 /**
36 * @var ImageService
37 */
38 protected $subject;
39
40 /**
41 * @var EnvironmentService|\PHPUnit_Framework_MockObject_MockObject
42 */
43 protected $environmentService;
44
45 /**
46 * Initialize ImageService and environment service mock
47 */
48 protected function setUp(): void
49 {
50 $this->environmentService = $this->createMock(EnvironmentService::class);
51 $this->subject = new ImageService($this->environmentService);
52 $_SERVER['HTTP_HOST'] = 'foo.bar';
53 }
54
55 /**
56 * @test
57 */
58 public function fileIsUnwrappedFromReferenceForProcessing(): void
59 {
60 $reference = $this->getAccessibleMock(FileReference::class, [], [], '', false);
61 $file = $this->createMock(File::class);
62 $file->expects($this->once())->method('process')->willReturn($this->createMock(ProcessedFile::class));
63 $reference->expects($this->once())->method('getOriginalFile')->willReturn($file);
64 $reference->_set('file', $file);
65
66 $this->subject->applyProcessingInstructions($reference, []);
67 }
68
69 /**
70 * @return array
71 */
72 public function prefixIsCorrectlyAppliedToGetImageUriDataProvider(): array
73 {
74 return [
75 'with scheme' => ['http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
76 'scheme relative' => ['//foo.bar/img.jpg', '//foo.bar/img.jpg'],
77 'without scheme' => ['foo.bar/img.jpg', '/prefix/foo.bar/img.jpg'],
78 ];
79 }
80
81 /**
82 * @test
83 * @dataProvider prefixIsCorrectlyAppliedToGetImageUriDataProvider
84 */
85 public function prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected): void
86 {
87 $this->environmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->willReturn(true);
88 $GLOBALS['TSFE'] = new \stdClass();
89 $GLOBALS['TSFE']->absRefPrefix = '/prefix/';
90
91 $file = $this->createMock(File::class);
92 $file->expects($this->once())->method('getPublicUrl')->willReturn($imageUri);
93
94 $this->assertSame($expected, $this->subject->getImageUri($file));
95 }
96
97 /**
98 * @return array
99 */
100 public function prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider(): array
101 {
102 return [
103 'with scheme' => ['http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
104 'scheme relative' => ['//foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
105 'without scheme' => ['foo.bar/img.jpg', 'http://foo.bar/prefix/foo.bar/img.jpg'],
106 ];
107 }
108
109 /**
110 * @test
111 * @dataProvider prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider
112 */
113 public function prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl($imageUri, $expected): void
114 {
115 $this->environmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->willReturn(true);
116 $GLOBALS['TSFE'] = new \stdClass();
117 $GLOBALS['TSFE']->absRefPrefix = '/prefix/';
118
119 $file = $this->createMock(File::class);
120 $file->expects($this->once())->method('getPublicUrl')->willReturn($imageUri);
121
122 $this->assertSame($expected, $this->subject->getImageUri($file, true));
123 }
124 }