Commit 02041b35 authored by Kunal Sachdev's avatar Kunal Sachdev Committed by Adam G-H
Browse files

Issue #3275316 by kunal.sachdev: Change...

Issue #3275316 by kunal.sachdev: Change AutomaticUpdatesKernelTestBase::setReleaseMetadata() to return metadata for different projects based on request path
parent 59b4aba1
Loading
Loading
Loading
Loading
+15 −5
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\RequestInterface;

/**
 * Base class for kernel tests of the Automatic Updates module.
@@ -67,7 +68,7 @@ abstract class AutomaticUpdatesKernelTestBase extends PackageManagerKernelTestBa
    // By default, pretend we're running Drupal core 9.8.0 and a non-security
    // update to 9.8.1 is available.
    $this->setCoreVersion('9.8.1');
    $this->setReleaseMetadata([__DIR__ . '/../../fixtures/release-history/drupal.9.8.2.xml']);
    $this->setReleaseMetadata(['drupal' => __DIR__ . '/../../fixtures/release-history/drupal.9.8.2.xml']);

    // Set a last cron run time so that the cron frequency validator will run
    // from a sane state.
@@ -118,15 +119,24 @@ abstract class AutomaticUpdatesKernelTestBase extends PackageManagerKernelTestBa
   * Sets the release metadata file to use when fetching available updates.
   *
   * @param string[] $files
   *   The paths of the XML metadata files to use.
   *   The paths of the XML metadata files to use, keyed by project name.
   */
  protected function setReleaseMetadata(array $files): void {
    $responses = [];
    foreach ($files as $file) {

    foreach ($files as $project => $file) {
      $metadata = Utils::tryFopen($file, 'r');
      $responses[] = new Response(200, [], Utils::streamFor($metadata));
      $responses["/release-history/$project/current"] = new Response(200, [], Utils::streamFor($metadata));
    }
    $handler = new MockHandler($responses);
    $callable = function (RequestInterface $request) use ($responses): Response {
      return $responses[$request->getUri()->getPath()] ?? new Response(404);
    };

    // The mock handler's queue consist of same callable as many times as the
    // number of requests we expect to be made for update XML because it will
    // retrieve one item off the queue for each request.
    // @see \GuzzleHttp\Handler\MockHandler::__invoke()
    $handler = new MockHandler(array_fill(0, count($responses), $callable));
    $this->client = new Client([
      'handler' => HandlerStack::create($handler),
    ]);
+12 −11
Original line number Diff line number Diff line
@@ -89,32 +89,32 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
    return [
      'disabled, normal release' => [
        CronUpdater::DISABLED,
        "$fixture_dir/drupal.9.8.2.xml",
        ['drupal' => "$fixture_dir/drupal.9.8.2.xml"],
        FALSE,
      ],
      'disabled, security release' => [
        CronUpdater::DISABLED,
        "$fixture_dir/drupal.9.8.1-security.xml",
        ['drupal' => "$fixture_dir/drupal.9.8.1-security.xml"],
        FALSE,
      ],
      'security only, security release' => [
        CronUpdater::SECURITY,
        "$fixture_dir/drupal.9.8.1-security.xml",
        ['drupal' => "$fixture_dir/drupal.9.8.1-security.xml"],
        TRUE,
      ],
      'security only, normal release' => [
        CronUpdater::SECURITY,
        "$fixture_dir/drupal.9.8.2.xml",
        ['drupal' => "$fixture_dir/drupal.9.8.2.xml"],
        FALSE,
      ],
      'enabled, normal release' => [
        CronUpdater::ALL,
        "$fixture_dir/drupal.9.8.2.xml",
        ['drupal' => "$fixture_dir/drupal.9.8.2.xml"],
        TRUE,
      ],
      'enabled, security release' => [
        CronUpdater::ALL,
        "$fixture_dir/drupal.9.8.1-security.xml",
        ['drupal' => "$fixture_dir/drupal.9.8.1-security.xml"],
        TRUE,
      ],
    ];
@@ -126,18 +126,19 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
   * @param string $setting
   *   Whether automatic updates should be enabled during cron. Possible values
   *   are 'disable', 'security', and 'patch'.
   * @param string $release_data
   * @param array $release_data
   *   If automatic updates are enabled, the path of the fake release metadata
   *   that should be served when fetching information on available updates.
   *   that should be served when fetching information on available updates,
   *   keyed by project name.
   * @param bool $will_update
   *   Whether an update should be performed, given the previous two arguments.
   *
   * @dataProvider providerUpdaterCalled
   */
  public function testUpdaterCalled(string $setting, string $release_data, bool $will_update): void {
  public function testUpdaterCalled(string $setting, array $release_data, bool $will_update): void {
    // Our form alter does not refresh information on available updates, so
    // ensure that the appropriate update data is loaded beforehand.
    $this->setReleaseMetadata([$release_data]);
    $this->setReleaseMetadata($release_data);
    $this->setCoreVersion('9.8.0');
    update_get_available(TRUE);

@@ -247,7 +248,7 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
    $this->installConfig('automatic_updates');
    $this->setCoreVersion('9.8.0');
    // Ensure that there is a security release to which we should update.
    $this->setReleaseMetadata([__DIR__ . "/../../fixtures/release-history/drupal.9.8.1-security.xml"]);
    $this->setReleaseMetadata(['drupal' => __DIR__ . "/../../fixtures/release-history/drupal.9.8.1-security.xml"]);

    // If the pre- or post-destroy events throw an exception, it will not be
    // caught by the cron updater, but it *will* be caught by the main cron
+2 −36
Original line number Diff line number Diff line
@@ -3,12 +3,6 @@
namespace Drupal\Tests\automatic_updates\Kernel;

use Drupal\automatic_updates\ProjectInfo;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\RequestInterface;

/**
 * @coversDefaultClass \Drupal\automatic_updates\ProjectInfo
@@ -49,7 +43,7 @@ class ProjectInfoTest extends AutomaticUpdatesKernelTestBase {
      $metadata_fixtures['drupal'] = $fixtures_directory . 'drupal.9.8.2.xml';
    }
    $metadata_fixtures[$project] = "$fixtures_directory$fixture";
    $this->setReleaseMetadataForProjects($metadata_fixtures);
    $this->setReleaseMetadata($metadata_fixtures);
    $project_info = new ProjectInfo($project);
    $actual_releases = $project_info->getInstallableReleases();
    // Assert that we returned the correct releases in the expected order.
@@ -119,39 +113,11 @@ class ProjectInfoTest extends AutomaticUpdatesKernelTestBase {
   * @covers ::getInstallableReleases()
   */
  public function testNotPublishedProject() {
    $this->setReleaseMetadataForProjects(['drupal' => __DIR__ . '/../../fixtures/release-history/drupal.9.8.2_unknown_status.xml']);
    $this->setReleaseMetadata(['drupal' => __DIR__ . '/../../fixtures/release-history/drupal.9.8.2_unknown_status.xml']);
    $project_info = new ProjectInfo('drupal');
    $this->expectException('RuntimeException');
    $this->expectExceptionMessage("The project 'drupal' can not be updated because its status is any status besides published");
    $project_info->getInstallableReleases();
  }

  /**
   * Sets the release metadata file to use when fetching available updates.
   *
   * @param string[] $files
   *   The paths of the XML metadata files to use, keyed by project name.
   */
  protected function setReleaseMetadataForProjects(array $files): void {
    $responses = [];

    foreach ($files as $project => $file) {
      $metadata = Utils::tryFopen($file, 'r');
      $responses["/release-history/$project/current"] = new Response(200, [], Utils::streamFor($metadata));
    }
    $callable = function (RequestInterface $request) use ($responses): Response {
      return $responses[$request->getUri()->getPath()] ?? new Response(404);
    };

    // The mock handler's queue consist of same callable as many times as the
    // number of requests we expect to be made for update XML because it will
    // retrieve one item off the queue for each request.
    // @see \GuzzleHttp\Handler\MockHandler::__invoke()
    $handler = new MockHandler(array_fill(0, count($responses), $callable));
    $this->client = new Client([
      'handler' => HandlerStack::create($handler),
    ]);
    $this->container->set('http_client', $this->client);
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -131,7 +131,7 @@ class UpdateVersionValidatorTest extends AutomaticUpdatesKernelTestBase {
    // In order to test what happens when only security updates are enabled
    // during cron (the default behavior), ensure that the latest available
    // release is a security update.
    $this->setReleaseMetadata([__DIR__ . '/../../../fixtures/release-history/drupal.9.8.1-security.xml']);
    $this->setReleaseMetadata(['drupal' => __DIR__ . '/../../../fixtures/release-history/drupal.9.8.1-security.xml']);

    $this->setCoreVersion('9.7.1');
    $this->assertCheckerResultsFromManager([], TRUE);
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ class ReleaseChooserTest extends AutomaticUpdatesKernelTestBase {
   */
  protected function setUp(): void {
    parent::setUp();
    $this->setReleaseMetadata([__DIR__ . '/../../fixtures/release-history/drupal.9.8.2-older-sec-release.xml']);
    $this->setReleaseMetadata(['drupal' => __DIR__ . '/../../fixtures/release-history/drupal.9.8.2-older-sec-release.xml']);

  }

Loading