bootstrap.test 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. '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. '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. '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. '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. '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. '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'), 'HTTP_HOST with / is invalid');
  78. $this->assertFalse(drupal_valid_http_host('security\\.drupal.org:80'), 'HTTP_HOST with \\ is invalid');
  79. $this->assertFalse(drupal_valid_http_host('security<.drupal.org:80'), 'HTTP_HOST with &lt; is invalid');
  80. $this->assertFalse(drupal_valid_http_host('security..drupal.org:80'), 'HTTP_HOST with .. is invalid');
  81. // IPv6 loopback address
  82. $this->assertTrue(drupal_valid_http_host('[::1]:80'), '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', '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, '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, '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, '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, 'Conditional request without If-None-Match returned 200 OK.');
  115. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', '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, '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', '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, 'Conditional request returned 200 OK for authenticated user.');
  123. $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), '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', 'Page was not cached.');
  133. $this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary header was sent.');
  134. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', 'Cache-Control header was sent.');
  135. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  136. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', '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', 'Page was cached.');
  140. $this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary: Cookie header was sent.');
  141. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', 'Cache-Control header was sent.');
  142. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  143. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', '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', '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', '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'), 'Caching was bypassed.');
  154. $this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, 'Vary: Cookie header was not sent.');
  155. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', 'Cache-Control header was sent.');
  156. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  157. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', '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', 'Page was not cached.');
  171. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  172. $this->assertRaw('</html>', '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', 'Page was cached.');
  176. $this->assertEqual($this->drupalGetHeader('Content-Encoding'), 'gzip', 'A Content-Encoding header was sent.');
  177. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  178. $this->assertRaw('</html>', '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', 'Page was cached.');
  182. $this->assertFalse($this->drupalGetHeader('Content-Encoding'), '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'))), 'Site title matches.');
  184. $this->assertRaw('</html>', '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'), 'Setting and retrieving values');
  206. // Make sure the variable persists across multiple requests.
  207. $this->drupalGet('system-test/variable-get');
  208. $this->assertText($variable, '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, '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'), '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), '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 aggressive 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 aggressive 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 aggressive 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 aggressive 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 the location of a profile. Profiles are a special case with
  294. // a fixed location and naming.
  295. $this->assertIdentical(drupal_get_filename('profile', 'standard'), 'profiles/standard/standard.profile', t('Retrieve install profile location.'));
  296. // When a file is not found in the database cache, drupal_get_filename()
  297. // searches several locations on the filesystem, including the DRUPAL_ROOT
  298. // directory. We use the '.script' extension below because this is a
  299. // non-existent filetype that will definitely not exist in the database.
  300. // Since there is already a scripts directory, drupal_get_filename() will
  301. // automatically check there for 'script' files, just as it does for (e.g.)
  302. // 'module' files in modules.
  303. $this->assertIdentical(drupal_get_filename('script', 'test'), 'scripts/test.script', t('Retrieve test script location.'));
  304. }
  305. }
  306. class BootstrapTimerTestCase extends DrupalUnitTestCase {
  307. public static function getInfo() {
  308. return array(
  309. 'name' => 'Timer test',
  310. 'description' => 'Test that timer_read() works both when a timer is running and when a timer is stopped.',
  311. 'group' => 'Bootstrap',
  312. );
  313. }
  314. /**
  315. * Test timer_read() to ensure it properly accumulates time when the timer
  316. * started and stopped multiple times.
  317. * @return
  318. */
  319. function testTimer() {
  320. timer_start('test');
  321. sleep(1);
  322. $this->assertTrue(timer_read('test') >= 1000, 'Timer measured 1 second of sleeping while running.');
  323. sleep(1);
  324. timer_stop('test');
  325. $this->assertTrue(timer_read('test') >= 2000, 'Timer measured 2 seconds of sleeping after being stopped.');
  326. timer_start('test');
  327. sleep(1);
  328. $this->assertTrue(timer_read('test') >= 3000, 'Timer measured 3 seconds of sleeping after being restarted.');
  329. sleep(1);
  330. $timer = timer_stop('test');
  331. $this->assertTrue(timer_read('test') >= 4000, 'Timer measured 4 seconds of sleeping after being stopped for a second time.');
  332. $this->assertEqual($timer['count'], 2, 'Timer counted 2 instances of being started.');
  333. }
  334. }
  335. /**
  336. * Test that resetting static variables works.
  337. */
  338. class BootstrapResettableStaticTestCase extends DrupalUnitTestCase {
  339. public static function getInfo() {
  340. return array(
  341. 'name' => 'Resettable static variables test',
  342. 'description' => 'Test that drupal_static() and drupal_static_reset() work.',
  343. 'group' => 'Bootstrap',
  344. );
  345. }
  346. /**
  347. * Test that a variable reference returned by drupal_static() gets reset when
  348. * drupal_static_reset() is called.
  349. */
  350. function testDrupalStatic() {
  351. $name = __CLASS__ . '_' . __METHOD__;
  352. $var = &drupal_static($name, 'foo');
  353. $this->assertEqual($var, 'foo', 'Variable returned by drupal_static() was set to its default.');
  354. // Call the specific reset and the global reset each twice to ensure that
  355. // multiple resets can be issued without odd side effects.
  356. $var = 'bar';
  357. drupal_static_reset($name);
  358. $this->assertEqual($var, 'foo', 'Variable was reset after first invocation of name-specific reset.');
  359. $var = 'bar';
  360. drupal_static_reset($name);
  361. $this->assertEqual($var, 'foo', 'Variable was reset after second invocation of name-specific reset.');
  362. $var = 'bar';
  363. drupal_static_reset();
  364. $this->assertEqual($var, 'foo', 'Variable was reset after first invocation of global reset.');
  365. $var = 'bar';
  366. drupal_static_reset();
  367. $this->assertEqual($var, 'foo', 'Variable was reset after second invocation of global reset.');
  368. }
  369. }
  370. /**
  371. * Test miscellaneous functions in bootstrap.inc.
  372. */
  373. class BootstrapMiscTestCase extends DrupalUnitTestCase {
  374. public static function getInfo() {
  375. return array(
  376. 'name' => 'Miscellaneous bootstrap unit tests',
  377. 'description' => 'Test miscellaneous functions in bootstrap.inc.',
  378. 'group' => 'Bootstrap',
  379. );
  380. }
  381. /**
  382. * Test miscellaneous functions in bootstrap.inc.
  383. */
  384. function testMisc() {
  385. // Test drupal_array_merge_deep().
  386. $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => 'X', 'class' => array('a', 'b')), 'language' => 'en');
  387. $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('c', 'd')), 'html' => TRUE);
  388. $expected = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('a', 'b', 'c', 'd')), 'language' => 'en', 'html' => TRUE);
  389. $this->assertIdentical(drupal_array_merge_deep($link_options_1, $link_options_2), $expected, 'drupal_array_merge_deep() returned a properly merged array.');
  390. }
  391. /**
  392. * Tests that the drupal_check_memory_limit() function works as expected.
  393. */
  394. function testCheckMemoryLimit() {
  395. $memory_limit = ini_get('memory_limit');
  396. // Test that a very reasonable amount of memory is available.
  397. $this->assertTrue(drupal_check_memory_limit('30MB'), '30MB of memory tested available.');
  398. // Get the available memory and multiply it by two to make it unreasonably
  399. // high.
  400. $twice_avail_memory = ($memory_limit * 2) . 'MB';
  401. // The function should always return true if the memory limit is set to -1.
  402. $this->assertTrue(drupal_check_memory_limit($twice_avail_memory, -1), 'drupal_check_memory_limit() returns TRUE when a limit of -1 (none) is supplied');
  403. // Test that even though we have 30MB of memory available - the function
  404. // returns FALSE when given an upper limit for how much memory can be used.
  405. $this->assertFalse(drupal_check_memory_limit('30MB', '16MB'), 'drupal_check_memory_limit() returns FALSE with a 16MB upper limit on a 30MB requirement.');
  406. // Test that an equal amount of memory to the amount requested returns TRUE.
  407. $this->assertTrue(drupal_check_memory_limit('30MB', '30MB'), 'drupal_check_memory_limit() returns TRUE when requesting 30MB on a 30MB requirement.');
  408. }
  409. }
  410. /**
  411. * Tests for overriding server variables via the API.
  412. */
  413. class BootstrapOverrideServerVariablesTestCase extends DrupalUnitTestCase {
  414. public static function getInfo() {
  415. return array(
  416. 'name' => 'Overriding server variables',
  417. 'description' => 'Test that drupal_override_server_variables() works correctly.',
  418. 'group' => 'Bootstrap',
  419. );
  420. }
  421. /**
  422. * Test providing a direct URL to to drupal_override_server_variables().
  423. */
  424. function testDrupalOverrideServerVariablesProvidedURL() {
  425. $tests = array(
  426. 'http://example.com' => array(
  427. 'HTTP_HOST' => 'example.com',
  428. 'SCRIPT_NAME' => isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL,
  429. ),
  430. 'http://example.com/index.php' => array(
  431. 'HTTP_HOST' => 'example.com',
  432. 'SCRIPT_NAME' => '/index.php',
  433. ),
  434. 'http://example.com/subdirectory/index.php' => array(
  435. 'HTTP_HOST' => 'example.com',
  436. 'SCRIPT_NAME' => '/subdirectory/index.php',
  437. ),
  438. );
  439. foreach ($tests as $url => $expected_server_values) {
  440. // Remember the original value of $_SERVER, since the function call below
  441. // will modify it.
  442. $original_server = $_SERVER;
  443. // Call drupal_override_server_variables() and ensure that all expected
  444. // $_SERVER variables were modified correctly.
  445. drupal_override_server_variables(array('url' => $url));
  446. foreach ($expected_server_values as $key => $value) {
  447. $this->assertIdentical($_SERVER[$key], $value);
  448. }
  449. // Restore the original value of $_SERVER.
  450. $_SERVER = $original_server;
  451. }
  452. }
  453. }