From 36fcf91cb57dc799790f4e67bd3cc3b6e96ed70c Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Thu, 21 Feb 2013 10:22:42 +0000 Subject: [PATCH] Issue #1891516 by sun, Berdir, chx: Remove $install parameter from DrupalUnitTestBase::enableModules(), encourage to create individual schema tables only. --- .../block/Tests/BlockStorageUnitTest.php | 5 +- .../Drupal/config/Tests/ConfigCRUDTest.php | 1 + .../Drupal/config/Tests/ConfigInstallTest.php | 1 + .../lib/Drupal/edit/Tests/EditTestBase.php | 5 +- .../edit/Tests/MetadataGeneratorTest.php | 2 + .../Drupal/entity/Tests/EntityDisplayTest.php | 2 +- .../filter/Tests/FilterDefaultConfigTest.php | 15 +- .../language/Tests/Views/LanguageTestBase.php | 14 +- .../Drupal/simpletest/DrupalUnitTestBase.php | 157 +++++++++++++----- .../Tests/DrupalUnitTestBaseTest.php | 85 ++++++---- .../Tests/Database/DatabaseTestBase.php | 12 +- .../Tests/Database/SelectComplexTest.php | 7 +- .../system/Tests/Entity/EntityUriTest.php | 11 +- .../views/Tests/Handler/AreaTextTest.php | 4 +- .../views/Tests/Handler/FieldCounterTest.php | 10 +- .../views/Tests/Handler/FieldUnitTest.php | 7 +- .../views/Tests/Handler/FieldUrlTest.php | 8 +- .../Tests/Handler/FilterEqualityTest.php | 5 +- .../Tests/Handler/FilterInOperatorTest.php | 5 +- .../views/Tests/Handler/FilterNumericTest.php | 5 +- .../views/Tests/Handler/FilterStringTest.php | 5 +- .../views/Tests/Handler/HandlerAliasTest.php | 4 +- .../lib/Drupal/views/Tests/ModuleTest.php | 2 +- .../views/Tests/Plugin/PluginUnitTestBase.php | 17 ++ .../Tests/Plugin/RelationshipJoinTestBase.php | 19 ++- .../views/Tests/Plugin/StyleMappingTest.php | 8 +- .../Drupal/views/Tests/TokenReplaceTest.php | 7 +- .../Drupal/views/Tests/ViewExecutableTest.php | 11 +- .../Drupal/views/Tests/ViewStorageTest.php | 1 + .../Drupal/views/Tests/ViewUnitTestBase.php | 52 ++++-- 30 files changed, 338 insertions(+), 149 deletions(-) create mode 100644 core/modules/views/lib/Drupal/views/Tests/Plugin/PluginUnitTestBase.php diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php index 596b88b6261e..d0c833ae5ff3 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php @@ -26,7 +26,7 @@ class BlockStorageUnitTest extends DrupalUnitTestBase { * * @var array */ - public static $modules = array('block_test'); + public static $modules = array('block', 'block_test'); /** * The block storage controller. @@ -46,7 +46,6 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $this->enableModules(array('block')); $this->controller = $this->container->get('plugin.manager.entity')->getStorageController('block'); } @@ -196,7 +195,7 @@ public function testDefaultBlocks() { $this->assertTrue(empty($entities), 'There are no blocks initially.'); // 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(); $entity = reset($entities); diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php index a04a928cb02c..e363577b5a50 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php @@ -163,6 +163,7 @@ function testNameValidation() { $message = 'Expected ConfigNameException was thrown when attempting to install invalid configuration.'; try { $this->enableModules(array('config_test_invalid_name')); + $this->installConfig(array('config_test_invalid_name')); $this->fail($message); } catch (ConfigNameException $e) { diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigInstallTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigInstallTest.php index 42a6fec66ff0..d406964fd411 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigInstallTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigInstallTest.php @@ -57,6 +57,7 @@ function testModuleInstallation() { // Install the test module. $this->enableModules(array('config_test')); + $this->installConfig(array('config_test')); // Verify that default module config exists. $config = config($default_config); diff --git a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php index 18d92d82583e..c3a91fbd49e1 100644 --- a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php +++ b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php @@ -20,7 +20,7 @@ class EditTestBase extends DrupalUnitTestBase { * * @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. @@ -29,7 +29,8 @@ function setUp() { parent::setUp(); $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. variable_set('field_storage_default', $this->default_storage); diff --git a/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php b/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php index a8cec27070f1..364e4e0dd2b0 100644 --- a/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php +++ b/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php @@ -56,6 +56,8 @@ public static function getInfo() { function setUp() { parent::setUp(); + $this->installSchema('field_test', 'test_entity_revision'); + $this->editorManager = new EditorManager(); $this->accessChecker = new MockEditEntityFieldAccessCheck(); $this->editorSelector = new EditorSelector($this->editorManager); diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php index 6d213f632e2a..3396f893d110 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php @@ -27,7 +27,7 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $this->enableModules(array('field')); + $this->installSchema('field', array('field_config', 'field_config_instance')); } /** diff --git a/core/modules/filter/lib/Drupal/filter/Tests/FilterDefaultConfigTest.php b/core/modules/filter/lib/Drupal/filter/Tests/FilterDefaultConfigTest.php index fd37fb8462f0..85b5feffc255 100644 --- a/core/modules/filter/lib/Drupal/filter/Tests/FilterDefaultConfigTest.php +++ b/core/modules/filter/lib/Drupal/filter/Tests/FilterDefaultConfigTest.php @@ -14,7 +14,7 @@ */ class FilterDefaultConfigTest extends DrupalUnitTestBase { - public static $modules = array('system', 'user', 'filter'); + public static $modules = array('system', 'user', 'filter', 'filter_test'); public static function getInfo() { return array( @@ -26,18 +26,20 @@ public static function getInfo() { function setUp() { parent::setUp(); - $this->enableModules(array('user')); + // filter_permission() calls into url() to output a link in the description. $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. */ function testInstallation() { - // Install filter_test module, which ships with custom default format. - $this->enableModules(array('filter_test')); - // Verify that the format was installed correctly. $format = filter_format_load('filter_test'); $this->assertTrue($format); @@ -87,9 +89,6 @@ function testInstallation() { * Tests that changes to FilterFormat::$roles do not have an effect. */ function testUpdateRoles() { - // Install filter_test module, which ships with custom default format. - $this->enableModules(array('filter_test')); - // Verify role permissions declared in default config. $format = filter_format_load('filter_test'); $this->assertEqual(array_keys(filter_get_roles_by_format($format)), array( diff --git a/core/modules/language/lib/Drupal/language/Tests/Views/LanguageTestBase.php b/core/modules/language/lib/Drupal/language/Tests/Views/LanguageTestBase.php index e84844b90044..9057a5a88809 100644 --- a/core/modules/language/lib/Drupal/language/Tests/Views/LanguageTestBase.php +++ b/core/modules/language/lib/Drupal/language/Tests/Views/LanguageTestBase.php @@ -15,12 +15,22 @@ */ abstract class LanguageTestBase extends ViewUnitTestBase { + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('system', 'language'); + protected function 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_save($language); } diff --git a/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php index 4531c9f65aa1..6dbf439c64bc 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php @@ -61,6 +61,14 @@ abstract class DrupalUnitTestBase extends UnitTestBase { */ 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. * @@ -89,7 +97,7 @@ protected function setUp() { // Bootstrap the kernel. // 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(); // Collect and set a fixed module list. @@ -97,22 +105,37 @@ protected function setUp() { $modules = array(); while ($class) { 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); } - $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. * * Extend this method in your test to register additional service overrides - * that need to persist a DrupalKernel reboot. This method is only called once - * for each test. + * that need to persist a DrupalKernel reboot. This method is called whenever + * the kernel is rebuilt. * * @see DrupalUnitTestBase::setUp() * @see DrupalUnitTestBase::enableModules() + * @see DrupalUnitTestBase::disableModules() */ public function containerBuild($container) { global $conf; @@ -137,7 +160,7 @@ public function containerBuild($container) { // away with a simple container holding the absolute bare minimum. When // a kernel is overridden then there's no need to re-register the keyvalue // service but when a test is happy with the superminimal container put - // together here, it still might a keyvalue storage for anything (for + // together here, it still might a keyvalue storage for anything (for // eg. module_enable) using state() -- that's why a memory service was // added in the first place. $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 * The name of the module that defines the table's schema. - * @param string $table - * The name of the table to install. + * @param string|array $tables + * 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 // 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 @@ -167,60 +208,86 @@ protected function installSchema($module, $table) { '@module' => $module, ))); } - $schema = drupal_get_schema_unprocessed($module, $table); - if (empty($schema)) { - throw new \RuntimeException(format_string("Unable to retrieve '@module' module schema for '@table' table.", array( - '@module' => $module, - '@table' => $table, - ))); + $tables = (array) $tables; + foreach ($tables as $table) { + $schema = drupal_get_schema_unprocessed($module, $table); + if (empty($schema)) { + throw new \RuntimeException(format_string("Unknown '@table' table schema in '@module' module.", array( + '@module' => $module, + '@table' => $table, + ))); + } + $this->container->get('database')->schema()->createTable($table, $schema); } - Database::getConnection()->schema()->createTable($table, $schema); // We need to refresh the schema cache, as any call to drupal_get_schema() // would not know of/return the schema otherwise. // @todo Refactor Schema API to make this obsolete. drupal_get_schema(NULL, TRUE); + $this->pass(format_string('Installed %module tables: %tables.', array( + '%tables' => '{' . implode('}, {', $tables) . '}', + '%module' => $module, + ))); } /** * 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 * A list of modules to enable. Dependencies are not resolved; i.e., * multiple modules have to be specified with dependent modules first. - * @param bool $install - * (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(). + * The new modules are only added to the active module list and loaded. */ - protected function enableModules(array $modules, $install = TRUE) { - if ($install) { - module_enable($modules, FALSE); + protected function enableModules(array $modules) { + // Set the list of modules in the extension handler. + $module_handler = $this->container->get('module_handler'); + $module_filenames = $module_handler->getModuleList(); + foreach ($modules as $module) { + $module_filenames[$module] = drupal_get_filename('module', $module); } - // Explicitly set the list of modules in the extension handler. - else { - $module_handler = $this->container->get('module_handler'); - $module_filenames = $module_handler->getModuleList(); - foreach ($modules as $module) { - $module_filenames[$module] = drupal_get_filename('module', $module); - } - $module_handler->setModuleList($module_filenames); - $module_handler->resetImplementations(); - $this->kernel->updateModules($module_filenames, $module_filenames); + $module_handler->setModuleList($module_filenames); + $module_handler->resetImplementations(); + // Update the kernel to make their services available. + $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 - // make theme() work. + $module_handler->setModuleList($module_filenames); + $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 // no longer the $module_handler instance from above. $module_handler = $this->container->get('module_handler'); $module_handler->reload(); + $this->pass(format_string('Disabled modules: %modules.', array( + '%modules' => implode(', ', $modules), + ))); } } diff --git a/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php b/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php index 242338efc533..3aeab60495e0 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php @@ -60,7 +60,7 @@ function testEnableModulesLoad() { $this->assertFalse(in_array($module, $list), "{$module}_permission() in module_implements() not found."); // Enable the module. - $this->enableModules(array($module), FALSE); + $this->enableModules(array($module)); // Verify that the module exists. $this->assertTrue(module_exists($module), "$module module found."); @@ -77,10 +77,6 @@ function testEnableModulesInstall() { $module = '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. $this->assertFalse(module_exists($module), "$module module not found."); $list = array_keys(drupal_container()->get('module_handler')->getModuleList()); @@ -92,8 +88,8 @@ function testEnableModulesInstall() { $schema = drupal_get_schema($table); $this->assertFalse($schema, "'$table' table schema not found."); - // Enable the module. - $this->enableModules(array($module)); + // Install the module. + module_enable(array($module)); // Verify that the enabled module exists. $this->assertTrue(module_exists($module), "$module module found."); @@ -107,24 +103,6 @@ function testEnableModulesInstall() { $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. */ @@ -133,6 +111,9 @@ function testEnableModulesInstallContainer() { // @todo field_sql_storage and field should technically not be necessary // for an entity query. $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. $query = entity_query('node'); // Disable node access checks, since User module is not enabled. @@ -158,6 +139,19 @@ function testInstallSchema() { $schema = drupal_get_schema($table); $this->assertTrue($schema, "'$table' table schema found."); + // Verify that a unknown table from an enabled module throws an error. + $table = 'unknown_entity_test_table'; + try { + $this->installSchema($module, $table); + $this->fail('Exception for non-retrievable schema found.'); + } + catch (\Exception $e) { + $this->pass('Exception for non-retrievable schema found.'); + } + $this->assertFalse(db_table_exists($table), "'$table' database table not found."); + $schema = drupal_get_schema($table); + $this->assertFalse($schema, "'$table' table schema not found."); + // Verify that a table from a unknown module cannot be installed. $module = 'database_test'; $table = 'test'; @@ -173,7 +167,7 @@ function testInstallSchema() { $this->assertFalse($schema, "'$table' table schema not found."); // Verify that the same table can be installed after enabling the module. - $this->enableModules(array($module), FALSE); + $this->enableModules(array($module)); $this->installSchema($module, $table); $this->assertTrue(db_table_exists($table), "'$table' database table found."); $schema = drupal_get_schema($table); @@ -181,7 +175,30 @@ function testInstallSchema() { } /** - * Tests that the fixed module list is retained after enabling and installing modules. + * Tests expected behavior of installConfig(). + */ + function testInstallConfig() { + $module = 'user'; + + // Verify that default config can only be installed for enabled modules. + try { + $this->installConfig(array($module)); + $this->fail('Exception for non-enabled module found.'); + } + catch (\Exception $e) { + $this->pass('Exception for non-enabled module found.'); + } + $this->assertFalse($this->container->get('config.storage')->exists('user.settings')); + + // Verify that default config can be installed. + $this->enableModules(array('user')); + $this->installConfig(array('user')); + $this->assertTrue($this->container->get('config.storage')->exists('user.settings')); + $this->assertTrue(config('user.settings')->get('register')); + } + + /** + * Tests that the module list is retained after enabling/installing/disabling modules. */ function testEnableModulesFixedList() { // entity_test is loaded via $modules; its entity type should exist. @@ -189,7 +206,7 @@ function testEnableModulesFixedList() { $this->assertTrue(TRUE == entity_get_info('entity_test')); // Load some additional modules; entity_test should still exist. - $this->enableModules(array('entity', 'field', 'field_sql_storage', 'text', 'entity_test'), FALSE); + $this->enableModules(array('entity', 'field', 'field_sql_storage', 'text', 'entity_test')); $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE); $this->assertTrue(TRUE == entity_get_info('entity_test')); @@ -209,7 +226,7 @@ function testEnableModulesFixedList() { $this->assertTrue(TRUE == entity_get_info('entity_test')); // Reactivate the disabled module without enabling it. - $this->enableModules(array('field_test'), FALSE); + $this->enableModules(array('field_test')); // Create a field and an instance. $display = entity_create('entity_display', array( @@ -234,14 +251,18 @@ function testEnableModulesFixedList() { * Tests that theme() works right after loading a module. */ function testEnableModulesTheme() { - $element = array( + $original_element = $element = array( '#type' => 'container', '#markup' => 'Foo', '#attributes' => array(), ); - $this->enableModules(array('system'), FALSE); + $this->enableModules(array('system')); // theme() throws an exception if modules are not loaded yet. - drupal_render($element); + $this->assertTrue(drupal_render($element)); + + $element = $original_element; + $this->disableModules(array('entity_test')); + $this->assertTrue(drupal_render($element)); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/DatabaseTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Database/DatabaseTestBase.php index 6d3b45875c68..17d7ec0386c2 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Database/DatabaseTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Database/DatabaseTestBase.php @@ -17,9 +17,19 @@ */ abstract class DatabaseTestBase extends DrupalUnitTestBase { + public static $modules = array('database_test'); + function setUp() { parent::setUp(); - $this->enableModules(array('database_test')); + $this->installSchema('database_test', array( + 'test', + 'test_people', + 'test_one_blob', + 'test_two_blobs', + 'test_task', + 'test_null', + 'test_serialized', + )); self::addSampleData(); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php index 5ad4faa59a4f..c0f181a541b3 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php @@ -17,7 +17,7 @@ class SelectComplexTest extends DatabaseTestBase { * * @var array */ - public static $modules = array('node_access_test'); + public static $modules = array('node_access_test', 'field'); public static function getInfo() { return array( @@ -27,6 +27,11 @@ public static function getInfo() { ); } + function setUp() { + parent::setUp(); + $this->installSchema('field', array('field_config', 'field_config_instance')); + } + /** * Tests simple JOIN statements. */ diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUriTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUriTest.php index 66a00e54553e..487fc43d11ef 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUriTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUriTest.php @@ -19,7 +19,7 @@ class EntityUriTest extends DrupalUnitTestBase { * * @var array */ - public static $modules = array('field', 'field_sql_storage', 'system', 'text'); + public static $modules = array('field', 'field_sql_storage', 'system', 'text', 'entity_test'); public static function getInfo() { return array( @@ -32,12 +32,9 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $this->installSchema('system', 'variable'); - $this->installSchema('system', 'url_alias'); - $this->installSchema('field', 'field_config'); - $this->installSchema('field', 'field_config_instance'); - - $this->enableModules(array('entity_test')); + $this->installSchema('system', array('variable', 'url_alias')); + $this->installSchema('field', array('field_config', 'field_config_instance')); + $this->installSchema('entity_test', array('entity_test')); } /** diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTextTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTextTest.php index fadce300b769..525f3271fea6 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTextTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTextTest.php @@ -16,6 +16,8 @@ */ class AreaTextTest extends ViewUnitTestBase { + public static $modules = array('system', 'user', 'filter'); + /** * Views used by this test. * @@ -34,7 +36,7 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $this->enableModules(array('system', 'user', 'filter')); + $this->installConfig(array('system', 'filter')); } public function testAreaText() { diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldCounterTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldCounterTest.php index 8c1152421572..721b3c504d93 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldCounterTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldCounterTest.php @@ -14,6 +14,13 @@ */ class FieldCounterTest extends ViewUnitTestBase { + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('user'); + /** * Views used by this test. * @@ -31,8 +38,7 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - - $this->enableModules(array('user')); + $this->installSchema('user', 'role_permission'); } function testSimple() { diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php index 450fee802daa..afff1b33d081 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php @@ -17,6 +17,8 @@ */ class FieldUnitTest extends ViewUnitTestBase { + public static $modules = array('user'); + /** * Views used by this test. * @@ -36,10 +38,9 @@ public static function getInfo() { ); } - protected function setUp() { + public function setUp() { parent::setUp(); - - $this->enableModules(array('user')); + $this->installSchema('user', 'role_permission'); } /** diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUrlTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUrlTest.php index 1deac88c7f5a..75e638e96c10 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUrlTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUrlTest.php @@ -14,6 +14,8 @@ */ class FieldUrlTest extends ViewUnitTestBase { + public static $modules = array('system'); + /** * Views used by this test. * @@ -29,9 +31,9 @@ public static function getInfo() { ); } - public function setup() { - parent::setup(); - $this->enableModules(array('system')); + public function setUp() { + parent::setUp(); + $this->installSchema('system', 'url_alias'); } function viewsData() { diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FilterEqualityTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FilterEqualityTest.php index a93cbe128ae1..f0f4814a9609 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FilterEqualityTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FilterEqualityTest.php @@ -14,6 +14,8 @@ */ class FilterEqualityTest extends ViewUnitTestBase { + public static $modules = array('system'); + /** * Views used by this test. * @@ -36,8 +38,7 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $this->enableModules(array('system')); - $this->enableModules(array('menu_link')); + $this->installSchema('system', array('menu_router', 'variable')); } function viewsData() { diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FilterInOperatorTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FilterInOperatorTest.php index 6b74ddd738ef..fb154c15a88d 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FilterInOperatorTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FilterInOperatorTest.php @@ -14,6 +14,8 @@ */ class FilterInOperatorTest extends ViewUnitTestBase { + public static $modules = array('system'); + /** * Views used by this test. * @@ -37,8 +39,7 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $this->enableModules(array('system')); - $this->enableModules(array('menu_link')); + $this->installSchema('system', array('menu_router', 'variable')); } function viewsData() { diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FilterNumericTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FilterNumericTest.php index d0235546073f..e12c94ab8aa8 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FilterNumericTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FilterNumericTest.php @@ -14,6 +14,8 @@ */ class FilterNumericTest extends ViewUnitTestBase { + public static $modules = array('system'); + /** * Views used by this test. * @@ -37,8 +39,7 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $this->enableModules(array('system')); - $this->enableModules(array('menu_link')); + $this->installSchema('system', array('menu_router', 'variable')); } function viewsData() { diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FilterStringTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FilterStringTest.php index c5f27f60a2d3..57489bb293ec 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FilterStringTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FilterStringTest.php @@ -14,6 +14,8 @@ */ class FilterStringTest extends ViewUnitTestBase { + public static $modules = array('system'); + /** * Views used by this test. * @@ -36,8 +38,7 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $this->enableModules(array('system')); - $this->enableModules(array('menu_link')); + $this->installSchema('system', array('menu_router', 'variable')); } function viewsData() { diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/HandlerAliasTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/HandlerAliasTest.php index 793f2d8e956b..a120aad0e480 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/HandlerAliasTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/HandlerAliasTest.php @@ -14,6 +14,8 @@ */ class HandlerAliasTest extends ViewUnitTestBase { + public static $modules = array('user'); + /** * Views used by this test. * @@ -32,7 +34,7 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $this->enableModules(array('user')); + $this->installSchema('user', 'users'); } /** diff --git a/core/modules/views/lib/Drupal/views/Tests/ModuleTest.php b/core/modules/views/lib/Drupal/views/Tests/ModuleTest.php index dacdbabe2885..374de36d54c1 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ModuleTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/ModuleTest.php @@ -60,7 +60,7 @@ function testviews_get_handler() { * Tests the load wrapper/helper functions. */ public function testLoadFunctions() { - $this->enableModules(array('node'), FALSE); + $this->enableModules(array('node')); $controller = $this->container->get('plugin.manager.entity')->getStorageController('view'); // Test views_view_is_enabled/disabled. diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/PluginUnitTestBase.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/PluginUnitTestBase.php new file mode 100644 index 000000000000..96ebcda4b7b0 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/PluginUnitTestBase.php @@ -0,0 +1,17 @@ +<?php + +/** + * @file + * Contains \Drupal\views\Tests\Plugin\PluginUnitTestBase. + */ + +namespace Drupal\views\Tests\Plugin; + +use Drupal\views\Tests\ViewUnitTestBase; + +/** + * Base test class for views plugin unit tests. + */ +abstract class PluginUnitTestBase extends ViewUnitTestBase { + +} diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/RelationshipJoinTestBase.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/RelationshipJoinTestBase.php index 63a9f230f212..5d2b524d9239 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Plugin/RelationshipJoinTestBase.php +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/RelationshipJoinTestBase.php @@ -13,19 +13,28 @@ * @see \Drupal\views\Tests\Handler\JoinTest * @see \Drupal\views\Tests\Plugin\RelationshipTest */ -abstract class RelationshipJoinTestBase extends PluginTestBase { +abstract class RelationshipJoinTestBase extends PluginUnitTestBase { /** * Modules to enable. * * @var array */ - public static $modules = array('user'); + public static $modules = array('system', 'user', 'field'); - protected function setUp() { - parent::setUp(); + /** + * Overrides \Drupal\views\Tests\ViewUnitTestBase::setUpFixtures(). + */ + protected function setUpFixtures() { + $this->installSchema('user', array('users', 'users_roles', 'role_permission')); + $this->installSchema('field', array('field_config', 'field_config_instance')); + $this->installConfig(array('user')); + parent::setUpFixtures(); - $this->enableViewsTestModule(); + // Create a record for uid 1. + $this->installSchema('system', 'sequences'); + $this->root_user = entity_create('user', array('name' => $this->randomName())); + $this->root_user->save(); } /** diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleMappingTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleMappingTest.php index 0ecc5331baf7..f745a08e2935 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleMappingTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleMappingTest.php @@ -12,6 +12,8 @@ */ class StyleMappingTest extends StyleTestBase { + public static $modules = array('system'); + /** * Views used by this test. * @@ -27,12 +29,6 @@ public static function getInfo() { ); } - public function setUp() { - parent::setUp(); - - $this->enableModules(array('system')); - } - /** * Verifies that the fields were mapped correctly. */ diff --git a/core/modules/views/lib/Drupal/views/Tests/TokenReplaceTest.php b/core/modules/views/lib/Drupal/views/Tests/TokenReplaceTest.php index 632504fc3515..f795818f43ef 100644 --- a/core/modules/views/lib/Drupal/views/Tests/TokenReplaceTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/TokenReplaceTest.php @@ -12,6 +12,8 @@ */ class TokenReplaceTest extends ViewUnitTestBase { + public static $modules = array('system'); + /** * Views used by this test. * @@ -27,10 +29,9 @@ public static function getInfo() { ); } - public function setUp() { + function setUp() { parent::setUp(); - - $this->enableModules(array('system')); + $this->installSchema('system', 'url_alias'); } /** diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewExecutableTest.php b/core/modules/views/lib/Drupal/views/Tests/ViewExecutableTest.php index ea3a87cf6d13..7afe70102fae 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewExecutableTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewExecutableTest.php @@ -23,6 +23,8 @@ */ class ViewExecutableTest extends ViewUnitTestBase { + public static $modules = array('system', 'node', 'comment', 'user', 'filter'); + /** * Views used by this test. * @@ -75,10 +77,13 @@ public static function getInfo() { ); } - protected function setUp() { - parent::setUp(); + protected function setUpFixtures() { + $this->installSchema('user', array('users', 'role_permission')); + $this->installSchema('node', array('node_type', 'node')); + $this->installSchema('comment', array('comment', 'node_comment_statistics')); + parent::setUpFixtures(); - $this->enableModules(array('system', 'node', 'comment', 'user', 'filter')); + $this->installConfig(array('filter')); } /** diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php b/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php index 91f7b31228dd..8336c758d34c 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php @@ -192,6 +192,7 @@ protected function displayTests() { protected function displayMethodTests() { // Enable the system module so l() can work using url_alias table. $this->enableModules(array('system')); + $this->installSchema('system', 'url_alias'); $config['display'] = array( 'page_1' => array( diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php b/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php index 334b5dadcc08..80b659cdfa76 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php @@ -24,14 +24,34 @@ */ abstract class ViewUnitTestBase extends DrupalUnitTestBase { + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('views', 'views_test_config', 'views_test_data'); + protected function setUp() { parent::setUp(); + $this->setUpFixtures(); + } + + /** + * Sets up the configuration and schema of views and views_test_data modules. + * + * Because the schema of views_test_data.module is dependent on the test + * using it, it cannot be enabled normally. + */ + protected function setUpFixtures() { // Define the schema and views data variable before enabling the test module. state()->set('views_test_data_schema', $this->schemaDefinition()); state()->set('views_test_data_views_data', $this->viewsData()); - $this->enableModules(array('views', 'views_test_config', 'views_test_data')); + $this->installConfig(array('views', 'views_test_config', 'views_test_data')); + foreach ($this->schemaDefinition() as $table => $schema) { + $this->installSchema('views_test_data', $table); + } // Load the test dataset. $data_set = $this->dataSet(); @@ -66,8 +86,8 @@ protected function setUp() { * @return bool * TRUE if the assertion succeeded, or FALSE otherwise. */ - protected function assertIdenticalResultset($view, $expected_result, $column_map = array(), $message = 'Identical result set') { - return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertIdentical'); + protected function assertIdenticalResultset($view, $expected_result, $column_map = array(), $message = NULL) { + return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, 'assertIdentical', $message); } /** @@ -89,8 +109,8 @@ protected function assertIdenticalResultset($view, $expected_result, $column_map * @return bool * TRUE if the assertion succeeded, or FALSE otherwise. */ - protected function assertNotIdenticalResultset($view, $expected_result, $column_map = array(), $message = 'Identical result set') { - return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, 'assertNotIdentical'); + protected function assertNotIdenticalResultset($view, $expected_result, $column_map = array(), $message = NULL) { + return $this->assertIdenticalResultsetHelper($view, $expected_result, $column_map, 'assertNotIdentical', $message); } /** @@ -106,11 +126,11 @@ protected function assertNotIdenticalResultset($view, $expected_result, $column_ * @param array $column_map * An associative array mapping the columns of the result set * from the view (as keys) and the expected result set (as values). - * @param string $message - * The message to display with the assertion. * @param string $assert_method * The TestBase assertion method to use (either 'assertIdentical' or * 'assertNotIdentical'). + * @param string $message + * (optional) The message to display with the assertion. * * @return bool * TRUE if the assertion succeeded, or FALSE otherwise. @@ -118,7 +138,7 @@ protected function assertNotIdenticalResultset($view, $expected_result, $column_ * @see \Drupal\views\Tests\ViewTestBase::assertIdenticalResultset() * @see \Drupal\views\Tests\ViewTestBase::assertNotIdenticalResultset() */ - protected function assertIdenticalResultsetHelper($view, $expected_result, $column_map, $message, $assert_method) { + protected function assertIdenticalResultsetHelper($view, $expected_result, $column_map, $assert_method, $message = NULL) { // Convert $view->result to an array of arrays. $result = array(); foreach ($view->result as $key => $value) { @@ -140,13 +160,24 @@ protected function assertIdenticalResultsetHelper($view, $expected_result, $colu $expected_result[$key] = $row; } + $this->verbose('<pre style="white-space: pre-wrap;">' + . "\n\nQuery:\n" . $view->build_info['query'] + . "\n\nQuery arguments:\n" . var_export($view->build_info['query_args'], TRUE) + . "\n\nActual result:\n" . var_export($result, TRUE) + . "\n\nExpected result:\n" . var_export($expected_result, TRUE)); + // Reset the numbering of the arrays. $result = array_values($result); $expected_result = array_values($expected_result); - $this->verbose('<pre>Returned data set: ' . print_r($result, TRUE) . "\n\nExpected: ". print_r($expected_result, TRUE)); - // Do the actual comparison. + if (!isset($message)) { + $not = (strpos($assert_method, 'Not') ? 'not' : ''); + $message = format_string("Actual result <pre>\n@actual\n</pre> is $not identical to expected <pre>\n@expected\n</pre>", array( + '@actual' => var_export($result, TRUE), + '@expected' => var_export($expected_result, TRUE), + )); + } return $this->$assert_method($result, $expected_result, $message); } @@ -188,7 +219,6 @@ protected function executeView($view, $args = array()) { $view->setDisplay(); $view->preExecute($args); $view->execute(); - $this->verbose('<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>'); } /** -- GitLab