/lib/minify-2.4.1b/min_unit_tests/test_environment.php

https://github.com/martinnemitz/Mage-Minify · PHP · 100 lines · 85 code · 12 blank · 3 comment · 18 complexity · 08d50f1ca4104f7d264f9fe849812e9a MD5 · raw file

  1. <?php
  2. if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
  3. // called directly
  4. if (isset($_GET['getOutputCompression'])) {
  5. echo (int)ini_get('zlib.output_compression');
  6. exit();
  7. }
  8. if (isset($_GET['hello'])) {
  9. // try to disable (may not work)
  10. ini_set('zlib.output_compression', '0');
  11. echo 'World!';
  12. exit();
  13. }
  14. }
  15. require_once '_inc.php';
  16. function test_environment()
  17. {
  18. global $thisDir;
  19. // check DOCROOT
  20. $noSlash = assertTrue(
  21. 0 === preg_match('@[\\\\/]$@', $_SERVER['DOCUMENT_ROOT'])
  22. ,'environment : DOCUMENT_ROOT should not end in trailing slash'
  23. );
  24. $isRealPath = assertTrue(false !== realpath($_SERVER['DOCUMENT_ROOT'])
  25. ,'environment : DOCUMENT_ROOT should pass realpath()'
  26. );
  27. $containsThisFile = assertTrue(
  28. 0 === strpos(realpath(__FILE__), realpath($_SERVER['DOCUMENT_ROOT']))
  29. ,'environment : DOCUMENT_ROOT should contain this test file'
  30. );
  31. if (! $noSlash || ! $isRealPath || ! $containsThisFile) {
  32. echo "\nDOCUMENT_ROOT is set to: '{$_SERVER['DOCUMENT_ROOT']}'. If you "
  33. . "cannot modify this, consider setting \$min_documentRoot in config.php\n\n";
  34. }
  35. if (isset($_SERVER['SUBDOMAIN_DOCUMENT_ROOT'])) {
  36. echo "\n!NOTE: environment : \$_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] is set. "
  37. . "You may need to set \$min_documentRoot to this in config.php\n";
  38. }
  39. if (realpath(__FILE__) !== realpath($_SERVER['DOCUMENT_ROOT'] . '/min_unit_tests/test_environment.php')) {
  40. echo "!NOTE: environment : /min_unit_tests/ is not directly inside DOCUMENT_ROOT\n";
  41. }
  42. $thisUrl = 'http://'
  43. . $_SERVER['HTTP_HOST'] // avoid redirects when SERVER_NAME doesn't match
  44. . ('80' === $_SERVER['SERVER_PORT'] ? '' : ":{$_SERVER['SERVER_PORT']}")
  45. . dirname($_SERVER['REQUEST_URI'])
  46. . '/test_environment.php';
  47. $oc = @file_get_contents($thisUrl . '?getOutputCompression=1');
  48. if (false === $oc || ! preg_match('/^[01]$/', $oc)) {
  49. echo "!WARN: environment : Local HTTP request failed. Testing cannot continue.\n";
  50. return;
  51. }
  52. if ('1' === $oc) {
  53. echo "!WARN: environment : zlib.output_compression is enabled in php.ini"
  54. . " or .htaccess.\n";
  55. }
  56. $fp = fopen($thisUrl . '?hello=1', 'r', false, stream_context_create(array(
  57. 'http' => array(
  58. 'method' => "GET",
  59. 'header' => "Accept-Encoding: deflate, gzip\r\n"
  60. )
  61. )));
  62. $meta = stream_get_meta_data($fp);
  63. $passed = true;
  64. foreach ($meta['wrapper_data'] as $i => $header) {
  65. if ((preg_match('@^Content-Length: (\\d+)$@i', $header, $m) && $m[1] !== '6')
  66. || preg_match('@^Content-Encoding:@i', $header, $m)
  67. ) {
  68. $passed = false;
  69. break;
  70. }
  71. }
  72. if ($passed && stream_get_contents($fp) !== 'World!') {
  73. $passed = false;
  74. }
  75. assertTrue(
  76. $passed
  77. ,'environment : PHP/server does not auto-HTTP-encode content'
  78. );
  79. fclose($fp);
  80. if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
  81. if (! $passed) {
  82. echo "\nReturned content should be 6 bytes and not HTTP encoded.\n"
  83. . "Headers returned by: {$thisUrl}?hello=1\n\n";
  84. var_export($meta['wrapper_data']);
  85. }
  86. }
  87. }
  88. test_environment();