Merge branch 'QA_3_4'
[phpmyadmin-regexreplace.git] / test / PMA_headerLocation_test.php
blobdaad6897c9daf7eed8c12ed6a0cf3346b5424468
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Test for PMA_sendHeaderLocation
6 */
8 /**
9 * Tests core.
11 require_once 'PHPUnit/Framework.php';
12 require_once 'PHPUnit/Extensions/OutputTestCase.php';
14 /**
15 * Include to test.
17 require_once './libraries/common.lib.php';
18 require_once './libraries/url_generating.lib.php';
19 require_once './libraries/core.lib.php';
20 require_once './libraries/select_lang.lib.php';
22 /**
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;
40 protected $runkitExt;
41 protected $apdExt;
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"))
54 $this->apdExt = true;
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
68 $substs = array(
69 'header' => 'if (isset($GLOBALS["header"])) $GLOBALS["header"] .= $a; else $GLOBALS["header"] = $a;',
70 'headers_sent' => 'return false;'
73 $args = array(
74 'header' => '$a',
75 'headers_sent' => ''
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);
114 else {
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);
125 else {
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');
151 if ($this->apdExt)
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']);
171 } else {
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']);
191 } else {
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']
211 // cleaning constant
212 runkit_constant_remove('PMA_COMING_FROM_COOKIE_LOGIN');
214 $this->assertEquals($header, $GLOBALS['header']);
216 } else {
217 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
222 public function testSendHeaderLocationWithoutSidWithoutIis()
224 if ($this->apdExt) {
226 $testUri = 'http://testurl.com/test.php';
227 $header = 'Location: ' . $testUri;
229 PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
230 $this->assertEquals($header, $GLOBALS['header']);
232 } else {
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);
244 else
245 $this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
248 // over 600 chars
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';
250 $testUri_html = htmlspecialchars($testUri);
251 $testUri_js = PMA_escapeJsString($testUri);
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_html . "\">\n" .
258 "<script type=\"text/javascript\">\n".
259 "//<![CDATA[\n" .
260 "setTimeout(\"window.location = unescape('\"" . $testUri_js . "\"')\", 2000);\n" .
261 "//]]>\n" .
262 "</script>\n" .
263 "</head>\n" .
264 "<body>\n" .
265 "<script type=\"text/javascript\">\n" .
266 "//<![CDATA[\n" .
267 "document.write('<p><a href=\"" . $testUri_html . "\">" . 'test link' . "</a></p>');\n" .
268 "//]]>\n" .
269 "</script></body></html>\n";
272 $this->expectOutputString($header);
274 PMA_sendHeaderLocation($testUri);
278 * other output tests
281 public function testWriteReloadNavigation()
283 $GLOBALS['reload'] = true;
284 $GLOBALS['db'] = 'test_db';
286 $url = './navigation.php?db='.$GLOBALS['db'] . '&lang=en-utf-8&convcharset=utf-8';
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 .
293 '}' . PHP_EOL .
294 '//]]>' . PHP_EOL .
295 '</script>' . PHP_EOL;
297 $this->expectOutputString($write);
298 PMA_reloadNavigation();
300 $this->assertFalse(isset($GLOBALS['reload']));
301 unset($GLOBALS['db']);