View source
<?php
namespace Drupal\service_container\Tests;
class ModuleHandlerTest extends ServiceContainerIntegrationTestBase {
public static function getInfo() {
return array(
'name' => 'ModuleHandler',
'description' => 'Tests the module handler.',
'group' => 'service_container',
);
}
function testModuleList() {
$profile_info = install_profile_info('testing', 'en');
$module_list = $profile_info['dependencies'];
$module_list[] = 'testing';
$module_list[] = 'service_container';
sort($module_list);
$this
->assertModuleList($module_list, 'Testing profile');
$this
->moduleInstaller()
->install(array(
'ban',
));
$module_list[] = 'ban';
sort($module_list);
$this
->assertModuleList($module_list, 'After adding a module');
unset($module_list[array_search('ban', $module_list)]);
$module_list[] = 'ban';
$this
->assertModuleList($module_list, 'After changing weights');
$fixed_list = array(
'system' => 'core/modules/system/system.module',
'menu' => 'core/modules/menu/menu.module',
);
$this
->moduleHandler()
->setModuleList($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'));
}
protected function assertModuleList(array $expected_values, $condition) {
$expected_values = array_values(array_unique($expected_values));
$enabled_modules = array_keys($this->container
->get('module_handler')
->getModuleList());
$enabled_modules = sort($enabled_modules);
$this
->assertEqual($expected_values, $enabled_modules, format_string('@condition: extension handler returns correct results', array(
'@condition' => $condition,
)));
}
function testModuleImplements() {
$this
->moduleInstaller()
->install(array(
'module_test',
));
$this
->moduleHandler()
->loadInclude('module_test', 'inc', 'module_test.file');
$modules = $this
->moduleHandler()
->getImplementations('test_hook');
$this
->assertTrue(in_array('module_test', $modules), 'Hook found.');
}
function testModuleInvoke() {
$this
->moduleInstaller()
->install(array(
'module_test',
), FALSE);
$result = $this
->moduleHandler()
->invoke('module_test', 'test_hook');
$this
->assertEqual('success!', $result['module_test']);
}
function testModuleInvokeAll() {
$this
->moduleInstaller()
->install(array(
'module_test',
), FALSE);
$result = $this
->moduleHandler()
->invokeAll('test_hook');
$this
->assertEqual('success!', $result['module_test']);
}
function testDependencyResolution() {
return;
$this
->moduleInstaller()
->install(array(
'module_test',
));
$this
->assertTrue($this
->moduleHandler()
->moduleExists('module_test'), 'Test module is enabled.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists('color'), 'Color module is disabled.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists('config'), 'Config module is disabled.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists('help'), 'Help module is disabled.');
\Drupal::state()
->set('module_test.dependency', 'missing dependency');
drupal_static_reset('system_rebuild_module_data');
$result = $this
->moduleInstaller()
->install(array(
'color',
));
$this
->assertFalse($result, 'ModuleHandler::install() returns FALSE if dependencies are missing.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists('color'), 'ModuleHandler::install() aborts if dependencies are missing.');
\Drupal::state()
->set('module_test.dependency', 'dependency');
drupal_static_reset('system_rebuild_module_data');
$result = $this
->moduleInstaller()
->install(array(
'color',
));
$this
->assertTrue($result, 'ModuleHandler::install() returns the correct value.');
$this
->assertTrue($this
->moduleHandler()
->moduleExists('config') && $this
->moduleHandler()
->moduleExists('help'), 'Dependency chain was installed.');
$this
->assertTrue($this
->moduleHandler()
->moduleExists('color'), 'Module installation with dependencies succeeded.');
$module_order = \Drupal::state()
->get('module_test.install_order') ?: array();
$this
->assertEqual($module_order, array(
'help',
'config',
'color',
));
$result = $this
->moduleInstaller()
->uninstall(array(
'config',
'help',
'color',
));
$this
->assertTrue($result, 'ModuleHandler::uninstall() returned TRUE.');
foreach (array(
'color',
'config',
'help',
) as $module) {
$this
->assertEqual(drupal_get_installed_schema_version($module), SCHEMA_UNINSTALLED, "{$module} module was uninstalled.");
}
$uninstalled_modules = \Drupal::state()
->get('module_test.uninstall_order') ?: array();
$this
->assertEqual($uninstalled_modules, array(
'color',
'config',
'help',
), 'Modules were uninstalled in the correct order.');
\Drupal::state()
->set('module_test.dependency', 'version dependency');
drupal_static_reset('system_rebuild_module_data');
$result = $this
->moduleInstaller()
->install(array(
'color',
));
$this
->assertTrue($result, 'ModuleHandler::install() returns the correct value.');
$this
->assertTrue($this
->moduleHandler()
->moduleExists('config') && $this
->moduleHandler()
->moduleExists('help'), 'Dependency chain was installed.');
$this
->assertTrue($this
->moduleHandler()
->moduleExists('color'), 'Module installation with version dependencies succeeded.');
$enable_order = \Drupal::state()
->get('module_test.install_order') ?: array();
$this
->assertIdentical($enable_order, array(
'help',
'config',
'color',
));
}
function testUninstallProfileDependency() {
return;
$profile = 'minimal';
$dependency = 'dblog';
$this
->settingsSet('install_profile', $profile);
$this
->enableModules(array(
'module_test',
$profile,
));
drupal_static_reset('system_rebuild_module_data');
$data = system_rebuild_module_data();
$this
->assertTrue(isset($data[$profile]->requires[$dependency]));
$this
->moduleInstaller()
->install(array(
$dependency,
));
$this
->assertTrue($this
->moduleHandler()
->moduleExists($dependency));
$result = $this
->moduleInstaller()
->uninstall(array(
$dependency,
));
$this
->assertTrue($result, 'ModuleHandler::uninstall() returns TRUE.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists($dependency));
$this
->assertEqual(drupal_get_installed_schema_version($dependency), SCHEMA_UNINSTALLED, "{$dependency} module was uninstalled.");
$uninstalled_modules = \Drupal::state()
->get('module_test.uninstall_order') ?: array();
$this
->assertTrue(in_array($dependency, $uninstalled_modules), "{$dependency} 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.');
}
function testModuleMetaData() {
$modules = system_rebuild_module_data();
$this
->assertTrue(!empty($modules['system']->info['mtime']), 'The system.info.yml file modification time field is present.');
$test_mtime = !empty($modules['system']->info['mtime']) ? $modules['system']->info['mtime'] : 0;
$this
->assertTrue(is_numeric($test_mtime) && $test_mtime > 0, 'The system.info.yml file modification time field contains a timestamp.');
}
function testThemeMetaData() {
$themes = system_rebuild_theme_data();
$this
->assertTrue(!empty($themes['bartik']->info['mtime']), 'The bartik.info.yml file modification time field is present.');
$test_mtime = !empty($themes['bartik']->info['mtime']) ? $themes['bartik']->info['mtime'] : 0;
$this
->assertTrue(is_numeric($test_mtime) && $test_mtime > 0, 'The bartik.info.yml file modification time field contains a timestamp.');
}
protected function moduleHandler() {
return $this->container
->get('module_handler');
}
protected function moduleInstaller() {
return $this->container
->get('module_installer');
}
}