Verified Commit fab2a835 authored by Dave Long's avatar Dave Long
Browse files

Issue #3264167 by Spokje, neclimdul, heykarthikwithu, longwave: Remove use of...

Issue #3264167 by Spokje, neclimdul, heykarthikwithu, longwave: Remove use of final Guzzle client class

(cherry picked from commit cc218774)
parent 9041793c
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
services:
  http_client.advisory_feed_test:
    public: false
    class: Drupal\advisory_feed_test\AdvisoriesTestHttpClient
    decorates: http_client
    arguments: ['@http_client.advisory_feed_test.inner']
    class: Drupal\advisory_feed_test\AdvisoryTestClientMiddleware
    tags:
      - { name: http_client_middleware }
  logger.advisory_feed_test:
    public: false
    class: Drupal\advisory_feed_test\TestSystemLoggerChannel
+47 −0
Original line number Diff line number Diff line
@@ -2,43 +2,31 @@

namespace Drupal\advisory_feed_test;

use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;

/**
 * Provides a decorator service for the 'http_client' service for testing.
 * Overrides the User-Agent HTTP header for outbound HTTP requests.
 */
class AdvisoriesTestHttpClient extends Client {
class AdvisoryTestClientMiddleware {

  /**
   * The decorated http_client service.
   *
   * @var \GuzzleHttp\Client
   */
  protected $innerClient;

  /**
   * Constructs an AdvisoriesTestHttpClient object.
   *
   * @param \GuzzleHttp\Client $client
   *   The decorated http_client service.
   */
  public function __construct(Client $client) {
    $this->innerClient = $client;
  }

  /**
   * {@inheritdoc}
   * HTTP middleware that replaces URI for advisory requests.
   */
  public function get($uri, array $options = []): ResponseInterface {
  public function __invoke() {
    return function ($handler) {
      return function (RequestInterface $request, array $options) use ($handler): PromiseInterface {
        $test_end_point = \Drupal::state()->get('advisories_test_endpoint');
    if ($test_end_point && strpos($uri, '://updates.drupal.org/psa.json') !== FALSE) {
        if ($test_end_point && strpos($request->getUri(), '://updates.drupal.org/psa.json') !== FALSE) {
          // Only override $uri if it matches the advisories JSON feed to avoid
          // changing any other uses of the 'http_client' service during tests with
          // this module installed.
      $uri = $test_end_point;
          $request = $request->withUri(new Uri($test_end_point));
        }
    return $this->innerClient->get($uri, $options);
        return $handler($request, $options);
      };
    };
  }

  /**
+6 −6
Original line number Diff line number Diff line
@@ -2,10 +2,10 @@

namespace Drupal\Tests\system\Functional\SecurityAdvisories;

use Drupal\advisory_feed_test\AdvisoryTestClientMiddleware;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\advisory_feed_test\AdvisoriesTestHttpClient;

/**
 * Tests of security advisories functionality.
@@ -116,7 +116,7 @@ protected function writeSettings(array $settings): void {
  public function testPsa(): void {
    $assert = $this->assertSession();
    // Setup test PSA endpoint.
    AdvisoriesTestHttpClient::setTestEndpoint($this->workingEndpointMixed);
    AdvisoryTestClientMiddleware::setTestEndpoint($this->workingEndpointMixed);
    $mixed_advisory_links = [
      'Critical Release - SA-2019-02-19',
      'Critical Release - PSA-Really Old',
@@ -153,7 +153,7 @@ public function testPsa(): void {
    // Log back in with user with permission to see the advisories.
    $this->drupalLogin($this->user);
    // Test cache.
    AdvisoriesTestHttpClient::setTestEndpoint($this->nonWorkingEndpoint);
    AdvisoryTestClientMiddleware::setTestEndpoint($this->nonWorkingEndpoint);
    $this->assertAdminPageLinks($mixed_advisory_links, REQUIREMENT_ERROR);
    $this->assertStatusReportLinks($mixed_advisory_links, REQUIREMENT_ERROR);

@@ -166,7 +166,7 @@ public function testPsa(): void {
    $assert->pageTextContains('Failed to fetch security advisory data:');

    // Test a PSA endpoint that returns invalid JSON.
    AdvisoriesTestHttpClient::setTestEndpoint($this->invalidJsonEndpoint, TRUE);
    AdvisoryTestClientMiddleware::setTestEndpoint($this->invalidJsonEndpoint, TRUE);
    // Assert that are no logged error messages before attempting to fetch the
    // invalid endpoint.
    $this->assertServiceAdvisoryLoggedErrors([]);
@@ -180,7 +180,7 @@ public function testPsa(): void {
    // Assert the error was logged again.
    $this->assertServiceAdvisoryLoggedErrors(['The security advisory JSON feed from Drupal.org could not be decoded.']);

    AdvisoriesTestHttpClient::setTestEndpoint($this->workingEndpointPsaOnly, TRUE);
    AdvisoryTestClientMiddleware::setTestEndpoint($this->workingEndpointPsaOnly, TRUE);
    $psa_advisory_links = [
      'Critical Release - PSA-Really Old',
      'Generic Module2 project - Moderately critical - Access bypass - SA-CONTRIB-2019-02-02',
@@ -193,7 +193,7 @@ public function testPsa(): void {
    $this->assertStatusReportLinks($psa_advisory_links, REQUIREMENT_WARNING);
    $this->assertAdminPageLinks($psa_advisory_links, REQUIREMENT_WARNING);

    AdvisoriesTestHttpClient::setTestEndpoint($this->workingEndpointNonPsaOnly, TRUE);
    AdvisoryTestClientMiddleware::setTestEndpoint($this->workingEndpointNonPsaOnly, TRUE);
    $non_psa_advisory_links = [
      'Critical Release - SA-2019-02-19',
      'Generic Module1 Project - Moderately critical - Access bypass - SA-CONTRIB-2019-02-02',
+0 −5
Original line number Diff line number Diff line
@@ -2185,11 +2185,6 @@ parameters:
			count: 1
			path: modules/system/system.install

		-
			message: "#^Class Drupal\\\\advisory_feed_test\\\\AdvisoriesTestHttpClient extends @final class GuzzleHttp\\\\Client\\.$#"
			count: 1
			path: modules/system/tests/modules/advisory_feed_test/src/AdvisoriesTestHttpClient.php

		-
			message: "#^Variable \\$messages might not be defined\\.$#"
			count: 1