2008-06-26 11:29:20 +00:00
<?php
2008-07-19 12:31:14 +00:00
// $Id$
2008-06-26 11:29:20 +00:00
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';
$this->forwarded_ip = '127.0.0.3';
$this->cluster_ip = '127.0.0.4';
$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(
2009-04-02 20:39:45 +00:00
ip_address() == $this->remote_ip,
2008-06-26 11:29:20 +00:00
t('Got remote IP address')
);
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(
2009-04-02 20:39:45 +00:00
ip_address() == $this->remote_ip,
2008-06-26 11:29:20 +00:00
t('Proxy forwarding without trusted proxies got remote IP address')
);
2008-07-02 20:05:11 +00:00
2008-06-26 11:29:20 +00:00
// Proxy forwarding on and proxy address not trusted.
variable_set('reverse_proxy_addresses', array($this->proxy_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(
2009-04-02 20:39:45 +00:00
ip_address() == $this->untrusted_ip,
2008-06-26 11:29:20 +00:00
t('Proxy forwarding with untrusted proxy got remote IP address')
);
// 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(
2009-04-02 20:39:45 +00:00
ip_address() == $this->forwarded_ip,
2008-06-26 11:29:20 +00:00
t('Proxy forwarding with trusted proxy got forwarded IP address')
2008-07-02 20:05:11 +00:00
);
2008-06-26 11:29:20 +00:00
// Cluster environment.
$_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(
2009-04-02 20:39:45 +00:00
ip_address() == $this->cluster_ip,
2008-06-26 11:29:20 +00:00
t('Cluster environment got cluster client IP')
);
2009-01-22 03:05:18 +00:00
$this->assertFalse(drupal_valid_http_host('security/.drupal.org:80'), t('HTTP_HOST with / is invalid'));
$this->assertFalse(drupal_valid_http_host('security\\.drupal.org:80'), t('HTTP_HOST with \\ is invalid'));
$this->assertFalse(drupal_valid_http_host('security<.drupal.org:80'), t('HTTP_HOST with < is invalid'));
$this->assertFalse(drupal_valid_http_host('security..drupal.org:80'), t('HTTP_HOST with .. is invalid'));
// IPv6 loopback address
$this->assertTrue(drupal_valid_http_host('[::1]:80'), t('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',
'description' => 'Enable the page cache and test it with conditional HTTP requests.',
'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() {
2008-12-03 14:51:53 +00:00
variable_set('cache', CACHE_NORMAL);
2008-11-23 18:12:08 +00:00
// Fill the cache.
2008-12-03 14:51:53 +00:00
$this->drupalGet('');
$this->drupalHead('');
2009-04-22 09:45:03 +00:00
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('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));
$this->assertResponse(304, t('Conditional request returned 304 Not Modified.'));
$this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC822, strtotime($last_modified)), 'If-None-Match: ' . $etag));
$this->assertResponse(304, t('Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.'));
$this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC850, strtotime($last_modified)), 'If-None-Match: ' . $etag));
$this->assertResponse(304, t('Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.'));
$this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified));
$this->assertResponse(200, t('Conditional request without If-None-Match returned 200 OK.'));
2009-04-22 09:45:03 +00:00
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
2008-12-03 14:51:53 +00:00
$this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC1123, strtotime($last_modified) + 1), 'If-None-Match: ' . $etag));
$this->assertResponse(200, t('Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.'));
2009-04-22 09:45:03 +00:00
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('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));
$this->assertResponse(200, t('Conditional request returned 200 OK for authenticated user.'));
2009-04-22 09:45:03 +00:00
$this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), t('Absense of Page was not cached.'));
2008-07-17 21:10:39 +00:00
}
2009-04-22 09:45:03 +00:00
/**
* Test cache headers.
*/
function testPageCache() {
variable_set('cache', CACHE_NORMAL);
// Fill the cache.
$this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', t('Page was not cached.'));
$this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', t('Vary header was sent.'));
$this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', t('Cache-Control header was sent.'));
$this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
$this->assertEqual($this->drupalGetHeader('Foo'), 'bar', t('Custom header was sent.'));
// Check cache.
$this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
$this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', t('Vary: Cookie header was sent.'));
$this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', t('Cache-Control header was sent.'));
$this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
$this->assertEqual($this->drupalGetHeader('Foo'), 'bar', t('Custom header was sent.'));
// Check replacing default headers.
$this->drupalGet('system-test/set-header', array('query' => array('name' => 'Expires', 'value' => 'Fri, 19 Nov 2008 05:00:00 GMT')));
$this->assertEqual($this->drupalGetHeader('Expires'), 'Fri, 19 Nov 2008 05:00:00 GMT', t('Default header was replaced.'));
$this->drupalGet('system-test/set-header', array('query' => array('name' => 'Vary', 'value' => 'User-Agent')));
$this->assertEqual($this->drupalGetHeader('Vary'), 'User-Agent,Accept-Encoding', t('Default header was replaced.'));
// 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')));
$this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), t('Caching was bypassed.'));
2009-05-18 14:28:34 +00:00
$this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, t('Vary: Cookie header was not sent.'));
2009-04-22 09:45:03 +00:00
$this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', t('Cache-Control header was sent.'));
$this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
$this->assertEqual($this->drupalGetHeader('Foo'), 'bar', t('Custom header was sent.'));
}
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);
2009-01-31 16:50:57 +00:00
$this->assertIdentical($variable, variable_get('simpletest_bootstrap_variable_test'), t('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');
$this->assertText($variable, t('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);
$this->assertIdentical($variable, $default_value, t('Deleting variables'));
}
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.
$this->assertIdentical(NULL, variable_get('simpletest_bootstrap_variable_test'), t('Variables are correctly defaulting to NULL.'));
// Tests passing 5 to the default parameter.
$this->assertIdentical(5, variable_get('simpletest_bootstrap_variable_test', 5), t('The default variable parameter is passed through correctly.'));
}
2008-09-08 20:49:47 +00:00
}
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.
2008-11-22 13:33:00 +00:00
variable_set('cache', CACHE_DISABLED);
$this->drupalGet('');
2008-11-23 18:12:08 +00:00
$calls = 1;
2009-05-30 11:17:32 +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.
variable_set('cache', CACHE_NORMAL);
$this->drupalGet('');
$calls++;
2009-05-30 11:17:32 +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
2008-11-22 15:29:50 +00:00
// Test with aggressive cache. Boot and exit should not fire since the
// page is cached.
2008-11-22 13:33:00 +00:00
variable_set('cache', CACHE_AGGRESSIVE);
$this->assertTrue(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has been cached.'));
$this->drupalGet('');
2009-05-30 11:17:32 +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 agressive 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 agressive cache and a cached page.'));
2008-11-22 13:33:00 +00:00
// Test with aggressive cache and page cache cleared. Boot and exit should
// be called.
$this->assertTrue(db_delete('cache_page')->execute(), t('Page cache cleared.'));
$this->drupalGet('');
$calls++;
2009-05-30 11:17:32 +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 agressive 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 agressive 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(
'name' => t('Get Filename Test'),
'description' => t('Test that drupal_get_filename() works correctly when the file is not found in the database.'),
'group' => t('Bootstrap'),
);
}
/**
* 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.
$this->assertIdentical(drupal_get_filename('module', 'php'), 'modules/php/php.module', t('Retrieve module location.'));
// Retrieving the location of a theme.
$this->assertIdentical(drupal_get_filename('theme', 'stark'), 'themes/stark/stark.info', t('Retrieve theme location.'));
// Retrieving the location of a theme engine.
$this->assertIdentical(drupal_get_filename('theme_engine', 'phptemplate'), 'themes/engines/phptemplate/phptemplate.engine', t('Retrieve theme engine location.'));
// Retrieving a file that is definitely not stored in the database.
$this->assertIdentical(drupal_get_filename('profile', 'default'), 'profiles/default/default.profile', t('Retrieve install profile location.'));
}
}