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