You are here

protected function UpdateTestBase::assertSecurityUpdates in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/update/tests/src/Functional/UpdateTestBase.php \Drupal\Tests\update\Functional\UpdateTestBase::assertSecurityUpdates()
  2. 9 core/modules/update/tests/src/Functional/UpdateTestBase.php \Drupal\Tests\update\Functional\UpdateTestBase::assertSecurityUpdates()

Asserts the expected security updates are displayed correctly on the page.

Parameters

string $project_path_part: The project path part needed for the release link.

string[] $expected_security_releases: The security releases, if any, that the status report should recommend.

string $expected_update_message_type: The type of update message expected.

string $update_element_css_locator: The CSS locator for the page element that contains the security updates.

File

core/modules/update/tests/src/Functional/UpdateTestBase.php, line 106

Class

UpdateTestBase
Defines some shared functions used by all update tests.

Namespace

Drupal\Tests\update\Functional

Code

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!');

      // Verify that the error icon is found.
      $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;

      // Ensure the expected links are security links.
      $this
        ->assertContains($release_url, $all_security_release_urls, "Release {$release_url} is a security release link.");
      $assert_session
        ->linkByHrefExists($release_url);
    }

    // Ensure no other links are shown as security releases.
    $this
      ->assertEquals([], array_diff($all_security_release_urls, $expected_release_urls));
  }
  else {

    // Ensure there were no security links.
    $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);
    }
  }
}