2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Test for PMA_sendHeaderLocation
11 require_once 'PHPUnit/Framework.php';
12 require_once 'PHPUnit/Extensions/OutputTestCase.php';
17 require_once './libraries/common.lib.php';
18 require_once './libraries/url_generating.lib.php';
19 require_once './libraries/core.lib.php';
22 * Test function sending headers.
23 * Warning - these tests set constants, so it can interfere with other tests
24 * If you have runkit extension, then it is possible to back changes made on constants
25 * rest of options can be tested only with apd, when functions header and headers_sent are redefined
26 * rename_function() of header and headers_sent may cause CLI error report in Windows XP (but tests are done correctly)
27 * additional functions which were created during tests must be stored to coverage test e.g.
29 * <code>rename_function('headers_sent', 'headers_sent'.str_replace(array('.', ' '),array('', ''),microtime()));</code>
31 * @package phpMyAdmin-test
34 class PMA_headerLocation_test
extends PHPUnit_Extensions_OutputTestCase
37 protected $oldIISvalue;
38 protected $oldSIDvalue;
44 public function __construct()
46 parent
::__construct();
47 $this->runkitExt
= false;
48 if (function_exists("runkit_constant_redefine"))
49 $this->runkitExt
= true;
51 $this->apdExt
= false;
52 if (function_exists("rename_function"))
56 if ($this->apdExt
&& !$GLOBALS['test_header']) {
58 // using apd extension to overriding header and headers_sent functions for test purposes
59 $GLOBALS['test_header'] = 1;
61 // rename_function() of header and headers_sent may cause CLI error report in Windows XP
62 rename_function('header', 'test_header');
63 rename_function('headers_sent', 'test_headers_sent');
65 // solution from: http://unixwars.com/2008/11/29/override_function-in-php/ to overriding more than one function
68 'header' => 'if (isset($GLOBALS["header"])) $GLOBALS["header"] .= $a; else $GLOBALS["header"] = $a;',
69 'headers_sent' => 'return false;'
77 foreach ($substs as $func => $ren_func) {
78 if (function_exists("__overridden__"))
79 rename_function("__overridden__", str_replace(array('.', ' '),array('', ''),microtime()));
80 override_function($func, $args[$func], $substs[$func]);
81 rename_function("__overridden__", str_replace(array('.', ' '),array('', ''),microtime()));
87 public function __destruct()
89 // rename_function may causes CLI error report in Windows XP, but nothing more happen
91 if ($this->apdExt
&& $GLOBALS['test_header']) {
92 $GLOBALS['test_header'] = 0;
94 rename_function('header', 'header'.str_replace(array('.', ' '),array('', ''),microtime()));
95 rename_function('headers_sent', 'headers_sent'.str_replace(array('.', ' '),array('', ''),microtime()));
97 rename_function('test_header', 'header');
98 rename_function('test_headers_sent', 'headers_sent');
102 public function setUp()
104 // cleaning constants
105 if ($this->runkitExt
) {
107 $this->oldIISvalue
= 'non-defined';
109 if (defined('PMA_IS_IIS')) {
110 $this->oldIISvalue
= PMA_IS_IIS
;
111 runkit_constant_redefine('PMA_IS_IIS', NULL);
114 runkit_constant_add('PMA_IS_IIS', NULL);
118 $this->oldSIDvalue
= 'non-defined';
120 if (defined('SID')) {
121 $this->oldSIDvalue
= SID
;
122 runkit_constant_redefine('SID', NULL);
125 runkit_constant_add('SID', NULL);
132 public function tearDown()
134 // cleaning constants
135 if ($this->runkitExt
) {
137 if ($this->oldIISvalue
!= 'non-defined')
138 runkit_constant_redefine('PMA_IS_IIS', $this->oldIISvalue
);
139 elseif (defined('PMA_IS_IIS')) {
140 runkit_constant_remove('PMA_IS_IIS');
143 if ($this->oldSIDvalue
!= 'non-defined')
144 runkit_constant_redefine('SID', $this->oldSIDvalue
);
145 elseif (defined('SID')) {
146 runkit_constant_remove('SID');
151 unset($GLOBALS['header']);
156 public function testSendHeaderLocationWithSidUrlWithQuestionMark()
158 if ($this->runkitExt
&& $this->apdExt
) {
160 runkit_constant_redefine('SID', md5('test_hash'));
162 $testUri = 'http://testurl.com/test.php?test=test';
163 $separator = PMA_get_arg_separator();
165 $header = 'Location: ' . $testUri . $separator . SID
;
167 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
168 $this->assertEquals($header, $GLOBALS['header']);
171 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
176 public function testSendHeaderLocationWithSidUrlWithoutQuestionMark()
178 if ($this->runkitExt
&& $this->apdExt
) {
180 runkit_constant_redefine('SID', md5('test_hash'));
182 $testUri = 'http://testurl.com/test.php';
183 $separator = PMA_get_arg_separator();
185 $header = 'Location: ' . $testUri . '?' . SID
;
187 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
188 $this->assertEquals($header, $GLOBALS['header']);
191 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
196 public function testSendHeaderLocationWithoutSidWithIis()
198 if ($this->runkitExt
&& $this->apdExt
) {
200 runkit_constant_redefine('PMA_IS_IIS', true);
201 runkit_constant_add('PMA_COMING_FROM_COOKIE_LOGIN', true);
203 $testUri = 'http://testurl.com/test.php';
204 $separator = PMA_get_arg_separator();
206 $header = 'Refresh: 0; ' . $testUri;
208 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
211 runkit_constant_remove('PMA_COMING_FROM_COOKIE_LOGIN');
213 $this->assertEquals($header, $GLOBALS['header']);
216 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
221 public function testSendHeaderLocationWithoutSidWithoutIis()
225 $testUri = 'http://testurl.com/test.php';
226 $header = 'Location: ' . $testUri;
228 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
229 $this->assertEquals($header, $GLOBALS['header']);
232 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
237 public function testSendHeaderLocationIisLongUri()
239 if (defined('PMA_IS_IIS') && $this->runkitExt
)
240 runkit_constant_redefine('PMA_IS_IIS', true);
241 elseif (!defined('PMA_IS_IIS'))
242 define('PMA_IS_IIS', true);
244 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
248 $testUri = 'http://testurl.com/test.php?testlonguri=over600chars&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test';
250 $header = "<html><head><title>- - -</title>\n" .
251 "<meta http-equiv=\"expires\" content=\"0\">\n" .
252 "<meta http-equiv=\"Pragma\" content=\"no-cache\">\n" .
253 "<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\n" .
254 "<meta http-equiv=\"Refresh\" content=\"0;url=" . $testUri . "\">\n" .
255 "<script type=\"text/javascript\">\n".
257 "setTimeout(\"window.location = unescape('\"" . $testUri . "\"')\", 2000);\n" .
262 "<script type=\"text/javascript\">\n" .
264 "document.write('<p><a href=\"" . $testUri . "\">" . 'test link' . "</a></p>');\n" .
266 "</script></body></html>\n";
269 $this->expectOutputString($header);
271 PMA_sendHeaderLocation($testUri);
278 public function testWriteReloadNavigation()
280 $GLOBALS['reload'] = true;
281 $GLOBALS['db'] = 'test_db';
283 $url = './navigation.php?db='.$GLOBALS['db'];
284 $write = PHP_EOL
. '<script type="text/javascript">' . PHP_EOL
.
285 '//<![CDATA[' . PHP_EOL
.
286 'if (typeof(window.parent) != \'undefined\'' . PHP_EOL
.
287 ' && typeof(window.parent.frame_navigation) != \'undefined\'' . PHP_EOL
.
288 ' && window.parent.goTo) {' . PHP_EOL
.
289 ' window.parent.goTo(\'' . $url . '\');' . PHP_EOL
.
292 '</script>' . PHP_EOL
;
294 $this->expectOutputString($write);
295 PMA_reloadNavigation();
297 $this->assertFalse(isset($GLOBALS['reload']));
298 unset($GLOBALS['db']);