From 765b8b0cc645fc11ed01bcc8183cd35686d0c37b Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Thu, 20 Jan 2022 14:50:22 +0100
Subject: [PATCH] hotline #149044 MT Fix rendering of icons starting with
 keyword uri

---
 VERSIONS_HOTLINE/149044                       |  1 +
 library/Class/Template.php                    |  9 ++--
 library/Class/Template/Update.php             |  7 +++
 library/ZendAfi/View/Helper/RenderIcon.php    |  2 +-
 .../ZendAfi/View/Helper/RenderIconTest.php    | 47 +++++++++++++++++++
 tests/scenarios/Templates/ChiliTest.php       |  4 +-
 .../Templates/TemplatesLibraryTest.php        | 26 +++++++++-
 7 files changed, 87 insertions(+), 9 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/149044
 create mode 100644 tests/library/ZendAfi/View/Helper/RenderIconTest.php

diff --git a/VERSIONS_HOTLINE/149044 b/VERSIONS_HOTLINE/149044
new file mode 100644
index 00000000000..fe5c0c1a945
--- /dev/null
+++ b/VERSIONS_HOTLINE/149044
@@ -0,0 +1 @@
+ - correctif #149044 : Magasin de thèmes : correction de l'affichage d'icônes personnalisées pour les rendus cartes
\ No newline at end of file
diff --git a/library/Class/Template.php b/library/Class/Template.php
index 5bad85cd644..5ae00f962db 100644
--- a/library/Class/Template.php
+++ b/library/Class/Template.php
@@ -566,10 +566,11 @@ class Class_Template {
   protected function _icoFromClassOrUrl($view, $key, $cache_key, $params, $category = '') {
     $params = $this->_updateParamsWithCategory($params, $category);
 
-    return $this->_icons_cache[$cache_key] =
-      (($icon = $view->renderIcon($key, $params))
-       ? $icon
-       : $key);
+    if ($icon = $view->renderIcon($key, $params))
+      return $this->_icons_cache[$cache_key] = $icon;
+
+    $parts = explode(' ', $key);
+    return $this->_icons_cache[$cache_key] = end($parts);
   }
 
 
diff --git a/library/Class/Template/Update.php b/library/Class/Template/Update.php
index d20d8010a9a..67fe2e33132 100644
--- a/library/Class/Template/Update.php
+++ b/library/Class/Template/Update.php
@@ -36,4 +36,11 @@ class Class_Template_Update {
                });
     $this->echoError("Mise à jour des thèmes du magasin terminée.\n");
   }
+
+
+  public function runWithEcho(bool $echo) : self {
+    $this->setEcho($echo);
+    $this->run();
+    return $this;
+  }
 }
diff --git a/library/ZendAfi/View/Helper/RenderIcon.php b/library/ZendAfi/View/Helper/RenderIcon.php
index fcec3133f99..79471f9969b 100644
--- a/library/ZendAfi/View/Helper/RenderIcon.php
+++ b/library/ZendAfi/View/Helper/RenderIcon.php
@@ -42,7 +42,7 @@ class ZendAfi_View_Helper_RenderIcon extends ZendAfi_View_Helper_BaseHelper {
 
     if ($strategy == static::URL_STRATEGY)
       return $this->view->tagImg(implode($rules),
-                                 $params);
+                                 $attribs);
 
     return '';
   }
diff --git a/tests/library/ZendAfi/View/Helper/RenderIconTest.php b/tests/library/ZendAfi/View/Helper/RenderIconTest.php
new file mode 100644
index 00000000000..738b92ff867
--- /dev/null
+++ b/tests/library/ZendAfi/View/Helper/RenderIconTest.php
@@ -0,0 +1,47 @@
+<?php
+/**
+ * Copyright (c) 2012-2022, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class ZendAfi_View_Helper_RenderIconTest extends ViewHelperTestCase {
+  protected $_storm_default_to_volatile = true;
+
+  public function fixtures() {
+    return [
+            ['/icon.png', [], '<img src="/icon.png" alt="">'],
+            ['/icon.png', ['height' => '50px'], '<img src="/icon.png" height="50px" alt="">'],
+            ['url /icon.png', ['height' => '50px'], '<img src="/icon.png" height="50px" alt="">'],
+            ['uri /icon.png', [], ''],
+            ['class fas fa-new-ico', ['height' => '20px'], '<i height="20px" class="fas fa-new-ico"></i>']
+    ];
+  }
+
+
+  /**
+   * @dataProvider fixtures
+   * @test
+   */
+  public function withRuleAndAttribsShouldRenderHTML($rules, $attribs, $rendered_html) {
+    $helper = new ZendAfi_View_Helper_RenderIcon();
+    $helper->setView($this->view);
+    $this->assertEquals($rendered_html,
+                        $helper->renderIcon($rules, $attribs));
+  }
+}
diff --git a/tests/scenarios/Templates/ChiliTest.php b/tests/scenarios/Templates/ChiliTest.php
index cdd853c4787..87a963c09e9 100644
--- a/tests/scenarios/Templates/ChiliTest.php
+++ b/tests/scenarios/Templates/ChiliTest.php
@@ -337,9 +337,7 @@ class ChiliTemplateUpdateSettingsTest extends ChiliTemplateTestCase {
     $chili_settings
       ->setSettings(serialize($chili_settings_instance->toArray()))->save();
 
-    $updader = (new Class_Template_Update);
-    $updader->setEcho(true);
-    $updader->run();
+    (new Class_Template_Update)->runWithEcho(true);
     $chili_settings->clearCache();
   }
 
diff --git a/tests/scenarios/Templates/TemplatesLibraryTest.php b/tests/scenarios/Templates/TemplatesLibraryTest.php
index 9e42c961fc3..2bcd6da0120 100644
--- a/tests/scenarios/Templates/TemplatesLibraryTest.php
+++ b/tests/scenarios/Templates/TemplatesLibraryTest.php
@@ -604,6 +604,15 @@ class TemplatesLibraryWidgetWithOSMAndLinkToProfileTest extends TemplatesIntonat
     Class_AdminVar::set('CACHE_ACTIF', 1);
     ZendAfi_Auth::getInstance()->clearIdentity();
 
+
+    $settings = Class_Template_Settings::findFirstBy(['template' => 'INTONATION']);
+    $settings_insance = $settings->getSettingsInstance();
+    $settings_insance
+      ->setIntonationIconsMapUtils(['osm_closed_marker' => 'uri /map/were-closed.png']);
+    $settings->setSettings(serialize($settings_insance->toArray()))->save();
+    (new Class_Template_Update)->runWithEcho(true);
+    $settings->clearCache();
+
     $this->dispatch('/opac/index/index/id_profil/72');
   }
 
@@ -611,7 +620,7 @@ class TemplatesLibraryWidgetWithOSMAndLinkToProfileTest extends TemplatesIntonat
   /** @test */
   public function customFieldServiceProjectionShouldBeAnAnchor() {
     $this->assertXPathContentContains('//div[contains(@class,"boite library")]//div[@class="dropdown-menu"]//li//a[@href = "/bib/widget/id_profil/72/id_module/21/id_division/3/custom_field_1/Projection"]',
-                                      'Projection', $this->_response->getBody());
+                                      'Projection');
   }
 
 
@@ -651,6 +660,21 @@ class TemplatesLibraryWidgetWithOSMAndLinkToProfileTest extends TemplatesIntonat
   }
 
 
+  /** @test */
+  public function dataOsmOpenUrlIconShouldBeMapSlashOpenMarkerIconDotPng() {
+    $this->assertXPath('//div[contains(@data-osm-open-url-icon, "/map/open-marker-icon.png")]');
+  }
+
+
+  /**
+   * @see http://forge.afi-sa.fr/issues/149044
+   * @test
+   */
+  public function dataOsmClosedUrlIconShouldBeMapSlashWereClosedDotPng() {
+    $this->assertXPath('//div[@data-osm-close-url-icon="/map/were-closed.png"]');
+  }
+
+
   /**
    * @test
    * @see https://forge.afi-sa.net/issues/114141
-- 
GitLab