Version 1
[yaffs-website] / web / core / modules / system / src / Tests / Installer / DistributionProfileExistingSettingsTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Installer;
4
5 use Drupal\Component\Serialization\Yaml;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\DrupalKernel;
8 use Drupal\Core\Site\Settings;
9 use Drupal\simpletest\InstallerTestBase;
10 use Symfony\Component\HttpFoundation\Request;
11
12
13 /**
14  * Tests distribution profile support with existing settings.
15  *
16  * @group Installer
17  */
18 class DistributionProfileExistingSettingsTest extends InstallerTestBase {
19
20   /**
21    * The distribution profile info.
22    *
23    * @var array
24    */
25   protected $info;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     $this->info = [
32       'type' => 'profile',
33       'core' => \Drupal::CORE_COMPATIBILITY,
34       'name' => 'Distribution profile',
35       'distribution' => [
36         'name' => 'My Distribution',
37         'install' => [
38           'theme' => 'bartik',
39         ],
40       ],
41     ];
42     // File API functions are not available yet.
43     $path = $this->siteDirectory . '/profiles/mydistro';
44     mkdir($path, 0777, TRUE);
45     file_put_contents("$path/mydistro.info.yml", Yaml::encode($this->info));
46
47     // Pre-configure hash salt.
48     // Any string is valid, so simply use the class name of this test.
49     $this->settings['settings']['hash_salt'] = (object) [
50       'value' => __CLASS__,
51       'required' => TRUE,
52     ];
53
54     // Pre-configure database credentials.
55     $connection_info = Database::getConnectionInfo();
56     unset($connection_info['default']['pdo']);
57     unset($connection_info['default']['init_commands']);
58
59     $this->settings['databases']['default'] = (object) [
60       'value' => $connection_info,
61       'required' => TRUE,
62     ];
63
64     // Use the kernel to find the site path because the site.path service should
65     // not be available at this point in the install process.
66     $site_path = DrupalKernel::findSitePath(Request::createFromGlobals());
67     // Pre-configure config directories.
68     $this->settings['config_directories'] = [
69       CONFIG_SYNC_DIRECTORY => (object) [
70         'value' => $site_path . '/files/config_staging',
71         'required' => TRUE,
72       ],
73     ];
74     mkdir($this->settings['config_directories'][CONFIG_SYNC_DIRECTORY]->value, 0777, TRUE);
75     parent::setUp();
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   protected function setUpLanguage() {
82     // Make settings file not writable.
83     $filename = $this->siteDirectory . '/settings.php';
84     // Make the settings file read-only.
85     // Not using File API; a potential error must trigger a PHP warning.
86     chmod($filename, 0444);
87
88     // Verify that the distribution name appears.
89     $this->assertRaw($this->info['distribution']['name']);
90     // Verify that the requested theme is used.
91     $this->assertRaw($this->info['distribution']['install']['theme']);
92     // Verify that the "Choose profile" step does not appear.
93     $this->assertNoText('profile');
94
95     parent::setUpLanguage();
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   protected function setUpProfile() {
102     // This step is skipped, because there is a distribution profile.
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   protected function setUpSettings() {
109     // This step should not appear, since settings.php is fully configured
110     // already.
111   }
112
113   /**
114    * Confirms that the installation succeeded.
115    */
116   public function testInstalled() {
117     $this->assertUrl('user/1');
118     $this->assertResponse(200);
119     // Confirm that we are logged-in after installation.
120     $this->assertText($this->rootUser->getUsername());
121
122     // Confirm that Drupal recognizes this distribution as the current profile.
123     $this->assertEqual(\Drupal::installProfile(), 'mydistro');
124     $this->assertNull(Settings::get('install_profile'), 'The install profile has not been written to settings.php.');
125     $this->assertEqual($this->config('core.extension')->get('profile'), 'mydistro', 'The install profile has been written to core.extension configuration.');
126
127     $this->rebuildContainer();
128     $this->pass('Container can be rebuilt even though distribution is not written to settings.php.');
129     $this->assertEqual(\Drupal::installProfile(), 'mydistro');
130   }
131
132 }