2009-01-03 08:45:28 +00:00
<?php
2009-04-26 15:14:55 +00:00
// $Id$
2009-01-03 08:45:28 +00:00
/**
* @file
* Tests for the module API.
*/
/**
* Unit tests for the module API.
*/
class ModuleUnitTest extends DrupalWebTestCase {
2009-03-31 01:49:55 +00:00
public static function getInfo() {
2009-01-03 08:45:28 +00:00
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Module API',
'description' => 'Test low-level module functions.',
'group' => 'Module',
2009-01-03 08:45:28 +00:00
);
}
/**
* The basic functionality of module_list().
*/
function testModuleList() {
2009-06-20 06:00:24 +00:00
// Build a list of modules, sorted alphabetically.
2010-01-05 18:00:14 +00:00
$profile_info = install_profile_info('standard', 'en');
2009-07-19 04:48:10 +00:00
$module_list = $profile_info['dependencies'];
2009-08-21 07:50:08 +00:00
// Install profile is a module that is expected to be loaded.
2010-01-05 18:00:14 +00:00
$module_list[] = 'standard';
2009-08-21 07:50:08 +00:00
2009-06-20 06:00:24 +00:00
sort($module_list);
// Compare this list to the one returned by module_list(). We expect them
// to match, since all default profile modules have a weight equal to 0
// (except for block.module, which has a lower weight but comes first in
// the alphabet anyway).
2010-01-05 18:00:14 +00:00
$this->assertModuleList($module_list, t('Standard profile'));
2009-01-03 08:45:28 +00:00
// Try to install a new module.
2010-01-13 05:08:29 +00:00
module_enable(array('contact'));
2009-06-20 06:00:24 +00:00
$module_list[] = 'contact';
sort($module_list);
2009-06-12 08:39:40 +00:00
$this->assertModuleList($module_list, t('After adding a module'));
2009-01-03 08:45:28 +00:00
// Try to mess with the module weights.
2009-05-30 11:17:32 +00:00
db_update('system')
->fields(array('weight' => 20))
2009-06-20 06:00:24 +00:00
->condition('name', 'contact')
2009-05-30 11:17:32 +00:00
->condition('type', 'module')
->execute();
2009-01-03 08:45:28 +00:00
// Reset the module list.
module_list(TRUE);
2009-06-20 06:00:24 +00:00
// Move contact to the end of the array.
unset($module_list[array_search('contact', $module_list)]);
$module_list[] = 'contact';
2009-06-12 08:39:40 +00:00
$this->assertModuleList($module_list, t('After changing weights'));
2009-01-03 08:45:28 +00:00
// Test the fixed list feature.
$fixed_list = array(
'system' => array('filename' => drupal_get_path('module', 'system')),
'menu' => array('filename' => drupal_get_path('module', 'menu')),
);
2009-08-24 00:14:23 +00:00
module_list(FALSE, FALSE, FALSE, $fixed_list);
2009-01-03 08:45:28 +00:00
$new_module_list = array_combine(array_keys($fixed_list), array_keys($fixed_list));
$this->assertModuleList($new_module_list, t('When using a fixed list'));
// Reset the module list.
module_list(TRUE);
2009-06-12 08:39:40 +00:00
$this->assertModuleList($module_list, t('After reset'));
2009-01-03 08:45:28 +00:00
}
/**
* Assert that module_list() return the expected values.
*
* @param $expected_values
2009-06-20 06:00:24 +00:00
* The expected values, sorted by weight and module name.
2009-01-03 08:45:28 +00:00
*/
protected function assertModuleList(Array $expected_values, $condition) {
2009-06-12 08:39:40 +00:00
$expected_values = array_combine($expected_values, $expected_values);
2010-08-05 23:53:39 +00:00
$this->assertEqual($expected_values, module_list(), t('@condition: module_list() returns correct results', array('@condition' => $condition)));
2009-01-03 08:45:28 +00:00
ksort($expected_values);
2010-08-05 23:53:39 +00:00
$this->assertIdentical($expected_values, module_list(FALSE, FALSE, TRUE), t('@condition: module_list() returns correctly sorted results', array('@condition' => $condition)));
2009-01-03 08:45:28 +00:00
}
2009-09-29 18:08:28 +00:00
/**
* Test module_implements() caching.
*/
function testModuleImplements() {
// Clear the cache.
2009-11-18 20:00:31 +00:00
cache_clear_all('module_implements', 'cache_bootstrap');
2010-08-05 23:53:39 +00:00
$this->assertFalse(cache_get('module_implements', 'cache_bootstrap'), t('The module implements cache is empty.'));
2009-09-29 18:08:28 +00:00
$this->drupalGet('');
2010-08-05 23:53:39 +00:00
$this->assertTrue(cache_get('module_implements', 'cache_bootstrap'), t('The module implements cache is populated after requesting a page.'));
2009-09-29 18:08:28 +00:00
// Test again with an authenticated user.
$this->user = $this->drupalCreateUser();
$this->drupalLogin($this->user);
2009-11-18 20:00:31 +00:00
cache_clear_all('module_implements', 'cache_bootstrap');
2009-09-29 18:08:28 +00:00
$this->drupalGet('');
2010-08-05 23:53:39 +00:00
$this->assertTrue(cache_get('module_implements', 'cache_bootstrap'), t('The module implements cache is populated after requesting a page.'));
2010-04-22 18:56:53 +00:00
// Make sure group include files are detected properly even when the file is
// already loaded when the cache is rebuilt.
// For that activate the module_test which provides the file to load.
module_enable(array('module_test'));
module_load_include('inc', 'module_test', 'module_test.file');
$modules = module_implements('test_hook');
$static = drupal_static('module_implements');
$this->assertTrue(in_array('module_test', $modules), 'Hook found.');
$this->assertEqual($static['test_hook']['module_test'], 'file', 'Include file detected.');
2009-09-29 18:08:28 +00:00
}
2010-01-02 18:56:20 +00:00
/**
2010-01-13 05:08:29 +00:00
* Test dependency resolution.
2010-01-02 18:56:20 +00:00
*/
2010-01-13 05:08:29 +00:00
function testDependencyResolution() {
module_enable(array('module_test'), FALSE);
2010-08-05 23:53:39 +00:00
$this->assertTrue(module_exists('module_test'), t('Test module is enabled.'));
2010-01-02 18:56:20 +00:00
// First, create a fake missing dependency. Forum depends on poll, which
// depends on a made-up module, foo. Nothing should be installed.
variable_set('dependency_test', 'missing dependency');
2010-01-13 05:08:29 +00:00
$result = module_enable(array('forum'));
2010-08-05 23:53:39 +00:00
$this->assertFalse($result, t('module_enable() returns FALSE if dependencies are missing.'));
$this->assertFalse(module_exists('forum'), t('module_enable() aborts if dependencies are missing.'));
2010-01-02 18:56:20 +00:00
2010-01-13 05:08:29 +00:00
// Now, fix the missing dependency. module_enable() should work.
2010-01-02 18:56:20 +00:00
variable_set('dependency_test', 'dependency');
2010-01-13 05:08:29 +00:00
$result = module_enable(array('forum'));
2010-08-05 23:53:39 +00:00
$this->assertTrue($result, t('module_enable() returns the correct value.'));
2010-01-02 18:56:20 +00:00
// Verify that the fake dependency chain was installed.
2010-08-05 23:53:39 +00:00
$this->assertTrue(module_exists('poll') && module_exists('php'), t('Dependency chain was installed by module_enable().'));
2010-01-02 18:56:20 +00:00
// Finally, verify that the original module was installed.
2010-08-05 23:53:39 +00:00
$this->assertTrue(module_exists('forum'), t('Module installation with unlisted dependencies succeeded.'));
2010-01-02 18:56:20 +00:00
}
2009-01-03 08:45:28 +00:00
}
2009-07-01 08:39:56 +00:00
2010-02-26 18:31:29 +00:00
/**
* Unit tests for module installation.
*/
class ModuleInstallTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Module installation',
'description' => 'Tests the installation of modules.',
'group' => 'Module',
);
}
function setUp() {
parent::setUp('module_test');
}
/**
* Test that calls to drupal_write_record() work during module installation.
*
* This is a useful function to test because modules often use it to insert
* initial data in their database tables when they are being installed or
* enabled. Furthermore, drupal_write_record() relies on the module schema
* information being available, so this also checks that the data from one of
* the module's hook implementations, in particular hook_schema(), is
* properly available during this time. Therefore, this test helps ensure
* that modules are fully functional while Drupal is installing and enabling
* them.
*/
function testDrupalWriteRecord() {
// Check for data that was inserted using drupal_write_record() while the
// 'module_test' module was being installed and enabled.
$data = db_query("SELECT data FROM {module_test}")->fetchCol();
2010-08-05 23:53:39 +00:00
$this->assertTrue(in_array('Data inserted in hook_install()', $data), t('Data inserted using drupal_write_record() in hook_install() is correctly saved.'));
$this->assertTrue(in_array('Data inserted in hook_enable()', $data), t('Data inserted using drupal_write_record() in hook_enable() is correctly saved.'));
2010-02-26 18:31:29 +00:00
}
}
2009-07-01 08:39:56 +00:00
/**
* Unit tests for module uninstallation and related hooks.
*/
class ModuleUninstallTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Module uninstallation',
2010-02-26 18:31:29 +00:00
'description' => 'Tests the uninstallation of modules.',
2009-07-13 21:51:42 +00:00
'group' => 'Module',
2009-07-01 08:39:56 +00:00
);
}
function setUp() {
parent::setUp('module_test', 'user');
}
/**
* Tests the hook_modules_uninstalled() of the user module.
*/
function testUserPermsUninstalled() {
// Uninstalls the module_test module, so hook_modules_uninstalled()
// is executed.
2010-02-26 18:31:29 +00:00
module_disable(array('module_test'));
2009-07-01 08:39:56 +00:00
drupal_uninstall_modules(array('module_test'));
// Are the perms defined by module_test removed from {role_permission}.
$count = db_query("SELECT COUNT(rid) FROM {role_permission} WHERE permission = :perm", array(':perm' => 'module_test perm'))->fetchField();
2010-08-05 23:53:39 +00:00
$this->assertEqual(0, $count, t('Permissions were all removed.'));
2009-07-01 08:39:56 +00:00
}
}