From 0beafe63fd71a60d965af433bb1c0156c5d65aad Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Tue, 23 Apr 2013 10:13:18 +0000
Subject: [PATCH] =?UTF-8?q?Ajout=20fonctionalit=C3=A9s=20permettant=20de?=
 =?UTF-8?q?=20retrouver=20une=20notice=20approchante=20via=20la=20cl=C3=A9?=
 =?UTF-8?q?=20alpha?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitattributes                               |  2 +
 library/Class/Notice/ClefAlpha.php           | 66 +++++++++++++++++
 library/Class/PanierNotice.php               | 33 +++++++++
 tests/library/Class/Notice/ClefAlphaTest.php | 76 ++++++++++++++++++++
 tests/library/Class/PanierNoticeTest.php     | 61 ++++++++++++----
 5 files changed, 224 insertions(+), 14 deletions(-)
 create mode 100644 library/Class/Notice/ClefAlpha.php
 create mode 100644 tests/library/Class/Notice/ClefAlphaTest.php

diff --git a/.gitattributes b/.gitattributes
index fbc2ff2c944..9a712a01308 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -2254,6 +2254,7 @@ library/Class/NatureDoc.php -text
 library/Class/Newsletter.php -text
 library/Class/NewsletterSubscription.php -text
 library/Class/Notice.php -text
+library/Class/Notice/ClefAlpha.php -text
 library/Class/Notice/DublinCoreVisitor.php -text
 library/Class/NoticeHtml.php -text
 library/Class/NoticeOAI.php -text
@@ -5128,6 +5129,7 @@ tests/library/Class/MultimediaTest.php -text
 tests/library/Class/NewsletterMailingTest.php -text
 tests/library/Class/NewsletterSubscriptionTest.php -text
 tests/library/Class/NewsletterTest.php -text
+tests/library/Class/Notice/ClefAlphaTest.php -text
 tests/library/Class/Notice/DublinCoreVisitorTest.php -text
 tests/library/Class/Notice/oai_dc.xsd -text
 tests/library/Class/Notice/simpledc20021212.xsd -text
diff --git a/library/Class/Notice/ClefAlpha.php b/library/Class/Notice/ClefAlpha.php
new file mode 100644
index 00000000000..e7a4a86c2da
--- /dev/null
+++ b/library/Class/Notice/ClefAlpha.php
@@ -0,0 +1,66 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 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).
+ *
+ * AFI-OPAC 2.0 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 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_Notice_ClefAlpha {
+	const SEPARATOR = '-';
+	const POS_TITRE = 0;
+	const POS_COMPLEMENT_TITRE = 1;
+	const POS_AUTEUR = 2;
+	const POS_TOME = 3;
+	const POS_EDITEUR = 4;
+	const POS_ANNEE = 5;
+	const POS_TYPE_DOC = 6;
+
+	protected $_clef;
+
+	public function __construct($clef) {
+		$this->_clef =  explode(self::SEPARATOR, $clef);
+	}
+
+
+	public function getClefTitre() {
+		return $this->_clef[self::POS_TITRE];
+	}
+
+
+	public function getTypeDoc() {
+		return $this->_clef[self::POS_TYPE_DOC];
+	}
+
+
+	public function getTome() {
+		return $this->_clef[self::POS_TOME];
+	}
+
+
+	public function getUniqueNoticeWithSameTitleTomeTypeDoc() {
+		$notices = Class_Notice::findAllBy(['where' => 'clef_alpha like "'.$this->getClefTitre().self::SEPARATOR.'%"',
+																				'tome_alpha' => $this->getTome(),
+																				'type_doc' => $this->getTypeDoc()]);
+		if (count($notices) === 1)
+			return $notices[0];
+
+		return null;
+	}
+	
+}
+
+?>
\ No newline at end of file
diff --git a/library/Class/PanierNotice.php b/library/Class/PanierNotice.php
index fa054c1236e..e3ff97b2c41 100644
--- a/library/Class/PanierNotice.php
+++ b/library/Class/PanierNotice.php
@@ -118,6 +118,39 @@ class Class_PanierNotice extends Storm_Model_Abstract {
 	 }
 
 
+	 public function numberOfLostNotices() {
+		 return $this->numberOfNotices() - count($this->getNoticesAsArray());
+	 }
+
+
+	 public function getExistingClesNotices() {
+		 $notices = $this->getNoticesAsArray();
+		 $cles_trouvees = [];
+		 foreach($notices as $notice) 
+		   $cles_trouvees []= $notice->getClefAlpha();
+
+		 return $cles_trouvees;
+	 }
+
+
+	 public function getLostClesNotices() {
+		 return array_values(array_diff($this->getClesNotices(), 
+																		$this->getExistingClesNotices()));
+	 }
+
+
+	 public function tryToFindLostClesNotices() {
+		 $lost_cles_notices = $this->getLostClesNotices();
+		 $new_cles_notices = [];
+		 foreach($lost_cles_notices as $cle) {
+			 if ($notice = (new Class_Notice_ClefAlpha($cle))->getUniqueNoticeWithSameTitleTomeTypeDoc())
+				 $new_cles_notices []= $notice->getClefAlpha();
+		 }
+
+		 $this->setClesNotices(array_merge($this->getExistingClesNotices(), $new_cles_notices));
+	 }
+
+
 	 public function beforeSave() {
 		 $this->setDateMaj(date('Y-m-d'));
 	 }
diff --git a/tests/library/Class/Notice/ClefAlphaTest.php b/tests/library/Class/Notice/ClefAlphaTest.php
new file mode 100644
index 00000000000..793dc37a7b4
--- /dev/null
+++ b/tests/library/Class/Notice/ClefAlphaTest.php
@@ -0,0 +1,76 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 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).
+ *
+ * AFI-OPAC 2.0 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 AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ */
+
+class Notice_ClefAlphaSusAImprevuTest extends Storm_Test_ModelTestCase {
+	public function setUp() {
+		parent::setUp();
+		$this->_clef = new Class_Notice_ClefAlpha('SUSALIMPREVU--BOUCQF-2-LELOMBARD-2012-1');
+
+
+		$this->_sus_imprevu = Class_Notice::newInstanceWithId(5, 
+																													['clef_alpha' => 'SUSALIMPREVU--BOUCQF-2-LELOMBARD-2010-1']);
+
+		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice')
+		->whenCalled('findAllBy')
+		->with(['where' => 'clef_alpha like "SUSALIMPREVU-%"',
+						'tome_alpha' => 2,
+						'type_doc' => 1])
+		->answers([$this->_sus_imprevu]);
+	}
+
+
+	/** @test */
+	public function cleTitreShouldBeSUSALIMPREVU() {
+		$this->assertEquals('SUSALIMPREVU', $this->_clef->getClefTitre());
+	}
+
+
+	/** @test */
+	public function typeDocShouldBeOne() {
+		$this->assertEquals(1, $this->_clef->getTypeDoc());
+	}
+
+
+	/** @test */
+	public function getUniqueNoticeWithSameTitleTomeTypeDocShouldReturnSusALimprevu() {
+		$this->assertEquals($this->_sus_imprevu, 
+												$this->_clef->getUniqueNoticeWithSameTitleTomeTypeDoc());
+	}
+
+
+	/** @test */
+	public function withTwoNoticesWithSameTitleTomeTypeDocGetUniqueNoticeShouldAnswerNull() {
+		$_sus_imprevu2 = Class_Notice::newInstanceWithId(5, 
+																										 ['clef_alpha' => 'SUSALIMPREVU--BOUCQF-3-LELOMBARD-2010-1']);
+
+		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice')
+		->whenCalled('findAllBy')
+		->with(['where' => 'clef_alpha like "SUSALIMPREVU-%"',
+						'tome_alpha' => 2,
+						'type_doc' => 1])
+		->answers([$this->_sus_imprevu, $_sus_imprevu2]);
+
+		$this->assertEmpty($this->_clef->getUniqueNoticeWithSameTitleTomeTypeDoc());
+		
+	}
+}
+
+?>
\ No newline at end of file
diff --git a/tests/library/Class/PanierNoticeTest.php b/tests/library/Class/PanierNoticeTest.php
index ac54cd04140..4ac5fdd1a0b 100644
--- a/tests/library/Class/PanierNoticeTest.php
+++ b/tests/library/Class/PanierNoticeTest.php
@@ -70,45 +70,78 @@ class PanierNoticeWithThreeNoticesTest extends ModelTestCase {
 		$this->fictions = new Class_PanierNotice();
 		$this->fictions->updateAttributes(array('id' => 4,
 																						'libelle' => 'Fictions',
-																						'notices' => ';STARWARS;INDIANAJONES;SPIDERMAN'));
+																						'notices' => ';STARWARS;INDIANAJONES;SPIDERMAN;TEMPLEPERDU---3--1985-1'));
 
 
-		$this->star_wars = new Class_Notice();
-		$this->star_wars->setUrlVignette('http://premiere.com/star_wars.png');
+		$this->star_wars = Class_Notice::newInstanceWithId(3, 
+																											 ['url_vignette' => 'http://premiere.com/star_wars.png',
+																												'clef_alpha' => 'STARWARS']);
 
-		$this->indiana_jones = new Class_Notice();
-		$this->indiana_jones->setUrlVignette('NO');
+		$this->indiana_jones = Class_Notice::newInstanceWithId(5, 
+																													 ['url_vignette' => 'NO',
+																														'clef_alpha' => 'INDIANAJONES']);
 
-		$this->spiderman = new Class_Notice();
-		$this->spiderman->setUrlVignette('http://premiere.com/spiderman.png');
+		$this->spiderman = Class_Notice::newInstanceWithId(6,
+																											 ['url_vignette' => 'http://premiere.com/spiderman.png',
+																												'clef_alpha' => 'SPIDERMAN']);
 
 
-		$this
-			->_generateLoaderFor('Class_Notice', array('findAllBy'))
-			->expects($this->once())
-			->method('findAllBy')
-			->with(['clef_alpha' => ['STARWARS', 'INDIANAJONES', 'SPIDERMAN'],
-							'order' => 'FIELD(clef_alpha, "STARWARS","INDIANAJONES","SPIDERMAN")'])
-			->will($this->returnValue(array($this->star_wars, $this->indiana_jones, $this->spiderman)));
+		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice')
+		  ->whenCalled('findAllBy')
+			->with(['clef_alpha' => ['STARWARS', 'INDIANAJONES', 'SPIDERMAN', 'TEMPLEPERDU---3--1985-1'],
+							'order' => 'FIELD(clef_alpha, "STARWARS","INDIANAJONES","SPIDERMAN","TEMPLEPERDU---3--1985-1")'])
+			->answers([$this->star_wars, $this->indiana_jones, $this->spiderman]);
 	}
 
+
 	public function testGetAllNotices() {
 		$notices = $this->fictions->getNoticesAsArray();
 		$this->assertEquals(array($this->star_wars, $this->indiana_jones, $this->spiderman), 
 												$notices);
 	}
 
+
 	public function testGetNoticesWithVignettesTrue() {
 		$notices = $this->fictions->getNoticesOnlyVignettes(true);
 		$this->assertEquals(array($this->star_wars, $this->spiderman),
 												$notices);
 	}
 
+
 	public function testGetNoticesWithVignettesFalse() {
 		$notices = $this->fictions->getNoticesOnlyVignettes(false);
 		$this->assertEquals(array($this->star_wars, $this->indiana_jones, $this->spiderman),
 												$notices);
 	}
+
+
+	/** @test */
+	public function numberOfLostNoticesShouldBeOne() {
+		$this->assertEquals(1, $this->fictions->numberOfLostNotices());
+	}
+
+
+	/** @test */
+	public function getLostClesNoticesShouldBeTEMPLEPERDU() {
+		$this->assertEquals(['TEMPLEPERDU---3--1985-1'], $this->fictions->getLostClesNotices());
+	}
+
+
+	/** @test */
+	public function afterTryToFindLostClesNoticesShouldUpdateClesNoticeWithTEMPLERETROUVE() {
+		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice')
+		  ->whenCalled('findAllBy')
+			->with(['where' => 'clef_alpha like "TEMPLEPERDU-%"',
+							'tome_alpha' => '2',
+							'type_doc' => 1])
+		  ->answers([Class_Notice::newInstanceWithId(54, ['clef_alpha' => 'TEMPLERETROUVE'])]);
+		
+		
+
+		$this->fictions->tryToFindLostClesNotices();
+		$this->assertEquals(['STARWARS', 'INDIANAJONES', 'SPIDERMAN', 'TEMPLERETROUVE'],
+												$this->fictions->getClesNotices());
+	}
 }
 
 
-- 
GitLab