From 6fea097dae56217bd231856863a7d2a0f6e9df91 Mon Sep 17 00:00:00 2001
From: Henri-Damien LAURENT <hdlaurent@afi-sa.fr>
Date: Thu, 2 Dec 2021 12:37:21 +0100
Subject: [PATCH] hotline#145531 : OPAC Search Form : Search Box Should display
 empty values for Library, sites and doc types

---
 VERSIONS_HOTLINE/145531                       |   1 +
 library/Class/Bib.php                         |   2 +
 library/Class/CodifAnnexe.php                 |   2 +-
 library/Class/TypeDoc.php                     |  17 ++-
 .../Intonation/Library/Widget/Search/View.php |  19 ++-
 .../Templates/TemplatesWidgetTest.php         | 136 +++++++++++++++++-
 6 files changed, 163 insertions(+), 14 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/145531

diff --git a/VERSIONS_HOTLINE/145531 b/VERSIONS_HOTLINE/145531
new file mode 100644
index 00000000000..f29743a3f09
--- /dev/null
+++ b/VERSIONS_HOTLINE/145531
@@ -0,0 +1 @@
+ - ticket #145531 : Barre de recherche : problème affichage des bibliothèques
\ No newline at end of file
diff --git a/library/Class/Bib.php b/library/Class/Bib.php
index c170d7e7de1..ebba4601314 100644
--- a/library/Class/Bib.php
+++ b/library/Class/Bib.php
@@ -208,6 +208,8 @@ class BibLoader extends Storm_Model_Loader {
     foreach(Class_Bib::findAllWithData() as $library)
       $facets [Class_Bib::CODE_FACETTE . $library->getId()] = $library->getLibelle();
 
+    asort($facets, SORT_NATURAL | SORT_FLAG_CASE);
+    array_unshift($facets, $this->_('toutes'));
     return $facets;
   }
 
diff --git a/library/Class/CodifAnnexe.php b/library/Class/CodifAnnexe.php
index 69b8c74c589..dddd7c36dba 100644
--- a/library/Class/CodifAnnexe.php
+++ b/library/Class/CodifAnnexe.php
@@ -63,7 +63,7 @@ class CodifAnnexeLoader extends Storm_Model_Loader {
     foreach ($annexes as $annexe)
       $multi_options_facets[$annexe->getFacetCode()] = $annexe->getLabel();
 
-    asort($multi_options_facets);
+    asort($multi_options_facets, SORT_NATURAL | SORT_FLAG_CASE);
     array_unshift($multi_options_facets, $this->_('tous'));
     return $multi_options_facets;
   }
diff --git a/library/Class/TypeDoc.php b/library/Class/TypeDoc.php
index d181336e6ec..586ff9fc86e 100644
--- a/library/Class/TypeDoc.php
+++ b/library/Class/TypeDoc.php
@@ -50,8 +50,15 @@ class TypeDocLoader extends Class_CosmoVar_ModelLoader  {
   }
 
 
+  /** For Tests */
+  public function setUsedIdsCache($idscache){
+    return $this->_used_ids_cache = $idscache;
+  }
+
+
   public function reset() {
     $this->_all_instances = null;
+    $this->_used_ids_cache = null;
     return $this;
   }
 
@@ -146,6 +153,7 @@ class TypeDocLoader extends Class_CosmoVar_ModelLoader  {
   }
 
 
+
   public function getLabelFor($type_doc_id) {
     return ($type_doc = Class_TypeDoc::find($type_doc_id))
       ? $type_doc->getLabel()
@@ -195,12 +203,19 @@ class TypeDocLoader extends Class_CosmoVar_ModelLoader  {
 
   public function getMultiOptionsFacets() {
     $multi_options = $this->getMultiOptions();
-    $multi_options_facets = ['' => $this->_('tous')];
+
+    $multi_options_facets=[];
+
     foreach ($multi_options as $id => $label) {
       if (!$doctype = Class_TypeDoc::find($id))
         continue;
       $multi_options_facets[$doctype->getFacetCode()] = $doctype->getLabel();
     }
+    if (!$multi_options_facets)
+      return;
+
+    asort($multi_options_facets, SORT_NATURAL | SORT_FLAG_CASE);
+    array_unshift($multi_options_facets, $this->_('tous'));
 
     return $multi_options_facets;
   }
diff --git a/library/templates/Intonation/Library/Widget/Search/View.php b/library/templates/Intonation/Library/Widget/Search/View.php
index 3b2ef7e2ce5..35ee54b9182 100644
--- a/library/templates/Intonation/Library/Widget/Search/View.php
+++ b/library/templates/Intonation/Library/Widget/Search/View.php
@@ -267,8 +267,8 @@ abstract class IntonationSearchRenderAbstract {
     $form = new ZendAfi_Form;
     $elements = [];
 
-    if ($this->_settings->getSelectDoc()) {
-      $options = Class_TypeDoc::getMultiOptionsFacets();
+    if ($this->_settings->getSelectDoc()
+        && $options = Class_TypeDoc::getMultiOptionsFacets()) {
       $label = $this->_settings->getDocTypeSelectionLabel();
       $elements [] =
         $form->createElement('select',
@@ -280,11 +280,9 @@ abstract class IntonationSearchRenderAbstract {
                               'multiOptions' => $options]);
     }
 
-    if ($this->_settings->getSelectAnnexe()) {
-      $options = Class_CodifAnnexe::getMultiOptionsFacets();
-      asort($options, SORT_NATURAL | SORT_FLAG_CASE);
+    if ($this->_settings->getSelectAnnexe()
+        && $options = Class_CodifAnnexe::getMultiOptionsFacets()) {
       $label = $this->_settings->getAnnexeSelectionLabel();
-
       $elements [] =
         $form->createElement('select',
                              'custom_multifacets_annexe',
@@ -295,10 +293,10 @@ abstract class IntonationSearchRenderAbstract {
                               'multiOptions' => $options]);
     }
 
-    if ($this->_settings->getSelectBib()) {
-      $options = Class_Bib::getMultiOptionsFacets();
-      asort($options, SORT_NATURAL | SORT_FLAG_CASE);
+    if ($this->_settings->getSelectBib()
+        && $options = Class_Bib::getMultiOptionsFacets()) {
       $label = $this->_settings->getLibrarySelectionLabel();
+
       $elements [] =
         $form->createElement('select',
                              'custom_multifacets_bib',
@@ -451,13 +449,12 @@ class IntonationSearchRenderInline extends IntonationSearchRenderAbstract {
   public function render() {
     $form = $this->_getMainForm();
 
-    $optional_form_elements = [];
-
     foreach($this->_getOptionalElements() as $element) {
       $label = $element->getLabel();
       $element->setLabel('');
       $multi_options = $element->getAttrib('options');
       unset($multi_options['']);
+      unset($multi_options[0]);
       array_unshift($multi_options, $label);
       $element->setAttrib('options', $multi_options);
       $form->addElement($element);
diff --git a/tests/scenarios/Templates/TemplatesWidgetTest.php b/tests/scenarios/Templates/TemplatesWidgetTest.php
index 86647fdf29e..c4efc2610ee 100644
--- a/tests/scenarios/Templates/TemplatesWidgetTest.php
+++ b/tests/scenarios/Templates/TemplatesWidgetTest.php
@@ -549,9 +549,17 @@ class TemplatesWidgetSearchInlineStyleTest extends TemplatesIntonationTestCase {
                ->setProfileId(72)
                ->load());
     $widget
-      ->setNewDatas(['IntonationFormStyle' => 'inline'])
+      ->setNewDatas(['IntonationFormStyle' => 'inline',
+                     'select_bib' => 1,
+                     'library_selection_label' => 'Bibliothèques du réseau',
+                     ])
       ->updateProfile();
 
+    $this->fixture(Class_Bib::class,
+                   [ 'id' => 2,
+                    'libelle' => 'Lothlorien',
+                    'visibilite' => Class_Bib::V_DATA]);
+
     $this->dispatch('/opac/index/index/id_profil/72', true);
   }
 
@@ -568,6 +576,24 @@ class TemplatesWidgetSearchInlineStyleTest extends TemplatesIntonationTestCase {
   }
 
 
+  /** @test */
+  public function searchLabelForBibShoulNotBePresent() {
+    $this->assertNotXPath('//div[contains(@class,"boite rech_simple")]//form//label[contains(@for,"custom_multifacets_bib")]');
+  }
+
+
+  /** @test */
+  public function searchOptionBibliothequeShouldDisplayFirstEntryBibliotheque() {
+    $this->assertXPath('//div[contains(@class,"boite rech_simple")]//form//select[@name="custom_multifacets_bib"]//option[1][@value="0"][@label="Bibliothèques du réseau"]');
+  }
+
+
+  /** @test */
+  public function searchOptionBibliothequeShouldDisplaySecondEntryLothlorien() {
+    $this->assertXPath('//div[contains(@class,"boite rech_simple")]//form//select[@name="custom_multifacets_bib"]//option[2][@value="B2"][@label="Lothlorien"]');
+  }
+
+
   /** @test */
   public function advancedSearchButtonShouldHaveClassDropdownToggle() {
     $this->assertXPath('//div[contains(@class,"boite rech_simple")]//div[@class="dropdown"]/button[contains(@class, "dropdown-toggle")]');
@@ -610,6 +636,114 @@ class TemplatesWidgetSearchToggleStyleTest extends TemplatesIntonationTestCase {
 
 
 
+class TemplatesWidgetSearchDefaultStyleTest extends TemplatesIntonationTestCase {
+
+  public function setUp() {
+    parent::setUp();
+    $widget = ((new Class_Systeme_Widget_Widget)
+               ->setId(8)
+               ->setProfileId(72)
+               ->load());
+
+    $widget
+      ->setNewDatas(['IntonationFormStyle' => '',
+                     'select_bib' => 1,
+                     'library_selection_label' => 'Bibliothèques du réseau',
+                     'doc_type_selection_label' => 'Typologie',
+                     'select_doc' => '5;2',
+                     'select_annexe' => 1,
+                     'domains_ids' => '1-2'])
+      ->updateProfile();
+
+    $this->fixture(Class_Bib::class,
+                   [ 'id' => 2,
+                    'libelle' => 'Lothlorien',
+                    'visibilite' => Class_Bib::V_DATA]);
+
+    $this->fixture(Class_CodifAnnexe::class,
+                   [ 'id' => 3,
+                    'libelle'=> 'Principal',
+                   'invisible' => 0]);
+
+    $this->fixture(Class_Notice::class,
+                   ['id' => 42,
+                    'type_doc' => Class_TypeDoc::LIVRE]);
+
+    $this->fixture(Class_TypeDoc::class,
+                   [ 'id' => Class_TypeDoc::LIVRE,
+                    'label'=> 'Livre']);
+
+    Class_TypeDoc::setUsedIdsCache([Class_TypeDoc::LIVRE]);
+
+    Class_Profil::getCurrentProfil()->setSelTypeDoc(Class_TypeDoc::LIVRE)->save();
+
+
+    $this->dispatch('/opac/index/index/id_profil/72', true);
+  }
+
+  public function tearDown() {
+    Class_TypeDoc::reset();
+    parent::tearDown();
+  }
+
+
+  /** @test */
+  public function searchWidgetShouldBeDefaultForm() {
+    $this->assertXPath('//div[contains(@class,"boite rech_simple")]//form');
+  }
+
+
+  /** @test */
+  public function searchLabelForBibShoulDisplayBibliothequesDuReseau() {
+    $this->assertXPathContentContains('//div[contains(@class,"boite rech_simple")]//form//label[contains(@for,"custom_multifacets_bib")]', 'Bibliothèques du réseau');
+  }
+
+
+  /** @test */
+  public function searchOptionBibliothequeShouldDisplayToutes() {
+    $this->assertXPath('//div[contains(@class,"boite rech_simple")]//form//select[@name="custom_multifacets_bib"]//option[1][@value="0"][@label="toutes"]');
+  }
+
+
+  /** @test */
+  public function searchOptionBibliothequeShouldDisplayOptionLothlorien() {
+    $this->assertXPath('//div[contains(@class,"boite rech_simple")]//form//select[@name="custom_multifacets_bib"]//option[2][@value="B2"][@label="Lothlorien"]');
+  }
+
+
+  /** @test */
+  public function searchLabelForAnnexeShouldDisplaySite() {
+    $this->assertXPathContentContains('//div[contains(@class,"boite rech_simple")]//form//label[contains(@for,"custom_multifacets_annexe")]', 'Site');
+  }
+
+
+  /** @test */
+  public function searchOptionForAnnexeShouldDisplayTous() {
+    $this->assertXPath('//div[contains(@class,"boite rech_simple")]//form//select[@name="custom_multifacets_annexe"]//option[1][@value="0"][@label="tous"]');
+  }
+
+
+  /** @test */
+  public function searchLabelForTypedocShoulDisplayTypeDoc() {
+    $this->assertXPathContentContains('//div[contains(@class,"boite rech_simple")]//form//label[contains(@for,"custom_multifacets_doctype")]', 'Typologie');
+  }
+
+
+  /** @test */
+  public function searchOptionTypeDocShoulDisplayTous() {
+    $this->assertXPath('//div[contains(@class,"boite rech_simple")]//form//select[contains(@name,"custom_multifacets_doctype")]//option[1][@value="0"][@label="tous"]');
+  }
+
+
+  /** @test */
+  public function searchOptionTypeDocShoulDisplayDocTypeLivre() {
+    $this->assertXPath('//div[contains(@class,"boite rech_simple")]//form//select[contains(@name,"custom_multifacets_doctype")]//option[2][@value="T1"][@label="Livre"]');
+  }
+}
+
+
+
+
 class TemplatesImageWidgetTest extends TemplatesIntonationTestCase {
 
   /** @test */
-- 
GitLab