3 namespace Drupal\config\Tests;
5 use Drupal\Core\Archiver\Tar;
6 use Drupal\Core\Serialization\Yaml;
7 use Drupal\simpletest\WebTestBase;
10 * Tests the user interface for exporting configuration.
14 class ConfigExportUITest extends WebTestBase {
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.
48 $this->drupalPostForm('admin/config/development/configuration/full/export', [], t('Export'));
49 $this->assertResponse(200, 'User can access the download callback.');
51 // Test if header contains file name with hostname and timestamp.
52 $request = \Drupal::request();
53 $hostname = str_replace('.', '-', $request->getHttpHost());
54 $header_content_disposition = $this->drupalGetHeader('content-disposition');
55 $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);
56 $this->assertTrue($header_match, "Header with filename matches the expected format.");
58 // Get the archived binary file provided to user for download.
59 $archive_data = $this->getRawContent();
61 // Temporarily save the archive file.
62 $uri = file_unmanaged_save_data($archive_data, 'temporary://config.tar.gz');
64 // Extract the archive and verify it's not empty.
65 $file_path = file_directory_temp() . '/' . file_uri_target($uri);
66 $archiver = new Tar($file_path);
67 $archive_contents = $archiver->listContents();
68 $this->assert(!empty($archive_contents), 'Downloaded archive file is not empty.');
70 // Prepare the list of config files from active storage, see
71 // \Drupal\config\Controller\ConfigController::downloadExport().
72 $storage_active = $this->container->get('config.storage');
74 foreach ($storage_active->listAll() as $config_name) {
75 $config_files[] = $config_name . '.yml';
77 // Assert that the downloaded archive file contents are the same as the test
79 $this->assertIdentical($archive_contents, $config_files);
81 // Ensure the test configuration override is in effect but was not exported.
82 $this->assertIdentical(\Drupal::config('system.maintenance')->get('message'), 'Foo');
83 $archiver->extract(file_directory_temp(), ['system.maintenance.yml']);
84 $file_contents = file_get_contents(file_directory_temp() . '/' . 'system.maintenance.yml');
85 $exported = Yaml::decode($file_contents);
86 $this->assertNotIdentical($exported['message'], 'Foo');
88 // Check the single export form doesn't have "form-required" elements.
89 $this->drupalGet('admin/config/development/configuration/single/export');
90 $this->assertNoRaw('js-form-required form-required', 'No form required fields are found.');
92 // Ensure the temporary file is not available to users without the
94 $this->drupalLogout();
95 $this->drupalGet('system/temporary', ['query' => ['file' => 'config.tar.gz']]);
96 $this->assertResponse(403);