You are here

public function SecurityAdvisoriesFetcherTest::testIntervalConfigUpdate in Drupal 9

Same name and namespace in other branches
  1. 10 core/modules/system/tests/src/Kernel/SecurityAdvisories/SecurityAdvisoriesFetcherTest.php \Drupal\Tests\system\Kernel\SecurityAdvisories\SecurityAdvisoriesFetcherTest::testIntervalConfigUpdate()

Tests that the stored advisories response is deleted on interval decrease.

File

core/modules/system/tests/src/Kernel/SecurityAdvisories/SecurityAdvisoriesFetcherTest.php, line 536

Class

SecurityAdvisoriesFetcherTest
@coversDefaultClass \Drupal\system\SecurityAdvisories\SecurityAdvisoriesFetcher

Namespace

Drupal\Tests\system\Kernel\SecurityAdvisories

Code

public function testIntervalConfigUpdate() : void {
  $feed_item_1 = [
    'is_psa' => 1,
    'type' => 'core',
    'title' => 'Oh no🙀! Advisory 1',
    'project' => 'drupal',
    'insecure' => [
      \Drupal::VERSION,
    ],
  ];
  $feed_item_2 = [
    'is_psa' => 1,
    'type' => 'core',
    'title' => 'Oh no😱! Advisory 2',
    'project' => 'drupal',
    'insecure' => [
      \Drupal::VERSION,
    ],
  ];
  $this
    ->setFeedItems([
    $feed_item_1,
    $feed_item_2,
  ]);
  $advisories = $this
    ->getAdvisories();
  $this
    ->assertCount(1, $advisories);
  $this
    ->assertSame($feed_item_1['title'], $advisories[0]
    ->getTitle());
  $this
    ->assertCount(1, $this->history);

  // Ensure that new feed item is not retrieved because the stored response
  // has not expired.
  $advisories = $this
    ->getAdvisories();
  $this
    ->assertCount(1, $this->history);
  $this
    ->assertCount(1, $advisories);
  $this
    ->assertSame($feed_item_1['title'], $advisories[0]
    ->getTitle());

  /** @var \Drupal\Core\Config\Config $config */
  $config = $this->container
    ->get('config.factory')
    ->getEditable('system.advisories');
  $interval = $config
    ->get('interval_hours');
  $config
    ->set('interval_hours', $interval + 1)
    ->save();

  // Ensure that new feed item is not retrieved when the interval is
  // increased.
  $advisories = $this
    ->getAdvisories();
  $this
    ->assertCount(1, $this->history);
  $this
    ->assertCount(1, $advisories);
  $this
    ->assertSame($feed_item_1['title'], $advisories[0]
    ->getTitle());

  // Ensure that new feed item is retrieved when the interval is decreased.
  $config
    ->set('interval_hours', $interval - 1)
    ->save();
  $advisories = $this
    ->getAdvisories();
  $this
    ->assertCount(2, $this->history);
  $this
    ->assertCount(1, $advisories);
  $this
    ->assertSame($feed_item_2['title'], $advisories[0]
    ->getTitle());
}