607e80c23b016ee61b868bf52b0831958a926f91
[yaffs-website] / web / core / modules / config / src / Tests / ConfigSingleImportExportTest.php
1 <?php
2
3 namespace Drupal\config\Tests;
4
5 use Drupal\Core\Serialization\Yaml;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests the user interface for importing/exporting a single configuration.
10  *
11  * @group config
12  */
13 class ConfigSingleImportExportTest extends WebTestBase {
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     $this->assertRaw(t('Are you sure you want to update the %name @type?', ['%name' => 'second', '@type' => 'test configuration']));
176     $this->drupalPostForm(NULL, [], t('Confirm'));
177     $entity = $storage->load('second');
178     $this->assertRaw(t('The configuration was imported successfully.'));
179     $this->assertTrue(is_string($entity->label()), 'Entity label is a string');
180     $this->assertTrue(strpos($entity->label(), 'ObjectSerialization') > 0, 'Label contains serialized object');
181   }
182
183   /**
184    * Tests importing a simple configuration file.
185    */
186   public function testImportSimpleConfiguration() {
187     $this->drupalLogin($this->drupalCreateUser(['import configuration']));
188     $config = $this->config('system.site')->set('name', 'Test simple import');
189
190     // Place branding block with site name into header region.
191     $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);
192
193     $edit = [
194       'config_type' => 'system.simple',
195       'config_name' => $config->getName(),
196       'import' => Yaml::encode($config->get()),
197     ];
198     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
199     $this->assertRaw(t('Are you sure you want to update the %name @type?', ['%name' => $config->getName(), '@type' => 'simple configuration']));
200     $this->drupalPostForm(NULL, [], t('Confirm'));
201     $this->drupalGet('');
202     $this->assertText('Test simple import');
203
204     // Ensure that ConfigImporter validation is running when importing simple
205     // configuration.
206     $config_data = $this->config('core.extension')->get();
207     // Simulate uninstalling the Config module.
208     unset($config_data['module']['config']);
209     $edit = [
210       'config_type' => 'system.simple',
211       'config_name' => 'core.extension',
212       'import' => Yaml::encode($config_data),
213     ];
214     $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
215     $this->assertText(t('Can not uninstall the Configuration module as part of a configuration synchronization through the user interface.'));
216
217   }
218
219   /**
220    * Tests exporting a single configuration file.
221    */
222   public function testExport() {
223     $this->drupalLogin($this->drupalCreateUser(['export configuration']));
224
225     $this->drupalGet('admin/config/development/configuration/single/export/system.simple');
226     $this->assertFieldByXPath('//select[@name="config_type"]//option[@selected="selected"]', t('Simple configuration'), 'The simple configuration option is selected when specified in the URL.');
227     // Spot check several known simple configuration files.
228     $element = $this->xpath('//select[@name="config_name"]');
229     $options = $this->getAllOptions($element[0]);
230     $expected_options = ['system.site', 'user.settings'];
231     foreach ($options as &$option) {
232       $option = (string) $option;
233     }
234     $this->assertIdentical($expected_options, array_intersect($expected_options, $options), 'The expected configuration files are listed.');
235
236     $this->drupalGet('admin/config/development/configuration/single/export/system.simple/system.image');
237     $this->assertFieldByXPath('//textarea[@name="export"]', "toolkit: gd\n_core:\n  default_config_hash: durWHaKeBaq4d9Wpi4RqwADj1OufDepcnJuhVLmKN24\n", 'The expected system configuration is displayed.');
238
239     $this->drupalGet('admin/config/development/configuration/single/export/date_format');
240     $this->assertFieldByXPath('//select[@name="config_type"]//option[@selected="selected"]', t('Date format'), 'The date format entity type is selected when specified in the URL.');
241
242     $this->drupalGet('admin/config/development/configuration/single/export/date_format/fallback');
243     $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.');
244
245     $fallback_date = \Drupal::entityManager()->getStorage('date_format')->load('fallback');
246     $yaml_text = (string) $this->xpath('//textarea[@name="export"]')[0];
247     $this->assertEqual(Yaml::decode($yaml_text), $fallback_date->toArray(), 'The fallback date format config entity export code is displayed.');
248   }
249
250 }