Issue #2759853 by klausi, dawehner: Create ConfigTestTrait to share code between PHPUnit and Simpletest

8.2.x
xjm 2016-07-26 20:42:44 -05:00
parent 473d830858
commit a1d45d3c8c
4 changed files with 68 additions and 99 deletions

View File

@ -7,13 +7,11 @@ use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Database\Database; 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\Site\Settings;
use Drupal\Core\StreamWrapper\PublicStream; use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\Test\TestDatabase; use Drupal\Core\Test\TestDatabase;
use Drupal\Core\Utility\Error; use Drupal\Core\Utility\Error;
use Drupal\Tests\ConfigTestTrait;
use Drupal\Tests\RandomGeneratorTrait; use Drupal\Tests\RandomGeneratorTrait;
use Drupal\Tests\SessionTestTrait; use Drupal\Tests\SessionTestTrait;
@ -27,6 +25,11 @@ abstract class TestBase {
use SessionTestTrait; use SessionTestTrait;
use RandomGeneratorTrait; use RandomGeneratorTrait;
use AssertHelperTrait; 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. * The test run ID.
@ -1537,51 +1540,6 @@ abstract class TestBase {
chmod($path, 0700); 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. * Configuration accessor for tests. Returns non-overridden configuration.
* *

View File

@ -7,9 +7,6 @@ use Drupal\Component\FileCache\FileCache;
use Drupal\Component\FileCache\FileCacheFactory; use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SafeMarkup; 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\Database\Database;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderInterface; use Drupal\Core\DependencyInjection\ServiceProviderInterface;
@ -20,6 +17,7 @@ use Drupal\Core\Language\Language;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\simpletest\AssertContentTrait; use Drupal\simpletest\AssertContentTrait;
use Drupal\simpletest\AssertHelperTrait; use Drupal\simpletest\AssertHelperTrait;
use Drupal\Tests\ConfigTestTrait;
use Drupal\Tests\RandomGeneratorTrait; use Drupal\Tests\RandomGeneratorTrait;
use Drupal\simpletest\TestServiceProvider; use Drupal\simpletest\TestServiceProvider;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
@ -55,6 +53,7 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
use AssertContentTrait; use AssertContentTrait;
use AssertHelperTrait; use AssertHelperTrait;
use RandomGeneratorTrait; use RandomGeneratorTrait;
use ConfigTestTrait;
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -1010,54 +1009,6 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
new Settings($settings); 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. * Stops test execution.
*/ */

View File

@ -56,6 +56,7 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
use ContentTypeCreationTrait { use ContentTypeCreationTrait {
createContentType as drupalCreateContentType; createContentType as drupalCreateContentType;
} }
use ConfigTestTrait;
use UserCreationTrait { use UserCreationTrait {
createRole as drupalCreateRole; createRole as drupalCreateRole;
createUser as drupalCreateUser; createUser as drupalCreateUser;

View File

@ -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));
}
}
}