View source
<?php
namespace Drupal\FunctionalTests\Installer;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Language\Language;
use Drupal\Core\Session\UserSession;
use Drupal\Core\Site\Settings;
use Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\RequirementsPageTrait;
use GuzzleHttp\HandlerStack;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
abstract class InstallerTestBase extends BrowserTestBase {
use RequirementsPageTrait;
protected $settings = [];
protected $langcode = 'en';
protected $profile = 'testing';
protected $parameters = [];
protected $translations = [
'Save and continue' => 'Save and continue',
];
protected $isInstalled = FALSE;
protected function setUp() {
$this->isInstalled = FALSE;
$this
->setupBaseUrl();
$this
->prepareDatabasePrefix();
$this
->prepareEnvironment();
$this->rootUser = new UserSession([
'uid' => 1,
'name' => 'admin',
'mail' => 'admin@example.com',
'pass_raw' => $this
->randomMachineName(),
]);
if (!empty($this->settings)) {
copy(DRUPAL_ROOT . '/sites/default/default.settings.php', DRUPAL_ROOT . '/' . $this->siteDirectory . '/settings.php');
$this
->writeSettings($this->settings);
}
$this->parameters = $this
->installParameters();
$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
->register('http_client', 'GuzzleHttp\\Client')
->setFactory('http_client_factory:fromOptions');
$this->container
->register('http_client_factory', 'Drupal\\Core\\Http\\ClientFactory')
->setArguments([
new Reference('http_handler_stack'),
]);
$handler_stack = HandlerStack::create();
$test_http_client_middleware = new TestHttpClientMiddleware();
$handler_stack
->push($test_http_client_middleware(), 'test.http_client.middleware');
$this->container
->set('http_handler_stack', $handler_stack);
$this->container
->set('app.root', DRUPAL_ROOT);
\Drupal::setContainer($this->container);
$this
->initMink();
$this
->initBrowserOutputFile();
$this
->visitInstaller();
$this
->setUpLanguage();
$this
->setUpProfile();
$this
->setUpRequirementsProblem();
$this
->setUpSettings();
$this
->setUpSite();
if ($this->isInstalled) {
$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');
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();
$this->container
->get('config.factory')
->getEditable('system.mail')
->set('interface.default', 'test_mail_collector')
->save();
$this
->installDefaultThemeFromClassProperty($this->container);
}
}
protected function initFrontPage() {
}
protected function visitInstaller() {
$this
->drupalGet($GLOBALS['base_url'] . '/core/install.php');
}
protected function setUpLanguage() {
$edit = [
'langcode' => $this->langcode,
];
$this
->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
}
protected function setUpProfile() {
$edit = [
'profile' => $this->profile,
];
$this
->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
}
protected function setUpSettings() {
$edit = $this
->translatePostValues($this->parameters['forms']['install_settings_form']);
$this
->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
}
protected function setUpRequirementsProblem() {
}
protected function setUpSite() {
$edit = $this
->translatePostValues($this->parameters['forms']['install_configure_form']);
$this
->drupalPostForm(NULL, $edit, $this->translations['Save and continue']);
$this->isInstalled = TRUE;
}
protected function refreshVariables() {
if ($this->isInstalled) {
parent::refreshVariables();
}
}
}