bootstrap.test

File

drupal-7.x/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. '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. // Disable compression mode.
  186. variable_set('page_compression', FALSE);
  187. // Verify if cached page is still available for a client with compression support.
  188. $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
  189. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  190. $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support enabled).');
  191. // Verify if cached page is still available for a client without compression support.
  192. $this->drupalGet('');
  193. $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support disabled).');
  194. }
  195. }
  196. class BootstrapVariableTestCase extends DrupalWebTestCase {
  197. function setUp() {
  198. parent::setUp('system_test');
  199. }
  200. public static function getInfo() {
  201. return array(
  202. 'name' => 'Variable test',
  203. 'description' => 'Make sure the variable system functions correctly.',
  204. 'group' => 'Bootstrap'
  205. );
  206. }
  207. /**
  208. * testVariable
  209. */
  210. function testVariable() {
  211. // Setting and retrieving values.
  212. $variable = $this->randomName();
  213. variable_set('simpletest_bootstrap_variable_test', $variable);
  214. $this->assertIdentical($variable, variable_get('simpletest_bootstrap_variable_test'), 'Setting and retrieving values');
  215. // Make sure the variable persists across multiple requests.
  216. $this->drupalGet('system-test/variable-get');
  217. $this->assertText($variable, 'Variable persists across multiple requests');
  218. // Deleting variables.
  219. $default_value = $this->randomName();
  220. variable_del('simpletest_bootstrap_variable_test');
  221. $variable = variable_get('simpletest_bootstrap_variable_test', $default_value);
  222. $this->assertIdentical($variable, $default_value, 'Deleting variables');
  223. }
  224. /**
  225. * Makes sure that the default variable parameter is passed through okay.
  226. */
  227. function testVariableDefaults() {
  228. // Tests passing nothing through to the default.
  229. $this->assertIdentical(NULL, variable_get('simpletest_bootstrap_variable_test'), 'Variables are correctly defaulting to NULL.');
  230. // Tests passing 5 to the default parameter.
  231. $this->assertIdentical(5, variable_get('simpletest_bootstrap_variable_test', 5), 'The default variable parameter is passed through correctly.');
  232. }
  233. }
  234. /**
  235. * Test hook_boot() and hook_exit().
  236. */
  237. class HookBootExitTestCase extends DrupalWebTestCase {
  238. public static function getInfo() {
  239. return array(
  240. 'name' => 'Boot and exit hook invocation',
  241. 'description' => 'Test that hook_boot() and hook_exit() are called correctly.',
  242. 'group' => 'Bootstrap',
  243. );
  244. }
  245. function setUp() {
  246. parent::setUp('system_test', 'dblog');
  247. }
  248. /**
  249. * Test calling of hook_boot() and hook_exit().
  250. */
  251. function testHookBootExit() {
  252. // Test with cache disabled. Boot and exit should always fire.
  253. variable_set('cache', 0);
  254. $this->drupalGet('');
  255. $calls = 1;
  256. $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.'));
  257. $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.'));
  258. // Test with normal cache. Boot and exit should be called.
  259. variable_set('cache', 1);
  260. $this->drupalGet('');
  261. $calls++;
  262. $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.'));
  263. $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.'));
  264. // Boot and exit should not fire since the page is cached.
  265. variable_set('page_cache_invoke_hooks', FALSE);
  266. $this->assertTrue(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has been cached.'));
  267. $this->drupalGet('');
  268. $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.'));
  269. $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.'));
  270. // Test with page cache cleared, boot and exit should be called.
  271. $this->assertTrue(db_delete('cache_page')->execute(), t('Page cache cleared.'));
  272. $this->drupalGet('');
  273. $calls++;
  274. $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.'));
  275. $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.'));
  276. }
  277. }
  278. /**
  279. * Test drupal_get_filename()'s availability.
  280. */
  281. class BootstrapGetFilenameTestCase extends DrupalUnitTestCase {
  282. public static function getInfo() {
  283. return array(
  284. 'name' => 'Get filename test',
  285. 'description' => 'Test that drupal_get_filename() works correctly when the file is not found in the database.',
  286. 'group' => 'Bootstrap',
  287. );
  288. }
  289. /**
  290. * Test that drupal_get_filename() works correctly when the file is not found in the database.
  291. */
  292. function testDrupalGetFilename() {
  293. // Reset the static cache so we can test the "db is not active" code of
  294. // drupal_get_filename().
  295. drupal_static_reset('drupal_get_filename');
  296. // Retrieving the location of a module.
  297. $this->assertIdentical(drupal_get_filename('module', 'php'), 'modules/php/php.module', t('Retrieve module location.'));
  298. // Retrieving the location of a theme.
  299. $this->assertIdentical(drupal_get_filename('theme', 'stark'), 'themes/stark/stark.info', t('Retrieve theme location.'));
  300. // Retrieving the location of a theme engine.
  301. $this->assertIdentical(drupal_get_filename('theme_engine', 'phptemplate'), 'themes/engines/phptemplate/phptemplate.engine', t('Retrieve theme engine location.'));
  302. // Retrieving the location of a profile. Profiles are a special case with
  303. // a fixed location and naming.
  304. $this->assertIdentical(drupal_get_filename('profile', 'standard'), 'profiles/standard/standard.profile', t('Retrieve install profile location.'));
  305. // When a file is not found in the database cache, drupal_get_filename()
  306. // searches several locations on the filesystem, including the DRUPAL_ROOT
  307. // directory. We use the '.script' extension below because this is a
  308. // non-existent filetype that will definitely not exist in the database.
  309. // Since there is already a scripts directory, drupal_get_filename() will
  310. // automatically check there for 'script' files, just as it does for (e.g.)
  311. // 'module' files in modules.
  312. $this->assertIdentical(drupal_get_filename('script', 'test'), 'scripts/test.script', t('Retrieve test script location.'));
  313. }
  314. }
  315. class BootstrapTimerTestCase extends DrupalUnitTestCase {
  316. public static function getInfo() {
  317. return array(
  318. 'name' => 'Timer test',
  319. 'description' => 'Test that timer_read() works both when a timer is running and when a timer is stopped.',
  320. 'group' => 'Bootstrap',
  321. );
  322. }
  323. /**
  324. * Test timer_read() to ensure it properly accumulates time when the timer
  325. * started and stopped multiple times.
  326. * @return
  327. */
  328. function testTimer() {
  329. timer_start('test');
  330. sleep(1);
  331. $this->assertTrue(timer_read('test') >= 1000, 'Timer measured 1 second of sleeping while running.');
  332. sleep(1);
  333. timer_stop('test');
  334. $this->assertTrue(timer_read('test') >= 2000, 'Timer measured 2 seconds of sleeping after being stopped.');
  335. timer_start('test');
  336. sleep(1);
  337. $this->assertTrue(timer_read('test') >= 3000, 'Timer measured 3 seconds of sleeping after being restarted.');
  338. sleep(1);
  339. $timer = timer_stop('test');
  340. $this->assertTrue(timer_read('test') >= 4000, 'Timer measured 4 seconds of sleeping after being stopped for a second time.');
  341. $this->assertEqual($timer['count'], 2, 'Timer counted 2 instances of being started.');
  342. }
  343. }
  344. /**
  345. * Test that resetting static variables works.
  346. */
  347. class BootstrapResettableStaticTestCase extends DrupalUnitTestCase {
  348. public static function getInfo() {
  349. return array(
  350. 'name' => 'Resettable static variables test',
  351. 'description' => 'Test that drupal_static() and drupal_static_reset() work.',
  352. 'group' => 'Bootstrap',
  353. );
  354. }
  355. /**
  356. * Test that a variable reference returned by drupal_static() gets reset when
  357. * drupal_static_reset() is called.
  358. */
  359. function testDrupalStatic() {
  360. $name = __CLASS__ . '_' . __METHOD__;
  361. $var = &drupal_static($name, 'foo');
  362. $this->assertEqual($var, 'foo', 'Variable returned by drupal_static() was set to its default.');
  363. // Call the specific reset and the global reset each twice to ensure that
  364. // multiple resets can be issued without odd side effects.
  365. $var = 'bar';
  366. drupal_static_reset($name);
  367. $this->assertEqual($var, 'foo', 'Variable was reset after first invocation of name-specific reset.');
  368. $var = 'bar';
  369. drupal_static_reset($name);
  370. $this->assertEqual($var, 'foo', 'Variable was reset after second invocation of name-specific reset.');
  371. $var = 'bar';
  372. drupal_static_reset();
  373. $this->assertEqual($var, 'foo', 'Variable was reset after first invocation of global reset.');
  374. $var = 'bar';
  375. drupal_static_reset();
  376. $this->assertEqual($var, 'foo', 'Variable was reset after second invocation of global reset.');
  377. }
  378. }
  379. /**
  380. * Test miscellaneous functions in bootstrap.inc.
  381. */
  382. class BootstrapMiscTestCase extends DrupalUnitTestCase {
  383. public static function getInfo() {
  384. return array(
  385. 'name' => 'Miscellaneous bootstrap unit tests',
  386. 'description' => 'Test miscellaneous functions in bootstrap.inc.',
  387. 'group' => 'Bootstrap',
  388. );
  389. }
  390. /**
  391. * Test miscellaneous functions in bootstrap.inc.
  392. */
  393. function testMisc() {
  394. // Test drupal_array_merge_deep().
  395. $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => 'X', 'class' => array('a', 'b')), 'language' => 'en');
  396. $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('c', 'd')), 'html' => TRUE);
  397. $expected = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('a', 'b', 'c', 'd')), 'language' => 'en', 'html' => TRUE);
  398. $this->assertIdentical(drupal_array_merge_deep($link_options_1, $link_options_2), $expected, 'drupal_array_merge_deep() returned a properly merged array.');
  399. }
  400. /**
  401. * Tests that the drupal_check_memory_limit() function works as expected.
  402. */
  403. function testCheckMemoryLimit() {
  404. $memory_limit = ini_get('memory_limit');
  405. // Test that a very reasonable amount of memory is available.
  406. $this->assertTrue(drupal_check_memory_limit('30MB'), '30MB of memory tested available.');
  407. // Get the available memory and multiply it by two to make it unreasonably
  408. // high.
  409. $twice_avail_memory = ($memory_limit * 2) . 'MB';
  410. // The function should always return true if the memory limit is set to -1.
  411. $this->assertTrue(drupal_check_memory_limit($twice_avail_memory, -1), 'drupal_check_memory_limit() returns TRUE when a limit of -1 (none) is supplied');
  412. // Test that even though we have 30MB of memory available - the function
  413. // returns FALSE when given an upper limit for how much memory can be used.
  414. $this->assertFalse(drupal_check_memory_limit('30MB', '16MB'), 'drupal_check_memory_limit() returns FALSE with a 16MB upper limit on a 30MB requirement.');
  415. // Test that an equal amount of memory to the amount requested returns TRUE.
  416. $this->assertTrue(drupal_check_memory_limit('30MB', '30MB'), 'drupal_check_memory_limit() returns TRUE when requesting 30MB on a 30MB requirement.');
  417. }
  418. }
  419. /**
  420. * Tests for overriding server variables via the API.
  421. */
  422. class BootstrapOverrideServerVariablesTestCase extends DrupalUnitTestCase {
  423. public static function getInfo() {
  424. return array(
  425. 'name' => 'Overriding server variables',
  426. 'description' => 'Test that drupal_override_server_variables() works correctly.',
  427. 'group' => 'Bootstrap',
  428. );
  429. }
  430. /**
  431. * Test providing a direct URL to to drupal_override_server_variables().
  432. */
  433. function testDrupalOverrideServerVariablesProvidedURL() {
  434. $tests = array(
  435. 'http://example.com' => array(
  436. 'HTTP_HOST' => 'example.com',
  437. 'SCRIPT_NAME' => isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL,
  438. ),
  439. 'http://example.com/index.php' => array(
  440. 'HTTP_HOST' => 'example.com',
  441. 'SCRIPT_NAME' => '/index.php',
  442. ),
  443. 'http://example.com/subdirectory/index.php' => array(
  444. 'HTTP_HOST' => 'example.com',
  445. 'SCRIPT_NAME' => '/subdirectory/index.php',
  446. ),
  447. );
  448. foreach ($tests as $url => $expected_server_values) {
  449. // Remember the original value of $_SERVER, since the function call below
  450. // will modify it.
  451. $original_server = $_SERVER;
  452. // Call drupal_override_server_variables() and ensure that all expected
  453. // $_SERVER variables were modified correctly.
  454. drupal_override_server_variables(array('url' => $url));
  455. foreach ($expected_server_values as $key => $value) {
  456. $this->assertIdentical($_SERVER[$key], $value);
  457. }
  458. // Restore the original value of $_SERVER.
  459. $_SERVER = $original_server;
  460. }
  461. }
  462. }