3 namespace Drupal\Tests\config\Functional;
5 use Drupal\Core\Archiver\Tar;
6 use Drupal\Core\Serialization\Yaml;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests the user interface for exporting configuration.
14 class ConfigExportUITest extends BrowserTestBase {
21 public static $modules = ['config', 'config_test'];
26 protected function setUp() {
29 // Set up an override.
30 $settings['config']['system.maintenance']['message'] = (object) [
34 $this->writeSettings($settings);
36 $this->drupalLogin($this->drupalCreateUser(['export configuration']));
40 * Tests export of configuration.
42 public function testExport() {
43 // Verify the export page with export submit button is available.
44 $this->drupalGet('admin/config/development/configuration/full/export');
45 $this->assertFieldById('edit-submit', t('Export'));
47 // Submit the export form and verify response. This will create a file in
48 // temporary directory with the default name config.tar.gz.
49 $this->drupalPostForm('admin/config/development/configuration/full/export', [], t('Export'));
50 $this->assertResponse(200, 'User can access the download callback.');
52 // Test if header contains file name with hostname and timestamp.
53 $request = \Drupal::request();
54 $hostname = str_replace('.', '-', $request->getHttpHost());
55 $header_content_disposition = $this->drupalGetHeader('content-disposition');
56 $header_match = (boolean) preg_match('/attachment; filename="config-' . preg_quote($hostname) . '-\d{4}-\d{2}-\d{2}-\d{2}-\d{2}\.tar\.gz"/', $header_content_disposition);
57 $this->assertTrue($header_match, "Header with filename matches the expected format.");
59 // Extract the archive and verify it's not empty.
60 $file_path = file_directory_temp() . '/' . 'config.tar.gz';
61 $archiver = new Tar($file_path);
62 $archive_contents = $archiver->listContents();
63 $this->assert(!empty($archive_contents), 'Downloaded archive file is not empty.');
65 // Prepare the list of config files from active storage, see
66 // \Drupal\config\Controller\ConfigController::downloadExport().
67 $storage_active = $this->container->get('config.storage');
69 foreach ($storage_active->listAll() as $config_name) {
70 $config_files[] = $config_name . '.yml';
72 // Assert that the downloaded archive file contents are the same as the test
74 $this->assertIdentical($archive_contents, $config_files);
76 // Ensure the test configuration override is in effect but was not exported.
77 $this->assertIdentical(\Drupal::config('system.maintenance')->get('message'), 'Foo');
78 $archiver->extract(file_directory_temp(), ['system.maintenance.yml']);
79 $file_contents = file_get_contents(file_directory_temp() . '/' . 'system.maintenance.yml');
80 $exported = Yaml::decode($file_contents);
81 $this->assertNotIdentical($exported['message'], 'Foo');
83 // Check the single export form doesn't have "form-required" elements.
84 $this->drupalGet('admin/config/development/configuration/single/export');
85 $this->assertNoRaw('js-form-required form-required', 'No form required fields are found.');
87 // Ensure the temporary file is not available to users without the
89 $this->drupalLogout();
90 $this->drupalGet('system/temporary', ['query' => ['file' => 'config.tar.gz']]);
91 $this->assertResponse(403);