Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / config / tests / src / Functional / ConfigExportUITest.php
1 <?php
2
3 namespace Drupal\Tests\config\Functional;
4
5 use Drupal\Core\Archiver\Tar;
6 use Drupal\Core\Serialization\Yaml;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests the user interface for exporting configuration.
11  *
12  * @group config
13  */
14 class ConfigExportUITest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['config', 'config_test'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     // Set up an override.
30     $settings['config']['system.maintenance']['message'] = (object) [
31       'value' => 'Foo',
32       'required' => TRUE,
33     ];
34     $this->writeSettings($settings);
35
36     $this->drupalLogin($this->drupalCreateUser(['export configuration']));
37   }
38
39   /**
40    * Tests export of configuration.
41    */
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'));
46
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.');
51
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.");
58
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.');
64
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');
68     $config_files = [];
69     foreach ($storage_active->listAll() as $config_name) {
70       $config_files[] = $config_name . '.yml';
71     }
72     // Assert that the downloaded archive file contents are the same as the test
73     // site active store.
74     $this->assertIdentical($archive_contents, $config_files);
75
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');
82
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.');
86
87     // Ensure the temporary file is not available to users without the
88     // permission.
89     $this->drupalLogout();
90     $this->drupalGet('system/temporary', ['query' => ['file' => 'config.tar.gz']]);
91     $this->assertResponse(403);
92   }
93
94 }