You are here

public function ConfigSingleImportExportTest::testImport in Zircon Profile 8

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

Tests importing a single configuration file.

File

core/modules/config/src/Tests/ConfigSingleImportExportTest.php, line 40
Contains \Drupal\config\Tests\ConfigSingleImportExportTest.

Class

ConfigSingleImportExportTest
Tests the user interface for importing/exporting a single configuration.

Namespace

Drupal\config\Tests

Code

public function testImport() {
  $storage = \Drupal::entityManager()
    ->getStorage('config_test');
  $uuid = \Drupal::service('uuid');
  $this
    ->drupalLogin($this
    ->drupalCreateUser(array(
    'import configuration',
  )));
  $import = <<<EOD
label: First
weight: 0
style: ''
status: '1'
EOD;
  $edit = array(
    'config_type' => 'config_test',
    'import' => $import,
  );

  // Attempt an import with a missing ID.
  $this
    ->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
  $this
    ->assertText(t('Missing ID key "@id_key" for this @entity_type import.', array(
    '@id_key' => 'id',
    '@entity_type' => 'Test configuration',
  )));

  // Perform an import with no specified UUID and a unique ID.
  $this
    ->assertNull($storage
    ->load('first'));
  $edit['import'] = "id: first\n" . $edit['import'];
  $this
    ->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
  $this
    ->assertRaw(t('Are you sure you want to create a new %name @type?', array(
    '%name' => 'first',
    '@type' => 'test configuration',
  )));
  $this
    ->drupalPostForm(NULL, array(), t('Confirm'));
  $entity = $storage
    ->load('first');
  $this
    ->assertIdentical($entity
    ->label(), 'First');
  $this
    ->assertIdentical($entity
    ->id(), 'first');
  $this
    ->assertTrue($entity
    ->status());
  $this
    ->assertRaw(t('The configuration was imported successfully.'));

  // Attempt an import with an existing ID but missing UUID.
  $this
    ->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
  $this
    ->assertText(t('An entity with this machine name already exists but the import did not specify a UUID.'));

  // Attempt an import with a mismatched UUID and existing ID.
  $edit['import'] .= "\nuuid: " . $uuid
    ->generate();
  $this
    ->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
  $this
    ->assertText(t('An entity with this machine name already exists but the UUID does not match.'));

  // Attempt an import with a custom ID.
  $edit['custom_entity_id'] = 'custom_id';
  $this
    ->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
  $this
    ->assertRaw(t('Are you sure you want to create a new %name @type?', array(
    '%name' => 'custom_id',
    '@type' => 'test configuration',
  )));
  $this
    ->drupalPostForm(NULL, array(), t('Confirm'));
  $this
    ->assertRaw(t('The configuration was imported successfully.'));

  // Perform an import with a unique ID and UUID.
  $import = <<<EOD
id: second
label: Second
weight: 0
style: ''
status: '0'
EOD;
  $edit = array(
    'config_type' => 'config_test',
    'import' => $import,
  );
  $second_uuid = $uuid
    ->generate();
  $edit['import'] .= "\nuuid: " . $second_uuid;
  $this
    ->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
  $this
    ->assertRaw(t('Are you sure you want to create a new %name @type?', array(
    '%name' => 'second',
    '@type' => 'test configuration',
  )));
  $this
    ->drupalPostForm(NULL, array(), t('Confirm'));
  $entity = $storage
    ->load('second');
  $this
    ->assertRaw(t('The configuration was imported successfully.'));
  $this
    ->assertIdentical($entity
    ->label(), 'Second');
  $this
    ->assertIdentical($entity
    ->id(), 'second');
  $this
    ->assertFalse($entity
    ->status());
  $this
    ->assertIdentical($entity
    ->uuid(), $second_uuid);

  // Perform an update.
  $import = <<<EOD
id: second
uuid: {<span class="php-variable">$second_uuid</span>}
label: 'Second updated'
weight: 0
style: ''
status: '0'
EOD;
  $edit = array(
    'config_type' => 'config_test',
    'import' => $import,
  );
  $this
    ->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
  $this
    ->assertRaw(t('Are you sure you want to update the %name @type?', array(
    '%name' => 'second',
    '@type' => 'test configuration',
  )));
  $this
    ->drupalPostForm(NULL, array(), t('Confirm'));
  $entity = $storage
    ->load('second');
  $this
    ->assertRaw(t('The configuration was imported successfully.'));
  $this
    ->assertIdentical($entity
    ->label(), 'Second updated');

  // Try to perform an update which adds missing dependencies.
  $import = <<<EOD
id: second
uuid: {<span class="php-variable">$second_uuid</span>}
label: 'Second updated'
weight: 0
style: ''
status: '0'
dependencies:
  module:
    - does_not_exist
EOD;
  $edit = array(
    'config_type' => 'config_test',
    'import' => $import,
  );
  $this
    ->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
  $this
    ->assertRaw(t('Configuration %name depends on the %owner module that will not be installed after import.', [
    '%name' => 'config_test.dynamic.second',
    '%owner' => 'does_not_exist',
  ]));
}