View source
<?php
namespace Drupal\config\Tests;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Uuid\Php;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\ConfigImporterException;
use Drupal\Core\Config\StorageComparer;
use Drupal\simpletest\KernelTestBase;
class ConfigImportRenameValidationTest extends KernelTestBase {
protected $configImporter;
public static $modules = [
'system',
'user',
'node',
'field',
'text',
'config_test',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installConfig(array(
'field',
));
$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
->createChangelist(), $this->container
->get('event_dispatcher'), $this->container
->get('config.manager'), $this->container
->get('lock.persistent'), $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'));
}
public function testRenameValidation() {
$test_entity_id = $this
->randomMachineName();
$test_entity = entity_create('config_test', array(
'id' => $test_entity_id,
'label' => $this
->randomMachineName(),
));
$test_entity
->save();
$uuid = $test_entity
->uuid();
$active = $this->container
->get('config.storage');
$sync = $this->container
->get('config.storage.sync');
$this
->copyConfig($active, $sync);
$test_entity
->delete();
$content_type = entity_create('node_type', array(
'type' => Unicode::strtolower($this
->randomMachineName(16)),
'name' => $this
->randomMachineName(),
'uuid' => $uuid,
));
$content_type
->save();
$this->configImporter
->reset();
$expected = array(
'node.type.' . $content_type
->id() . '::config_test.dynamic.' . $test_entity_id,
);
$renames = $this->configImporter
->getUnprocessedConfiguration('rename');
$this
->assertIdentical($expected, $renames);
try {
$this->configImporter
->import();
$this
->fail('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
} catch (ConfigImporterException $e) {
$this
->pass('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
$expected = array(
SafeMarkup::format('Entity type mismatch on rename. @old_type not equal to @new_type for existing configuration @old_name and staged configuration @new_name.', array(
'@old_type' => 'node_type',
'@new_type' => 'config_test',
'@old_name' => 'node.type.' . $content_type
->id(),
'@new_name' => 'config_test.dynamic.' . $test_entity_id,
)),
);
$this
->assertEqual($expected, $this->configImporter
->getErrors());
}
}
public function testRenameSimpleConfigValidation() {
$uuid = new Php();
$config = $this
->config('config_test.new');
$uuid_value = $uuid
->generate();
$config
->set('uuid', $uuid_value)
->save();
$active = $this->container
->get('config.storage');
$sync = $this->container
->get('config.storage.sync');
$this
->copyConfig($active, $sync);
$config
->delete();
$config = $this
->config('config_test.old');
$config
->set('uuid', $uuid_value)
->save();
$this->configImporter
->reset();
$expected = array(
'config_test.old::config_test.new',
);
$renames = $this->configImporter
->getUnprocessedConfiguration('rename');
$this
->assertIdentical($expected, $renames);
try {
$this->configImporter
->import();
$this
->fail('Expected ConfigImporterException thrown when simple configuration is renamed.');
} catch (ConfigImporterException $e) {
$this
->pass('Expected ConfigImporterException thrown when simple configuration is renamed.');
$expected = array(
SafeMarkup::format('Rename operation for simple configuration. Existing configuration @old_name and staged configuration @new_name.', array(
'@old_name' => 'config_test.old',
'@new_name' => 'config_test.new',
)),
);
$this
->assertEqual($expected, $this->configImporter
->getErrors());
}
}
}