/application/library/Thirdpart/Minify/min_unit_tests/test_Minify.php

https://gitlab.com/flyhope/Hiblog · PHP · 245 lines · 203 code · 32 blank · 10 comment · 17 complexity · 2c7d4600e00db0f26e1b2b6dbdc8b73e MD5 · raw file

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