From 36cae8ac4b30ffb9f113a19696e77de3d8ac1ee8 Mon Sep 17 00:00:00 2001
From: Arthur Suzuki <arthur.suzuki@biblibre.com>
Date: Thu, 11 Mar 2021 18:15:55 +0100
Subject: [PATCH] hotline#129284: Koha users with Debarred information could
 not subscribe to activities

---
 .../WebService/SIGB/Koha/PatronInfoReader.php |  9 ++-
 tests/fixtures/KohaFixtures.php               | 61 +++++++++++++++++++
 .../Class/WebService/SIGB/KohaTest.php        | 56 +++++++++++++++++
 3 files changed, 124 insertions(+), 2 deletions(-)

diff --git a/library/Class/WebService/SIGB/Koha/PatronInfoReader.php b/library/Class/WebService/SIGB/Koha/PatronInfoReader.php
index 0f75c707c5a..b4517b61192 100644
--- a/library/Class/WebService/SIGB/Koha/PatronInfoReader.php
+++ b/library/Class/WebService/SIGB/Koha/PatronInfoReader.php
@@ -21,7 +21,7 @@
 
 class Class_WebService_SIGB_Koha_PatronInfoReader
   extends Class_WebService_SIGB_AbstractILSDIPatronInfoReader {
-  use Class_WebService_SIGB_Koha_TraitFormat, Trait_Translator;
+  use Class_WebService_SIGB_Koha_TraitFormat, Trait_Translator, Trait_TimeSource;
 
   protected
     $_current_exemplaire_operation,
@@ -115,7 +115,12 @@ class Class_WebService_SIGB_Koha_PatronInfoReader
 
 
   public function endDebarred($data) {
-    if ($data)
+    if ($data === 1){
+      $this->_emprunteur->beBlocked();
+      return;
+    }
+
+    if( strtotime($data) > $this->getCurrentTime())
       $this->_emprunteur->beBlocked();
   }
 
diff --git a/tests/fixtures/KohaFixtures.php b/tests/fixtures/KohaFixtures.php
index 8ee3e56e023..a273b9a8d6c 100644
--- a/tests/fixtures/KohaFixtures.php
+++ b/tests/fixtures/KohaFixtures.php
@@ -1935,6 +1935,67 @@ class KohaFixtures {
         }
 
 
+        public static function xmlGetPatronInfoDebarredWithDate() {
+          return '<?xml version="1.0" encoding="UTF-8" ?>
+<GetPatronInfo>
+  <privacy_guarantor_checkouts>0</privacy_guarantor_checkouts>
+  <updated_on>2019-02-18 09:26:19</updated_on>
+  <checkprevcheckout>inherit</checkprevcheckout>
+  <debarredcomment>OVERDUES_PROCESS 09/07/2017 23:41</debarredcomment>
+  <altcontactphone></altcontactphone>
+  <sex>F</sex>
+  <title>Mme.</title>
+  <country>FRANCE</country>
+  <altcontactsurname></altcontactsurname>
+  <sort1>EMPL</sort1>
+  <lang>default</lang>
+  <contactnote></contactnote>
+  <dateenrolled>2016-09-15</dateenrolled>
+  <categorycode>INDICALF</categorycode>
+  <B_address></B_address>
+  <altcontactzipcode></altcontactzipcode>
+  <B_email></B_email>
+  <email>badpatron@debarred.lol</email>
+  <altcontactfirstname></altcontactfirstname>
+  <othernames></othernames>
+  <borrowernotes></borrowernotes>
+  <mobile></mobile>
+  <zipcode>42600</zipcode>
+  <branchcode>MTRMTB</branchcode>
+  <fax></fax>
+  <surname>DEBARRED</surname>
+  <dateexpiry>2017-09-15</dateexpiry>
+  <address2></address2>
+  <phone></phone>
+  <B_phone></B_phone>
+  <B_country></B_country>
+  <privacy>1</privacy>
+  <loans>
+  </loans>
+  <altcontactaddress3></altcontactaddress3>
+  <city>Arkham</city>
+  <B_city></B_city>
+  <sort2></sort2>
+  <phonepro>123456790</phonepro>
+  <opacnote></opacnote>
+  <cardnumber>0000000</cardnumber>
+  <altcontactcountry></altcontactcountry>
+  <dateofbirth>1985-01-17</dateofbirth>
+  <debarred>2021-03-01</debarred>
+  <altcontactaddress1></altcontactaddress1>
+  <B_address2></B_address2>
+  <altcontactaddress2></altcontactaddress2>
+  <branchname>fictivBib</branchname>
+  <B_zipcode></B_zipcode>
+  <firstname>Bad Patron</firstname>
+  <address>rue de la zile</address>
+  <borrowernumber>420</borrowernumber>
+  <login_attempts>0</login_attempts>
+</GetPatronInfo>
+';
+        }
+
+
         public static function xmlGetRecord2046() {
           return '<?xml version="1.0" encoding="UTF-8" ?>
 <GetRecords>
diff --git a/tests/library/Class/WebService/SIGB/KohaTest.php b/tests/library/Class/WebService/SIGB/KohaTest.php
index 02d9f95a5f9..340ce24a739 100644
--- a/tests/library/Class/WebService/SIGB/KohaTest.php
+++ b/tests/library/Class/WebService/SIGB/KohaTest.php
@@ -877,6 +877,62 @@ class KohaGetEmprunteurDebarredWithIdSIGBTest extends KohaTestCase {
 
 
 
+class KohaGetEmprunteurDebarredDateWithIdSIGBTest extends KohaTestCase {
+
+  public function setUp() {
+    parent::setUp();
+    $this->mock_web_client
+      ->whenCalled('postData')
+      ->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/ilsdi.pl',
+             ['service' => 'AuthenticatePatron',
+              'username' => 'JEAN',
+              'password' => 'zork'])
+      ->answers(KohaFixtures::xmlLookupPatronDebarred())
+      ->whenCalled('open_url')
+      ->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/ilsdi.pl?service=GetPatronInfo&patron_id=420&show_contact=1&show_loans=0&show_holds=1')
+      ->answers(KohaFixtures::xmlGetPatronInfoDebarredWithDate());
+
+
+  }
+
+  protected function _setTimeSourceAndgetEmprunteur($date){
+    Class_WebService_SIGB_Koha_PatronInfoReader::setTimeSource(new TimeSourceForTest($date));
+
+    $this->jean = $this->service
+      ->getEmprunteur($this->fixture('Class_Users',
+                                     ['id' => 43,
+                                      'login' => 'JEAN',
+                                      'password' => 'zork',
+                                      'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
+                                      'idabon' => 'Jean',
+                                      'id_site' => 3,
+                                      'id_sigb' => '420']));
+
+  }
+
+
+  public function tearDown() {
+    Class_WebService_SIGB_Koha_PatronInfoReader::setTimeSource(null);
+  }
+
+
+  /** @test */
+  public function debarredWithDateInFutureShouldBeBlocked() {
+    $this->_setTimeSourceAndgetEmprunteur('2021-02-20');
+    $this->assertTrue($this->jean->isBlocked());
+  }
+
+
+  /** @test */
+  public function debarredWithDateInPastShouldNotBeBlocked() {
+    $this->_setTimeSourceAndgetEmprunteur('2021-03-02');
+    $this->assertFalse($this->jean->isBlocked());
+  }
+}
+
+
+
+
 class KohaGetEmprunteurJeanAndreWithIdSIGBTest extends KohaTestCase {
   public function setUp() {
     parent::setUp();
-- 
GitLab