2008-06-26 11:29:20 +00:00
<?php
class BootstrapIPAddressTestCase extends DrupalWebTestCase {
2009-03-31 01:49:55 +00:00
public static function getInfo() {
2008-06-26 11:29:20 +00:00
return array(
2009-07-13 21:51:42 +00:00
'name' => 'IP address and HTTP_HOST test',
'description' => 'Get the IP address from the current visitor from the server variables, check hostname validation.',
'group' => 'Bootstrap'
2008-06-26 11:29:20 +00:00
);
}
function setUp() {
$this->oldserver = $_SERVER;
$this->remote_ip = '127.0.0.1';
$this->proxy_ip = '127.0.0.2';
2010-06-14 13:24:32 +00:00
$this->proxy2_ip = '127.0.0.3';
$this->forwarded_ip = '127.0.0.4';
$this->cluster_ip = '127.0.0.5';
2008-06-26 11:29:20 +00:00
$this->untrusted_ip = '0.0.0.0';
2008-07-02 20:05:11 +00:00
2009-04-02 20:39:45 +00:00
drupal_static_reset('ip_address');
2008-06-26 11:29:20 +00:00
$_SERVER['REMOTE_ADDR'] = $this->remote_ip;
unset($_SERVER['HTTP_X_FORWARDED_FOR']);
unset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']);
2008-07-02 20:05:11 +00:00
2008-06-26 11:29:20 +00:00
parent::setUp();
}
2008-07-02 20:05:11 +00:00
2008-06-26 11:29:20 +00:00
function tearDown() {
$_SERVER = $this->oldserver;
2009-04-02 20:39:45 +00:00
drupal_static_reset('ip_address');
2008-06-26 11:29:20 +00:00
parent::tearDown();
}
/**
2008-11-02 10:56:35 +00:00
* test IP Address and hostname
2008-06-26 11:29:20 +00:00
*/
2008-11-02 10:56:35 +00:00
function testIPAddressHost() {
2008-06-26 11:29:20 +00:00
// Test the normal IP address.
$this->assertTrue(
2010-08-05 23:53:39 +00:00
ip_address() == $this->remote_ip,
2013-07-03 15:00:12 +00:00
'Got remote IP address.'
2010-08-05 23:53:39 +00:00
);
2008-07-02 20:05:11 +00:00
2008-06-26 11:29:20 +00:00
// Proxy forwarding on but no proxy addresses defined.
variable_set('reverse_proxy', 1);
$this->assertTrue(
2010-08-05 23:53:39 +00:00
ip_address() == $this->remote_ip,
2013-07-03 15:00:12 +00:00
'Proxy forwarding without trusted proxies got remote IP address.'
2010-08-05 23:53:39 +00:00
);
2008-07-02 20:05:11 +00:00
2008-06-26 11:29:20 +00:00
// Proxy forwarding on and proxy address not trusted.
2010-06-14 13:24:32 +00:00
variable_set('reverse_proxy_addresses', array($this->proxy_ip, $this->proxy2_ip));
2009-04-02 20:39:45 +00:00
drupal_static_reset('ip_address');
2008-06-26 11:29:20 +00:00
$_SERVER['REMOTE_ADDR'] = $this->untrusted_ip;
$this->assertTrue(
2010-08-05 23:53:39 +00:00
ip_address() == $this->untrusted_ip,
2013-07-03 15:00:12 +00:00
'Proxy forwarding with untrusted proxy got remote IP address.'
2010-08-05 23:53:39 +00:00
);
2008-06-26 11:29:20 +00:00
// Proxy forwarding on and proxy address trusted.
$_SERVER['REMOTE_ADDR'] = $this->proxy_ip;
$_SERVER['HTTP_X_FORWARDED_FOR'] = $this->forwarded_ip;
2009-04-02 20:39:45 +00:00
drupal_static_reset('ip_address');
2008-06-26 11:29:20 +00:00
$this->assertTrue(
2010-08-05 23:53:39 +00:00
ip_address() == $this->forwarded_ip,
2013-07-03 15:00:12 +00:00
'Proxy forwarding with trusted proxy got forwarded IP address.'
2010-08-05 23:53:39 +00:00
);
2010-06-14 13:24:32 +00:00
// Multi-tier architecture with comma separated values in header.
$_SERVER['REMOTE_ADDR'] = $this->proxy_ip;
$_SERVER['HTTP_X_FORWARDED_FOR'] = implode(', ', array($this->untrusted_ip, $this->forwarded_ip, $this->proxy2_ip));
drupal_static_reset('ip_address');
$this->assertTrue(
2010-08-05 23:53:39 +00:00
ip_address() == $this->forwarded_ip,
2013-07-03 15:00:12 +00:00
'Proxy forwarding with trusted 2-tier proxy got forwarded IP address.'
2010-08-05 23:53:39 +00:00
);
2008-07-02 20:05:11 +00:00
2010-03-17 13:58:45 +00:00
// Custom client-IP header.
variable_set('reverse_proxy_header', 'HTTP_X_CLUSTER_CLIENT_IP');
2008-06-26 11:29:20 +00:00
$_SERVER['HTTP_X_CLUSTER_CLIENT_IP'] = $this->cluster_ip;
2009-04-02 20:39:45 +00:00
drupal_static_reset('ip_address');
2008-06-26 11:29:20 +00:00
$this->assertTrue(
2010-08-05 23:53:39 +00:00
ip_address() == $this->cluster_ip,
2013-07-03 15:00:12 +00:00
'Cluster environment got cluster client IP.'
2010-08-05 23:53:39 +00:00
);
2010-06-14 13:24:32 +00:00
// Verifies that drupal_valid_http_host() prevents invalid characters.
2013-07-03 15:00:12 +00:00
$this->assertFalse(drupal_valid_http_host('security/.drupal.org:80'), 'HTTP_HOST with / is invalid');
$this->assertFalse(drupal_valid_http_host('security\\.drupal.org:80'), 'HTTP_HOST with \\ is invalid');
$this->assertFalse(drupal_valid_http_host('security<.drupal.org:80'), 'HTTP_HOST with < is invalid');
$this->assertFalse(drupal_valid_http_host('security..drupal.org:80'), 'HTTP_HOST with .. is invalid');
2014-07-16 20:03:02 +00:00
// Verifies that host names are shorter than 1000 characters.
$this->assertFalse(drupal_valid_http_host(str_repeat('x', 1001)), 'HTTP_HOST with more than 1000 characters is invalid.');
$this->assertFalse(drupal_valid_http_host(str_repeat('.', 101)), 'HTTP_HOST with more than 100 subdomains is invalid.');
$this->assertFalse(drupal_valid_http_host(str_repeat(':', 101)), 'HTTP_HOST with more than 100 portseparators is invalid.');
2009-01-22 03:05:18 +00:00
// IPv6 loopback address
2013-07-03 15:00:12 +00:00
$this->assertTrue(drupal_valid_http_host('[::1]:80'), 'HTTP_HOST containing IPv6 loopback is valid');
2008-06-26 11:29:20 +00:00
}
}
2008-07-17 21:10:39 +00:00
class BootstrapPageCacheTestCase extends DrupalWebTestCase {
2009-03-31 01:49:55 +00:00
public static function getInfo() {
2008-07-17 21:10:39 +00:00
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Page cache test',
2009-11-02 03:12:05 +00:00
'description' => 'Enable the page cache and test it with various HTTP requests.',
2009-07-13 21:51:42 +00:00
'group' => 'Bootstrap'
2008-07-17 21:10:39 +00:00
);
}
2009-04-22 09:45:03 +00:00
function setUp() {
parent::setUp('system_test');
}
2008-07-17 21:10:39 +00:00
/**
2009-04-22 09:45:03 +00:00
* Test support for requests containing If-Modified-Since and If-None-Match headers.
2008-07-17 21:10:39 +00:00
*/
2009-04-22 09:45:03 +00:00
function testConditionalRequests() {
2010-05-12 08:26:15 +00:00
variable_set('cache', 1);
2008-12-03 14:51:53 +00:00
2008-11-23 18:12:08 +00:00
// Fill the cache.
2008-12-03 14:51:53 +00:00
$this->drupalGet('');
$this->drupalHead('');
2013-07-03 15:00:12 +00:00
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
2008-12-03 14:51:53 +00:00
$etag = $this->drupalGetHeader('ETag');
$last_modified = $this->drupalGetHeader('Last-Modified');
$this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
2013-07-03 15:00:12 +00:00
$this->assertResponse(304, 'Conditional request returned 304 Not Modified.');
2008-12-03 14:51:53 +00:00
$this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC822, strtotime($last_modified)), 'If-None-Match: ' . $etag));
2013-07-03 15:00:12 +00:00
$this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
2008-12-03 14:51:53 +00:00
$this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC850, strtotime($last_modified)), 'If-None-Match: ' . $etag));
2013-07-03 15:00:12 +00:00
$this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
2008-12-03 14:51:53 +00:00
$this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified));
2013-07-03 15:00:12 +00:00
$this->assertResponse(200, 'Conditional request without If-None-Match returned 200 OK.');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
2008-12-03 14:51:53 +00:00
2014-11-05 03:44:01 +00:00
$this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC7231, strtotime($last_modified) + 1), 'If-None-Match: ' . $etag));
2013-07-03 15:00:12 +00:00
$this->assertResponse(200, 'Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
2008-11-23 18:12:08 +00:00
2008-12-03 14:51:53 +00:00
$user = $this->drupalCreateUser();
$this->drupalLogin($user);
$this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
2013-07-03 15:00:12 +00:00
$this->assertResponse(200, 'Conditional request returned 200 OK for authenticated user.');
$this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Absense of Page was not cached.');
2015-03-29 21:24:26 +00:00
$this->assertFalse($this->drupalGetHeader('ETag'), 'ETag HTTP headers are not present for logged in users.');
$this->assertFalse($this->drupalGetHeader('Last-Modified'), 'Last-Modified HTTP headers are not present for logged in users.');
2008-07-17 21:10:39 +00:00
}
2009-04-22 09:45:03 +00:00
/**
* Test cache headers.
*/
function testPageCache() {
2010-05-12 08:26:15 +00:00
variable_set('cache', 1);
2009-04-22 09:45:03 +00:00
// Fill the cache.
$this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
2013-07-03 15:00:12 +00:00
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
$this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary header was sent.');
$this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', 'Cache-Control header was sent.');
$this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
$this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
2009-04-22 09:45:03 +00:00
// Check cache.
$this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
2013-07-03 15:00:12 +00:00
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
$this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary: Cookie header was sent.');
$this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', 'Cache-Control header was sent.');
$this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
$this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
2009-04-22 09:45:03 +00:00
// Check replacing default headers.
$this->drupalGet('system-test/set-header', array('query' => array('name' => 'Expires', 'value' => 'Fri, 19 Nov 2008 05:00:00 GMT')));
2013-07-03 15:00:12 +00:00
$this->assertEqual($this->drupalGetHeader('Expires'), 'Fri, 19 Nov 2008 05:00:00 GMT', 'Default header was replaced.');
2009-04-22 09:45:03 +00:00
$this->drupalGet('system-test/set-header', array('query' => array('name' => 'Vary', 'value' => 'User-Agent')));
2013-07-03 15:00:12 +00:00
$this->assertEqual($this->drupalGetHeader('Vary'), 'User-Agent,Accept-Encoding', 'Default header was replaced.');
2009-04-22 09:45:03 +00:00
// Check that authenticated users bypass the cache.
$user = $this->drupalCreateUser();
$this->drupalLogin($user);
$this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
2013-07-03 15:00:12 +00:00
$this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.');
$this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, 'Vary: Cookie header was not sent.');
$this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', 'Cache-Control header was sent.');
$this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
$this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
2009-04-22 09:45:03 +00:00
}
2009-11-02 03:12:05 +00:00
/**
* Test page compression.
2009-11-10 17:27:54 +00:00
*
2009-11-02 03:12:05 +00:00
* The test should pass even if zlib.output_compression is enabled in php.ini,
* .htaccess or similar, or if compression is done outside PHP, e.g. by the
* mod_deflate Apache module.
*/
function testPageCompression() {
2010-05-12 08:26:15 +00:00
variable_set('cache', 1);
2009-11-02 03:12:05 +00:00
// Fill the cache and verify that output is compressed.
$this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
2013-07-03 15:00:12 +00:00
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
2009-11-02 03:12:05 +00:00
$this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
2013-07-03 15:00:12 +00:00
$this->assertRaw('</html>', 'Page was gzip compressed.');
2009-11-02 03:12:05 +00:00
// Verify that cached output is compressed.
$this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
2013-07-03 15:00:12 +00:00
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
$this->assertEqual($this->drupalGetHeader('Content-Encoding'), 'gzip', 'A Content-Encoding header was sent.');
2009-11-02 03:12:05 +00:00
$this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
2013-07-03 15:00:12 +00:00
$this->assertRaw('</html>', 'Page was gzip compressed.');
2009-11-02 03:12:05 +00:00
// Verify that a client without compression support gets an uncompressed page.
$this->drupalGet('');
2013-07-03 15:00:12 +00:00
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
$this->assertFalse($this->drupalGetHeader('Content-Encoding'), 'A Content-Encoding header was not sent.');
$this->assertTitle(t('Welcome to @site-name | @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))), 'Site title matches.');
$this->assertRaw('</html>', 'Page was not compressed.');
2013-12-30 16:38:30 +00:00
// Disable compression mode.
variable_set('page_compression', FALSE);
// Verify if cached page is still available for a client with compression support.
$this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
$this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
$this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support enabled).');
// Verify if cached page is still available for a client without compression support.
$this->drupalGet('');
$this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support disabled).');
2009-11-02 03:12:05 +00:00
}
2008-07-17 21:10:39 +00:00
}
2008-09-08 20:49:47 +00:00
class BootstrapVariableTestCase extends DrupalWebTestCase {
2008-09-15 20:48:10 +00:00
2008-09-08 20:49:47 +00:00
function setUp() {
parent::setUp('system_test');
}
2009-03-31 01:49:55 +00:00
public static function getInfo() {
2008-09-08 20:49:47 +00:00
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Variable test',
'description' => 'Make sure the variable system functions correctly.',
'group' => 'Bootstrap'
2008-09-08 20:49:47 +00:00
);
}
/**
* testVariable
*/
function testVariable() {
// Setting and retrieving values.
$variable = $this->randomName();
variable_set('simpletest_bootstrap_variable_test', $variable);
2013-07-03 15:00:12 +00:00
$this->assertIdentical($variable, variable_get('simpletest_bootstrap_variable_test'), 'Setting and retrieving values');
2008-09-08 20:49:47 +00:00
// Make sure the variable persists across multiple requests.
$this->drupalGet('system-test/variable-get');
2013-07-03 15:00:12 +00:00
$this->assertText($variable, 'Variable persists across multiple requests');
2008-09-15 20:48:10 +00:00
2008-09-08 20:49:47 +00:00
// Deleting variables.
$default_value = $this->randomName();
variable_del('simpletest_bootstrap_variable_test');
$variable = variable_get('simpletest_bootstrap_variable_test', $default_value);
2013-07-03 15:00:12 +00:00
$this->assertIdentical($variable, $default_value, 'Deleting variables');
2008-09-08 20:49:47 +00:00
}
2009-01-31 16:50:57 +00:00
/**
* Makes sure that the default variable parameter is passed through okay.
*/
function testVariableDefaults() {
// Tests passing nothing through to the default.
2013-07-03 15:00:12 +00:00
$this->assertIdentical(NULL, variable_get('simpletest_bootstrap_variable_test'), 'Variables are correctly defaulting to NULL.');
2009-01-31 16:50:57 +00:00
// Tests passing 5 to the default parameter.
2013-07-03 15:00:12 +00:00
$this->assertIdentical(5, variable_get('simpletest_bootstrap_variable_test', 5), 'The default variable parameter is passed through correctly.');
2009-01-31 16:50:57 +00:00
}
2008-09-08 20:49:47 +00:00
}
2008-11-22 13:33:00 +00:00
2015-03-30 04:29:14 +00:00
/**
* Tests the auto-loading behavior of the code registry.
*/
class BootstrapAutoloadTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Code registry',
'description' => 'Test that the code registry functions correctly.',
'group' => 'Bootstrap',
);
}
function setUp() {
parent::setUp('drupal_autoload_test');
}
/**
* Tests that autoloader name matching is not case sensitive.
*/
function testAutoloadCase() {
// Test interface autoloader.
$this->assertTrue(drupal_autoload_interface('drupalautoloadtestinterface'), 'drupal_autoload_interface() recognizes <em>DrupalAutoloadTestInterface</em> in lower case.');
// Test class autoloader.
$this->assertTrue(drupal_autoload_class('drupalautoloadtestclass'), 'drupal_autoload_class() recognizes <em>DrupalAutoloadTestClass</em> in lower case.');
}
}
2008-11-22 13:33:00 +00:00
/**
2009-08-04 04:02:26 +00:00
* Test hook_boot() and hook_exit().
2008-11-22 13:33:00 +00:00
*/
class HookBootExitTestCase extends DrupalWebTestCase {
2009-03-31 01:49:55 +00:00
public static function getInfo() {
2008-11-22 13:33:00 +00:00
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Boot and exit hook invocation',
'description' => 'Test that hook_boot() and hook_exit() are called correctly.',
'group' => 'Bootstrap',
2008-11-22 13:33:00 +00:00
);
}
function setUp() {
parent::setUp('system_test', 'dblog');
}
/**
* Test calling of hook_boot() and hook_exit().
*/
function testHookBootExit() {
2008-11-23 18:12:08 +00:00
// Test with cache disabled. Boot and exit should always fire.
2010-05-12 08:26:15 +00:00
variable_set('cache', 0);
2008-11-22 13:33:00 +00:00
$this->drupalGet('');
2008-11-23 18:12:08 +00:00
$calls = 1;
2010-08-05 23:53:39 +00:00
$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.'));
$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.'));
2008-11-22 13:33:00 +00:00
// Test with normal cache. Boot and exit should be called.
2010-05-12 08:26:15 +00:00
variable_set('cache', 1);
2008-11-22 13:33:00 +00:00
$this->drupalGet('');
$calls++;
2010-08-05 23:53:39 +00:00
$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.'));
$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.'));
2008-11-22 13:33:00 +00:00
2009-08-16 18:39:45 +00:00
// Boot and exit should not fire since the page is cached.
variable_set('page_cache_invoke_hooks', FALSE);
2010-08-05 23:53:39 +00:00
$this->assertTrue(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has been cached.'));
2008-11-22 13:33:00 +00:00
$this->drupalGet('');
2011-10-02 18:09:00 +00:00
$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.'));
$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.'));
2008-11-22 13:33:00 +00:00
2009-08-16 18:39:45 +00:00
// Test with page cache cleared, boot and exit should be called.
2010-08-05 23:53:39 +00:00
$this->assertTrue(db_delete('cache_page')->execute(), t('Page cache cleared.'));
2008-11-22 13:33:00 +00:00
$this->drupalGet('');
$calls++;
2011-10-02 18:09:00 +00:00
$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.'));
$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.'));
2008-11-22 13:33:00 +00:00
}
}
2009-08-04 04:02:26 +00:00
/**
* Test drupal_get_filename()'s availability.
*/
class BootstrapGetFilenameTestCase extends DrupalUnitTestCase {
public static function getInfo() {
return array(
2009-09-20 17:40:42 +00:00
'name' => 'Get filename test',
'description' => 'Test that drupal_get_filename() works correctly when the file is not found in the database.',
'group' => 'Bootstrap',
2009-08-04 04:02:26 +00:00
);
}
/**
* Test that drupal_get_filename() works correctly when the file is not found in the database.
*/
function testDrupalGetFilename() {
// Reset the static cache so we can test the "db is not active" code of
// drupal_get_filename().
drupal_static_reset('drupal_get_filename');
// Retrieving the location of a module.
2010-08-05 23:53:39 +00:00
$this->assertIdentical(drupal_get_filename('module', 'php'), 'modules/php/php.module', t('Retrieve module location.'));
2009-08-04 04:02:26 +00:00
// Retrieving the location of a theme.
2010-08-05 23:53:39 +00:00
$this->assertIdentical(drupal_get_filename('theme', 'stark'), 'themes/stark/stark.info', t('Retrieve theme location.'));
2009-08-04 04:02:26 +00:00
// Retrieving the location of a theme engine.
2010-08-05 23:53:39 +00:00
$this->assertIdentical(drupal_get_filename('theme_engine', 'phptemplate'), 'themes/engines/phptemplate/phptemplate.engine', t('Retrieve theme engine location.'));
2009-08-04 04:02:26 +00:00
2011-11-12 04:41:16 +00:00
// Retrieving the location of a profile. Profiles are a special case with
// a fixed location and naming.
2010-08-05 23:53:39 +00:00
$this->assertIdentical(drupal_get_filename('profile', 'standard'), 'profiles/standard/standard.profile', t('Retrieve install profile location.'));
2011-11-12 04:41:16 +00:00
// When a file is not found in the database cache, drupal_get_filename()
// searches several locations on the filesystem, including the DRUPAL_ROOT
// directory. We use the '.script' extension below because this is a
// non-existent filetype that will definitely not exist in the database.
// Since there is already a scripts directory, drupal_get_filename() will
// automatically check there for 'script' files, just as it does for (e.g.)
// 'module' files in modules.
$this->assertIdentical(drupal_get_filename('script', 'test'), 'scripts/test.script', t('Retrieve test script location.'));
2009-08-04 04:02:26 +00:00
}
}
2009-10-18 11:36:49 +00:00
class BootstrapTimerTestCase extends DrupalUnitTestCase {
public static function getInfo() {
return array(
'name' => 'Timer test',
'description' => 'Test that timer_read() works both when a timer is running and when a timer is stopped.',
'group' => 'Bootstrap',
);
}
/**
* Test timer_read() to ensure it properly accumulates time when the timer
* started and stopped multiple times.
2009-11-10 17:27:54 +00:00
* @return
2009-10-18 11:36:49 +00:00
*/
function testTimer() {
timer_start('test');
sleep(1);
2013-07-03 15:00:12 +00:00
$this->assertTrue(timer_read('test') >= 1000, 'Timer measured 1 second of sleeping while running.');
2009-10-18 11:36:49 +00:00
sleep(1);
timer_stop('test');
2013-07-03 15:00:12 +00:00
$this->assertTrue(timer_read('test') >= 2000, 'Timer measured 2 seconds of sleeping after being stopped.');
2009-10-18 11:36:49 +00:00
timer_start('test');
sleep(1);
2013-07-03 15:00:12 +00:00
$this->assertTrue(timer_read('test') >= 3000, 'Timer measured 3 seconds of sleeping after being restarted.');
2009-10-18 11:36:49 +00:00
sleep(1);
$timer = timer_stop('test');
2013-07-03 15:00:12 +00:00
$this->assertTrue(timer_read('test') >= 4000, 'Timer measured 4 seconds of sleeping after being stopped for a second time.');
$this->assertEqual($timer['count'], 2, 'Timer counted 2 instances of being started.');
2009-10-18 11:36:49 +00:00
}
}
2009-11-15 21:41:06 +00:00
/**
* Test that resetting static variables works.
*/
class BootstrapResettableStaticTestCase extends DrupalUnitTestCase {
public static function getInfo() {
return array(
'name' => 'Resettable static variables test',
'description' => 'Test that drupal_static() and drupal_static_reset() work.',
'group' => 'Bootstrap',
);
}
/**
* Test that a variable reference returned by drupal_static() gets reset when
2010-01-30 07:59:26 +00:00
* drupal_static_reset() is called.
2009-11-15 21:41:06 +00:00
*/
function testDrupalStatic() {
$name = __CLASS__ . '_' . __METHOD__;
$var = &drupal_static($name, 'foo');
2013-07-03 15:00:12 +00:00
$this->assertEqual($var, 'foo', 'Variable returned by drupal_static() was set to its default.');
2009-11-15 21:41:06 +00:00
// Call the specific reset and the global reset each twice to ensure that
// multiple resets can be issued without odd side effects.
$var = 'bar';
drupal_static_reset($name);
2013-07-03 15:00:12 +00:00
$this->assertEqual($var, 'foo', 'Variable was reset after first invocation of name-specific reset.');
2009-11-15 21:41:06 +00:00
$var = 'bar';
drupal_static_reset($name);
2013-07-03 15:00:12 +00:00
$this->assertEqual($var, 'foo', 'Variable was reset after second invocation of name-specific reset.');
2009-11-15 21:41:06 +00:00
$var = 'bar';
drupal_static_reset();
2013-07-03 15:00:12 +00:00
$this->assertEqual($var, 'foo', 'Variable was reset after first invocation of global reset.');
2009-11-15 21:41:06 +00:00
$var = 'bar';
drupal_static_reset();
2013-07-03 15:00:12 +00:00
$this->assertEqual($var, 'foo', 'Variable was reset after second invocation of global reset.');
2009-11-15 21:41:06 +00:00
}
}
2010-11-20 05:02:46 +00:00
/**
* Test miscellaneous functions in bootstrap.inc.
*/
class BootstrapMiscTestCase extends DrupalUnitTestCase {
public static function getInfo() {
return array(
'name' => 'Miscellaneous bootstrap unit tests',
'description' => 'Test miscellaneous functions in bootstrap.inc.',
'group' => 'Bootstrap',
);
}
/**
* Test miscellaneous functions in bootstrap.inc.
*/
function testMisc() {
// Test drupal_array_merge_deep().
$link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => 'X', 'class' => array('a', 'b')), 'language' => 'en');
$link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('c', 'd')), 'html' => TRUE);
$expected = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('a', 'b', 'c', 'd')), 'language' => 'en', 'html' => TRUE);
2013-07-03 15:00:12 +00:00
$this->assertIdentical(drupal_array_merge_deep($link_options_1, $link_options_2), $expected, 'drupal_array_merge_deep() returned a properly merged array.');
2010-11-20 05:02:46 +00:00
}
2013-08-06 03:58:15 +00:00
/**
* Tests that the drupal_check_memory_limit() function works as expected.
*/
function testCheckMemoryLimit() {
$memory_limit = ini_get('memory_limit');
// Test that a very reasonable amount of memory is available.
$this->assertTrue(drupal_check_memory_limit('30MB'), '30MB of memory tested available.');
// Get the available memory and multiply it by two to make it unreasonably
// high.
$twice_avail_memory = ($memory_limit * 2) . 'MB';
// The function should always return true if the memory limit is set to -1.
$this->assertTrue(drupal_check_memory_limit($twice_avail_memory, -1), 'drupal_check_memory_limit() returns TRUE when a limit of -1 (none) is supplied');
// Test that even though we have 30MB of memory available - the function
// returns FALSE when given an upper limit for how much memory can be used.
$this->assertFalse(drupal_check_memory_limit('30MB', '16MB'), 'drupal_check_memory_limit() returns FALSE with a 16MB upper limit on a 30MB requirement.');
// Test that an equal amount of memory to the amount requested returns TRUE.
$this->assertTrue(drupal_check_memory_limit('30MB', '30MB'), 'drupal_check_memory_limit() returns TRUE when requesting 30MB on a 30MB requirement.');
}
2010-11-20 05:02:46 +00:00
}
2010-11-23 03:08:34 +00:00
/**
* Tests for overriding server variables via the API.
*/
class BootstrapOverrideServerVariablesTestCase extends DrupalUnitTestCase {
public static function getInfo() {
return array(
'name' => 'Overriding server variables',
'description' => 'Test that drupal_override_server_variables() works correctly.',
'group' => 'Bootstrap',
);
}
/**
* Test providing a direct URL to to drupal_override_server_variables().
*/
function testDrupalOverrideServerVariablesProvidedURL() {
$tests = array(
'http://example.com' => array(
'HTTP_HOST' => 'example.com',
'SCRIPT_NAME' => isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL,
),
'http://example.com/index.php' => array(
'HTTP_HOST' => 'example.com',
'SCRIPT_NAME' => '/index.php',
),
'http://example.com/subdirectory/index.php' => array(
'HTTP_HOST' => 'example.com',
'SCRIPT_NAME' => '/subdirectory/index.php',
),
);
foreach ($tests as $url => $expected_server_values) {
// Remember the original value of $_SERVER, since the function call below
// will modify it.
$original_server = $_SERVER;
// Call drupal_override_server_variables() and ensure that all expected
// $_SERVER variables were modified correctly.
drupal_override_server_variables(array('url' => $url));
foreach ($expected_server_values as $key => $value) {
$this->assertIdentical($_SERVER[$key], $value);
}
// Restore the original value of $_SERVER.
$_SERVER = $original_server;
}
}
}
2015-03-18 19:20:37 +00:00
/**
* Tests for $_GET['destination'] and $_REQUEST['destination'] validation.
*/
class BootstrapDestinationTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'URL destination validation',
'description' => 'Test that $_GET[\'destination\'] and $_REQUEST[\'destination\'] cannot contain external URLs.',
'group' => 'Bootstrap',
);
}
function setUp() {
parent::setUp('system_test');
}
/**
* Tests that $_GET/$_REQUEST['destination'] only contain internal URLs.
*
* @see _drupal_bootstrap_variables()
* @see system_test_get_destination()
* @see system_test_request_destination()
*/
public function testDestination() {
$test_cases = array(
array(
'input' => 'node',
'output' => 'node',
'message' => "Standard internal example node path is present in the 'destination' parameter.",
),
array(
'input' => '/example.com',
'output' => '/example.com',
'message' => 'Internal path with one leading slash is allowed.',
),
array(
'input' => '//example.com/test',
'output' => '',
'message' => 'External URL without scheme is not allowed.',
),
array(
'input' => 'example:test',
'output' => 'example:test',
'message' => 'Internal URL using a colon is allowed.',
),
array(
'input' => 'http://example.com',
'output' => '',
'message' => 'External URL is not allowed.',
),
array(
'input' => 'javascript:alert(0)',
'output' => 'javascript:alert(0)',
'message' => 'Javascript URL is allowed because it is treated as an internal URL.',
),
);
foreach ($test_cases as $test_case) {
// Test $_GET['destination'].
$this->drupalGet('system-test/get-destination', array('query' => array('destination' => $test_case['input'])));
$this->assertIdentical($test_case['output'], $this->drupalGetContent(), $test_case['message']);
// Test $_REQUEST['destination']. There's no form to submit to, so
// drupalPost() won't work here; this just tests a direct $_POST request
// instead.
$curl_parameters = array(
CURLOPT_URL => $this->getAbsoluteUrl('system-test/request-destination'),
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => 'destination=' . urlencode($test_case['input']),
CURLOPT_HTTPHEADER => array(),
);
$post_output = $this->curlExec($curl_parameters);
$this->assertIdentical($test_case['output'], $post_output, $test_case['message']);
}
// Make sure that 404 pages do not populate $_GET['destination'] with
// external URLs.
variable_set('site_404', 'system-test/get-destination');
$this->drupalGet('http://example.com', array('external' => FALSE));
$this->assertIdentical('', $this->drupalGetContent(), 'External URL is not allowed on 404 pages.');
}
}