From 45c245740f0e64826495b9a7be5a220f14bfba22 Mon Sep 17 00:00:00 2001
From: Arthur Suzuki <arthur.suzuki@biblibre.com>
Date: Mon, 13 May 2019 19:04:23 +0200
Subject: [PATCH] hotline#91532 : SIGB Koha : updates error messages displayed
 in case of failure when placing a hold through the webservice.

---
 VERSIONS_HOTLINE/91532                        |  1 +
 .../Class/WebService/SIGB/Koha/Service.php    | 10 +--
 tests/fixtures/KohaFixtures.php               |  8 ++
 .../Class/WebService/SIGB/KohaTest.php        | 77 +++++++++++++++++++
 .../HandleBranchcode/HandleBranchcodeTest.php | 10 +--
 5 files changed, 93 insertions(+), 13 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/91532

diff --git a/VERSIONS_HOTLINE/91532 b/VERSIONS_HOTLINE/91532
new file mode 100644
index 00000000000..24247dd6cc4
--- /dev/null
+++ b/VERSIONS_HOTLINE/91532
@@ -0,0 +1 @@
+ - ticket #91532 : SIGB Koha : mise à jour des message d'erreur affichés en cas d'echec sur une réservation via les webservices.
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Koha/Service.php b/library/Class/WebService/SIGB/Koha/Service.php
index 26c661bb5dd..ea5e374369d 100644
--- a/library/Class/WebService/SIGB/Koha/Service.php
+++ b/library/Class/WebService/SIGB/Koha/Service.php
@@ -48,7 +48,10 @@ class Class_WebService_SIGB_Koha_Service extends Class_WebService_SIGB_AbstractR
                        'cannotReserveFromOtherBranches' => $this->_('réservation impossible dans cette bibliothèque'),
                        'tooManyReserves' => $this->_('nombre maximum de réservations atteint'),
                        'notReservable' => $this->_('ce document ne peut normalement pas être réservé'),
+                       'OpacItemHoldNotAllowed' => $this->_('réservation interdite via le portail pour ce document'),
+                       'LocationNotFound' => $this->_('site de retrait introuvable'),
                        'debarred' => $this->_('compte bloqué'),
+                       'PatronDebarred' => $this->_('compte bloqué'),
                        'expired' => $this->_('compte expiré'),
                        'alreadyReserved' => $this->_('document déjà réservé sur votre compte'),
                        'none_available' => $this->_('aucun document n\'est disponible pour la réservation'),
@@ -185,13 +188,6 @@ class Class_WebService_SIGB_Koha_Service extends Class_WebService_SIGB_AbstractR
   }
 
 
-  protected function _xmlError($xml) {
-    return (false !== strpos($xml, 'OpacItemHoldNotAllowed'))
-      ? $this->_('La réservation à l\'exemplaire est interdite')
-      : '';
-  }
-
-
   protected function _setPickupLocation($args, $code) {
     if (trim($code) && $annexe = Class_CodifAnnexe::findFirstBy(['code' => $code]))
       $args['pickup_location'] = $annexe->getIdOrigine();
diff --git a/tests/fixtures/KohaFixtures.php b/tests/fixtures/KohaFixtures.php
index 45ad5114c0e..738645654e0 100644
--- a/tests/fixtures/KohaFixtures.php
+++ b/tests/fixtures/KohaFixtures.php
@@ -751,6 +751,14 @@ class KohaFixtures {
     }
 
 
+    public static function xmlAuthenticatePatronLostCard() {
+      return '<?xml version="1.0" encoding="UTF-8" ?>
+              <AuthenticatePatron>
+              <code>PatronLostCard</code>
+              </AuthenticatePatron>';
+    }
+
+
     public static function xmlGetPatronInfoLaure() {
       return '<?xml version="1.0" encoding="UTF-8" ?>
         <GetPatronInfo>
diff --git a/tests/library/Class/WebService/SIGB/KohaTest.php b/tests/library/Class/WebService/SIGB/KohaTest.php
index 1c966bd2725..7386c5aa26e 100644
--- a/tests/library/Class/WebService/SIGB/KohaTest.php
+++ b/tests/library/Class/WebService/SIGB/KohaTest.php
@@ -1067,6 +1067,54 @@ class KohaOperationsTest extends KohaTestCase {
     $this->assertEquals(array('statut' => false, 'erreur' => 'Réservation impossible'),
                         $this->service->reserverExemplaire($this->_lafond, $this->_exemplaire_mireille_abeille, ''));
   }
+
+
+  /** @test @see http://forge.afi-sa.fr/issues/91532 */
+  public function reserverExemplaireShouldBeExplicitIfPatronDebarred() {
+    $this->mock_web_client
+      ->whenCalled('open_url')
+      ->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/ilsdi.pl?service=HoldTitle&patron_id=572&bib_id=89863&request_location=127.0.0.1')
+      ->answers('<HoldTitle>
+                    <code>PatronDebarred</code>
+                 </HoldTitle>');
+
+    $this->assertEquals(
+                        ['statut' => false,
+                         'erreur' => 'Réservation impossible : compte bloqué'],
+                        $this->service->reserverExemplaire($this->_lafond, $this->_exemplaire_mireille_abeille, ''));
+  }
+
+
+  /** @test */
+  public function reserverExemplaireShouldBeExplicitIfOpacItemHoldNotAllowed() {
+    $this->mock_web_client
+      ->whenCalled('open_url')
+      ->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/ilsdi.pl?service=HoldTitle&patron_id=572&bib_id=89863&request_location=127.0.0.1')
+      ->answers('<HoldTitle>
+                    <code>OpacItemHoldNotAllowed</code>
+                 </HoldTitle>');
+
+    $this->assertEquals(
+                        ['statut' => false,
+                         'erreur' => 'Réservation impossible : réservation interdite via le portail pour ce document'],
+                        $this->service->reserverExemplaire($this->_lafond, $this->_exemplaire_mireille_abeille, ''));
+  }
+
+
+  /** @test */
+  public function reserverExemplaireShouldBeExplicitIfLocationNotFound() {
+    $this->mock_web_client
+      ->whenCalled('open_url')
+      ->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/ilsdi.pl?service=HoldTitle&patron_id=572&bib_id=89863&request_location=127.0.0.1')
+      ->answers('<HoldTitle>
+                    <code>LocationNotFound</code>
+                 </HoldTitle>');
+
+    $this->assertEquals(
+                        ['statut' => false,
+                         'erreur' => 'Réservation impossible : site de retrait introuvable'],
+                        $this->service->reserverExemplaire($this->_lafond, $this->_exemplaire_mireille_abeille, ''));
+  }
 }
 
 
@@ -1318,6 +1366,13 @@ class KohaAuthenticateWSTest extends KohaTestCase {
               'password' => '1989'])
       ->answers(KohaFixtures::xmlAuthenticatePatronOk())
 
+      ->whenCalled('postData')
+      ->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/ilsdi.pl',
+             ['service' => 'AuthenticatePatron',
+              'username' => 'gilles',
+              'password' => '1984'])
+      ->answers(KohaFixtures::xmlAuthenticatePatronLostCard())
+
       ->whenCalled('open_url')
       ->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/ilsdi.pl?service=GetPatronInfo&patron_id=96138&show_contact=1&show_loans=1&show_holds=1')
       ->answers(KohaFixtures::xmlGetPatronInfoDupont());
@@ -1329,9 +1384,19 @@ class KohaAuthenticateWSTest extends KohaTestCase {
                                                  'idabon' => 'john',
                                                  'id_site' => 3]);
 
+    $this->user_lost = $this->fixture('Class_Users', ['id' => 11,
+                                                 'login' => 'gilles',
+                                                 'password' => '1984',
+                                                 'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
+                                                 'idabon' => 'gilles',
+                                                 'id_site' => 3]);
+
+
     $this->emprunteur = $this->service->getEmprunteur($this->user);
+    $this->emprunteur_lost = $this->service->getEmprunteur($this->user_lost);
 
     $this->emprunteur->updateUser($this->user);
+    $this->emprunteur_lost->updateUser($this->user_lost);
   }
 
   public function expectedUser() {
@@ -1359,6 +1424,18 @@ class KohaAuthenticateWSTest extends KohaTestCase {
   }
 
 
+  /** @test */
+  public function userWithLostCardShouldThrowError() {
+    $this->mock_web_client
+      ->whenCalled('postData')
+      ->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/ilsdi.pl',
+             ['service' => 'AuthenticatePatron',
+              'username' => 'john',
+              'password' => '1989'])
+      ->answers(KohaFixtures::xmlAuthenticatePatronLostCard());
+
+    $this->assertFalse($this->emprunteur_lost->isValid());
+  }
 }
 
 
diff --git a/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php b/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php
index 6a92ca98e8e..cc0e07ba5d9 100644
--- a/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php
+++ b/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php
@@ -181,12 +181,10 @@ class HandleBranchcodeHoldItemAndHoldTitleTest extends HandleBranchcodeTestCase
       ->whenCalled('open_url')
       ->with($this->ilsdi . '?service=HoldItem&item_id=&pickup_location=CHY-GB')
       ->willDo(function() {$this->expected_call [] = 1;
-                           return '<HoldTitle>
-                                   <title>Mireille l\'abeille</title>
-                                   <pickup_location>Bibliothèque Départementale de la Meuse</pickup_location>
-                                   <HoldItem>OpacItemHoldNotAllowed</HoldItem>
-                                 </HoldTitle>';
-                           })
+                           return '<HoldItem>
+                                      <code>OpacItemHoldNotAllowed</code>
+                                   </HoldItem>';
+                          })
 
       ->whenCalled('open_url')
       ->with($this->ilsdi . '?service=HoldTitle&request_location=127.0.0.1&pickup_location=CHY-GB')
-- 
GitLab