From 9f5d67b31f8a0ba34fecd096d0717a4334398f4a Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Wed, 15 Jun 2022 17:49:05 +0200
Subject: [PATCH] dev #156612 fix template css injection

---
 VERSIONS_WIP/156612                           |  1 +
 library/Class/Template.php                    | 15 ++++++----
 library/ZendAfi/View/Helper/Template/Opac.php |  7 +++--
 library/templates/Chili/Template.php          |  7 +++++
 library/templates/Herisson/Template.php       |  7 +++++
 .../templates/Intonation/Library/Styles.php   | 22 ++++++++++----
 library/templates/Intonation/Template.php     | 13 --------
 library/templates/Muscle/Template.php         |  7 +++++
 library/templates/MyBibApp/Template.php       |  7 +++++
 library/templates/Polygone/Template.php       |  7 +++++
 library/templates/TerreDuMilieu/Template.php  |  7 +++++
 tests/scenarios/Templates/ChiliTest.php       | 30 +++++++++++--------
 .../Templates/HerissonTemplateTest.php        |  6 ++++
 .../Templates/MuscleTemplateTest.php          | 26 +++++++++++++++-
 .../Templates/MyBibAppTemplateTest.php        |  2 +-
 15 files changed, 125 insertions(+), 39 deletions(-)
 create mode 100644 VERSIONS_WIP/156612

diff --git a/VERSIONS_WIP/156612 b/VERSIONS_WIP/156612
new file mode 100644
index 00000000000..fe771efbf55
--- /dev/null
+++ b/VERSIONS_WIP/156612
@@ -0,0 +1 @@
+ - fonctionnalité #156612 : Magasin de thèmes : Cocher ou décocher le fichier CSS d'un thème est maintenant correctement pris en compte.
\ No newline at end of file
diff --git a/library/Class/Template.php b/library/Class/Template.php
index f23046172bc..4c0deba2e59 100644
--- a/library/Class/Template.php
+++ b/library/Class/Template.php
@@ -659,11 +659,6 @@ class Class_Template {
   }
 
 
-  public function getTemplateCssUrls() {
-    return [];
-  }
-
-
   public function addTemplateJsTo($script_loader) {
     return $this;
   }
@@ -734,4 +729,14 @@ class Class_Template {
     $files_names = $this->getFileSystem()->fileNamesAt(TEMPLATES_CSS_PATCHS);
     return array_combine($files_names, $files_names);
   }
+
+
+  public function getFolder() : string {
+    return dirname((new ReflectionClass($this))->getFileName());
+  }
+
+
+  public function getTemplateCssFileName() : string {
+    return '';
+  }
 }
\ No newline at end of file
diff --git a/library/ZendAfi/View/Helper/Template/Opac.php b/library/ZendAfi/View/Helper/Template/Opac.php
index c98e22340f0..25b79bdb45c 100644
--- a/library/ZendAfi/View/Helper/Template/Opac.php
+++ b/library/ZendAfi/View/Helper/Template/Opac.php
@@ -173,8 +173,11 @@ class ZendAfi_View_Helper_Template_Opac extends ZendAfi_View_Helper_BaseHelper {
     if ($css_patchs = $this->_styles->cssPatchsUrls())
       $head_scripts->addStyleSheets($css_patchs);
 
-    if ($template_css = $this->_styles->templateCssUrls())
-      $head_scripts->addStyleSheets($template_css);
+    if ($intonation_css = $this->_styles->intonationCssUrl())
+      $head_scripts->addStyleSheet($intonation_css);
+
+    if ($template_css = $this->_styles->templateCssUrl())
+      $head_scripts->addStyleSheet($template_css);
 
     $this->_template->addTemplateJsTo($script_loader);
 
diff --git a/library/templates/Chili/Template.php b/library/templates/Chili/Template.php
index c17d82b189d..a4bd18f8816 100644
--- a/library/templates/Chili/Template.php
+++ b/library/templates/Chili/Template.php
@@ -116,4 +116,11 @@ class Chili_Template extends Intonation_Template {
   public function renderOpac($view) {
     return (new Chili_View_Opac($this, $view))->render();
   }
+
+
+  public function getTemplateCssFileName() : string {
+    return $this->getChiliCss()
+      ? 'chili'
+      : '';
+  }
 }
diff --git a/library/templates/Herisson/Template.php b/library/templates/Herisson/Template.php
index f1dafe4143b..0f753349929 100644
--- a/library/templates/Herisson/Template.php
+++ b/library/templates/Herisson/Template.php
@@ -77,4 +77,11 @@ class Herisson_Template extends Intonation_Template {
       ? Chili_View_RenderMultipleCarousel::class
       : '';
   }
+
+
+  public function getTemplateCssFileName() : string {
+    return $this->getHerissonCss()
+      ? 'herisson'
+      : '';
+  }
 }
diff --git a/library/templates/Intonation/Library/Styles.php b/library/templates/Intonation/Library/Styles.php
index 93113d933dd..a7dc0bb71e9 100644
--- a/library/templates/Intonation/Library/Styles.php
+++ b/library/templates/Intonation/Library/Styles.php
@@ -32,9 +32,10 @@ class Intonation_Library_Styles {
     $css = new Storm_Collection([$this->fontAwesomeCssUrl(),
                                  $this->bootstrapCssUrl(),
                                  $this->bootstrapRgaaCssUrl(),
-                                 $this->coreCssUrl()]);
+                                 $this->coreCssUrl(),
+                                 $this->intonationCssUrl()]);
     $css->addAll($this->cssPatchsUrls());
-    $css->addAll($this->templateCssUrls());
+    $css->add($this->templateCssUrl());
     $css->addAll($this->customCssUrls());
     $css->add($this->profileHeaderCssUrl());
 
@@ -72,7 +73,16 @@ class Intonation_Library_Styles {
 
 
   protected function _cssUrlFor($path) {
-    return Class_Url::absolute('/library/templates/Intonation/Assets/' . $path);
+    return Class_Url::relative('/library/templates/Intonation/Assets/' . $path);
+  }
+
+
+  protected function _cssUrlForCssFileName(string $file_name) : string {
+    return Class_Url::relative('/library/templates/'
+                               . $this->_template->getFolder()
+                               . '/Assets/css/'
+                               . $file_name
+                               . '.css');
   }
 
 
@@ -112,7 +122,9 @@ class Intonation_Library_Styles {
   }
 
 
-  public function templateCssUrls() {
-    return $this->_template->getTemplateCssUrls();
+  public function templateCssUrl() : string {
+    return ($css_file_name_enable = $this->_template->getTemplateCssFileName())
+      ? $this->_cssUrlForCssFileName($css_file_name_enable)
+      : '';
   }
 }
diff --git a/library/templates/Intonation/Template.php b/library/templates/Intonation/Template.php
index 5c715782dfb..971300af743 100644
--- a/library/templates/Intonation/Template.php
+++ b/library/templates/Intonation/Template.php
@@ -411,19 +411,6 @@ class Intonation_Template extends Class_Template {
   }
 
 
-  public function getTemplateCssUrls() {
-    $css = $this->getIntonationCss()
-      ? [ $this->_assetsTemplateCssPath(Intonation_Template::class) ]
-      : [];
-
-    if (Intonation_Template::ID === $this->getId())
-      return $css;
-
-    $css[] = $this->_assetsTemplateCssPath(static::class);
-    return $css;
-  }
-
-
   protected function _assetsTemplateCssPath($classname) {
     $directory = explode('_', $classname)[0];
     $file = strtolower($directory);
diff --git a/library/templates/Muscle/Template.php b/library/templates/Muscle/Template.php
index ea040b3228a..8f576b8e543 100644
--- a/library/templates/Muscle/Template.php
+++ b/library/templates/Muscle/Template.php
@@ -54,4 +54,11 @@ class Muscle_Template extends Intonation_Template {
     $helper = new Muscle_Library_FormCustomizer($this);
     return $helper->getTemplateForm($form);
   }
+
+
+  public function getTemplateCssFileName() : string {
+    return $this->getMuscleCss()
+      ? 'muscle'
+      : '';
+  }
 }
diff --git a/library/templates/MyBibApp/Template.php b/library/templates/MyBibApp/Template.php
index 63b4b47ceab..5355e74f5b3 100644
--- a/library/templates/MyBibApp/Template.php
+++ b/library/templates/MyBibApp/Template.php
@@ -52,4 +52,11 @@ class MyBibApp_Template extends Intonation_Template {
     $helper = new MyBibApp_Library_FormCustomizer($this);
     return $helper->getTemplateForm($form);
   }
+
+
+  public function getTemplateCssFileName() : string {
+    return $this->getMybibappCss()
+      ? 'mybibapp'
+      : '';
+  }
 }
diff --git a/library/templates/Polygone/Template.php b/library/templates/Polygone/Template.php
index 13980e82a37..10d10b0b800 100644
--- a/library/templates/Polygone/Template.php
+++ b/library/templates/Polygone/Template.php
@@ -63,4 +63,11 @@ class Polygone_Template extends Intonation_Template {
 
     return $this;
   }
+
+
+  public function getTemplateCssFileName() : string {
+    return $this->getPolygoneCss()
+      ? 'polygone'
+      : '';
+  }
 }
diff --git a/library/templates/TerreDuMilieu/Template.php b/library/templates/TerreDuMilieu/Template.php
index 910975067bb..dd33a7ed3a3 100644
--- a/library/templates/TerreDuMilieu/Template.php
+++ b/library/templates/TerreDuMilieu/Template.php
@@ -54,4 +54,11 @@ class TerreDuMilieu_Template extends Intonation_Template {
     $helper = new TerreDuMilieu_Library_FormCustomizer($this);
     return $helper->getTemplateForm($form);
   }
+
+
+  public function getTemplateCssFileName() : string {
+    return $this->getTerredumilieuCss()
+      ? 'terredumilieu'
+      : '';
+  }
 }
diff --git a/tests/scenarios/Templates/ChiliTest.php b/tests/scenarios/Templates/ChiliTest.php
index 929d0dff09c..0f1457586d1 100644
--- a/tests/scenarios/Templates/ChiliTest.php
+++ b/tests/scenarios/Templates/ChiliTest.php
@@ -264,31 +264,35 @@ class ChiliTemplateOpacAdvancedSearchTest extends ChiliTemplateTestCase {
 
 
 
+
 class ChiliTemplateSearchResultMoreOptionsTest extends ChiliTemplateTestCase {
+
   public function setUp() {
     parent::setUp();
 
-    $this->fixture('Class_CodifAuteur',
+    $this->fixture(Class_CodifAuteur::class,
                    ['id' => 43,
                     'libelle' => 'Pomme d\'API']);
 
-    $records = [$this->fixture('Class_Notice',
+    $records = [$this->fixture(Class_Notice::class,
                                ['id' => 89,
                                 'titre_principal' => 'Tintin e i picaros']),
-                $this->fixture('Class_Notice',
+
+                $this->fixture(Class_Notice::class,
                                ['id' => 99,
                                 'titre_principal' => 'Tintin au Tibet'])];
 
-    $sql = $this->mock()
-                ->whenCalled('fetchAll')
-                ->with("select id_notice, facettes from notices Where (MATCH(titres, auteurs, editeur, collection, matieres, dewey, other_terms) AGAINST('+(JARDIN JARDINS JARDIN)' IN BOOLEAN MODE)) and type=1 order by MATCH(auteurs) AGAINST('P_JARDIN') desc, MATCH(titres) AGAINST('P_JARDIN') desc, MATCH(titres) AGAINST('JARDIN') desc, MATCH(auteurs) AGAINST('JARDIN') desc, date_creation desc",
-                       true,
-                       false)
-                ->answers([[89, ''], [99, '']])
+    $sql = $this
+      ->mock()
+      ->whenCalled('fetchAll')
+      ->with("select id_notice, facettes from notices Where (MATCH(titres, auteurs, editeur, collection, matieres, dewey, other_terms) AGAINST('+(JARDIN JARDINS JARDIN)' IN BOOLEAN MODE)) and type=1 order by MATCH(auteurs) AGAINST('P_JARDIN') desc, MATCH(titres) AGAINST('P_JARDIN') desc, MATCH(titres) AGAINST('JARDIN') desc, MATCH(auteurs) AGAINST('JARDIN') desc, date_creation desc",
+             true,
+             false)
+      ->answers([[89, ''], [99, '']])
 
-                ->whenCalled('fetchAll')
-                ->with('select distinct(type_doc) from notices')
-                ->answers([]);
+      ->whenCalled('fetchAll')
+      ->with('select distinct(type_doc) from notices')
+      ->answers([]);
 
     Zend_Registry::set('sql', $sql);
   }
@@ -315,6 +319,7 @@ class ChiliTemplateSearchResultMoreOptionsTest extends ChiliTemplateTestCase {
     $user
       ->setPassword('uie,uie,uie,')
       ->setMail('email@doesnotexist.org');
+
     $user->assertSave();
     $this->dispatch('/recherche/send-mail/expressionRecherche/jardin');
     $this->assertXPathContentContains('//div', 'Tintin au Tibet');
@@ -323,6 +328,7 @@ class ChiliTemplateSearchResultMoreOptionsTest extends ChiliTemplateTestCase {
 
 
 
+
 class ChiliTemplateIndexFormulaireContactTest extends ChiliTemplateTestCase {
 
   /** @test */
diff --git a/tests/scenarios/Templates/HerissonTemplateTest.php b/tests/scenarios/Templates/HerissonTemplateTest.php
index 984ffe7f7bd..8247a16b173 100644
--- a/tests/scenarios/Templates/HerissonTemplateTest.php
+++ b/tests/scenarios/Templates/HerissonTemplateTest.php
@@ -180,6 +180,12 @@ class HerissonTemplateIndexTest extends HerissonTemplateTestCase {
                        . Class_Url::relative('/library/templates/Herisson/Assets/images/herisson_logo.png')
                        . '"]');
   }
+
+
+  /** @test */
+  public function herissonCssFileShouldBeLoaded() {
+    $this->assertXPath('//link[contains(@href, "/library/templates/Herisson/Assets/css/herisson.css")]');
+  }
 }
 
 
diff --git a/tests/scenarios/Templates/MuscleTemplateTest.php b/tests/scenarios/Templates/MuscleTemplateTest.php
index 56ea385d347..cb3a6e08dc7 100644
--- a/tests/scenarios/Templates/MuscleTemplateTest.php
+++ b/tests/scenarios/Templates/MuscleTemplateTest.php
@@ -21,7 +21,6 @@
 
 
 abstract class MuscleTemplateTestCase extends Admin_AbstractControllerTestCase {
-  protected $_storm_default_to_volatile = true;
 
   public function setUp() {
     parent::setUp();
@@ -597,3 +596,28 @@ class MuscleTemplateNotLoggedTest extends MuscleTemplateTestCase {
     $this->assertXPath('//form/input[@id="login"][@class= "zendafi_form_login_login btn btn-sm btn-primary order-3 my-3"]');
   }
 }
+
+
+
+
+class MuscleTemplateRemoveCssTest extends MuscleTemplateTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    $this->fixture(Class_Template_Settings::class,
+                   ['id' => 1,
+                    'template' => 'MUSCLE']);
+
+    Class_Template_Settings::find(1)
+      ->updateAttributes(['MuscleMuscleCss' => ""])
+      ->save();
+
+    $this->dispatch('/index');
+  }
+
+
+  /** @test */
+  public function muscleCssFileShouldNotBePresent() {
+    $this->assertNotXPath('//link[contains(@href, "muscle.css")]');
+  }
+}
diff --git a/tests/scenarios/Templates/MyBibAppTemplateTest.php b/tests/scenarios/Templates/MyBibAppTemplateTest.php
index f75ee8226e8..eb5b870e48c 100644
--- a/tests/scenarios/Templates/MyBibAppTemplateTest.php
+++ b/tests/scenarios/Templates/MyBibAppTemplateTest.php
@@ -168,7 +168,7 @@ class MyBibAppTemplateOpacIndexWithUserAgentTest extends MyBibAppTemplateTestCas
 
   /** @test */
   public function pageShouldLoadMyBibAppCss() {
-    $this->assertXPath('//head/link[contains(@href, "/templates/MyBibApp/Assets/css/mybibapp.css")]');
+    $this->assertXPath('//head/link[contains(@href, "/library/templates/MyBibApp/Assets/css/mybibapp.css")]');
   }
 
 
-- 
GitLab