View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\update\Functional;
use Drupal\Core\Utility\ProjectInfo;
use Drupal\update\UpdateManagerInterface;
class UpdateContribTest extends UpdateTestBase {
use UpdateTestTrait;
protected $updateTableLocator = 'table.update:nth-of-type(2)';
protected $updateProject = 'aaa_update_test';
protected static $modules = [
'aaa_update_test',
'bbb_update_test',
'ccc_update_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
}
public function testNoReleasesAvailable() {
$this
->mockInstalledExtensionsInfo([
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
]);
$this
->mockDefaultExtensionsInfo([
'version' => '8.0.0',
]);
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
'aaa_update_test' => 'no-releases',
]);
$this
->assertSession()
->responseContains('<h3>Drupal core</h3>');
$this
->assertSession()
->linkExists('Drupal');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/drupal');
$this
->assertSession()
->pageTextContains('Up to date');
$this
->assertSession()
->responseContains('<h3>Modules</h3>');
$this
->assertSession()
->pageTextNotContains('Update available');
$this
->assertSession()
->pageTextContains('No available releases found');
$this
->assertSession()
->linkNotExists('AAA Update test');
$this
->assertSession()
->linkByHrefNotExists('http://example.com/project/aaa_update_test');
$available = update_get_available();
$this
->assertFalse(isset($available['aaa_update_test']['fetch_status']), 'Results are cached even if no releases are available.');
}
public function testUpdateContribBasic() {
$installed_extensions = [
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
];
$this
->mockInstalledExtensionsInfo($installed_extensions);
$this
->mockDefaultExtensionsInfo([
'version' => '8.0.0',
]);
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
'aaa_update_test' => '1_0',
]);
$this
->standardTests();
$this
->assertSession()
->pageTextContains('Up to date');
$this
->assertSession()
->responseContains('<h3>Modules</h3>');
$this
->assertSession()
->pageTextNotContains('Update available');
$this
->assertSession()
->linkExists('AAA Update test');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/aaa_update_test');
$installed_extensions['aaa_update_test']['hidden'] = TRUE;
$this
->mockInstalledExtensionsInfo($installed_extensions);
$this
->mockDefaultExtensionsInfo([
'version' => '8.0.0',
]);
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
'aaa_update_test' => '1_0',
]);
$this
->assertSession()
->linkNotExists('AAA Update test');
$this
->assertSession()
->linkByHrefNotExists('http://example.com/project/aaa_update_test');
$installed_extensions['aaa_update_test']['package'] = 'aaa_update_test';
$this
->mockInstalledExtensionsInfo($installed_extensions);
$this
->mockDefaultExtensionsInfo([
'version' => '8.0.0',
]);
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
'aaa_update_test' => '1_0',
]);
$this
->assertSession()
->linkExists('AAA Update test');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/aaa_update_test');
}
public function testUpdateContribOrder() {
$this
->mockDefaultExtensionsInfo([
'version' => '8.0.0',
]);
$this
->mockInstalledExtensionsInfo([
'aaa_update_test' => [
'project' => 'ccc_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
'bbb_update_test' => [
'project' => 'bbb_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
'ccc_update_test' => [
'project' => 'ccc_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
]);
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
'#all' => '1_0',
]);
$this
->standardTests();
$this
->assertSession()
->pageTextContains('Up to date');
$this
->assertSession()
->pageTextNotContains('Update available');
$this
->assertSession()
->pageTextContains('AAA Update test');
$this
->assertSession()
->pageTextContains('BBB Update test');
$this
->assertSession()
->pageTextContains('CCC Update test');
$this
->assertSession()
->linkNotExists('AAA Update test');
$this
->assertSession()
->linkByHrefNotExists('http://example.com/project/aaa_update_test');
$this
->assertSession()
->linkExists('BBB Update test');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/bbb_update_test');
$this
->assertSession()
->linkExists('CCC Update test');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/ccc_update_test');
$bbb_project_link = '<div class="project-update__title"><a href="http://example.com/project/bbb_update_test">BBB Update test</a>';
$ccc_project_link = '<div class="project-update__title"><a href="http://example.com/project/ccc_update_test">CCC Update test</a>';
$this
->assertLessThan(strpos($this
->getSession()
->getPage()
->getContent(), $ccc_project_link), strpos($this
->getSession()
->getPage()
->getContent(), $bbb_project_link));
}
public function testUpdateBaseThemeSecurityUpdate() {
\Drupal::service('theme_installer')
->install([
'update_test_subtheme',
]);
$this
->mockInstalledExtensionsInfo([
'update_test_basetheme' => [
'project' => 'update_test_basetheme',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
'update_test_subtheme' => [
'project' => 'update_test_subtheme',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
]);
$xml_mapping = [
'drupal' => '8.0.0',
'update_test_subtheme' => '1_0',
'update_test_basetheme' => '1_1-sec',
];
$this
->refreshUpdateStatus($xml_mapping);
$this
->assertSession()
->pageTextContains('Security update required!');
$this->updateProject = 'update_test_basetheme';
$this
->assertVersionUpdateLinks('Security update', '8.x-1.1');
}
public function testNormalUpdateAvailable() {
$assert_session = $this
->assertSession();
$this
->drupalGet('admin/reports/updates/check');
$assert_session
->statusCodeEquals(403);
$this
->mockInstalledExtensionsInfo([
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
]);
foreach ([
'1.1',
'1.2',
'2.0',
] as $version) {
foreach ([
'-beta1',
'-alpha1',
'',
] as $extra_version) {
$full_version = "8.x-{$version}{$extra_version}";
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
'aaa_update_test' => str_replace('.', '_', $version) . $extra_version,
]);
$this
->standardTests();
$assert_session
->pageTextNotContains('Security update required!');
$this
->assertSession()
->responseNotContains('8.x-3.0');
$this->updateTableLocator = 'table.update:nth-of-type(2)';
switch ($version) {
case '1.1':
if ($extra_version == '') {
$assert_session
->elementTextNotContains('css', $this->updateTableLocator, 'Up to date');
$assert_session
->elementTextContains('css', $this->updateTableLocator, 'Update available');
$this
->assertVersionUpdateLinks('Recommended version', $full_version);
$assert_session
->elementTextNotContains('css', $this->updateTableLocator, 'Latest version:');
$assert_session
->elementContains('css', $this->updateTableLocator, 'warning.svg');
}
else {
$assert_session
->elementTextContains('css', $this->updateTableLocator, 'Up to date');
$assert_session
->elementTextNotContains('css', $this->updateTableLocator, 'Update available');
$assert_session
->elementTextNotContains('css', $this->updateTableLocator, 'Recommended version:');
$this
->assertVersionUpdateLinks('Latest version', $full_version);
$assert_session
->elementContains('css', $this->updateTableLocator, 'check.svg');
}
break;
case '1.2':
if ($extra_version == '') {
$assert_session
->elementTextNotContains('css', $this->updateTableLocator, 'Up to date');
$assert_session
->elementTextContains('css', $this->updateTableLocator, 'Update available');
$this
->assertVersionUpdateLinks('Recommended version:', $full_version);
$assert_session
->elementTextNotContains('css', $this->updateTableLocator, 'Latest version:');
$assert_session
->elementContains('css', $this->updateTableLocator, 'warning.svg');
}
else {
$assert_session
->elementTextNotContains('css', $this->updateTableLocator, 'Up to date');
$assert_session
->elementTextContains('css', $this->updateTableLocator, 'Update available');
$this
->assertVersionUpdateLinks('Recommended version:', '8.x-1.1');
$this
->assertVersionUpdateLinks('Latest version:', $full_version);
$assert_session
->elementTextContains('css', $this->updateTableLocator, 'Latest version:');
$assert_session
->elementContains('css', $this->updateTableLocator, 'warning.svg');
}
break;
case '2.0':
$assert_session
->elementTextNotContains('css', $this->updateTableLocator, 'Up to date');
$assert_session
->elementTextContains('css', $this->updateTableLocator, 'Update available');
$this
->assertVersionUpdateLinks('Recommended version', '8.x-1.2');
$this
->assertVersionUpdateLinks('Also available', $full_version);
$assert_session
->elementTextNotContains('css', $this->updateTableLocator, 'Latest version:');
$assert_session
->elementContains('css', $this->updateTableLocator, 'warning.svg');
}
}
}
}
public function testUpdateShowDisabledThemes() {
$update_settings = $this
->config('update.settings');
$extension_config = $this
->config('core.extension');
foreach ($extension_config
->get('theme') as $theme => $weight) {
if (str_starts_with($theme, 'update_test_')) {
$extension_config
->clear("theme.{$theme}");
}
}
$extension_config
->save();
$this
->mockInstalledExtensionsInfo([
'update_test_basetheme' => [
'project' => 'update_test_basetheme',
'version' => '8.x-1.1',
'hidden' => FALSE,
],
'update_test_subtheme' => [
'project' => 'update_test_subtheme',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
]);
$update_settings
->set('fetch.max_attempts', 99999)
->save();
$xml_mapping = [
'drupal' => '8.0.0',
'update_test_subtheme' => '1_0',
'update_test_basetheme' => '1_1-sec',
];
foreach ([
TRUE,
FALSE,
] as $check_disabled) {
$update_settings
->set('check.disabled_extensions', $check_disabled)
->save();
$this
->refreshUpdateStatus($xml_mapping);
$this
->assertSession()
->pageTextNotMatches('/Themes/');
if ($check_disabled) {
$this
->assertSession()
->pageTextContains('Uninstalled themes');
$this
->assertSession()
->linkExists('Update test base theme');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/update_test_basetheme');
$this
->assertSession()
->linkExists('Update test subtheme');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/update_test_subtheme');
}
else {
$this
->assertSession()
->pageTextNotContains('Uninstalled themes');
$this
->assertSession()
->linkNotExists('Update test base theme');
$this
->assertSession()
->linkByHrefNotExists('http://example.com/project/update_test_basetheme');
$this
->assertSession()
->linkNotExists('Update test subtheme');
$this
->assertSession()
->linkByHrefNotExists('http://example.com/project/update_test_subtheme');
}
}
}
public function testUpdateHiddenBaseTheme() {
\Drupal::moduleHandler()
->loadInclude('update', 'inc', 'update.compare');
\Drupal::service('theme_installer')
->install([
'update_test_subtheme',
]);
$this
->mockInstalledExtensionsInfo([
'update_test_basetheme' => [
'project' => 'update_test_basetheme',
'hidden' => TRUE,
],
'update_test_subtheme' => [
'project' => 'update_test_subtheme',
'hidden' => FALSE,
],
]);
$projects = \Drupal::service('update.manager')
->getProjects();
$theme_data = \Drupal::service('extension.list.theme')
->reset()
->getList();
$project_info = new ProjectInfo();
$project_info
->processInfoList($projects, $theme_data, 'theme', TRUE);
$this
->assertNotEmpty($projects['update_test_basetheme'], 'Valid base theme (update_test_basetheme) was found.');
}
public function testUpdateBrokenFetchURL() {
$this
->mockInstalledExtensionsInfo([
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
'bbb_update_test' => [
'project' => 'bbb_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
'ccc_update_test' => [
'project' => 'ccc_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
]);
$this
->mockDefaultExtensionsInfo([
'version' => '8.0.0',
]);
$this
->drupalGet('admin/reports/updates');
$xml_mapping = [
'drupal' => '8.0.0',
'aaa_update_test' => '1_0',
'bbb_update_test' => 'does-not-exist',
'ccc_update_test' => '1_0',
];
$this
->refreshUpdateStatus($xml_mapping);
$this
->assertSession()
->pageTextContains('Up to date');
$this
->assertSession()
->pageTextMatchesCount(3, '/Up to date/');
$this
->assertSession()
->pageTextNotContains('Update available');
$this
->assertSession()
->responseContains('<div class="project-update__status">Failed to get available update data');
$this
->assertSession()
->pageTextContainsOnce('Checked available update data for 3 projects.');
$this
->assertSession()
->pageTextContainsOnce('Failed to get available update data for one project.');
$this
->assertSession()
->linkExists('AAA Update test');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/aaa_update_test');
$this
->assertSession()
->linkNotExists('BBB Update test');
$this
->assertSession()
->linkByHrefNotExists('http://example.com/project/bbb_update_test');
$this
->assertSession()
->linkExists('CCC Update test');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/ccc_update_test');
}
public function testHookUpdateStatusAlter() {
$update_admin_user = $this
->drupalCreateUser([
'administer site configuration',
'administer software updates',
]);
$this
->drupalLogin($update_admin_user);
$this
->mockInstalledExtensionsInfo([
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
]);
$this
->mockDefaultExtensionsInfo([
'version' => '8.0.0',
]);
$update_test_config = $this
->config('update_test.settings');
$update_status = [
'aaa_update_test' => [
'status' => UpdateManagerInterface::NOT_SECURE,
],
];
$update_test_config
->set('update_status', $update_status)
->save();
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
'aaa_update_test' => '1_0',
]);
$this
->assertSession()
->responseContains('<h3>Modules</h3>');
$this
->assertSession()
->pageTextContains('Security update required!');
$this
->assertSession()
->linkExists('AAA Update test');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/aaa_update_test');
$update_test_config
->set('update_status', [])
->save();
$this
->drupalGet('admin/reports/updates');
$this
->assertSession()
->responseContains('<h3>Modules</h3>');
$this
->assertSession()
->pageTextNotContains('Security update required!');
$this
->assertSession()
->linkExists('AAA Update test');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/aaa_update_test');
$update_test_config
->set('update_status', $update_status)
->save();
$this
->drupalGet('admin/modules/update');
$this
->assertSession()
->pageTextContains('Security update');
$update_test_config
->set('update_status', [])
->save();
$this
->drupalGet('admin/modules/update');
$this
->assertSession()
->pageTextNotContains('Security update');
}
public function testCoreCompatibilityMessage() {
$this
->mockInstalledExtensionsInfo([
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
]);
$this
->mockDefaultExtensionsInfo([
'version' => '8.0.0',
]);
$this
->refreshUpdateStatus([
'drupal' => '8.1.1',
'aaa_update_test' => '8.x-1.2',
]);
$this
->assertCoreCompatibilityMessage('8.x-1.2', '8.0.0 to 8.1.1', 'Recommended version:');
$this
->assertCoreCompatibilityMessage('8.x-1.3-beta1', '8.0.0, 8.1.1', 'Latest version:');
$this
->refreshUpdateStatus([
'drupal' => '8.1.1-core_compatibility',
'aaa_update_test' => '8.x-1.2',
]);
$this
->assertCoreCompatibilityMessage('8.x-1.2', '8.0.0 to 8.0.1', 'Recommended version:');
$this
->assertCoreCompatibilityMessage('8.x-1.3-beta1', '8.0.0', 'Latest version:');
$this
->refreshUpdateStatus([
'drupal' => '8.1.1-alpha1',
'aaa_update_test' => '8.x-1.2',
]);
$this
->assertCoreCompatibilityMessage('8.x-1.2', '8.0.0 to 8.1.0', 'Recommended version:');
$this
->assertCoreCompatibilityMessage('8.x-1.3-beta1', '8.0.0', 'Latest version:');
$this
->refreshUpdateStatus([
'drupal' => '8.1.1',
'aaa_update_test' => 'core_compatibility.8.x-1.2_8.x-2.2',
]);
$this
->assertCoreCompatibilityMessage('8.x-1.2', '8.1.0 to 8.1.1', 'Security update:', FALSE);
$this
->assertCoreCompatibilityMessage('8.x-2.2', '8.1.1', 'Also available:', FALSE);
}
public function testSecurityUpdateAvailability($module_version, array $expected_security_releases, $expected_update_message_type, $fixture) {
$this
->mockInstalledExtensionsInfo([
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => $module_version,
'hidden' => FALSE,
],
]);
$this
->mockDefaultExtensionsInfo([
'version' => '8.0.0',
]);
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
'aaa_update_test' => $fixture,
]);
$this
->assertSecurityUpdates('aaa_update_test', $expected_security_releases, $expected_update_message_type, 'table.update:nth-of-type(2)');
}
public static function securityUpdateAvailabilityProvider() {
return [
'8.x-1.0, 8.x-1.2' => [
'module_patch_version' => '8.x-1.0',
'expected_security_releases' => [
'8.x-1.2',
],
'expected_update_message_type' => static::SECURITY_UPDATE_REQUIRED,
'fixture' => 'sec.8.x-1.2',
],
'8.x-1.0, 8.x-1.1 8.x-1.2' => [
'module_patch_version' => '8.x-1.0',
'expected_security_releases' => [
'8.x-1.2',
],
'expected_update_message_type' => static::SECURITY_UPDATE_REQUIRED,
'fixture' => 'sec.8.x-1.1_8.x-1.2',
],
'8.x-2.0, 8.x-2.2' => [
'module_patch_version' => '8.x-2.0',
'expected_security_releases' => [
'8.x-2.2',
],
'expected_update_message_type' => static::SECURITY_UPDATE_REQUIRED,
'fixture' => 'sec.8.x-2.2_1.x_secure',
],
'8.x-2.2, 8.x-1.2 8.x-2.2' => [
'module_patch_version' => '8.x-2.2',
'expected_security_releases' => [],
'expected_update_message_type' => static::UPDATE_NONE,
'fixture' => 'sec.8.x-1.2_8.x-2.2',
],
'8.x-1.0, 8.x-1.2 8.x-2.2' => [
'module_patch_version' => '8.x-1.0',
'expected_security_releases' => [
'8.x-1.2',
],
'expected_update_message_type' => static::SECURITY_UPDATE_REQUIRED,
'fixture' => 'sec.8.x-1.2_8.x-2.2',
],
'8.x-1.0, 8.x-2.2, not insecure' => [
'module_patch_version' => '8.x-1.0',
'expected_security_releases' => [],
'expected_update_message_type' => static::UPDATE_AVAILABLE,
'fixture' => 'sec.8.x-2.2_1.x_secure',
],
'8.x-1.2, 8.x-1.2 8.x-2.2' => [
'module_patch_version' => '8.x-1.2',
'expected_security_release' => [],
'expected_update_message_type' => static::UPDATE_NONE,
'fixture' => 'sec.8.x-1.2_8.x-2.2',
],
'8.x-2.0, 8.x-1.2 8.x-2.2' => [
'module_patch_version' => '8.x-2.0',
'expected_security_releases' => [
'8.x-2.2',
],
'expected_update_message_type' => static::SECURITY_UPDATE_REQUIRED,
'fixture' => 'sec.8.x-1.2_8.x-2.2',
],
];
}
public function testRevokedRelease() {
$this
->mockInstalledExtensionsInfo([
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
]);
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
$this->updateProject => '1_0-supported',
]);
$this
->confirmRevokedStatus('8.x-1.0', '8.x-2.0', 'Also available:');
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
$this->updateProject => '1_0-unsupported',
]);
$this
->confirmRevokedStatus('8.x-1.0', '8.x-2.0', 'Recommended version:');
}
public function testUnsupportedRelease() {
$this
->mockInstalledExtensionsInfo([
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.1',
'hidden' => FALSE,
],
]);
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
$this->updateProject => '1_0-supported',
]);
$this
->confirmUnsupportedStatus('8.x-1.1', '8.x-2.0', 'Also available:');
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
$this->updateProject => '1_0-unsupported',
]);
$this
->confirmUnsupportedStatus('8.x-1.1', '8.x-2.0', 'Recommended version:');
}
public function testNonStandardVersionStrings() {
$version_infos = [
'invalid' => [
'version' => 'llama',
'expected' => 'Invalid version: llama',
],
'empty' => [
'version' => '',
'expected' => 'Empty version',
],
'null' => [
'expected' => 'Invalid version: Unknown',
],
];
foreach ($version_infos as $version_info) {
$installed_extensions = [
'aaa_update_test' => [
'project' => 'aaa_update_test',
'hidden' => FALSE,
],
];
if (isset($version_info['version'])) {
$installed_extensions['aaa_update_test']['version'] = $version_info['version'];
}
$this
->mockInstalledExtensionsInfo($installed_extensions);
$this
->refreshUpdateStatus([
'drupal' => '8.0.0',
$this->updateProject => '1_0-supported',
]);
$this
->standardTests();
$this
->assertSession()
->elementTextContains('css', $this->updateTableLocator, $version_info['expected']);
}
}
protected function assertCoreCompatibilityMessage(string $version, string $expected_range, string $expected_release_title, bool $is_compatible = TRUE) : void {
$update_element = $this
->findUpdateElementByLabel($expected_release_title);
$this
->assertTrue($update_element
->hasLink($version));
$compatibility_details = $update_element
->find('css', '.project-update__compatibility-details details');
$this
->assertStringContainsString("Requires Drupal core: {$expected_range}", $compatibility_details
->getText());
$details_summary_element = $compatibility_details
->find('css', 'summary');
if ($is_compatible) {
$this
->assertFalse($compatibility_details
->hasAttribute('open'));
$this
->assertSame('Compatible', $details_summary_element
->getText());
}
else {
$this
->assertTrue($compatibility_details
->hasAttribute('open'));
$this
->assertSame('Not compatible', $details_summary_element
->getText());
}
$this
->assertFalse($update_element
->hasLink('Download'));
}
}