From 1a9345aaacdc859b94c1b3e9ff62ef21d3aa480c Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Wed, 6 Dec 2017 15:16:49 +0100
Subject: [PATCH] dev #13615 : nanook implementation of loan history

---
 FEATURES/13615                                | 10 +++
 VERSIONS_WIP/13615                            |  1 +
 .../Class/WebService/SIGB/AbstractService.php |  4 +-
 .../SIGB/Nanook/GetLoanHistoryReader.php      | 90 +++++++++++++++++++
 .../Class/WebService/SIGB/Nanook/Service.php  | 13 +++
 tests/fixtures/NanookFixtures.php             | 67 ++++++++++++++
 .../Class/WebService/SIGB/LoansHistory.php    | 45 ++++++++++
 .../Class/WebService/SIGB/NanookTest.php      | 69 ++++++++++++++
 8 files changed, 297 insertions(+), 2 deletions(-)
 create mode 100644 FEATURES/13615
 create mode 100644 VERSIONS_WIP/13615
 create mode 100644 library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php
 create mode 100644 tests/library/Class/WebService/SIGB/LoansHistory.php

diff --git a/FEATURES/13615 b/FEATURES/13615
new file mode 100644
index 00000000000..35975429b05
--- /dev/null
+++ b/FEATURES/13615
@@ -0,0 +1,10 @@
+        '13615' =>
+            ['Label' => $this->_('35/55 Consultation de l'historique des prêts depuis le compte lecteur Bokeh [CCTP Agglo2b] [CCTP Concarneau]'),
+             'Desc' => '',
+             'Image' => '',
+             'Video' => '',
+             'Category' => '',
+             'Right' => function($feature_description, $user) {return true;},
+             'Wiki' => '',
+             'Test' => '',
+             'Date' => '2017-12-06'],
\ No newline at end of file
diff --git a/VERSIONS_WIP/13615 b/VERSIONS_WIP/13615
new file mode 100644
index 00000000000..fd51347f2c5
--- /dev/null
+++ b/VERSIONS_WIP/13615
@@ -0,0 +1 @@
+ - ticket #13615 : 35/55 Consultation de l'historique des prêts depuis le compte lecteur Bokeh [CCTP Agglo2b] [CCTP Concarneau]
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/AbstractService.php b/library/Class/WebService/SIGB/AbstractService.php
index a57ee1bfda1..84a94d24542 100644
--- a/library/Class/WebService/SIGB/AbstractService.php
+++ b/library/Class/WebService/SIGB/AbstractService.php
@@ -129,8 +129,8 @@ abstract class Class_WebService_SIGB_AbstractService {
   }
 
 
-  public function loansHistory() {
-    return [];
+  public function loansHistory($emprunteur, $page) {
+    return new Class_WebService_SIGB_LoansHistory();
   }
 
 
diff --git a/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php b/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php
new file mode 100644
index 00000000000..1a32b4a524d
--- /dev/null
+++ b/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php
@@ -0,0 +1,90 @@
+<?php
+/**
+ * Copyright (c) 2012, 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 Class_WebService_SIGB_Nanook_GetLoanHistoryReader {
+  protected
+    $_loans = [],
+    $_total_count = 0,
+    $_loans_history,
+    $_current_loan;
+
+
+  /**
+   * @param string $xml
+   * @return Class_WebService_SIGB_*_PatronInfoReader
+   */
+  public function parseXML($xml) {
+    $this->_loans_history = new Class_WebService_SIGB_LoansHistory();
+    $this->_xml_parser = Class_WebService_SIGB_Nanook_XMLParser::newInstance()->setElementHandler($this);
+    $this->_xml_parser->parse($xml);
+
+    return $this;
+  }
+
+
+  public function startLoans($attributes) {
+    $this->_loans_history->setTotalCount($attributes['COUNT']);
+  }
+
+
+  public function startLoan()  {
+    $this->_current_loan = Class_WebService_SIGB_Emprunt::newInstanceWithEmptyExemplaire();
+    $this->_loans_history->addLoan($this->_current_loan);
+  }
+
+
+  public function endTitle($data) {
+    $this->_current_loan->setTitre($data);
+  }
+
+  public function endItemId($data) {
+    $this->_current_loan->setId($data);
+    $this->_current_loan->getExemplaire()->setId($data);
+  }
+
+
+  public function endBibId($data) {
+    $this->_current_loan->getExemplaire()->setNoNotice($data);
+  }
+
+
+  public function endAuthor($data) {
+    $this->_current_loan->setAuteur($data);
+  }
+
+
+  public function getHistory() {
+    return $this->_loans_history;
+  }
+
+
+  public function endLocationLabel($data) {
+    $this->_current_loan->setBibliotheque($data);
+  }
+
+
+  public function endBackDate($data) {
+    $date = implode('/', array_reverse(explode('-', $data)));
+    $this->_current_loan->getExemplaire()->setDateRetour($date);
+
+  }
+
+
+}
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Nanook/Service.php b/library/Class/WebService/SIGB/Nanook/Service.php
index d82cbdeab0f..51aeccdbbc5 100644
--- a/library/Class/WebService/SIGB/Nanook/Service.php
+++ b/library/Class/WebService/SIGB/Nanook/Service.php
@@ -135,6 +135,19 @@ class Class_Webservice_SIGB_Nanook_Service extends Class_WebService_SIGB_Abstrac
       : $emprunteur;
   }
 
+  public function providesLoansHistory() {
+    return true;
+  }
+
+
+  public function loansHistory($emprunteur, $page) {
+    $url = $this->buildQueryURL(['service' => 'GetLoanHistory',
+                                 'patronId' => $emprunteur->getId()])
+      .'?pageNumber='.$page;
+    $xml = $this->getWebClient()->open_url($url);
+    return (new Class_WebService_SIGB_Nanook_GetLoanHistoryReader())->parseXML($xml)->getHistory();
+  }
+
 
   // This method is call in Class_Suggestion->save();
   // Class_Suggestion->save() always return true without considering webservice answers.
diff --git a/tests/fixtures/NanookFixtures.php b/tests/fixtures/NanookFixtures.php
index 3edca036e7e..73a74baf3af 100644
--- a/tests/fixtures/NanookFixtures.php
+++ b/tests/fixtures/NanookFixtures.php
@@ -587,4 +587,71 @@ class NanookFixtures {
               <error>PatronNotFound</error>
             </GetPickupLocation>';
   }
+
+
+  public static function xmlGetLoanHistoryPageOne() {
+    return '<GetLoanHistory>
+  <loans count="3" pageNumber="1" pageSize="2">
+    <loan>
+      <bibId>464732
+      </bibId>
+      <itemId>231645
+      </itemId>
+      <title>La femme à droite sur la photo
+      </title>
+      <author>Valentin Musso
+      </author>
+      <locationLabel>Bressuire
+      </locationLabel>
+      <loanDate>2017-08-24
+      </loanDate>
+      <backDate>2017-09-02
+      </backDate>
+    </loan>
+    <loan>
+      <bibId>448794
+      </bibId>
+      <itemId>221545
+      </itemId>
+      <title>Le caméléon
+      </title>
+      <author>Angela Behelle
+      </author>
+      <locationLabel>Bressuire
+      </locationLabel>
+      <loanDate>2017-08-24
+      </loanDate>
+      <backDate>2017-10-07
+      </backDate>
+    </loan>
+  </loans>
+</GetLoanHistory>';
+  }
+
+
+  public static function xmlGetLoanHistoryPageTwo() {
+      return '
+<GetLoanHistory>
+  <loans count="3" pageNumber="2" pageSize="2">
+    <loan>
+      <bibId>127324
+      </bibId>
+      <itemId>110155
+      </itemId>
+      <title>Je te croquerai !
+      </title>
+      <author>Lucy Cousins
+      </author>
+      <locationLabel>Bressuire
+      </locationLabel>
+      <loanDate>2017-08-02
+      </loanDate>
+      <backDate>2017-08-24
+      </backDate>
+    </loan>
+  </loans>
+</GetLoanHistory>';
+
+
+  }
 }
\ No newline at end of file
diff --git a/tests/library/Class/WebService/SIGB/LoansHistory.php b/tests/library/Class/WebService/SIGB/LoansHistory.php
new file mode 100644
index 00000000000..eac85f01a80
--- /dev/null
+++ b/tests/library/Class/WebService/SIGB/LoansHistory.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, 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 Class_WebService_SIGB_LoansHistory {
+  protected $_total_count=0,
+    $_loans = [];
+
+  public function totalCount() {
+    return $this->_total_count;
+  }
+
+
+  public function getLoans() {
+    return $this->_loans;
+  }
+
+  public function setTotalCount($count) {
+    $this->_total_count = $count;
+  }
+
+  public function addLoan($loan) {
+    $this->_loans[] = $loan;
+  }
+}
+
+?>
\ No newline at end of file
diff --git a/tests/library/Class/WebService/SIGB/NanookTest.php b/tests/library/Class/WebService/SIGB/NanookTest.php
index f439fedbbaf..c150351d264 100644
--- a/tests/library/Class/WebService/SIGB/NanookTest.php
+++ b/tests/library/Class/WebService/SIGB/NanookTest.php
@@ -1280,3 +1280,72 @@ class NanookPickupLocationsActiveWithSiteIdTest extends NanookTestCase {
     $this->assertEquals([], $this->_service->pickupLocationsFor($user, $item));
   }
 }
+
+
+class NanookLoanHistoryTest extends NanookTestCase {
+  protected $loans_history,
+    $emprunteur;
+
+
+  public function setUp() {
+    parent::setUp();
+     $this->_mock_web_client
+      ->whenCalled('open_url')
+      ->with('http://localhost:8080/afi_Nanook/ilsdi/service/GetLoanHistory/patronId/34?pageNumber=1')
+      ->answers(NanookFixtures::xmlGetLoanHistoryPageOne())
+      ->whenCalled('open_url')
+      ->with('http://localhost:8080/afi_Nanook/ilsdi/service/GetLoanHistory/patronId/34?pageNumber=2')
+      ->answers(NanookFixtures::xmlGetLoanHistoryPageTwo())
+      ->beStrict();
+     $this->emprunteur = new Class_WebService_SIGB_Emprunteur('34', 'harlock');
+     xdebug_break();
+     $this->loans_history = $this->_service->loansHistory($this->emprunteur,1);
+  }
+
+
+  /** @test */
+  public function userShouldHaveThreeLoans() {
+    $this->assertEquals(3, $this->loans_history->totalCount());
+  }
+
+
+  /** @test */
+  public function oneLoanShouldContainsTitleLeCameleon() {
+    $this->assertEquals("Le caméléon", $this->loans_history->getLoans()[1]->getTitre());
+  }
+
+
+  /** @test */
+  public function firstLoanIdShouldBe231645() {
+    $this->assertEquals("231645", $this->loans_history->getLoans()[0]->getId());
+  }
+
+  /** @test */
+  public function noticeNumberShouldBe464732() {
+    $this->assertEquals(464732, $this->loans_history->getLoans()[0]->getNoNotice());
+  }
+
+
+  /** @test */
+  public function authorShouldBeMusso() {
+    $this->assertEquals('Valentin Musso', $this->loans_history->getLoans()[0]->getAuteur());
+  }
+
+
+  /** @test */
+  public function bibShouldBeBressuire() {
+    $this->assertEquals('Bressuire', $this->loans_history->getLoans()[0]->getBibliotheque());
+  }
+
+
+  /** @test */
+  public function backDateShouldBeSeptember2017() {
+    $this->assertEquals('02/09/2017', $this->loans_history->getLoans()[0]->getDateRetour());
+  }
+
+  /** @test */
+  public function secondPageLoanTitleShouldBeJeteCroquerai() {
+    $this->assertEquals('Je te croquerai !', $this->_service->loansHistory($this->emprunteur,2)->getLoans()[0]->getTitre());
+  }
+
+}
\ No newline at end of file
-- 
GitLab