Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / config / tests / src / Functional / ConfigSingleImportExportTest.php
1 <?php
2
3 namespace Drupal\Tests\config\Functional;
4
5 use Drupal\Core\Serialization\Yaml;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests the user interface for importing/exporting a single configuration.
10  *
11  * @group config
12  */
13 class ConfigSingleImportExportTest extends BrowserTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = [
21     'block',
22     'config',
23     'config_test',
24     // Adding language module makes it possible to involve non-default
25     // (language.xx) collections in import/export operations.
26     'language',
27   ];
28
29   protected function setUp() {
30     parent::setUp();
31
32     $this->drupalPlaceBlock('page_title_block');
33   }
34
35   /**
36    * Tests importing a single configuration file.
37    */
38   public function testImport() {
39     $storage = \Drupal::entityManager()->getStorage('config_test');
40     $uuid = \Drupal::service('uuid');
41
42     $this->drupalLogin($this->drupalCreateUser(['import configuration']));
43
44     // Attempt an import with invalid YAML.
45     $edit = [
46       'config_type' => 'action',
47       'import' => '{{{',
48     ];
49
50     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
51     // Assert the static portion of the error since different parsers could give different text in their error.
52     $this->assertText('The import failed with the following message: ');
53
54     $import = <<<EOD
55 label: First
56 weight: 0
57 style: ''
58 status: '1'
59 EOD;
60     $edit = [
61       'config_type' => 'config_test',
62       'import' => $import,
63     ];
64     // Attempt an import with a missing ID.
65     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
66     $this->assertText(t('Missing ID key "@id_key" for this @entity_type import.', ['@id_key' => 'id', '@entity_type' => 'Test configuration']));
67
68     // Perform an import with no specified UUID and a unique ID.
69     $this->assertNull($storage->load('first'));
70     $edit['import'] = "id: first\n" . $edit['import'];
71     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
72     $this->assertRaw(t('Are you sure you want to create a new %name @type?', ['%name' => 'first', '@type' => 'test configuration']));
73     $this->drupalPostForm(NULL, [], t('Confirm'));
74     $entity = $storage->load('first');
75     $this->assertIdentical($entity->label(), 'First');
76     $this->assertIdentical($entity->id(), 'first');
77     $this->assertTrue($entity->status());
78     $this->assertRaw(t('The configuration was imported successfully.'));
79
80     // Attempt an import with an existing ID but missing UUID.
81     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
82     $this->assertText(t('An entity with this machine name already exists but the import did not specify a UUID.'));
83
84     // Attempt an import with a mismatched UUID and existing ID.
85     $edit['import'] .= "\nuuid: " . $uuid->generate();
86     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
87     $this->assertText(t('An entity with this machine name already exists but the UUID does not match.'));
88
89     // Attempt an import with a custom ID.
90     $edit['custom_entity_id'] = 'custom_id';
91     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
92     $this->assertRaw(t('Are you sure you want to create a new %name @type?', ['%name' => 'custom_id', '@type' => 'test configuration']));
93     $this->drupalPostForm(NULL, [], t('Confirm'));
94     $this->assertRaw(t('The configuration was imported successfully.'));
95
96     // Perform an import with a unique ID and UUID.
97     $import = <<<EOD
98 id: second
99 label: Second
100 weight: 0
101 style: ''
102 status: '0'
103 EOD;
104     $edit = [
105       'config_type' => 'config_test',
106       'import' => $import,
107     ];
108     $second_uuid = $uuid->generate();
109     $edit['import'] .= "\nuuid: " . $second_uuid;
110     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
111     $this->assertRaw(t('Are you sure you want to create a new %name @type?', ['%name' => 'second', '@type' => 'test configuration']));
112     $this->drupalPostForm(NULL, [], t('Confirm'));
113     $entity = $storage->load('second');
114     $this->assertRaw(t('The configuration was imported successfully.'));
115     $this->assertIdentical($entity->label(), 'Second');
116     $this->assertIdentical($entity->id(), 'second');
117     $this->assertFalse($entity->status());
118     $this->assertIdentical($entity->uuid(), $second_uuid);
119
120     // Perform an update.
121     $import = <<<EOD
122 id: second
123 uuid: $second_uuid
124 label: 'Second updated'
125 weight: 0
126 style: ''
127 status: '0'
128 EOD;
129     $edit = [
130       'config_type' => 'config_test',
131       'import' => $import,
132     ];
133     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
134     $this->assertRaw(t('Are you sure you want to update the %name @type?', ['%name' => 'second', '@type' => 'test configuration']));
135     $this->drupalPostForm(NULL, [], t('Confirm'));
136     $entity = $storage->load('second');
137     $this->assertRaw(t('The configuration was imported successfully.'));
138     $this->assertIdentical($entity->label(), 'Second updated');
139
140     // Try to perform an update which adds missing dependencies.
141     $import = <<<EOD
142 id: second
143 uuid: $second_uuid
144 label: 'Second updated'
145 weight: 0
146 style: ''
147 status: '0'
148 dependencies:
149   module:
150     - does_not_exist
151 EOD;
152     $edit = [
153       'config_type' => 'config_test',
154       'import' => $import,
155     ];
156     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
157     $this->assertRaw(t('Configuration %name depends on the %owner module that will not be installed after import.', ['%name' => 'config_test.dynamic.second', '%owner' => 'does_not_exist']));
158
159     // Try to preform an update which would create a PHP object if Yaml parsing
160     // not securely set up.
161     // Perform an update.
162     $import = <<<EOD
163 id: second
164 uuid: $second_uuid
165 label: !php/object "O:36:\"Drupal\\\Core\\\Test\\\ObjectSerialization\":0:{}"
166 weight: 0
167 style: ''
168 status: '0'
169 EOD;
170     $edit = [
171       'config_type' => 'config_test',
172       'import' => $import,
173     ];
174     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
175     if (extension_loaded('yaml')) {
176       // If the yaml extension is loaded it will work but not create the PHP
177       // object.
178       $this->assertRaw(t('Are you sure you want to update the %name @type?', [
179         '%name' => 'second',
180         '@type' => 'test configuration',
181       ]));
182       $this->drupalPostForm(NULL, [], t('Confirm'));
183       $entity = $storage->load('second');
184       $this->assertRaw(t('The configuration was imported successfully.'));
185       $this->assertTrue(is_string($entity->label()), 'Entity label is a string');
186       $this->assertTrue(strpos($entity->label(), 'ObjectSerialization') > 0, 'Label contains serialized object');
187     }
188     else {
189       // If the Symfony parser is used there will be an error.
190       $this->assertSession()->responseContains('The import failed with the following message:');
191       $this->assertSession()->responseContains('Object support when parsing a YAML file has been disabled');
192     }
193   }
194
195   /**
196    * Tests importing a simple configuration file.
197    */
198   public function testImportSimpleConfiguration() {
199     $this->drupalLogin($this->drupalCreateUser(['import configuration']));
200     $config = $this->config('system.site')->set('name', 'Test simple import');
201
202     // Place branding block with site name into header region.
203     $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);
204
205     $edit = [
206       'config_type' => 'system.simple',
207       'config_name' => $config->getName(),
208       'import' => Yaml::encode($config->get()),
209     ];
210     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
211     $this->assertRaw(t('Are you sure you want to update the %name @type?', ['%name' => $config->getName(), '@type' => 'simple configuration']));
212     $this->drupalPostForm(NULL, [], t('Confirm'));
213     $this->drupalGet('');
214     $this->assertText('Test simple import');
215
216     // Ensure that ConfigImporter validation is running when importing simple
217     // configuration.
218     $config_data = $this->config('core.extension')->get();
219     // Simulate uninstalling the Config module.
220     unset($config_data['module']['config']);
221     $edit = [
222       'config_type' => 'system.simple',
223       'config_name' => 'core.extension',
224       'import' => Yaml::encode($config_data),
225     ];
226     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
227     $this->assertText(t('Can not uninstall the Configuration module as part of a configuration synchronization through the user interface.'));
228
229   }
230
231   /**
232    * Tests exporting a single configuration file.
233    */
234   public function testExport() {
235     $this->drupalLogin($this->drupalCreateUser(['export configuration']));
236
237     $this->drupalGet('admin/config/development/configuration/single/export/system.simple');
238     $this->assertFieldByXPath('//select[@name="config_type"]//option[@selected="selected"]', t('Simple configuration'), 'The simple configuration option is selected when specified in the URL.');
239     // Spot check several known simple configuration files.
240     $element = $this->xpath('//select[@name="config_name"]')[0];
241     $options = $element->findAll('css', 'option');
242     $expected_options = ['system.site', 'user.settings'];
243     foreach ($options as &$option) {
244       $option = $option->getValue();
245     }
246     $this->assertIdentical($expected_options, array_intersect($expected_options, $options), 'The expected configuration files are listed.');
247
248     $this->drupalGet('admin/config/development/configuration/single/export/system.simple/system.image');
249     $this->assertEquals("toolkit: gd\n_core:\n  default_config_hash: durWHaKeBaq4d9Wpi4RqwADj1OufDepcnJuhVLmKN24\n", $this->xpath('//textarea[@name="export"]')[0]->getValue(), 'The expected system configuration is displayed.');
250
251     $this->drupalGet('admin/config/development/configuration/single/export/date_format');
252     $this->assertFieldByXPath('//select[@name="config_type"]//option[@selected="selected"]', t('Date format'), 'The date format entity type is selected when specified in the URL.');
253
254     $this->drupalGet('admin/config/development/configuration/single/export/date_format/fallback');
255     $this->assertFieldByXPath('//select[@name="config_name"]//option[@selected="selected"]', t('Fallback date format (fallback)'), 'The fallback date format config entity is selected when specified in the URL.');
256
257     $fallback_date = \Drupal::entityManager()->getStorage('date_format')->load('fallback');
258     $yaml_text = $this->xpath('//textarea[@name="export"]')[0]->getValue();
259     $this->assertEqual(Yaml::decode($yaml_text), $fallback_date->toArray(), 'The fallback date format config entity export code is displayed.');
260   }
261
262 }