From f4948dcb1a1ff5255cf9cacb3ceb969278938804 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@git-test.afi-sa.fr>
Date: Fri, 10 Jan 2014 17:02:27 +0000
Subject: [PATCH] Epub : refacto Opds en RessourceNumerique et ajout de la
 bibliotheque dans l'import

---
 library/Class/AlbumRessource.php              | 15 +++----
 library/Class/TypeDoc.php                     |  4 +-
 .../WebService/BibNumerique/Abstract.php      |  4 +-
 .../BibNumerique/RessourceNumerique.php       | 23 +++++++++--
 .../Class/WebService/OPDS/CatalogEntry.php    | 21 ++++------
 .../admin/controllers/OpdsControllerTest.php  | 34 +++++++--------
 .../library/Class/WebService/ArteVODTest.php  | 41 ++++++++++---------
 7 files changed, 76 insertions(+), 66 deletions(-)

diff --git a/library/Class/AlbumRessource.php b/library/Class/AlbumRessource.php
index 2d4cbc50210..7759e37a020 100644
--- a/library/Class/AlbumRessource.php
+++ b/library/Class/AlbumRessource.php
@@ -24,16 +24,11 @@ class AlbumRessourceLoader extends Storm_Model_Loader {
 	 * @return int
 	 */
 	public function getNextOrderFor($album) {
-		$row = $this->getTable()->fetchRow(
-			$this->getTable()->select()
-									->from(
-										$this->getTable()->info(Zend_Db_Table::NAME),
-										array('order' => 'MAX(ordre)')
-									)
-									->where('id_album = ?', $album->getId())
-		);
-
-		return ($row->order + 1);
+		$ressource=Class_AlbumRessource::findFirstBy(['order' => 'ordre desc',
+															'id_album' => $album->getId()]);
+		if (!$ressource)
+			return 1;
+		return ($ressource->getOrdre()+ 1);
 	}
 }
 
diff --git a/library/Class/TypeDoc.php b/library/Class/TypeDoc.php
index bef8a22f2ab..d5c1108d502 100644
--- a/library/Class/TypeDoc.php
+++ b/library/Class/TypeDoc.php
@@ -19,7 +19,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
  */
 
-class TypeDocLoader {
+class TypeDocLoader extends Storm_Model_Loader  {
 	protected $_all_instances;
 	
 	public function __construct() {
@@ -64,7 +64,7 @@ class TypeDocLoader {
 		return $this->findAll();
 	}
 
-	public function findAll() {
+	public function findAll($select = NULL) {
 		if (isset($this->_all_instances))
 			return $this->_all_instances;
 	
diff --git a/library/Class/WebService/BibNumerique/Abstract.php b/library/Class/WebService/BibNumerique/Abstract.php
index 0bd925dbb0f..6a49cfd08be 100644
--- a/library/Class/WebService/BibNumerique/Abstract.php
+++ b/library/Class/WebService/BibNumerique/Abstract.php
@@ -61,9 +61,9 @@ abstract class Class_WebService_BibNumerique_Abstract extends Class_WebService_A
 			$this->getLogger()->info(sprintf('Traitement de la page %s / %s',
 																			 $current_page,
 																			 $total_page));
-
+			$ress_numerique=$reader->getRessourcesNumeriques();
 			$this->_harvested_ids = array_merge($this->_harvested_ids,
-				                                  $this->_importRessources($reader->getRessourcesNumeriques()));
+				                                  $this->_importRessources($ress_numerique));
 			$current_page++;
 		} while ($current_page <= $total_page);
 
diff --git a/library/Class/WebService/BibNumerique/RessourceNumerique.php b/library/Class/WebService/BibNumerique/RessourceNumerique.php
index 3d18de4a950..821b1370f0d 100644
--- a/library/Class/WebService/BibNumerique/RessourceNumerique.php
+++ b/library/Class/WebService/BibNumerique/RessourceNumerique.php
@@ -36,7 +36,7 @@
 	protected $_matieres = [];
 	protected $_ressources = [];
 	protected $_zones = [];
-
+	protected $_bibliotheques='';
 
 
 	public function setId($id) {
@@ -193,6 +193,14 @@
 		return $this->_zones[$codif];
 	}
 
+	public function setBibliotheques($bibs) {
+		$this->_bibliotheques=$bibs;
+		return $this;
+	}
+
+	public function getBibliotheques() {
+		return $this->_bibliotheques;
+	}
 
 
 	public function addMatiere($matiere_libelle) {
@@ -241,7 +249,9 @@
 
 
 		$this->fillAlbum($album);
-		
+
+		$this->fillAlbumTypeDoc($album);
+
 		$this->updateRessourceNumeriqueNotes($album);
 		//$this->importZones($album);
 		if ($album->save())
@@ -293,7 +303,14 @@
 		return $this;
 	}
 
-  public function fillAlbum($album) {}
+  public function fillAlbum($album) {
+	}
+
+	public function fillAlbumTypeDoc($album) {
+		if ($type_doc=$album->getTypeDoc())
+			$album->setBibliotheques($type_doc->getBibliotheques());
+	}
+
 
 	public function getRessourceCategorieLibelle(){}
 
diff --git a/library/Class/WebService/OPDS/CatalogEntry.php b/library/Class/WebService/OPDS/CatalogEntry.php
index dd16173ce90..f13df5a4d02 100644
--- a/library/Class/WebService/OPDS/CatalogEntry.php
+++ b/library/Class/WebService/OPDS/CatalogEntry.php
@@ -18,7 +18,7 @@
  * along with AFI-OPAC 2.0; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
  */
-class Class_WebService_OPDS_CatalogEntry {
+class Class_WebService_OPDS_CatalogEntry extends Class_WebService_BibNumerique_RessourceNumerique { 
 	protected $_properties;
 	protected $_isNotice;
 	protected $_files;
@@ -60,27 +60,20 @@ class Class_WebService_OPDS_CatalogEntry {
 	}
 
 
-	public function import() {
-		$libelle = sprintf('import opds du %s', date('d M Y'));
-		if (!$category = Class_AlbumCategorie::getLoader()->findFirstBy(array('libelle' => $libelle)))
-			$category = Class_AlbumCategorie::getLoader()->newInstance()->setLibelle($libelle);
+	public function getRessourceLibelle() {
+		return sprintf('import opds du %s', date('d M Y'));
+	}
 
-		$category->save();
 
-		$album = Class_Album::getLoader()->newInstance()
-			->setTitre($this->getTitle())
-			->setAuteur($this->getAuthor())
-			->setCategorie($category)
+	public function fillAlbum($album) {
+		$album ->setAuteur($this->getAuthor())
 			->beEPUB();
-		$album->save();
-
 		foreach ($this->_files as $file)
 			$file->newRessourceInAlbum($album);
 
-
-		return $album;
 	}
 
+
 	public function getFiles() {
 		return $this->_files;
 	}
diff --git a/tests/application/modules/admin/controllers/OpdsControllerTest.php b/tests/application/modules/admin/controllers/OpdsControllerTest.php
index 04a36b12149..cbd95a96832 100644
--- a/tests/application/modules/admin/controllers/OpdsControllerTest.php
+++ b/tests/application/modules/admin/controllers/OpdsControllerTest.php
@@ -358,9 +358,8 @@ class Admin_OpdsControllerUnknownIdsActionErrorsTest extends Admin_OpdsControlle
 class Admin_OpdsControllerActionsWithUnknowCatalogTest extends Admin_OpdsControllerTestCase {
 	public function setUp() {
 		parent::setUp();
-		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_OpdsCatalog')
-			->whenCalled('find')
-			->answers(null);
+		Class_OpdsCatalog::beVolatile();
+
 	}
 
 	
@@ -526,21 +525,18 @@ class Admin_OpdsControllerBrowseEbooksGratuitsImportTest extends Admin_OpdsContr
 			->whenCalled('open_url')
 			->with('http://www.opacsgratuits.com/opds/feed.php?mode=maj')
 			->answers(OPDSFeedFixtures::ebooksGratuitsLastUpdatedXml());
-		
-		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Album')
-			->whenCalled('save')
-			->willDo(function($model){
-					$model->setId(777);
-					return true;
-				});
 
-		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_AlbumRessource')
-			->whenCalled('save')
-			->answers(true);
+		$this->fixture('Class_Album', ['id'=>776]);
+		Class_AlbumRessource::beVolatile();
+		Class_AlbumCategorie::beVolatile();
+		$codif_type_doc=$this->fixture('Class_CodifTypeDoc', ['id' => Class_TypeDoc::EPUB,
+																													'famille_id' => Class_CodifTypeDoc::INCONNU,
+																													'bibliotheques' => '1;5']);
 
-		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_AlbumCategorie')
-			->whenCalled('save')
-			->answers(true);
+		$this->fixture('Class_TypeDoc', ['id'=>Class_TypeDoc::EPUB, 
+																		 'codif_type_doc' => $codif_type_doc,
+																		 'label'=> 'Type doc'
+ 										 ]);
 
 
 		Class_WebService_OPDS_EntryFile::defaultDownloader(Storm_Test_ObjectWrapper::mock()
@@ -558,6 +554,12 @@ class Admin_OpdsControllerBrowseEbooksGratuitsImportTest extends Admin_OpdsContr
 		$this->assertRedirectTo('/admin/album/edit_album/id/777');
 	}
 
+  /** @test */
+	public function albumCreatedShouldContainsBibliotheque() {
+		$album=Class_Album::find(777);
+		$this->assertEquals('1;5', $album->getBibliotheques());
+	}
+	
 }
 
 
diff --git a/tests/library/Class/WebService/ArteVODTest.php b/tests/library/Class/WebService/ArteVODTest.php
index 7421de20c73..4f801c87ffd 100644
--- a/tests/library/Class/WebService/ArteVODTest.php
+++ b/tests/library/Class/WebService/ArteVODTest.php
@@ -77,44 +77,40 @@ class ArteVODHarverstingTwoFilmsInTwoPages extends ArteVODHarverstingTestCase {
 			->answers(ArteVODFixtures::secondFilm())
 
 			->beStrict();
+		$this->fixture('Class_AlbumCategorie',null);
+		Class_Album::beVolatile();
 
-		$this->_category_wrapper = Storm_Test_ObjectWrapper::onLoaderOfModel('Class_AlbumCategorie')
-			->whenCalled('findFirstBy')->answers(null)
-			->whenCalled('save')->answers(true);
+		$codif_type_doc=$this->fixture('Class_CodifTypeDoc', ['id' => Class_TypeDoc::ARTEVOD,
+																					'famille_id' => Class_CodifTypeDoc::INCONNU,
+																					'bibliotheques' => '1;8']);
 
-		$this->_album_wrapper = Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Album')
-			->whenCalled('findFirstBy')->answers(null)
-			->whenCalled('save')->answers(true)
-			->whenCalled('deleteBy')->answers(null);
+		$this->fixture('Class_TypeDoc', ['id'=>Class_TypeDoc::ARTEVOD, 
+																		 'codif_type_doc' => $codif_type_doc,
+																		 'label'=> 'Type doc'
+ 										 ]);
 
 		$service = new Class_WebService_BibNumerique_ArteVOD();
 		$service->harvest();
 	}
 
 
-	/** @test */
-	public function shouldHaveCreatedCategorie() {
-		$this->assertTrue($this->_category_wrapper->methodHasBeenCalled('save'));
-	}
-
-
 	/** @test */
 	public function shouldHaveCreatedTwoAlbums() {
-		$this->assertEquals(2, $this->_category_wrapper->methodCallCount('save'));
+		$this->assertEquals(2,count(Class_AlbumCategorie::findAll()));
 	}
 
 
 	/** @test */
 	public function secondAlbumExternalUriShouldBeBlancheNeige() {
 		$this->assertEquals('http://www.mediatheque-numerique.com/films/blanche-nage',
-												$this->_album_wrapper->getFirstAttributeForLastCallOn('save')->getExternalUri());
+												Class_Album::findFirstBy(['order' => 'id desc'])->getExternalUri());
 	}
 
 
 
 	/** @test */
 	public function secondAlbumZone215ShouldBe70Min() {
-		$notes = $this->_album_wrapper->getFirstAttributeForLastCallOn('save')->getNotesAsArray();
+		$notes = Class_Album::findFirstBy(['order' => 'id desc'])->getNotesAsArray();
 		$this->assertEquals(['a' => 'Vidéo (70 min)'],
 												$notes[0]['data']);
 	}
@@ -132,20 +128,27 @@ class ArteVODHarverstingTwoFilmsInTwoPages extends ArteVODHarverstingTestCase {
 
 	/** @test */
 	public function bybyeBlondieActorsShouldContainsSoko() {
-		$album=$this->_album_wrapper->getAttributesForMethodCallAt('save',-2)[0];
+
+		$album=Class_Album::findFirstBy(['titre'=> 'Bye Bye Blondie']);
 		$this->assertEquals(['a' => 'Soko',
 												 '4' => '005'],
 												$album->getNotesAsArray()[4]['data']);
 	}
 
 
-/** @test */
+  /** @test */
 	public function bybyeBlondieTagsShouldContainsAdaptationLitteraireRetrouvaillesEtc() {
-		$album=$this->_album_wrapper->getAttributesForMethodCallAt('save',-2)[0];
+		$album=Class_Album::findFirstBy(['titre'=> 'Bye Bye Blondie']);
 		$this->assertEquals('Adaptation littéraire;Changement;Drogue;Jeunesse;Liberté;Retrouvailles;Rock;Séduction',
 												$album->getTags());
 	}
 
+	/** @test */
+	public function bibliothequeShouldBeImportedInByeByeBlondie() {
+		$album=Class_Album::findFirstBy(['titre'=> 'Bye Bye Blondie']);
+		$this->assertEquals('1;8',$album->getBibliotheques());
+	}
+
 
 }
 
-- 
GitLab