View source
<?php
namespace Drupal\Tests\automatic_updates\Build;
use Drupal\BuildTests\QuickStart\QuickStartTestBase;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Tests\automatic_updates\Traits\LocalPackagesTrait;
use Drupal\Tests\automatic_updates\Traits\SettingsTrait;
abstract class UpdateTestBase extends QuickStartTestBase {
use LocalPackagesTrait {
getPackagePath as traitGetPackagePath;
copyPackage as traitCopyPackage;
}
use SettingsTrait;
private $metadataServer;
private $webRoot;
protected function tearDown() : void {
if ($this->metadataServer) {
$this->metadataServer
->stop();
}
parent::tearDown();
}
protected function copyPackage(string $source_dir, string $destination_dir = NULL) : string {
return $this
->traitCopyPackage($source_dir, $destination_dir ?: $this
->getWorkspaceDirectory());
}
protected function getPackagePath(array $package) : string {
if ($package['name'] === 'drupal/core') {
return 'core';
}
[
$vendor,
$name,
] = explode('/', $package['name']);
if ($vendor === 'drupal' && $package['type'] === 'drupal-module') {
return implode(DIRECTORY_SEPARATOR, [
'modules',
'contrib',
$name,
]);
}
return $this
->traitGetPackagePath($package);
}
protected function getWebRoot() : string {
return $this
->getWorkspaceDirectory() . DIRECTORY_SEPARATOR . $this->webRoot;
}
protected function setReleaseMetadata(array $xml_map) : void {
$xml_map = var_export($xml_map, TRUE);
$code = <<<END
\$config['update_test.settings']['xml_map'] = {<span class="php-variable">$xml_map</span>};
END;
if (empty($this->metadataServer)) {
$port = $this
->findAvailablePort();
$this->metadataServer = $this
->instantiateServer($port, $this->webRoot);
$code .= <<<END
\$config['update.settings']['fetch']['url'] = 'http://localhost:{<span class="php-variable">$port</span>}/automatic-update-test';
END;
}
$this
->addSettings($code, $this
->getWebRoot());
}
public function visit($request_uri = '/', $working_dir = NULL) {
return parent::visit($request_uri, $working_dir ?: $this->webRoot);
}
public function formLogin($username, $password, $working_dir = NULL) {
parent::formLogin($username, $password, $working_dir ?: $this->webRoot);
}
public function installQuickStart($profile, $working_dir = NULL) {
parent::installQuickStart($profile, $working_dir ?: $this->webRoot);
$php = <<<END
\$settings['extension_discovery_scan_tests'] = TRUE;
\$config['system.logging']['error_level'] = 'verbose';
END;
$this
->addSettings($php, $this
->getWebRoot());
}
protected function createTestSite(string $template) : void {
$template_dir = implode(DIRECTORY_SEPARATOR, [
$this
->getDrupalRoot(),
'composer',
'Template',
]);
$recommended_template = $this
->createPathRepository($template_dir . DIRECTORY_SEPARATOR . 'RecommendedProject');
$legacy_template = $this
->createPathRepository($template_dir . DIRECTORY_SEPARATOR . 'LegacyProject');
$dir = $this
->getWorkspaceDirectory();
$command = sprintf("composer create-project %s %s --no-install --stability dev --repository '%s' --repository '%s'", $template, $dir, Json::encode($recommended_template), Json::encode($legacy_template));
$this
->executeCommand($command);
$this
->assertCommandSuccessful();
$composer = $dir . DIRECTORY_SEPARATOR . 'composer.json';
$data = $this
->readJson($composer);
$data = $this
->getInitialConfiguration($data);
$this->webRoot = $data['extra']['drupal-scaffold']['locations']['web-root'];
$this
->writeJson($composer, $data);
$this
->executeCommand('composer install --no-dev');
$this
->assertCommandSuccessful();
}
protected function getInitialConfiguration(array $data) : array {
$drupal_root = $this
->getDrupalRoot();
$core_composer_dir = $drupal_root . DIRECTORY_SEPARATOR . 'composer';
$repositories = [];
$metapackage_dir = $core_composer_dir . DIRECTORY_SEPARATOR . 'Metapackage';
$repositories['drupal/core-recommended'] = $this
->createPathRepository($metapackage_dir . DIRECTORY_SEPARATOR . 'CoreRecommended');
$repositories['drupal/core-dev'] = $this
->createPathRepository($metapackage_dir . DIRECTORY_SEPARATOR . 'DevDependencies');
$plugin_dir = $core_composer_dir . DIRECTORY_SEPARATOR . 'Plugin';
$repositories['drupal/core-project-message'] = $this
->createPathRepository($plugin_dir . DIRECTORY_SEPARATOR . 'ProjectMessage');
$repositories['drupal/core-composer-scaffold'] = $this
->createPathRepository($plugin_dir . DIRECTORY_SEPARATOR . 'Scaffold');
$repositories['drupal/core-vendor-hardening'] = $this
->createPathRepository($plugin_dir . DIRECTORY_SEPARATOR . 'VendorHardening');
$repositories = array_merge($repositories, $this
->getLocalPackageRepositories($drupal_root));
$repositories['packagist.org'] = FALSE;
$repositories['drupal/automatic_updates'] = [
'type' => 'path',
'url' => __DIR__ . '/../../..',
];
$data['require']['drupal/automatic_updates'] = '*';
$data['repositories'] = $repositories;
$data['replace']['symfony/polyfill-php72'] = '*';
$data['replace']['symfony/polyfill-php73'] = '*';
return $data;
}
protected function assertCoreVersion(string $expected_version) : void {
$this
->visit('/admin/reports/status');
$item = $this
->getMink()
->assertSession()
->elementExists('css', 'h3:contains("Drupal Version")')
->getParent()
->getText();
$this
->assertStringContainsString($expected_version, $item);
}
protected function installModules(array $modules) : void {
$mink = $this
->getMink();
$page = $mink
->getSession()
->getPage();
$assert_session = $mink
->assertSession();
$this
->visit('/admin/modules');
foreach ($modules as $module) {
$page
->checkField("modules[{$module}][enable]");
}
$page
->pressButton('Install');
$form_id = $assert_session
->elementExists('css', 'input[type="hidden"][name="form_id"]')
->getValue();
if ($form_id === 'system_modules_confirm_form') {
$page
->pressButton('Continue');
$assert_session
->statusCodeEquals(200);
}
}
protected function checkForUpdates() : void {
$this
->visit('/admin/reports/updates');
$this
->getMink()
->getSession()
->getPage()
->clickLink('Check manually');
$this
->waitForBatchJob();
}
protected function waitForBatchJob() : void {
$refresh = $this
->getMink()
->getSession()
->getPage()
->find('css', 'meta[http-equiv="Refresh"], meta[http-equiv="refresh"]');
if ($refresh) {
if (preg_match('/\\d+;\\s*URL=\'?(?<url>[^\']*)/i', $refresh
->getAttribute('content'), $match)) {
$url = Html::decodeEntities($match['url']);
$this
->visit($url);
$this
->waitForBatchJob();
}
}
}
}