From cb12c6778140f56e3bfb9931ff8f53955714194a Mon Sep 17 00:00:00 2001
From: Ghislain Loas <gloas@afi-sa.fr>
Date: Tue, 2 May 2023 08:34:28 +0000
Subject: [PATCH] hotline 177809 fix borrower login when membership label has
 changed

---
 VERSIONS_HOTLINE/177809                       |  1 +
 library/Class/Membership.php                  | 15 +++----
 .../WebService/SIGB/Nanook/Emprunteur.php     |  2 +-
 tests/library/Class/MembershipTest.php        | 44 +++++++++++++++++++
 4 files changed, 52 insertions(+), 10 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/177809
 create mode 100644 tests/library/Class/MembershipTest.php

diff --git a/VERSIONS_HOTLINE/177809 b/VERSIONS_HOTLINE/177809
new file mode 100644
index 00000000000..af13e5d60a6
--- /dev/null
+++ b/VERSIONS_HOTLINE/177809
@@ -0,0 +1 @@
+ - correctif #177809 : Compte lecteur : Correction de la connexion à son compte lecteur.
\ No newline at end of file
diff --git a/library/Class/Membership.php b/library/Class/Membership.php
index f4c435f84b5..04487f58e44 100644
--- a/library/Class/Membership.php
+++ b/library/Class/Membership.php
@@ -21,16 +21,13 @@
 
 
 class MembershipLoader extends Storm_Model_Loader {
-  protected
-    $_used_memberships_cache;
-
-  public function findOrCreate(string $code,
-                               string $libelle) : Class_Membership {
-    $params = ['code' => $code ?? 0];
-    if ($libelle)
-      $params['libelle'] = $libelle;
 
-    return Class_Membership::findFirstBy($params) ?? Class_Membership::newInstance($params);
+  public function findOrCreate(string $uniq_code,
+                               string $label) : Class_Membership {
+    $params = ['code' => $uniq_code];
+    $membership = Class_Membership::findFirstBy($params) ?? Class_Membership::newInstance($params);
+    $membership->setLibelle($label);
+    return $membership;
   }
 
 
diff --git a/library/Class/WebService/SIGB/Nanook/Emprunteur.php b/library/Class/WebService/SIGB/Nanook/Emprunteur.php
index e7beb4e270f..afe88f0fcbe 100644
--- a/library/Class/WebService/SIGB/Nanook/Emprunteur.php
+++ b/library/Class/WebService/SIGB/Nanook/Emprunteur.php
@@ -36,7 +36,7 @@ class Class_WebService_SIGB_Nanook_Emprunteur extends Class_WebService_SIGB_Empr
 
   protected function _processSubscriptions(Class_Users $user) :void{
     foreach ($this->getSubscriptions() as $subscription){
-      $membership = Class_Membership::findOrCreate( $subscription->getId(), $subscription->getLabel());
+      $membership = Class_Membership::findOrCreate($subscription->getId(), $subscription->getLabel());
       if ($membership->isNew())
         $membership->save();
       $user_membership = Class_User_Membership::findOrCreate(
diff --git a/tests/library/Class/MembershipTest.php b/tests/library/Class/MembershipTest.php
new file mode 100644
index 00000000000..54a2d1db177
--- /dev/null
+++ b/tests/library/Class/MembershipTest.php
@@ -0,0 +1,44 @@
+<?php
+/**
+ * Copyright (c) 2012-2023, 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
+ */
+
+
+class MembershipFindOrCreateTest extends ModelTestCase {
+  /** @test */
+  public function shouldReturnMembershipUniqInDB() {
+    $uniq_in_db =
+      $this->fixture(Class_Membership::class,
+                     ['id' => 1,
+                      'code' => 'UNIQ IN DB',
+                      'libelle' => 'ABC']);
+
+    $this->assertEquals($uniq_in_db, Class_Membership::findOrCreate('UNIQ IN DB', 'Nouveau libelle'));
+    return $uniq_in_db;
+  }
+
+
+  /**
+   * @test
+   * @depends shouldReturnMembershipUniqInDB
+   */
+  public function uniqInDbLabelShouldBeNewLabel(Class_Membership $membership) {
+    $this->assertEquals('Nouveau libelle', $membership->getLibelle());
+  }
+}
-- 
GitLab