You are here

public function ConfigImportRenameValidationTest::testRenameValidation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/config/src/Tests/ConfigImportRenameValidationTest.php \Drupal\config\Tests\ConfigImportRenameValidationTest::testRenameValidation()

Tests configuration renaming validation.

File

core/modules/config/src/Tests/ConfigImportRenameValidationTest.php, line 71
Contains \Drupal\config\Tests\ConfigImportRenameValidationTest.

Class

ConfigImportRenameValidationTest
Tests validating renamed configuration in a configuration import.

Namespace

Drupal\config\Tests

Code

public function testRenameValidation() {

  // Create a test entity.
  $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();

  // Stage the test entity and then delete it from the active storage.
  $active = $this->container
    ->get('config.storage');
  $sync = $this->container
    ->get('config.storage.sync');
  $this
    ->copyConfig($active, $sync);
  $test_entity
    ->delete();

  // Create a content type with a matching UUID in the active storage.
  $content_type = entity_create('node_type', array(
    'type' => Unicode::strtolower($this
      ->randomMachineName(16)),
    'name' => $this
      ->randomMachineName(),
    'uuid' => $uuid,
  ));
  $content_type
    ->save();

  // Confirm that the staged configuration is detected as a rename since the
  // UUIDs match.
  $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 to import the configuration. We expect an exception to be thrown
  // because the staged entity is of a different type.
  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());
  }
}