InstallerTestBase.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 Symfony\Component\DependencyInjection\ContainerBuilder;
  9. use Symfony\Component\DependencyInjection\Reference;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. /**
  13. * Base class for testing the interactive installer.
  14. *
  15. * @deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0.
  16. * Use \Drupal\FunctionalTests\Installer\InstallerTestBase. See
  17. * https://www.drupal.org/node/2988752
  18. */
  19. abstract class InstallerTestBase extends WebTestBase {
  20. /**
  21. * Custom settings.php values to write for a test run.
  22. *
  23. * @var array
  24. * An array of settings to write out, in the format expected by
  25. * drupal_rewrite_settings().
  26. */
  27. protected $settings = [];
  28. /**
  29. * The language code in which to install Drupal.
  30. *
  31. * @var string
  32. */
  33. protected $langcode = 'en';
  34. /**
  35. * The installation profile to install.
  36. *
  37. * @var string
  38. */
  39. protected $profile = 'testing';
  40. /**
  41. * Additional parameters to use for installer screens.
  42. *
  43. * @see WebTestBase::installParameters()
  44. *
  45. * @var array
  46. */
  47. protected $parameters = [];
  48. /**
  49. * A string translation map used for translated installer screens.
  50. *
  51. * Keys are English strings, values are translated strings.
  52. *
  53. * @var array
  54. */
  55. protected $translations = [
  56. 'Save and continue' => 'Save and continue',
  57. ];
  58. /**
  59. * Whether the installer has completed.
  60. *
  61. * @var bool
  62. */
  63. protected $isInstalled = FALSE;
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function setUp() {
  68. $this->isInstalled = FALSE;
  69. // Define information about the user 1 account.
  70. $this->rootUser = new UserSession([
  71. 'uid' => 1,
  72. 'name' => 'admin',
  73. 'mail' => 'admin@example.com',
  74. 'pass_raw' => $this->randomMachineName(),
  75. ]);
  76. // If any $settings are defined for this test, copy and prepare an actual
  77. // settings.php, so as to resemble a regular installation.
  78. if (!empty($this->settings)) {
  79. // Not using File API; a potential error must trigger a PHP warning.
  80. copy(DRUPAL_ROOT . '/sites/default/default.settings.php', DRUPAL_ROOT . '/' . $this->siteDirectory . '/settings.php');
  81. $this->writeSettings($this->settings);
  82. }
  83. // Note that WebTestBase::installParameters() returns form input values
  84. // suitable for a programmed \Drupal::formBuilder()->submitForm().
  85. // @see WebTestBase::translatePostValues()
  86. $this->parameters = $this->installParameters();
  87. // Set up a minimal container (required by WebTestBase). Set cookie and
  88. // server information so that XDebug works.
  89. // @see install_begin_request()
  90. $request = Request::create($GLOBALS['base_url'] . '/core/install.php', 'GET', [], $_COOKIE, [], $_SERVER);
  91. $this->container = new ContainerBuilder();
  92. $request_stack = new RequestStack();
  93. $request_stack->push($request);
  94. $this->container
  95. ->set('request_stack', $request_stack);
  96. $this->container
  97. ->setParameter('language.default_values', Language::$defaultValues);
  98. $this->container
  99. ->register('language.default', 'Drupal\Core\Language\LanguageDefault')
  100. ->addArgument('%language.default_values%');
  101. $this->container
  102. ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
  103. ->addArgument(new Reference('language.default'));
  104. $this->container
  105. ->set('app.root', DRUPAL_ROOT);
  106. \Drupal::setContainer($this->container);
  107. $this->visitInstaller();
  108. // Select language.
  109. $this->setUpLanguage();
  110. // Select profile.
  111. $this->setUpProfile();
  112. // Address the requirements problem screen, if any.
  113. $this->setUpRequirementsProblem();
  114. // Configure settings.
  115. $this->setUpSettings();
  116. // @todo Allow test classes based on this class to act on further installer
  117. // screens.
  118. // Configure site.
  119. $this->setUpSite();
  120. if ($this->isInstalled) {
  121. // Import new settings.php written by the installer.
  122. $request = Request::createFromGlobals();
  123. $class_loader = require $this->container->get('app.root') . '/autoload.php';
  124. Settings::initialize($this->container->get('app.root'), DrupalKernel::findSitePath($request), $class_loader);
  125. foreach ($GLOBALS['config_directories'] as $type => $path) {
  126. $this->configDirectories[$type] = $path;
  127. }
  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->prepareLegacyRequest($request);
  137. $this->container = $this->kernel->getContainer();
  138. // Manually configure the test mail collector implementation to prevent
  139. // tests from sending out emails and collect them in state instead.
  140. $this->container->get('config.factory')
  141. ->getEditable('system.mail')
  142. ->set('interface.default', 'test_mail_collector')
  143. ->save();
  144. }
  145. }
  146. /**
  147. * Visits the interactive installer.
  148. */
  149. protected function visitInstaller() {
  150. $this->drupalGet($GLOBALS['base_url'] . '/core/install.php');
  151. }
  152. /**
  153. * Installer step: Select language.
  154. */
  155. protected function setUpLanguage() {
  156. $edit = [
  157. 'langcode' => $this->langcode,
  158. ];
  159. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  160. }
  161. /**
  162. * Installer step: Select installation profile.
  163. */
  164. protected function setUpProfile() {
  165. $edit = [
  166. 'profile' => $this->profile,
  167. ];
  168. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  169. }
  170. /**
  171. * Installer step: Configure settings.
  172. */
  173. protected function setUpSettings() {
  174. $edit = $this->translatePostValues($this->parameters['forms']['install_settings_form']);
  175. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  176. }
  177. /**
  178. * Installer step: Requirements problem.
  179. *
  180. * Override this method to test specific requirements warnings or errors
  181. * during the installer.
  182. *
  183. * @see system_requirements()
  184. */
  185. protected function setUpRequirementsProblem() {
  186. // By default, skip the "recommended PHP version" warning on older test
  187. // environments. This allows the installer to be tested consistently on
  188. // both recommended PHP versions and older (but still supported) versions.
  189. if (version_compare(phpversion(), '7.0') < 0) {
  190. $this->continueOnExpectedWarnings(['PHP']);
  191. }
  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. /**
  215. * Continues installation when an expected warning is found.
  216. *
  217. * @param string[] $expected_warnings
  218. * A list of warning summaries to expect on the requirements screen (e.g.
  219. * 'PHP', 'PHP OPcode caching', etc.). If only the expected warnings
  220. * are found, the test will click the "continue anyway" link to go to the
  221. * next screen of the installer. If an expected warning is not found, or if
  222. * a warning not in the list is present, a fail is raised.
  223. */
  224. protected function continueOnExpectedWarnings($expected_warnings = []) {
  225. // Don't try to continue if there are errors.
  226. if (strpos($this->getTextContent(), 'Errors found') !== FALSE) {
  227. return;
  228. }
  229. // Allow only details elements that are directly after the warning header
  230. // or each other. There is no guaranteed wrapper we can rely on across
  231. // distributions. When there are multiple warnings, the selectors will be:
  232. // - h3#warning+details summary
  233. // - h3#warning+details+details summary
  234. // - etc.
  235. // We add one more selector than expected warnings to confirm that there
  236. // isn't any other warning before clicking the link.
  237. // @todo Make this more reliable in
  238. // https://www.drupal.org/project/drupal/issues/2927345.
  239. $selectors = [];
  240. for ($i = 0; $i <= count($expected_warnings); $i++) {
  241. $selectors[] = 'h3#warning' . implode('', array_fill(0, $i + 1, '+details')) . ' summary';
  242. }
  243. $warning_elements = $this->cssSelect(implode(', ', $selectors));
  244. // Confirm that there are only the expected warnings.
  245. $warnings = [];
  246. foreach ($warning_elements as $warning) {
  247. $warnings[] = trim((string) $warning);
  248. }
  249. $this->assertEqual($expected_warnings, $warnings);
  250. $this->clickLink('continue anyway');
  251. }
  252. }