ConfigExportImportUITest.php 12.5 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\Core\Archiver\ArchiveTar;
11 12 13
use Drupal\simpletest\WebTestBase;

/**
14
 * Tests the user interface for importing/exporting configuration.
15 16 17 18
 *
 * 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.
19 20
 *
 * @group config
21 22 23 24 25 26 27 28 29 30 31
 */
class ConfigExportImportUITest extends WebTestBase {

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

  /**
32
   * Modules to enable.
33
   *
34
   * @var array
35
   */
36
  public static $modules = array('config', 'node', 'field');
37

38 39 40
  /**
   * {@inheritdoc}
   */
41 42 43 44 45 46 47 48 49 50
  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.
    $this->drupalLogin($this->root_user);
  }

  /**
51
   * Tests a simple site export import case.
52
   */
53 54 55 56
  public function testExportImport() {
    $this->originalSlogan = \Drupal::config('system.site')->get('slogan');
    $this->newSlogan = $this->randomString(16);
    $this->assertNotEqual($this->newSlogan, $this->originalSlogan);
57
    \Drupal::config('system.site')
58
      ->set('slogan', $this->newSlogan)
59
      ->save();
60 61
    $this->assertEqual(\Drupal::config('system.site')->get('slogan'), $this->newSlogan);

62 63 64 65
    // Create a content type.
    $this->content_type = $this->drupalCreateContentType();

    // Create a field.
66 67 68
    $this->fieldName = drupal_strtolower($this->randomName());
    $this->fieldStorage = entity_create('field_storage_config', array(
      'name' => $this->fieldName,
69 70 71
      'entity_type' => 'node',
      'type' => 'text',
    ));
72
    $this->fieldStorage->save();
73
    entity_create('field_instance_config', array(
74
      'field_storage' => $this->fieldStorage,
75 76 77
      'bundle' => $this->content_type->type,
    ))->save();
    entity_get_form_display('node', $this->content_type->type, 'default')
78
      ->setComponent($this->fieldName, array(
79 80 81 82
        'type' => 'text_textfield',
      ))
      ->save();
    entity_get_display('node', $this->content_type->type, 'full')
83
      ->setComponent($this->fieldName)
84 85 86
      ->save();

    $this->drupalGet('node/add/' . $this->content_type->type);
87
    $this->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
88 89

    // Export the configuration.
90
    $this->drupalPostForm('admin/config/development/configuration/full/export', array(), 'Export');
91 92 93
    $this->tarball = $this->drupalGetContent();

    \Drupal::config('system.site')
94
      ->set('slogan', $this->originalSlogan)
95
      ->save();
96
    $this->assertEqual(\Drupal::config('system.site')->get('slogan'), $this->originalSlogan);
97

98 99 100
    // Delete the custom field.
    $field_instances = entity_load_multiple('field_instance_config');
    foreach ($field_instances as $field_instance) {
101
      if ($field_instance->field_name == $this->fieldName) {
102 103 104
        $field_instance->delete();
      }
    }
105 106 107 108
    $field_storages = entity_load_multiple('field_storage_config');
    foreach ($field_storages as $field_storage) {
      if ($field_storage->name == $this->fieldName) {
        $field_storage->delete();
109 110 111
      }
    }
    $this->drupalGet('node/add/' . $this->content_type->type);
112
    $this->assertNoFieldByName("{$this->fieldName}[0][value]", '', 'Widget is not displayed');
113 114

    // Import the configuration.
115 116
    $filename = 'temporary://' . $this->randomName();
    file_put_contents($filename, $this->tarball);
117
    $this->drupalPostForm('admin/config/development/configuration/full/import', array('files[import_tarball]' => $filename), 'Upload');
118
    $this->drupalPostForm(NULL, array(), 'Import all');
119 120

    $this->assertEqual(\Drupal::config('system.site')->get('slogan'), $this->newSlogan);
121 122

    $this->drupalGet('node/add');
123
    $this->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
124
  }
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

  /**
   * 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');
    $this->tarball = $this->drupalGetContent();
    $filename = file_directory_temp() .'/' . $this->randomName();
    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.');

    // Create the tar contains the expected contect for the collections.
    $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');
    $this->assertNoText(t('There are no configuration changes.'));
    $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');
    $this->assertText(t('There are no configuration changes.'));

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

238
}