bootstrap.test 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. // Verifies that host names are shorter than 1000 characters.
  82. $this->assertFalse(drupal_valid_http_host(str_repeat('x', 1001)), 'HTTP_HOST with more than 1000 characters is invalid.');
  83. $this->assertFalse(drupal_valid_http_host(str_repeat('.', 101)), 'HTTP_HOST with more than 100 subdomains is invalid.');
  84. $this->assertFalse(drupal_valid_http_host(str_repeat(':', 101)), 'HTTP_HOST with more than 100 portseparators is invalid.');
  85. // IPv6 loopback address
  86. $this->assertTrue(drupal_valid_http_host('[::1]:80'), 'HTTP_HOST containing IPv6 loopback is valid');
  87. }
  88. }
  89. class BootstrapPageCacheTestCase extends DrupalWebTestCase {
  90. public static function getInfo() {
  91. return array(
  92. 'name' => 'Page cache test',
  93. 'description' => 'Enable the page cache and test it with various HTTP requests.',
  94. 'group' => 'Bootstrap'
  95. );
  96. }
  97. function setUp() {
  98. parent::setUp('system_test');
  99. }
  100. /**
  101. * Test support for requests containing If-Modified-Since and If-None-Match headers.
  102. */
  103. function testConditionalRequests() {
  104. variable_set('cache', 1);
  105. // Fill the cache.
  106. $this->drupalGet('');
  107. $this->drupalHead('');
  108. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  109. $etag = $this->drupalGetHeader('ETag');
  110. $last_modified = $this->drupalGetHeader('Last-Modified');
  111. $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
  112. $this->assertResponse(304, 'Conditional request returned 304 Not Modified.');
  113. $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC822, strtotime($last_modified)), 'If-None-Match: ' . $etag));
  114. $this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
  115. $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC850, strtotime($last_modified)), 'If-None-Match: ' . $etag));
  116. $this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
  117. $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified));
  118. $this->assertResponse(200, 'Conditional request without If-None-Match returned 200 OK.');
  119. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  120. $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC7231, strtotime($last_modified) + 1), 'If-None-Match: ' . $etag));
  121. $this->assertResponse(200, 'Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.');
  122. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  123. $user = $this->drupalCreateUser();
  124. $this->drupalLogin($user);
  125. $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
  126. $this->assertResponse(200, 'Conditional request returned 200 OK for authenticated user.');
  127. $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Absense of Page was not cached.');
  128. }
  129. /**
  130. * Test cache headers.
  131. */
  132. function testPageCache() {
  133. variable_set('cache', 1);
  134. // Fill the cache.
  135. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
  136. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  137. $this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary header was sent.');
  138. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', 'Cache-Control header was sent.');
  139. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  140. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
  141. // Check cache.
  142. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
  143. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  144. $this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary: Cookie header was sent.');
  145. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', 'Cache-Control header was sent.');
  146. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  147. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
  148. // Check replacing default headers.
  149. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Expires', 'value' => 'Fri, 19 Nov 2008 05:00:00 GMT')));
  150. $this->assertEqual($this->drupalGetHeader('Expires'), 'Fri, 19 Nov 2008 05:00:00 GMT', 'Default header was replaced.');
  151. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Vary', 'value' => 'User-Agent')));
  152. $this->assertEqual($this->drupalGetHeader('Vary'), 'User-Agent,Accept-Encoding', 'Default header was replaced.');
  153. // Check that authenticated users bypass the cache.
  154. $user = $this->drupalCreateUser();
  155. $this->drupalLogin($user);
  156. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
  157. $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.');
  158. $this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, 'Vary: Cookie header was not sent.');
  159. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', 'Cache-Control header was sent.');
  160. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  161. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
  162. }
  163. /**
  164. * Test page compression.
  165. *
  166. * The test should pass even if zlib.output_compression is enabled in php.ini,
  167. * .htaccess or similar, or if compression is done outside PHP, e.g. by the
  168. * mod_deflate Apache module.
  169. */
  170. function testPageCompression() {
  171. variable_set('cache', 1);
  172. // Fill the cache and verify that output is compressed.
  173. $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
  174. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  175. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  176. $this->assertRaw('</html>', 'Page was gzip compressed.');
  177. // Verify that cached output is compressed.
  178. $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
  179. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  180. $this->assertEqual($this->drupalGetHeader('Content-Encoding'), 'gzip', 'A Content-Encoding header was sent.');
  181. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  182. $this->assertRaw('</html>', 'Page was gzip compressed.');
  183. // Verify that a client without compression support gets an uncompressed page.
  184. $this->drupalGet('');
  185. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  186. $this->assertFalse($this->drupalGetHeader('Content-Encoding'), 'A Content-Encoding header was not sent.');
  187. $this->assertTitle(t('Welcome to @site-name | @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))), 'Site title matches.');
  188. $this->assertRaw('</html>', 'Page was not compressed.');
  189. // Disable compression mode.
  190. variable_set('page_compression', FALSE);
  191. // Verify if cached page is still available for a client with compression support.
  192. $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
  193. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  194. $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support enabled).');
  195. // Verify if cached page is still available for a client without compression support.
  196. $this->drupalGet('');
  197. $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support disabled).');
  198. }
  199. }
  200. class BootstrapVariableTestCase extends DrupalWebTestCase {
  201. function setUp() {
  202. parent::setUp('system_test');
  203. }
  204. public static function getInfo() {
  205. return array(
  206. 'name' => 'Variable test',
  207. 'description' => 'Make sure the variable system functions correctly.',
  208. 'group' => 'Bootstrap'
  209. );
  210. }
  211. /**
  212. * testVariable
  213. */
  214. function testVariable() {
  215. // Setting and retrieving values.
  216. $variable = $this->randomName();
  217. variable_set('simpletest_bootstrap_variable_test', $variable);
  218. $this->assertIdentical($variable, variable_get('simpletest_bootstrap_variable_test'), 'Setting and retrieving values');
  219. // Make sure the variable persists across multiple requests.
  220. $this->drupalGet('system-test/variable-get');
  221. $this->assertText($variable, 'Variable persists across multiple requests');
  222. // Deleting variables.
  223. $default_value = $this->randomName();
  224. variable_del('simpletest_bootstrap_variable_test');
  225. $variable = variable_get('simpletest_bootstrap_variable_test', $default_value);
  226. $this->assertIdentical($variable, $default_value, 'Deleting variables');
  227. }
  228. /**
  229. * Makes sure that the default variable parameter is passed through okay.
  230. */
  231. function testVariableDefaults() {
  232. // Tests passing nothing through to the default.
  233. $this->assertIdentical(NULL, variable_get('simpletest_bootstrap_variable_test'), 'Variables are correctly defaulting to NULL.');
  234. // Tests passing 5 to the default parameter.
  235. $this->assertIdentical(5, variable_get('simpletest_bootstrap_variable_test', 5), 'The default variable parameter is passed through correctly.');
  236. }
  237. }
  238. /**
  239. * Test hook_boot() and hook_exit().
  240. */
  241. class HookBootExitTestCase extends DrupalWebTestCase {
  242. public static function getInfo() {
  243. return array(
  244. 'name' => 'Boot and exit hook invocation',
  245. 'description' => 'Test that hook_boot() and hook_exit() are called correctly.',
  246. 'group' => 'Bootstrap',
  247. );
  248. }
  249. function setUp() {
  250. parent::setUp('system_test', 'dblog');
  251. }
  252. /**
  253. * Test calling of hook_boot() and hook_exit().
  254. */
  255. function testHookBootExit() {
  256. // Test with cache disabled. Boot and exit should always fire.
  257. variable_set('cache', 0);
  258. $this->drupalGet('');
  259. $calls = 1;
  260. $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.'));
  261. $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.'));
  262. // Test with normal cache. Boot and exit should be called.
  263. variable_set('cache', 1);
  264. $this->drupalGet('');
  265. $calls++;
  266. $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.'));
  267. $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.'));
  268. // Boot and exit should not fire since the page is cached.
  269. variable_set('page_cache_invoke_hooks', FALSE);
  270. $this->assertTrue(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has been cached.'));
  271. $this->drupalGet('');
  272. $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.'));
  273. $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.'));
  274. // Test with page cache cleared, boot and exit should be called.
  275. $this->assertTrue(db_delete('cache_page')->execute(), t('Page cache cleared.'));
  276. $this->drupalGet('');
  277. $calls++;
  278. $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.'));
  279. $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.'));
  280. }
  281. }
  282. /**
  283. * Test drupal_get_filename()'s availability.
  284. */
  285. class BootstrapGetFilenameTestCase extends DrupalUnitTestCase {
  286. public static function getInfo() {
  287. return array(
  288. 'name' => 'Get filename test',
  289. 'description' => 'Test that drupal_get_filename() works correctly when the file is not found in the database.',
  290. 'group' => 'Bootstrap',
  291. );
  292. }
  293. /**
  294. * Test that drupal_get_filename() works correctly when the file is not found in the database.
  295. */
  296. function testDrupalGetFilename() {
  297. // Reset the static cache so we can test the "db is not active" code of
  298. // drupal_get_filename().
  299. drupal_static_reset('drupal_get_filename');
  300. // Retrieving the location of a module.
  301. $this->assertIdentical(drupal_get_filename('module', 'php'), 'modules/php/php.module', t('Retrieve module location.'));
  302. // Retrieving the location of a theme.
  303. $this->assertIdentical(drupal_get_filename('theme', 'stark'), 'themes/stark/stark.info', t('Retrieve theme location.'));
  304. // Retrieving the location of a theme engine.
  305. $this->assertIdentical(drupal_get_filename('theme_engine', 'phptemplate'), 'themes/engines/phptemplate/phptemplate.engine', t('Retrieve theme engine location.'));
  306. // Retrieving the location of a profile. Profiles are a special case with
  307. // a fixed location and naming.
  308. $this->assertIdentical(drupal_get_filename('profile', 'standard'), 'profiles/standard/standard.profile', t('Retrieve install profile location.'));
  309. // When a file is not found in the database cache, drupal_get_filename()
  310. // searches several locations on the filesystem, including the DRUPAL_ROOT
  311. // directory. We use the '.script' extension below because this is a
  312. // non-existent filetype that will definitely not exist in the database.
  313. // Since there is already a scripts directory, drupal_get_filename() will
  314. // automatically check there for 'script' files, just as it does for (e.g.)
  315. // 'module' files in modules.
  316. $this->assertIdentical(drupal_get_filename('script', 'test'), 'scripts/test.script', t('Retrieve test script location.'));
  317. }
  318. }
  319. class BootstrapTimerTestCase extends DrupalUnitTestCase {
  320. public static function getInfo() {
  321. return array(
  322. 'name' => 'Timer test',
  323. 'description' => 'Test that timer_read() works both when a timer is running and when a timer is stopped.',
  324. 'group' => 'Bootstrap',
  325. );
  326. }
  327. /**
  328. * Test timer_read() to ensure it properly accumulates time when the timer
  329. * started and stopped multiple times.
  330. * @return
  331. */
  332. function testTimer() {
  333. timer_start('test');
  334. sleep(1);
  335. $this->assertTrue(timer_read('test') >= 1000, 'Timer measured 1 second of sleeping while running.');
  336. sleep(1);
  337. timer_stop('test');
  338. $this->assertTrue(timer_read('test') >= 2000, 'Timer measured 2 seconds of sleeping after being stopped.');
  339. timer_start('test');
  340. sleep(1);
  341. $this->assertTrue(timer_read('test') >= 3000, 'Timer measured 3 seconds of sleeping after being restarted.');
  342. sleep(1);
  343. $timer = timer_stop('test');
  344. $this->assertTrue(timer_read('test') >= 4000, 'Timer measured 4 seconds of sleeping after being stopped for a second time.');
  345. $this->assertEqual($timer['count'], 2, 'Timer counted 2 instances of being started.');
  346. }
  347. }
  348. /**
  349. * Test that resetting static variables works.
  350. */
  351. class BootstrapResettableStaticTestCase extends DrupalUnitTestCase {
  352. public static function getInfo() {
  353. return array(
  354. 'name' => 'Resettable static variables test',
  355. 'description' => 'Test that drupal_static() and drupal_static_reset() work.',
  356. 'group' => 'Bootstrap',
  357. );
  358. }
  359. /**
  360. * Test that a variable reference returned by drupal_static() gets reset when
  361. * drupal_static_reset() is called.
  362. */
  363. function testDrupalStatic() {
  364. $name = __CLASS__ . '_' . __METHOD__;
  365. $var = &drupal_static($name, 'foo');
  366. $this->assertEqual($var, 'foo', 'Variable returned by drupal_static() was set to its default.');
  367. // Call the specific reset and the global reset each twice to ensure that
  368. // multiple resets can be issued without odd side effects.
  369. $var = 'bar';
  370. drupal_static_reset($name);
  371. $this->assertEqual($var, 'foo', 'Variable was reset after first invocation of name-specific reset.');
  372. $var = 'bar';
  373. drupal_static_reset($name);
  374. $this->assertEqual($var, 'foo', 'Variable was reset after second invocation of name-specific reset.');
  375. $var = 'bar';
  376. drupal_static_reset();
  377. $this->assertEqual($var, 'foo', 'Variable was reset after first invocation of global reset.');
  378. $var = 'bar';
  379. drupal_static_reset();
  380. $this->assertEqual($var, 'foo', 'Variable was reset after second invocation of global reset.');
  381. }
  382. }
  383. /**
  384. * Test miscellaneous functions in bootstrap.inc.
  385. */
  386. class BootstrapMiscTestCase extends DrupalUnitTestCase {
  387. public static function getInfo() {
  388. return array(
  389. 'name' => 'Miscellaneous bootstrap unit tests',
  390. 'description' => 'Test miscellaneous functions in bootstrap.inc.',
  391. 'group' => 'Bootstrap',
  392. );
  393. }
  394. /**
  395. * Test miscellaneous functions in bootstrap.inc.
  396. */
  397. function testMisc() {
  398. // Test drupal_array_merge_deep().
  399. $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => 'X', 'class' => array('a', 'b')), 'language' => 'en');
  400. $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('c', 'd')), 'html' => TRUE);
  401. $expected = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('a', 'b', 'c', 'd')), 'language' => 'en', 'html' => TRUE);
  402. $this->assertIdentical(drupal_array_merge_deep($link_options_1, $link_options_2), $expected, 'drupal_array_merge_deep() returned a properly merged array.');
  403. }
  404. /**
  405. * Tests that the drupal_check_memory_limit() function works as expected.
  406. */
  407. function testCheckMemoryLimit() {
  408. $memory_limit = ini_get('memory_limit');
  409. // Test that a very reasonable amount of memory is available.
  410. $this->assertTrue(drupal_check_memory_limit('30MB'), '30MB of memory tested available.');
  411. // Get the available memory and multiply it by two to make it unreasonably
  412. // high.
  413. $twice_avail_memory = ($memory_limit * 2) . 'MB';
  414. // The function should always return true if the memory limit is set to -1.
  415. $this->assertTrue(drupal_check_memory_limit($twice_avail_memory, -1), 'drupal_check_memory_limit() returns TRUE when a limit of -1 (none) is supplied');
  416. // Test that even though we have 30MB of memory available - the function
  417. // returns FALSE when given an upper limit for how much memory can be used.
  418. $this->assertFalse(drupal_check_memory_limit('30MB', '16MB'), 'drupal_check_memory_limit() returns FALSE with a 16MB upper limit on a 30MB requirement.');
  419. // Test that an equal amount of memory to the amount requested returns TRUE.
  420. $this->assertTrue(drupal_check_memory_limit('30MB', '30MB'), 'drupal_check_memory_limit() returns TRUE when requesting 30MB on a 30MB requirement.');
  421. }
  422. }
  423. /**
  424. * Tests for overriding server variables via the API.
  425. */
  426. class BootstrapOverrideServerVariablesTestCase extends DrupalUnitTestCase {
  427. public static function getInfo() {
  428. return array(
  429. 'name' => 'Overriding server variables',
  430. 'description' => 'Test that drupal_override_server_variables() works correctly.',
  431. 'group' => 'Bootstrap',
  432. );
  433. }
  434. /**
  435. * Test providing a direct URL to to drupal_override_server_variables().
  436. */
  437. function testDrupalOverrideServerVariablesProvidedURL() {
  438. $tests = array(
  439. 'http://example.com' => array(
  440. 'HTTP_HOST' => 'example.com',
  441. 'SCRIPT_NAME' => isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL,
  442. ),
  443. 'http://example.com/index.php' => array(
  444. 'HTTP_HOST' => 'example.com',
  445. 'SCRIPT_NAME' => '/index.php',
  446. ),
  447. 'http://example.com/subdirectory/index.php' => array(
  448. 'HTTP_HOST' => 'example.com',
  449. 'SCRIPT_NAME' => '/subdirectory/index.php',
  450. ),
  451. );
  452. foreach ($tests as $url => $expected_server_values) {
  453. // Remember the original value of $_SERVER, since the function call below
  454. // will modify it.
  455. $original_server = $_SERVER;
  456. // Call drupal_override_server_variables() and ensure that all expected
  457. // $_SERVER variables were modified correctly.
  458. drupal_override_server_variables(array('url' => $url));
  459. foreach ($expected_server_values as $key => $value) {
  460. $this->assertIdentical($_SERVER[$key], $value);
  461. }
  462. // Restore the original value of $_SERVER.
  463. $_SERVER = $original_server;
  464. }
  465. }
  466. }