From 3d1f780612136f6181ebd064405d31d11da73f02 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@git-test.afi-sa.fr>
Date: Fri, 4 May 2012 08:44:10 +0000
Subject: [PATCH] =?UTF-8?q?OAI:=20Refacto=20ListSets=20est=20trait=C3=A9?=
 =?UTF-8?q?=20via=20MVC=20+=20apparition=20request=20ListSets?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitattributes                                |  4 +-
 .../opac/controllers/OaiController.php        | 25 ++++++++++-
 .../scripts/oai/list-identifiers.xml.phtml    | 24 +---------
 .../views/scripts/oai/list-sets.xml.phtml     | 12 +++++
 .../OAI/Request/ListIdentifiers.php           |  1 -
 .../Class/WebService/OAI/Request/ListSets.php | 44 +++++++++++++++++++
 .../OAIControllerListSetsTest.php}            | 20 ++++-----
 7 files changed, 93 insertions(+), 37 deletions(-)
 create mode 100644 application/modules/opac/views/scripts/oai/list-sets.xml.phtml
 create mode 100644 library/Class/WebService/OAI/Request/ListSets.php
 rename tests/{library/Class/WebService/OAI/Response/ListSetsTest.php => application/modules/opac/controllers/OAIControllerListSetsTest.php} (81%)

diff --git a/.gitattributes b/.gitattributes
index 534f76daf50..ead39383d32 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -586,6 +586,7 @@ application/modules/opac/views/scripts/oai/identify.xml.phtml -text svneol=unset
 application/modules/opac/views/scripts/oai/list-identifiers.xml.phtml -text
 application/modules/opac/views/scripts/oai/list-metadata-formats.xml.phtml -text svneol=unset#application/xml
 application/modules/opac/views/scripts/oai/list-records.xml.phtml -text svneol=unset#application/xml
+application/modules/opac/views/scripts/oai/list-sets.xml.phtml -text svneol=unset#application/xml
 application/modules/opac/views/scripts/panier/error.phtml -text
 application/modules/opac/views/scripts/panier/export.phtml -text
 application/modules/opac/views/scripts/panier/index.phtml -text
@@ -1776,6 +1777,7 @@ library/Class/WebService/OAI/Request/Identify.php -text
 library/Class/WebService/OAI/Request/ListIdentifiers.php -text
 library/Class/WebService/OAI/Request/ListMetadataFormats.php -text
 library/Class/WebService/OAI/Request/ListRecords.php -text
+library/Class/WebService/OAI/Request/ListSets.php -text
 library/Class/WebService/OAI/Response/GetRecord.php -text
 library/Class/WebService/OAI/Response/Identify.php -text
 library/Class/WebService/OAI/Response/ListIdentifiers.php -text
@@ -3443,6 +3445,7 @@ tests/application/modules/opac/controllers/OAIControllerIdentifyTest.php -text
 tests/application/modules/opac/controllers/OAIControllerListIdentifiersTest.php -text
 tests/application/modules/opac/controllers/OAIControllerListMetadataFormatsTest.php -text
 tests/application/modules/opac/controllers/OAIControllerListRecordsTest.php -text
+tests/application/modules/opac/controllers/OAIControllerListSetsTest.php -text
 tests/application/modules/opac/controllers/OAIControllerTest.php -text
 tests/application/modules/opac/controllers/PanierControllerTest.php -text
 tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php -text
@@ -3520,7 +3523,6 @@ tests/library/Class/UsersTest.php -text
 tests/library/Class/VodeclicLinkTest.php -text
 tests/library/Class/WebService/BabelioTest.php -text
 tests/library/Class/WebService/OAI/Response/GetRecordTest.php -text
-tests/library/Class/WebService/OAI/Response/ListSetsTest.php -text
 tests/library/Class/WebService/OAI/ResumptionTokenTest.php -text
 tests/library/Class/WebService/OAIIdentify.xml -text
 tests/library/Class/WebService/OAIListIdentifiers.xml -text
diff --git a/application/modules/opac/controllers/OaiController.php b/application/modules/opac/controllers/OaiController.php
index a4708bf0213..60022f584f6 100644
--- a/application/modules/opac/controllers/OaiController.php
+++ b/application/modules/opac/controllers/OaiController.php
@@ -25,6 +25,7 @@ class OaiController extends Zend_Controller_Action {
 			->addActionContext('identify', 'xml')
 			->addActionContext('list-metadata-formats', 'xml')
 			->addActionContext('list-records', 'xml')
+			->addActionContext('list-sets', 'xml')
 			->initContext();
 	}
 
@@ -36,7 +37,8 @@ class OaiController extends Zend_Controller_Action {
 		$verbsMapping = array('ListIdentifiers' => 'list-identifiers',
 													'Identify' => 'identify',
 													'ListMetadataFormats' => 'list-metadata-formats',
-													'ListRecords' => 'list-records');
+													'ListRecords' => 'list-records',
+													'ListSets' => 'list-sets');
 
 		if (array_key_exists($this->_getParam('verb'), $verbsMapping)) {
 			$this->_forward($verbsMapping[$this->_getParam('verb')], null, null, 
@@ -126,6 +128,27 @@ class OaiController extends Zend_Controller_Action {
 		$this->view->request = $request;
 		$this->view->builder = new Class_Xml_Builder();
 	}
+
+
+	public function listSetsAction() {
+		$this->getHelper('ViewRenderer')->setLayoutScript('empty.phtml');
+		$baseUrl = $this->buildBaseUrl();
+		$request = new Class_WebService_OAI_Request_ListSets($this->_request->getParams(), 
+																												 $baseUrl);
+		$this->view->request = $request;
+		$builder = new Class_Xml_Builder();
+
+		if ($catalogs = $request->getCatalogs()) {
+			$visitor = new Class_WebService_OAI_CatalogueVisitor($builder);
+			$sets = '';
+			foreach ($catalogs as $catalog) {
+				$visitor->visitCatalogue($catalog);
+				$sets .= $builder->set($visitor->xml());
+			}
+			$this->view->sets = $sets;
+		}
+		$this->view->builder = $builder;
+	}
 }
 
 ?>
\ No newline at end of file
diff --git a/application/modules/opac/views/scripts/oai/list-identifiers.xml.phtml b/application/modules/opac/views/scripts/oai/list-identifiers.xml.phtml
index 1ed2fa75e78..ca640de2128 100644
--- a/application/modules/opac/views/scripts/oai/list-identifiers.xml.phtml
+++ b/application/modules/opac/views/scripts/oai/list-identifiers.xml.phtml
@@ -1,26 +1,4 @@
-<?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 
- */
-
-echo '<?xml version="1.0" ?>'
-?>
+<?php echo '<?xml version="1.0" ?>';?>
 <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"
 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
diff --git a/application/modules/opac/views/scripts/oai/list-sets.xml.phtml b/application/modules/opac/views/scripts/oai/list-sets.xml.phtml
new file mode 100644
index 00000000000..ea0aa807505
--- /dev/null
+++ b/application/modules/opac/views/scripts/oai/list-sets.xml.phtml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"
+				 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+				 xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
+	<responseDate><?php echo date('c');?></responseDate>
+	<?php echo $this->request->renderOn($this->builder); ?>
+	<?php if ($this->sets) { ?>
+	<ListSets>
+		<?php echo $this->sets;?>
+	</ListSets>
+	<?php } ?>
+</OAI-PMH>
\ No newline at end of file
diff --git a/library/Class/WebService/OAI/Request/ListIdentifiers.php b/library/Class/WebService/OAI/Request/ListIdentifiers.php
index ed7fad70466..bd9a15a9223 100644
--- a/library/Class/WebService/OAI/Request/ListIdentifiers.php
+++ b/library/Class/WebService/OAI/Request/ListIdentifiers.php
@@ -71,7 +71,6 @@ class Class_WebService_OAI_Request_ListIdentifiers {
 			return $builder->error(array('code' => 'noRecordsMatch'));
 
 		$token = null;
-		xdebug_break();
 		if ($this->_resumptionToken 
 				&& !($token = Class_WebService_OAI_ResumptionToken::find($this->_resumptionToken)))
 			return $builder->error(array('code' => 'badResumptionToken'));
diff --git a/library/Class/WebService/OAI/Request/ListSets.php b/library/Class/WebService/OAI/Request/ListSets.php
new file mode 100644
index 00000000000..e6ac9d35760
--- /dev/null
+++ b/library/Class/WebService/OAI/Request/ListSets.php
@@ -0,0 +1,44 @@
+<?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_WebService_OAI_Request_ListSets {
+	const SETS_BY_PAGE = 100;
+	protected $_baseUrl;
+	protected $_catalogs;
+
+	public function __construct($params, $baseUrl) {
+		$this->_baseUrl = $baseUrl;
+		$this->_catalogs = Class_Catalogue::getLoader()->findAllBy(array('where' => 'oai_spec is not null',
+																																		 'where' => 'oai_spec !=\'\'',
+																																		 'order' => 'oai_spec'));
+	}
+
+
+	public function getCatalogs() {
+		return $this->_catalogs;
+	}
+
+
+	public function renderOn($builder) {
+		$attributes = array('verb' => 'ListSets');
+		return $builder->request($attributes, $this->_baseUrl);
+	}
+}
+?>
\ No newline at end of file
diff --git a/tests/library/Class/WebService/OAI/Response/ListSetsTest.php b/tests/application/modules/opac/controllers/OAIControllerListSetsTest.php
similarity index 81%
rename from tests/library/Class/WebService/OAI/Response/ListSetsTest.php
rename to tests/application/modules/opac/controllers/OAIControllerListSetsTest.php
index 00f4cc5f904..ecfbbf8109f 100644
--- a/tests/library/Class/WebService/OAI/Response/ListSetsTest.php
+++ b/tests/application/modules/opac/controllers/OAIControllerListSetsTest.php
@@ -18,9 +18,9 @@
  * 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 
  */
+require_once 'AbstractControllerTestCase.php';
 
-class ListSetsTest extends Storm_Test_ModelTestCase {
-	protected $_response;
+class OAIControllerListSetsTest extends AbstractControllerTestCase {
 	protected $_xpath;
 
 	public function setUp() {
@@ -43,14 +43,13 @@ class ListSetsTest extends Storm_Test_ModelTestCase {
 									 'order' => 'oai_spec'))
 			->answers(array($catalogue_bd, $catalogue_musique));
 
-
-		$this->_response = new Class_WebService_OAI_Response_ListSets('http://afi.fr/oai');
+		$this->dispatch('/opac/oai/request?verb=ListSets');
 	}
 
 
 	/** @test */
 	public function setMusiqueShouldContainSpecMusic() {
-		$this->_xpath->assertXpathContentContains($this->_response->xml(),
+		$this->_xpath->assertXpathContentContains($this->_response->getBody(),
 																							'//oai:ListSets/oai:set/oai:setSpec',
 																							'music');
 	}
@@ -58,7 +57,7 @@ class ListSetsTest extends Storm_Test_ModelTestCase {
 
 	/** @test */
 	public function setMusiqueShouldContainNameMusique() {
-		$this->_xpath->assertXpathContentContains($this->_response->xml(),
+		$this->_xpath->assertXpathContentContains($this->_response->getBody(),
 																							'//oai:ListSets/oai:set/oai:setName',
 																							'Musique');
 	}
@@ -66,7 +65,7 @@ class ListSetsTest extends Storm_Test_ModelTestCase {
 
   /** @test */
 	public function setMusiqueShouldContainDescriptionLaMusiqueQuiBalance() {
-		$this->_xpath->assertXpathContentContains($this->_response->xml(),
+		$this->_xpath->assertXpathContentContains($this->_response->getBody(),
 																							'//oai:ListSets/oai:set/oai:setDescription',
 																							'La musique qui balance');
 	}
@@ -75,7 +74,7 @@ class ListSetsTest extends Storm_Test_ModelTestCase {
 
 	/** @test */
 	public function setBdsShouldContainsSpecLivresColonBds() {
-		$this->_xpath->assertXpathContentContains($this->_response->xml(),
+		$this->_xpath->assertXpathContentContains($this->_response->getBody(),
 																							'//oai:ListSets/oai:set/oai:setSpec',
 																							'livres:bd');
 	}
@@ -83,7 +82,7 @@ class ListSetsTest extends Storm_Test_ModelTestCase {
 
 	/** @test */
 	public function setBdsShouldContainsNameBds() {
-		$this->_xpath->assertXpathContentContains($this->_response->xml(),
+		$this->_xpath->assertXpathContentContains($this->_response->getBody(),
 																							'//oai:ListSets/oai:set/oai:setName',
 																							'BDs'); 		
 	}
@@ -91,9 +90,8 @@ class ListSetsTest extends Storm_Test_ModelTestCase {
 
   /** @test */
 	public function setBdsShouldNotContainDescription() {
-		$this->_xpath->assertNotXpath($this->_response->xml(),
+		$this->_xpath->assertNotXpath($this->_response->getBody(),
 																	'//oai:ListSets/oai:set/oai:setSpec[text()="livres:bd"][following-sibling::oai:setDescription]');
 	}
-
 }
 ?>
\ No newline at end of file
-- 
GitLab