[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Service / ImageScriptServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Resource\File;
18 use TYPO3\CMS\Core\Resource\FileReference;
19 use TYPO3\CMS\Core\Resource\ProcessedFile;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Extbase\Service\EnvironmentService;
22 use TYPO3\CMS\Extbase\Service\ImageService;
23
24 /**
25 * Test case
26 */
27 class ImageScriptServiceTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
28 {
29 /**
30 * @var ImageService
31 */
32 protected $subject;
33
34 /**
35 * @var EnvironmentService|\PHPUnit_Framework_MockObject_MockObject
36 */
37 protected $environmentService;
38
39 /**
40 * Initialize ImageService and environment service mock
41 */
42 protected function setUp()
43 {
44 $this->subject = new ImageService();
45 $this->environmentService = $this->createMock(EnvironmentService::class);
46 $this->inject($this->subject, 'environmentService', $this->environmentService);
47 GeneralUtility::flushInternalRuntimeCaches();
48 $_SERVER['HTTP_HOST'] = 'foo.bar';
49 }
50
51 /**
52 * @test
53 */
54 public function fileIsUnwrappedFromReferenceForProcessing()
55 {
56 $reference = $this->getAccessibleMock(FileReference::class, [], [], '', false);
57 $file = $this->createMock(File::class);
58 $file->expects($this->once())->method('process')->willReturn($this->createMock(ProcessedFile::class));
59 $reference->expects($this->once())->method('getOriginalFile')->willReturn($file);
60 $reference->_set('file', $file);
61
62 $this->subject->applyProcessingInstructions($reference, []);
63 }
64
65 /**
66 * @return array
67 */
68 public function prefixIsCorrectlyAppliedToGetImageUriDataProvider()
69 {
70 return [
71 'with scheme' => ['http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
72 'scheme relative' => ['//foo.bar/img.jpg', '//foo.bar/img.jpg'],
73 'without scheme' => ['foo.bar/img.jpg', '/prefix/foo.bar/img.jpg'],
74 ];
75 }
76
77 /**
78 * @test
79 * @dataProvider prefixIsCorrectlyAppliedToGetImageUriDataProvider
80 */
81 public function prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected)
82 {
83 $this->environmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->willReturn(true);
84 $GLOBALS['TSFE'] = new \stdClass();
85 $GLOBALS['TSFE']->absRefPrefix = '/prefix/';
86
87 $file = $this->createMock(File::class);
88 $file->expects($this->once())->method('getPublicUrl')->willReturn($imageUri);
89
90 $this->assertSame($expected, $this->subject->getImageUri($file));
91 }
92
93 /**
94 * @return array
95 */
96 public function prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider()
97 {
98 return [
99 'with scheme' => ['http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
100 'scheme relative' => ['//foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
101 'without scheme' => ['foo.bar/img.jpg', 'http://foo.bar/prefix/foo.bar/img.jpg'],
102 ];
103 }
104
105 /**
106 * @test
107 * @dataProvider prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider
108 */
109 public function prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl($imageUri, $expected)
110 {
111 $this->environmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->willReturn(true);
112 $GLOBALS['TSFE'] = new \stdClass();
113 $GLOBALS['TSFE']->absRefPrefix = '/prefix/';
114
115 $file = $this->createMock(File::class);
116 $file->expects($this->once())->method('getPublicUrl')->willReturn($imageUri);
117
118 $this->assertSame($expected, $this->subject->getImageUri($file, true));
119 }
120 }