View source
<?php
namespace Drupal\Tests\update\Functional;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Utility\ProjectInfo;
use Drupal\update\UpdateManagerInterface;
class UpdateContribTest extends UpdateTestBase {
protected $updateTableLocator = 'table.update:nth-of-type(2)';
protected $updateProject = 'aaa_update_test';
public static $modules = [
'update_test',
'update',
'aaa_update_test',
'bbb_update_test',
'ccc_update_test',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
}
public function testNoReleasesAvailable() {
$system_info = [
'#all' => [
'version' => '8.0.0',
],
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
];
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->refreshUpdateStatus([
'drupal' => '0.0',
'aaa_update_test' => 'no-releases',
]);
$this
->drupalGet('admin/reports/updates');
$this
->assertRaw('<h3>' . t('Drupal core') . '</h3>');
$this
->assertRaw(Link::fromTextAndUrl(t('Drupal'), Url::fromUri('http://example.com/project/drupal'))
->toString());
$this
->assertText(t('Up to date'));
$this
->assertRaw('<h3>' . t('Modules') . '</h3>');
$this
->assertNoText(t('Update available'));
$this
->assertText(t('No available releases found'));
$this
->assertNoRaw(Link::fromTextAndUrl(t('AAA Update test'), Url::fromUri('http://example.com/project/aaa_update_test'))
->toString());
$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() {
$project_link = Link::fromTextAndUrl(t('AAA Update test'), Url::fromUri('http://example.com/project/aaa_update_test'))
->toString();
$system_info = [
'#all' => [
'version' => '8.0.0',
],
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
];
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->refreshUpdateStatus([
'drupal' => '0.0',
'aaa_update_test' => '1_0',
]);
$this
->standardTests();
$this
->assertText(t('Up to date'));
$this
->assertRaw('<h3>' . t('Modules') . '</h3>');
$this
->assertNoText(t('Update available'));
$this
->assertRaw($project_link, 'Link to aaa_update_test project appears.');
$system_info['aaa_update_test']['hidden'] = TRUE;
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->refreshUpdateStatus([
'drupal' => '0.0',
'aaa_update_test' => '1_0',
]);
$this
->assertNoRaw($project_link, 'Link to aaa_update_test project does not appear.');
$system_info['aaa_update_test']['package'] = 'aaa_update_test';
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->refreshUpdateStatus([
'drupal' => '0.0',
'aaa_update_test' => '1_0',
]);
$this
->assertRaw($project_link, 'Link to aaa_update_test project appears.');
}
public function testUpdateContribOrder() {
$system_info = [
'#all' => [
'version' => '8.0.0',
],
'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
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->refreshUpdateStatus([
'drupal' => '0.0',
'#all' => '1_0',
]);
$this
->standardTests();
$this
->assertText(t('Up to date'));
$this
->assertNoText(t('Update available'));
$this
->assertText(t('AAA Update test'));
$this
->assertText(t('BBB Update test'));
$this
->assertText(t('CCC Update test'));
$this
->assertNoRaw(Link::fromTextAndUrl(t('AAA Update test'), Url::fromUri('http://example.com/project/aaa_update_test'))
->toString(), 'Link to aaa_update_test project does not appear.');
$this
->assertRaw(Link::fromTextAndUrl(t('BBB Update test'), Url::fromUri('http://example.com/project/bbb_update_test'))
->toString(), 'Link to bbb_update_test project appears.');
$this
->assertRaw(Link::fromTextAndUrl(t('CCC Update test'), Url::fromUri('http://example.com/project/ccc_update_test'))
->toString(), 'Link to bbb_update_test project appears.');
$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
->assertTrue(strpos($this
->getSession()
->getPage()
->getContent(), $bbb_project_link) < strpos($this
->getSession()
->getPage()
->getContent(), $ccc_project_link), "'BBB Update test' project is listed before the 'CCC Update test' project");
}
public function testUpdateBaseThemeSecurityUpdate() {
\Drupal::service('theme_installer')
->install([
'update_test_subtheme',
]);
$system_info = [
'#all' => [
'version' => '8.0.0',
],
'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,
],
];
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$xml_mapping = [
'drupal' => '0.0',
'update_test_subtheme' => '1_0',
'update_test_basetheme' => '1_1-sec',
];
$this
->refreshUpdateStatus($xml_mapping);
$this
->assertText(t('Security update required!'));
$this
->assertRaw(Link::fromTextAndUrl(t('Update test base theme'), Url::fromUri('http://example.com/project/update_test_basetheme'))
->toString(), 'Link to the Update test base theme project appears.');
}
public function testNormalUpdateAvailable() {
$assert_session = $this
->assertSession();
$this
->drupalGet('admin/reports/updates/check');
$assert_session
->statusCodeEquals(403);
$system_info = [
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
];
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
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' => '0.0',
'aaa_update_test' => str_replace('.', '_', $version) . $extra_version,
]);
$this
->standardTests();
$this
->drupalGet('admin/reports/updates');
$this
->clickLink('Check manually');
$this
->checkForMetaRefresh();
$assert_session
->pageTextNotContains('Security update required!');
$this
->assertNoRaw('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 (preg_match('/^update_test_/', $theme)) {
$extension_config
->clear("theme.{$theme}");
}
}
$extension_config
->save();
$system_info = [
'#all' => [
'version' => '8.0.0',
],
'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();
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$xml_mapping = [
'drupal' => '0.0',
'update_test_subtheme' => '1_0',
'update_test_basetheme' => '1_1-sec',
];
$base_theme_project_link = Link::fromTextAndUrl(t('Update test base theme'), Url::fromUri('http://example.com/project/update_test_basetheme'))
->toString();
$sub_theme_project_link = Link::fromTextAndUrl(t('Update test subtheme'), Url::fromUri('http://example.com/project/update_test_subtheme'))
->toString();
foreach ([
TRUE,
FALSE,
] as $check_disabled) {
$update_settings
->set('check.disabled_extensions', $check_disabled)
->save();
$this
->refreshUpdateStatus($xml_mapping);
$this
->assertNoText(t('Themes'));
if ($check_disabled) {
$this
->assertText(t('Uninstalled themes'));
$this
->assertRaw($base_theme_project_link, 'Link to the Update test base theme project appears.');
$this
->assertRaw($sub_theme_project_link, 'Link to the Update test subtheme project appears.');
}
else {
$this
->assertNoText(t('Uninstalled themes'));
$this
->assertNoRaw($base_theme_project_link, 'Link to the Update test base theme project does not appear.');
$this
->assertNoRaw($sub_theme_project_link, 'Link to the Update test subtheme project does not appear.');
}
}
}
public function testUpdateHiddenBaseTheme() {
module_load_include('compare.inc', 'update');
\Drupal::service('theme_installer')
->install([
'update_test_subtheme',
]);
$system_info = [
'update_test_basetheme' => [
'project' => 'update_test_basetheme',
'hidden' => TRUE,
],
'update_test_subtheme' => [
'project' => 'update_test_subtheme',
'hidden' => FALSE,
],
];
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$projects = \Drupal::service('update.manager')
->getProjects();
$theme_data = \Drupal::service('theme_handler')
->rebuildThemeData();
$project_info = new ProjectInfo();
$project_info
->processInfoList($projects, $theme_data, 'theme', TRUE);
$this
->assertTrue(!empty($projects['update_test_basetheme']), 'Valid base theme (update_test_basetheme) was found.');
}
public function testUpdateBrokenFetchURL() {
$system_info = [
'#all' => [
'version' => '8.0.0',
],
'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
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->drupalGet('admin/reports/updates');
$xml_mapping = [
'drupal' => '0.0',
'aaa_update_test' => '1_0',
'bbb_update_test' => 'does-not-exist',
'ccc_update_test' => '1_0',
];
$this
->refreshUpdateStatus($xml_mapping);
$this
->assertText(t('Up to date'));
$this
->assertNoUniqueText(t('Up to date'));
$this
->assertNoText(t('Update available'));
$this
->assertRaw('<div class="project-update__status">' . t('Failed to get available update data'));
$this
->assertUniqueText(t('Checked available update data for 3 projects.'));
$this
->assertUniqueText(t('Failed to get available update data for one project.'));
$this
->assertRaw(Link::fromTextAndUrl(t('AAA Update test'), Url::fromUri('http://example.com/project/aaa_update_test'))
->toString(), 'Link to aaa_update_test project appears.');
$this
->assertNoRaw(Link::fromTextAndUrl(t('BBB Update test'), Url::fromUri('http://example.com/project/bbb_update_test'))
->toString(), 'Link to bbb_update_test project does not appear.');
$this
->assertRaw(Link::fromTextAndUrl(t('CCC Update test'), Url::fromUri('http://example.com/project/ccc_update_test'))
->toString(), 'Link to bbb_update_test project appears.');
}
public function testHookUpdateStatusAlter() {
$update_test_config = $this
->config('update_test.settings');
$update_admin_user = $this
->drupalCreateUser([
'administer site configuration',
'administer software updates',
]);
$this
->drupalLogin($update_admin_user);
$system_info = [
'#all' => [
'version' => '8.0.0',
],
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
];
$update_test_config
->set('system_info', $system_info)
->save();
$update_status = [
'aaa_update_test' => [
'status' => UpdateManagerInterface::NOT_SECURE,
],
];
$update_test_config
->set('update_status', $update_status)
->save();
$this
->refreshUpdateStatus([
'drupal' => '0.0',
'aaa_update_test' => '1_0',
]);
$this
->drupalGet('admin/reports/updates');
$this
->assertRaw('<h3>' . t('Modules') . '</h3>');
$this
->assertText(t('Security update required!'));
$this
->assertRaw(Link::fromTextAndUrl(t('AAA Update test'), Url::fromUri('http://example.com/project/aaa_update_test'))
->toString(), 'Link to aaa_update_test project appears.');
$update_test_config
->set('update_status', [])
->save();
$this
->drupalGet('admin/reports/updates');
$this
->assertRaw('<h3>' . t('Modules') . '</h3>');
$this
->assertNoText(t('Security update required!'));
$this
->assertRaw(Link::fromTextAndUrl(t('AAA Update test'), Url::fromUri('http://example.com/project/aaa_update_test'))
->toString(), 'Link to aaa_update_test project appears.');
$update_test_config
->set('update_status', $update_status)
->save();
$this
->drupalGet('admin/modules/update');
$this
->assertText(t('Security update'));
$update_test_config
->set('update_status', [])
->save();
$this
->drupalGet('admin/modules/update');
$this
->assertNoText(t('Security update'));
}
public function testCoreCompatibilityMessage() {
$system_info = [
'#all' => [
'version' => '8.0.0',
],
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
];
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->refreshUpdateStatus([
'drupal' => '1.1-core_compatibility',
'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' => '1.1-alpha1-core_compatibility',
'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' => '1.1-core_compatibility',
'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) {
$system_info = [
'#all' => [
'version' => '8.0.0',
],
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => $module_version,
'hidden' => FALSE,
],
];
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->refreshUpdateStatus([
'drupal' => '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 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() {
$system_info = [
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.0',
'hidden' => FALSE,
],
];
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->refreshUpdateStatus([
'drupal' => '0.0',
$this->updateProject => '1_0-supported',
]);
$this
->confirmRevokedStatus('8.x-1.0', '8.x-2.0', 'Also available:');
$this
->refreshUpdateStatus([
'drupal' => '0.0',
$this->updateProject => '1_0-unsupported',
]);
$this
->confirmRevokedStatus('8.x-1.0', '8.x-2.0', 'Recommended version:');
}
public function testUnsupportedRelease() {
$system_info = [
'aaa_update_test' => [
'project' => 'aaa_update_test',
'version' => '8.x-1.1',
'hidden' => FALSE,
],
];
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->refreshUpdateStatus([
'drupal' => '0.0',
$this->updateProject => '1_0-supported',
]);
$this
->confirmUnsupportedStatus('8.x-1.1', '8.x-2.0', 'Also available:');
$this
->refreshUpdateStatus([
'drupal' => '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) {
$system_info = [
'aaa_update_test' => [
'project' => 'aaa_update_test',
'hidden' => FALSE,
],
];
if (isset($version_info['version'])) {
$system_info['aaa_update_test']['version'] = $version_info['version'];
}
$this
->config('update_test.settings')
->set('system_info', $system_info)
->save();
$this
->refreshUpdateStatus([
'drupal' => '0.0',
$this->updateProject => '1_0-supported',
]);
$this
->standardTests();
$this
->assertSession()
->elementTextContains('css', $this->updateTableLocator, $version_info['expected']);
}
}
protected function assertCoreCompatibilityMessage($version, $expected_range, $expected_release_title, $is_compatible = TRUE) {
$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) {
$download_version = str_replace('.', '-', $version);
$this
->assertFalse($compatibility_details
->hasAttribute('open'));
$this
->assertSame('Compatible', $details_summary_element
->getText());
$this
->assertEquals($update_element
->findLink('Download')
->getAttribute('href'), "http://example.com/{$this->updateProject}-{$download_version}.tar.gz");
}
else {
$this
->assertTrue($compatibility_details
->hasAttribute('open'));
$this
->assertSame('Not compatible', $details_summary_element
->getText());
$this
->assertFalse($update_element
->hasLink('Download'));
}
}
}