From 259c083729209bb6fd6454e28e15a13966d0cf1c Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
Date: Wed, 12 Feb 2025 15:03:04 +0100
Subject: [PATCH] hotline#205425 : dedup record links and keep the one with a
 label

---
 VERSIONS_HOTLINE/205425                       |  1 +
 library/Class/Notice/Urls.php                 | 24 ++++++++++++++++++-
 .../TemplatesRecordsWithUrlsTest.php          | 23 ++++++++++++++++--
 3 files changed, 45 insertions(+), 3 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/205425

diff --git a/VERSIONS_HOTLINE/205425 b/VERSIONS_HOTLINE/205425
new file mode 100644
index 00000000000..03886e9a1d8
--- /dev/null
+++ b/VERSIONS_HOTLINE/205425
@@ -0,0 +1 @@
+- correctif #205425 : Magasin de thèmes: La description affiche qu'une seule fois le lien internet avec le bon libellé
\ No newline at end of file
diff --git a/library/Class/Notice/Urls.php b/library/Class/Notice/Urls.php
index 265d7014cc4..6266dffe2b4 100644
--- a/library/Class/Notice/Urls.php
+++ b/library/Class/Notice/Urls.php
@@ -96,13 +96,35 @@ class Class_Notice_Urls {
         $link_label = $data['valeur'];
     }
 
-    return $link_url
+    return ($link_url && !$this->_hasBeenReplacedOrIgnored($link_url, $link_label))
       ? (new Class_Notice_Field)->setUrl($this->_ensureHttp($link_url))
                                 ->setLabel($link_label)
       : null;
   }
 
 
+  protected function _hasBeenReplacedOrIgnored(string $url, ?string $label): bool
+  {
+    if (!$existing = $this->_getExisting($url))
+      return false;
+
+    if (!$existing->getLabel() && $label)
+      $existing->setLabel($label);
+
+    return true;
+  }
+
+
+  protected function _getExisting(string $url): ?Class_Notice_Field
+  {
+    foreach ($this->_urls as $key => $existing)
+      if ($url == $existing->get('Url') ?? '')
+        return $existing;
+
+    return null;
+  }
+
+
   public function _get856a() : array {
     if ($this->_notice->isCvs())
       return [];
diff --git a/tests/scenarios/Templates/TemplatesRecordsWithUrlsTest.php b/tests/scenarios/Templates/TemplatesRecordsWithUrlsTest.php
index 19a68de1329..c60b1f9386d 100644
--- a/tests/scenarios/Templates/TemplatesRecordsWithUrlsTest.php
+++ b/tests/scenarios/Templates/TemplatesRecordsWithUrlsTest.php
@@ -62,8 +62,7 @@ class TemplatesRecordsWithUrlsWithTextReplacementTest
   public function pageShouldContainsUrlMonSiteWebDotFrWithCliquezMoiAsText()
   {
     $this->assertXPathContentContains('//div[contains(@class, "jumbotron_section_content")]//dt[contains(@class,"liens internet")]/following-sibling::dd//a[@href="https://monsiteweb.fr"]',
-                                      'Cliquez moi pour aller sur',
-    $this->_response->getBody());
+                                      'Cliquez moi pour aller sur');
   }
 
 
@@ -73,4 +72,24 @@ class TemplatesRecordsWithUrlsWithTextReplacementTest
     $this->assertXPathContentContains('//div[contains(@class, "jumbotron_section_content")]//dt[contains(@class,"liens internet")]/following-sibling::dd//a[@href="https://autresite.fr"]',
                                       'Follow me');
   }
+
+
+  /** @test */
+  public function linkMonSiteWebDotFrShouldBePresentOnlyOnce()
+  {
+    $this->assertXPathCount('//div[contains(@class, "jumbotron_section_content")]/'
+                            . '/dt[contains(@class,"liens internet")]/following-sibling'
+                            . '::dd//a[@href="https://monsiteweb.fr"]', 1);
+
+  }
+
+
+  /** @test */
+  public function linkAutreSiteDotFrShouldBePresentOnlyOnce()
+  {
+    $this->assertXPathCount('//div[contains(@class, "jumbotron_section_content")]/'
+                            . '/dt[contains(@class,"liens internet")]/following-sibling'
+                            . '::dd//a[@href="https://autresite.fr"]', 1);
+
+  }
 }
-- 
GitLab