2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Test for PMA_sendHeaderLocation
6 * @package phpMyAdmin-test
12 require_once 'libraries/common.lib.php';
13 require_once 'libraries/url_generating.lib.php';
14 require_once 'libraries/core.lib.php';
15 require_once 'libraries/select_lang.lib.php';
18 * Test function sending headers.
19 * Warning - these tests set constants, so it can interfere with other tests
20 * If you have runkit extension, then it is possible to back changes made on constants
21 * rest of options can be tested only with apd, when functions header and headers_sent are redefined
22 * rename_function() of header and headers_sent may cause CLI error report in Windows XP (but tests are done correctly)
23 * additional functions which were created during tests must be stored to coverage test e.g.
25 * <code>rename_function('headers_sent', 'headers_sent'.str_replace(array('.', ' '),array('', ''),microtime()));</code>
27 * @package phpMyAdmin-test
30 class PMA_headerLocation_test
extends PHPUnit_Extensions_OutputTestCase
33 protected $oldIISvalue;
34 protected $oldSIDvalue;
38 public function __construct()
40 parent
::__construct();
41 $this->runkitExt
= false;
42 if (function_exists("runkit_constant_redefine"))
43 $this->runkitExt
= true;
45 $this->apdExt
= false;
46 if (function_exists("rename_function"))
50 if ($this->apdExt
&& !$GLOBALS['test_header']) {
52 // using apd extension to overriding header and headers_sent functions for test purposes
53 $GLOBALS['test_header'] = 1;
55 // rename_function() of header and headers_sent may cause CLI error report in Windows XP
56 rename_function('header', 'test_header');
57 rename_function('headers_sent', 'test_headers_sent');
59 // solution from: http://unixwars.com/2008/11/29/override_function-in-php/ to overriding more than one function
62 'header' => 'if (isset($GLOBALS["header"])) $GLOBALS["header"] .= $a; else $GLOBALS["header"] = $a;',
63 'headers_sent' => 'return false;'
71 foreach ($substs as $func => $ren_func) {
72 if (function_exists("__overridden__"))
73 rename_function("__overridden__", str_replace(array('.', ' '),array('', ''),microtime()));
74 override_function($func, $args[$func], $substs[$func]);
75 rename_function("__overridden__", str_replace(array('.', ' '),array('', ''),microtime()));
81 public function __destruct()
83 // rename_function may causes CLI error report in Windows XP, but nothing more happen
85 if ($this->apdExt
&& $GLOBALS['test_header']) {
86 $GLOBALS['test_header'] = 0;
88 rename_function('header', 'header'.str_replace(array('.', ' '),array('', ''),microtime()));
89 rename_function('headers_sent', 'headers_sent'.str_replace(array('.', ' '),array('', ''),microtime()));
91 rename_function('test_header', 'header');
92 rename_function('test_headers_sent', 'headers_sent');
96 public function setUp()
100 // cleaning constants
101 if ($this->runkitExt
) {
103 $this->oldIISvalue
= 'non-defined';
105 if (defined('PMA_IS_IIS')) {
106 $this->oldIISvalue
= PMA_IS_IIS
;
107 runkit_constant_redefine('PMA_IS_IIS', NULL);
110 runkit_constant_add('PMA_IS_IIS', NULL);
114 $this->oldSIDvalue
= 'non-defined';
116 if (defined('SID')) {
117 $this->oldSIDvalue
= SID
;
118 runkit_constant_redefine('SID', NULL);
121 runkit_constant_add('SID', NULL);
128 public function tearDown()
132 // cleaning constants
133 if ($this->runkitExt
) {
135 if ($this->oldIISvalue
!= 'non-defined')
136 runkit_constant_redefine('PMA_IS_IIS', $this->oldIISvalue
);
137 elseif (defined('PMA_IS_IIS')) {
138 runkit_constant_remove('PMA_IS_IIS');
141 if ($this->oldSIDvalue
!= 'non-defined')
142 runkit_constant_redefine('SID', $this->oldSIDvalue
);
143 elseif (defined('SID')) {
144 runkit_constant_remove('SID');
149 unset($GLOBALS['header']);
154 public function testSendHeaderLocationWithSidUrlWithQuestionMark()
156 if ($this->runkitExt
&& $this->apdExt
) {
158 runkit_constant_redefine('SID', md5('test_hash'));
160 $testUri = 'http://testurl.com/test.php?test=test';
161 $separator = PMA_get_arg_separator();
163 $header = 'Location: ' . $testUri . $separator . SID
;
165 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
166 $this->assertEquals($header, $GLOBALS['header']);
169 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
174 public function testSendHeaderLocationWithSidUrlWithoutQuestionMark()
176 if ($this->runkitExt
&& $this->apdExt
) {
178 runkit_constant_redefine('SID', md5('test_hash'));
180 $testUri = 'http://testurl.com/test.php';
181 $separator = PMA_get_arg_separator();
183 $header = 'Location: ' . $testUri . '?' . SID
;
185 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
186 $this->assertEquals($header, $GLOBALS['header']);
189 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
194 public function testSendHeaderLocationWithoutSidWithIis()
196 if ($this->runkitExt
&& $this->apdExt
) {
198 runkit_constant_redefine('PMA_IS_IIS', true);
199 runkit_constant_add('PMA_COMING_FROM_COOKIE_LOGIN', true);
201 $testUri = 'http://testurl.com/test.php';
202 $separator = PMA_get_arg_separator();
204 $header = 'Refresh: 0; ' . $testUri;
206 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
209 runkit_constant_remove('PMA_COMING_FROM_COOKIE_LOGIN');
211 $this->assertEquals($header, $GLOBALS['header']);
214 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
219 public function testSendHeaderLocationWithoutSidWithoutIis()
223 $testUri = 'http://testurl.com/test.php';
224 $header = 'Location: ' . $testUri;
226 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
227 $this->assertEquals($header, $GLOBALS['header']);
230 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
235 public function testSendHeaderLocationIisLongUri()
237 if (defined('PMA_IS_IIS') && $this->runkitExt
)
238 runkit_constant_redefine('PMA_IS_IIS', true);
239 elseif (!defined('PMA_IS_IIS'))
240 define('PMA_IS_IIS', true);
242 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
246 $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';
247 $testUri_html = htmlspecialchars($testUri);
248 $testUri_js = PMA_escapeJsString($testUri);
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_html . "\">\n" .
255 "<script type=\"text/javascript\">\n".
257 "setTimeout(\"window.location = unescape('\"" . $testUri_js . "\"')\", 2000);\n" .
262 "<script type=\"text/javascript\">\n" .
264 "document.write('<p><a href=\"" . $testUri_html . "\">" . __('Go') . "</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?'.PMA_generate_common_url($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']);