From ece5a5f4ab112bc3b7627c07c666a956ca27d07f Mon Sep 17 00:00:00 2001
From: phenaproxima <phenaproxima@205645.no-reply.drupal.org>
Date: Wed, 6 Apr 2022 20:24:17 +0000
Subject: [PATCH] Issue #3273807 by phenaproxima: ProjectInfo should always
 refresh data

---
 src/CronUpdater.php                           |  2 +-
 src/Form/UpdaterForm.php                      |  4 +--
 src/ProjectInfo.php                           | 35 ++++++-------------
 src/ReleaseChooser.php                        | 11 ------
 src/Validation/ReadinessValidationManager.php |  3 --
 tests/src/Kernel/ReleaseChooserTest.php       |  1 -
 tests/src/Unit/ProjectInfoTest.php            |  8 ++++-
 7 files changed, 20 insertions(+), 44 deletions(-)

diff --git a/src/CronUpdater.php b/src/CronUpdater.php
index c5bf5c3507..cba9e6cc33 100644
--- a/src/CronUpdater.php
+++ b/src/CronUpdater.php
@@ -73,7 +73,7 @@ class CronUpdater extends Updater {
       return;
     }
 
-    $next_release = $this->releaseChooser->refresh()->getLatestInInstalledMinor();
+    $next_release = $this->releaseChooser->getLatestInInstalledMinor();
     if ($next_release) {
       $this->performUpdate($next_release->getVersion());
     }
diff --git a/src/Form/UpdaterForm.php b/src/Form/UpdaterForm.php
index 92b7cc300f..d121735fcb 100644
--- a/src/Form/UpdaterForm.php
+++ b/src/Form/UpdaterForm.php
@@ -139,7 +139,7 @@ class UpdaterForm extends FormBase {
       //   currently installed minor. Failing that, try to show the latest
       //   release in the next minor. If neither of those are available, just
       //   show the first available release.
-      $recommended_release = $this->releaseChooser->refresh()->getLatestInInstalledMinor();
+      $recommended_release = $this->releaseChooser->getLatestInInstalledMinor();
       if (!$recommended_release) {
         $recommended_release = $this->releaseChooser->getLatestInNextMinor();
         if (!$recommended_release) {
@@ -176,7 +176,7 @@ class UpdaterForm extends FormBase {
       ],
     ];
 
-    $project = $project_info->getProjectInfo('drupal');
+    $project = $project_info->getProjectInfo();
     if (empty($project['title']) || empty($project['link'])) {
       throw new \UnexpectedValueException('Expected project data to have a title and link.');
     }
diff --git a/src/ProjectInfo.php b/src/ProjectInfo.php
index e61253b2be..fce9a1ae6d 100644
--- a/src/ProjectInfo.php
+++ b/src/ProjectInfo.php
@@ -35,19 +35,14 @@ class ProjectInfo {
   /**
    * Returns up-to-date project information.
    *
-   * @param bool $refresh
-   *   (optional) Whether to fetch the latest information about available
-   *   updates from drupal.org. This can be an expensive operation, so defaults
-   *   to FALSE.
-   *
    * @return array|null
    *   The retrieved project information.
    *
    * @throws \RuntimeException
    *   If data about available updates cannot be retrieved.
    */
-  public function getProjectInfo(bool $refresh = FALSE): ?array {
-    $available_updates = update_get_available($refresh);
+  public function getProjectInfo(): ?array {
+    $available_updates = update_get_available(TRUE);
     $project_data = update_calculate_project_data($available_updates);
     return $project_data[$this->name] ?? NULL;
   }
@@ -55,11 +50,6 @@ class ProjectInfo {
   /**
    * Gets all project releases to which the site can update.
    *
-   * @param bool $refresh
-   *   (optional) Whether to fetch the latest information about available
-   *   updates from drupal.org. This can be an expensive operation, so defaults
-   *   to FALSE.
-   *
    * @return \Drupal\automatic_updates_9_3_shim\ProjectRelease[]|null
    *   If the project information is available, an array of releases that can be
    *   installed, keyed by version number; otherwise NULL. The releases are in
@@ -67,19 +57,19 @@ class ProjectInfo {
    *   first).
    *
    * @throws \RuntimeException
-   *   Thrown if $refresh is TRUE and there are no available releases.
+   *   Thrown if there are no available releases.
    *
    * @todo Remove or simplify this function in https://www.drupal.org/i/3252190.
    */
-  public function getInstallableReleases(bool $refresh = FALSE): ?array {
-    $project = $this->getProjectInfo($refresh);
+  public function getInstallableReleases(): ?array {
+    $project = $this->getProjectInfo();
     if (!$project) {
       return NULL;
     }
     $installed_version = $this->getInstalledVersion();
-    // If we refreshed and we were able to get available releases we should
-    // always have at least have the current release stored.
-    if ($refresh && empty($project['releases'])) {
+    // If we were able to get available releases we should always have at least
+    // the current release stored.
+    if (empty($project['releases'])) {
       throw new \RuntimeException('There was a problem getting update information. Try again later.');
     }
     // If we're already up-to-date, there's nothing else we need to do.
@@ -111,17 +101,12 @@ class ProjectInfo {
   /**
    * Returns the installed project version, according to the Update module.
    *
-   * @param bool $refresh
-   *   (optional) Whether to fetch the latest information about available
-   *   updates from drupal.org. This can be an expensive operation, so defaults
-   *   to FALSE.
-   *
    * @return string|null
    *   The installed project version as known to the Update module or NULL if
    *   the project information is not available.
    */
-  public function getInstalledVersion(bool $refresh = FALSE): ?string {
-    if ($project_data = $this->getProjectInfo($refresh)) {
+  public function getInstalledVersion(): ?string {
+    if ($project_data = $this->getProjectInfo()) {
       return $project_data['existing_version'];
     }
     return NULL;
diff --git a/src/ReleaseChooser.php b/src/ReleaseChooser.php
index b0edf92b28..6163320a2c 100644
--- a/src/ReleaseChooser.php
+++ b/src/ReleaseChooser.php
@@ -39,17 +39,6 @@ class ReleaseChooser {
     $this->projectInfo = new ProjectInfo('drupal');
   }
 
-  /**
-   * Refreshes the project information through the Update module.
-   *
-   * @return $this
-   *   The called object.
-   */
-  public function refresh(): self {
-    $this->projectInfo->getProjectInfo(TRUE);
-    return $this;
-  }
-
   /**
    * Returns the releases that are installable.
    *
diff --git a/src/Validation/ReadinessValidationManager.php b/src/Validation/ReadinessValidationManager.php
index 436569bc63..08e50d6f8e 100644
--- a/src/Validation/ReadinessValidationManager.php
+++ b/src/Validation/ReadinessValidationManager.php
@@ -4,7 +4,6 @@ namespace Drupal\automatic_updates\Validation;
 
 use Drupal\automatic_updates\CronUpdater;
 use Drupal\automatic_updates\Event\ReadinessCheckEvent;
-use Drupal\automatic_updates\ProjectInfo;
 use Drupal\automatic_updates\Updater;
 use Drupal\Component\Datetime\TimeInterface;
 use Drupal\Core\Config\ConfigFactoryInterface;
@@ -112,8 +111,6 @@ class ReadinessValidationManager implements EventSubscriberInterface {
       $stage = $this->cronUpdater;
     }
     $event = new ReadinessCheckEvent($stage);
-    // Version validators will need up-to-date project info.
-    (new ProjectInfo('drupal'))->getProjectInfo(TRUE);
     $this->eventDispatcher->dispatch($event);
     $results = $event->getResults();
     $this->keyValueExpirable->setWithExpire(
diff --git a/tests/src/Kernel/ReleaseChooserTest.php b/tests/src/Kernel/ReleaseChooserTest.php
index dac21432ae..9b4470b282 100644
--- a/tests/src/Kernel/ReleaseChooserTest.php
+++ b/tests/src/Kernel/ReleaseChooserTest.php
@@ -145,7 +145,6 @@ class ReleaseChooserTest extends AutomaticUpdatesKernelTestBase {
     $this->config('automatic_updates.settings')->set('allow_core_minor_updates', $minor_support)->save();
     /** @var \Drupal\automatic_updates\ReleaseChooser $chooser */
     $chooser = $this->container->get($chooser_service);
-    $chooser->refresh();
     $this->assertReleaseVersion($current_minor, $chooser->getLatestInInstalledMinor());
     $this->assertReleaseVersion($next_minor, $chooser->getLatestInNextMinor());
   }
diff --git a/tests/src/Unit/ProjectInfoTest.php b/tests/src/Unit/ProjectInfoTest.php
index 1b5eda2b6e..6578069d10 100644
--- a/tests/src/Unit/ProjectInfoTest.php
+++ b/tests/src/Unit/ProjectInfoTest.php
@@ -122,7 +122,7 @@ class ProjectInfoTest extends UnitTestCase {
           '8.2.4' => $release_objects['8.2.4'],
         ],
       ],
-      [
+      'no data' => [
         NULL,
         NULL,
       ],
@@ -141,6 +141,12 @@ class ProjectInfoTest extends UnitTestCase {
    */
   public function testGetInstallableReleases(?array $project_data, ?array $expected_releases): void {
     $project_info = $this->getMockedProjectInfo($project_data);
+
+    // If data is returned, but there are no releases, we should get an
+    // exception.
+    if (isset($project_data, $expected_releases) && empty($project_data['releases'])) {
+      $this->expectExceptionMessage('There was a problem getting update information. Try again later.');
+    }
     $this->assertEqualsCanonicalizing($expected_releases, $project_info->getInstallableReleases());
   }
 
-- 
GitLab