InstallerTestBase.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace Drupal\FunctionalTests\Installer;
  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 Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware;
  8. use Drupal\Tests\BrowserTestBase;
  9. use GuzzleHttp\HandlerStack;
  10. use Symfony\Component\DependencyInjection\ContainerBuilder;
  11. use Symfony\Component\DependencyInjection\Reference;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. /**
  15. * Base class for testing the interactive installer.
  16. */
  17. abstract class InstallerTestBase extends BrowserTestBase {
  18. /**
  19. * Custom settings.php values to write for a test run.
  20. *
  21. * @var array
  22. * An array of settings to write out, in the format expected by
  23. * drupal_rewrite_settings().
  24. */
  25. protected $settings = [];
  26. /**
  27. * The language code in which to install Drupal.
  28. *
  29. * @var string
  30. */
  31. protected $langcode = 'en';
  32. /**
  33. * The installation profile to install.
  34. *
  35. * @var string
  36. */
  37. protected $profile = 'testing';
  38. /**
  39. * Additional parameters to use for installer screens.
  40. *
  41. * @see FunctionalTestSetupTrait::installParameters()
  42. *
  43. * @var array
  44. */
  45. protected $parameters = [];
  46. /**
  47. * A string translation map used for translated installer screens.
  48. *
  49. * Keys are English strings, values are translated strings.
  50. *
  51. * @var array
  52. */
  53. protected $translations = [
  54. 'Save and continue' => 'Save and continue',
  55. ];
  56. /**
  57. * Whether the installer has completed.
  58. *
  59. * @var bool
  60. */
  61. protected $isInstalled = FALSE;
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function setUp() {
  66. $this->isInstalled = FALSE;
  67. $this->setupBaseUrl();
  68. $this->prepareDatabasePrefix();
  69. // Install Drupal test site.
  70. $this->prepareEnvironment();
  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 FunctionalTestSetupTrait::installParameters() returns form
  86. // input values suitable for a programmed
  87. // \Drupal::formBuilder()->submitForm().
  88. // @see InstallerTestBase::translatePostValues()
  89. $this->parameters = $this->installParameters();
  90. // Set up a minimal container (required by BrowserTestBase). Set cookie and
  91. // server information so that XDebug works.
  92. // @see install_begin_request()
  93. $request = Request::create($GLOBALS['base_url'] . '/core/install.php', 'GET', [], $_COOKIE, [], $_SERVER);
  94. $this->container = new ContainerBuilder();
  95. $request_stack = new RequestStack();
  96. $request_stack->push($request);
  97. $this->container
  98. ->set('request_stack', $request_stack);
  99. $this->container
  100. ->setParameter('language.default_values', Language::$defaultValues);
  101. $this->container
  102. ->register('language.default', 'Drupal\Core\Language\LanguageDefault')
  103. ->addArgument('%language.default_values%');
  104. $this->container
  105. ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
  106. ->addArgument(new Reference('language.default'));
  107. $this->container
  108. ->register('http_client', 'GuzzleHttp\Client')
  109. ->setFactory('http_client_factory:fromOptions');
  110. $this->container
  111. ->register('http_client_factory', 'Drupal\Core\Http\ClientFactory')
  112. ->setArguments([new Reference('http_handler_stack')]);
  113. $handler_stack = HandlerStack::create();
  114. $test_http_client_middleware = new TestHttpClientMiddleware();
  115. $handler_stack->push($test_http_client_middleware(), 'test.http_client.middleware');
  116. $this->container
  117. ->set('http_handler_stack', $handler_stack);
  118. $this->container
  119. ->set('app.root', DRUPAL_ROOT);
  120. \Drupal::setContainer($this->container);
  121. // Setup Mink.
  122. $this->initMink();
  123. // Set up the browser test output file.
  124. $this->initBrowserOutputFile();
  125. $this->visitInstaller();
  126. // Select language.
  127. $this->setUpLanguage();
  128. // Select profile.
  129. $this->setUpProfile();
  130. // Address the requirements problem screen, if any.
  131. $this->setUpRequirementsProblem();
  132. // Configure settings.
  133. $this->setUpSettings();
  134. // @todo Allow test classes based on this class to act on further installer
  135. // screens.
  136. // Configure site.
  137. $this->setUpSite();
  138. if ($this->isInstalled) {
  139. // Import new settings.php written by the installer.
  140. $request = Request::createFromGlobals();
  141. $class_loader = require $this->container->get('app.root') . '/autoload.php';
  142. Settings::initialize($this->container->get('app.root'), DrupalKernel::findSitePath($request), $class_loader);
  143. foreach ($GLOBALS['config_directories'] as $type => $path) {
  144. $this->configDirectories[$type] = $path;
  145. }
  146. // After writing settings.php, the installer removes write permissions
  147. // from the site directory. To allow drupal_generate_test_ua() to write
  148. // a file containing the private key for drupal_valid_test_ua(), the site
  149. // directory has to be writable.
  150. // BrowserTestBase::tearDown() will delete the entire test site directory.
  151. // Not using File API; a potential error must trigger a PHP warning.
  152. chmod($this->container->get('app.root') . '/' . $this->siteDirectory, 0777);
  153. $this->kernel = DrupalKernel::createFromRequest($request, $class_loader, 'prod', FALSE);
  154. $this->kernel->prepareLegacyRequest($request);
  155. $this->container = $this->kernel->getContainer();
  156. // Manually configure the test mail collector implementation to prevent
  157. // tests from sending out emails and collect them in state instead.
  158. $this->container->get('config.factory')
  159. ->getEditable('system.mail')
  160. ->set('interface.default', 'test_mail_collector')
  161. ->save();
  162. }
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. protected function initFrontPage() {
  168. // We don't want to visit the front page with the installer when
  169. // initializing Mink, so we do nothing here.
  170. }
  171. /**
  172. * Visits the interactive installer.
  173. */
  174. protected function visitInstaller() {
  175. $this->drupalGet($GLOBALS['base_url'] . '/core/install.php');
  176. }
  177. /**
  178. * Installer step: Select language.
  179. */
  180. protected function setUpLanguage() {
  181. $edit = [
  182. 'langcode' => $this->langcode,
  183. ];
  184. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  185. }
  186. /**
  187. * Installer step: Select installation profile.
  188. */
  189. protected function setUpProfile() {
  190. $edit = [
  191. 'profile' => $this->profile,
  192. ];
  193. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  194. }
  195. /**
  196. * Installer step: Configure settings.
  197. */
  198. protected function setUpSettings() {
  199. $edit = $this->translatePostValues($this->parameters['forms']['install_settings_form']);
  200. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  201. }
  202. /**
  203. * Installer step: Requirements problem.
  204. *
  205. * Override this method to test specific requirements warnings or errors
  206. * during the installer.
  207. *
  208. * @see system_requirements()
  209. */
  210. protected function setUpRequirementsProblem() {
  211. // By default, skip the "recommended PHP version" warning on older test
  212. // environments. This allows the installer to be tested consistently on
  213. // both recommended PHP versions and older (but still supported) versions.
  214. if (version_compare(phpversion(), '7.0') < 0) {
  215. $this->continueOnExpectedWarnings(['PHP']);
  216. }
  217. }
  218. /**
  219. * Final installer step: Configure site.
  220. */
  221. protected function setUpSite() {
  222. $edit = $this->translatePostValues($this->parameters['forms']['install_configure_form']);
  223. $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
  224. // If we've got to this point the site is installed using the regular
  225. // installation workflow.
  226. $this->isInstalled = TRUE;
  227. }
  228. /**
  229. * {@inheritdoc}
  230. *
  231. * FunctionalTestSetupTrait::refreshVariables() tries to operate on persistent
  232. * storage, which is only available after the installer completed.
  233. */
  234. protected function refreshVariables() {
  235. if ($this->isInstalled) {
  236. parent::refreshVariables();
  237. }
  238. }
  239. /**
  240. * Continues installation when an expected warning is found.
  241. *
  242. * @param string[] $expected_warnings
  243. * A list of warning summaries to expect on the requirements screen (e.g.
  244. * 'PHP', 'PHP OPcode caching', etc.). If only the expected warnings
  245. * are found, the test will click the "continue anyway" link to go to the
  246. * next screen of the installer. If an expected warning is not found, or if
  247. * a warning not in the list is present, a fail is raised.
  248. */
  249. protected function continueOnExpectedWarnings($expected_warnings = []) {
  250. // Don't try to continue if there are errors.
  251. if (strpos($this->getTextContent(), 'Errors found') !== FALSE) {
  252. return;
  253. }
  254. // Allow only details elements that are directly after the warning header
  255. // or each other. There is no guaranteed wrapper we can rely on across
  256. // distributions. When there are multiple warnings, the selectors will be:
  257. // - h3#warning+details summary
  258. // - h3#warning+details+details summary
  259. // - etc.
  260. // We add one more selector than expected warnings to confirm that there
  261. // isn't any other warning before clicking the link.
  262. // @todo Make this more reliable in
  263. // https://www.drupal.org/project/drupal/issues/2927345.
  264. $selectors = [];
  265. for ($i = 0; $i <= count($expected_warnings); $i++) {
  266. $selectors[] = 'h3#warning' . implode('', array_fill(0, $i + 1, '+details')) . ' summary';
  267. }
  268. $warning_elements = $this->cssSelect(implode(', ', $selectors));
  269. // Confirm that there are only the expected warnings.
  270. $warnings = [];
  271. foreach ($warning_elements as $warning) {
  272. $warnings[] = trim($warning->getText());
  273. }
  274. $this->assertEquals($expected_warnings, $warnings);
  275. $this->clickLink('continue anyway');
  276. $this->checkForMetaRefresh();
  277. }
  278. }