From a3f8e749b814fba648660ca5ca2fae00b865b6eb Mon Sep 17 00:00:00 2001
From: gloas <gloas@git-test.afi-sa.fr>
Date: Tue, 20 Aug 2013 13:54:05 +0000
Subject: [PATCH] ajout des tests + refacto class abonne cosmogramme avant rel
 #9357

---
 cosmogramme/php/classes/classe_abonne.php     | 112 +++++++++++-------
 .../php/classes/classe_codif_langue.php       |   1 -
 .../php/classes/classe_codif_matiere.php      |   1 -
 cosmogramme/php/classes/classe_iso2709.php    |   2 +
 cosmogramme/tests/bootstrap.php               |   4 +-
 .../AbonneControllerNewslettersTest.php       |   1 +
 6 files changed, 77 insertions(+), 44 deletions(-)

diff --git a/cosmogramme/php/classes/classe_abonne.php b/cosmogramme/php/classes/classe_abonne.php
index 30df14938f4..b9eadd615ce 100644
--- a/cosmogramme/php/classes/classe_abonne.php
+++ b/cosmogramme/php/classes/classe_abonne.php
@@ -39,10 +39,22 @@ class abonne
 		$profil=fetchEnreg("select * from profil_donnees where id_profil=$id_profil");
 		$this->type_accents=$profil["accents"];
 		$attribs=unserialize($profil["attributs"]);
-		if($profil["format"]==4) $this->champs=$attribs[5]["xml_champs_abonne"];
-		else $this->champs=explode(";",$attribs[1]["champs"]);
+		if($profil["format"]==4) 
+			$this->champs=$attribs[5]["xml_champs_abonne"];
+		else 
+			$this->champs=explode(";",$attribs[1]["champs"]);
 	}
 
+
+	public function setChamps($champs) {
+		$this->champs = $champs;
+	}
+
+	public function setidBib($id_bib) {
+		$this->id_bib = $id_bib;
+	}
+
+
 // ----------------------------------------------------------------
 // Import d'une fiche
 // ----------------------------------------------------------------
@@ -57,78 +69,98 @@ class abonne
 // ----------------------------------------------------------------
 	public function importFicheAscii($data)
 	{
+		xdebug_break();
 		// Transco accents
 		$data=$this->changeAccents($data);
 
 		// Données d'import
 		$data=explode(chr(9),$data);
 		for($i=0; $i<count($this->champs); $i++)
-		{
-			$colonne=$this->champs[$i];
-			if($colonne!="NULL") $enreg[$colonne]=$data[$i];
-		}
+			{
+				$colonne=$this->champs[$i];
+				if($colonne!="NULL") 
+					$enreg[$colonne]=$data[$i];
+			}
 
 		// Completer l'enregistrement
-		$enreg["ID_SITE"]=$this->id_bib;
-		$enreg["LOGIN"]=$enreg["IDABON"];
-		$enreg["ROLE"]="abonne_sigb";
-		$enreg["ROLE_LEVEL"]=2;
-		$enreg["STATUT"]=0;
-		if ($enreg["NUM_CARTE"] && strlen($enreg["NUM_CARTE"]>0)) {
+		$enreg = $this->prepareData($enreg);
+
+		if (isset($enreg["NUM_CARTE"]) && strlen($enreg["NUM_CARTE"]>0)) {
 			$enreg["IDABON"]=$enreg["NUM_CARTE"];
 			unset($enreg["NUM_CARTE"]);
 		}
-		if((int)$enreg["ORDREABON"] <1 ) $enreg["ORDREABON"]=1;
-		if(strlen($enreg["NAISSANCE"]) == 10) $enreg["NAISSANCE"]=rendDate($enreg["NAISSANCE"], 0);
-		$enreg["DATE_DEBUT"]=rendDate($enreg["DATE_DEBUT"], 0);
-		$enreg["DATE_FIN"]=rendDate($enreg["DATE_FIN"], 0);
-		if(!$enreg["PASSWORD"] and $enreg["NAISSANCE"]) $enreg["PASSWORD"]=rendDate($enreg["NAISSANCE"],1);
+		if((int)$enreg["ORDREABON"] <1 ) 
+			$enreg["ORDREABON"]=1;
+		
+		if(isset($enreg["NAISSANCE"]) && (strlen($enreg["NAISSANCE"]) == 10)) 
+			$enreg["NAISSANCE"]=rendDate($enreg["NAISSANCE"], 0);
+		
+		if(!$enreg["PASSWORD"] and $enreg["NAISSANCE"]) 
+			$enreg["PASSWORD"]=rendDate($enreg["NAISSANCE"],1);
 
-		// Ecrire
-		global $sql;
-		$id_user=$sql->fetchOne("select ID_USER from bib_admin_users where LOGIN='".$enreg["LOGIN"]."' and ID_SITE=".$this->id_bib." and PASSWORD='".str_replace("'", "\'", $enreg["PASSWORD"])."'");
-		if(!$id_user) $sql->insert("bib_admin_users",$enreg);
-		else $sql->update("update bib_admin_users set @SET@ where ID_USER=$id_user",$enreg);
+		$this->saveorUpdateInDB($enreg);
 	}
 
 // ----------------------------------------------------------------
 // Import d'une fiche en XML
 // ----------------------------------------------------------------
-	public function importFicheXml($data)
-	{
+	public function importFicheXml($data){
 		// Transco accents
 		$data=$this->changeAccents($data);
-
+		
 		// Données d'import
 		$data=new SimpleXMLElement($data);
 		$nombre=count($this->champs);
-		foreach($this->champs as $champ => $balise)
-		{
-			if($balise)
-			{
+		foreach($this->champs as $champ => $balise)	{
+			if($balise){
 				$cmd="\$enreg['$champ']=(string)\$data->".addslashes($balise).";";
 				eval($cmd);
 			}
 		}
 
+		$enreg = $this->prepateData($enreg);
+
 		// formattage des dates
 		$enreg["NAISSANCE"]=rendDate($enreg["NAISSANCE"], 0);
-		$enreg["DATE_DEBUT"]=rendDate($enreg["DATE_DEBUT"], 0);
-		$enreg["DATE_FIN"]=rendDate($enreg["DATE_FIN"], 0);
 		
 		// valeurs par défaut
+		if(!$enreg["ORDREABON"]) 
+			$enreg["ORDREABON"]=1;
+
+		// Ecrire
+		
+
+		$this->saveOrUpdateInDB($id_user,$enreg);
+	}
+
+
+	protected function prepareData($enreg) {
+		$enreg["DATE_DEBUT"]= isset($enreg["DATE_DEBUT"]) ? rendDate($enreg["DATE_DEBUT"], 0) : '';
+		$enreg["DATE_FIN"]= isset($enreg["DATE_FIN"]) ? rendDate($enreg["DATE_FIN"], 0) : '';
 		$enreg["ID_SITE"]=$this->id_bib;
 		$enreg["LOGIN"]=$enreg["IDABON"];
-		if(!$enreg["ORDREABON"]) $enreg["ORDREABON"]=1;
 		$enreg["ROLE"]="abonne_sigb";
 		$enreg["ROLE_LEVEL"]=2;
 		$enreg["STATUT"]=0;
+		return $enreg;
+	}
 
-		// Ecrire
-		$id_user=fetchOne("select ID_USER from bib_admin_users where LOGIN='".$enreg["LOGIN"]."' and ID_SITE=".$this->id_bib);
-		if(!$id_user) sqlInsert("bib_admin_users",$enreg);
-		else sqlUpdate("update bib_admin_users set @SET@ where ID_USER=$id_user",$enreg);
+
+	protected function saveOrUpdateInDB($data){
+		$user = Class_Users::findFirstBy(
+			['login'=>$data["LOGIN"],
+			 'id_site'=>$data['ID_SITE']]);
+		
+		if(!$user)
+			$user= new Class_Users();
+		
+		$user
+			->updateAttributes($data)
+			->save();
+		
 	}
+	
+
 
 // ----------------------------------------------------------------
 // Transformation des accents
@@ -137,14 +169,14 @@ class abonne
 	{
 		if(!trim($chaine)) return $chaine;
 		switch($this->type_accents)
-		{
+			{
 			case 2: // Windows
 				return utf8_encode($chaine);
 			case 3: // Dos
 				for($i=0; $i < strlen($chaine); $i++) $new.=$this->dosDecode($chaine[$i]);
 				return utf8_encode($new);
 			default: return $chaine;
-		}
+			}
 	}
 
 // ----------------------------------------------------------------
@@ -153,7 +185,7 @@ class abonne
 	private function dosDecode($char)
 	{
 		switch($char)
-		{
+			{
 			case 0xe9: $result = 'é';	break ;
 			case 0xe8: $result = 'è';	break ;
 			case 0xeb: $result = 'ë';	break ;
@@ -172,7 +204,7 @@ class abonne
 			case 0xe7: $result = 'ç';	break ;
 			case 0xc7: $result = 'Ç';	break ;
 			default: $result = $char;	break;
-		}
+			}
 		return $result;
 	}
 		
diff --git a/cosmogramme/php/classes/classe_codif_langue.php b/cosmogramme/php/classes/classe_codif_langue.php
index 8a77dd5296a..4ff55e1c445 100644
--- a/cosmogramme/php/classes/classe_codif_langue.php
+++ b/cosmogramme/php/classes/classe_codif_langue.php
@@ -21,7 +21,6 @@
 require_once('classe_codif_abstract.php');
 
 class codif_langue extends codif_abstract {
-	use trait_singleton;
 
 	protected $_table_name = 'codif_langue';
 	protected $_primary_key = 'id_langue';
diff --git a/cosmogramme/php/classes/classe_codif_matiere.php b/cosmogramme/php/classes/classe_codif_matiere.php
index cf1950f88d3..2ad2a8946dc 100644
--- a/cosmogramme/php/classes/classe_codif_matiere.php
+++ b/cosmogramme/php/classes/classe_codif_matiere.php
@@ -21,7 +21,6 @@
 require_once('classe_codif_abstract.php');
 
 class codif_matiere extends codif_abstract{
-	use trait_singleton;
 
 	protected $_table_name = 'codif_matiere';
 	protected $_primary_key = 'id_matiere';
diff --git a/cosmogramme/php/classes/classe_iso2709.php b/cosmogramme/php/classes/classe_iso2709.php
index 14448a22e13..70c2157ef40 100644
--- a/cosmogramme/php/classes/classe_iso2709.php
+++ b/cosmogramme/php/classes/classe_iso2709.php
@@ -439,6 +439,8 @@ class iso2709_record {
 
 	public function update($export_accents = false) {
 		// supprime les lignes vides d'inner_data
+		$data= '';
+		
 		$inner_data_old = array_filter($this->inner_data);
 		$this->inner_data = [];
 		$this->inner_directory = [];
diff --git a/cosmogramme/tests/bootstrap.php b/cosmogramme/tests/bootstrap.php
index b18b827f697..ea78502a27d 100644
--- a/cosmogramme/tests/bootstrap.php
+++ b/cosmogramme/tests/bootstrap.php
@@ -1,7 +1,7 @@
 <?php
 
-//error_reporting(E_ALL^E_DEPRECATED);
-error_reporting(E_ERROR | E_PARSE | E_COMPILE_ERROR);
+//error_reporting(E_ALL^E_DEPRECATED ^E_WARNING);
+xberror_reporting(E_ERROR | E_PARSE | E_COMPILE_ERROR);
 
 ini_set('display_startup_errors', 1);
 ini_set('display_errors', 1);
diff --git a/tests/application/modules/opac/controllers/AbonneControllerNewslettersTest.php b/tests/application/modules/opac/controllers/AbonneControllerNewslettersTest.php
index 88a4672630f..00564bae2bc 100644
--- a/tests/application/modules/opac/controllers/AbonneControllerNewslettersTest.php
+++ b/tests/application/modules/opac/controllers/AbonneControllerNewslettersTest.php
@@ -517,4 +517,5 @@ class AbonneControllerNewslettersValidationsTest extends AbonneControllerWithTwo
 	}
 }
 
+
 ?>
\ No newline at end of file
-- 
GitLab