ConfigInstallTest.php 7.8 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\config\Tests\ConfigInstallTest.
6 7 8 9
 */

namespace Drupal\config\Tests;

10
use Drupal\simpletest\KernelTestBase;
11 12 13

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

20 21 22
  /**
   * {@inheritdoc}
   */
23
  protected function setUp() {
24 25 26 27 28 29 30
    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']);
  }

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

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

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

48
    // Install the test module.
49 50
    $this->enableModules(array('config_test', 'config_schema_test'));
    $this->installConfig(array('config_test', 'config_schema_test'));
51

52
    // After module installation the new schema should exist.
53
    $this->assertTrue(\Drupal::service('config.typed')->hasConfigSchema('config_schema_test.schema_in_install'), 'Configuration schema for config_schema_test.schema_in_install exists.');
54

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

63
    // Verify that config_test API hooks were invoked for the dynamic default
64
    // configuration entity.
65 66 67 68 69 70
    $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']));
71 72

    // Ensure that data type casting is applied during config installation.
73
    $config = $this->config('config_schema_test.schema_in_install');
74 75 76
    $this->assertIdentical($config->get('integer'), 1);

    // Test that uninstalling configuration removes configuration schema.
77
    $this->config('core.extension')->set('module', array())->save();
78
    \Drupal::service('config.manager')->uninstall('module', 'config_test');
79
    $this->assertFalse(\Drupal::service('config.typed')->hasConfigSchema('config_schema_test.schema_in_install'), 'Configuration schema for config_schema_test.schema_in_install does not exist.');
80 81 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  }

  /**
   * 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']);
    }

    // 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());
  }
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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.
    $this->enableModules(array('config_test', 'config_collection_install_test'));
    $this->installConfig(array('config_test'));
    /** @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');

    // The config_test.dynamic.dotted.default configuraton object saved in the
    // 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);
173
  }
174

175
}