Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / update / tests / src / Functional / UpdateTestBase.php
index a4d07d0b682288230809868e72dda3a10623b2ef..1daa8801e0827b80d1c62c89de01735c7479a9fe 100644 (file)
@@ -25,6 +25,21 @@ use Drupal\Tests\BrowserTestBase;
  */
 abstract class UpdateTestBase extends BrowserTestBase {
 
+  /**
+   * Denotes a security update will be required in the test case.
+   */
+  const SECURITY_UPDATE_REQUIRED = 'SECURITY_UPDATE_REQUIRED';
+
+  /**
+   * Denotes an update will be available in the test case.
+   */
+  const UPDATE_AVAILABLE = 'UPDATE_AVAILABLE';
+
+  /**
+   * Denotes no update will be available in the test case.
+   */
+  const UPDATE_NONE = 'UPDATE_NONE';
+
   protected function setUp() {
     parent::setUp();
 
@@ -81,4 +96,75 @@ abstract class UpdateTestBase extends BrowserTestBase {
     $this->assertNoText(t('No available releases found'));
   }
 
+  /**
+   * Asserts the expected security updates are displayed correctly on the page.
+   *
+   * @param string $project_path_part
+   *   The project path part needed for the download and release links.
+   * @param string[] $expected_security_releases
+   *   The security releases, if any, that the status report should recommend.
+   * @param string $expected_update_message_type
+   *   The type of update message expected.
+   * @param string $update_element_css_locator
+   *   The CSS locator for the page element that contains the security updates.
+   */
+  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']"));
+    $all_security_download_urls = array_map(function ($link) {
+      return $link->getAttribute('href');
+    }, $page->findAll('css', "$update_element_css_locator .version-security a[href$='.tar.gz']"));
+    if ($expected_security_releases) {
+      $expected_download_urls = [];
+      $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', 'Error icon was found.');
+      }
+      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";
+        $download_url = "http://example.com/$project_path_part-$expected_url_version.tar.gz";
+        $expected_release_urls[] = $release_url;
+        $expected_download_urls[] = $download_url;
+        // Ensure the expected links are security links.
+        $this->assertTrue(in_array($release_url, $all_security_release_urls), "Release $release_url is a security release link.");
+        $this->assertTrue(in_array($download_url, $all_security_download_urls), "Release $download_url is a security download link.");
+        $assert_session->linkByHrefExists($release_url);
+        $assert_session->linkByHrefExists($download_url);
+      }
+      // Ensure no other links are shown as security releases.
+      $this->assertEquals([], array_diff($all_security_release_urls, $expected_release_urls));
+      $this->assertEquals([], array_diff($all_security_download_urls, $expected_download_urls));
+    }
+    else {
+      // Ensure there were no security links.
+      $this->assertEquals([], $all_security_release_urls);
+      $this->assertEquals([], $all_security_download_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);
+      }
+    }
+  }
+
 }