InstallerTestBase.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Drupal\simpletest;
  3. use Drupal\Core\DrupalKernel;
  4. use Drupal\Core\Language\Language;
  5. use Drupal\Core\Session\UserSession;
  6. use Drupal\Core\Site\Settings;
  7. use Symfony\Component\DependencyInjection\ContainerBuilder;
  8. use Symfony\Component\DependencyInjection\Reference;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. /**
  12. * Base class for testing the interactive installer.
  13. */
  14. abstract class InstallerTestBase extends WebTestBase {
  15. /**
  16. * Custom settings.php values to write for a test run.
  17. *
  18. * @var array
  19. * An array of settings to write out, in the format expected by
  20. * drupal_rewrite_settings().
  21. */
  22. protected $settings = [];
  23. /**
  24. * The language code in which to install Drupal.
  25. *
  26. * @var string
  27. */
  28. protected $langcode = 'en';
  29. /**
  30. * The installation profile to install.
  31. *
  32. * @var string
  33. */
  34. protected $profile = 'testing';
  35. /**
  36. * Additional parameters to use for installer screens.
  37. *
  38. * @see WebTestBase::installParameters()
  39. *
  40. * @var array
  41. */
  42. protected $parameters = [];
  43. /**
  44. * A string translation map used for translated installer screens.
  45. *
  46. * Keys are English strings, values are translated strings.
  47. *
  48. * @var array
  49. */
  50. protected $translations = [
  51. 'Save and continue' => 'Save and continue',
  52. ];
  53. /**
  54. * Whether the installer has completed.
  55. *
  56. * @var bool
  57. */
  58. protected $isInstalled = FALSE;
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function setUp() {
  63. $this->isInstalled = FALSE;
  64. // Define information about the user 1 account.
  65. $this->rootUser = new UserSession([
  66. 'uid' => 1,
  67. 'name' => 'admin',
  68. 'mail' => 'admin@example.com',
  69. 'pass_raw' => $this->randomMachineName(),
  70. ]);
  71. // If any $settings are defined for this test, copy and prepare an actual
  72. // settings.php, so as to resemble a regular installation.
  73. if (!empty($this->settings)) {
  74. // Not using File API; a potential error must trigger a PHP warning.
  75. copy(DRUPAL_ROOT . '/sites/default/default.settings.php', DRUPAL_ROOT . '/' . $this->siteDirectory . '/settings.php');
  76. $this->writeSettings($this->settings);
  77. }
  78. // Note that WebTestBase::installParameters() returns form input values
  79. // suitable for a programmed \Drupal::formBuilder()->submitForm().
  80. // @see WebTestBase::translatePostValues()
  81. $this->parameters = $this->installParameters();
  82. // Set up a minimal container (required by WebTestBase). Set cookie and
  83. // server information so that XDebug works.
  84. // @see install_begin_request()
  85. $request = Request::create($GLOBALS['base_url'] . '/core/install.php', 'GET', [], $_COOKIE, [], $_SERVER);
  86. $this->container = new ContainerBuilder();
  87. $request_stack = new RequestStack();
  88. $request_stack->push($request);
  89. $this->container
  90. ->set('request_stack', $request_stack);
  91. $this->container
  92. ->setParameter('language.default_values', Language::$defaultValues);
  93. $this->container
  94. ->register('language.default', 'Drupal\Core\Language\LanguageDefault')
  95. ->addArgument('%language.default_values%');
  96. $this->container
  97. ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
  98. ->addArgument(new Reference('language.default'));
  99. $this->container
  100. ->set('app.root', DRUPAL_ROOT);
  101. \Drupal::setContainer($this->container);
  102. $this->visitInstaller();
  103. // Select language.
  104. $this->setUpLanguage();
  105. // Select profile.
  106. $this->setUpProfile();
  107. // Configure settings.
  108. $this->setUpSettings();
  109. // @todo Allow test classes based on this class to act on further installer
  110. // screens.
  111. // Configure site.
  112. $this->setUpSite();
  113. if ($this->isInstalled) {
  114. // Import new settings.php written by the installer.
  115. $request = Request::createFromGlobals();
  116. $class_loader = require $this->container->get('app.root') . '/autoload.php';
  117. Settings::initialize($this->container->get('app.root'), DrupalKernel::findSitePath($request), $class_loader);
  118. foreach ($GLOBALS['config_directories'] as $type => $path) {
  119. $this->configDirectories[$type] = $path;
  120. }
  121. // After writing settings.php, the installer removes write permissions
  122. // from the site directory. To allow drupal_generate_test_ua() to write
  123. // a file containing the private key for drupal_valid_test_ua(), the site
  124. // directory has to be writable.
  125. // WebTestBase::tearDown() will delete the entire test site directory.
  126. // Not using File API; a potential error must trigger a PHP warning.
  127. chmod($this->container->get('app.root') . '/' . $this->siteDirectory, 0777);
  128. $this->kernel = DrupalKernel::createFromRequest($request, $class_loader, 'prod', FALSE);
  129. $this->kernel->prepareLegacyRequest($request);
  130. $this->container = $this->kernel->getContainer();
  131. // Manually configure the test mail collector implementation to prevent
  132. // tests from sending out emails and collect them in state instead.
  133. $this->container->get('config.factory')
  134. ->getEditable('system.mail')
  135. ->set('interface.default', 'test_mail_collector')
  136. ->save();
  137. }
  138. }
  139. /**
  140. * Visits the interactive installer.
  141. */
  142. protected function visitInstaller() {
  143. $this->drupalGet($GLOBALS['base_url'] . '/core/install.php');
  144. }
  145. /**
  146. * Installer step: Select language.
  147. */
  148. protected function setUpLanguage() {
  149. $edit = [
  150. 'langcode' => $this->langcode,
  151. ];
  152. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  153. }
  154. /**
  155. * Installer step: Select installation profile.
  156. */
  157. protected function setUpProfile() {
  158. $edit = [
  159. 'profile' => $this->profile,
  160. ];
  161. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  162. }
  163. /**
  164. * Installer step: Configure settings.
  165. */
  166. protected function setUpSettings() {
  167. $edit = $this->translatePostValues($this->parameters['forms']['install_settings_form']);
  168. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  169. }
  170. /**
  171. * Final installer step: Configure site.
  172. */
  173. protected function setUpSite() {
  174. $edit = $this->translatePostValues($this->parameters['forms']['install_configure_form']);
  175. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  176. // If we've got to this point the site is installed using the regular
  177. // installation workflow.
  178. $this->isInstalled = TRUE;
  179. }
  180. /**
  181. * {@inheritdoc}
  182. *
  183. * WebTestBase::refreshVariables() tries to operate on persistent storage,
  184. * which is only available after the installer completed.
  185. */
  186. protected function refreshVariables() {
  187. if ($this->isInstalled) {
  188. parent::refreshVariables();
  189. }
  190. }
  191. }