From 3a41722541ba318bed6c0b585e93783b11c675ae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?ANDRE=20s=C3=A9bastien?= <sandre@afi-sa.fr>
Date: Wed, 13 Mar 2024 10:29:46 +0000
Subject: [PATCH] hotline : #197478 : fix pickup location with no annexes

---
 VERSIONS_HOTLINE/197478                       |  1 +
 .../WebService/SIGB/Koha/CommunityService.php | 29 ++++++-
 .../WebService/SIGB/KohaCommunityTest.php     | 81 ++++++++++++++-----
 3 files changed, 89 insertions(+), 22 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/197478

diff --git a/VERSIONS_HOTLINE/197478 b/VERSIONS_HOTLINE/197478
new file mode 100644
index 00000000000..f8a74cde3d1
--- /dev/null
+++ b/VERSIONS_HOTLINE/197478
@@ -0,0 +1 @@
+ - correctif #197478 : Koha : Exemplaires : Si il n'y a pas d'annexes de créé pour ce portail, une tentative de réservation doit retourner la liste des Annexes possible provenant du SIGB.
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Koha/CommunityService.php b/library/Class/WebService/SIGB/Koha/CommunityService.php
index f459d6db8d1..26dd6d6a136 100644
--- a/library/Class/WebService/SIGB/Koha/CommunityService.php
+++ b/library/Class/WebService/SIGB/Koha/CommunityService.php
@@ -258,7 +258,8 @@ class Class_WebService_SIGB_Koha_CommunityService
   }
 
 
-  public function pickupLocationsFor(Class_Users $user, Class_Exemplaire $item) : array {
+  public function pickupLocationsFor(Class_Users $user, Class_Exemplaire $item): array
+  {
     $url = $this->_buildUrlForEndpointWithParams('items/' . $item->getSubfield(9) . '/pickup_locations',
                                                  ['_match' => 'exact',
                                                   'patron_id' => $user->getEmprunteurId()]);
@@ -271,11 +272,31 @@ class Class_WebService_SIGB_Koha_CommunityService
     if (isset($locations->errors) || isset($locations->error))
       return ['statut' => false, 'erreur' => 'Erreurs du webservice'];
 
+    return (Class_CodifAnnexe::count() > 0)
+      ? $this->_pickupLocationsWithAnnexe($locations)
+      : $this->_pickupLocationsWithoutAnnexe($locations);
+  }
+
+
+  protected function _pickupLocationsWithAnnexe(array $locations): array
+  {
     $pickup_locations = [];
-    foreach($locations as $bib)
+
+    foreach ($locations as $bib)
       if (($annexe = Class_CodifAnnexe::findFirstBy(['id_origine' => $bib->library_id]))
-          && !$annexe->getNoPickup())
-        $pickup_locations[$bib->library_id] = $bib->name;
+          && ! $annexe->getNoPickup())
+        $pickup_locations [$bib->library_id] = $bib->name;
+
+    return $pickup_locations;
+  }
+
+
+  protected function _pickupLocationsWithoutAnnexe(array $locations): array
+  {
+    $pickup_locations = [];
+
+    foreach ($locations as $bib)
+      $pickup_locations [$bib->library_id] = $bib->name;
 
     return $pickup_locations;
   }
diff --git a/tests/library/Class/WebService/SIGB/KohaCommunityTest.php b/tests/library/Class/WebService/SIGB/KohaCommunityTest.php
index bd8aa13b855..a985a37a3f4 100644
--- a/tests/library/Class/WebService/SIGB/KohaCommunityTest.php
+++ b/tests/library/Class/WebService/SIGB/KohaCommunityTest.php
@@ -637,14 +637,44 @@ class KohaCommunityHoldsTest extends KohaCommunityTestCase {
 
 
 
-class KohaCommunityPickupLocationsTest extends KohaCommunityTestCase {
-  protected
-    $_response,
-    $_holds;
+abstract class AbstractKohaCommunityPickupLocationsTestCase extends KohaCommunityTestCase
+{
 
-  public function setUp() {
+  public function setUp()
+  {
     parent::setUp();
 
+    $this->_createAnnexes();
+
+    $this->mock_web_client->whenCalled('open_url')
+                          ->with(static::BASE_URL . 'items/96630/pickup_locations?_match=exact&patron_id=32007',
+                                 ['auth'=> ['user' => 'koha_admin', 'password' => 'k0h@_P455']])
+                          ->answers(KohaCommunityFixtures::getPickupLocations())
+                          ->beStrict();
+
+    $this->item = $this->fixture(Class_Exemplaire::class,
+                           ['id' => 2,
+                            'id_origine' => 96629,
+                            'zone995' => serialize([['clef' => '9',
+                                                     'valeur' => '96630']])]);
+  }
+
+
+  protected function _createAnnexes(): self
+  {
+    return $this;
+  }
+}
+
+
+
+
+class KohaCommunityPickupLocationsWithAnnexeTest
+  extends AbstractKohaCommunityPickupLocationsTestCase
+{
+
+  protected function _createAnnexes(): self
+  {
     $this->fixture(Class_CodifAnnexe::class,
                    ['id' => 15,
                     'libelle' => 'IEP Grenoble',
@@ -657,23 +687,13 @@ class KohaCommunityPickupLocationsTest extends KohaCommunityTestCase {
                     'id_origine' => 'BIBLIOBUS',
                     'no_pickup' => true]);
 
-    $this->mock_web_client->whenCalled('open_url')
-                          ->with(static::BASE_URL . 'items/96630/pickup_locations?_match=exact&patron_id=32007',
-                                 ['auth'=> ['user' => 'koha_admin', 'password' => 'k0h@_P455']])
-                          ->answers(KohaCommunityFixtures::getPickupLocations())
-                          ->beStrict();
-
-    $this->item = $this->fixture(Class_Exemplaire::class,
-                           ['id' => 2,
-                            'id_origine' => 96629,
-                            'zone995' => serialize([['clef' => '9',
-                                                     'valeur' => '96630']])
-                           ]);
+    return $this;
   }
 
 
   /** @test */
-  public function responseStatusShouldBeIEGPBibliothequeSciencePo() {
+  public function responseStatusShouldBeIEGPBibliothequeSciencePo()
+  {
     $this->assertEquals(["IEPG" => "Bibliothèque de Sciences Po Grenoble"],
                         $this->service->pickupLocationsFor($this->user, $this->item));
   }
@@ -682,6 +702,31 @@ class KohaCommunityPickupLocationsTest extends KohaCommunityTestCase {
 
 
 
+class KohaCommunityPickupLocationsWithoutAnnexeTest
+  extends AbstractKohaCommunityPickupLocationsTestCase
+{
+
+  protected function _createAnnexes(): self
+  {
+    Class_CodifAnnexe::deleteBy([]);
+    Class_CodifAnnexe::clearCache();
+
+    return $this;
+  }
+
+
+  /** @test */
+  public function responseStatusShouldContainAllAnnexeFromSIGB()
+  {
+    $this->assertEquals(['IEPG' => 'Bibliothèque de Sciences Po Grenoble',
+                         'BIBLIOBUS' => 'Bibliobus'],
+                        $this->service->pickupLocationsFor($this->user, $this->item));
+  }
+}
+
+
+
+
 class KohaCommunityEditUserTest extends KohaCommunityTestCase {
 
   protected $_patron = ['surname' => 'haddock',
-- 
GitLab