InstallerTestBase.php 7.7 KB

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