Commit 36fcf91c authored by catch's avatar catch
Browse files

Issue #1891516 by sun, Berdir, chx: Remove $install parameter from...

Issue #1891516 by sun, Berdir, chx: Remove $install parameter from DrupalUnitTestBase::enableModules(), encourage to create individual schema tables only.
parent faaddf2f
...@@ -26,7 +26,7 @@ class BlockStorageUnitTest extends DrupalUnitTestBase { ...@@ -26,7 +26,7 @@ class BlockStorageUnitTest extends DrupalUnitTestBase {
* *
* @var array * @var array
*/ */
public static $modules = array('block_test'); public static $modules = array('block', 'block_test');
/** /**
* The block storage controller. * The block storage controller.
...@@ -46,7 +46,6 @@ public static function getInfo() { ...@@ -46,7 +46,6 @@ public static function getInfo() {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->enableModules(array('block'));
$this->controller = $this->container->get('plugin.manager.entity')->getStorageController('block'); $this->controller = $this->container->get('plugin.manager.entity')->getStorageController('block');
} }
...@@ -196,7 +195,7 @@ public function testDefaultBlocks() { ...@@ -196,7 +195,7 @@ public function testDefaultBlocks() {
$this->assertTrue(empty($entities), 'There are no blocks initially.'); $this->assertTrue(empty($entities), 'There are no blocks initially.');
// Install the block_test.module, so that its default config is installed. // Install the block_test.module, so that its default config is installed.
$this->enableModules(array('block_test')); $this->installConfig(array('block_test'));
$entities = $this->controller->load(); $entities = $this->controller->load();
$entity = reset($entities); $entity = reset($entities);
......
...@@ -163,6 +163,7 @@ function testNameValidation() { ...@@ -163,6 +163,7 @@ function testNameValidation() {
$message = 'Expected ConfigNameException was thrown when attempting to install invalid configuration.'; $message = 'Expected ConfigNameException was thrown when attempting to install invalid configuration.';
try { try {
$this->enableModules(array('config_test_invalid_name')); $this->enableModules(array('config_test_invalid_name'));
$this->installConfig(array('config_test_invalid_name'));
$this->fail($message); $this->fail($message);
} }
catch (ConfigNameException $e) { catch (ConfigNameException $e) {
......
...@@ -57,6 +57,7 @@ function testModuleInstallation() { ...@@ -57,6 +57,7 @@ function testModuleInstallation() {
// Install the test module. // Install the test module.
$this->enableModules(array('config_test')); $this->enableModules(array('config_test'));
$this->installConfig(array('config_test'));
// Verify that default module config exists. // Verify that default module config exists.
$config = config($default_config); $config = config($default_config);
......
...@@ -20,7 +20,7 @@ class EditTestBase extends DrupalUnitTestBase { ...@@ -20,7 +20,7 @@ class EditTestBase extends DrupalUnitTestBase {
* *
* @var array * @var array
*/ */
public static $modules = array('system', 'entity', 'field_test', 'field', 'number', 'text', 'edit'); public static $modules = array('system', 'entity', 'field', 'field_sql_storage', 'field_test', 'number', 'text', 'edit');
/** /**
* Sets the default field storage backend for fields created during tests. * Sets the default field storage backend for fields created during tests.
...@@ -29,7 +29,8 @@ function setUp() { ...@@ -29,7 +29,8 @@ function setUp() {
parent::setUp(); parent::setUp();
$this->installSchema('system', 'variable'); $this->installSchema('system', 'variable');
$this->enableModules(array('field', 'field_sql_storage', 'field_test')); $this->installSchema('field', array('field_config', 'field_config_instance'));
$this->installSchema('field_test', 'test_entity');
// Set default storage backend. // Set default storage backend.
variable_set('field_storage_default', $this->default_storage); variable_set('field_storage_default', $this->default_storage);
......
...@@ -56,6 +56,8 @@ public static function getInfo() { ...@@ -56,6 +56,8 @@ public static function getInfo() {
function setUp() { function setUp() {
parent::setUp(); parent::setUp();
$this->installSchema('field_test', 'test_entity_revision');
$this->editorManager = new EditorManager(); $this->editorManager = new EditorManager();
$this->accessChecker = new MockEditEntityFieldAccessCheck(); $this->accessChecker = new MockEditEntityFieldAccessCheck();
$this->editorSelector = new EditorSelector($this->editorManager); $this->editorSelector = new EditorSelector($this->editorManager);
......
...@@ -27,7 +27,7 @@ public static function getInfo() { ...@@ -27,7 +27,7 @@ public static function getInfo() {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->enableModules(array('field')); $this->installSchema('field', array('field_config', 'field_config_instance'));
} }
/** /**
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
*/ */
class FilterDefaultConfigTest extends DrupalUnitTestBase { class FilterDefaultConfigTest extends DrupalUnitTestBase {
public static $modules = array('system', 'user', 'filter'); public static $modules = array('system', 'user', 'filter', 'filter_test');
public static function getInfo() { public static function getInfo() {
return array( return array(
...@@ -26,18 +26,20 @@ public static function getInfo() { ...@@ -26,18 +26,20 @@ public static function getInfo() {
function setUp() { function setUp() {
parent::setUp(); parent::setUp();
$this->enableModules(array('user'));
// filter_permission() calls into url() to output a link in the description. // filter_permission() calls into url() to output a link in the description.
$this->installSchema('system', 'url_alias'); $this->installSchema('system', 'url_alias');
$this->installSchema('user', array('users_roles', 'role_permission'));
// Install filter_test module, which ships with custom default format.
$this->installConfig(array('user', 'filter_test'));
} }
/** /**
* Tests installation of default formats. * Tests installation of default formats.
*/ */
function testInstallation() { function testInstallation() {
// Install filter_test module, which ships with custom default format.
$this->enableModules(array('filter_test'));
// Verify that the format was installed correctly. // Verify that the format was installed correctly.
$format = filter_format_load('filter_test'); $format = filter_format_load('filter_test');
$this->assertTrue($format); $this->assertTrue($format);
...@@ -87,9 +89,6 @@ function testInstallation() { ...@@ -87,9 +89,6 @@ function testInstallation() {
* Tests that changes to FilterFormat::$roles do not have an effect. * Tests that changes to FilterFormat::$roles do not have an effect.
*/ */
function testUpdateRoles() { function testUpdateRoles() {
// Install filter_test module, which ships with custom default format.
$this->enableModules(array('filter_test'));
// Verify role permissions declared in default config. // Verify role permissions declared in default config.
$format = filter_format_load('filter_test'); $format = filter_format_load('filter_test');
$this->assertEqual(array_keys(filter_get_roles_by_format($format)), array( $this->assertEqual(array_keys(filter_get_roles_by_format($format)), array(
......
...@@ -15,12 +15,22 @@ ...@@ -15,12 +15,22 @@
*/ */
abstract class LanguageTestBase extends ViewUnitTestBase { abstract class LanguageTestBase extends ViewUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system', 'language');
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->installSchema('language', 'language');
$this->installSchema('system', 'variable');
$this->enableModules(array('system', 'language'));
// Create another language beside English. // Create English and another language beside English.
$language = new Language(array('langcode' => 'en'));
language_save($language);
$language = new Language(array('langcode' => 'xx-lolspeak', 'name' => 'Lolspeak')); $language = new Language(array('langcode' => 'xx-lolspeak', 'name' => 'Lolspeak'));
language_save($language); language_save($language);
} }
......
...@@ -61,6 +61,14 @@ abstract class DrupalUnitTestBase extends UnitTestBase { ...@@ -61,6 +61,14 @@ abstract class DrupalUnitTestBase extends UnitTestBase {
*/ */
protected $keyValueFactory; protected $keyValueFactory;
/**
* Overrides \Drupal\simpletest\UnitTestBase::__construct().
*/
function __construct($test_id = NULL) {
parent::__construct($test_id);
$this->skipClasses[__CLASS__] = TRUE;
}
/** /**
* Sets up Drupal unit test environment. * Sets up Drupal unit test environment.
* *
...@@ -89,7 +97,7 @@ protected function setUp() { ...@@ -89,7 +97,7 @@ protected function setUp() {
// Bootstrap the kernel. // Bootstrap the kernel.
// No need to dump it; this test runs in-memory. // No need to dump it; this test runs in-memory.
$this->kernel = new DrupalKernel('testing', TRUE, drupal_classloader(), FALSE); $this->kernel = new DrupalKernel('unit_testing', TRUE, drupal_classloader(), FALSE);
$this->kernel->boot(); $this->kernel->boot();
// Collect and set a fixed module list. // Collect and set a fixed module list.
...@@ -97,22 +105,37 @@ protected function setUp() { ...@@ -97,22 +105,37 @@ protected function setUp() {
$modules = array(); $modules = array();
while ($class) { while ($class) {
if (property_exists($class, 'modules')) { if (property_exists($class, 'modules')) {
$modules = array_merge($modules, $class::$modules); // Only add the modules, if the $modules property was not inherited.
$rp = new \ReflectionProperty($class, 'modules');
if ($rp->class == $class) {
$modules[$class] = $class::$modules;
}
} }
$class = get_parent_class($class); $class = get_parent_class($class);
} }
$this->enableModules(array_unique($modules), FALSE); // Modules have been collected in reverse class hierarchy order; modules
// defined by base classes should be sorted first. Then, merge the results
// together.
$modules = array_reverse($modules);
$modules = call_user_func_array('array_merge_recursive', $modules);
$this->enableModules($modules, FALSE);
}
protected function tearDown() {
$this->kernel->shutdown();
parent::tearDown();
} }
/** /**
* Sets up the base service container for this test. * Sets up the base service container for this test.
* *
* Extend this method in your test to register additional service overrides * Extend this method in your test to register additional service overrides
* that need to persist a DrupalKernel reboot. This method is only called once * that need to persist a DrupalKernel reboot. This method is called whenever
* for each test. * the kernel is rebuilt.
* *
* @see DrupalUnitTestBase::setUp() * @see DrupalUnitTestBase::setUp()
* @see DrupalUnitTestBase::enableModules() * @see DrupalUnitTestBase::enableModules()
* @see DrupalUnitTestBase::disableModules()
*/ */
public function containerBuild($container) { public function containerBuild($container) {
global $conf; global $conf;
...@@ -147,16 +170,34 @@ public function containerBuild($container) { ...@@ -147,16 +170,34 @@ public function containerBuild($container) {
} }
/** /**
* Installs a specific table from a module schema definition. * Installs default configuration for a given list of modules.
* *
* Use this to install a particular table from System module. * @param array $modules
* A list of modules for which to install default configuration.
*/
protected function installConfig(array $modules) {
foreach ($modules as $module) {
if (!$this->container->get('module_handler')->moduleExists($module)) {
throw new \RuntimeException(format_string("'@module' module is not enabled.", array(
'@module' => $module,
)));
}
config_install_default_config('module', $module);
}
$this->pass(format_string('Installed default config: %modules.', array(
'%modules' => implode(', ', $modules),
)));
}
/**
* Installs a specific table from a module schema definition.
* *
* @param string $module * @param string $module
* The name of the module that defines the table's schema. * The name of the module that defines the table's schema.
* @param string $table * @param string|array $tables
* The name of the table to install. * The name or an array of the names of the tables to install.
*/ */
protected function installSchema($module, $table) { protected function installSchema($module, $tables) {
// drupal_get_schema_unprocessed() is technically able to install a schema // drupal_get_schema_unprocessed() is technically able to install a schema
// of a non-enabled module, but its ability to load the module's .install // of a non-enabled module, but its ability to load the module's .install
// file depends on many other factors. To prevent differences in test // file depends on many other factors. To prevent differences in test
...@@ -167,45 +208,37 @@ protected function installSchema($module, $table) { ...@@ -167,45 +208,37 @@ protected function installSchema($module, $table) {
'@module' => $module, '@module' => $module,
))); )));
} }
$tables = (array) $tables;
foreach ($tables as $table) {
$schema = drupal_get_schema_unprocessed($module, $table); $schema = drupal_get_schema_unprocessed($module, $table);
if (empty($schema)) { if (empty($schema)) {
throw new \RuntimeException(format_string("Unable to retrieve '@module' module schema for '@table' table.", array( throw new \RuntimeException(format_string("Unknown '@table' table schema in '@module' module.", array(
'@module' => $module, '@module' => $module,
'@table' => $table, '@table' => $table,
))); )));
} }
Database::getConnection()->schema()->createTable($table, $schema); $this->container->get('database')->schema()->createTable($table, $schema);
}
// We need to refresh the schema cache, as any call to drupal_get_schema() // We need to refresh the schema cache, as any call to drupal_get_schema()
// would not know of/return the schema otherwise. // would not know of/return the schema otherwise.
// @todo Refactor Schema API to make this obsolete. // @todo Refactor Schema API to make this obsolete.
drupal_get_schema(NULL, TRUE); drupal_get_schema(NULL, TRUE);
$this->pass(format_string('Installed %module tables: %tables.', array(
'%tables' => '{' . implode('}, {', $tables) . '}',
'%module' => $module,
)));
} }
/** /**
* Enables modules for this test. * Enables modules for this test.
* *
* Callbacks invoked by module_enable() may need to access information
* provided by info hooks of the new modules already. However, module_enable()
* enables the new modules in the system.module configuration only, but that
* has no effect, since we are operating with a fixed module list.
*
* @param array $modules * @param array $modules
* A list of modules to enable. Dependencies are not resolved; i.e., * A list of modules to enable. Dependencies are not resolved; i.e.,
* multiple modules have to be specified with dependent modules first. * multiple modules have to be specified with dependent modules first.
* @param bool $install * The new modules are only added to the active module list and loaded.
* (optional) Whether to install the list of modules via module_enable().
* Defaults to TRUE. If FALSE, the new modules are only added to the fixed
* module list and loaded.
*
* @todo Remove $install argument and replace all callers that do not pass
* FALSE with module_enable().
*/ */
protected function enableModules(array $modules, $install = TRUE) { protected function enableModules(array $modules) {
if ($install) { // Set the list of modules in the extension handler.
module_enable($modules, FALSE);
}
// Explicitly set the list of modules in the extension handler.
else {
$module_handler = $this->container->get('module_handler'); $module_handler = $this->container->get('module_handler');
$module_filenames = $module_handler->getModuleList(); $module_filenames = $module_handler->getModuleList();
foreach ($modules as $module) { foreach ($modules as $module) {
...@@ -213,14 +246,48 @@ protected function enableModules(array $modules, $install = TRUE) { ...@@ -213,14 +246,48 @@ protected function enableModules(array $modules, $install = TRUE) {
} }
$module_handler->setModuleList($module_filenames); $module_handler->setModuleList($module_filenames);
$module_handler->resetImplementations(); $module_handler->resetImplementations();
// Update the kernel to make their services available.
$this->kernel->updateModules($module_filenames, $module_filenames); $this->kernel->updateModules($module_filenames, $module_filenames);
// Ensure isLoaded() is TRUE in order to make theme() work.
// Note that the kernel has rebuilt the container; this $module_handler is
// no longer the $module_handler instance from above.
$module_handler = $this->container->get('module_handler');
$module_handler->reload();
$this->pass(format_string('Enabled modules: %modules.', array(
'%modules' => implode(', ', $modules),
)));
}
/**
* Disables modules for this test.
*
* @param array $modules
* A list of modules to disable. Dependencies are not resolved; i.e.,
* multiple modules have to be specified with dependent modules first.
* Code of previously active modules is still loaded. The modules are only
* removed from the active module list.
*/
protected function disableModules(array $modules) {
// Unset the list of modules in the extension handler.
$module_handler = $this->container->get('module_handler');
$module_filenames = $module_handler->getModuleList();
foreach ($modules as $module) {
unset($module_filenames[$module]);
} }
// Regardless of loaded or installed, ensure isLoaded() is TRUE in order to $module_handler->setModuleList($module_filenames);
// make theme() work. $module_handler->resetImplementations();
// Update the kernel to remove their services.
$this->kernel->updateModules($module_filenames, $module_filenames);
// Ensure isLoaded() is TRUE in order to make theme() work.
// Note that the kernel has rebuilt the container; this $module_handler is // Note that the kernel has rebuilt the container; this $module_handler is
// no longer the $module_handler instance from above. // no longer the $module_handler instance from above.
$module_handler = $this->container->get('module_handler'); $module_handler = $this->container->get('module_handler');
$module_handler->reload(); $module_handler->reload();
$this->pass(format_string('Disabled modules: %modules.', array(
'%modules' => implode(', ', $modules),
)));
} }
} }
...@@ -60,7 +60,7 @@ function testEnableModulesLoad() { ...@@ -60,7 +60,7 @@ function testEnableModulesLoad() {
$this->assertFalse(in_array($module, $list), "{$module}_permission() in module_implements() not found."); $this->assertFalse(in_array($module, $list), "{$module}_permission() in module_implements() not found.");
// Enable the module. // Enable the module.
$this->enableModules(array($module), FALSE); $this->enableModules(array($module));
// Verify that the module exists. // Verify that the module exists.
$this->assertTrue(module_exists($module), "$module module found."); $this->assertTrue(module_exists($module), "$module module found.");
...@@ -77,10 +77,6 @@ function testEnableModulesInstall() { ...@@ -77,10 +77,6 @@ function testEnableModulesInstall() {
$module = 'node'; $module = 'node';
$table = 'node'; $table = 'node';
// @todo Remove after configuration system conversion.
$this->enableModules(array('system'), FALSE);
$this->installSchema('system', 'variable');
// Verify that the module does not exist yet. // Verify that the module does not exist yet.
$this->assertFalse(module_exists($module), "$module module not found."); $this->assertFalse(module_exists($module), "$module module not found.");
$list = array_keys(drupal_container()->get('module_handler')->getModuleList()); $list = array_keys(drupal_container()->get('module_handler')->getModuleList());
...@@ -92,8 +88,8 @@ function testEnableModulesInstall() { ...@@ -92,8 +88,8 @@ function testEnableModulesInstall() {
$schema = drupal_get_schema($table); $schema = drupal_get_schema($table);
$this->assertFalse($schema, "'$table' table schema not found."); $this->assertFalse($schema, "'$table' table schema not found.");
// Enable the module. // Install the module.
$this->enableModules(array($module)); module_enable(array($module));
// Verify that the enabled module exists. // Verify that the enabled module exists.
$this->assertTrue(module_exists($module), "$module module found."); $this->assertTrue(module_exists($module), "$module module found.");
...@@ -107,24 +103,6 @@ function testEnableModulesInstall() { ...@@ -107,24 +103,6 @@ function testEnableModulesInstall() {
$this->assertTrue($schema, "'$table' table schema found."); $this->assertTrue($schema, "'$table' table schema found.");
} }
/**
* Tests installing of multiple modules via enableModules().
*
* Regression test: Each passed module has to be enabled and installed on its
* own, in the same way as module_enable() enables only one module after the
* other.
*/
function testEnableModulesInstallMultiple() {
// Field retrieves entity type plugins, and EntityTypeManager calls into
// hook_entity_info_alter(). If both modules would be first enabled together
// instead of each on its own, then Node module's alter implementation
// would be called and this simply blows up. To further complicate matters,
// additionally install Comment module, whose entity bundles depend on node
// types.
$this->enableModules(array('field', 'node', 'comment'));
$this->pass('Comment module was installed.');
}
/** /**
* Tests installing modules via enableModules() with DepedencyInjection services. * Tests installing modules via enableModules() with DepedencyInjection services.
*/ */
...@@ -133,6 +111,9 @@ function testEnableModulesInstallContainer() { ...@@ -133,6 +111,9 @@ function testEnableModulesInstallContainer() {
// @todo field_sql_storage and field should technically not be necessary // @todo field_sql_storage and field should technically not be necessary
// for an entity query. // for an entity query.
$this->enableModules(array('field_sql_storage', 'field', 'node')); $this->enableModules(array('field_sql_storage', 'field', 'node'));
$this->installSchema('field', array('field_config', 'field_config_instance'));
$this->installSchema('node', array('node_type', 'node'));
// Perform an entity query against node. // Perform an entity query against node.
$query = entity_query('node');