View source
<?php
namespace Drupal\Tests\update\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
abstract class UpdateTestBase extends BrowserTestBase {
const SECURITY_UPDATE_REQUIRED = 'SECURITY_UPDATE_REQUIRED';
const UPDATE_AVAILABLE = 'UPDATE_AVAILABLE';
const UPDATE_NONE = 'UPDATE_NONE';
protected $updateTableLocator;
protected $updateProject;
protected function refreshUpdateStatus($xml_map, $url = 'update-test') {
$this
->config('update.settings')
->set('fetch.url', Url::fromUri('base:' . $url, [
'absolute' => TRUE,
])
->toString())
->save();
$this
->config('update_test.settings')
->set('xml_map', $xml_map)
->save();
$this
->drupalGet('admin/reports/updates');
$this
->clickLink('Check manually');
$this
->checkForMetaRefresh();
}
protected function standardTests() {
$this
->assertSession()
->responseContains('<h3>Drupal core</h3>');
$this
->assertSession()
->linkExists('Drupal');
$this
->assertSession()
->linkByHrefExists('http://example.com/project/drupal');
$this
->assertSession()
->pageTextNotContains('No available releases found');
$this
->assertSession()
->pageTextContains('Last checked:');
$this
->assertSession()
->responseNotContains('.tar.gz');
}
protected function assertSecurityUpdates($project_path_part, array $expected_security_releases, $expected_update_message_type, $update_element_css_locator) {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->standardTests();
$assert_session
->elementTextNotContains('css', $update_element_css_locator, 'Not supported');
$all_security_release_urls = array_map(function ($link) {
return $link
->getAttribute('href');
}, $page
->findAll('css', "{$update_element_css_locator} .version-security a[href\$='-release']"));
if ($expected_security_releases) {
$expected_release_urls = [];
if ($expected_update_message_type === static::SECURITY_UPDATE_REQUIRED) {
$assert_session
->elementTextNotContains('css', $update_element_css_locator, 'Update available');
$assert_session
->elementTextContains('css', $update_element_css_locator, 'Security update required!');
$assert_session
->responseContains('error.svg');
}
else {
$assert_session
->elementTextContains('css', $update_element_css_locator, 'Update available');
$assert_session
->elementTextNotContains('css', $update_element_css_locator, 'Security update required!');
}
$assert_session
->elementTextNotContains('css', $update_element_css_locator, 'Up to date');
foreach ($expected_security_releases as $expected_security_release) {
$expected_url_version = str_replace('.', '-', $expected_security_release);
$release_url = "http://example.com/{$project_path_part}-{$expected_url_version}-release";
$assert_session
->responseNotContains("http://example.com/{$project_path_part}-{$expected_url_version}.tar.gz");
$expected_release_urls[] = $release_url;
$this
->assertContains($release_url, $all_security_release_urls, "Release {$release_url} is a security release link.");
$assert_session
->linkByHrefExists($release_url);
}
$this
->assertEquals([], array_diff($all_security_release_urls, $expected_release_urls));
}
else {
$this
->assertEquals([], $all_security_release_urls);
$assert_session
->pageTextNotContains('Security update required!');
if ($expected_update_message_type === static::UPDATE_AVAILABLE) {
$assert_session
->elementTextContains('css', $update_element_css_locator, 'Update available');
$assert_session
->elementTextNotContains('css', $update_element_css_locator, 'Up to date');
}
elseif ($expected_update_message_type === static::UPDATE_NONE) {
$assert_session
->elementTextNotContains('css', $update_element_css_locator, 'Update available');
$assert_session
->elementTextContains('css', $update_element_css_locator, 'Up to date');
}
else {
$this
->fail('Unexpected value for $expected_update_message_type: ' . $expected_update_message_type);
}
}
}
protected function assertVersionUpdateLinks($label, $version) {
$update_element = $this
->findUpdateElementByLabel($label);
$url_version = str_replace('.', '-', $version);
$this
->assertEquals($update_element
->findLink($version)
->getAttribute('href'), "http://example.com/{$this->updateProject}-{$url_version}-release");
$this
->assertStringNotContainsString("http://example.com/{$this->updateProject}-{$version}.tar.gz", $update_element
->getOuterHtml());
$this
->assertEquals($update_element
->findLink('Release notes')
->getAttribute('href'), "http://example.com/{$this->updateProject}-{$url_version}-release");
}
protected function confirmRevokedStatus($revoked_version, $newer_version, $new_version_label) {
$this
->drupalGet('admin/reports/updates');
$this
->clickLink('Check manually');
$this
->checkForMetaRefresh();
$this
->assertUpdateTableTextContains('Revoked!');
$this
->assertUpdateTableTextContains($revoked_version);
$this
->assertUpdateTableElementContains('error.svg');
$this
->assertUpdateTableTextContains('Release revoked: Your currently installed release has been revoked, and is no longer available for download. Disabling everything included in this release or upgrading is strongly recommended!');
$this
->assertVersionUpdateLinks($new_version_label, $newer_version);
}
protected function confirmUnsupportedStatus($unsupported_version, $newer_version, $new_version_label) {
$this
->drupalGet('admin/reports/updates');
$this
->clickLink('Check manually');
$this
->checkForMetaRefresh();
$this
->assertUpdateTableTextContains('Not supported!');
$this
->assertUpdateTableTextContains($unsupported_version);
$this
->assertUpdateTableElementContains('error.svg');
$this
->assertUpdateTableTextContains('Release not supported: Your currently installed release is now unsupported, and is no longer available for download. Disabling everything included in this release or upgrading is strongly recommended!');
$this
->assertVersionUpdateLinks($new_version_label, $newer_version);
}
protected function assertUpdateTableTextContains($text) {
$this
->assertSession()
->elementTextContains('css', $this->updateTableLocator, $text);
}
protected function assertUpdateTableTextNotContains($text) {
$this
->assertSession()
->elementTextNotContains('css', $this->updateTableLocator, $text);
}
protected function assertUpdateTableElementContains($text) {
$this
->assertSession()
->elementContains('css', $this->updateTableLocator, $text);
}
protected function assertUpdateTableElementNotContains($text) {
$this
->assertSession()
->elementNotContains('css', $this->updateTableLocator, $text);
}
protected function findUpdateElementByLabel($label) {
$update_elements = $this
->getSession()
->getPage()
->findAll('css', $this->updateTableLocator . " .project-update__version:contains(\"{$label}\")");
$this
->assertCount(1, $update_elements);
return $update_elements[0];
}
}