PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.5
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / mrclay / minify / tests / MinifyTest.php

MinifyTest.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/mrclay/minify/tests/MinifyTest.php

217 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Minify\Test;
4
5 use Minify;
6 use Minify_Cache_Null;
7 use Minify_Controller_Files;
8 use Minify_Env;
9 use Minify_Source_Factory;
10
11 class MinifyTest extends TestCase
12 {
13 public function test_Minify()
14 {
15 $minifyTestPath = self::$test_files . '/minify';
16
17 $_SERVER['DOCUMENT_ROOT'] = dirname(__DIR__);
18
19 $tomorrow = $_SERVER['REQUEST_TIME'] + 86400;
20 $lastModified = $_SERVER['REQUEST_TIME'] - 86400;
21
22 // Test 304 response
23
24 // simulate conditional headers
25 $_SERVER['HTTP_IF_NONE_MATCH'] = "\"{$lastModified}pub\"";
26 $_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s \G\M\T', $lastModified);
27
28 $minify = new Minify(new Minify_Cache_Null());
29 $env = new Minify_Env(array(
30 'server' => $_SERVER,
31 ));
32 $sourceFactory = new Minify_Source_Factory($env, array(), new Minify_Cache_Null());
33 $controller = new Minify_Controller_Files($env, $sourceFactory);
34
35 $output = $minify->serve($controller, array(
36 'files' => self::$test_files . '/css/styles.css', // controller casts to array
37 'quiet' => true,
38 'lastModifiedTime' => $lastModified,
39 'encodeOutput' => false,
40 ));
41
42 $expected = array(
43 'success' => true,
44 'statusCode' => 304,
45 'content' => '',
46 'headers' => array(
47 'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $_SERVER['REQUEST_TIME'] + 1800),
48 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
49 'ETag' => "\"pub{$lastModified}\"",
50 'Cache-Control' => 'max-age=1800',
51 '_responseCode' => 'HTTP/1.0 304 Not Modified',
52 ),
53 );
54
55 $this->assertEquals($expected, $output, '304 response');
56
57 $this->markTestIncomplete('minifier classes aren\'t loaded for 304s');
58 // $this->assertTrue(!class_exists('Minify_CSSmin', false),
59 // 'Minify : minifier classes aren\'t loaded for 304s');
60
61 // Test JS and Expires
62
63 $content = preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'));
64 $lastModified = max(
65 filemtime($minifyTestPath . '/email.js'),
66 filemtime($minifyTestPath . '/QueryString.js')
67 );
68 $expected = array(
69 'success' => true,
70 'statusCode' => 200,
71 // JSMin always converts to \n line endings
72 'content' => $content,
73 'headers' => array(
74 'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
75 'Vary' => 'Accept-Encoding',
76 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
77 'ETag' => "\"pub{$lastModified}\"",
78 'Cache-Control' => 'max-age=86400',
79 'Content-Length' => $this->countBytes($content),
80 'Content-Type' => 'application/x-javascript; charset=utf-8',
81 )
82 );
83
84 unset($_SERVER['HTTP_IF_NONE_MATCH']);
85 unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
86
87 $env = new Minify_Env(array(
88 'server' => $_SERVER,
89 ));
90 $sourceFactory = new Minify_Source_Factory($env, array(), new Minify_Cache_Null());
91 $controller = new Minify_Controller_Files($env, $sourceFactory);
92 $output = $minify->serve($controller, array(
93 'files' => array(
94 $minifyTestPath . '/email.js',
95 $minifyTestPath . '/QueryString.js',
96 ),
97 'quiet' => true,
98 'maxAge' => 86400,
99 'encodeOutput' => false,
100 ));
101
102 $this->assertEquals($expected, $output, 'JS and Expires');
103
104 // test for Issue 73
105 $expected = ";function h(){}";
106 $output = $minify->serve($controller, array(
107 'files' => array(
108 $minifyTestPath . '/issue73_1.js',
109 $minifyTestPath . '/issue73_2.js',
110 ),
111 'quiet' => true,
112 'encodeOutput' => false,
113 ));
114 $output = $output['content'];
115
116 $this->assertEquals($expected, $output, 'Issue 73');
117
118 // test for Issue 89
119 $expected = file_get_contents($minifyTestPath . '/issue89_out.min.css');
120 $output = $minify->serve($controller, array(
121 'files' => array(
122 $minifyTestPath . '/issue89_1.css',
123 $minifyTestPath . '/issue89_2.css',
124 ),
125 'quiet' => true,
126 'encodeOutput' => false,
127 'bubbleCssImports' => true,
128 ));
129 $output = $output['content'];
130
131 $this->assertEquals($expected, $output, 'Issue 89 : bubbleCssImports');
132
133 $output = $minify->serve($controller, array(
134 'files' => array(
135 $minifyTestPath . '/issue89_1.css',
136 $minifyTestPath . '/issue89_2.css',
137 ),
138 'quiet' => true,
139 'encodeOutput' => false,
140 ));
141 $output = $output['content'];
142
143 $defaultOptions = $minify->getDefaultOptions();
144
145 $this->assertEquals(0, strpos($output, $defaultOptions['importWarning']), 'Issue 89 : detect invalid imports');
146
147 $output = $minify->serve($controller, array(
148 'files' => array(
149 $minifyTestPath . '/issue89_1.css',
150 ),
151 'quiet' => true,
152 'encodeOutput' => false,
153 ));
154 $output = $output['content'];
155
156 $this->assertFalse(
157 strpos($output, $defaultOptions['importWarning']),
158 'Issue 89 : don\'t warn about valid imports'
159 );
160
161 // Test Issue 132
162 if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
163 $output = $minify->serve($controller, array(
164 'files' => array(__DIR__ . '/_test_files/js/issue132.js'),
165 'quiet' => true,
166 'encodeOutput' => false,
167 ));
168
169 $this->assertEquals(
170 77,
171 $output['headers']['Content-Length'],
172 'Issue 132 : mbstring.func_overload shouldn\'t cause incorrect Content-Length'
173 );
174 }
175
176 // Test minifying CSS and responding with Etag/Last-Modified
177
178 // don't allow conditional headers
179 unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
180
181 $expectedContent = file_get_contents($minifyTestPath . '/minified.css');
182
183 $expected = array(
184 'success' => true,
185 'statusCode' => 200,
186 'content' => $expectedContent,
187 'headers' => array(
188 'Vary' => 'Accept-Encoding',
189 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
190 'ETag' => "\"pub{$lastModified}\"",
191 'Cache-Control' => 'max-age=0',
192 'Content-Length' => $this->countBytes($expectedContent),
193 'Content-Type' => 'text/css; charset=utf-8',
194 )
195 );
196
197 $env = new Minify_Env(array(
198 'server' => $_SERVER,
199 ));
200 $sourceFactory = new Minify_Source_Factory($env, array(), new Minify_Cache_Null());
201 $controller = new Minify_Controller_Files($env, $sourceFactory);
202
203 $output = $minify->serve($controller, array(
204 'files' => array(
205 self::$test_files . '/css/styles.css',
206 self::$test_files . '/css/comments.css',
207 ),
208 'quiet' => true,
209 'lastModifiedTime' => $lastModified,
210 'encodeOutput' => false,
211 'maxAge' => false,
212 ));
213
214 $this->assertEquals($expected, $output, 'CSS and Etag/Last-Modified');
215 }
216 }
217