Skip navigation
Help

bootstrap.test

  1. drupal
    1. 7 drupal/modules/simpletest/tests/bootstrap.test

Classes

NameDescription
BootstrapGetFilenameTestCaseTest drupal_get_filename()'s availability.
BootstrapIPAddressTestCase
BootstrapMiscTestCaseTest miscellaneous functions in bootstrap.inc.
BootstrapOverrideServerVariablesTestCaseTests for overriding server variables via the API.
BootstrapPageCacheTestCase
BootstrapResettableStaticTestCaseTest that resetting static variables works.
BootstrapTimerTestCase
BootstrapVariableTestCase
HookBootExitTestCaseTest hook_boot() and hook_exit().

File

drupal/modules/simpletest/tests/bootstrap.test
View source
  1. <?php
  2. class BootstrapIPAddressTestCase extends DrupalWebTestCase {
  3. public static function getInfo() {
  4. return array(
  5. 'name' => 'IP address and HTTP_HOST test',
  6. 'description' => 'Get the IP address from the current visitor from the server variables, check hostname validation.',
  7. 'group' => 'Bootstrap'
  8. );
  9. }
  10. function setUp() {
  11. $this->oldserver = $_SERVER;
  12. $this->remote_ip = '127.0.0.1';
  13. $this->proxy_ip = '127.0.0.2';
  14. $this->proxy2_ip = '127.0.0.3';
  15. $this->forwarded_ip = '127.0.0.4';
  16. $this->cluster_ip = '127.0.0.5';
  17. $this->untrusted_ip = '0.0.0.0';
  18. drupal_static_reset('ip_address');
  19. $_SERVER['REMOTE_ADDR'] = $this->remote_ip;
  20. unset($_SERVER['HTTP_X_FORWARDED_FOR']);
  21. unset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']);
  22. parent::setUp();
  23. }
  24. function tearDown() {
  25. $_SERVER = $this->oldserver;
  26. drupal_static_reset('ip_address');
  27. parent::tearDown();
  28. }
  29. /**
  30. * test IP Address and hostname
  31. */
  32. function testIPAddressHost() {
  33. // Test the normal IP address.
  34. $this->assertTrue(
  35. ip_address() == $this->remote_ip,
  36. t('Got remote IP address.')
  37. );
  38. // Proxy forwarding on but no proxy addresses defined.
  39. variable_set('reverse_proxy', 1);
  40. $this->assertTrue(
  41. ip_address() == $this->remote_ip,
  42. t('Proxy forwarding without trusted proxies got remote IP address.')
  43. );
  44. // Proxy forwarding on and proxy address not trusted.
  45. variable_set('reverse_proxy_addresses', array($this->proxy_ip, $this->proxy2_ip));
  46. drupal_static_reset('ip_address');
  47. $_SERVER['REMOTE_ADDR'] = $this->untrusted_ip;
  48. $this->assertTrue(
  49. ip_address() == $this->untrusted_ip,
  50. t('Proxy forwarding with untrusted proxy got remote IP address.')
  51. );
  52. // Proxy forwarding on and proxy address trusted.
  53. $_SERVER['REMOTE_ADDR'] = $this->proxy_ip;
  54. $_SERVER['HTTP_X_FORWARDED_FOR'] = $this->forwarded_ip;
  55. drupal_static_reset('ip_address');
  56. $this->assertTrue(
  57. ip_address() == $this->forwarded_ip,
  58. t('Proxy forwarding with trusted proxy got forwarded IP address.')
  59. );
  60. // Multi-tier architecture with comma separated values in header.
  61. $_SERVER['REMOTE_ADDR'] = $this->proxy_ip;
  62. $_SERVER['HTTP_X_FORWARDED_FOR'] = implode(', ', array($this->untrusted_ip, $this->forwarded_ip, $this->proxy2_ip));
  63. drupal_static_reset('ip_address');
  64. $this->assertTrue(
  65. ip_address() == $this->forwarded_ip,
  66. t('Proxy forwarding with trusted 2-tier proxy got forwarded IP address.')
  67. );
  68. // Custom client-IP header.
  69. variable_set('reverse_proxy_header', 'HTTP_X_CLUSTER_CLIENT_IP');
  70. $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'] = $this->cluster_ip;
  71. drupal_static_reset('ip_address');
  72. $this->assertTrue(
  73. ip_address() == $this->cluster_ip,
  74. t('Cluster environment got cluster client IP.')
  75. );
  76. // Verifies that drupal_valid_http_host() prevents invalid characters.
  77. $this->assertFalse(drupal_valid_http_host('security/.drupal.org:80'), t('HTTP_HOST with / is invalid'));
  78. $this->assertFalse(drupal_valid_http_host('security\\.drupal.org:80'), t('HTTP_HOST with \\ is invalid'));
  79. $this->assertFalse(drupal_valid_http_host('security<.drupal.org:80'), t('HTTP_HOST with &lt; is invalid'));
  80. $this->assertFalse(drupal_valid_http_host('security..drupal.org:80'), t('HTTP_HOST with .. is invalid'));
  81. // IPv6 loopback address
  82. $this->assertTrue(drupal_valid_http_host('[::1]:80'), t('HTTP_HOST containing IPv6 loopback is valid'));
  83. }
  84. }
  85. class BootstrapPageCacheTestCase extends DrupalWebTestCase {
  86. public static function getInfo() {
  87. return array(
  88. 'name' => 'Page cache test',
  89. 'description' => 'Enable the page cache and test it with various HTTP requests.',
  90. 'group' => 'Bootstrap'
  91. );
  92. }
  93. function setUp() {
  94. parent::setUp('system_test');
  95. }
  96. /**
  97. * Test support for requests containing If-Modified-Since and If-None-Match headers.
  98. */
  99. function testConditionalRequests() {
  100. variable_set('cache', 1);
  101. // Fill the cache.
  102. $this->drupalGet('');
  103. $this->drupalHead('');
  104. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
  105. $etag = $this->drupalGetHeader('ETag');
  106. $last_modified = $this->drupalGetHeader('Last-Modified');
  107. $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
  108. $this->assertResponse(304, t('Conditional request returned 304 Not Modified.'));
  109. $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC822, strtotime($last_modified)), 'If-None-Match: ' . $etag));
  110. $this->assertResponse(304, t('Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.'));
  111. $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC850, strtotime($last_modified)), 'If-None-Match: ' . $etag));
  112. $this->assertResponse(304, t('Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.'));
  113. $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified));
  114. $this->assertResponse(200, t('Conditional request without If-None-Match returned 200 OK.'));
  115. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
  116. $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC1123, strtotime($last_modified) + 1), 'If-None-Match: ' . $etag));
  117. $this->assertResponse(200, t('Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.'));
  118. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
  119. $user = $this->drupalCreateUser();
  120. $this->drupalLogin($user);
  121. $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
  122. $this->assertResponse(200, t('Conditional request returned 200 OK for authenticated user.'));
  123. $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), t('Absense of Page was not cached.'));
  124. }
  125. /**
  126. * Test cache headers.
  127. */
  128. function testPageCache() {
  129. variable_set('cache', 1);
  130. // Fill the cache.
  131. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
  132. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', t('Page was not cached.'));
  133. $this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', t('Vary header was sent.'));
  134. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', t('Cache-Control header was sent.'));
  135. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
  136. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', t('Custom header was sent.'));
  137. // Check cache.
  138. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
  139. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
  140. $this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', t('Vary: Cookie header was sent.'));
  141. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', t('Cache-Control header was sent.'));
  142. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
  143. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', t('Custom header was sent.'));
  144. // Check replacing default headers.
  145. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Expires', 'value' => 'Fri, 19 Nov 2008 05:00:00 GMT')));
  146. $this->assertEqual($this->drupalGetHeader('Expires'), 'Fri, 19 Nov 2008 05:00:00 GMT', t('Default header was replaced.'));
  147. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Vary', 'value' => 'User-Agent')));
  148. $this->assertEqual($this->drupalGetHeader('Vary'), 'User-Agent,Accept-Encoding', t('Default header was replaced.'));
  149. // Check that authenticated users bypass the cache.
  150. $user = $this->drupalCreateUser();
  151. $this->drupalLogin($user);
  152. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
  153. $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), t('Caching was bypassed.'));
  154. $this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, t('Vary: Cookie header was not sent.'));
  155. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', t('Cache-Control header was sent.'));
  156. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
  157. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', t('Custom header was sent.'));
  158. }
  159. /**
  160. * Test page compression.
  161. *
  162. * The test should pass even if zlib.output_compression is enabled in php.ini,
  163. * .htaccess or similar, or if compression is done outside PHP, e.g. by the
  164. * mod_deflate Apache module.
  165. */
  166. function testPageCompression() {
  167. variable_set('cache', 1);
  168. // Fill the cache and verify that output is compressed.
  169. $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
  170. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', t('Page was not cached.'));
  171. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  172. $this->assertRaw('</html>', t('Page was gzip compressed.'));
  173. // Verify that cached output is compressed.
  174. $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
  175. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
  176. $this->assertEqual($this->drupalGetHeader('Content-Encoding'), 'gzip', t('A Content-Encoding header was sent.'));
  177. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  178. $this->assertRaw('</html>', t('Page was gzip compressed.'));
  179. // Verify that a client without compression support gets an uncompressed page.
  180. $this->drupalGet('');
  181. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
  182. $this->assertFalse($this->drupalGetHeader('Content-Encoding'), t('A Content-Encoding header was not sent.'));
  183. $this->assertTitle(t('Welcome to @site-name | @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))), t('Site title matches.'));
  184. $this->assertRaw('</html>', t('Page was not compressed.'));
  185. }
  186. }
  187. class BootstrapVariableTestCase extends DrupalWebTestCase {
  188. function setUp() {
  189. parent::setUp('system_test');
  190. }
  191. public static function getInfo() {
  192. return array(
  193. 'name' => 'Variable test',
  194. 'description' => 'Make sure the variable system functions correctly.',
  195. 'group' => 'Bootstrap'
  196. );
  197. }
  198. /**
  199. * testVariable
  200. */
  201. function testVariable() {
  202. // Setting and retrieving values.
  203. $variable = $this->randomName();
  204. variable_set('simpletest_bootstrap_variable_test', $variable);
  205. $this->assertIdentical($variable, variable_get('simpletest_bootstrap_variable_test'), t('Setting and retrieving values'));
  206. // Make sure the variable persists across multiple requests.
  207. $this->drupalGet('system-test/variable-get');
  208. $this->assertText($variable, t('Variable persists across multiple requests'));
  209. // Deleting variables.
  210. $default_value = $this->randomName();
  211. variable_del('simpletest_bootstrap_variable_test');
  212. $variable = variable_get('simpletest_bootstrap_variable_test', $default_value);
  213. $this->assertIdentical($variable, $default_value, t('Deleting variables'));
  214. }
  215. /**
  216. * Makes sure that the default variable parameter is passed through okay.
  217. */
  218. function testVariableDefaults() {
  219. // Tests passing nothing through to the default.
  220. $this->assertIdentical(NULL, variable_get('simpletest_bootstrap_variable_test'), t('Variables are correctly defaulting to NULL.'));
  221. // Tests passing 5 to the default parameter.
  222. $this->assertIdentical(5, variable_get('simpletest_bootstrap_variable_test', 5), t('The default variable parameter is passed through correctly.'));
  223. }
  224. }
  225. /**
  226. * Test hook_boot() and hook_exit().
  227. */
  228. class HookBootExitTestCase extends DrupalWebTestCase {
  229. public static function getInfo() {
  230. return array(
  231. 'name' => 'Boot and exit hook invocation',
  232. 'description' => 'Test that hook_boot() and hook_exit() are called correctly.',
  233. 'group' => 'Bootstrap',
  234. );
  235. }
  236. function setUp() {
  237. parent::setUp('system_test', 'dblog');
  238. }
  239. /**
  240. * Test calling of hook_boot() and hook_exit().
  241. */
  242. function testHookBootExit() {
  243. // Test with cache disabled. Boot and exit should always fire.
  244. variable_set('cache', 0);
  245. $this->drupalGet('');
  246. $calls = 1;
  247. $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot called with disabled cache.'));
  248. $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit called with disabled cache.'));
  249. // Test with normal cache. Boot and exit should be called.
  250. variable_set('cache', 1);
  251. $this->drupalGet('');
  252. $calls++;
  253. $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot called with normal cache.'));
  254. $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit called with normal cache.'));
  255. // Boot and exit should not fire since the page is cached.
  256. variable_set('page_cache_invoke_hooks', FALSE);
  257. $this->assertTrue(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has been cached.'));
  258. $this->drupalGet('');
  259. $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot not called with agressive cache and a cached page.'));
  260. $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit not called with agressive cache and a cached page.'));
  261. // Test with page cache cleared, boot and exit should be called.
  262. $this->assertTrue(db_delete('cache_page')->execute(), t('Page cache cleared.'));
  263. $this->drupalGet('');
  264. $calls++;
  265. $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot called with agressive cache and no cached page.'));
  266. $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit called with agressive cache and no cached page.'));
  267. }
  268. }
  269. /**
  270. * Test drupal_get_filename()'s availability.
  271. */
  272. class BootstrapGetFilenameTestCase extends DrupalUnitTestCase {
  273. public static function getInfo() {
  274. return array(
  275. 'name' => 'Get filename test',
  276. 'description' => 'Test that drupal_get_filename() works correctly when the file is not found in the database.',
  277. 'group' => 'Bootstrap',
  278. );
  279. }
  280. /**
  281. * Test that drupal_get_filename() works correctly when the file is not found in the database.
  282. */
  283. function testDrupalGetFilename() {
  284. // Reset the static cache so we can test the "db is not active" code of
  285. // drupal_get_filename().
  286. drupal_static_reset('drupal_get_filename');
  287. // Retrieving the location of a module.
  288. $this->assertIdentical(drupal_get_filename('module', 'php'), 'modules/php/php.module', t('Retrieve module location.'));
  289. // Retrieving the location of a theme.
  290. $this->assertIdentical(drupal_get_filename('theme', 'stark'), 'themes/stark/stark.info', t('Retrieve theme location.'));
  291. // Retrieving the location of a theme engine.
  292. $this->assertIdentical(drupal_get_filename('theme_engine', 'phptemplate'), 'themes/engines/phptemplate/phptemplate.engine', t('Retrieve theme engine location.'));
  293. // Retrieving a file that is definitely not stored in the database.
  294. $this->assertIdentical(drupal_get_filename('profile', 'standard'), 'profiles/standard/standard.profile', t('Retrieve install profile location.'));
  295. }
  296. }
  297. class BootstrapTimerTestCase extends DrupalUnitTestCase {
  298. public static function getInfo() {
  299. return array(
  300. 'name' => 'Timer test',
  301. 'description' => 'Test that timer_read() works both when a timer is running and when a timer is stopped.',
  302. 'group' => 'Bootstrap',
  303. );
  304. }
  305. /**
  306. * Test timer_read() to ensure it properly accumulates time when the timer
  307. * started and stopped multiple times.
  308. * @return
  309. */
  310. function testTimer() {
  311. timer_start('test');
  312. sleep(1);
  313. $this->assertTrue(timer_read('test') >= 1000, t('Timer measured 1 second of sleeping while running.'));
  314. sleep(1);
  315. timer_stop('test');
  316. $this->assertTrue(timer_read('test') >= 2000, t('Timer measured 2 seconds of sleeping after being stopped.'));
  317. timer_start('test');
  318. sleep(1);
  319. $this->assertTrue(timer_read('test') >= 3000, t('Timer measured 3 seconds of sleeping after being restarted.'));
  320. sleep(1);
  321. $timer = timer_stop('test');
  322. $this->assertTrue(timer_read('test') >= 4000, t('Timer measured 4 seconds of sleeping after being stopped for a second time.'));
  323. $this->assertEqual($timer['count'], 2, t('Timer counted 2 instances of being started.'));
  324. }
  325. }
  326. /**
  327. * Test that resetting static variables works.
  328. */
  329. class BootstrapResettableStaticTestCase extends DrupalUnitTestCase {
  330. public static function getInfo() {
  331. return array(
  332. 'name' => 'Resettable static variables test',
  333. 'description' => 'Test that drupal_static() and drupal_static_reset() work.',
  334. 'group' => 'Bootstrap',
  335. );
  336. }
  337. /**
  338. * Test that a variable reference returned by drupal_static() gets reset when
  339. * drupal_static_reset() is called.
  340. */
  341. function testDrupalStatic() {
  342. $name = __CLASS__ . '_' . __METHOD__;
  343. $var = &drupal_static($name, 'foo');
  344. $this->assertEqual($var, 'foo', t('Variable returned by drupal_static() was set to its default.'));
  345. // Call the specific reset and the global reset each twice to ensure that
  346. // multiple resets can be issued without odd side effects.
  347. $var = 'bar';
  348. drupal_static_reset($name);
  349. $this->assertEqual($var, 'foo', t('Variable was reset after first invocation of name-specific reset.'));
  350. $var = 'bar';
  351. drupal_static_reset($name);
  352. $this->assertEqual($var, 'foo', t('Variable was reset after second invocation of name-specific reset.'));
  353. $var = 'bar';
  354. drupal_static_reset();
  355. $this->assertEqual($var, 'foo', t('Variable was reset after first invocation of global reset.'));
  356. $var = 'bar';
  357. drupal_static_reset();
  358. $this->assertEqual($var, 'foo', t('Variable was reset after second invocation of global reset.'));
  359. }
  360. }
  361. /**
  362. * Test miscellaneous functions in bootstrap.inc.
  363. */
  364. class BootstrapMiscTestCase extends DrupalUnitTestCase {
  365. public static function getInfo() {
  366. return array(
  367. 'name' => 'Miscellaneous bootstrap unit tests',
  368. 'description' => 'Test miscellaneous functions in bootstrap.inc.',
  369. 'group' => 'Bootstrap',
  370. );
  371. }
  372. /**
  373. * Test miscellaneous functions in bootstrap.inc.
  374. */
  375. function testMisc() {
  376. // Test drupal_array_merge_deep().
  377. $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => 'X', 'class' => array('a', 'b')), 'language' => 'en');
  378. $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('c', 'd')), 'html' => TRUE);
  379. $expected = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('a', 'b', 'c', 'd')), 'language' => 'en', 'html' => TRUE);
  380. $this->assertIdentical(drupal_array_merge_deep($link_options_1, $link_options_2), $expected, t('drupal_array_merge_deep() returned a properly merged array.'));
  381. }
  382. }
  383. /**
  384. * Tests for overriding server variables via the API.
  385. */
  386. class BootstrapOverrideServerVariablesTestCase extends DrupalUnitTestCase {
  387. public static function getInfo() {
  388. return array(
  389. 'name' => 'Overriding server variables',
  390. 'description' => 'Test that drupal_override_server_variables() works correctly.',
  391. 'group' => 'Bootstrap',
  392. );
  393. }
  394. /**
  395. * Test providing a direct URL to to drupal_override_server_variables().
  396. */
  397. function testDrupalOverrideServerVariablesProvidedURL() {
  398. $tests = array(
  399. 'http://example.com' => array(
  400. 'HTTP_HOST' => 'example.com',
  401. 'SCRIPT_NAME' => isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL,
  402. ),
  403. 'http://example.com/index.php' => array(
  404. 'HTTP_HOST' => 'example.com',
  405. 'SCRIPT_NAME' => '/index.php',
  406. ),
  407. 'http://example.com/subdirectory/index.php' => array(
  408. 'HTTP_HOST' => 'example.com',
  409. 'SCRIPT_NAME' => '/subdirectory/index.php',
  410. ),
  411. );
  412. foreach ($tests as $url => $expected_server_values) {
  413. // Remember the original value of $_SERVER, since the function call below
  414. // will modify it.
  415. $original_server = $_SERVER;
  416. // Call drupal_override_server_variables() and ensure that all expected
  417. // $_SERVER variables were modified correctly.
  418. drupal_override_server_variables(array('url' => $url));
  419. foreach ($expected_server_values as $key => $value) {
  420. $this->assertIdentical($_SERVER[$key], $value);
  421. }
  422. // Restore the original value of $_SERVER.
  423. $_SERVER = $original_server;
  424. }
  425. }
  426. }