annotate core/modules/update/tests/src/Functional/UpdateTestBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\update\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\DrupalKernel;
Chris@0 6 use Drupal\Core\Url;
Chris@0 7 use Drupal\Tests\BrowserTestBase;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Defines some shared functions used by all update tests.
Chris@0 11 *
Chris@0 12 * The overarching methodology of these tests is we need to compare a given
Chris@0 13 * state of installed modules and themes (e.g., version, project grouping,
Chris@0 14 * timestamps, etc) against a current state of what the release history XML
Chris@0 15 * files we fetch say is available. We have dummy XML files (in the
Chris@0 16 * core/modules/update/tests directory) that describe various scenarios of
Chris@0 17 * what's available for different test projects, and we have dummy .info file
Chris@0 18 * data (specified via hook_system_info_alter() in the update_test helper
Chris@0 19 * module) describing what's currently installed. Each test case defines a set
Chris@0 20 * of projects to install, their current state (via the
Chris@0 21 * 'update_test_system_info' variable) and the desired available update data
Chris@0 22 * (via the 'update_test_xml_map' variable), and then performs a series of
Chris@0 23 * assertions that the report matches our expectations given the specific
Chris@0 24 * initial state and availability scenario.
Chris@0 25 */
Chris@0 26 abstract class UpdateTestBase extends BrowserTestBase {
Chris@0 27
Chris@17 28 /**
Chris@17 29 * Denotes a security update will be required in the test case.
Chris@17 30 */
Chris@17 31 const SECURITY_UPDATE_REQUIRED = 'SECURITY_UPDATE_REQUIRED';
Chris@17 32
Chris@17 33 /**
Chris@17 34 * Denotes an update will be available in the test case.
Chris@17 35 */
Chris@17 36 const UPDATE_AVAILABLE = 'UPDATE_AVAILABLE';
Chris@17 37
Chris@17 38 /**
Chris@17 39 * Denotes no update will be available in the test case.
Chris@17 40 */
Chris@17 41 const UPDATE_NONE = 'UPDATE_NONE';
Chris@17 42
Chris@0 43 protected function setUp() {
Chris@0 44 parent::setUp();
Chris@0 45
Chris@0 46 // Change the root path which Update Manager uses to install and update
Chris@0 47 // projects to be inside the testing site directory. See
Chris@0 48 // \Drupal\update\UpdateRootFactory::get() for equivalent changes to the
Chris@0 49 // test child site.
Chris@0 50 $request = \Drupal::request();
Chris@0 51 $update_root = $this->container->get('update.root') . '/' . DrupalKernel::findSitePath($request);
Chris@0 52 $this->container->set('update.root', $update_root);
Chris@0 53 \Drupal::setContainer($this->container);
Chris@0 54
Chris@0 55 // Create the directories within the root path within which the Update
Chris@0 56 // Manager will install projects.
Chris@0 57 foreach (drupal_get_updaters() as $updater_info) {
Chris@0 58 $updater = $updater_info['class'];
Chris@0 59 $install_directory = $update_root . '/' . $updater::getRootDirectoryRelativePath();
Chris@0 60 if (!is_dir($install_directory)) {
Chris@0 61 mkdir($install_directory);
Chris@0 62 }
Chris@0 63 }
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * Refreshes the update status based on the desired available update scenario.
Chris@0 68 *
Chris@0 69 * @param $xml_map
Chris@0 70 * Array that maps project names to availability scenarios to fetch. The key
Chris@0 71 * '#all' is used if a project-specific mapping is not defined.
Chris@0 72 * @param $url
Chris@0 73 * (optional) A string containing the URL to fetch update data from.
Chris@0 74 * Defaults to 'update-test'.
Chris@0 75 *
Chris@0 76 * @see \Drupal\update_test\Controller\UpdateTestController::updateTest()
Chris@0 77 */
Chris@0 78 protected function refreshUpdateStatus($xml_map, $url = 'update-test') {
Chris@0 79 // Tell the Update Manager module to fetch from the URL provided by
Chris@0 80 // update_test module.
Chris@0 81 $this->config('update.settings')->set('fetch.url', Url::fromUri('base:' . $url, ['absolute' => TRUE])->toString())->save();
Chris@0 82 // Save the map for UpdateTestController::updateTest() to use.
Chris@0 83 $this->config('update_test.settings')->set('xml_map', $xml_map)->save();
Chris@0 84 // Manually check the update status.
Chris@0 85 $this->drupalGet('admin/reports/updates');
Chris@0 86 $this->clickLink(t('Check manually'));
Chris@0 87 $this->checkForMetaRefresh();
Chris@0 88 }
Chris@0 89
Chris@0 90 /**
Chris@0 91 * Runs a series of assertions that are applicable to all update statuses.
Chris@0 92 */
Chris@0 93 protected function standardTests() {
Chris@0 94 $this->assertRaw('<h3>' . t('Drupal core') . '</h3>');
Chris@0 95 $this->assertRaw(\Drupal::l(t('Drupal'), Url::fromUri('http://example.com/project/drupal')), 'Link to the Drupal project appears.');
Chris@0 96 $this->assertNoText(t('No available releases found'));
Chris@0 97 }
Chris@0 98
Chris@17 99 /**
Chris@17 100 * Asserts the expected security updates are displayed correctly on the page.
Chris@17 101 *
Chris@17 102 * @param string $project_path_part
Chris@17 103 * The project path part needed for the download and release links.
Chris@17 104 * @param string[] $expected_security_releases
Chris@17 105 * The security releases, if any, that the status report should recommend.
Chris@17 106 * @param string $expected_update_message_type
Chris@17 107 * The type of update message expected.
Chris@17 108 * @param string $update_element_css_locator
Chris@17 109 * The CSS locator for the page element that contains the security updates.
Chris@17 110 */
Chris@17 111 protected function assertSecurityUpdates($project_path_part, array $expected_security_releases, $expected_update_message_type, $update_element_css_locator) {
Chris@17 112 $assert_session = $this->assertSession();
Chris@17 113 $page = $this->getSession()->getPage();
Chris@17 114 $this->standardTests();
Chris@17 115 $assert_session->elementTextNotContains('css', $update_element_css_locator, 'Not supported');
Chris@17 116 $all_security_release_urls = array_map(function ($link) {
Chris@17 117 return $link->getAttribute('href');
Chris@17 118 }, $page->findAll('css', "$update_element_css_locator .version-security a[href$='-release']"));
Chris@17 119 $all_security_download_urls = array_map(function ($link) {
Chris@17 120 return $link->getAttribute('href');
Chris@17 121 }, $page->findAll('css', "$update_element_css_locator .version-security a[href$='.tar.gz']"));
Chris@17 122 if ($expected_security_releases) {
Chris@17 123 $expected_download_urls = [];
Chris@17 124 $expected_release_urls = [];
Chris@17 125 if ($expected_update_message_type === static::SECURITY_UPDATE_REQUIRED) {
Chris@17 126 $assert_session->elementTextNotContains('css', $update_element_css_locator, 'Update available');
Chris@17 127 $assert_session->elementTextContains('css', $update_element_css_locator, 'Security update required!');
Chris@17 128 $assert_session->responseContains('error.svg', 'Error icon was found.');
Chris@17 129 }
Chris@17 130 else {
Chris@17 131 $assert_session->elementTextContains('css', $update_element_css_locator, 'Update available');
Chris@17 132 $assert_session->elementTextNotContains('css', $update_element_css_locator, 'Security update required!');
Chris@17 133 }
Chris@17 134 $assert_session->elementTextNotContains('css', $update_element_css_locator, 'Up to date');
Chris@17 135 foreach ($expected_security_releases as $expected_security_release) {
Chris@17 136 $expected_url_version = str_replace('.', '-', $expected_security_release);
Chris@17 137 $release_url = "http://example.com/$project_path_part-$expected_url_version-release";
Chris@17 138 $download_url = "http://example.com/$project_path_part-$expected_url_version.tar.gz";
Chris@17 139 $expected_release_urls[] = $release_url;
Chris@17 140 $expected_download_urls[] = $download_url;
Chris@17 141 // Ensure the expected links are security links.
Chris@17 142 $this->assertTrue(in_array($release_url, $all_security_release_urls), "Release $release_url is a security release link.");
Chris@17 143 $this->assertTrue(in_array($download_url, $all_security_download_urls), "Release $download_url is a security download link.");
Chris@17 144 $assert_session->linkByHrefExists($release_url);
Chris@17 145 $assert_session->linkByHrefExists($download_url);
Chris@17 146 }
Chris@17 147 // Ensure no other links are shown as security releases.
Chris@17 148 $this->assertEquals([], array_diff($all_security_release_urls, $expected_release_urls));
Chris@17 149 $this->assertEquals([], array_diff($all_security_download_urls, $expected_download_urls));
Chris@17 150 }
Chris@17 151 else {
Chris@17 152 // Ensure there were no security links.
Chris@17 153 $this->assertEquals([], $all_security_release_urls);
Chris@17 154 $this->assertEquals([], $all_security_download_urls);
Chris@17 155 $assert_session->pageTextNotContains('Security update required!');
Chris@17 156 if ($expected_update_message_type === static::UPDATE_AVAILABLE) {
Chris@17 157 $assert_session->elementTextContains('css', $update_element_css_locator, 'Update available');
Chris@17 158 $assert_session->elementTextNotContains('css', $update_element_css_locator, 'Up to date');
Chris@17 159 }
Chris@17 160 elseif ($expected_update_message_type === static::UPDATE_NONE) {
Chris@17 161 $assert_session->elementTextNotContains('css', $update_element_css_locator, 'Update available');
Chris@17 162 $assert_session->elementTextContains('css', $update_element_css_locator, 'Up to date');
Chris@17 163 }
Chris@17 164 else {
Chris@17 165 $this->fail('Unexpected value for $expected_update_message_type: ' . $expected_update_message_type);
Chris@17 166 }
Chris@17 167 }
Chris@17 168 }
Chris@17 169
Chris@0 170 }