ConfigSingleImportExportTest.php 11 KB

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