From a1d45d3c8c238bc8b51ac04eeee6a951d5efc735 Mon Sep 17 00:00:00 2001 From: xjm <xjm@65776.no-reply.drupal.org> Date: Tue, 26 Jul 2016 20:42:44 -0500 Subject: [PATCH] Issue #2759853 by klausi, dawehner: Create ConfigTestTrait to share code between PHPUnit and Simpletest --- core/modules/simpletest/src/TestBase.php | 54 ++--------------- .../Drupal/KernelTests/KernelTestBase.php | 53 +---------------- core/tests/Drupal/Tests/BrowserTestBase.php | 1 + core/tests/Drupal/Tests/ConfigTestTrait.php | 59 +++++++++++++++++++ 4 files changed, 68 insertions(+), 99 deletions(-) create mode 100644 core/tests/Drupal/Tests/ConfigTestTrait.php diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php index 6fa40a3bae0d..4258f8038812 100644 --- a/core/modules/simpletest/src/TestBase.php +++ b/core/modules/simpletest/src/TestBase.php @@ -7,13 +7,11 @@ use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Database\Database; -use Drupal\Core\Config\ConfigImporter; -use Drupal\Core\Config\StorageComparer; -use Drupal\Core\Config\StorageInterface; use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PublicStream; use Drupal\Core\Test\TestDatabase; use Drupal\Core\Utility\Error; +use Drupal\Tests\ConfigTestTrait; use Drupal\Tests\RandomGeneratorTrait; use Drupal\Tests\SessionTestTrait; @@ -27,6 +25,11 @@ abstract class TestBase { use SessionTestTrait; use RandomGeneratorTrait; use AssertHelperTrait; + // For backwards compatibility switch the visbility of the methods to public. + use ConfigTestTrait { + configImporter as public; + copyConfig as public; + } /** * The test run ID. @@ -1537,51 +1540,6 @@ public static function filePreDeleteCallback($path) { chmod($path, 0700); } - /** - * Returns a ConfigImporter object to import test importing of configuration. - * - * @return \Drupal\Core\Config\ConfigImporter - * The ConfigImporter object. - */ - public function configImporter() { - if (!$this->configImporter) { - // Set up the ConfigImporter object for testing. - $storage_comparer = new StorageComparer( - $this->container->get('config.storage.sync'), - $this->container->get('config.storage'), - $this->container->get('config.manager') - ); - $this->configImporter = new ConfigImporter( - $storage_comparer, - $this->container->get('event_dispatcher'), - $this->container->get('config.manager'), - $this->container->get('lock'), - $this->container->get('config.typed'), - $this->container->get('module_handler'), - $this->container->get('module_installer'), - $this->container->get('theme_handler'), - $this->container->get('string_translation') - ); - } - // Always recalculate the changelist when called. - return $this->configImporter->reset(); - } - - /** - * Copies configuration objects from source storage to target storage. - * - * @param \Drupal\Core\Config\StorageInterface $source_storage - * The source config storage service. - * @param \Drupal\Core\Config\StorageInterface $target_storage - * The target config storage service. - */ - public function copyConfig(StorageInterface $source_storage, StorageInterface $target_storage) { - $target_storage->deleteAll(); - foreach ($source_storage->listAll() as $name) { - $target_storage->write($name, $source_storage->read($name)); - } - } - /** * Configuration accessor for tests. Returns non-overridden configuration. * diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index ebe6b87bd40e..4063d701ddda 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -7,9 +7,6 @@ use Drupal\Component\FileCache\FileCacheFactory; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\SafeMarkup; -use Drupal\Core\Config\ConfigImporter; -use Drupal\Core\Config\StorageComparer; -use Drupal\Core\Config\StorageInterface; use Drupal\Core\Database\Database; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ServiceProviderInterface; @@ -20,6 +17,7 @@ use Drupal\Core\Site\Settings; use Drupal\simpletest\AssertContentTrait; use Drupal\simpletest\AssertHelperTrait; +use Drupal\Tests\ConfigTestTrait; use Drupal\Tests\RandomGeneratorTrait; use Drupal\simpletest\TestServiceProvider; use Symfony\Component\DependencyInjection\Reference; @@ -55,6 +53,7 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser use AssertContentTrait; use AssertHelperTrait; use RandomGeneratorTrait; + use ConfigTestTrait; /** * {@inheritdoc} @@ -1010,54 +1009,6 @@ protected function setSetting($name, $value) { new Settings($settings); } - /** - * Returns a ConfigImporter object to import test configuration. - * - * @return \Drupal\Core\Config\ConfigImporter - * - * @todo Move into Config-specific test base class. - */ - protected function configImporter() { - if (!$this->configImporter) { - // Set up the ConfigImporter object for testing. - $storage_comparer = new StorageComparer( - $this->container->get('config.storage.sync'), - $this->container->get('config.storage'), - $this->container->get('config.manager') - ); - $this->configImporter = new ConfigImporter( - $storage_comparer, - $this->container->get('event_dispatcher'), - $this->container->get('config.manager'), - $this->container->get('lock'), - $this->container->get('config.typed'), - $this->container->get('module_handler'), - $this->container->get('module_installer'), - $this->container->get('theme_handler'), - $this->container->get('string_translation') - ); - } - // Always recalculate the changelist when called. - return $this->configImporter->reset(); - } - - /** - * Copies configuration objects from a source storage to a target storage. - * - * @param \Drupal\Core\Config\StorageInterface $source_storage - * The source config storage. - * @param \Drupal\Core\Config\StorageInterface $target_storage - * The target config storage. - * - * @todo Move into Config-specific test base class. - */ - protected function copyConfig(StorageInterface $source_storage, StorageInterface $target_storage) { - $target_storage->deleteAll(); - foreach ($source_storage->listAll() as $name) { - $target_storage->write($name, $source_storage->read($name)); - } - } - /** * Stops test execution. */ diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index ded28f2f7f5b..ce2721b785ae 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -56,6 +56,7 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase { use ContentTypeCreationTrait { createContentType as drupalCreateContentType; } + use ConfigTestTrait; use UserCreationTrait { createRole as drupalCreateRole; createUser as drupalCreateUser; diff --git a/core/tests/Drupal/Tests/ConfigTestTrait.php b/core/tests/Drupal/Tests/ConfigTestTrait.php new file mode 100644 index 000000000000..330ff470e29c --- /dev/null +++ b/core/tests/Drupal/Tests/ConfigTestTrait.php @@ -0,0 +1,59 @@ +<?php + +namespace Drupal\Tests; + +use Drupal\Core\Config\ConfigImporter; +use Drupal\Core\Config\StorageComparer; +use Drupal\Core\Config\StorageInterface; + +/** + * Provides helper methods to deal with config system objects in tests. + */ +trait ConfigTestTrait { + + /** + * Returns a ConfigImporter object to import test configuration. + * + * @return \Drupal\Core\Config\ConfigImporter + * The config importer object. + */ + protected function configImporter() { + if (!$this->configImporter) { + // Set up the ConfigImporter object for testing. + $storage_comparer = new StorageComparer( + $this->container->get('config.storage.sync'), + $this->container->get('config.storage'), + $this->container->get('config.manager') + ); + $this->configImporter = new ConfigImporter( + $storage_comparer, + $this->container->get('event_dispatcher'), + $this->container->get('config.manager'), + $this->container->get('lock'), + $this->container->get('config.typed'), + $this->container->get('module_handler'), + $this->container->get('module_installer'), + $this->container->get('theme_handler'), + $this->container->get('string_translation') + ); + } + // Always recalculate the changelist when called. + return $this->configImporter->reset(); + } + + /** + * Copies configuration objects from source storage to target storage. + * + * @param \Drupal\Core\Config\StorageInterface $source_storage + * The source config storage service. + * @param \Drupal\Core\Config\StorageInterface $target_storage + * The target config storage service. + */ + protected function copyConfig(StorageInterface $source_storage, StorageInterface $target_storage) { + $target_storage->deleteAll(); + foreach ($source_storage->listAll() as $name) { + $target_storage->write($name, $source_storage->read($name)); + } + } + +} -- GitLab