36997706d397f01802acd49c8fe846a981d547bb
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / ResourceCompressorTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
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\ResourceCompressor;
18
19 /**
20 * Testcase for the ResourceCompressor class
21 */
22 class ResourceCompressorTest extends BaseTestCase {
23
24 /**
25 * @var ResourceCompressor|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
26 */
27 protected $subject;
28
29 /**
30 * Set up the test
31 */
32 protected function setUp() {
33 parent::setUp();
34 $this->subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Resource\ResourceCompressor::class, array('compressCssFile', 'compressJsFile', 'createMergedCssFile', 'createMergedJsFile', 'getFilenameFromMainDir', 'checkBaseDirectory'));
35 }
36
37 /**
38 * @return array
39 */
40 public function cssFixStatementsDataProvider() {
41 return array(
42 'nothing to do - no charset/import/namespace' => array(
43 'body { background: #ffffff; }',
44 'body { background: #ffffff; }'
45 ),
46 'import in front' => array(
47 '@import url(http://www.example.com/css); body { background: #ffffff; }',
48 'LF/* moved by compressor */LF@import url(http://www.example.com/css);LFbody { background: #ffffff; }'
49 ),
50 'import in back, without quotes' => array(
51 'body { background: #ffffff; } @import url(http://www.example.com/css);',
52 'LF/* moved by compressor */LF@import url(http://www.example.com/css);LFbody { background: #ffffff; }'
53 ),
54 'import in back, with double-quotes' => array(
55 'body { background: #ffffff; } @import url("http://www.example.com/css");',
56 'LF/* moved by compressor */LF@import url("http://www.example.com/css");LFbody { background: #ffffff; }'
57 ),
58 'import in back, with single-quotes' => array(
59 'body { background: #ffffff; } @import url(\'http://www.example.com/css\');',
60 'LF/* moved by compressor */LF@import url(\'http://www.example.com/css\');LFbody { background: #ffffff; }'
61 ),
62 'import in middle and back, without quotes' => array(
63 'body { background: #ffffff; } @import url(http://www.example.com/A); div { background: #000; } @import url(http://www.example.com/B);',
64 'LF/* moved by compressor */LF@import url(http://www.example.com/A);LF/* moved by compressor */LF@import url(http://www.example.com/B);LFbody { background: #ffffff; } div { background: #000; }'
65 ),
66 );
67 }
68
69 /**
70 * @test
71 * @dataProvider cssFixStatementsDataProvider
72 */
73 public function cssFixStatementsMovesStatementsToTopIfNeeded($input, $expected) {
74 $result = $this->subject->_call('cssFixStatements', $input);
75 $resultWithReadableLinefeed = str_replace(LF, 'LF', $result);
76 $this->assertEquals($expected, $resultWithReadableLinefeed);
77 }
78
79 /**
80 * @test
81 */
82 public function compressedCssFileIsFlaggedToNotCompressAgain() {
83 $fileName = 'fooFile.css';
84 $compressedFileName = $fileName . '.gz';
85 $testFileFixture = array(
86 $fileName => array(
87 'file' => $fileName,
88 'compress' => TRUE,
89 )
90 );
91 $this->subject->expects($this->once())
92 ->method('compressCssFile')
93 ->with($fileName)
94 ->will($this->returnValue($compressedFileName));
95
96 $result = $this->subject->compressCssFiles($testFileFixture);
97
98 $this->assertArrayHasKey($compressedFileName, $result);
99 $this->assertArrayHasKey('compress', $result[$compressedFileName]);
100 $this->assertFalse($result[$compressedFileName]['compress']);
101 }
102
103 /**
104 * @test
105 */
106 public function compressedJsFileIsFlaggedToNotCompressAgain() {
107 $fileName = 'fooFile.js';
108 $compressedFileName = $fileName . '.gz';
109 $testFileFixture = array(
110 $fileName => array(
111 'file' => $fileName,
112 'compress' => TRUE,
113 )
114 );
115 $this->subject->expects($this->once())
116 ->method('compressJsFile')
117 ->with($fileName)
118 ->will($this->returnValue($compressedFileName));
119
120 $result = $this->subject->compressJsFiles($testFileFixture);
121
122 $this->assertArrayHasKey($compressedFileName, $result);
123 $this->assertArrayHasKey('compress', $result[$compressedFileName]);
124 $this->assertFalse($result[$compressedFileName]['compress']);
125 }
126
127
128 /**
129 * @test
130 */
131 public function concatenatedCssFileIsFlaggedToNotConcatenateAgain() {
132 $fileName = 'fooFile.css';
133 $concatenatedFileName = 'merged_' . $fileName;
134 $testFileFixture = array(
135 $fileName => array(
136 'file' => $fileName,
137 'excludeFromConcatenation' => FALSE,
138 'media' => 'all',
139 )
140 );
141 $this->subject->expects($this->once())
142 ->method('createMergedCssFile')
143 ->will($this->returnValue($concatenatedFileName));
144 $this->subject->setRelativePath('');
145
146 $result = $this->subject->concatenateCssFiles($testFileFixture);
147
148 $this->assertArrayHasKey($concatenatedFileName, $result);
149 $this->assertArrayHasKey('excludeFromConcatenation', $result[$concatenatedFileName]);
150 $this->assertTrue($result[$concatenatedFileName]['excludeFromConcatenation']);
151 }
152
153 /**
154 * @test
155 */
156 public function concatenatedJsFileIsFlaggedToNotConcatenateAgain() {
157 $fileName = 'fooFile.js';
158 $concatenatedFileName = 'merged_' . $fileName;
159 $testFileFixture = array(
160 $fileName => array(
161 'file' => $fileName,
162 'excludeFromConcatenation' => FALSE,
163 'section' => 'top',
164 )
165 );
166 $this->subject->expects($this->once())
167 ->method('createMergedJsFile')
168 ->will($this->returnValue($concatenatedFileName));
169 $this->subject->setRelativePath('');
170
171 $result = $this->subject->concatenateJsFiles($testFileFixture);
172
173 $this->assertArrayHasKey($concatenatedFileName, $result);
174 $this->assertArrayHasKey('excludeFromConcatenation', $result[$concatenatedFileName]);
175 $this->assertTrue($result[$concatenatedFileName]['excludeFromConcatenation']);
176 }
177
178 /**
179 * @return array
180 */
181 public function calcStatementsDataProvider() {
182 return array(
183 'simple calc' => array(
184 'calc(100% - 3px)',
185 'calc(100% - 3px)',
186 ),
187 'complex calc with parentheses at the beginning' => array(
188 'calc((100%/20) - 2*3px)',
189 'calc((100%/20) - 2*3px)',
190 ),
191 'complex calc with parentheses at the end' => array(
192 'calc(100%/20 - 2*3px - (200px + 3%))',
193 'calc(100%/20 - 2*3px - (200px + 3%))',
194 ),
195 'complex calc with many parentheses' => array(
196 'calc((100%/20) - (2 * (3px - (200px + 3%))))',
197 'calc((100%/20) - (2 * (3px - (200px + 3%))))',
198 ),
199 );
200 }
201
202 /**
203 * @test
204 * @dataProvider calcStatementsDataProvider
205 */
206 public function calcFunctionMustRetainWhitespaces($input, $expected) {
207 $result = $this->subject->_call('compressCssString', $input);
208 $this->assertSame($expected, trim($result));
209 }
210
211 }