InstallerExistingConfigTestBase.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.
  29. $info = [
  30. 'type' => 'profile',
  31. 'core' => \Drupal::CORE_COMPATIBILITY,
  32. 'name' => 'Configuration installation test profile (' . $this->profile . ')',
  33. ];
  34. // File API functions are not available yet.
  35. $path = $this->siteDirectory . '/profiles/' . $this->profile;
  36. if ($this->existingSyncDirectory) {
  37. $config_sync_directory = $this->siteDirectory . '/config/sync';
  38. $this->settings['config_directories'][CONFIG_SYNC_DIRECTORY] = (object) [
  39. 'value' => $config_sync_directory,
  40. 'required' => TRUE,
  41. ];
  42. }
  43. else {
  44. // Put the sync directory inside the profile.
  45. $config_sync_directory = $path . '/config/sync';
  46. }
  47. mkdir($path, 0777, TRUE);
  48. file_put_contents("$path/{$this->profile}.info.yml", Yaml::encode($info));
  49. // Create config/sync directory and extract tarball contents to it.
  50. mkdir($config_sync_directory, 0777, TRUE);
  51. $files = [];
  52. $list = $archiver->listContent();
  53. if (is_array($list)) {
  54. /** @var array $list */
  55. foreach ($list as $file) {
  56. $files[] = $file['filename'];
  57. }
  58. $archiver->extractList($files, $config_sync_directory);
  59. }
  60. }
  61. /**
  62. * Gets the filepath to the configuration tarball.
  63. *
  64. * The tarball will be extracted to the install profile's config/sync
  65. * directory for testing.
  66. *
  67. * @return string
  68. * The filepath to the configuration tarball.
  69. */
  70. abstract protected function getConfigTarball();
  71. /**
  72. * {@inheritdoc}
  73. */
  74. protected function installParameters() {
  75. $parameters = parent::installParameters();
  76. // The options that change configuration are disabled when installing from
  77. // existing configuration.
  78. unset($parameters['forms']['install_configure_form']['site_name']);
  79. unset($parameters['forms']['install_configure_form']['site_mail']);
  80. unset($parameters['forms']['install_configure_form']['update_status_module']);
  81. return $parameters;
  82. }
  83. /**
  84. * Confirms that the installation installed the configuration correctly.
  85. */
  86. public function testConfigSync() {
  87. // After installation there is no snapshot and nothing to import.
  88. $change_list = $this->configImporter()->getStorageComparer()->getChangelist();
  89. $expected = [
  90. 'create' => [],
  91. // The system.mail is changed configuration because the test system
  92. // changes it to ensure that mails are not sent.
  93. 'update' => ['system.mail'],
  94. 'delete' => [],
  95. 'rename' => [],
  96. ];
  97. $this->assertEqual($expected, $change_list);
  98. }
  99. /**
  100. * Installer step: Select installation profile.
  101. */
  102. protected function setUpProfile() {
  103. if ($this->existingSyncDirectory) {
  104. $edit = [
  105. 'profile' => SelectProfileForm::CONFIG_INSTALL_PROFILE_KEY,
  106. ];
  107. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  108. }
  109. else {
  110. parent::setUpProfile();
  111. }
  112. }
  113. }