diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php index 2568c24..460f497 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php @@ -7,12 +7,12 @@ namespace Drupal\config\Tests; -use Drupal\simpletest\UnitTestBase; +use Drupal\simpletest\DrupalUnitTestBase; /** * Tests CRUD operations on configuration objects. */ -class ConfigCRUDTest extends UnitTestBase { +class ConfigCRUDTest extends DrupalUnitTestBase { public static function getInfo() { return array( 'name' => 'CRUD operations', diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigFileContentTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigFileContentTest.php index 25a3f3d..dfc0d66 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigFileContentTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigFileContentTest.php @@ -8,12 +8,12 @@ namespace Drupal\config\Tests; use Drupal\Core\Config\FileStorage; -use Drupal\simpletest\UnitTestBase; +use Drupal\simpletest\DrupalUnitTestBase; /** * Tests reading and writing file contents. */ -class ConfigFileContentTest extends UnitTestBase { +class ConfigFileContentTest extends DrupalUnitTestBase { public static function getInfo() { return array( 'name' => 'File content', diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigImportTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigImportTest.php index f0a0e78..61220de 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigImportTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigImportTest.php @@ -7,12 +7,12 @@ namespace Drupal\config\Tests; -use Drupal\simpletest\UnitTestBase; +use Drupal\simpletest\DrupalUnitTestBase; /** * Tests importing configuration from files into active store. */ -class ConfigImportTest extends UnitTestBase { +class ConfigImportTest extends DrupalUnitTestBase { /** * Modules to enable. diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigInstallTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigInstallTest.php index 8a3f3a3..531c496 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigInstallTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigInstallTest.php @@ -7,12 +7,12 @@ namespace Drupal\config\Tests; -use Drupal\simpletest\UnitTestBase; +use Drupal\simpletest\DrupalUnitTestBase; /** * Tests installation of configuration objects in installation functionality. */ -class ConfigInstallTest extends UnitTestBase { +class ConfigInstallTest extends DrupalUnitTestBase { public static function getInfo() { return array( 'name' => 'Installation functionality', diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigOverrideTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigOverrideTest.php index 7cc032a..de7784f 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigOverrideTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigOverrideTest.php @@ -7,12 +7,12 @@ namespace Drupal\config\Tests; -use Drupal\simpletest\UnitTestBase; +use Drupal\simpletest\DrupalUnitTestBase; /** * Tests configuration overrides via $conf in settings.php. */ -class ConfigOverrideTest extends UnitTestBase { +class ConfigOverrideTest extends DrupalUnitTestBase { /** * Modules to enable. diff --git a/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php b/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php index f794500..064146c 100644 --- a/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php +++ b/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php @@ -7,7 +7,7 @@ namespace Drupal\config\Tests\Storage; -use Drupal\simpletest\UnitTestBase; +use Drupal\simpletest\DrupalUnitTestBase; /** * Base class for testing storage controller operations. @@ -21,7 +21,7 @@ * supply the necessary helper methods to interact with the raw/native storage * directly. */ -abstract class ConfigStorageTestBase extends UnitTestBase { +abstract class ConfigStorageTestBase extends DrupalUnitTestBase { /** * Tests storage controller CRUD operations. diff --git a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php similarity index 62% copy from core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php copy to core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php index 1926b96..68485b6 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php @@ -2,22 +2,31 @@ /** * @file - * Definition of Drupal\simpletest\UnitTestBase. + * Definition of Drupal\simpletest\DrupalUnitTestBase. */ namespace Drupal\simpletest; use Drupal\Core\Database\Database; -use Drupal\Core\Database\ConnectionNotDefinedException; /** - * Test case for Drupal unit tests. + * Base test case class for Drupal unit tests. * - * These tests can not access the database nor files. Calling any Drupal - * function that needs the database will throw exceptions. These include - * watchdog(), module_implements(), module_invoke_all() etc. + * Tests extending this base class can access files and the database, but the + * entire environment is initially empty. Drupal runs in a minimal mocked + * environment, comparable to the one in the installer or update.php. + * + * The module/hook system is functional and operates on a fixed module list. + * Additional modules needed in a test may be loaded and added to the fixed + * module list. + * + * @see DrupalUnitTestBase::enableModules() + * + * By installing the {system} schema, modules may be fully installed, too. + * + * @see DrupalUnitTestBase::installSchema() */ -abstract class UnitTestBase extends TestBase { +abstract class DrupalUnitTestBase extends UnitTestBase { /** * Fixed module list being used by this test. @@ -32,14 +41,6 @@ private $moduleList = array(); /** - * Constructor for UnitTestBase. - */ - function __construct($test_id = NULL) { - parent::__construct($test_id); - $this->skipClasses[__CLASS__] = TRUE; - } - - /** * Sets up unit test environment. * * Unlike Drupal\simpletest\WebTestBase::setUp(), UnitTestBase::setUp() does not @@ -48,20 +49,7 @@ function __construct($test_id = NULL) { * method. */ protected function setUp() { - global $conf; - - // Create the database prefix for this test. - $this->prepareDatabasePrefix(); - - // Prepare the environment for running tests. - $this->prepareEnvironment(); - if (!$this->setupEnvironment) { - return FALSE; - } - - // Reset all statics and variables to perform tests in a clean environment. - $conf = array(); - drupal_static_reset(); + parent::setUp(); // Ensure that the module list is initially empty. $this->moduleList = array(); @@ -75,27 +63,6 @@ protected function setUp() { $class = get_parent_class($class); } $this->enableModules($modules, FALSE); - - // Provide a minimal, partially mocked environment for unit tests. - $conf['file_public_path'] = $this->public_files_directory; - $conf['lock_backend'] = 'Drupal\Core\Lock\NullLockBackend'; - $conf['cache_classes'] = array('cache' => 'Drupal\Core\Cache\NullBackend'); - $this->container->register('config.storage', 'Drupal\Core\Config\FileStorage') - ->addArgument($this->configDirectories[CONFIG_ACTIVE_DIRECTORY]); - - // Change the database prefix. - // All static variables need to be reset before the database prefix is - // changed, since Drupal\Core\Utility\CacheArray implementations attempt to - // write back to persistent caches when they are destructed. - $this->changeDatabasePrefix(); - if (!$this->setupDatabasePrefix) { - return FALSE; - } - - // Set user agent to be consistent with WebTestBase. - $_SERVER['HTTP_USER_AGENT'] = $this->databasePrefix; - - $this->setup = TRUE; } /** diff --git a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php index 1926b96..9a5d658 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php @@ -11,7 +11,7 @@ use Drupal\Core\Database\ConnectionNotDefinedException; /** - * Test case for Drupal unit tests. + * Base test case class for unit tests. * * These tests can not access the database nor files. Calling any Drupal * function that needs the database will throw exceptions. These include @@ -20,18 +20,6 @@ abstract class UnitTestBase extends TestBase { /** - * Fixed module list being used by this test. - * - * @var array - * An associative array containing the required data for the $fixed_list - * argument of module_list(). - * - * @see UnitTestBase::setUp() - * @see UnitTestBase::enableModules() - */ - private $moduleList = array(); - - /** * Constructor for UnitTestBase. */ function __construct($test_id = NULL) { @@ -63,18 +51,8 @@ protected function setUp() { $conf = array(); drupal_static_reset(); - // Ensure that the module list is initially empty. - $this->moduleList = array(); - // Collect and set a fixed module list. - $class = get_class($this); - $modules = array(); - while ($class) { - if (property_exists($class, 'modules')) { - $modules = array_merge($modules, $class::$modules); - } - $class = get_parent_class($class); - } - $this->enableModules($modules, FALSE); + // Enforce an empty module list. + module_list(NULL, array()); // Provide a minimal, partially mocked environment for unit tests. $conf['file_public_path'] = $this->public_files_directory; @@ -97,59 +75,4 @@ protected function setUp() { $this->setup = TRUE; } - - /** - * Installs a specific table from a module schema definition. - * - * @param string $module - * The name of the module that defines the table's schema. - * @param string $table - * The name of the table to install. - */ - protected function installSchema($module, $table) { - require_once DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$module.install"; - $function = $module . '_schema'; - $schema = $function(); - Database::getConnection()->schema()->createTable($table, $schema[$table]); - } - - /** - * 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 {system} only, but that has no effect, since we - * are operating with a fixed module list. - * - * The {system} table is required to install new modules. Install it via: - * @code - * $this->installSchema('system', 'system'); - * @endcode - * - * @param array $modules - * A list of modules to enable. - * @param bool $install - * (optional) Whether to install the list of modules via module_enable(). - * Defaults to TRUE. Requires the {system} table to exist. If FALSE, the new - * modules are only added to the fixed module list and loaded. - * - * @todo Remove this method as soon as there is an Extensions service - * implementation that is able to manage a fixed module list. - */ - protected function enableModules(array $modules, $install = TRUE) { - // Set the modules in the fixed module_list(). - foreach ($modules as $module) { - $this->moduleList[$module] = drupal_get_path('module', $module); - } - module_list(NULL, $this->moduleList); - - // Call module_enable() to enable (install) the new modules. - if ($install) { - module_enable($modules); - } - // Otherwise, only ensure that the new modules are loaded. - else { - module_load_all(FALSE, TRUE); - } - } }