annotate core/modules/simpletest/src/InstallerTestBase.php @ 17:129ea1e6d783

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