You are here

public function ConfigExportImportUITest::testExportImport in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/config/src/Tests/ConfigExportImportUITest.php \Drupal\config\Tests\ConfigExportImportUITest::testExportImport()

Tests a simple site export import case.

File

core/modules/config/src/Tests/ConfigExportImportUITest.php, line 92
Contains \Drupal\config\Tests\ConfigExportImportUITest.

Class

ConfigExportImportUITest
Tests the user interface for importing/exporting configuration.

Namespace

Drupal\config\Tests

Code

public function testExportImport() {

  // 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.'));
  $this->originalSlogan = $this
    ->config('system.site')
    ->get('slogan');
  $this->newSlogan = $this
    ->randomString(16);
  $this
    ->assertNotEqual($this->newSlogan, $this->originalSlogan);
  $this
    ->config('system.site')
    ->set('slogan', $this->newSlogan)
    ->save();
  $this
    ->assertEqual($this
    ->config('system.site')
    ->get('slogan'), $this->newSlogan);

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

  // Create a field.
  $this->fieldName = Unicode::strtolower($this
    ->randomMachineName());
  $this->fieldStorage = entity_create('field_storage_config', array(
    'field_name' => $this->fieldName,
    'entity_type' => 'node',
    'type' => 'text',
  ));
  $this->fieldStorage
    ->save();
  entity_create('field_config', array(
    'field_storage' => $this->fieldStorage,
    'bundle' => $this->contentType
      ->id(),
  ))
    ->save();

  // Update the displays so that configuration does not change unexpectedly on
  // import.
  entity_get_form_display('node', $this->contentType
    ->id(), 'default')
    ->setComponent($this->fieldName, array(
    'type' => 'text_textfield',
  ))
    ->save();
  entity_get_display('node', $this->contentType
    ->id(), 'full')
    ->setComponent($this->fieldName)
    ->save();
  entity_get_display('node', $this->contentType
    ->id(), 'default')
    ->setComponent($this->fieldName)
    ->save();
  entity_get_display('node', $this->contentType
    ->id(), 'teaser')
    ->removeComponent($this->fieldName)
    ->save();
  $this
    ->drupalGet('node/add/' . $this->contentType
    ->id());
  $this
    ->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');

  // Export the configuration.
  $this
    ->drupalPostForm('admin/config/development/configuration/full/export', array(), 'Export');
  $this->tarball = $this
    ->getRawContent();
  $this
    ->config('system.site')
    ->set('slogan', $this->originalSlogan)
    ->save();
  $this
    ->assertEqual($this
    ->config('system.site')
    ->get('slogan'), $this->originalSlogan);

  // Delete the custom field.
  $fields = FieldConfig::loadMultiple();
  foreach ($fields as $field) {
    if ($field
      ->getName() == $this->fieldName) {
      $field
        ->delete();
    }
  }
  $field_storages = FieldStorageConfig::loadMultiple();
  foreach ($field_storages as $field_storage) {
    if ($field_storage
      ->getName() == $this->fieldName) {
      $field_storage
        ->delete();
    }
  }
  $this
    ->drupalGet('node/add/' . $this->contentType
    ->id());
  $this
    ->assertNoFieldByName("{$this->fieldName}[0][value]", '', 'Widget is not displayed');

  // Import the configuration.
  $filename = 'temporary://' . $this
    ->randomMachineName();
  file_put_contents($filename, $this->tarball);
  $this
    ->drupalPostForm('admin/config/development/configuration/full/import', array(
    'files[import_tarball]' => $filename,
  ), 'Upload');

  // 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());
  $this
    ->drupalPostForm(NULL, array(), 'Import all');

  // 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.'));
  $this
    ->assertEqual($this
    ->config('system.site')
    ->get('slogan'), $this->newSlogan);
  $this
    ->drupalGet('node/add');
  $this
    ->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
  $this
    ->config('system.site')
    ->set('slogan', $this->originalSlogan)
    ->save();
  $this
    ->drupalGet('admin/config/development/configuration');
  $this
    ->assertText(t('Warning message'));
  $this
    ->assertText('The following items in your active configuration have changes since the last import that may be lost on the next import.');

  // Ensure the item is displayed as part of a list (to avoid false matches
  // on the rest of the page) and that the list markup is not escaped.
  $this
    ->assertRaw('<li>system.site</li>');

  // Remove everything from sync. The warning about differences between the
  // active and snapshot should no longer exist.
  \Drupal::service('config.storage.sync')
    ->deleteAll();
  $this
    ->drupalGet('admin/config/development/configuration');
  $this
    ->assertNoText(t('Warning message'));
  $this
    ->assertNoText('The following items in your active configuration have changes since the last import that may be lost on the next import.');
  $this
    ->assertText(t('There are no configuration changes to import.'));

  // Write a file to sync. The warning about differences between the active
  // and snapshot should now exist.

  /** @var \Drupal\Core\Config\StorageInterface $sync */
  $sync = $this->container
    ->get('config.storage.sync');
  $data = $this
    ->config('system.site')
    ->get();
  $data['slogan'] = 'in the face';
  $this
    ->copyConfig($this->container
    ->get('config.storage'), $sync);
  $sync
    ->write('system.site', $data);
  $this
    ->drupalGet('admin/config/development/configuration');
  $this
    ->assertText(t('Warning message'));
  $this
    ->assertText('The following items in your active configuration have changes since the last import that may be lost on the next import.');

  // Ensure the item is displayed as part of a list (to avoid false matches
  // on the rest of the page) and that the list markup is not escaped.
  $this
    ->assertRaw('<li>system.site</li>');
}