2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Test for PMA_sendHeaderLocation
12 require_once 'PHPUnit/Framework.php';
13 require_once 'PHPUnit/Extensions/OutputTestCase.php';
18 require_once './libraries/common.lib.php';
19 require_once './libraries/url_generating.lib.php';
20 require_once './libraries/core.lib.php';
23 * Test function sending headers.
24 * Warning - these tests set constants, so it can interfere with other tests
25 * If you have runkit extension, then it is possible to back changes made on constants
26 * rest of options can be tested only with apd, when functions header and headers_sent are redefined
27 * rename_function() of header and headers_sent may cause CLI error report in Windows XP (but tests are done correctly)
28 * additional functions which were created during tests must be stored to coverage test e.g.
30 * <code>rename_function('headers_sent', 'headers_sent'.str_replace(array('.', ' '),array('', ''),microtime()));</code>
32 * @package phpMyAdmin-test
35 class PMA_headerLocation_test
extends PHPUnit_Extensions_OutputTestCase
38 protected $oldIISvalue;
39 protected $oldSIDvalue;
45 public function __construct()
47 parent
::__construct();
48 $this->runkitExt
= false;
49 if (function_exists("runkit_constant_redefine"))
50 $this->runkitExt
= true;
52 $this->apdExt
= false;
53 if (function_exists("rename_function"))
57 if ($this->apdExt
&& !$GLOBALS['test_header']) {
59 // using apd extension to overriding header and headers_sent functions for test purposes
60 $GLOBALS['test_header'] = 1;
62 // rename_function() of header and headers_sent may cause CLI error report in Windows XP
63 rename_function('header', 'test_header');
64 rename_function('headers_sent', 'test_headers_sent');
66 // solution from: http://unixwars.com/2008/11/29/override_function-in-php/ to overriding more than one function
69 'header' => 'if (isset($GLOBALS["header"])) $GLOBALS["header"] .= $a; else $GLOBALS["header"] = $a;',
70 'headers_sent' => 'return false;'
78 foreach ($substs as $func => $ren_func) {
79 if (function_exists("__overridden__"))
80 rename_function("__overridden__", str_replace(array('.', ' '),array('', ''),microtime()));
81 override_function($func, $args[$func], $substs[$func]);
82 rename_function("__overridden__", str_replace(array('.', ' '),array('', ''),microtime()));
88 public function __destruct()
90 // rename_function may causes CLI error report in Windows XP, but nothing more happen
92 if ($this->apdExt
&& $GLOBALS['test_header']) {
93 $GLOBALS['test_header'] = 0;
95 rename_function('header', 'header'.str_replace(array('.', ' '),array('', ''),microtime()));
96 rename_function('headers_sent', 'headers_sent'.str_replace(array('.', ' '),array('', ''),microtime()));
98 rename_function('test_header', 'header');
99 rename_function('test_headers_sent', 'headers_sent');
103 public function setUp()
105 // cleaning constants
106 if ($this->runkitExt
) {
108 $this->oldIISvalue
= 'non-defined';
110 if (defined('PMA_IS_IIS')) {
111 $this->oldIISvalue
= PMA_IS_IIS
;
112 runkit_constant_redefine('PMA_IS_IIS', NULL);
115 runkit_constant_add('PMA_IS_IIS', NULL);
119 $this->oldSIDvalue
= 'non-defined';
121 if (defined('SID')) {
122 $this->oldSIDvalue
= SID
;
123 runkit_constant_redefine('SID', NULL);
126 runkit_constant_add('SID', NULL);
133 public function tearDown()
135 // cleaning constants
136 if ($this->runkitExt
) {
138 if ($this->oldIISvalue
!= 'non-defined')
139 runkit_constant_redefine('PMA_IS_IIS', $this->oldIISvalue
);
140 elseif (defined('PMA_IS_IIS')) {
141 runkit_constant_remove('PMA_IS_IIS');
144 if ($this->oldSIDvalue
!= 'non-defined')
145 runkit_constant_redefine('SID', $this->oldSIDvalue
);
146 elseif (defined('SID')) {
147 runkit_constant_remove('SID');
152 unset($GLOBALS['header']);
157 public function testSendHeaderLocationWithSidUrlWithQuestionMark()
159 if ($this->runkitExt
&& $this->apdExt
) {
161 runkit_constant_redefine('SID', md5('test_hash'));
163 $testUri = 'http://testurl.com/test.php?test=test';
164 $separator = PMA_get_arg_separator();
166 $header = 'Location: ' . $testUri . $separator . SID
;
168 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
169 $this->assertEquals($header, $GLOBALS['header']);
172 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
177 public function testSendHeaderLocationWithSidUrlWithoutQuestionMark()
179 if ($this->runkitExt
&& $this->apdExt
) {
181 runkit_constant_redefine('SID', md5('test_hash'));
183 $testUri = 'http://testurl.com/test.php';
184 $separator = PMA_get_arg_separator();
186 $header = 'Location: ' . $testUri . '?' . SID
;
188 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
189 $this->assertEquals($header, $GLOBALS['header']);
192 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
197 public function testSendHeaderLocationWithoutSidWithIis()
199 if ($this->runkitExt
&& $this->apdExt
) {
201 runkit_constant_redefine('PMA_IS_IIS', true);
202 runkit_constant_add('PMA_COMING_FROM_COOKIE_LOGIN', true);
204 $testUri = 'http://testurl.com/test.php';
205 $separator = PMA_get_arg_separator();
207 $header = 'Refresh: 0; ' . $testUri;
209 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
212 runkit_constant_remove('PMA_COMING_FROM_COOKIE_LOGIN');
214 $this->assertEquals($header, $GLOBALS['header']);
217 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
222 public function testSendHeaderLocationWithoutSidWithoutIis()
226 $testUri = 'http://testurl.com/test.php';
227 $header = 'Location: ' . $testUri;
229 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
230 $this->assertEquals($header, $GLOBALS['header']);
233 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
238 public function testSendHeaderLocationIisLongUri()
240 if (defined('PMA_IS_IIS') && $this->runkitExt
)
241 runkit_constant_redefine('PMA_IS_IIS', true);
242 elseif (!defined('PMA_IS_IIS'))
243 define('PMA_IS_IIS', true);
245 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
249 $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';
251 $GLOBALS['strGo'] = 'test link';
253 $header = "<html><head><title>- - -</title>\n" .
254 "<meta http-equiv=\"expires\" content=\"0\">\n" .
255 "<meta http-equiv=\"Pragma\" content=\"no-cache\">\n" .
256 "<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\n" .
257 "<meta http-equiv=\"Refresh\" content=\"0;url=" . $testUri . "\">\n" .
258 "<script type=\"text/javascript\">\n".
260 "setTimeout(\"window.location = unescape('\"" . $testUri . "\"')\", 2000);\n" .
265 "<script type=\"text/javascript\">\n" .
267 "document.write('<p><a href=\"" . $testUri . "\">" . $GLOBALS['strGo'] . "</a></p>');\n" .
269 "</script></body></html>\n";
272 $this->expectOutputString($header);
274 PMA_sendHeaderLocation($testUri);
281 public function testWriteReloadNavigation()
283 $GLOBALS['reload'] = true;
284 $GLOBALS['db'] = 'test_db';
286 $url = './navigation.php?db='.$GLOBALS['db'];
287 $write = PHP_EOL
. '<script type="text/javascript">' . PHP_EOL
.
288 '//<![CDATA[' . PHP_EOL
.
289 'if (typeof(window.parent) != \'undefined\'' . PHP_EOL
.
290 ' && typeof(window.parent.frame_navigation) != \'undefined\'' . PHP_EOL
.
291 ' && window.parent.goTo) {' . PHP_EOL
.
292 ' window.parent.goTo(\'' . $url . '\');' . PHP_EOL
.
295 '</script>' . PHP_EOL
;
297 $this->expectOutputString($write);
298 PMA_reloadNavigation();
300 $this->assertFalse(isset($GLOBALS['reload']));
301 unset($GLOBALS['db']);