ConfigExportUITest.php in Drupal 9
File
core/modules/config/tests/src/Functional/ConfigExportUITest.php
View source
<?php
namespace Drupal\Tests\config\Functional;
use Drupal\Core\Archiver\Tar;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Tests\BrowserTestBase;
class ConfigExportUITest extends BrowserTestBase {
protected static $modules = [
'config',
'config_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$settings['config']['system.maintenance']['message'] = (object) [
'value' => 'Foo',
'required' => TRUE,
];
$this
->writeSettings($settings);
$this
->drupalLogin($this
->drupalCreateUser([
'export configuration',
]));
}
public function testExport() {
$this
->drupalGet('admin/config/development/configuration/full/export');
$this
->assertSession()
->buttonExists('Export');
$this
->drupalGet('admin/config/development/configuration/full/export');
$this
->submitForm([], 'Export');
$this
->assertSession()
->statusCodeEquals(200);
$request = \Drupal::request();
$hostname = str_replace('.', '-', $request
->getHttpHost());
$this
->assertSession()
->responseHeaderMatches('content-disposition', '/attachment; filename="config-' . preg_quote($hostname) . '-\\d{4}-\\d{2}-\\d{2}-\\d{2}-\\d{2}\\.tar\\.gz"/');
$file_system = \Drupal::service('file_system');
assert($file_system instanceof FileSystemInterface);
$temp_directory = $file_system
->getTempDirectory();
$file_path = $temp_directory . '/config.tar.gz';
$archiver = new Tar($file_path);
$archive_contents = $archiver
->listContents();
$this
->assertNotEmpty($archive_contents, 'Downloaded archive file is not empty.');
$storage_active = $this->container
->get('config.storage');
$config_files = [];
foreach ($storage_active
->listAll() as $config_name) {
$config_files[] = $config_name . '.yml';
}
$this
->assertSame($config_files, $archive_contents);
$this
->assertSame('Foo', \Drupal::config('system.maintenance')
->get('message'));
$archiver
->extract($temp_directory, [
'system.maintenance.yml',
]);
$file_contents = file_get_contents($temp_directory . '/' . 'system.maintenance.yml');
$exported = Yaml::decode($file_contents);
$this
->assertNotSame('Foo', $exported['message']);
$this
->drupalGet('admin/config/development/configuration/single/export');
$this
->assertSession()
->responseNotContains('js-form-required form-required');
$this
->drupalLogout();
$this
->drupalGet('system/temporary', [
'query' => [
'file' => 'config.tar.gz',
],
]);
$this
->assertSession()
->statusCodeEquals(403);
}
}