db backup prior to drupal security update
[yaffs-website] / web / core / modules / config / src / Tests / ConfigExportUITest.php
1 <?php
2
3 namespace Drupal\config\Tests;
4
5 use Drupal\Core\Archiver\Tar;
6 use Drupal\Core\Serialization\Yaml;
7 use Drupal\simpletest\WebTestBase;
8
9 /**
10  * Tests the user interface for exporting configuration.
11  *
12  * @group config
13  */
14 class ConfigExportUITest extends WebTestBase {
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.
48     $this->drupalPostForm('admin/config/development/configuration/full/export', [], t('Export'));
49     $this->assertResponse(200, 'User can access the download callback.');
50
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.");
57
58     // Get the archived binary file provided to user for download.
59     $archive_data = $this->getRawContent();
60
61     // Temporarily save the archive file.
62     $uri = file_unmanaged_save_data($archive_data, 'temporary://config.tar.gz');
63
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.');
69
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');
73     $config_files = [];
74     foreach ($storage_active->listAll() as $config_name) {
75       $config_files[] = $config_name . '.yml';
76     }
77     // Assert that the downloaded archive file contents are the same as the test
78     // site active store.
79     $this->assertIdentical($archive_contents, $config_files);
80
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');
87
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.');
91
92     // Ensure the temporary file is not available to users without the
93     // permission.
94     $this->drupalLogout();
95     $this->drupalGet('system/temporary', ['query' => ['file' => 'config.tar.gz']]);
96     $this->assertResponse(403);
97   }
98
99 }