function ConfigSingleImportExportTest::testImport

Same name in other branches
  1. 8.9.x core/modules/config/tests/src/Functional/ConfigSingleImportExportTest.php \Drupal\Tests\config\Functional\ConfigSingleImportExportTest::testImport()
  2. 10 core/modules/config/tests/src/Functional/ConfigSingleImportExportTest.php \Drupal\Tests\config\Functional\ConfigSingleImportExportTest::testImport()
  3. 11.x core/modules/config/tests/src/Functional/ConfigSingleImportExportTest.php \Drupal\Tests\config\Functional\ConfigSingleImportExportTest::testImport()

Tests importing a single configuration file.

File

core/modules/config/tests/src/Functional/ConfigSingleImportExportTest.php, line 46

Class

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

Namespace

Drupal\Tests\config\Functional

Code

public function testImport() {
    $storage = \Drupal::entityTypeManager()->getStorage('config_test');
    $uuid = \Drupal::service('uuid');
    $this->drupalLogin($this->drupalCreateUser([
        'import configuration',
    ]));
    // Attempt an import with invalid YAML.
    $edit = [
        'config_type' => 'action',
        'import' => '{{{',
    ];
    $this->drupalGet('admin/config/development/configuration/single/import');
    $this->submitForm($edit, 'Import');
    // Assert the static portion of the error since different parsers could give different text in their error.
    $this->assertSession()
        ->pageTextContains('The import failed with the following message: ');
    $import = <<<EOD
label: First
weight: 0
style: ''
status: '1'
EOD;
    $edit = [
        'config_type' => 'config_test',
        'import' => $import,
    ];
    // Attempt an import with a missing ID.
    $this->drupalGet('admin/config/development/configuration/single/import');
    $this->submitForm($edit, 'Import');
    $this->assertSession()
        ->pageTextContains('Missing ID key "id" for this Test configuration import.');
    // Perform an import with no specified UUID and a unique ID.
    $this->assertNull($storage->load('first'));
    $edit['import'] = "id: first\n" . $edit['import'];
    $this->drupalGet('admin/config/development/configuration/single/import');
    $this->submitForm($edit, 'Import');
    $this->assertSession()
        ->pageTextContains('Are you sure you want to create a new first test configuration?');
    $this->submitForm([], 'Confirm');
    $entity = $storage->load('first');
    $this->assertSame('First', $entity->label());
    $this->assertSame('first', $entity->id());
    $this->assertTrue($entity->status());
    $this->assertSession()
        ->pageTextContains('The configuration was imported successfully.');
    // Attempt an import with an existing ID but missing UUID.
    $this->drupalGet('admin/config/development/configuration/single/import');
    $this->submitForm($edit, 'Import');
    $this->assertSession()
        ->pageTextContains('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->drupalGet('admin/config/development/configuration/single/import');
    $this->submitForm($edit, 'Import');
    $this->assertSession()
        ->pageTextContains('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->drupalGet('admin/config/development/configuration/single/import');
    $this->submitForm($edit, 'Import');
    $this->assertSession()
        ->pageTextContains('Are you sure you want to create a new custom_id test configuration?');
    $this->submitForm([], 'Confirm');
    $this->assertSession()
        ->pageTextContains('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 = [
        'config_type' => 'config_test',
        'import' => $import,
    ];
    $second_uuid = $uuid->generate();
    $edit['import'] .= "\nuuid: " . $second_uuid;
    $this->drupalGet('admin/config/development/configuration/single/import');
    $this->submitForm($edit, 'Import');
    $this->assertSession()
        ->pageTextContains('Are you sure you want to create a new second test configuration?');
    $this->submitForm([], 'Confirm');
    $entity = $storage->load('second');
    $this->assertSession()
        ->pageTextContains('The configuration was imported successfully.');
    $this->assertSame('Second', $entity->label());
    $this->assertSame('second', $entity->id());
    $this->assertFalse($entity->status());
    $this->assertSame($second_uuid, $entity->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 = [
        'config_type' => 'config_test',
        'import' => $import,
    ];
    $this->drupalGet('admin/config/development/configuration/single/import');
    $this->submitForm($edit, 'Import');
    $this->assertSession()
        ->pageTextContains('Are you sure you want to update the second test configuration?');
    $this->submitForm([], 'Confirm');
    $entity = $storage->load('second');
    $this->assertSession()
        ->pageTextContains('The configuration was imported successfully.');
    $this->assertSame('Second updated', $entity->label());
    // 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 = [
        'config_type' => 'config_test',
        'import' => $import,
    ];
    $this->drupalGet('admin/config/development/configuration/single/import');
    $this->submitForm($edit, 'Import');
    $this->assertSession()
        ->pageTextContains('Configuration config_test.dynamic.second depends on the does_not_exist module that will not be installed after import.');
    // Try to preform an update which would create a PHP object if Yaml parsing
    // not securely set up.
    // Perform an update.
    $import = <<<EOD
id: second
uuid: {<span class="php-variable">$second_uuid</span>}
label: !php/object "O:36:\\"Drupal\\\\Core\\\\Test\\\\ObjectSerialization\\":0:{}"
weight: 0
style: ''
status: '0'
EOD;
    $edit = [
        'config_type' => 'config_test',
        'import' => $import,
    ];
    $this->drupalGet('admin/config/development/configuration/single/import');
    $this->submitForm($edit, 'Import');
    if (extension_loaded('yaml')) {
        // If the yaml extension is loaded it will work but not create the PHP
        // object.
        $this->assertSession()
            ->pageTextContains('Are you sure you want to update the second test configuration?');
        $this->submitForm([], 'Confirm');
        $entity = $storage->load('second');
        $this->assertSession()
            ->pageTextContains('The configuration was imported successfully.');
        $this->assertIsString($entity->label());
        $this->assertStringContainsString('ObjectSerialization', $entity->label(), 'Label contains serialized object');
    }
    else {
        // If the Symfony parser is used there will be an error.
        $this->assertSession()
            ->responseContains('The import failed with the following message:');
        $this->assertSession()
            ->responseContains('Object support when parsing a YAML file has been disabled');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.