You are here

public function SecurityAdvisoriesFetcherTest::testInvalidJsonResponse in Drupal 9

Tests that invalid JSON feed responses are not stored.

File

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

Class

SecurityAdvisoriesFetcherTest
@coversDefaultClass \Drupal\system\SecurityAdvisories\SecurityAdvisoriesFetcher

Namespace

Drupal\Tests\system\Kernel\SecurityAdvisories

Code

public function testInvalidJsonResponse() : void {
  $non_json_response = new Response(200, [], '1');
  $json_response = new Response(200, [], '[]');

  // Set 2 non-JSON responses and 1 JSON response.
  $this
    ->setTestFeedResponses([
    $non_json_response,
    $non_json_response,
    $json_response,
  ]);
  $this
    ->assertNull($this
    ->getAdvisories());
  $this
    ->assertCount(1, $this->history);
  $this
    ->assertServiceAdvisoryLoggedErrors([
    'The security advisory JSON feed from Drupal.org could not be decoded.',
  ]);

  // Confirm that previous non-JSON response was not stored.
  $this
    ->assertNull($this
    ->getAdvisories());
  $this
    ->assertCount(2, $this->history);
  $this
    ->assertServiceAdvisoryLoggedErrors([
    'The security advisory JSON feed from Drupal.org could not be decoded.',
  ]);

  // Confirm that if $allow_http_request is set to FALSE a new request will
  // not be attempted.
  $this
    ->assertNull($this
    ->getAdvisories(FALSE));
  $this
    ->assertCount(2, $this->history);

  // Make a 3rd request that will return a valid JSON response.
  $this
    ->assertCount(0, $this
    ->getAdvisories());
  $this
    ->assertCount(3, $this->history);

  // Confirm that getting the advisories after a valid JSON response will use
  // the stored response and not make another 'http_client' request.
  $this
    ->assertCount(0, $this
    ->getAdvisories());
  $this
    ->assertCount(3, $this->history);
  $this
    ->assertServiceAdvisoryLoggedErrors([]);
}