InstallerExistingConfigTestBase.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Drupal\FunctionalTests\Installer;
  3. use Drupal\Component\Serialization\Yaml;
  4. use Drupal\Core\Archiver\ArchiveTar;
  5. use Drupal\Core\Installer\Form\SelectProfileForm;
  6. /**
  7. * Provides a base class for testing installing from existing configuration.
  8. */
  9. abstract class InstallerExistingConfigTestBase extends InstallerTestBase {
  10. /**
  11. * This is set by the profile in the core.extension extracted.
  12. */
  13. protected $profile = NULL;
  14. /**
  15. * @todo
  16. */
  17. protected $existingSyncDirectory = FALSE;
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function prepareEnvironment() {
  22. parent::prepareEnvironment();
  23. $archiver = new ArchiveTar($this->getConfigTarball(), 'gz');
  24. if ($this->profile === NULL) {
  25. $core_extension = Yaml::decode($archiver->extractInString('core.extension.yml'));
  26. $this->profile = $core_extension['profile'];
  27. }
  28. // Create a profile for testing. We set core_version_requirement to '*' for
  29. // the test so that it does not need to be updated between major versions.
  30. $info = [
  31. 'type' => 'profile',
  32. 'core_version_requirement' => '*',
  33. 'name' => 'Configuration installation test profile (' . $this->profile . ')',
  34. ];
  35. // File API functions are not available yet.
  36. $path = $this->siteDirectory . '/profiles/' . $this->profile;
  37. if ($this->existingSyncDirectory) {
  38. $config_sync_directory = $this->siteDirectory . '/config/sync';
  39. $this->settings['settings']['config_sync_directory'] = (object) [
  40. 'value' => $config_sync_directory,
  41. 'required' => TRUE,
  42. ];
  43. }
  44. else {
  45. // Put the sync directory inside the profile.
  46. $config_sync_directory = $path . '/config/sync';
  47. }
  48. mkdir($path, 0777, TRUE);
  49. file_put_contents("$path/{$this->profile}.info.yml", Yaml::encode($info));
  50. // Create config/sync directory and extract tarball contents to it.
  51. mkdir($config_sync_directory, 0777, TRUE);
  52. $files = [];
  53. $list = $archiver->listContent();
  54. if (is_array($list)) {
  55. /** @var array $list */
  56. foreach ($list as $file) {
  57. $files[] = $file['filename'];
  58. }
  59. $archiver->extractList($files, $config_sync_directory);
  60. }
  61. }
  62. /**
  63. * Gets the filepath to the configuration tarball.
  64. *
  65. * The tarball will be extracted to the install profile's config/sync
  66. * directory for testing.
  67. *
  68. * @return string
  69. * The filepath to the configuration tarball.
  70. */
  71. abstract protected function getConfigTarball();
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function installParameters() {
  76. $parameters = parent::installParameters();
  77. // The options that change configuration are disabled when installing from
  78. // existing configuration.
  79. unset($parameters['forms']['install_configure_form']['site_name']);
  80. unset($parameters['forms']['install_configure_form']['site_mail']);
  81. unset($parameters['forms']['install_configure_form']['update_status_module']);
  82. return $parameters;
  83. }
  84. /**
  85. * Confirms that the installation installed the configuration correctly.
  86. */
  87. public function testConfigSync() {
  88. // After installation there is no snapshot and nothing to import.
  89. $change_list = $this->configImporter()->getStorageComparer()->getChangelist();
  90. $expected = [
  91. 'create' => [],
  92. // The system.mail is changed configuration because the test system
  93. // changes it to ensure that mails are not sent.
  94. 'update' => ['system.mail'],
  95. 'delete' => [],
  96. 'rename' => [],
  97. ];
  98. $this->assertEqual($expected, $change_list);
  99. }
  100. /**
  101. * Installer step: Select installation profile.
  102. */
  103. protected function setUpProfile() {
  104. if ($this->existingSyncDirectory) {
  105. $edit = [
  106. 'profile' => SelectProfileForm::CONFIG_INSTALL_PROFILE_KEY,
  107. ];
  108. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  109. }
  110. else {
  111. parent::setUpProfile();
  112. }
  113. }
  114. }