From 943ff96c16c0ad6fbe7a76f8b15089e35a6445a0 Mon Sep 17 00:00:00 2001
From: David Cameron <david@cadeyrn.us>
Date: Tue, 4 Jul 2023 08:50:50 -0500
Subject: [PATCH] Issue #1337898 by dcam, jessebeach, naxoc, kerby70, pwolanin,
 mgifford, mstef, larowlan: The watchdog message in aggregator.fetch.inc
 refers to $feed->title, a value that is not guaranteed to be known

---
 src/Plugin/aggregator/fetcher/DefaultFetcher.php | 11 +++++++++--
 src/Plugin/aggregator/parser/DefaultParser.php   |  6 +++++-
 tests/src/Functional/FeedParserTest.php          |  4 ++--
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/Plugin/aggregator/fetcher/DefaultFetcher.php b/src/Plugin/aggregator/fetcher/DefaultFetcher.php
index f306b9b4f..79eea0408 100644
--- a/src/Plugin/aggregator/fetcher/DefaultFetcher.php
+++ b/src/Plugin/aggregator/fetcher/DefaultFetcher.php
@@ -8,6 +8,7 @@ use Drupal\Component\Datetime\DateTimePlus;
 use Drupal\Core\Http\ClientFactory;
 use Drupal\Core\Messenger\MessengerInterface;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
 use GuzzleHttp\Exception\TransferException;
 use GuzzleHttp\Psr7\Request;
 use Psr\Http\Message\RequestInterface;
@@ -126,8 +127,14 @@ class DefaultFetcher implements FetcherInterface, ContainerFactoryPluginInterfac
       return TRUE;
     }
     catch (TransferException $e) {
-      $this->logger->warning('The feed from %site seems to be broken because of error "%error".', ['%site' => $feed->label(), '%error' => $e->getMessage()]);
-      $this->messenger->addWarning(t('The feed from %site seems to be broken because of error "%error".', ['%site' => $feed->label(), '%error' => $e->getMessage()]));
+      $this->logger->warning('The feed from %url seems to be broken because of error "%error".', [
+        '%url' => $feed->getUrl(),
+        '%error' => $e->getMessage(),
+      ]);
+      $this->messenger->addWarning(new TranslatableMarkup('The feed from %url seems to be broken because of error "%error".', [
+        '%url' => $feed->getUrl(),
+        '%error' => $e->getMessage(),
+      ]));
       return FALSE;
     }
   }
diff --git a/src/Plugin/aggregator/parser/DefaultParser.php b/src/Plugin/aggregator/parser/DefaultParser.php
index 66448fa5a..ff9e92310 100644
--- a/src/Plugin/aggregator/parser/DefaultParser.php
+++ b/src/Plugin/aggregator/parser/DefaultParser.php
@@ -5,6 +5,7 @@ namespace Drupal\aggregator\Plugin\aggregator\parser;
 use Drupal\aggregator\Plugin\ParserInterface;
 use Drupal\aggregator\FeedInterface;
 use Drupal\Core\Messenger\MessengerTrait;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
 use Laminas\Feed\Reader\Reader;
 use Laminas\Feed\Reader\Exception\ExceptionInterface;
 
@@ -34,7 +35,10 @@ class DefaultParser implements ParserInterface {
     }
     catch (ExceptionInterface $e) {
       watchdog_exception('aggregator', $e);
-      $this->messenger()->addError(t('The feed from %site seems to be broken because of error "%error".', ['%site' => $feed->label(), '%error' => $e->getMessage()]));
+      $this->messenger()->addError(new TranslatableMarkup('The feed from %url seems to be broken because of error "%error".', [
+        '%url' => $feed->getUrl(),
+        '%error' => $e->getMessage(),
+      ]));
 
       return FALSE;
     }
diff --git a/tests/src/Functional/FeedParserTest.php b/tests/src/Functional/FeedParserTest.php
index 916510b70..d8ed13223 100644
--- a/tests/src/Functional/FeedParserTest.php
+++ b/tests/src/Functional/FeedParserTest.php
@@ -112,14 +112,14 @@ class FeedParserTest extends AggregatorTestBase {
    */
   public function testInvalidFeed() {
     // Simulate a typo in the URL to force a curl exception.
-    $invalid_url = 'http:/www.drupal.org';
+    $invalid_url = 'https:/www.drupal.org';
     $feed = Feed::create(['url' => $invalid_url, 'title' => $this->randomMachineName()]);
     $feed->save();
 
     // Update the feed. Use the UI to be able to check the message easily.
     $this->drupalGet('admin/config/services/aggregator');
     $this->clickLink('Update items');
-    $this->assertSession()->pageTextContains('The feed from ' . $feed->label() . ' seems to be broken because of error');
+    $this->assertSession()->pageTextContains('The feed from ' . $feed->getUrl() . ' seems to be broken because of error');
   }
 
 }
-- 
GitLab