annotate core/modules/simpletest/src/InstallerTestBase.php @ 0:c75dbcec494b

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