ConfigImportRenameValidationTest.php 5.61 KB
Newer Older
1 2
<?php

3
namespace Drupal\KernelTests\Core\Config;
4

5
use Drupal\Component\Utility\SafeMarkup;
6 7 8 9 10
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;
11
use Drupal\node\Entity\NodeType;
12
use Drupal\KernelTests\KernelTestBase;
13 14 15

/**
 * Tests validating renamed configuration in a configuration import.
16 17
 *
 * @group config
18
 */
19
class ConfigImportRenameValidationTest extends KernelTestBase {
20 21 22 23 24 25 26 27 28 29 30 31 32

  /**
   * Config Importer object used for testing.
   *
   * @var \Drupal\Core\Config\ConfigImporter
   */
  protected $configImporter;

  /**
   * Modules to enable.
   *
   * @var array
   */
33
  public static $modules = ['system', 'user', 'node', 'field', 'text', 'config_test'];
34 35 36 37

  /**
   * {@inheritdoc}
   */
38
  protected function setUp() {
39 40
    parent::setUp();

41 42
    $this->installEntitySchema('user');
    $this->installEntitySchema('node');
43
    $this->installConfig(['field']);
44 45 46

    // Set up the ConfigImporter object for testing.
    $storage_comparer = new StorageComparer(
47
      $this->container->get('config.storage.sync'),
48 49
      $this->container->get('config.storage'),
      $this->container->get('config.manager')
50 51 52 53 54
    );
    $this->configImporter = new ConfigImporter(
      $storage_comparer->createChangelist(),
      $this->container->get('event_dispatcher'),
      $this->container->get('config.manager'),
55
      $this->container->get('lock.persistent'),
56 57
      $this->container->get('config.typed'),
      $this->container->get('module_handler'),
58
      $this->container->get('module_installer'),
59 60 61 62 63 64 65 66 67 68
      $this->container->get('theme_handler'),
      $this->container->get('string_translation')
    );
  }

  /**
   * Tests configuration renaming validation.
   */
  public function testRenameValidation() {
    // Create a test entity.
69
    $test_entity_id = $this->randomMachineName();
70
    $test_entity = entity_create('config_test', [
71
      'id' => $test_entity_id,
72
      'label' => $this->randomMachineName(),
73
    ]);
74 75 76 77 78
    $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');
79 80
    $sync = $this->container->get('config.storage.sync');
    $this->copyConfig($active, $sync);
81 82 83
    $test_entity->delete();

    // Create a content type with a matching UUID in the active storage.
84
    $content_type = NodeType::create([
85 86
      'type' => Unicode::strtolower($this->randomMachineName(16)),
      'name' => $this->randomMachineName(),
87
      'uuid' => $uuid,
88
    ]);
89 90 91 92 93
    $content_type->save();

    // Confirm that the staged configuration is detected as a rename since the
    // UUIDs match.
    $this->configImporter->reset();
94
    $expected = [
95
      'node.type.' . $content_type->id() . '::config_test.dynamic.' . $test_entity_id,
96
    ];
97 98 99 100 101 102 103 104 105 106 107
    $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.');
108 109 110
      $expected = [
        SafeMarkup::format('Entity type mismatch on rename. @old_type not equal to @new_type for existing configuration @old_name and staged configuration @new_name.', ['@old_type' => 'node_type', '@new_type' => 'config_test', '@old_name' => 'node.type.' . $content_type->id(), '@new_name' => 'config_test.dynamic.' . $test_entity_id])
      ];
111
      $this->assertEqual($expected, $this->configImporter->getErrors());
112 113 114 115 116 117 118 119 120
    }
  }

  /**
   * Tests configuration renaming validation for simple configuration.
   */
  public function testRenameSimpleConfigValidation() {
    $uuid = new Php();
    // Create a simple configuration with a UUID.
121
    $config = $this->config('config_test.new');
122 123 124 125
    $uuid_value = $uuid->generate();
    $config->set('uuid', $uuid_value)->save();

    $active = $this->container->get('config.storage');
126 127
    $sync = $this->container->get('config.storage.sync');
    $this->copyConfig($active, $sync);
128 129 130
    $config->delete();

    // Create another simple configuration with the same UUID.
131
    $config = $this->config('config_test.old');
132 133 134 135 136
    $config->set('uuid', $uuid_value)->save();

    // Confirm that the staged configuration is detected as a rename since the
    // UUIDs match.
    $this->configImporter->reset();
137
    $expected = [
138
      'config_test.old::config_test.new'
139
    ];
140 141 142 143 144 145 146 147 148 149 150
    $renames = $this->configImporter->getUnprocessedConfiguration('rename');
    $this->assertIdentical($expected, $renames);

    // Try to import the configuration. We expect an exception to be thrown
    // because the rename is for simple configuration.
    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.');
151 152 153
      $expected = [
        SafeMarkup::format('Rename operation for simple configuration. Existing configuration @old_name and staged configuration @new_name.', ['@old_name' => 'config_test.old', '@new_name' => 'config_test.new'])
      ];
154
      $this->assertEqual($expected, $this->configImporter->getErrors());
155 156 157 158
    }
  }

}