From a615a3e5e18c6cbf22bf9213b72456baf84f3cef Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Wed, 2 May 2018 11:35:23 +0200
Subject: [PATCH] dev #73233 fix tests comments

---
 FEATURES/73233                                | 10 +++++
 .../opac/controllers/RechercheController.php  |  8 ++--
 library/Class/SearchForm.php                  | 14 ++++++-
 library/Class/SearchForm/Validator.php        |  1 +
 library/ZendAfi/Form/AdvancedSearch.php       |  9 +++--
 .../ZendAfi/View/Helper/AdvancedSearch.php    | 39 ++++++++++---------
 .../AdvancedSearch/AdvancedSearchTest.php     | 10 ++++-
 7 files changed, 63 insertions(+), 28 deletions(-)
 create mode 100644 FEATURES/73233

diff --git a/FEATURES/73233 b/FEATURES/73233
new file mode 100644
index 00000000000..15eaacee961
--- /dev/null
+++ b/FEATURES/73233
@@ -0,0 +1,10 @@
+        '73233' =>
+            ['Label' => $this->_('Personnalisatio de la recherche avanacée'),
+             'Desc' => $this->_('Possibilité de personnaliser des formulaires de recherche avancées.'),
+             'Image' => '',
+             'Video' => '',
+             'Category' => '',
+             'Right' => function($feature_description, $user) {return true;},
+             'Wiki' => '',
+             'Test' => '',
+             'Date' => '2018-05-02'],
\ No newline at end of file
diff --git a/application/modules/opac/controllers/RechercheController.php b/application/modules/opac/controllers/RechercheController.php
index 92378df2ad8..d4e5ab71851 100644
--- a/application/modules/opac/controllers/RechercheController.php
+++ b/application/modules/opac/controllers/RechercheController.php
@@ -264,9 +264,11 @@ class RechercheController extends ZendAfi_Controller_Action {
   public function avanceeAction() {
     $this->view->annexes = Class_CodifAnnexe::getMultiOptions();
     $this->view->criteres_recherche = $this->newCriteresRecherches($this->_request->getParams());
-    $this->view->search_forms = isset($this->preferences['forms'])
-      ? Class_SearchForm::findAllBy(['id' => explode(';', $this->preferences['forms'])])
-      : [];
+
+    $this->view->search_forms =
+      Class_SearchForm::findAllByPrefs(isset($this->preferences['forms'])
+                                       ? $this->preferences['forms']
+                                       : '');
   }
 
 
diff --git a/library/Class/SearchForm.php b/library/Class/SearchForm.php
index 9a1534b823b..7e7e376e7eb 100644
--- a/library/Class/SearchForm.php
+++ b/library/Class/SearchForm.php
@@ -30,6 +30,18 @@ class Class_SearchFormLoader extends Storm_Model_Loader {
     }
     return $multi_options;
   }
+
+
+  public function findAllByPrefs($ids) {
+    if(!$ids)
+      return [];
+
+    if(!$ids = array_filter(explode(';', $ids)))
+      return [];
+
+    return Class_SearchForm::findAllBy(['id' => $ids,
+                                        'order' => sprintf('FIELD(id, %s)', implode(',', $ids))]);
+  }
 }
 
 
@@ -117,7 +129,7 @@ class Class_SearchFormWrapper {
       return $this->addError($this->_('Le fichier lié à ce formulaire n\'est pas valide : %s', $validator->getError()));
 
     $includer = Class_SearchForm::getIncluder();
-    $form = new ZendAfi_Form();
+    $form = new ZendAfi_Form_AdvancedSearch();
     $includer($file->getRealpath(), $form);
     return $this->setForm($form);
   }
diff --git a/library/Class/SearchForm/Validator.php b/library/Class/SearchForm/Validator.php
index 412905d62d7..23d76392cb3 100644
--- a/library/Class/SearchForm/Validator.php
+++ b/library/Class/SearchForm/Validator.php
@@ -106,6 +106,7 @@ class Class_SearchForm_ValidatorMethodCalls extends NodeVisitorAbstract {
     $_trusted_methods =
     [
      // form
+     'loadDefaults',
      'addElement',
      'addDisplayGroup',
      'addUniqDisplayGroup',
diff --git a/library/ZendAfi/Form/AdvancedSearch.php b/library/ZendAfi/Form/AdvancedSearch.php
index 48bdae9983f..6a7adc475b1 100644
--- a/library/ZendAfi/Form/AdvancedSearch.php
+++ b/library/ZendAfi/Form/AdvancedSearch.php
@@ -23,9 +23,7 @@
 class ZendAfi_Form_AdvancedSearch extends ZendAfi_Form {
   protected $_axes = [];
 
-  public function init() {
-    parent::init();
-
+  public function loadDefaults() {
     $this->_axes = ['titres' => $this->_('Titres'),
                     'auteurs' => $this->_('Auteurs'),
                     'matieres' => $this->_('Sujets'),
@@ -87,6 +85,8 @@ class ZendAfi_Form_AdvancedSearch extends ZendAfi_Form {
     $this->addDisplayGroup(['type_recherche', 'tri', 'annee', 'nouveaute', 'type_doc', 'annexe', 'section', 'genre'],
                            'options',
                            ['legend' => $this->_('Options de la recherche')]);
+
+    return $this;
   }
 
 
@@ -96,6 +96,7 @@ class ZendAfi_Form_AdvancedSearch extends ZendAfi_Form {
     foreach(array_keys($this->_axes) as $type)
       $this->$type->loadDefault($datas);
 
-    $this->annee->loadDefault($datas);
+    if($this->annee)
+      $this->annee->loadDefault($datas);
   }
 }
diff --git a/library/ZendAfi/View/Helper/AdvancedSearch.php b/library/ZendAfi/View/Helper/AdvancedSearch.php
index 651c4024720..243714e8380 100644
--- a/library/ZendAfi/View/Helper/AdvancedSearch.php
+++ b/library/ZendAfi/View/Helper/AdvancedSearch.php
@@ -28,11 +28,7 @@ class ZendAfi_View_Helper_AdvancedSearch extends ZendAfi_View_Helper_BaseHelper
 
     $html = [];
     foreach($search_forms as $search_form)
-      $html [] =
-      $this->_advancedFormLabel($search_form)
-      . $this->_advancedForm($search_form);
-
-    $html = array_filter($html);
+      $html = $this->_advancedForm($search_form, $html);
 
     return empty($html)
       ? $this->_renderDefaultAdvancedSearch()
@@ -67,7 +63,7 @@ class ZendAfi_View_Helper_AdvancedSearch extends ZendAfi_View_Helper_BaseHelper
 
 
   protected function _defaultAdvancedSearch() {
-    return $this->_populateForm(new ZendAfi_Form_AdvancedSearch);
+    return $this->_populateForm((new ZendAfi_Form_AdvancedSearch)->loadDefaults());
   }
 
 
@@ -80,28 +76,33 @@ class ZendAfi_View_Helper_AdvancedSearch extends ZendAfi_View_Helper_BaseHelper
   }
 
 
-  protected function _advancedForm($search_form) {
+  protected function _advancedForm($search_form, $content) {
     if(!$form_wrapper = $search_form->getFormInstance())
-      return $this->_renderDefaultAdvancedSearch();
+      return $content;
+
+    if((!$form_wrapper->isFormVisible() || $form_wrapper->hasError())
+       && !Class_Users::isCurrentUserCanAccesBackend())
+      return $content;
+
+    $html = $this->_advancedFormLabel($search_form);
 
-    if($form_wrapper->hasError())
-      return $this->_renderErrors($form_wrapper);
+    if($form_wrapper->hasError()) {
+      $content[] = $html . $this->_renderErrors($form_wrapper);
+      return $content;
+    }
 
-    $html = $this->view->renderForm($this->_populateForm($form_wrapper->getForm()));
+    if(!$form_wrapper->isFormVisible())
+      $html = $this->_tag('p', $this->_('Ce formulaire n\'est pas visible.'), ['class' => 'error']) . $html;
 
-    if($form_wrapper->isFormVisible())
-      return $html;
+    $html .= $this->view->renderForm($this->_populateForm($form_wrapper->getForm()));
+    $content[] = $html;
 
-    return Class_Users::isCurrentUserCanAccesBackend()
-      ? $this->_tag('p', $this->_('Ce formulaire n\'est pas visible.'), ['class' => 'error']) . $html
-      : $this->_renderDefaultAdvancedSearch();
+    return $content;
   }
 
 
   protected function _renderErrors($form_wrapper) {
-    return Class_Users::isCurrentUserCanAccesBackend()
-      ? $this->_tag('p', implode(BR, $form_wrapper->getErrors()), ['class' => 'error'])
-      : $this->_renderDefaultAdvancedSearch();
+    return $this->_tag('p', implode(BR, $form_wrapper->getErrors()), ['class' => 'error']);
   }
 
 
diff --git a/tests/scenarios/AdvancedSearch/AdvancedSearchTest.php b/tests/scenarios/AdvancedSearch/AdvancedSearchTest.php
index f347d98d01a..8ebaf0af935 100644
--- a/tests/scenarios/AdvancedSearch/AdvancedSearchTest.php
+++ b/tests/scenarios/AdvancedSearch/AdvancedSearchTest.php
@@ -240,7 +240,9 @@ class AdvancedSearch_AdvancedSearchNofileCustomFormSelectedNotLoggedTest
   protected function _prepareFixtures() {
     parent::_prepareFixtures();
     ZendAfi_Auth::getInstance()->clearIdentity();
-    $this->fixture('Class_SearchForm', ['id' => 3, 'filename' => '']);
+    $this->fixture('Class_SearchForm', ['id' => 3,
+                                        'label' => 'DVD',
+                                        'filename' => '']);
   }
 
 
@@ -248,6 +250,12 @@ class AdvancedSearch_AdvancedSearchNofileCustomFormSelectedNotLoggedTest
   public function shouldDisplayAdvancedSearch() {
     $this->assertXPath('//form//input[@name="rech_titres"]');
   }
+
+
+  /** @test */
+  public function shouldNotDisplayDVD() {
+    $this->assertNotXpathContentContains('//h3', 'DVD');
+  }
 }
 
 
-- 
GitLab