annotate core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php @ 16:c2387f117808

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