From 80cfb8d80faf3265ea0528f65d7ed0d6aed38efe Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Fri, 22 Mar 2013 09:45:28 +0000
Subject: [PATCH] =?UTF-8?q?On=20peut=20rechercher=20un=20catalogue=20via?=
 =?UTF-8?q?=20un=20chemin=20type=20/Histoire/Politique.=20Servira=20pour?=
 =?UTF-8?q?=20synchroniser=20cat=C3=A9gories=20sito/cms/...=20et=20domaine?=
 =?UTF-8?q?sOn=20peut=20rechercher=20un=20catalogue=20via=20un=20chemin=20?=
 =?UTF-8?q?type=20/Histoire/Politique?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../controllers/RechercheController.php       | 10 +--
 library/Class/Catalogue.php                   | 35 +++++++++--
 .../controllers/SitothequeControllerTest.php  |  3 +-
 tests/library/Class/CatalogueTest.php         | 62 ++++++++++++++++---
 tests/library/Class/SitothequeTest.php        |  5 +-
 5 files changed, 95 insertions(+), 20 deletions(-)

diff --git a/application/modules/telephone/controllers/RechercheController.php b/application/modules/telephone/controllers/RechercheController.php
index 2e704adb9a8..8936faeb5a0 100644
--- a/application/modules/telephone/controllers/RechercheController.php
+++ b/application/modules/telephone/controllers/RechercheController.php
@@ -40,13 +40,15 @@ class Telephone_RechercheController extends RechercheController {
 		$this->_('Notices liées') => ['action' => 'frbr']
     ];
 
-    if ($notice->isLivreNumerique())
+    if ($notice->isLivreNumerique()) {
       $actions[$this->_('Feuilleter le livre')] = ['action' => 'ressourcesnumeriques',
-						   'attribs' => ['data-ajax' => 'false']];
+																									 'attribs' => ['data-ajax' => 'false']];
+		}
 
-    if ($notice->isArteVOD())
+    if ($notice->isArteVOD()) {
       $actions[$this->_('Bande-annonce')] = ['action' => 'ressourcesnumeriques',
-					     'attribs' => ['data-ajax' => 'false']];
+																						 'attribs' => ['data-ajax' => 'false']];
+		}
 
     $this->view->notice = $notice;
     $this->view->actions = $actions;
diff --git a/library/Class/Catalogue.php b/library/Class/Catalogue.php
index 11547d797fc..9a38336dc38 100644
--- a/library/Class/Catalogue.php
+++ b/library/Class/Catalogue.php
@@ -187,6 +187,7 @@ class CatalogueLoader extends Storm_Model_Loader {
 		return implode(' and ', $clauses);
 	}
 
+
 	public function allByIdLabel() {
 		$catalogues = Class_Catalogue::findAllBy(['order' => 'libelle']);
 		$allids=[];
@@ -196,15 +197,26 @@ class CatalogueLoader extends Storm_Model_Loader {
 		return $allids;
 	}
 
+
 	public function findTopCatalogues() {
 		return Class_Catalogue::findAllBy(['where' => 'parent_id is null',
 																			 'order' => 'libelle']);
 	}
 
+
+	public function findByPath($path) {
+		$parts = array_filter(explode(Class_Catalogue::PATH_SEPARATOR, $path));
+		if (!$root = Class_Catalogue::findFirstBy(['parent_id' => null,
+																							 'libelle' => array_shift($parts)]))
+			return null;
+		return $root->sousDomaineByPath(implode(Class_Catalogue::PATH_SEPARATOR, $parts));
+	}
 }
 
 
 class Class_Catalogue extends Storm_Model_Abstract {
+	const PATH_SEPARATOR = '/';
+	
 	protected 
 		$_table_name = 'catalogue',
 		$_table_primary = 'ID_CATALOGUE',
@@ -249,15 +261,30 @@ class Class_Catalogue extends Storm_Model_Abstract {
 		$_until;
 
 
-	public static function getLoader() {
-		return self::getLoaderFor(__CLASS__);
-	}
-
 	public static function newCatalogueForAll() {
 		return new AllNoticesCatalogue();
 	}
 
 
+	public function sousDomaineByPath($path) {
+		if (!$parts = array_filter(explode(static::PATH_SEPARATOR, $path)))
+			return $this;
+
+		if ($child = static::getLoader()->findFirstBy(['parent_id' => $this->getId(),
+																									 'libelle' => array_shift($parts)]))
+			return $child->sousDomaineByPath(implode(static::PATH_SEPARATOR, $parts));
+
+		return null;
+	}
+
+
+	public function getPath() {
+		return (($parent = $this->getDomaineParent()) 
+						? $parent->getPath() 
+						: '').static::PATH_SEPARATOR.$this->getLibelle();
+	}
+
+
 	public function acceptVisitor($visitor) {
 		$visitor->visitOaiSpec($this->getOaiSpec());
 		$visitor->visitLibelle($this->getLibelle());
diff --git a/tests/application/modules/admin/controllers/SitothequeControllerTest.php b/tests/application/modules/admin/controllers/SitothequeControllerTest.php
index d3d5efa663b..2c34665b696 100644
--- a/tests/application/modules/admin/controllers/SitothequeControllerTest.php
+++ b/tests/application/modules/admin/controllers/SitothequeControllerTest.php
@@ -204,7 +204,6 @@ class SitothequeControllerAddActionTest extends SitothequeControllerTestCase {
 	public function titreShouldBeAjouterUnSite() {
 		$this->assertXPathContentContains('//h1', 'Ajouter un site');
 	}
-
 }
 
 
@@ -218,7 +217,7 @@ class SitothequeControllerPostAddActionTest extends SitothequeControllerTestCase
 		$this->postDispatch('/admin/sito/sitoadd/id_cat/2', 
 												['titre' => 'google',
 												 'url' => 'http://www.google.fr',
-													'id_items' => '23-23'], 
+												 'id_items' => '23-23'], 
 												true);
 	}
 
diff --git a/tests/library/Class/CatalogueTest.php b/tests/library/Class/CatalogueTest.php
index 760d09a7567..a9878429663 100644
--- a/tests/library/Class/CatalogueTest.php
+++ b/tests/library/Class/CatalogueTest.php
@@ -132,6 +132,8 @@ class CatalogueTestGetSelectionFacette extends ModelTestCase {
 }
 
 
+
+
 class CatalogueTestGetPagedNotices extends ModelTestCase {
 	protected $_catalogue;
 	protected $_noticeWrapper;
@@ -515,20 +517,32 @@ class CatalogueTestGetNoticesByPreferences extends ModelTestCase {
 class CatalogueParentTest extends Storm_Test_ModelTestCase {
 	public function setUp() {
 		parent::setUp();
-		$this->_histoire = Class_Catalogue::newInstanceWithId(100, [ 'libelle' => 'Histoire']);	
+		$this->_histoire = Class_Catalogue::newInstanceWithId(100, [ 'libelle' => 'Histoire',
+																																 'parent_id' => 0]);	
 		$this->_politique = Class_Catalogue::newInstanceWithId(200, [ 'libelle' => 'Politique',
 																																'parent_id' => 100]);	
 		$this->_moyenage = Class_Catalogue::newInstanceWithId(300, [ 'libelle'=>'Moyen-age',
 																															 'parent_id' => 100]);
 		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Catalogue')
-		->whenCalled('findAllBy')
-		->with(['role' => 'domaine_parent',
-						'model' => $this->_histoire,
-						'order' => 'libelle'
-		])
-		->answers([ $this->_politique, $this->_moyenage]);
+			->whenCalled('findAllBy')
+			->with(['role' => 'domaine_parent',
+							'model' => $this->_histoire,
+							'order' => 'libelle'
+							 ])
+			->answers([ $this->_politique, $this->_moyenage])
 
-		
+			->whenCalled('findFirstBy')
+			->answers(null)
+
+			->whenCalled('findFirstBy')
+			->with(['parent_id' => null,
+							'libelle' => 'Histoire'])
+			->answers($this->_histoire)
+
+			->whenCalled('findFirstBy')
+			->with(['parent_id' => 100,
+							'libelle' => 'Politique'])
+			->answers($this->_politique);
 	}
 
 
@@ -549,6 +563,38 @@ class CatalogueParentTest extends Storm_Test_ModelTestCase {
 		$this->_politique->setParentId($this->_politique->getId());
 		$this->assertNull($this->_politique->getParentId());
 	}
+
+
+	/** @test */
+	public function findByPathHistoireShouldAnswerCatalogueHistoire() {
+		$this->assertEquals($this->_histoire,
+												Class_Catalogue::findByPath('/Histoire'));
+	}
+
+
+	/** @test */
+	public function findByPathHistoireSlashPolitiqueShouldAnswerCataloguePolitique() {
+		$this->assertEquals($this->_politique,
+												Class_Catalogue::findByPath('/Histoire/Politique'));
+	}
+
+
+	/** @test */
+	public function findByPathHistoireSlashZorkShouldAnswerNull() {
+		$this->assertNull(Class_Catalogue::findByPath('/Histoire/Zork'));
+	}
+
+
+	/** @test */
+	public function findByPathZorkShouldAnswerNull() {
+		$this->assertNull(Class_Catalogue::findByPath('/Zork'));
+	}
+
+
+	/** @test */
+	public function cataloguePolitiquePathShouldBeSlashHistoireSlashPolitique() {
+		$this->assertEquals('/Histoire/Politique', $this->_politique->getPath());
+	}
 }
 
 
diff --git a/tests/library/Class/SitothequeTest.php b/tests/library/Class/SitothequeTest.php
index ac2626c341f..c43d276502e 100644
--- a/tests/library/Class/SitothequeTest.php
+++ b/tests/library/Class/SitothequeTest.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 SitothequeTest extends Storm_Test_ModelTestCase {
+class SitothequeSitesFromIdsAndCategoriesTest extends Storm_Test_ModelTestCase {
 	/** @test */
 	function getSitesFromIdAndCategoriesShouldReturnFeeds() {
 		$site_premiere = Class_Sitotheque::getLoader()->newInstanceWithId(26);
@@ -35,6 +35,7 @@ class SitothequeTest extends Storm_Test_ModelTestCase {
 		$this->assertEquals(array($site_premiere, $site_allocine, $site_telerama),
 												$sites);
 	}
-
 }
+
+
 ?>
\ No newline at end of file
-- 
GitLab