From 5ddc664b6c47039bf683e33220943d726e11db44 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Wed, 4 Feb 2015 15:14:58 +0100
Subject: [PATCH] 
 hotline_#18504_pergame_exemplaire_same_id_origine_between_album_and_sigb fix
 test failures

---
 library/Class/Profil.php                      |  4 +-
 library/Class/Reservation.php                 |  4 +-
 library/Class/Systeme/PergameService.php      | 61 ++++++------
 library/Class/WebService/SIGB/Exemplaire.php  | 22 ++---
 library/Class/WebService/SIGB/Reservation.php |  4 +-
 .../Class/WebService/SIGB/PergameTest.php     | 97 +++++++++----------
 6 files changed, 89 insertions(+), 103 deletions(-)

diff --git a/library/Class/Profil.php b/library/Class/Profil.php
index 880b7ec61fd..cd9b9ac39a9 100644
--- a/library/Class/Profil.php
+++ b/library/Class/Profil.php
@@ -212,8 +212,8 @@ class Class_Profil extends Storm_Model_Abstract {
 																																								 'plan' => 1,
 																																								 'resa' => 1,
 																																								 'dispo' => 1,
-																																								 'date_retour' => 0],
-																															 'en_pret' => 'En prêt']),
+																																								 'date_retour' => 0,
+																																								 'en_pret' => Class_WebService_SIGB_Exemplaire::DISPO_EN_PRET]]),
 				 'hauteur_banniere' => 100,
 				 'mail_site' => '',
 				 'mail_suggestion_achat' => '',
diff --git a/library/Class/Reservation.php b/library/Class/Reservation.php
index 1f76954e597..2e75a6ed7b1 100644
--- a/library/Class/Reservation.php
+++ b/library/Class/Reservation.php
@@ -23,8 +23,8 @@ class Class_Reservation extends Storm_Model_Abstract {
 	protected $_table_name = 'reservations';
 	protected $_table_primary = 'id_resa';
 
-	protected $_belongs_to = array('bib' => array('model' => 'Class_Bib',
-																								'referenced_in' => 'id_site'));
+	protected $_belongs_to = ['bib' => ['model' => 'Class_Bib',
+																			'referenced_in' => 'id_site']];
 
 	protected $_notice;
 
diff --git a/library/Class/Systeme/PergameService.php b/library/Class/Systeme/PergameService.php
index 33c2dabf0fd..710f8c982bb 100644
--- a/library/Class/Systeme/PergameService.php
+++ b/library/Class/Systeme/PergameService.php
@@ -16,7 +16,7 @@
  *
  * 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 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 class Class_Systeme_PergameService {
@@ -58,7 +58,7 @@ class Class_Systeme_PergameService {
 		$nb_resas=fetchOne("select count(*) from reservations where IDABON='".$this->user->IDABON."' and ORDREABON=".$this->user->ORDREABON);
 		return $nb_resas;
 	}
-	
+
 
 	public function getPrets() {
 		$data=fetchAll("select * from prets where IDABON='".$this->user->IDABON."' and ORDREABON=".$this->user->ORDREABON." and EN_COURS=1");
@@ -80,31 +80,26 @@ class Class_Systeme_PergameService {
 
 
 	public function getDisponibilite($ex) {
-		// terme en pret
-		$tmp=Class_Profil::getCurrentProfil()->getCfgNoticeAsArray();
-		$libelle_en_pret="emprunté";
-		if (array_isset("en_pret", $tmp["exemplaires"]))
-			$libelle_en_pret=$tmp["exemplaires"]["en_pret"];
-		
 		// verif pret
 		$code_barres=$ex["code_barres"];
-		$prets = fetchAll("select * from prets where code_barres='$code_barres' and EN_COURS=1");
+		$prets = Class_Pret::findAllBy(['code_barres' => $code_barres,
+																		'en_cours' => 1]);
 
 		// Activité
 		$ex["dispo"]=$ex["activite"];
 		$ex["reservable"]=true;
 		if ($prets)
 		{
-			$ex["dispo"]=$libelle_en_pret;
+			$ex["dispo"] = Class_Profil::getCurrentProfil()->getCfgNoticeAsArray()['exemplaires']['en_pret'];
 			$ex["date_retour"] = strftime('%d/%m/%y' ,strtotime($prets[0]['DATE_RETOUR']));
-		}	
+		}
 		else
 		{
 			$regles=$this->getReglesReservation($ex["id_bib"]);
 			if($regles["Autoriser_docs_disponibles"]==1) $ex["reservable"]=true;
 			else $ex["reservable"]=false;
 		}
-		
+
 		// nombre de réservations
 		$nb_resas=fetchOne("select count(*) from reservations where ID_NOTICE_ORIGINE=".intval($ex["id_origine"]));
 		if($nb_resas)
@@ -152,7 +147,7 @@ class Class_Systeme_PergameService {
 			return $notice;
 		}
 
-		return ($model = Class_Notice::find($id_notice)) 
+		return ($model = Class_Notice::find($id_notice))
 			? $model->getNotice('JA') : [];
 	}
 
@@ -166,26 +161,26 @@ class Class_Systeme_PergameService {
 		}
 
 		foreach($ids as $id) {
-			if ($inSql > '') 
+			if ($inSql > '')
 				$inSql.=",";
 			$inSql.=$id["id_notice"];
 		}
 
 		$id_notice = fetchOne("select id_notice from notices where id_notice in($inSql) and type_doc=$support");
 
-		return ($model = Class_Notice::find($id_notice)) 
+		return ($model = Class_Notice::find($id_notice))
 			? $model->getNotice('JA') : [];
 	}
 
 
 	public function reserverExemplairePergame($id_bib, $exemplaire, $code_annexe) {
-		if (!$this->user || !$this->user->ID_USER) 
+		if (!$this->user || !$this->user->ID_USER)
 			return ['erreur' => 'Vous devez être connecté pour réserver un document'];
 
-		if(!$this->user->IDABON) 
+		if(!$this->user->IDABON)
 			return ['erreur' => 'Vous devez être connecté en tant qu\'abonné pour réserver un document'];
-		
-		if (!$exemplaire) 
+
+		if (!$exemplaire)
 			return ['erreur' => 'Une erreur s\'est produite lors de la lecture de la notice.'];
 
 		$notice = $exemplaire->getNotice();
@@ -202,22 +197,22 @@ class Class_Systeme_PergameService {
 			'idabon' => $id_abon,
 			'ordreabon'=> $ordre_abon]);
 
-		if ($resa) 
+		if ($resa)
 			return ['erreur' => 'Vous avez déjà réservé ce document le ' . formatDate($resa->getDateResa(), 1)];
 
 		$regles = $this->getReglesReservation($exemplaire->getIdBib());
 
 		// controle quota par carte
 		$nb = Class_Reservation::countBy(['idabon' => $id_abon]);
-		if ($nb >= $regles['Max_par_carte']) 
+		if ($nb >= $regles['Max_par_carte'])
 			return ['erreur' => 'La réservation est impossible car vous avez atteint le nombre maximum de réservations sur votre carte.'];
-		
+
 		// controle quota par document
 		$nb = Class_Reservation::countBy(['id_notice_origine' => $id_origine]);
 		if ($nb >= $regles['Max_par_document'])
 			return ['erreur' => 'La réservation est impossible car le nombre maximum de réservations pour ce document a été atteint (' . $regles["Max_par_document"] . ').'];
-		
-		if (!$id_bib) 
+
+		if (!$id_bib)
 			$id_bib = 1;
 
 		$time_source = self::getTimeSource();
@@ -241,7 +236,7 @@ class Class_Systeme_PergameService {
 
 	 public function supprimerReservation($id_reservation)  {
 		 $resa = fetchEnreg("select * from reservations where ID_RESA=$id_reservation");
-		 if(!$resa) 
+		 if(!$resa)
 			 return false;
 		 sqlExecute("delete from reservations where ID_RESA=$id_reservation");
 
@@ -269,7 +264,7 @@ class Class_Systeme_PergameService {
 		 $nbProlong = (int)$pret["NB_PROLONGATIONS"];
 		 $nbProlong += 1;
 		 $dateRetour = $pret["DATE_RETOUR"];
-		 if($nbProlong > $regles["Nombre_max_par_document"]) 
+		 if($nbProlong > $regles["Nombre_max_par_document"])
 			 return ['statut' => 0,
 			         'erreur' => 'Le prêt n\'a pas pu être prolongé car il a atteint le nombre de prolongations autorisé.' . $complement_msg];
 
@@ -277,7 +272,7 @@ class Class_Systeme_PergameService {
 		 $anteriorite_max = (int)$regles['Anteriorite_max_en_jours'];
 		 if ($anteriorite_max) {
 			 $ecart = ecartDates($dateJour, $dateRetour);
-			 if ($ecart>$anteriorite_max) 
+			 if ($ecart>$anteriorite_max)
 				 return ['statut' => 0,
 				         'erreur' => 'Le prêt n\'a pas pu être prolongé car il a un retard trop important.' . $complement_msg];
 		 }
@@ -285,14 +280,14 @@ class Class_Systeme_PergameService {
 		 // Controle si le doc est réservé
 		 if ($regles['Interdire_si_reservation'] == 1) {
 			 $controle = fetchOne("Select Count(*) From reservations Where ID_NOTICE_ORIGINE=".$pret["ID_NOTICE_ORIGINE"]);
-			 if($controle > 0) 
+			 if($controle > 0)
 				 return ['statut' => 0,
 				         'erreur' => 'Le prêt n\'a pas pu être prolongé car il est réservé.' . $complement_msg];
 		 }
 
 		 // On prolonge
 		 $newDate = ajouterJours($pret["DATE_RETOUR"], $regles['Duree_en_jours']);
-		 while($newDate<=$dateJour) 
+		 while($newDate<=$dateJour)
 			 $newDate = ajouterJours($newDate,$regles['Duree_en_jours']);
 		 $tempsProlong=(int)$regles['Duree_en_jours'];
 
@@ -312,7 +307,7 @@ class Class_Systeme_PergameService {
 
 	 private function ecrireTransaction($type_mvt, $enreg) {
 		 $data = '';
-		 foreach($enreg as $item) 
+		 foreach($enreg as $item)
 			 $data .= $item . '|';
 		 Class_Transaction::newInstance(['type_mvt' => $type_mvt, 'data' => $data])
 			 ->save();
@@ -320,13 +315,13 @@ class Class_Systeme_PergameService {
 
 
 	public function getReglesReservation($id_bib) {
-		if (!$id_bib) 
+		if (!$id_bib)
 			return false;
 		$bib = Class_IntBib::find($id_bib);
 		$data = $bib->getCommParamsAsArray();
-		if (!isset($data["Max_par_carte"])) 
+		if (!isset($data["Max_par_carte"]))
 			$data["Max_par_carte"] = 3;
-		if (!isset($data["Max_par_document"])) 
+		if (!isset($data["Max_par_document"]))
 			$data["Max_par_document"] = 3;
 		return $data;
 	}
diff --git a/library/Class/WebService/SIGB/Exemplaire.php b/library/Class/WebService/SIGB/Exemplaire.php
index 749d489f922..82383398640 100644
--- a/library/Class/WebService/SIGB/Exemplaire.php
+++ b/library/Class/WebService/SIGB/Exemplaire.php
@@ -16,7 +16,7 @@
  *
  * 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 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 class Class_WebService_SIGB_Exemplaire {
@@ -99,16 +99,16 @@ class Class_WebService_SIGB_Exemplaire {
 	public function getExemplaireOPAC() {
 		if (isset($this->_exemplaire_opac))
 			return $this->_exemplaire_opac;
-		
+
 		if ($no_notice = $this->getNoNotice())
 			$params = array('id_origine' => $no_notice);
-		
+
 		if ($this->code_barre)
 			$params = array('code_barres' => $this->code_barre);
 
 		if (!isset($params))
 			return null;
-		
+
 		return $this->_exemplaire_opac = Class_Exemplaire::getLoader()->findFirstBy($params);
 	}
 
@@ -182,7 +182,7 @@ class Class_WebService_SIGB_Exemplaire {
 		return $this->bibliotheque;
 	}
 
- 
+
 	public function getAuteur(){
 		if (!$this->auteur  and ($notice = $this->getNoticeOPAC()))
 			$this->auteur = $notice->getAuteurPrincipal();
@@ -294,13 +294,7 @@ class Class_WebService_SIGB_Exemplaire {
 
 
 	public function getLibelleDispoEnPret() {
-		if (!$tmp = Class_Profil::getCurrentProfil()->getCfgNoticeAsArray())
-			return self::DISPO_EN_PRET;
-
-		if (array_isset("en_pret", $tmp["exemplaires"]))
-			 return $tmp["exemplaires"]["en_pret"];
-
-		return self::DISPO_EN_PRET;
+		return Class_Profil::getCurrentProfil()->getCfgNoticeAsArray()['exemplaires']['en_pret'];
 	}
 
 
@@ -367,11 +361,11 @@ class Class_WebService_SIGB_Exemplaire {
 		return $this;
 	}
 
-	
+
 	public function getEdition() {
 		return $this->edition;
 	}
-	
+
 
 	public function getDisponibiliteLabel() {
 		return $this->_disponibiliteLabel;
diff --git a/library/Class/WebService/SIGB/Reservation.php b/library/Class/WebService/SIGB/Reservation.php
index 8626b0d7884..049bfbfe3ae 100644
--- a/library/Class/WebService/SIGB/Reservation.php
+++ b/library/Class/WebService/SIGB/Reservation.php
@@ -67,8 +67,8 @@ class Class_WebService_SIGB_Reservation extends Class_WebService_SIGB_Exemplaire
 		if (!$code_annexe = $this->getAttribute('Lieu'))
 			return;
 
-		if ($annexe = Class_CodifAnnexe::getLoader()->findFirstBy(array('code' => $code_annexe)))
-				$this->setBibliotheque($annexe->getLibelle());
+		if ($annexe = Class_CodifAnnexe::findFirstBy(['code' => $code_annexe]))
+			$this->setBibliotheque($annexe->getLibelle());
 	}
 
 
diff --git a/tests/library/Class/WebService/SIGB/PergameTest.php b/tests/library/Class/WebService/SIGB/PergameTest.php
index a8999ac69be..bf02aaf6461 100644
--- a/tests/library/Class/WebService/SIGB/PergameTest.php
+++ b/tests/library/Class/WebService/SIGB/PergameTest.php
@@ -23,6 +23,16 @@ abstract class PergameServiceTestCase extends Storm_Test_ModelTestCase {
 	public function setUp() {
 		parent::setUp();
 
+		Class_Profil::setCurrentProfil($this->fixture('Class_Profil',
+																									['id' => 1,
+																									 'libelle' => 'Pergame FTW']));
+
+
+		$this->fixture('Class_Notice',
+									 ['id' => 5,
+										'titre_principal' => 'Trouble maker',
+										'auteur_princiapl' => 'pergame']);
+
 		$this->notice_potter = $this->fixture('Class_Notice',
 																					['id' => 1,
 																					 'titre_principal' => 'Harry Potter',
@@ -41,7 +51,7 @@ abstract class PergameServiceTestCase extends Storm_Test_ModelTestCase {
 																							 'id_notice' => 1,
 																							 'code_barres' => 'C-24',
 																							 'id_origine' => '1HP',
-																							 'activite' => 'En rayon',
+																							 'activite' => 'En prêt',
 																							 'id_bib' => 2]);
 
 		$this->potter_cran_reserve = $this->fixture('Class_Exemplaire',
@@ -50,6 +60,7 @@ abstract class PergameServiceTestCase extends Storm_Test_ModelTestCase {
 																								 'code_barres' => 'C-25',
 																								 'id_origine' => '1HP',
 																								 'activite' => 'En rayon',
+																								 'zone995' => 'I\' from sigb',
 																								 'id_bib' => 2]);
 
 		$this->potter_cran_dispo = $this->fixture('Class_Exemplaire',
@@ -60,6 +71,15 @@ abstract class PergameServiceTestCase extends Storm_Test_ModelTestCase {
 																							 'activite' => 'En rayon',
 																							 'id_bib' => 2]);
 
+		$this->fixture('Class_Exemplaire',
+									 ['id' => 27,
+										'id_notice' => 5,
+										'code_barres' => 'C-24',
+										'id_origine' => '1HP',
+										'activite' => 'En rayon',
+										'id_bib' => 2]);
+
+
 		$this->fixture('Class_IntBib',
 									 ['id' => 1])
 				 ->setCommParams(serialize(["Autoriser_docs_disponibles" => 1]))
@@ -223,6 +243,12 @@ class PergameServiceGetEmprunteurTest extends PergameServiceTestCase {
 		$resa = $this->emprunteur_jc->getReservations()[1];
 		$this->assertEmpty($resa->getBibliotheque());
 	}
+
+
+	/** @test */
+	public function firstReservationRecordShouldBeHarryPotter() {
+		$this->assertEquals('Harry Potter', $this->_first_reservation->getNoticeOPAC()->getTitrePrincipal());
+	}
 }
 
 
@@ -232,57 +258,28 @@ class PergameServiceGetExemplairePotterTest extends PergameServiceTestCase {
 	public function setUp() {
 		parent::setUp();
 
-		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Exemplaire')
-			->whenCalled('findAllBy')
-			->with(array('role' => 'notice',
-									 'model' => $this->notice_potter))
-			->answers(array($this->potter_annecy,
-											$this->potter_cran_prete,
-											$this->potter_cran_reserve, $this->potter_cran_dispo))
-
-			->whenCalled('findFirstBy')
-			->with(array('id_origine' => '1HP',
-									 'id_bib' => 1))
-			->answers($this->potter_annecy)
-
-			->whenCalled('findFirstBy')
-			->with(array('id_origine' => '1HP',
-									 'id_bib' => 2))
-			->answers($this->potter_cran_prete);
-
-
-		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Pret')
-			->whenCalled('findFirstBy')
-			->answers(null)
-
-			->whenCalled('findFirstBy')
-			->with(array('id_site' => 2,
-									 'id_notice_origine' => '1HP',
-									 'code_barres' => 'C-24',
-									 'EN_COURS' => 1))
-			->answers(Class_Pret::getLoader()
-								->newInstanceWithId(59)
-								->setCodeBarres('C-24')
-								->setIdNoticeOrigine(1)
-								->setIdPergame('1HP')
-								->setEnCours(1)
-								->setDateRetour('2010-09-07')
-								->setIdSite(2));
+		$this->fixture('Class_Pret',
+									 ['id' => 59,
+										'code_barres' => 'C-24',
+										'id_notice_origine' => 1,
+										'id_pergame' => '1HP',
+										'en_cours' => 1,
+										'date_retour' => '2010-09-07',
+										'id_site' => 2]);
 
+		$this->fixture('Class_Reservation',
+									 ['id' => 34,
+										'id_site' => 2,
+										'id_notice_origine' => 1]);
+
+		$this->_exemplaire_annecy =
+			Class_WebService_SIGB_Pergame::getService(['id_bib' => 1])
+			->getExemplaire('1HP', 'A-23');
+
+		$this->_exemplaire_cran_prete =
+			Class_WebService_SIGB_Pergame::getService(['id_bib' => 2])
+			->getExemplaire('1HP', 'C-24');
 
-		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Reservation')
-			->whenCalled('findAllBy')
-			->answers(array())
-
-			->whenCalled('findAllBy')
-			->with(array('id_site' => 2, 'id_notice_origine' => '1HP'))
-			->answers(array(Class_Reservation::getLoader()
-											->newInstanceWithId(34)
-											->setIdSite(2)
-											->setIdNoticeOrigine(1)));
-
-		$this->_exemplaire_annecy = Class_WebService_SIGB_Pergame::getService(array('id_bib' => 1))->getExemplaire('1HP', 'A-23');
-		$this->_exemplaire_cran_prete = Class_WebService_SIGB_Pergame::getService(array('id_bib' => 2))->getExemplaire('1HP', 'C-24');
 		$this->_exemplaire_cran_reserve = $this->_service_cran->getExemplaire('1HP', 'C-25');
 		$this->_exemplaire_cran_dispo = $this->_service_cran->getExemplaire('1HP', 'C-26');
 	}
-- 
GitLab