From cd7e43f504ae29adc530b2d0beee6eef00d51982 Mon Sep 17 00:00:00 2001
From: Henri-Damien LAURENT <hdlaurent@afi-sa.fr>
Date: Tue, 16 Mar 2021 12:12:22 +0100
Subject: [PATCH] hotline#128431 : Notice : Item availability label and
 holdability could be broken wehn barcode contains trailing spaces

---
 VERSIONS_HOTLINE/128431                       |  1 +
 library/Class/WebService/SIGB/Exemplaire.php  | 61 +++++++++++--------
 tests/fixtures/KohaFixtures.php               |  2 +-
 .../Class/WebService/SIGB/NoticeCacheTest.php | 55 +++++++++++++++++
 4 files changed, 92 insertions(+), 27 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/128431
 create mode 100644 tests/library/Class/WebService/SIGB/NoticeCacheTest.php

diff --git a/VERSIONS_HOTLINE/128431 b/VERSIONS_HOTLINE/128431
new file mode 100644
index 00000000000..1413899d012
--- /dev/null
+++ b/VERSIONS_HOTLINE/128431
@@ -0,0 +1 @@
+ - ticket #128431 : Correction d'affichage du statut non disponible et de la réservabilité quand le code barre contient une espace
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Exemplaire.php b/library/Class/WebService/SIGB/Exemplaire.php
index 74bbd3f4c74..be43b6529ba 100644
--- a/library/Class/WebService/SIGB/Exemplaire.php
+++ b/library/Class/WebService/SIGB/Exemplaire.php
@@ -162,7 +162,7 @@ class Class_WebService_SIGB_Exemplaire {
   public function getNoticeOPAC() {
     if (!isset($this->_notice_opac) &&
         ($exemplaire_opac = $this->getExemplaireOPAC()))
-      $this->_notice_opac = $exemplaire_opac->getNotice();
+      $this->setNoticeOPAC($exemplaire_opac->getNotice());
 
     return $this->_notice_opac;
   }
@@ -203,15 +203,15 @@ class Class_WebService_SIGB_Exemplaire {
   }
 
 
-  public function getBibliotheque(){
-    if (!$this->bibliotheque  and ($exemplaire = $this->getExemplaireOPAC()))
-      $this->bibliotheque = $exemplaire->getBibLibelle();
+  public function getBibliotheque() {
+    if (!$this->bibliotheque && ($exemplaire = $this->getExemplaireOPAC()))
+      $this->setBibliotheque($exemplaire->getBibLibelle());
 
     return $this->bibliotheque;
   }
 
 
-  public function getAuteur(){
+  public function getAuteur() {
     return $this->getNoticeOPAC()
       ? $this->_notice_opac->getAuteurPrincipal()
       : $this->auteur;
@@ -223,19 +223,19 @@ class Class_WebService_SIGB_Exemplaire {
   }
 
 
-  public function setAuteur($auteur){
+  public function setAuteur($auteur) {
     $this->auteur = $auteur;
     return $this;
   }
 
 
-  public function setSection($section){
+  public function setSection($section) {
     $this->section = $section;
     return $this;
   }
 
 
-  public function getSection(){
+  public function getSection() {
     return $this->section;
   }
 
@@ -246,7 +246,7 @@ class Class_WebService_SIGB_Exemplaire {
   }
 
 
-  public function getDateRetour(){
+  public function getDateRetour() {
     return $this->date_retour;
   }
 
@@ -262,16 +262,16 @@ class Class_WebService_SIGB_Exemplaire {
   }
 
 
-  public function getCodeBarre(){
+  public function getCodeBarre() {
     if (!$this->code_barre && ($ex_opac = $this->getExemplaireOPAC()))
-      $this->code_barre = $ex_opac->getCodeBarres();
+      $this->setCodeBarre($ex_opac->getCodeBarres());
 
     return $this->code_barre;
   }
 
 
-  public function setCodeBarre($code_barre){
-    $this->code_barre = $code_barre;
+  public function setCodeBarre($code_barre) {
+    $this->code_barre = trim($code_barre);
     return $this;
   }
 
@@ -284,7 +284,7 @@ class Class_WebService_SIGB_Exemplaire {
 
   public function getCote() {
     if (!$this->_cote && ($item = $this->getExemplaireOPAC()))
-      $this->_cote = $item->getCote();
+      $this->setCote($item->getCote());
 
     return $this->_cote;
   }
@@ -301,7 +301,7 @@ class Class_WebService_SIGB_Exemplaire {
   }
 
 
-  public function getId(){
+  public function getId() {
     return $this->id;
   }
 
@@ -323,8 +323,10 @@ class Class_WebService_SIGB_Exemplaire {
   public function getDisponibilite() {
     if ($this->_isEnPret)
       return $this->disponibilite;
+
     if (null !== $this->_disponibiliteLabel)
       return $this->_disponibiliteLabel;
+
     return $this->disponibilite;
   }
 
@@ -395,23 +397,28 @@ class Class_WebService_SIGB_Exemplaire {
     return $this;
   }
 
+
   public function setDisponibiliteExcluDuPret() {
     $this->setDisponibilite($this->message('DISPO_EXCLU_DU_PRET'));
     return $this;
   }
 
 
-  public function setDisponibilite($disponibilite){
+  public function setDisponibilite($disponibilite) {
     $this->disponibilite = $disponibilite;
-    if ($this->disponibilite == "") $this->setDisponibiliteLibre();
+    if ($this->disponibilite == "")
+      $this->setDisponibiliteLibre();
+
     return $this;
   }
 
+
   public function setNbReservations($nb_resas) {
     $this->nb_resas = $nb_resas;
     return $this;
   }
 
+
   public function getNbReservations() {
     return $this->nb_resas;
   }
@@ -461,13 +468,13 @@ class Class_WebService_SIGB_Exemplaire {
   }
 
 
-  public function setReservable($reservable){
+  public function setReservable($reservable) {
     $this->reservable = ($reservable == "true");
     return $this;
   }
 
 
-  public function beReservable(){
+  public function beReservable() {
     return $this->setReservable(true);
   }
 
@@ -486,7 +493,7 @@ class Class_WebService_SIGB_Exemplaire {
   /**
    * @return bool
    */
-  public function isReservable(){
+  public function isReservable() {
     if (isset($this->reservable))
       return $this->reservable;
 
@@ -503,14 +510,14 @@ class Class_WebService_SIGB_Exemplaire {
 
 
   public function setConsultationAvailable($bool = true) {
-    $this->consultation=$bool;
+    $this->consultation = $bool;
   }
 
 
   /**
    * @return bool
    */
-  public function isPilonne(){
+  public function isPilonne() {
     return $this->getDisponibilite() == $this->message('DISPO_PILONNE');
   }
 
@@ -519,22 +526,24 @@ class Class_WebService_SIGB_Exemplaire {
     return (null !== $this->getId());
   }
 
+
   public function isDisponible() {
     return $this->message('DISPO_LIBRE') == $this->disponibilite;
   }
 
 
   /** @codeCoverageIgnore */
-  public function __toString(){
+  public function __toString() {
     $str = $this->titre;
-    if (isset($this->id)) $str.=" #".$this->id.",";
+    if (isset($this->id))
+      $str.=" #".$this->id.",";
+
     $str .= '  Reservable: '.($this->isReservable() ? 'true' : 'false').",";
     $str .= '  Disponibilite: '.$this->getDisponibilite();
     $str .= '  Section:'.$this->getSection();
     $str .= '  Bibliothèque:'.$this->getBibliotheque();
     $str .= '  Auteur:'.$this->getAuteur();
+
     return $str;
   }
 }
-
-?>
diff --git a/tests/fixtures/KohaFixtures.php b/tests/fixtures/KohaFixtures.php
index a273b9a8d6c..2fb600efc71 100644
--- a/tests/fixtures/KohaFixtures.php
+++ b/tests/fixtures/KohaFixtures.php
@@ -827,7 +827,7 @@ class KohaFixtures {
                 <ccode>ROMJEUN</ccode>
                 <itemcallnumber>JR ROW h</itemcallnumber>
                 <date_due></date_due>
-                <barcode>2661660439</barcode>
+                <barcode>2661660439 </barcode>
                 <itemlost>0</itemlost>
                 <datelastseen>2011-02-18</datelastseen>
                 <homebranch>BDM</homebranch>
diff --git a/tests/library/Class/WebService/SIGB/NoticeCacheTest.php b/tests/library/Class/WebService/SIGB/NoticeCacheTest.php
new file mode 100644
index 00000000000..c19eeab3b94
--- /dev/null
+++ b/tests/library/Class/WebService/SIGB/NoticeCacheTest.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Copyright (c) 2012-2021, 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
+ */
+
+require_once 'tests/fixtures/KohaFixtures.php';
+
+class WebService_SIGB_NoticeCacheTest extends ModelTestCase {
+  protected $_exemplaire;
+
+  public function setUp() {
+    parent::setUp();
+
+    Class_Profil::setCurrentProfil($this->fixture('Class_Profil',
+                                                  ['id' => 1,
+                                                   'libelle' => 'Actu']));
+
+    $this->mock_web_client = $this->mock();
+
+    $this->service = Class_WebService_SIGB_Koha::getService(['url_serveur' => 'http://cat-aficg55.biblibre.com/cgi-bin/koha/ilsdi.pl']);
+    $this->service->setWebClient($this->mock_web_client);
+    $this->mock_web_client
+      ->whenCalled('open_url')
+      ->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/ilsdi.pl?service=GetRecords&id=33233')
+      ->answers(KohaFixtures::xmlGetRecordHarryPotter());
+
+    $notice_cache = new Class_WebService_SIGB_NoticeCache($this->service);
+    $this->_exemplaire = $notice_cache->getExemplaire('33233', '2661660439');
+  }
+
+
+  /**
+   * @see https://forge.afi-sa.net/issues/128431
+   * @test
+   */
+  public function exemplaireShouldBeValid() {
+    $this->assertTrue($this->_exemplaire->isValid());
+  }
+}
-- 
GitLab