function ConfigExportImportUITest::testExportImport

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

Tests a simple site export import case.

File

core/modules/config/tests/src/Functional/ConfigExportImportUITest.php, line 91

Class

ConfigExportImportUITest
Tests the user interface for importing/exporting configuration.

Namespace

Drupal\Tests\config\Functional

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->assertEquals($this->newSlogan, $this->config('system.site')
        ->get('slogan'));
    // Create a content type.
    $this->contentType = $this->drupalCreateContentType();
    // Create a field.
    $this->fieldName = mb_strtolower($this->randomMachineName());
    $this->fieldStorage = FieldStorageConfig::create([
        'field_name' => $this->fieldName,
        'entity_type' => 'node',
        'type' => 'text',
    ]);
    $this->fieldStorage
        ->save();
    FieldConfig::create([
        'field_storage' => $this->fieldStorage,
        'bundle' => $this->contentType
            ->id(),
    ])
        ->save();
    $display_repository = $this->container
        ->get('entity_display.repository');
    // Update the displays so that configuration does not change unexpectedly on
    // import.
    $display_repository->getFormDisplay('node', $this->contentType
        ->id(), 'default')
        ->setComponent($this->fieldName, [
        'type' => 'text_textfield',
    ])
        ->save();
    $display_repository->getViewDisplay('node', $this->contentType
        ->id(), 'full')
        ->setComponent($this->fieldName)
        ->save();
    $display_repository->getViewDisplay('node', $this->contentType
        ->id(), 'default')
        ->setComponent($this->fieldName)
        ->save();
    $display_repository->getViewDisplay('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', [], 'Export');
    $this->tarball = $this->getSession()
        ->getPage()
        ->getContent();
    $this->config('system.site')
        ->set('slogan', $this->originalSlogan)
        ->save();
    $this->assertEquals($this->originalSlogan, $this->config('system.site')
        ->get('slogan'));
    // 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', [
        '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, [], '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->assertEquals($this->newSlogan, $this->config('system.site')
        ->get('slogan'));
    $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>');
}

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