ConfigExportImportUITest.php 15 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\config\Tests\ConfigExportImportUITest.
 */

namespace Drupal\config\Tests;

10
use Drupal\Component\Utility\Unicode;
11
use Drupal\Core\Archiver\ArchiveTar;
12
use Drupal\field\Entity\FieldConfig;
13
use Drupal\field\Entity\FieldStorageConfig;
14 15 16
use Drupal\simpletest\WebTestBase;

/**
17
 * Tests the user interface for importing/exporting configuration.
18 19 20 21
 *
 * Each testX method does a complete rebuild of a Drupal site, so values being
 * tested need to be stored in protected properties in order to survive until
 * the next rebuild.
22 23
 *
 * @group config
24 25 26 27 28 29 30 31 32 33
 */
class ConfigExportImportUITest extends WebTestBase {

  /**
   * The contents of the config export tarball, held between test methods.
   *
   * @var string
   */
  protected $tarball;

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  /**
   * Holds the original 'site slogan' before testing.
   *
   * @var string
   */
  protected $originalSlogan;

  /**
   * Holds a randomly generated new 'site slogan' for testing.
   *
   * @var string
   */
  protected $newSlogan;


  /**
   * Holds a content type.
   *
   * @var \Drupal\node\NodeInterface
   */
  protected $contentType;

  /**
   * Holds the randomly-generated name of a field.
   *
   * @var string
   */
  protected $fieldName;

  /**
   * Holds the field storage entity for $fieldName.
   *
   * @var \Drupal\field\FieldStorageConfigInterface
   */
  protected $fieldStorage;

70
  /**
71
   * Modules to enable.
72
   *
73
   * @var array
74
   */
75
  public static $modules = array('config', 'node', 'field');
76

77 78 79
  /**
   * {@inheritdoc}
   */
80 81 82 83 84 85
  protected function setUp() {
    parent::setUp();
    // The initial import must be done with uid 1 because if separately named
    // roles are created then the role is lost after import. If the roles
    // created have the same name then the sync will fail because they will
    // have different UUIDs.
86
    $this->drupalLogin($this->rootUser);
87 88 89
  }

  /**
90
   * Tests a simple site export import case.
91
   */
92
  public function testExportImport() {
93 94 95 96 97
    // After installation there is no snapshot and nothing to import.
    $this->drupalGet('admin/config/development/configuration');
    $this->assertNoText(t('Warning message'));
    $this->assertText(t('There are no configuration changes to import.'));

98
    $this->originalSlogan = $this->config('system.site')->get('slogan');
99 100
    $this->newSlogan = $this->randomString(16);
    $this->assertNotEqual($this->newSlogan, $this->originalSlogan);
101
    $this->config('system.site')
102
      ->set('slogan', $this->newSlogan)
103
      ->save();
104
    $this->assertEqual($this->config('system.site')->get('slogan'), $this->newSlogan);
105

106
    // Create a content type.
107
    $this->contentType = $this->drupalCreateContentType();
108 109

    // Create a field.
110
    $this->fieldName = Unicode::strtolower($this->randomMachineName());
111
    $this->fieldStorage = entity_create('field_storage_config', array(
112
      'field_name' => $this->fieldName,
113 114 115
      'entity_type' => 'node',
      'type' => 'text',
    ));
116
    $this->fieldStorage->save();
117
    entity_create('field_config', array(
118
      'field_storage' => $this->fieldStorage,
119
      'bundle' => $this->contentType->id(),
120
    ))->save();
121 122
    // Update the displays so that configuration does not change unexpectedly on
    // import.
123
    entity_get_form_display('node', $this->contentType->id(), 'default')
124
      ->setComponent($this->fieldName, array(
125 126 127
        'type' => 'text_textfield',
      ))
      ->save();
128
    entity_get_display('node', $this->contentType->id(), 'full')
129
      ->setComponent($this->fieldName)
130
      ->save();
131 132 133 134 135 136
    entity_get_display('node', $this->contentType->id(), 'default')
      ->setComponent($this->fieldName)
      ->save();
    entity_get_display('node', $this->contentType->id(), 'teaser')
      ->removeComponent($this->fieldName)
      ->save();
137

138
    $this->drupalGet('node/add/' . $this->contentType->id());
139
    $this->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
140 141

    // Export the configuration.
142
    $this->drupalPostForm('admin/config/development/configuration/full/export', array(), 'Export');
143
    $this->tarball = $this->getRawContent();
144

145
    $this->config('system.site')
146
      ->set('slogan', $this->originalSlogan)
147
      ->save();
148
    $this->assertEqual($this->config('system.site')->get('slogan'), $this->originalSlogan);
149

150
    // Delete the custom field.
151
    $fields = FieldConfig::loadMultiple();
152 153 154
    foreach ($fields as $field) {
      if ($field->field_name == $this->fieldName) {
        $field->delete();
155 156
      }
    }
157
    $field_storages = FieldStorageConfig::loadMultiple();
158
    foreach ($field_storages as $field_storage) {
159
      if ($field_storage->getName() == $this->fieldName) {
160
        $field_storage->delete();
161 162
      }
    }
163
    $this->drupalGet('node/add/' . $this->contentType->id());
164
    $this->assertNoFieldByName("{$this->fieldName}[0][value]", '', 'Widget is not displayed');
165 166

    // Import the configuration.
167
    $filename = 'temporary://' . $this->randomMachineName();
168
    file_put_contents($filename, $this->tarball);
169
    $this->drupalPostForm('admin/config/development/configuration/full/import', array('files[import_tarball]' => $filename), 'Upload');
170 171 172 173 174
    // There is no snapshot yet because an import has never run.
    $this->assertNoText(t('Warning message'));
    $this->assertNoText(t('There are no configuration changes to import.'));
    $this->assertText($this->contentType->label());

175
    $this->drupalPostForm(NULL, array(), 'Import all');
176 177 178
    // After importing the snapshot has been updated an there are no warnings.
    $this->assertNoText(t('Warning message'));
    $this->assertText(t('There are no configuration changes to import.'));
179

180
    $this->assertEqual($this->config('system.site')->get('slogan'), $this->newSlogan);
181 182

    $this->drupalGet('node/add');
183
    $this->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
184

185
    $this->config('system.site')
186 187 188
      ->set('slogan', $this->originalSlogan)
      ->save();
    $this->drupalGet('admin/config/development/configuration');
189 190 191 192 193 194 195
    $this->assertText(t('Warning message'));
    $this->assertText('Your current configuration has changed. Changes to these configuration items will be lost on the next synchronization: system.site');
    // Remove everything from staging. The warning about differences between the
    // active and snapshot should still exist.
    \Drupal::service('config.storage.staging')->deleteAll();
    $this->drupalGet('admin/config/development/configuration');
    $this->assertText(t('Warning message'));
196
    $this->assertText('Your current configuration has changed. Changes to these configuration items will be lost on the next synchronization: system.site');
197
    $this->assertText(t('There are no configuration changes to import.'));
198
  }
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

  /**
   * Tests an export and import of collections.
   */
  public function testExportImportCollections() {

    /** @var \Drupal\Core\Config\StorageInterface $active_storage */
    $active_storage = \Drupal::service('config.storage');
    $test1_storage = $active_storage->createCollection('collection.test1');
    $test1_storage->write('config_test.create', array('foo' => 'bar'));
    $test1_storage->write('config_test.update', array('foo' => 'bar'));
    $test2_storage = $active_storage->createCollection('collection.test2');
    $test2_storage->write('config_test.another_create', array('foo' => 'bar'));
    $test2_storage->write('config_test.another_update', array('foo' => 'bar'));

    // Export the configuration.
    $this->drupalPostForm('admin/config/development/configuration/full/export', array(), 'Export');
216
    $this->tarball = $this->getRawContent();
217
    $filename = file_directory_temp() .'/' . $this->randomMachineName();
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    file_put_contents($filename, $this->tarball);

    // Set up the active storage collections to test import.
    $test1_storage->delete('config_test.create');
    $test1_storage->write('config_test.update', array('foo' => 'baz'));
    $test1_storage->write('config_test.delete', array('foo' => 'bar'));
    $test2_storage->delete('config_test.another_create');
    $test2_storage->write('config_test.another_update', array('foo' => 'baz'));
    $test2_storage->write('config_test.another_delete', array('foo' => 'bar'));

    // Create a snapshot.
    $snapshot_storage = \Drupal::service('config.storage.snapshot');
    \Drupal::service('config.manager')->createSnapshot($active_storage, $snapshot_storage);

    // Ensure that the snapshot has the expected collection data before import.
    $test1_snapshot = $snapshot_storage->createCollection('collection.test1');
    $data = $test1_snapshot->read('config_test.delete');
    $this->assertEqual($data, array('foo' => 'bar'), 'The config_test.delete in collection.test1 exists in the snapshot storage.');
    $data = $test1_snapshot->read('config_test.update');
    $this->assertEqual($data, array('foo' => 'baz'), 'The config_test.update in collection.test1 exists in the snapshot storage.');
    $this->assertFalse($test1_snapshot->read('config_test.create'), 'The config_test.create in collection.test1 does not exist in the snapshot storage.');
    $test2_snapshot = $snapshot_storage->createCollection('collection.test2');
    $data = $test2_snapshot->read('config_test.another_delete');
    $this->assertEqual($data, array('foo' => 'bar'), 'The config_test.another_delete in collection.test2 exists in the snapshot storage.');
    $data = $test2_snapshot->read('config_test.another_update');
    $this->assertEqual($data, array('foo' => 'baz'), 'The config_test.another_update in collection.test2 exists in the snapshot storage.');
    $this->assertFalse($test2_snapshot->read('config_test.another_create'), 'The config_test.another_create in collection.test2 does not exist in the snapshot storage.');

246
    // Create the tar that contains the expected content for the collections.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    $tar = new ArchiveTar($filename, 'gz');
    $content_list = $tar->listContent();
    // Convert the list of files into something easy to search.
    $files = array();
    foreach ($content_list as $file) {
      $files[] = $file['filename'];
    }
    $this->assertTrue(in_array('collection/test1/config_test.create.yml', $files), 'Config export contains collection/test1/config_test.create.yml.');
    $this->assertTrue(in_array('collection/test2/config_test.another_create.yml', $files), 'Config export contains collection/test2/config_test.another_create.yml.');
    $this->assertTrue(in_array('collection/test1/config_test.update.yml', $files), 'Config export contains collection/test1/config_test.update.yml.');
    $this->assertTrue(in_array('collection/test2/config_test.another_update.yml', $files), 'Config export contains collection/test2/config_test.another_update.yml.');
    $this->assertFalse(in_array('collection/test1/config_test.delete.yml', $files), 'Config export does not contain collection/test1/config_test.delete.yml.');
    $this->assertFalse(in_array('collection/test2/config_test.another_delete.yml', $files), 'Config export does not contain collection/test2/config_test.another_delete.yml.');

    $this->drupalPostForm('admin/config/development/configuration/full/import', array('files[import_tarball]' => $filename), 'Upload');
    // Verify that there are configuration differences to import.
    $this->drupalGet('admin/config/development/configuration');
264
    $this->assertNoText(t('There are no configuration changes to import.'));
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    $this->assertText(t('!collection configuration collection', array('!collection' => 'collection.test1')));
    $this->assertText(t('!collection configuration collection', array('!collection' => 'collection.test2')));
    $this->assertText('config_test.create');
    $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test1/config_test.create');
    $this->assertText('config_test.update');
    $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test1/config_test.update');
    $this->assertText('config_test.delete');
    $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test1/config_test.delete');
    $this->assertText('config_test.another_create');
    $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test2/config_test.another_create');
    $this->assertText('config_test.another_update');
    $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test2/config_test.another_update');
    $this->assertText('config_test.another_delete');
    $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test2/config_test.another_delete');

    $this->drupalPostForm(NULL, array(), 'Import all');
281
    $this->assertText(t('There are no configuration changes to import.'));
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

    // Test data in collections.
    $data = $test1_storage->read('config_test.create');
    $this->assertEqual($data, array('foo' => 'bar'), 'The config_test.create in collection.test1 has been created.');
    $data = $test1_storage->read('config_test.update');
    $this->assertEqual($data, array('foo' => 'bar'), 'The config_test.update in collection.test1 has been updated.');
    $this->assertFalse($test1_storage->read('config_test.delete'), 'The config_test.delete in collection.test1 has been deleted.');

    $data = $test2_storage->read('config_test.another_create');
    $this->assertEqual($data, array('foo' => 'bar'), 'The config_test.another_create in collection.test2 has been created.');
    $data = $test2_storage->read('config_test.another_update');
    $this->assertEqual($data, array('foo' => 'bar'), 'The config_test.another_update in collection.test2 has been updated.');
    $this->assertFalse($test2_storage->read('config_test.another_delete'), 'The config_test.another_delete in collection.test2 has been deleted.');

    // Ensure that the snapshot has been updated with the collection data.
    $snapshot_storage = \Drupal::service('config.storage.snapshot');
    $test1_snapshot = $snapshot_storage->createCollection('collection.test1');
    $data = $test1_snapshot->read('config_test.create');
    $this->assertEqual($data, array('foo' => 'bar'), 'The config_test.create in collection.test1 has been created in the snapshot storage.');
    $data = $test1_snapshot->read('config_test.update');
    $this->assertEqual($data, array('foo' => 'bar'), 'The config_test.update in collection.test1 has been updated in the snapshot storage.');
    $this->assertFalse($test1_snapshot->read('config_test.delete'), 'The config_test.delete in collection.test1 does not exist in the snapshot storage.');
    $test2_snapshot = $snapshot_storage->createCollection('collection.test2');
    $data = $test2_snapshot->read('config_test.another_create');
    $this->assertEqual($data, array('foo' => 'bar'), 'The config_test.another_create in collection.test2 has been created in the snapshot storage.');
    $data = $test2_snapshot->read('config_test.another_update');
    $this->assertEqual($data, array('foo' => 'bar'), 'The config_test.another_update in collection.test2 has been updated in the snapshot storage.');
    $this->assertFalse($test2_snapshot->read('config_test.another_delete'), 'The config_test.another_delete in collection.test2 does not exist in the snapshot storage.');
  }

312
}