View source
<?php
class BootstrapIPAddressTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'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',
);
}
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';
drupal_static_reset('ip_address');
$_SERVER['REMOTE_ADDR'] = $this->remote_ip;
unset($_SERVER['HTTP_X_FORWARDED_FOR']);
unset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']);
parent::setUp();
}
function tearDown() {
$_SERVER = $this->oldserver;
drupal_static_reset('ip_address');
parent::tearDown();
}
function testIPAddressHost() {
$this
->assertTrue(ip_address() == $this->remote_ip, t('Got remote IP address'));
variable_set('reverse_proxy', 1);
$this
->assertTrue(ip_address() == $this->remote_ip, t('Proxy forwarding without trusted proxies got remote IP address'));
variable_set('reverse_proxy_addresses', array(
$this->proxy_ip,
));
drupal_static_reset('ip_address');
$_SERVER['REMOTE_ADDR'] = $this->untrusted_ip;
$this
->assertTrue(ip_address() == $this->untrusted_ip, t('Proxy forwarding with untrusted proxy got remote IP address'));
$_SERVER['REMOTE_ADDR'] = $this->proxy_ip;
$_SERVER['HTTP_X_FORWARDED_FOR'] = $this->forwarded_ip;
drupal_static_reset('ip_address');
$this
->assertTrue(ip_address() == $this->forwarded_ip, t('Proxy forwarding with trusted proxy got forwarded IP address'));
$_SERVER['HTTP_X_CLUSTER_CLIENT_IP'] = $this->cluster_ip;
drupal_static_reset('ip_address');
$this
->assertTrue(ip_address() == $this->cluster_ip, t('Cluster environment got cluster client IP'));
$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'));
$this
->assertTrue(drupal_valid_http_host('[::1]:80'), t('HTTP_HOST containing IPv6 loopback is valid'));
}
}
class BootstrapPageCacheTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Page cache test',
'description' => 'Enable the page cache and test it with conditional HTTP requests.',
'group' => 'Bootstrap',
);
}
function setUp() {
parent::setUp('system_test');
}
function testConditionalRequests() {
variable_set('cache', CACHE_NORMAL);
$this
->drupalGet('');
$this
->drupalHead('');
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
$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.'));
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
$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.'));
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Page was cached.'));
$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.'));
$this
->assertFalse($this
->drupalGetHeader('X-Drupal-Cache'), t('Absense of Page was not cached.'));
}
function testPageCache() {
variable_set('cache', CACHE_NORMAL);
$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.'));
$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.'));
$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.'));
$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.'));
$this
->assertTrue(strpos($this
->drupalGetHeader('Vary'), 'Cookie') === FALSE, t('Vary: Cookie header was not sent.'));
$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.'));
}
}
class BootstrapVariableTestCase extends DrupalWebTestCase {
function setUp() {
parent::setUp('system_test');
}
public static function getInfo() {
return array(
'name' => 'Variable test',
'description' => 'Make sure the variable system functions correctly.',
'group' => 'Bootstrap',
);
}
function testVariable() {
$variable = $this
->randomName();
variable_set('simpletest_bootstrap_variable_test', $variable);
$this
->assertIdentical($variable, variable_get('simpletest_bootstrap_variable_test'), t('Setting and retrieving values'));
$this
->drupalGet('system-test/variable-get');
$this
->assertText($variable, t('Variable persists across multiple requests'));
$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'));
}
function testVariableDefaults() {
$this
->assertIdentical(NULL, variable_get('simpletest_bootstrap_variable_test'), t('Variables are correctly defaulting to NULL.'));
$this
->assertIdentical(5, variable_get('simpletest_bootstrap_variable_test', 5), t('The default variable parameter is passed through correctly.'));
}
}
class HookBootExitTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Boot and exit hook invocation',
'description' => 'Test that hook_boot() and hook_exit() are called correctly.',
'group' => 'Bootstrap',
);
}
function setUp() {
parent::setUp('system_test', 'dblog');
}
function testHookBootExit() {
variable_set('cache', CACHE_DISABLED);
$this
->drupalGet('');
$calls = 1;
$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.'));
variable_set('cache', CACHE_NORMAL);
$this
->drupalGet('');
$calls++;
$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.'));
variable_set('page_cache_invoke_hooks', FALSE);
$this
->assertTrue(cache_get(url('', array(
'absolute' => TRUE,
)), 'cache_page'), t('Page has been cached.'));
$this
->drupalGet('');
$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.'));
$this
->assertTrue(db_delete('cache_page')
->execute(), t('Page cache cleared.'));
$this
->drupalGet('');
$calls++;
$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.'));
}
}
class BootstrapGetFilenameTestCase extends DrupalUnitTestCase {
public static function getInfo() {
return array(
'name' => 'Get filename test',
'description' => 'Test that drupal_get_filename() works correctly when the file is not found in the database.',
'group' => 'Bootstrap',
);
}
function testDrupalGetFilename() {
drupal_static_reset('drupal_get_filename');
$this
->assertIdentical(drupal_get_filename('module', 'php'), 'modules/php/php.module', t('Retrieve module location.'));
$this
->assertIdentical(drupal_get_filename('theme', 'stark'), 'themes/stark/stark.info', t('Retrieve theme location.'));
$this
->assertIdentical(drupal_get_filename('theme_engine', 'phptemplate'), 'themes/engines/phptemplate/phptemplate.engine', t('Retrieve theme engine location.'));
$this
->assertIdentical(drupal_get_filename('profile', 'default'), 'profiles/default/default.profile', t('Retrieve install profile location.'));
}
}
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',
);
}
function testTimer() {
timer_start('test');
sleep(1);
$this
->assertTrue(timer_read('test') >= 1000, t('Timer measured 1 second of sleeping while running.'));
sleep(1);
timer_stop('test');
$this
->assertTrue(timer_read('test') >= 2000, t('Timer measured 2 seconds of sleeping after being stopped.'));
timer_start('test');
sleep(1);
$this
->assertTrue(timer_read('test') >= 3000, t('Timer measured 3 seconds of sleeping after being restarted.'));
sleep(1);
$timer = timer_stop('test');
$this
->assertTrue(timer_read('test') >= 4000, t('Timer measured 4 seconds of sleeping after being stopped for a second time.'));
$this
->assertEqual($timer['count'], 2, t('Timer counted 2 instances of being started.'));
}
}