simpletest-8.x-3.x-dev/src/InstallerTestBase.php
src/InstallerTestBase.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | <?php namespace Drupal\simpletest; @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); use Drupal\Core\DrupalKernel; use Drupal\Core\Language\Language; use Drupal\Core\Session\UserSession; use Drupal\Core\Site\Settings; use Drupal\Tests\RequirementsPageTrait; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; /** * Base class for testing the interactive installer. * * @deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. * Use \Drupal\FunctionalTests\Installer\InstallerTestBase. See */ abstract class InstallerTestBase extends WebTestBase { use RequirementsPageTrait; /** * Custom settings.php values to write for a test run. * * @var array * An array of settings to write out, in the format expected by * drupal_rewrite_settings(). */ protected $settings = []; /** * The language code in which to install Drupal. * * @var string */ protected $langcode = 'en' ; /** * The installation profile to install. * * @var string */ protected $profile = 'testing' ; /** * Additional parameters to use for installer screens. * * @see WebTestBase::installParameters() * * @var array */ protected $parameters = []; /** * A string translation map used for translated installer screens. * * Keys are English strings, values are translated strings. * * @var array */ protected $translations = [ 'Save and continue' => 'Save and continue' , ]; /** * Whether the installer has completed. * * @var bool */ protected $isInstalled = FALSE; /** * {@inheritdoc} */ protected function setUp() { $this ->isInstalled = FALSE; // Define information about the user 1 account. $this ->rootUser = new UserSession([ 'uid' => 1, 'name' => 'admin' , 'mail' => 'admin@example.com' , 'pass_raw' => $this ->randomMachineName(), ]); // If any $settings are defined for this test, copy and prepare an actual // settings.php, so as to resemble a regular installation. if (! empty ( $this ->settings)) { // Not using File API; a potential error must trigger a PHP warning. copy (DRUPAL_ROOT . '/sites/default/default.settings.php' , DRUPAL_ROOT . '/' . $this ->siteDirectory . '/settings.php' ); $this ->writeSettings( $this ->settings); } // Note that WebTestBase::installParameters() returns form input values // suitable for a programmed \Drupal::formBuilder()->submitForm(). // @see WebTestBase::translatePostValues() $this ->parameters = $this ->installParameters(); // Set up a minimal container (required by WebTestBase). Set cookie and // server information so that XDebug works. // @see install_begin_request() $request = Request::create( $GLOBALS [ 'base_url' ] . '/core/install.php' , 'GET' , [], $_COOKIE , [], $_SERVER ); $this ->container = new ContainerBuilder(); $request_stack = new RequestStack(); $request_stack ->push( $request ); $this ->container ->set( 'request_stack' , $request_stack ); $this ->container ->setParameter( 'language.default_values' , Language:: $defaultValues ); $this ->container ->register( 'language.default' , 'Drupal\Core\Language\LanguageDefault' ) ->addArgument( '%language.default_values%' ); $this ->container ->register( 'string_translation' , 'Drupal\Core\StringTranslation\TranslationManager' ) ->addArgument( new Reference( 'language.default' )); $this ->container ->set( 'app.root' , DRUPAL_ROOT); \Drupal::setContainer( $this ->container); $this ->visitInstaller(); // Select language. $this ->setUpLanguage(); // Select profile. $this ->setUpProfile(); // Address the requirements problem screen, if any. $this ->setUpRequirementsProblem(); // Configure settings. $this ->setUpSettings(); // @todo Allow test classes based on this class to act on further installer // screens. // Configure site. $this ->setUpSite(); if ( $this ->isInstalled) { // Import new settings.php written by the installer. $request = Request::createFromGlobals(); $class_loader = require $this ->container->get( 'app.root' ) . '/autoload.php' ; Settings::initialize( $this ->container->get( 'app.root' ), DrupalKernel::findSitePath( $request ), $class_loader ); $this ->configDirectories[ 'sync' ] = Settings::get( 'config_sync_directory' ); // After writing settings.php, the installer removes write permissions // from the site directory. To allow drupal_generate_test_ua() to write // a file containing the private key for drupal_valid_test_ua(), the site // directory has to be writable. // WebTestBase::tearDown() will delete the entire test site directory. // Not using File API; a potential error must trigger a PHP warning. chmod ( $this ->container->get( 'app.root' ) . '/' . $this ->siteDirectory, 0777); $this ->kernel = DrupalKernel::createFromRequest( $request , $class_loader , 'prod' , FALSE); $this ->kernel->boot(); $this ->kernel->preHandle( $request ); $this ->container = $this ->kernel->getContainer(); // Ensure our request includes the session if appropriate. if (PHP_SAPI !== 'cli' ) { $request ->setSession( $this ->container->get( 'session' )); } // Manually configure the test mail collector implementation to prevent // tests from sending out emails and collect them in state instead. $this ->container->get( 'config.factory' ) ->getEditable( 'system.mail' ) ->set( 'interface.default' , 'test_mail_collector' ) ->save(); } } /** * Visits the interactive installer. */ protected function visitInstaller() { $this ->drupalGet( $GLOBALS [ 'base_url' ] . '/core/install.php' ); } /** * Installer step: Select language. */ protected function setUpLanguage() { $edit = [ 'langcode' => $this ->langcode, ]; $this ->drupalPostForm(NULL, $edit , $this ->translations[ 'Save and continue' ]); } /** * Installer step: Select installation profile. */ protected function setUpProfile() { $edit = [ 'profile' => $this ->profile, ]; $this ->drupalPostForm(NULL, $edit , $this ->translations[ 'Save and continue' ]); } /** * Installer step: Configure settings. */ protected function setUpSettings() { $edit = $this ->translatePostValues( $this ->parameters[ 'forms' ][ 'install_settings_form' ]); $this ->drupalPostForm(NULL, $edit , $this ->translations[ 'Save and continue' ]); } /** * Installer step: Requirements problem. * * Override this method to test specific requirements warnings or errors * during the installer. * * @see system_requirements() */ protected function setUpRequirementsProblem() { // Do nothing. } /** * Final installer step: Configure site. */ protected function setUpSite() { $edit = $this ->translatePostValues( $this ->parameters[ 'forms' ][ 'install_configure_form' ]); $this ->drupalPostForm(NULL, $edit , $this ->translations[ 'Save and continue' ]); // If we've got to this point the site is installed using the regular // installation workflow. $this ->isInstalled = TRUE; } /** * {@inheritdoc} * * WebTestBase::refreshVariables() tries to operate on persistent storage, * which is only available after the installer completed. */ protected function refreshVariables() { if ( $this ->isInstalled) { parent::refreshVariables(); } } } |