DistributionProfileExistingSettingsTest.php 3.9 KB

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