Newer
Older
<?php
// $Id$

Dries Buytaert
committed
/**
* Helper class for module test cases.
*/
class ModuleTestCase extends DrupalWebTestCase {
protected $admin_user;

Dries Buytaert
committed
function setUp() {
parent::setUp('system_test');

Dries Buytaert
committed
$this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer modules'));

Dries Buytaert
committed
$this->drupalLogin($this->admin_user);
}
/**

Dries Buytaert
committed
* Assert there are tables that begin with the specified base table name.
*
* @param $base_table
* Beginning of table name to look for.
* @param $count
* (optional) Whether or not to assert that there are tables that match the
* specified base table. Defaults to TRUE.
*/

Dries Buytaert
committed
function assertTableCount($base_table, $count = TRUE) {
$tables = db_find_tables(Database::getConnection()->prefixTables('{' . $base_table . '}') . '%');

Dries Buytaert
committed
if ($count) {
return $this->assertTrue($tables, t('Tables matching "@base_table" found.', array('@base_table' => $base_table)));
}
return $this->assertFalse($tables, t('Tables matching "@base_table" not found.', array('@base_table' => $base_table)));
}
/**

Dries Buytaert
committed
* Assert the list of modules are enabled or disabled.
*
* @param $modules
* Module list to check.
* @param $enabled
* Expected module state.
*/

Dries Buytaert
committed
function assertModules(array $modules, $enabled) {
module_list(TRUE);
foreach ($modules as $module) {
if ($enabled) {
$message = 'Module "@module" is enabled.';
}
else {
$message = 'Module "@module" is not enabled.';
}
$this->assertEqual(module_exists($module), $enabled, t($message, array('@module' => $module)));
}
}

Dries Buytaert
committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Verify a log entry was entered for a module's status change.
* Called in the same way of the expected original watchdog() execution.
*
* @param $type
* The category to which this message belongs.
* @param $message
* The message to store in the log. Keep $message translatable
* by not concatenating dynamic values into it! Variables in the
* message should be added by using placeholder strings alongside
* the variables argument to declare the value of the placeholders.
* See t() for documentation on how $message and $variables interact.
* @param $variables
* Array of variables to replace in the message on display or
* NULL if message is already translated or not possible to
* translate.
* @param $severity
* The severity of the message, as per RFC 3164.
* @param $link
* A link to associate with the message.
*/
function assertLogMessage($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = '') {
$count = db_select('watchdog', 'w')
->condition('type', $type)
->condition('message', $message)
->condition('variables', serialize($variables))
->condition('severity', $severity)
->condition('link', $link)
->countQuery()
->execute()
->fetchField();
$this->assertTrue($count > 0, t('watchdog table contains @count rows for @message', array('@count' => $count, '@message' => $message)));
}

Dries Buytaert
committed
}

Dries Buytaert
committed
/**
* Test module enabling/disabling functionality.
*/
class EnableDisableTestCase extends ModuleTestCase {

Angie Byron
committed
public static function getInfo() {

Dries Buytaert
committed
return array(
'name' => 'Enable/disable modules',
'description' => 'Enable/disable core module and confirm table creation/deletion.',
'group' => 'Module',

Dries Buytaert
committed
);
}
/**
* Enable a module, check the database for related tables, disable module,
* check for related tables, uninstall module, check for related tables.
* Also check for invocation of the hook_module_action hook.
*/
function testEnableDisable() {
// Enable aggregator, and check tables.
$this->assertModules(array('aggregator'), FALSE);
$this->assertTableCount('aggregator', FALSE);
// Install (and enable) aggregator module.
$edit = array();

Angie Byron
committed
$edit['modules[Core][aggregator][enable]'] = 'aggregator';
$edit['modules[Core][forum][enable]'] = 'forum';

Angie Byron
committed
$this->drupalPost('admin/modules', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
// Check that hook_modules_installed and hook_modules_enabled hooks were invoked and check tables.
$this->assertText(t('hook_modules_installed fired for aggregator'), t('hook_modules_installed fired.'));
$this->assertText(t('hook_modules_enabled fired for aggregator'), t('hook_modules_enabled fired.'));
$this->assertModules(array('aggregator'), TRUE);
$this->assertTableCount('aggregator', TRUE);

Dries Buytaert
committed
$this->assertLogMessage('system', "%module module enabled.", array('%module' => 'aggregator'), WATCHDOG_INFO);
// Disable aggregator, check tables, uninstall aggregator, check tables.
$edit = array();

Angie Byron
committed
$edit['modules[Core][aggregator][enable]'] = FALSE;

Angie Byron
committed
$this->drupalPost('admin/modules', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
// Check that hook_modules_disabled hook was invoked and check tables.
$this->assertText(t('hook_modules_disabled fired for aggregator'), t('hook_modules_disabled fired.'));
$this->assertModules(array('aggregator'), FALSE);
$this->assertTableCount('aggregator', TRUE);

Dries Buytaert
committed
$this->assertLogMessage('system', "%module module disabled.", array('%module' => 'aggregator'), WATCHDOG_INFO);
// Uninstall the module.
$edit = array();
$edit['uninstall[aggregator]'] = 'aggregator';

Angie Byron
committed
$this->drupalPost('admin/modules/uninstall', $edit, t('Uninstall'));
$this->drupalPost(NULL, NULL, t('Uninstall'));
$this->assertText(t('The selected modules have been uninstalled.'), t('Modules status has been updated.'));
// Check that hook_modules_uninstalled hook was invoked and check tables.
$this->assertText(t('hook_modules_uninstalled fired for aggregator'), t('hook_modules_uninstalled fired.'));
$this->assertModules(array('aggregator'), FALSE);
$this->assertTableCount('aggregator', FALSE);

Dries Buytaert
committed
$this->assertLogMessage('system', "%module module uninstalled.", array('%module' => 'aggregator'), WATCHDOG_INFO);

Dries Buytaert
committed
// Reinstall (and enable) aggregator module.
$edit = array();
$edit['modules[Core][aggregator][enable]'] = 'aggregator';

Angie Byron
committed
$this->drupalPost('admin/modules', $edit, t('Save configuration'));

Dries Buytaert
committed
$this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
}

Dries Buytaert
committed
}
/**
* Test module dependency functionality.
*/
class ModuleDependencyTestCase extends ModuleTestCase {

Angie Byron
committed
public static function getInfo() {

Dries Buytaert
committed
return array(
'name' => 'Module dependencies',
'description' => 'Enable module without dependency enabled.',
'group' => 'Module',

Dries Buytaert
committed
);
}
/**
* Attempt to enable translation module without locale enabled.
*/
function testEnableWithoutDependency() {
// Attempt to enable content translation without locale enabled.
$edit = array();

Angie Byron
committed
$edit['modules[Core][translation][enable]'] = 'translation';

Angie Byron
committed
$this->drupalPost('admin/modules', $edit, t('Save configuration'));
$this->assertText(t('Some required modules must be enabled'), t('Dependecy required.'));
$this->assertModules(array('translation', 'locale'), FALSE);
// Assert that the locale tables weren't enabled.
$this->assertTableCount('languages', FALSE);
$this->assertTableCount('locale', FALSE);
$this->drupalPost(NULL, NULL, t('Continue'));
$this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
$this->assertModules(array('translation', 'locale'), TRUE);
// Assert that the locale tables were enabled.
$this->assertTableCount('languages', TRUE);
$this->assertTableCount('locale', TRUE);
}
/**
* Attempt to enable a module with a missing dependency.
*/
function testMissingModules() {
// Test that the system_dependencies_test module is marked
// as missing a dependency.

Angie Byron
committed
$this->drupalGet('admin/modules');
$this->assertRaw(t('@module (<span class="admin-missing">missing</span>)', array('@module' => drupal_ucfirst('_missing_dependency'))), t('A module with missing dependencies is marked as such.'));
$checkbox = $this->xpath('//input[@type="checkbox" and @disabled="disabled" and @name="modules[Testing][system_dependencies_test][enable]"]');
$this->assert(count($checkbox) == 1, t('Checkbox for the module is disabled.'));
// Force enable the system_dependencies_test module.

Angie Byron
committed
module_enable(array('system_dependencies_test'), FALSE);
// Verify that the module is forced to be disabled when submitting
// the module page.

Angie Byron
committed
$this->drupalPost('admin/modules', array(), t('Save configuration'));
$this->assertText(t('The @module module is missing, so the following module will be disabled: @depends.', array('@module' => '_missing_dependency', '@depends' => 'system_dependencies_test')), t('The module missing dependencies will be disabled.'));
// Confirm.
$this->drupalPost(NULL, NULL, t('Continue'));
// Verify that the module has been disabled.
$this->assertModules(array('system_dependencies_test'), FALSE);
}

Dries Buytaert
committed
}

Dries Buytaert
committed
/**
* Test module dependency on specific versions.
*/
class ModuleVersionTestCase extends ModuleTestCase {
public static function getInfo() {
return array(
'name' => 'Module versions',
'description' => 'Check module version dependencies.',
'group' => 'Module',
);
}

Angie Byron
committed
function setUp() {

Dries Buytaert
committed
parent::setUp('module_test');
}
/**
* Test version dependencies.
*/
function testModuleVersions() {
$dependencies = array(

Angie Byron
committed
// Alternating between being compatible and incompatible with 7.x-2.4-beta3.

Dries Buytaert
committed
// The first is always a compatible.
'common_test',
// Branch incompatibility.
'common_test (1.x)',
// Branch compatibility.
'common_test (2.x)',
// Another branch incompatibility.
'common_test (>2.x)',
// Another branch compatibility.
'common_test (<=2.x)',
// Another branch incompatibility.
'common_test (<2.x)',
// Another branch compatibility.
'common_test (>=2.x)',
// Nonsense, misses a dash. Incompatible with everything.
'common_test (=7.x2.x, >=2.4)',
// Core version is optional. Compatible.

Angie Byron
committed
'common_test (=7.x-2.x, >=2.4-alpha2)',

Dries Buytaert
committed
// Test !=, explicitly incompatible.

Angie Byron
committed
'common_test (=2.x, !=2.4-beta3)',

Dries Buytaert
committed
// Three operations. Compatible.
'common_test (=2.x, !=2.3, <2.5)',

Angie Byron
committed
// Testing extra version. Incompatible.
'common_test (<=2.4-beta2)',
// Testing extra version. Compatible.
'common_test (>2.4-beta2)',
// Testing extra version. Incompatible.
'common_test (>2.4-rc0)',

Dries Buytaert
committed
);
variable_set('dependencies', $dependencies);
$n = count($dependencies);
for ($i = 0; $i < $n; $i++) {

Angie Byron
committed
$this->drupalGet('admin/modules');

Dries Buytaert
committed
$checkbox = $this->xpath('//input[@id="edit-modules-testing-module-test-enable"]');

Dries Buytaert
committed
$this->assertEqual(!empty($checkbox[0]['disabled']), $i % 2, $dependencies[$i]);
}
}
}

Dries Buytaert
committed
/**
* Test required modules functionality.
*/
class ModuleRequiredTestCase extends ModuleTestCase {

Angie Byron
committed
public static function getInfo() {

Dries Buytaert
committed
return array(
'name' => 'Required modules',
'description' => 'Attempt disabling of required modules.',
'group' => 'Module',

Dries Buytaert
committed
);
}
/**
* Assert that core required modules cannot be disabled.
*/
function testDisableRequired() {
$required_modules = drupal_required_modules();

Angie Byron
committed
$this->drupalGet('admin/modules');
foreach ($required_modules as $module) {
// Check to make sure the checkbox for required module is not found.
$this->assertNoFieldByName('modules[Core][' . $module . '][enable]');
}
}
}

Dries Buytaert
committed

Dries Buytaert
committed
class IPAddressBlockingTestCase extends DrupalWebTestCase {
protected $blocking_user;

Dries Buytaert
committed
/**

Dries Buytaert
committed
* Implement getInfo().

Dries Buytaert
committed
*/

Angie Byron
committed
public static function getInfo() {

Dries Buytaert
committed
return array(
'name' => 'IP address blocking',
'description' => 'Test IP address blocking.',
'group' => 'System'

Dries Buytaert
committed
);
}
/**

Dries Buytaert
committed
* Implement setUp().

Dries Buytaert
committed
*/
function setUp() {
parent::setUp();
// Create user.

Dries Buytaert
committed
$this->blocking_user = $this->drupalCreateUser(array('block IP addresses'));
$this->drupalLogin($this->blocking_user);

Dries Buytaert
committed
}
/**
* Test a variety of user input to confirm correct validation and saving of data.

Dries Buytaert
committed
*/
function testIPAddressValidation() {
$this->drupalGet('admin/config/people/ip-blocking');

Dries Buytaert
committed
// Block a valid IP address.
$edit = array();
$edit['ip'] = '192.168.1.1';
$this->drupalPost('admin/config/people/ip-blocking', $edit, t('Save'));

Dries Buytaert
committed
$ip = db_query("SELECT iid from {blocked_ips} WHERE ip = :ip", array(':ip' => $edit['ip']))->fetchField();

Dries Buytaert
committed
$this->assertTrue($ip, t('IP address found in database.'));

Dries Buytaert
committed
$this->assertRaw(t('The IP address %ip has been blocked.', array('%ip' => $edit['ip'])), t('IP address was blocked.'));
// Try to block an IP address that's already blocked.
$edit = array();
$edit['ip'] = '192.168.1.1';
$this->drupalPost('admin/config/people/ip-blocking', $edit, t('Save'));

Dries Buytaert
committed
$this->assertText(t('This IP address is already blocked.'));
// Try to block a reserved IP address.
$edit = array();
$edit['ip'] = '255.255.255.255';
$this->drupalPost('admin/config/people/ip-blocking', $edit, t('Save'));
$this->assertText(t('Enter a valid IP address.'));

Dries Buytaert
committed
// Try to block a reserved IP address.
$edit = array();
$edit['ip'] = 'test.example.com';
$this->drupalPost('admin/config/people/ip-blocking', $edit, t('Save'));
$this->assertText(t('Enter a valid IP address.'));

Dries Buytaert
committed
// Submit an empty form.
$edit = array();
$edit['ip'] = '';
$this->drupalPost('admin/config/people/ip-blocking', $edit, t('Save'));
$this->assertText(t('Enter a valid IP address.'));

Dries Buytaert
committed

Dries Buytaert
committed
// Pass an IP address as a URL parameter and submit it.
$submit_ip = '1.2.3.4';
$this->drupalPost('admin/config/people/ip-blocking/' . $submit_ip, NULL, t('Save'));
$ip = db_query("SELECT iid from {blocked_ips} WHERE ip = :ip", array(':ip' => $submit_ip))->fetchField();
$this->assertTrue($ip, t('IP address found in database'));
$this->assertRaw(t('The IP address %ip has been blocked.', array('%ip' => $submit_ip)), t('IP address was blocked.'));

Dries Buytaert
committed
// Submit your own IP address. This fails, although it works when testing manually.

Dries Buytaert
committed
// TODO: on some systems this test fails due to a bug or inconsistency in cURL.
// $edit = array();
// $edit['ip'] = ip_address();
// $this->drupalPost('admin/config/people/ip-blocking', $edit, t('Save'));

Dries Buytaert
committed
// $this->assertText(t('You may not block your own IP address.'));

Dries Buytaert
committed
}
}

Dries Buytaert
committed
class CronRunTestCase extends DrupalWebTestCase {

Dries Buytaert
committed
* Implement getInfo().

Angie Byron
committed
public static function getInfo() {
'name' => 'Cron run',
'description' => 'Test cron run.',
'group' => 'System'
);
}
/**
* Test cron runs.
*/
function testCronRun() {

Dries Buytaert
committed
global $base_url;

Dries Buytaert
committed
// Run cron anonymously without any cron key.

Dries Buytaert
committed
$this->drupalGet($base_url . '/cron.php', array('external' => TRUE));
$this->assertResponse(403);
// Run cron anonymously with a random cron key.
$key = $this->randomName(16);

Dries Buytaert
committed
$this->drupalGet($base_url . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => $key)));
$this->assertResponse(403);
// Run cron anonymously with the valid cron key.
$key = variable_get('cron_key', 'drupal');

Dries Buytaert
committed
$this->drupalGet($base_url . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => $key)));
$this->assertResponse(200);
}

Dries Buytaert
committed

Dries Buytaert
committed
/**

Dries Buytaert
committed
* Ensure that the automatic cron run callback is working.

Dries Buytaert
committed
*
* In these tests we do not use REQUEST_TIME to track start time, because we
* need the exact time when cron is triggered.
*/

Dries Buytaert
committed
function testAutomaticCron() {

Dries Buytaert
committed
// Ensure cron does not run when the cron threshold is enabled and was
// not passed.

Dries Buytaert
committed
$cron_last = time();
$cron_safe_threshold = 100;
variable_set('cron_last', $cron_last);
variable_set('cron_safe_threshold', $cron_safe_threshold);

Dries Buytaert
committed
$this->drupalGet('');

Dries Buytaert
committed
$this->assertRaw('"cronCheck":' . ($cron_last + $cron_safe_threshold));
$this->drupalGet('system/run-cron-check');
$this->assertExpiresHeader($cron_last + $cron_safe_threshold);
$this->assertTrue($cron_last == variable_get('cron_last', NULL), t('Cron does not run when the cron threshold is not passed.'));

Dries Buytaert
committed
// Test if cron runs when the cron threshold was passed.

Dries Buytaert
committed
$cron_last = time() - 200;
variable_set('cron_last', $cron_last);

Dries Buytaert
committed
$this->drupalGet('');

Dries Buytaert
committed
$this->assertRaw('"cronCheck":' . ($cron_last + $cron_safe_threshold));
sleep(1);
$this->drupalGet('system/run-cron-check');
$this->assertExpiresHeader(variable_get('cron_last', NULL) + $cron_safe_threshold);
$this->assertTrue($cron_last < variable_get('cron_last', NULL), t('Cron runs when the cron threshold is passed.'));
// Disable the cron threshold through the interface.
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
$this->drupalLogin($admin_user);
$this->drupalPost('admin/config/system/site-information', array('cron_safe_threshold' => 0), t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'));
$this->drupalLogout();
// Test if cron does not run when the cron threshold is disabled.
$cron_last = time() - 200;
variable_set('cron_last', $cron_last);

Dries Buytaert
committed
$this->drupalGet('');

Dries Buytaert
committed
$this->assertNoRaw('cronCheck');
$this->drupalGet('system/run-cron-check');
$this->assertResponse(403);
$this->assertTrue($cron_last == variable_get('cron_last', NULL), t('Cron does not run when the cron threshold is disabled.'));
}
/**
* Assert that the Expires header is a specific timestamp.
*
* @param $timestamp
* The timestamp value to match against the header.
*/
private function assertExpiresHeader($timestamp) {
$expires = $this->drupalGetHeader('Expires');
$expires = strtotime($expires);
$this->assertEqual($expires, $timestamp, t('Expires header expected @expected got @actual.', array('@expected' => $timestamp, '@actual' => $expires)));

Dries Buytaert
committed
}

Dries Buytaert
committed
/**
* Ensure that temporary files are removed.

Dries Buytaert
committed
*
* Create files for all the possible combinations of age and status. We are
* using UPDATE statements rather than file_save() because it would set the

Dries Buytaert
committed
* timestamp.

Dries Buytaert
committed
*/
function testTempFileCleanup() {
// Temporary file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
$temp_old = file_save_data('');
db_update('file')

Dries Buytaert
committed
->fields(array(
'status' => 0,
'timestamp' => 1,
))
->condition('fid', $temp_old->fid)
->execute();
$this->assertTrue(file_exists($temp_old->uri), t('Old temp file was created correctly.'));

Dries Buytaert
committed
// Temporary file that is less than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
$temp_new = file_save_data('');
db_update('file')

Dries Buytaert
committed
->fields(array('status' => 0))
->condition('fid', $temp_new->fid)
->execute();
$this->assertTrue(file_exists($temp_new->uri), t('New temp file was created correctly.'));

Dries Buytaert
committed
// Permanent file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
$perm_old = file_save_data('');
db_update('file')

Dries Buytaert
committed
->fields(array('timestamp' => 1))
->condition('fid', $temp_old->fid)
->execute();
$this->assertTrue(file_exists($perm_old->uri), t('Old permanent file was created correctly.'));

Dries Buytaert
committed
// Permanent file that is newer than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
$perm_new = file_save_data('');
$this->assertTrue(file_exists($perm_new->uri), t('New permanent file was created correctly.'));

Dries Buytaert
committed
// Run cron and then ensure that only the old, temp file was deleted.

Angie Byron
committed
$this->cronRun();
$this->assertFalse(file_exists($temp_old->uri), t('Old temp file was correctly removed.'));
$this->assertTrue(file_exists($temp_new->uri), t('New temp file was correctly ignored.'));
$this->assertTrue(file_exists($perm_old->uri), t('Old permanent file was correctly ignored.'));
$this->assertTrue(file_exists($perm_new->uri), t('New permanent file was correctly ignored.'));

Dries Buytaert
committed
}

Dries Buytaert
committed
}
class AdminMetaTagTestCase extends DrupalWebTestCase {
/**

Dries Buytaert
committed
* Implement getInfo().

Angie Byron
committed
public static function getInfo() {
return array(
'name' => 'Fingerprinting meta tag',
'description' => 'Confirm that the fingerprinting meta tag appears as expected.',
'group' => 'System'
);
}
/**
* Verify that the meta tag HTML is generated correctly.
*/
public function testMetaTag() {
list($version, ) = explode('.', VERSION);
$string = '<meta name="Generator" content="Drupal ' . $version . ' (http://drupal.org)" />';
$this->drupalGet('node');
$this->assertRaw($string, t('Fingerprinting meta tag generated correctly.'), t('System'));
}
}

Dries Buytaert
committed
/**
* Tests custom access denied functionality.
*/
class AccessDeniedTestCase extends DrupalWebTestCase {
protected $admin_user;

Angie Byron
committed
public static function getInfo() {
'name' => '403 functionality',

Dries Buytaert
committed
'description' => 'Tests page access denied functionality, including custom 403 pages.',
'group' => 'System'
);
}
function setUp() {
parent::setUp();
// Create an administrative user.

Angie Byron
committed
$this->admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer blocks'));
$this->drupalLogin($this->admin_user);
}
function testAccessDenied() {
$this->drupalGet('admin');
$this->assertText(t('Access denied'), t('Found the default 403 page'));

Angie Byron
committed
$this->assertResponse(403);
'title' => $this->randomName(10),
'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(100)))),
);
$node = $this->drupalCreateNode($edit);
// Use a custom 403 page.

Angie Byron
committed
$this->drupalPost('admin/config/system/site-information', array('site_403' => 'node/' . $node->nid), t('Save configuration'));
$this->drupalGet('admin');
$this->assertText($node->title, t('Found the custom 403 page'));
// Logout and check that the user login block is shown on custom 403 pages.
$this->drupalLogout();
$this->drupalGet('admin');
$this->assertText($node->title, t('Found the custom 403 page'));
$this->assertText(t('User login'), t('Blocks are shown on the custom 403 page'));
// Log back in and remove the custom 403 page.
$this->drupalLogin($this->admin_user);

Angie Byron
committed
$this->drupalPost('admin/config/system/site-information', array('site_403' => ''), t('Save configuration'));
// Logout and check that the user login block is shown on default 403 pages.
$this->drupalLogout();
$this->drupalGet('admin');
$this->assertText(t('Access denied'), t('Found the default 403 page'));

Angie Byron
committed
$this->assertResponse(403);
$this->assertText(t('User login'), t('Blocks are shown on the default 403 page'));

Angie Byron
committed
// Log back in, set the custom 403 page to /user and remove the block
$this->drupalLogin($this->admin_user);
variable_set('site_403', 'user');
$this->drupalPost('admin/structure/block', array('user_login[region]' => '-1'), t('Save blocks'));
// Check that we can log in from the 403 page.
$this->drupalLogout();
$edit = array(
'name' => $this->admin_user->name,
'pass' => $this->admin_user->pass_raw,
);

Angie Byron
committed
$this->drupalPost('admin/config/system/site-information', $edit, t('Log in'));

Angie Byron
committed
// Check that we're still on the same page.
$this->assertText(t('Site information'));

Dries Buytaert
committed
class PageNotFoundTestCase extends DrupalWebTestCase {
protected $admin_user;

Dries Buytaert
committed
/**

Dries Buytaert
committed
* Implement getInfo().

Dries Buytaert
committed
*/

Angie Byron
committed
public static function getInfo() {

Dries Buytaert
committed
return array(
'name' => '404 functionality',
'description' => "Tests page not found functionality, including custom 404 pages.",
'group' => 'System'

Dries Buytaert
committed
);
}

Dries Buytaert
committed
/**

Dries Buytaert
committed
* Implement setUp().

Dries Buytaert
committed
*/
function setUp() {
parent::setUp();
// Create an administrative user.
$this->admin_user = $this->drupalCreateUser(array('administer site configuration'));
$this->drupalLogin($this->admin_user);
}
function testPageNotFound() {
$this->drupalGet($this->randomName(10));
$this->assertText(t('Page not found'), t('Found the default 404 page'));

Dries Buytaert
committed
$edit = array(
'title' => $this->randomName(10),
'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(100)))),

Dries Buytaert
committed
);
$node = $this->drupalCreateNode($edit);
// Use a custom 404 page.

Angie Byron
committed
$this->drupalPost('admin/config/system/site-information', array('site_404' => 'node/' . $node->nid), t('Save configuration'));

Dries Buytaert
committed
$this->drupalGet($this->randomName(10));
$this->assertText($node->title, t('Found the custom 404 page'));

Dries Buytaert
committed
}
}

Angie Byron
committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/**
* Tests site maintenance functionality.
*/
class SiteMaintenanceTestCase extends DrupalWebTestCase {
protected $admin_user;
public static function getInfo() {
return array(
'name' => 'Site maintenance mode functionality',
'description' => 'Test access to site while in maintenance mode.',
'group' => 'System',
);
}
function setUp() {
parent::setUp();
// Create a user allowed to access site in maintenance mode.
$this->user = $this->drupalCreateUser(array('access site in maintenance mode'));
// Create an administrative user.
$this->admin_user = $this->drupalCreateUser(array('administer site configuration', 'access site in maintenance mode'));
$this->drupalLogin($this->admin_user);
}
/**
* Verify site maintenance mode functionality.
*/
function testSiteMaintenance() {
// Turn on maintenance mode.
$edit = array(
'maintenance_mode' => 1,
);
$this->drupalPost('admin/config/development/maintenance', $edit, t('Save configuration'));
$admin_message = t('Operating in maintenance mode. <a href="@url">Go online.</a>', array('@url' => url('admin/config/development/maintenance')));
$user_message = t('Operating in maintenance mode.');
$offline_message = t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')));
$this->drupalGet('');
$this->assertRaw($admin_message, t('Found the site maintenance mode message.'));
// Logout and verify that offline message is displayed.
$this->drupalLogout();
$this->drupalGet('');
$this->assertText($offline_message);
$this->drupalGet('node');
$this->assertText($offline_message);
$this->drupalGet('user/register');
$this->assertText($offline_message);
$this->drupalGet('user/password');
$this->assertText($offline_message);
// Verify that user is able to log in.
$this->drupalGet('user');
$this->assertNoText($offline_message);
$this->drupalGet('user/login');
$this->assertNoText($offline_message);
// Log in user and verify that maintenance mode message is displayed
// directly after login.
$edit = array(
'name' => $this->user->name,
'pass' => $this->user->pass_raw,
);
$this->drupalPost(NULL, $edit, t('Log in'));
$this->assertText($user_message);
// Log in administrative user and configure a custom site offline message.
$this->drupalLogout();
$this->drupalLogin($this->admin_user);
$this->drupalGet('admin/config/development/maintenance');
$this->assertNoRaw($admin_message, t('Site maintenance mode message not displayed.'));
$offline_message = 'Sorry, not online.';
$edit = array(
'maintenance_mode_message' => $offline_message,
);
$this->drupalPost(NULL, $edit, t('Save configuration'));
// Logout and verify that custom site offline message is displayed.
$this->drupalLogout();
$this->drupalGet('');
$this->assertRaw($offline_message, t('Found the site offline message.'));
}
}

Angie Byron
committed
/**
* Tests generic date and time handling capabilities of Drupal.
*/
class DateTimeFunctionalTest extends DrupalWebTestCase {

Angie Byron
committed
public static function getInfo() {

Angie Byron
committed
return array(
'name' => 'Date and time',
'description' => 'Configure date and time settings. Test date formatting and time zone handling, including daylight saving time.',
'group' => 'System',

Angie Byron
committed
);
}

Dries Buytaert
committed
function setUp() {
parent::setUp();
// Create admin user and log in admin user.
$this->admin_user = $this->drupalCreateUser(array('administer site configuration'));
$this->drupalLogin($this->admin_user);
}

Angie Byron
committed
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
/**
* Test time zones and DST handling.
*/
function testTimeZoneHandling() {
// Setup date/time settings for Honolulu time.
variable_set('date_default_timezone', 'Pacific/Honolulu');
variable_set('configurable_timezones', 0);
variable_set('date_format_medium', 'Y-m-d H:i:s O');
// Create some nodes with different authored-on dates.
$date1 = '2007-01-31 21:00:00 -1000';
$date2 = '2007-07-31 21:00:00 -1000';
$node1 = $this->drupalCreateNode(array('created' => strtotime($date1), 'type' => 'article'));
$node2 = $this->drupalCreateNode(array('created' => strtotime($date2), 'type' => 'article'));
// Confirm date format and time zone.
$this->drupalGet("node/$node1->nid");
$this->assertText('2007-01-31 21:00:00 -1000', t('Date should be identical, with GMT offset of -10 hours.'));
$this->drupalGet("node/$node2->nid");
$this->assertText('2007-07-31 21:00:00 -1000', t('Date should be identical, with GMT offset of -10 hours.'));
// Set time zone to Los Angeles time.
variable_set('date_default_timezone', 'America/Los_Angeles');
// Confirm date format and time zone.
$this->drupalGet("node/$node1->nid");
$this->assertText('2007-01-31 23:00:00 -0800', t('Date should be two hours ahead, with GMT offset of -8 hours.'));
$this->drupalGet("node/$node2->nid");
$this->assertText('2007-08-01 00:00:00 -0700', t('Date should be three hours ahead, with GMT offset of -7 hours.'));
}

Dries Buytaert
committed
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/**
* Test date type configuration.
*/
function testDateTypeConfiguration() {
// Confirm system date types appear.
$this->drupalGet('admin/config/regional/date-time');
$this->assertText(t('Medium'), 'System date types appear in date type list.');
$this->assertNoRaw('href="/admin/config/regional/date-time/types/medium/delete"', 'No delete link appear for system date types.');
// Add custom date type.
$this->clickLink(t('Add date type'));
$date_type = $this->randomName(8);
$machine_name = 'machine_' . $date_type;
$date_format = 'd.m.Y - H:i';
$edit = array(
'date_type' => $date_type,
'machine_name' => $machine_name,
'date_format' => $date_format,
);
$this->drupalPost('admin/config/regional/date-time/types/add', $edit, t('Add date type'));
$this->assertEqual($this->getUrl(), url('admin/config/regional/date-time', array('absolute' => TRUE)), t('Correct page redirection.'));
$this->assertText(t('New date type added successfully.'), 'Date type added confirmation message appears.');
$this->assertText($date_type, 'Custom date type appears in the date type list.');
$this->assertText(t('delete'), 'Delete link for custom date type appears.');
// Delete custom date type.
$this->clickLink(t('delete'));
$this->drupalPost('admin/config/regional/date-time/types/' . $machine_name . '/delete', array(), t('Remove'));
$this->assertEqual($this->getUrl(), url('admin/config/regional/date-time', array('absolute' => TRUE)), t('Correct page redirection.'));
$this->assertText(t('Removed date type ' . $date_type), 'Custom date type removed.');
}
/**
* Test date format configuration.
*/
function testDateFormatConfiguration() {
// Confirm 'no custom date formats available' message appears.
$this->drupalGet('admin/config/regional/date-time/formats');
$this->assertText(t('No custom date formats available.'), 'No custom date formats message appears.');
// Add custom date format.
$this->clickLink(t('Add format'));
$edit = array(
'date_format' => 'Y',
);
$this->drupalPost('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
$this->assertEqual($this->getUrl(), url('admin/config/regional/date-time/formats', array('absolute' => TRUE)), t('Correct page redirection.'));
$this->assertNoText(t('No custom date formats available.'), 'No custom date formats message does not appear.');
$this->assertText(t('Custom date format added.'), 'Custom date format added.');
// Ensure custom date format appears in date type configuration options.
$this->drupalGet('admin/config/regional/date-time');
$this->assertRaw('<option value="Y">', 'Custom date format appears in options.');
// Edit custom date format.
$this->drupalGet('admin/config/regional/date-time/formats');
$this->clickLink(t('edit'));
$edit = array(
'date_format' => 'Y m',
);
$this->drupalPost($this->getUrl(), $edit, t('Save format'));
$this->assertEqual($this->getUrl(), url('admin/config/regional/date-time/formats', array('absolute' => TRUE)), t('Correct page redirection.'));
$this->assertText(t('Custom date format updated.'), 'Custom date format successfully updated.');
// Delete custom date format.
$this->clickLink(t('delete'));
$this->drupalPost($this->getUrl(), array(), t('Remove'));
$this->assertEqual($this->getUrl(), url('admin/config/regional/date-time/formats', array('absolute' => TRUE)), t('Correct page redirection.'));
$this->assertText(t('Removed date format'), 'Custom date format removed successfully.');
}

Angie Byron
committed
}

Angie Byron
committed
class PageTitleFiltering extends DrupalWebTestCase {
protected $content_user;
protected $saved_title;
/**

Dries Buytaert
committed
* Implement getInfo().

Angie Byron
committed
*/

Angie Byron
committed
public static function getInfo() {

Angie Byron
committed
return array(
'name' => 'HTML in page titles',
'description' => 'Tests correct handling or conversion by drupal_set_title() and drupal_get_title().',
'group' => 'System'

Angie Byron
committed
);
}
/**

Dries Buytaert
committed
* Implement setUp().

Angie Byron
committed
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
*/
function setUp() {
parent::setUp();
$this->content_user = $this->drupalCreateUser(array('create page content', 'access content'));
$this->drupalLogin($this->content_user);
$this->saved_title = drupal_get_title();
}
/**
* Reset page title.
*/
function tearDown() {
// Restore the page title.
drupal_set_title($this->saved_title, PASS_THROUGH);
parent::tearDown();
}
/**
* Tests the handling of HTML by drupal_set_title() and drupal_get_title()
*/
function testTitleTags() {
$title = "string with <em>HTML</em>";
// drupal_set_title's $filter is CHECK_PLAIN by default, so the title should be
// returned with check_plain().
drupal_set_title($title, CHECK_PLAIN);
$this->assertTrue(strpos(drupal_get_title(), '<em>') === FALSE, t('Tags in title converted to entities when $output is CHECK_PLAIN.'));
// drupal_set_title's $filter is passed as PASS_THROUGH, so the title should be
// returned with HTML.
drupal_set_title($title, PASS_THROUGH);
$this->assertTrue(strpos(drupal_get_title(), '<em>') !== FALSE, t('Tags in title are not converted to entities when $output is PASS_THROUGH.'));
// Generate node content.
$langcode = LANGUAGE_NONE;

Angie Byron
committed
$edit = array(
"title" => '!SimpleTest! ' . $title . $this->randomName(20),

Angie Byron
committed
"body[$langcode][0][value]" => '!SimpleTest! test body' . $this->randomName(200),

Angie Byron
committed
);
// Create the node with HTML in the title.
$this->drupalPost('node/add/page', $edit, t('Save'));
$node = $this->drupalGetNodeByTitle($edit["title"]);

Angie Byron
committed
$this->assertNotNull($node, 'Node created and found in database');
$this->drupalGet("node/" . $node->nid);
$this->assertText(check_plain($edit["title"]), 'Check to make sure tags in the node title are converted.');

Angie Byron
committed
}
}

Dries Buytaert
committed
/**
* Test front page functionality and administration.
*/
class FrontPageTestCase extends DrupalWebTestCase {

Angie Byron
committed
public static function getInfo() {

Dries Buytaert
committed
return array(
'name' => 'Front page',
'description' => 'Tests front page functionality and administration.',
'group' => 'System',

Dries Buytaert
committed
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
);
}
function setUp() {
parent::setUp('system_test');
// Create admin user, log in admin user, and create one node.
$this->admin_user = $this->drupalCreateUser(array('access content', 'administer site configuration'));
$this->drupalLogin($this->admin_user);
$this->node_path = "node/" . $this->drupalCreateNode(array('promote' => 1))->nid;
// Enable front page logging in system_test.module.
variable_set('front_page_output', 1);
}
/**
* Test front page functionality.
*/
function testDrupalIsFrontPage() {
$this->drupalGet('');
$this->assertText(t('On front page.'), t('Path is the front page.'));
$this->drupalGet('node');
$this->assertText(t('On front page.'), t('Path is the front page.'));
$this->drupalGet($this->node_path);
$this->assertNoText(t('On front page.'), t('Path is not the front page.'));
// Change the front page to an invalid path.
$edit = array('site_frontpage' => 'kittens');

Angie Byron
committed
$this->drupalPost('admin/config/system/site-information', $edit, t('Save configuration'));

Dries Buytaert
committed
$this->assertText(t("The path '@path' is either invalid or you do not have access to it.", array('@path' => $edit['site_frontpage'])));
// Change the front page to a valid path.
$edit['site_frontpage'] = $this->node_path;

Angie Byron
committed
$this->drupalPost('admin/config/system/site-information', $edit, t('Save configuration'));

Dries Buytaert
committed
$this->assertText(t('The configuration options have been saved.'), t('The front page path has been saved.'));
$this->drupalGet('');
$this->assertText(t('On front page.'), t('Path is the front page.'));
$this->drupalGet('node');
$this->assertNoText(t('On front page.'), t('Path is not the front page.'));
$this->drupalGet($this->node_path);
$this->assertText(t('On front page.'), t('Path is the front page.'));
}
}