ConfigInstallTest.php 11.2 KB
Newer Older
1 2
<?php

3
namespace Drupal\KernelTests\Core\Config;
4

5
use Drupal\Core\Config\InstallStorage;
6
use Drupal\Core\Config\PreExistingConfigException;
7
use Drupal\Core\Config\UnmetDependenciesException;
8
use Drupal\KernelTests\KernelTestBase;
9 10 11

/**
 * Tests installation of configuration objects in installation functionality.
12
 *
13
 * @group config
14
 * @see \Drupal\Core\Config\ConfigInstaller
15
 */
16
class ConfigInstallTest extends KernelTestBase {
17

18 19 20 21 22
  /**
   * {@inheritdoc}
   */
  public static $modules = ['system'];

23 24 25
  /**
   * {@inheritdoc}
   */
26
  protected function setUp() {
27 28 29 30 31 32 33
    parent::setUp();

    // Ensure the global variable being asserted by this test does not exist;
    // a previous test executed in this request/process might have set it.
    unset($GLOBALS['hook_config_test']);
  }

34 35 36 37 38
  /**
   * Tests module installation.
   */
  function testModuleInstallation() {
    $default_config = 'config_test.system';
39
    $default_configuration_entity = 'config_test.dynamic.dotted.default';
40 41

    // Verify that default module config does not exist before installation yet.
42
    $config = $this->config($default_config);
43
    $this->assertIdentical($config->isNew(), TRUE);
44
    $config = $this->config($default_configuration_entity);
45 46
    $this->assertIdentical($config->isNew(), TRUE);

47 48
    // Ensure that schema provided by modules that are not installed is not
    // available.
49
    $this->assertFalse(\Drupal::service('config.typed')->hasConfigSchema('config_schema_test.someschema'), 'Configuration schema for config_schema_test.someschema does not exist.');
50

51
    // Install the test module.
52
    $this->installModules(array('config_test'));
53

54
    // Verify that default module config exists.
55 56
    \Drupal::configFactory()->reset($default_config);
    \Drupal::configFactory()->reset($default_configuration_entity);
57
    $config = $this->config($default_config);
58
    $this->assertIdentical($config->isNew(), FALSE);
59
    $config = $this->config($default_configuration_entity);
60 61
    $this->assertIdentical($config->isNew(), FALSE);

62
    // Verify that config_test API hooks were invoked for the dynamic default
63
    // configuration entity.
64 65 66 67 68 69
    $this->assertFalse(isset($GLOBALS['hook_config_test']['load']));
    $this->assertTrue(isset($GLOBALS['hook_config_test']['presave']));
    $this->assertTrue(isset($GLOBALS['hook_config_test']['insert']));
    $this->assertFalse(isset($GLOBALS['hook_config_test']['update']));
    $this->assertFalse(isset($GLOBALS['hook_config_test']['predelete']));
    $this->assertFalse(isset($GLOBALS['hook_config_test']['delete']));
70

71 72 73 74 75
    // Install the schema test module.
    $this->enableModules(array('config_schema_test'));
    $this->installConfig(array('config_schema_test'));

    // After module installation the new schema should exist.
76
    $this->assertTrue(\Drupal::service('config.typed')->hasConfigSchema('config_schema_test.someschema'), 'Configuration schema for config_schema_test.someschema exists.');
77 78

    // Test that uninstalling configuration removes configuration schema.
79
    $this->config('core.extension')->set('module', array())->save();
80
    \Drupal::service('config.manager')->uninstall('module', 'config_test');
81
    $this->assertFalse(\Drupal::service('config.typed')->hasConfigSchema('config_schema_test.someschema'), 'Configuration schema for config_schema_test.someschema does not exist.');
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  }

  /**
   * Tests that collections are ignored if the event does not return anything.
   */
  public function testCollectionInstallationNoCollections() {
    // Install the test module.
    $this->enableModules(array('config_collection_install_test'));
    $this->installConfig(array('config_collection_install_test'));
    /** @var \Drupal\Core\Config\StorageInterface $active_storage */
    $active_storage = \Drupal::service('config.storage');
    $this->assertEqual(array(), $active_storage->getAllCollectionNames());
  }

  /**
   * Tests config objects in collections are installed as expected.
   */
  public function testCollectionInstallationCollections() {
    $collections = array(
      'another_collection',
      'collection.test1',
      'collection.test2',
    );
    // Set the event listener to return three possible collections.
    // @see \Drupal\config_collection_install_test\EventSubscriber
    \Drupal::state()->set('config_collection_install_test.collection_names', $collections);
    // Install the test module.
    $this->enableModules(array('config_collection_install_test'));
    $this->installConfig(array('config_collection_install_test'));
    /** @var \Drupal\Core\Config\StorageInterface $active_storage */
    $active_storage = \Drupal::service('config.storage');
    $this->assertEqual($collections, $active_storage->getAllCollectionNames());
    foreach ($collections as $collection) {
      $collection_storage = $active_storage->createCollection($collection);
      $data = $collection_storage->read('config_collection_install_test.test');
      $this->assertEqual($collection, $data['collection']);
    }

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    // Tests that clashing configuration in collections is detected.
    try {
      \Drupal::service('module_installer')->install(['config_collection_clash_install_test']);
      $this->fail('Expected PreExistingConfigException not thrown.');
    }
    catch (PreExistingConfigException $e) {
      $this->assertEqual($e->getExtension(), 'config_collection_clash_install_test');
      $this->assertEqual($e->getConfigObjects(), [
        'another_collection' => ['config_collection_install_test.test'],
        'collection.test1' => ['config_collection_install_test.test'],
        'collection.test2' => ['config_collection_install_test.test'],
      ]);
      $this->assertEqual($e->getMessage(), 'Configuration objects (another_collection/config_collection_install_test.test, collection/test1/config_collection_install_test.test, collection/test2/config_collection_install_test.test) provided by config_collection_clash_install_test already exist in active configuration');
    }

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    // Test that the we can use the config installer to install all the
    // available default configuration in a particular collection for enabled
    // extensions.
    \Drupal::service('config.installer')->installCollectionDefaultConfig('entity');
    // The 'entity' collection will not exist because the 'config_test' module
    // is not enabled.
    $this->assertEqual($collections, $active_storage->getAllCollectionNames());
    // Enable the 'config_test' module and try again.
    $this->enableModules(array('config_test'));
    \Drupal::service('config.installer')->installCollectionDefaultConfig('entity');
    $collections[] = 'entity';
    $this->assertEqual($collections, $active_storage->getAllCollectionNames());
    $collection_storage = $active_storage->createCollection('entity');
    $data = $collection_storage->read('config_test.dynamic.dotted.default');
    $this->assertIdentical(array('label' => 'entity'), $data);

    // Test that the config manager uninstalls configuration from collections
    // as expected.
    \Drupal::service('config.manager')->uninstall('module', 'config_collection_install_test');
    $this->assertEqual(array('entity'), $active_storage->getAllCollectionNames());
    \Drupal::service('config.manager')->uninstall('module', 'config_test');
    $this->assertEqual(array(), $active_storage->getAllCollectionNames());
  }
158

159 160 161 162 163 164 165 166 167 168 169 170 171 172
  /**
   * Tests collections which do not support config entities install correctly.
   *
   * Config entity detection during config installation is done by matching
   * config name prefixes. If a collection provides a configuration with a
   * matching name but does not support config entities it should be created
   * using simple configuration.
   */
  public function testCollectionInstallationCollectionConfigEntity() {
    $collections = array(
      'entity',
    );
    \Drupal::state()->set('config_collection_install_test.collection_names', $collections);
    // Install the test module.
173
    $this->installModules(array('config_test', 'config_collection_install_test'));
174 175 176 177 178
    /** @var \Drupal\Core\Config\StorageInterface $active_storage */
    $active_storage = \Drupal::service('config.storage');
    $this->assertEqual($collections, $active_storage->getAllCollectionNames());
    $collection_storage = $active_storage->createCollection('entity');

179
    // The config_test.dynamic.dotted.default configuration object saved in the
180 181 182 183 184 185 186 187 188
    // active store should be a configuration entity complete with UUID. Because
    // the entity collection does not support configuration entities the
    // configuration object stored there with the same name should only contain
    // a label.
    $name = 'config_test.dynamic.dotted.default';
    $data = $active_storage->read($name);
    $this->assertTrue(isset($data['uuid']));
    $data = $collection_storage->read($name);
    $this->assertIdentical(array('label' => 'entity'), $data);
189
  }
190

191 192 193 194 195 196 197 198 199 200 201
  /**
   * Tests the configuration with unmet dependencies is not installed.
   */
  public function testDependencyChecking() {
    $this->installModules(['config_test']);
    try {
      $this->installModules(['config_install_dependency_test']);
      $this->fail('Expected UnmetDependenciesException not thrown.');
    }
    catch (UnmetDependenciesException $e) {
      $this->assertEqual($e->getExtension(), 'config_install_dependency_test');
202 203
      $this->assertEqual($e->getConfigObjects(), ['config_test.dynamic.other_module_test_with_dependency' => ['config_other_module_config_test']]);
      $this->assertEqual($e->getMessage(), 'Configuration objects provided by <em class="placeholder">config_install_dependency_test</em> have unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test)</em>');
204 205 206
    }
    $this->installModules(['config_other_module_config_test']);
    $this->installModules(['config_install_dependency_test']);
207 208 209 210 211
    $entity = \Drupal::entityManager()->getStorage('config_test')->load('other_module_test_with_dependency');
    $this->assertTrue($entity, 'The config_test.dynamic.other_module_test_with_dependency configuration has been created during install.');
    // Ensure that dependencies can be added during module installation by
    // hooks.
    $this->assertIdentical('config_install_dependency_test', $entity->getDependencies()['module'][0]);
212 213 214 215 216 217 218 219
  }

  /**
   * Tests imported configuration entities with and without language information.
   */
  function testLanguage() {
    $this->installModules(['config_test_language']);
    // Test imported configuration with implicit language code.
220 221
    $storage = new InstallStorage();
    $data = $storage->read('config_test.dynamic.dotted.english');
222 223 224 225 226 227 228
    $this->assertTrue(!isset($data['langcode']));
    $this->assertEqual(
      $this->config('config_test.dynamic.dotted.english')->get('langcode'),
      'en'
    );

    // Test imported configuration with explicit language code.
229
    $data = $storage->read('config_test.dynamic.dotted.french');
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    $this->assertEqual($data['langcode'], 'fr');
    $this->assertEqual(
      $this->config('config_test.dynamic.dotted.french')->get('langcode'),
      'fr'
    );
  }

  /**
   * Installs a module.
   *
   * @param array $modules
   *   The module names.
   */
  protected function installModules(array $modules) {
    $this->container->get('module_installer')->install($modules);
    $this->container = \Drupal::getContainer();
  }

248
}