From f431fd69ecd97b2a3d4e34eaa2b5b5c47389c26a Mon Sep 17 00:00:00 2001
From: Patrick Barroca <pbarroca@afi-sa.fr>
Date: Wed, 1 Jul 2020 17:07:55 +0200
Subject: [PATCH] hotline #113404 : fix error on suggest creation

---
 VERSIONS_HOTLINE/113404                       |  1 +
 .../WebService/SIGB/Koha/BuySuggestForm.php   | 13 +++-
 .../WebService/SIGB/Nanook/BuySuggestForm.php | 13 +++-
 library/ZendAfi/Form/SuggestionAchat.php      | 12 ++++
 .../ZendAfi/Form/SuggestionAchat/Abstract.php | 10 +++-
 .../User/RichContent/NewSuggestion.php        |  4 +-
 tests/scenarios/Templates/TemplatesTest.php   | 60 +++++++++++++++++++
 7 files changed, 108 insertions(+), 5 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/113404

diff --git a/VERSIONS_HOTLINE/113404 b/VERSIONS_HOTLINE/113404
new file mode 100644
index 00000000000..113ba36cb1e
--- /dev/null
+++ b/VERSIONS_HOTLINE/113404
@@ -0,0 +1 @@
+ - ticket #113404 : Magasin de thème : Correction d'une erreur possible lors de la création d'une suggestion d'achat
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Koha/BuySuggestForm.php b/library/Class/WebService/SIGB/Koha/BuySuggestForm.php
index 0925c859377..6c27c7fe9cb 100644
--- a/library/Class/WebService/SIGB/Koha/BuySuggestForm.php
+++ b/library/Class/WebService/SIGB/Koha/BuySuggestForm.php
@@ -85,5 +85,16 @@ class Class_WebService_SIGB_Koha_BuySuggestForm extends ZendAfi_Form_SuggestionA
 
       ->addElement('submit', 'submit', ['label' => $this->_('Envoyer')]);
   }
+
+
+  /** @return string */
+  public function titleValue() {
+    return $this->getValue('Title');
+  }
+
+
+  /** @return string */
+  public function authorValue() {
+    return $this->getValue('Author');
+  }
 }
-?>
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Nanook/BuySuggestForm.php b/library/Class/WebService/SIGB/Nanook/BuySuggestForm.php
index b053bc092ba..3d4a3e9b6bf 100644
--- a/library/Class/WebService/SIGB/Nanook/BuySuggestForm.php
+++ b/library/Class/WebService/SIGB/Nanook/BuySuggestForm.php
@@ -95,5 +95,16 @@ class Class_WebService_SIGB_Nanook_BuySuggestForm extends ZendAfi_Form_Suggestio
 
       ->addElement('submit', 'submit', ['label' => $this->_('Envoyer')]);
   }
+
+
+  /** @return string */
+  public function titleValue() {
+    return $this->getValue('Title');
+  }
+
+
+  /** @return string */
+  public function authorValue() {
+    return $this->getValue('Author');
+  }
 }
-?>
diff --git a/library/ZendAfi/Form/SuggestionAchat.php b/library/ZendAfi/Form/SuggestionAchat.php
index d7ec5dde984..2c762ba0b3f 100644
--- a/library/ZendAfi/Form/SuggestionAchat.php
+++ b/library/ZendAfi/Form/SuggestionAchat.php
@@ -102,4 +102,16 @@ class ZendAfi_Form_SuggestionAchat extends ZendAfi_Form_SuggestionAchat_Abstract
     $this->removeElement('submit');
     return $this;
   }
+
+
+  /** @return string */
+  public function titleValue() {
+    return $this->getValue('titre');
+  }
+
+
+  /** @return string */
+  public function authorValue() {
+    return $this->getValue('auteur');
+  }
 }
diff --git a/library/ZendAfi/Form/SuggestionAchat/Abstract.php b/library/ZendAfi/Form/SuggestionAchat/Abstract.php
index 76555df9ef2..20ee177b367 100644
--- a/library/ZendAfi/Form/SuggestionAchat/Abstract.php
+++ b/library/ZendAfi/Form/SuggestionAchat/Abstract.php
@@ -19,7 +19,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
-class ZendAfi_Form_SuggestionAchat_Abstract extends ZendAfi_Form {
+abstract class ZendAfi_Form_SuggestionAchat_Abstract extends ZendAfi_Form {
 
   protected $_user;
 
@@ -53,4 +53,12 @@ class ZendAfi_Form_SuggestionAchat_Abstract extends ZendAfi_Form {
       ->setAttrib('id', 'suggestion')
       ->setAttrib('class', 'zend_form');
   }
+
+
+  /** @return string */
+  abstract public function titleValue();
+
+
+  /** @return string */
+  abstract public function authorValue();
 }
diff --git a/library/templates/Intonation/Library/View/Wrapper/User/RichContent/NewSuggestion.php b/library/templates/Intonation/Library/View/Wrapper/User/RichContent/NewSuggestion.php
index 97ef534e223..1a1c2670e71 100644
--- a/library/templates/Intonation/Library/View/Wrapper/User/RichContent/NewSuggestion.php
+++ b/library/templates/Intonation/Library/View/Wrapper/User/RichContent/NewSuggestion.php
@@ -63,8 +63,8 @@ class Intonation_Library_View_Wrapper_User_RichContent_NewSuggestion extends Int
     if ($this->_records) {
       $html [] = $this->_view->tag('p', $this->_('Les documents suivants sont dans le catalogue.'));
       $html [] = $this->_view->tag('p', $this->_('Votre suggestion %s de %s en fait-elle partie ?',
-                                                 $this->_form->getElement('Title')->getValue(),
-                                                 $this->_form->getElement('Author')->getValue()));
+                                                 $this->_form->titleValue(),
+                                                 $this->_form->authorValue()));
 
       $html [] = $this->_view->button(new Class_Entity(['Text' => $this->_('Oui'),
                                                         'Url' => $this->_view->url(['action' => 'suggestion-achat']),
diff --git a/tests/scenarios/Templates/TemplatesTest.php b/tests/scenarios/Templates/TemplatesTest.php
index d5fd63eaaa3..c661221273a 100644
--- a/tests/scenarios/Templates/TemplatesTest.php
+++ b/tests/scenarios/Templates/TemplatesTest.php
@@ -5471,6 +5471,66 @@ class TemplatesSuggestionAchatAddTest extends TemplatesIntonationTestCase {
 
 
 
+class TemplatesSuggestionAchatAddPostWithSearchRecordTest extends TemplatesIntonationTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    Class_AdminVar::set('SEARCH_RECORD_BEFORE_SUGGEST', 1);
+
+    $search_result = $this->mock()
+                          ->whenCalled('fetchRecords')
+                          ->answers([$this->fixture('Class_Notice', ['id' => 12])]);
+
+    Class_MoteurRecherche::setInstance($this->mock()
+                                       ->whenCalled('lancerRecherche')
+                                       ->answers($search_result));
+
+    Class_Profil::find(72)
+      ->setModulePreference('abonne',
+                            'suggestion-achat-add',
+                            'help-text',
+                            'Entrez votre suggestion');
+
+    $this->postDispatch('/opac/abonne/suggestion-achat-add/id_profil/72',
+                        ['titre' => 'Les Dieux eux-même',
+                         'auteur' => 'Isaac Asimov',
+                         'type_doc_id' => '1',
+                         'description_url' =>	'',
+                         'isbn' => '',
+                         'bib_id' => '2',
+                         'commentaire' => 'Indispensable',
+                         'submit' => 'Envoyer']);
+  }
+
+
+  public function tearDown() {
+    Class_MoteurRecherche::setInstance(null);
+    parent::tearDown();
+  }
+
+
+  /** @test */
+  public function pageShouldContainsIsYourSuggestionInResult() {
+    $this->assertXPathContentContains('//p',
+                                      'Votre suggestion Les Dieux eux-même de Isaac Asimov en fait-elle partie ?');
+  }
+
+
+  /** @test */
+  public function pageShouldContainsYesChoice() {
+    $this->assertXPathContentContains('//button', 'Oui');
+  }
+
+
+  /** @test */
+  public function pageShouldContainsNoChoice() {
+    $this->assertXPathContentContains('//button', 'Non');
+  }
+}
+
+
+
+
 class TemplatesDispatchLibraryWidgetWithOSMTest extends TemplatesIntonationTestCase {
   public function setUp() {
     parent::setUp();
-- 
GitLab