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