ConfigImporterTest.php 8.09 KB
Newer Older
1 2 3 4
<?php

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

namespace Drupal\config\Tests;

10 11
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\StorageComparerManifest;
12
use Drupal\simpletest\DrupalUnitTestBase;
13 14

/**
15
 * Tests importing configuration from files into active configuration.
16
 */
17 18 19 20 21 22 23 24
class ConfigImporterTest extends DrupalUnitTestBase {

  /**
   * Config Importer object used for testing.
   *
   * @var \Drupal\Core\Config\ConfigImporter
   */
  protected $configImporter;
25 26 27 28 29 30

  /**
   * Modules to enable.
   *
   * @var array
   */
31
  public static $modules = array('config_test', 'system');
32

33 34 35
  public static function getInfo() {
    return array(
      'name' => 'Import configuration',
36
      'description' => 'Tests importing configuration from files into active configuration.',
37 38 39 40
      'group' => 'Configuration',
    );
  }

41 42 43
  function setUp() {
    parent::setUp();

44 45
    $this->installSchema('system', 'config_snapshot');

46 47 48 49
    config_install_default_config('module', 'config_test');
    // Installing config_test's default configuration pollutes the global
    // variable being used for recording hook invocations by this test already,
    // so it has to be cleared out manually.
50
    unset($GLOBALS['hook_config_test']);
51 52 53 54 55 56 57 58 59 60 61 62

    // Set up the ConfigImporter object for testing.
    $config_comparer = new StorageComparerManifest(
      $this->container->get('config.storage.staging'),
      $this->container->get('config.storage'));
    $this->configImporter = new ConfigImporter(
      $config_comparer->createChangelist(),
      $this->container->get('event_dispatcher'),
      $this->container->get('config.factory'),
      $this->container->get('plugin.manager.entity'),
      $this->container->get('lock')
    );
63 64 65 66 67 68
  }

  /**
   * Tests omission of module APIs for bare configuration operations.
   */
  function testNoImport() {
69
    $dynamic_name = 'config_test.dynamic.dotted.default';
70 71 72

    // Verify the default configuration values exist.
    $config = config($dynamic_name);
73
    $this->assertIdentical($config->get('id'), 'dotted.default');
74 75 76 77 78

    // Verify that a bare config() does not involve module APIs.
    $this->assertFalse(isset($GLOBALS['hook_config_test']));
  }

79 80 81 82
  /**
   * Tests deletion of configuration during import.
   */
  function testDeleted() {
83
    $dynamic_name = 'config_test.dynamic.dotted.default';
84 85
    $storage = $this->container->get('config.storage');
    $staging = $this->container->get('config.storage.staging');
86 87 88

    // Verify the default configuration values exist.
    $config = config($dynamic_name);
89
    $this->assertIdentical($config->get('id'), 'dotted.default');
90

91 92
    // Create an empty manifest to delete the configuration object.
    $staging->write('manifest.config_test.dynamic', array());
93
    // Import.
94
    $this->configImporter->reset()->import();
95 96

    // Verify the values have disappeared.
97
    $this->assertIdentical($storage->read($dynamic_name), FALSE);
98 99 100

    $config = config($dynamic_name);
    $this->assertIdentical($config->get('id'), NULL);
101 102 103 104 105 106 107 108

    // Verify that appropriate module API hooks have been invoked.
    $this->assertTrue(isset($GLOBALS['hook_config_test']['load']));
    $this->assertFalse(isset($GLOBALS['hook_config_test']['presave']));
    $this->assertFalse(isset($GLOBALS['hook_config_test']['insert']));
    $this->assertFalse(isset($GLOBALS['hook_config_test']['update']));
    $this->assertTrue(isset($GLOBALS['hook_config_test']['predelete']));
    $this->assertTrue(isset($GLOBALS['hook_config_test']['delete']));
109 110

    // Verify that there is nothing more to import.
111
    $this->assertFalse($this->configImporter->hasUnprocessedChanges());
112 113 114 115 116 117 118
  }

  /**
   * Tests creation of configuration during import.
   */
  function testNew() {
    $dynamic_name = 'config_test.dynamic.new';
119 120
    $storage = $this->container->get('config.storage');
    $staging = $this->container->get('config.storage.staging');
121

122
    // Verify the configuration to create does not exist yet.
123 124 125
    $this->assertIdentical($storage->exists($dynamic_name), FALSE, $dynamic_name . ' not found.');

    $this->assertIdentical($staging->exists($dynamic_name), FALSE, $dynamic_name . ' not found.');
126

127
    // Create new config entity.
128
    $original_dynamic_data = array(
129
      'id' => 'new',
130
      'uuid' => '30df59bd-7b03-4cf7-bb35-d42fc49f0651',
131
      'label' => 'New',
132
      'style' => '',
133
      'status' => '1',
134
      'langcode' => language_default()->langcode,
135
      'protected_property' => '',
136
    );
137
    $staging->write($dynamic_name, $original_dynamic_data);
138 139 140 141 142 143

    // Create manifest for new config entity.
    $manifest_data = config('manifest.config_test.dynamic')->get();
    $manifest_data[$original_dynamic_data['id']]['name'] = 'config_test.dynamic.' . $original_dynamic_data['id'];
    $staging->write('manifest.config_test.dynamic', $manifest_data);

144
    $this->assertIdentical($staging->exists($dynamic_name), TRUE, $dynamic_name . ' found.');
145 146

    // Import.
147
    $this->configImporter->reset()->import();
148 149 150

    // Verify the values appeared.
    $config = config($dynamic_name);
151
    $this->assertIdentical($config->get('label'), $original_dynamic_data['label']);
152 153 154 155 156 157 158 159

    // Verify that appropriate module API hooks have been invoked.
    $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']));
160 161

    // Verify that there is nothing more to import.
162
    $this->assertFalse($this->configImporter->hasUnprocessedChanges());
163 164 165 166 167 168 169
  }

  /**
   * Tests updating of configuration during import.
   */
  function testUpdated() {
    $name = 'config_test.system';
170
    $dynamic_name = 'config_test.dynamic.dotted.default';
171 172
    $storage = $this->container->get('config.storage');
    $staging = $this->container->get('config.storage.staging');
173

174
    // Verify that the configuration objects to import exist.
175 176 177
    $this->assertIdentical($storage->exists($name), TRUE, $name . ' found.');
    $this->assertIdentical($storage->exists($dynamic_name), TRUE, $dynamic_name . ' found.');

178 179 180
    // Replace the file content of the existing configuration objects in the
    // staging directory.
    $original_name_data = array(
181
      'foo' => 'beer',
182
    );
183
    $staging->write($name, $original_name_data);
184
    $original_dynamic_data = $storage->read($dynamic_name);
185 186
    $original_dynamic_data['label'] = 'Updated';
    $staging->write($dynamic_name, $original_dynamic_data);
187 188 189
    // Create manifest for updated config entity.
    $manifest_data = config('manifest.config_test.dynamic')->get();
    $staging->write('manifest.config_test.dynamic', $manifest_data);
190

191
    // Verify the active configuration still returns the default values.
192 193 194 195 196 197
    $config = config($name);
    $this->assertIdentical($config->get('foo'), 'bar');
    $config = config($dynamic_name);
    $this->assertIdentical($config->get('label'), 'Default');

    // Import.
198
    $this->configImporter->reset()->import();
199 200 201 202 203 204

    // Verify the values were updated.
    $config = config($name);
    $this->assertIdentical($config->get('foo'), 'beer');
    $config = config($dynamic_name);
    $this->assertIdentical($config->get('label'), 'Updated');
205

206
    // Verify that the original file content is still the same.
207 208
    $this->assertIdentical($staging->read($name), $original_name_data);
    $this->assertIdentical($staging->read($dynamic_name), $original_dynamic_data);
209

210 211 212 213 214 215 216
    // Verify that appropriate module API hooks have been invoked.
    $this->assertTrue(isset($GLOBALS['hook_config_test']['load']));
    $this->assertTrue(isset($GLOBALS['hook_config_test']['presave']));
    $this->assertFalse(isset($GLOBALS['hook_config_test']['insert']));
    $this->assertTrue(isset($GLOBALS['hook_config_test']['update']));
    $this->assertFalse(isset($GLOBALS['hook_config_test']['predelete']));
    $this->assertFalse(isset($GLOBALS['hook_config_test']['delete']));
217 218

    // Verify that there is nothing more to import.
219
    $this->assertFalse($this->configImporter->hasUnprocessedChanges());
220
  }
221

222
}