- <?php
- * @file
- * Tests for the module API.
- */
-
- * Unit tests for the module API.
- */
- class ModuleUnitTest extends BackdropWebTestCase {
-
-
- * @var User
- */
- protected $user;
-
-
- * The basic functionality of module_list().
- */
- function testModuleList() {
-
- $profile_info = install_profile_info('standard', 'en');
- $module_list = $profile_info['dependencies'];
-
-
- $module_list[] = 'standard';
-
- sort($module_list);
-
-
-
-
- $this->assertModuleList($module_list, t('Standard profile'));
-
-
- module_enable(array('dependency_test3'));
- $module_list[] = 'dependency_test3';
- sort($module_list);
- $this->assertModuleList($module_list, t('After adding a module'));
-
-
- db_update('system')
- ->fields(array('weight' => 20))
- ->condition('name', 'dependency_test3')
- ->condition('type', 'module')
- ->execute();
-
- module_list(TRUE);
-
- unset($module_list[array_search('dependency_test3', $module_list)]);
- $module_list[] = 'dependency_test3';
- $this->assertModuleList($module_list, t('After changing weights'));
-
-
- $fixed_list = array(
- 'system' => array('filename' => backdrop_get_path('module', 'system')),
- 'menu' => array('filename' => backdrop_get_path('module', 'menu')),
- );
- module_list(FALSE, FALSE, FALSE, $fixed_list);
- $new_module_list = array_combine(array_keys($fixed_list), array_keys($fixed_list));
- $this->assertModuleList($new_module_list, t('When using a fixed list'));
-
-
- module_list(TRUE);
- $this->assertModuleList($module_list, t('After reset'));
- }
-
-
- * Assert that module_list() return the expected values.
- *
- * @param $expected_values
- * The expected values, sorted by weight and module name.
- */
- protected function assertModuleList(Array $expected_values, $condition) {
- $expected_values = array_combine($expected_values, $expected_values);
- $this->assertEqual($expected_values, module_list(), format_string('@condition: module_list() returns correct results', array('@condition' => $condition)));
- ksort($expected_values);
- $this->assertIdentical($expected_values, module_list(FALSE, FALSE, TRUE), format_string('@condition: module_list() returns correctly sorted results', array('@condition' => $condition)));
- }
-
-
- * Test module_implements() caching.
- */
- function testModuleImplements() {
-
- cache('bootstrap')->delete('module_implements');
- $this->assertFalse(cache('bootstrap')->get('module_implements'), 'The module implements cache is empty.');
- $this->backdropGet('');
-
-
-
- sleep(3);
-
- $this->assertTrue(cache('bootstrap')->get('module_implements'), 'The module implements cache is populated after requesting a page.');
-
-
- $this->user = $this->backdropCreateUser();
- $this->backdropLogin($this->user);
- cache('bootstrap')->delete('module_implements');
- $this->backdropGet('');
- sleep(3);
- $this->assertTrue(cache('bootstrap')->get('module_implements'), 'The module implements cache is populated after requesting a page.');
-
-
-
-
- module_enable(array('module_test'));
-
- module_load_include('inc', 'module_test', 'module_test.file');
- $modules = module_implements('test_hook');
- $static = &backdrop_static('module_implements');
- $this->assertTrue(in_array('module_test', $modules), 'Hook found.');
- $this->assertEqual($static['test_hook']['module_test'], 'file', 'Include file detected.');
- }
-
-
- * Test that module_invoke() can load a hook defined in hook_hook_info().
- */
- function testModuleInvoke() {
- module_enable(array('module_test'), FALSE);
- $this->resetAll();
- $this->backdropGet('module-test/hook-dynamic-loading-invoke');
- $this->assertText('success!', 'module_invoke() dynamically loads a hook defined in hook_hook_info().');
- }
-
-
- * Test that module_invoke_all() can load a hook defined in hook_hook_info().
- */
- function testModuleInvokeAll() {
- module_enable(array('module_test'), FALSE);
- $this->resetAll();
- $this->backdropGet('module-test/hook-dynamic-loading-invoke-all');
- $this->assertText('success!', 'module_invoke_all() dynamically loads a hook defined in hook_hook_info().');
- }
-
-
- * Test dependency resolution.
- */
- function testDependencyResolution() {
-
-
-
- module_enable(array('module_test'), FALSE);
- module_disable(array('dependency_test2'), FALSE);
- $this->assertTrue(module_exists('module_test'), 'Test module is enabled.');
- $this->assertFalse(module_exists('dependency_test3'), 'Third module is disabled.');
- $this->assertFalse(module_exists('dependency_test2'), 'Second module is disabled.');
- $this->assertFalse(module_exists('dependency_test1'), 'First module is disabled.');
-
-
-
-
- state_set('dependency_test', 'missing dependency');
- backdrop_static_reset('system_rebuild_module_data');
- $result = module_enable(array('dependency_test3'));
- $this->assertFalse($result, 'module_enable() returns FALSE if dependencies are missing.');
- $this->assertFalse(module_exists('dependency_test3'), 'module_enable() aborts if dependencies are missing.');
-
-
-
- state_set('dependency_test', 'dependency');
- backdrop_static_reset('system_rebuild_module_data');
- $result = module_enable(array('dependency_test3'));
- $this->assertTrue($result, 'module_enable() returns the correct value.');
-
- $this->assertTrue(module_exists('dependency_test2') && module_exists('dependency_test1'), 'Dependency chain was installed by module_enable().');
-
- $this->assertTrue(module_exists('dependency_test3'), 'Module installation with unlisted dependencies succeeded.');
-
- $this->assertEqual(state_get('test_module_enable_order', array()), array('dependency_test1', 'dependency_test2', 'dependency_test3'), 'Modules were enabled in the correct order by module_enable().');
-
-
-
- module_disable(array('dependency_test1'));
- $this->assertTrue(!module_exists('dependency_test3') && !module_exists('dependency_test2'), 'Dependency chain was disabled by module_disable().');
- $this->assertFalse(module_exists('dependency_test1'), 'Disabling a module with unlisted dependents succeeded.');
- $this->assertEqual(state_get('test_module_disable_order', array()), array('dependency_test3', 'dependency_test2', 'dependency_test1'), 'Modules were disabled in the correct order by module_disable().');
-
-
-
-
- $profile = backdrop_get_profile();
- $info = install_profile_info($profile);
- $this->assertTrue(in_array('comment', $info['dependencies']), 'Comment module is listed as a dependency of the installation profile.');
- $this->assertTrue(module_exists('comment'), 'Comment module is enabled.');
- module_disable(array('comment'));
- $this->assertFalse(module_exists('comment'), 'Comment module was disabled.');
- $disabled_modules = state_get('test_module_disable_order', array());
- $this->assertTrue(in_array('comment', $disabled_modules), 'Comment module is in the list of disabled modules.');
- $this->assertFalse(in_array($profile, $disabled_modules), 'The installation profile is not in the list of disabled modules.');
-
-
-
-
- $result = backdrop_uninstall_modules(array('dependency_test1'));
- $this->assertFalse($result, 'Calling backdrop_uninstall_modules() on a module whose dependents are not uninstalled fails.');
- foreach (array('dependency_test3', 'dependency_test2', 'dependency_test1') as $module) {
- $this->assertNotEqual(backdrop_get_installed_schema_version($module), SCHEMA_UNINSTALLED, format_string('The @module module was not uninstalled.', array('@module' => $module)));
- }
-
-
-
-
- $result = backdrop_uninstall_modules(array('dependency_test2', 'dependency_test1', 'dependency_test3'));
- $this->assertTrue($result, t('backdrop_uninstall_modules() returns the correct value.'));
- foreach (array('dependency_test3', 'dependency_test2', 'dependency_test1') as $module) {
- $this->assertEqual(backdrop_get_installed_schema_version($module), SCHEMA_UNINSTALLED, format_string('The @module module was uninstalled.', array('@module' => $module)));
- }
- $this->assertEqual(state_get('test_module_uninstall_order', array()), array('dependency_test3', 'dependency_test2', 'dependency_test1'), 'Modules were uninstalled in the correct order by backdrop_uninstall_modules().');
-
-
-
- $result = backdrop_uninstall_modules(array('comment'));
- $this->assertTrue($result, 'backdrop_uninstall_modules() returns the correct value.');
- $this->assertEqual(backdrop_get_installed_schema_version('comment'), SCHEMA_UNINSTALLED, 'Comment module was uninstalled.');
- $uninstalled_modules = state_get('test_module_uninstall_order', array());
- $this->assertTrue(in_array('comment', $uninstalled_modules), 'Comment module is in the list of uninstalled modules.');
- $this->assertFalse(in_array($profile, $uninstalled_modules), 'The installation profile is not in the list of uninstalled modules.');
-
-
-
-
-
- state_set('dependency_test', 'version dependency');
- backdrop_static_reset('system_rebuild_module_data');
- $result = module_enable(array('dependency_test3'));
- $this->assertTrue($result, 'module_enable() returns the correct value.');
-
- $this->assertTrue(module_exists('dependency_test2') && module_exists('dependency_test1'), 'Dependency chain was installed by module_enable().');
-
- $this->assertTrue(module_exists('dependency_test3'), 'Module installation with version dependencies succeeded.');
-
- $enable_order = state_get('test_module_enable_order', array());
- $first_position = array_search('dependency_test1', $enable_order);
- $second_position = array_search('dependency_test2', $enable_order);
- $third_position = array_search('dependency_test3', $enable_order);
- $first_before_second = $first_position !== FALSE && $second_position !== FALSE && $first_position < $second_position;
- $second_before_third = $second_position !== FALSE && $third_position !== FALSE && $second_position < $third_position;
- $this->assertTrue($first_before_second && $second_before_third, 'Modules were enabled in the correct order by module_enable().');
- }
- }
-
- * Unit tests for module installation.
- */
- class ModuleInstallTestCase extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('module_test');
- }
-
-
- * Test that calls to backdrop_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, backdrop_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 Backdrop is installing and enabling
- * them.
- */
- function testBackdropWriteRecord() {
-
-
- $data = db_query("SELECT data FROM {module_test}")->fetchCol();
- $this->assertTrue(in_array('Data inserted in hook_install()', $data), 'Data inserted using backdrop_write_record() in hook_install() is correctly saved.');
- $this->assertTrue(in_array('Data inserted in hook_enable()', $data), 'Data inserted using backdrop_write_record() in hook_enable() is correctly saved.');
- }
- }
-
- * Unit tests for module uninstallation and related hooks.
- */
- class ModuleUninstallTestCase extends BackdropWebTestCase {
-
-
- * @var User
- */
- protected $admin_user;
-
- function setUp() {
- parent::setUp('module_test', 'user');
- }
-
-
- * Tests the hook_modules_uninstalled() of the user module.
- */
- function testUserPermsUninstalled() {
-
- user_role_grant_permissions(BACKDROP_AUTHENTICATED_ROLE, array('module_test perm'));
- $role = user_role_load(BACKDROP_AUTHENTICATED_ROLE);
- $this->assertTrue(in_array('module_test perm', $role->permissions), 'Test module permission enabled.');
-
-
-
- module_disable(array('module_test'));
- backdrop_uninstall_modules(array('module_test'));
-
-
- $role = user_role_load(BACKDROP_AUTHENTICATED_ROLE);
- $this->assertFalse(in_array('module_test perm', $role->permissions), 'Test module permission removed upon uninstall.');
- }
-
-
- * Tests that uninstalling locale does not cause problems.
- */
- public function testUninstallLocale() {
-
- module_enable(array('language', 'locale'), FALSE);
- $language = (object) array(
- 'langcode' => 'fr',
- 'name' => 'French',
- 'direction' => LANGUAGE_LTR,
- 'enabled' => TRUE,
- );
- language_save($language);
- config_set('system.core', 'language_default', 'fr');
- $prefixes = array(
- 'en' => 'en',
- 'fr' => '',
- );
- config_set('locale.settings', 'language_negotiation_url_prefixes', $prefixes);
-
- global $language;
- $language = language_load('fr');
-
- $this->admin_user = $this->backdropCreateUser(array(
- 'administer modules',
- ));
- $this->backdropLogin($this->admin_user);
-
- module_disable(array('locale'), FALSE);
- $edit = array(
- 'uninstall[locale]' => TRUE,
- );
-
-
- $this->backdropPost('admin/modules/uninstall', $edit, t('Uninstall'));
- $this->backdropPost(NULL, array(), t('Uninstall'));
-
- $this->backdropGet('admin/modules/uninstall');
- $this->assertNoText('Uninstall Locale module', 'Module Locale has been successfully uninstalled');
- }
-
-
-
- * Test that uninstalling Comment and then immediately adding a node works.
- */
- public function testUninstallCommentAddNode() {
- $admin_user = $this->backdropCreateUser(array(
- 'administer modules',
- 'bypass node access',
- ));
- $this->backdropLogin($admin_user);
- module_disable(array('comment'), FALSE);
- $edit = array(
- 'uninstall[comment]' => TRUE,
- );
- $this->backdropPost('admin/modules/uninstall', $edit, t('Uninstall'));
- $this->backdropPost(NULL, array(), t('Uninstall'));
-
- $node_edit = array(
- 'title' => 'This is a new node',
- );
- $this->backdropGet('node/add/post');
-
-
- $this->backdropPost(NULL, $node_edit, t('Save'));
- $this->assertNoText('SQLSTATE[42S22]: Column not found', 'No sql error when saving node');
- $this->assertRaw('<h1 class="page-title">This is a new node</h1>', 'Node has been saved after uninstalling the comment module');
- }
- }
-
- * Unit tests for module_implements_alter().
- */
- class ModuleImplementsAlterTestCase extends BackdropWebTestCase {
-
- * Tests hook_module_implements_alter() adding an implementation.
- */
- function testModuleImplementsAlter() {
- module_enable(array('module_test'), FALSE);
- $this->assertTrue(module_exists('module_test'), 'Test module is enabled.');
-
-
- $this->assertTrue(function_exists('module_test_permission'),
- 'The file module_test.module was successfully included.');
-
- $modules = module_implements('permission');
- $this->assertTrue(in_array('module_test', $modules), 'module_test implements hook_permission.');
-
- $modules = module_implements('module_implements_alter');
- $this->assertTrue(in_array('module_test', $modules), 'module_test implements hook_module_implements_alter().');
-
-
- $this->assertFalse(function_exists('module_test_altered_test_hook'),
- 'The file module_test.implementations.inc is not included yet.');
-
-
-
- $this->assertTrue(in_array('module_test', module_implements('altered_test_hook')),
- 'module_test implements hook_altered_test_hook().');
-
-
- $this->assertTrue(function_exists('module_test_altered_test_hook'),
- 'The file module_test.implementations.inc was included.');
- }
-
- }