diff --git a/library/Class/Catalogue.php b/library/Class/Catalogue.php index 7b5bc7068bb3945a7e7c03718a28b7e8f260fe04..0ac6161e7dfb9cdacd70d11801f5c50fc2daf611 100644 --- a/library/Class/Catalogue.php +++ b/library/Class/Catalogue.php @@ -22,7 +22,7 @@ // OPAC3 - Catalogues de notices //////////////////////////////////////////////////////////////////////////////// class CatalogueLoader extends Storm_Model_Loader { - const DEFAULT_ITEMS_BY_PAGE = 1000; + const DEFAULT_ITEMS_BY_PAGE = 100; public function loadNoticesFor($catalogue, $itemsByPage = self::DEFAULT_ITEMS_BY_PAGE, $page = 1) { if (null == $catalogue) @@ -38,6 +38,9 @@ class CatalogueLoader extends Storm_Model_Loader { public function clausesFor($catalogue) { + if (1 == $catalogue->getAll()) + return '1=1'; + $conditions = array(); if ($facets = $this->facetsClauseFor($catalogue)) $conditions[] = $facets; @@ -174,13 +177,20 @@ class Class_Catalogue extends Storm_Model_Abstract 'annee_fin' => '', 'cote_debut' => '', 'cote_fin' => '', - 'nouveaute' => ''); + 'nouveaute' => '', + 'all' => ''); public static function getLoader() { return self::getLoaderFor(__CLASS__); } + public static function newCatalogueForAll() { + $instance = new self(); + return $instance->setAll(1); + } + + public function acceptVisitor($visitor) { $visitor->visitOaiSpec($this->getOaiSpec()); $visitor->visitLibelle($this->getLibelle()); diff --git a/library/Class/Notice.php b/library/Class/Notice.php index 5783316526d86329a09f6fd86f4444e333e6419d..3ff63f9c98c3516424ac0ae0c10142c4fac43733 100644 --- a/library/Class/Notice.php +++ b/library/Class/Notice.php @@ -52,6 +52,10 @@ class NoticeLoader extends Storm_Model_Loader return $result[0]; } + + public function findAllByCatalogue($catalogue) { + return Class_Catalogue::getLoader()->loadNoticesFor($catalogue); + } } class Class_Notice extends Storm_Model_Abstract diff --git a/library/Class/WebService/OAI/Response/ListIdentifiers.php b/library/Class/WebService/OAI/Response/ListIdentifiers.php index a7a1e1c4ec3cd9f5e8669e36d56f0c8fb5ea8329..b27b5caee2b0c83243a01751f927119ced380cdd 100644 --- a/library/Class/WebService/OAI/Response/ListIdentifiers.php +++ b/library/Class/WebService/OAI/Response/ListIdentifiers.php @@ -19,23 +19,74 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ class Class_WebService_OAI_Response_ListIdentifiers extends Class_WebService_OAI_Response_Null { - protected $_notices = array(); + protected $_metadataPrefix; + protected $_catalogue; + protected $_set; + protected $_from; + protected $_until; + protected $_resumptionToken; + protected $_notices; + + + public function xml($params = array()) { + $this->_params = array_merge(array('metadataPrefix' => null, + 'set' => null, + 'from' => null, + 'until' => null, + 'resumptionToken' => null), + $params); + + $this->_metadataPrefix = $this->_params['metadataPrefix']; + $this->_set = $this->_params['set']; + $this->_from = $this->_params['from']; + $this->_until = $this->_params['until']; + $this->_resumptionToken = $this->_params['resumptionToken']; + + $this->_catalogue = $this->getCatalogueFromSetSpec($this->_set); + + return parent::xml(); + } + public function buildXmlOn($builder) { - $response = $builder->request(array('verb' => 'ListIdentifiers'), - $this->_baseUrl); + $requestOptions = array('verb' => 'ListIdentifiers'); + if (null !== $this->_set) + $requestOptions['set'] = $this->_set; + if (null !== $this->_from) + $requestOptions['from'] = $this->_from; + if (null !== $this->_until) + $requestOptions['until'] = $this->_until; + if (null !== $this->_resumptionToken) + $requestOptions['resumptionToken'] = $this->_resumptionToken; + if (null !== $this->_metadataPrefix) + $requestOptions['metadataPrefix'] = $this->_metadataPrefix; + + $response = $builder->request($requestOptions, $this->_baseUrl); if ($errors = $this->buildErrorsOn($builder)) return $response . $errors; - return $response . $this->listIdentifiers($builder); + return $response . $this->listIdentifiers($builder); } public function buildErrorsOn($builder) { - if (!isset($this->_params['metadataPrefix'])) + if (!$this->_metadataPrefix) return $builder->error(array('code' => 'badArgument'), 'Missing metadataPrefix'); + if ('oai_dc' != $this->_metadataPrefix) + return $builder->error(array('code' => 'cannotDisseminateFormat')); + + if ($this->_set && !$this->_catalogue) + return $builder->error(array('code' => 'badArgument'), 'Set not found'); + + if ($this->_resumptionToken + && !($token = Class_WebService_OAI_ResumptionToken::find($this->_resumptionToken))) + return $builder->error(array('code' => 'badResumptionToken')); + + $this->_notices = Class_Notice::getLoader()->findAllByCatalogue($this->_catalogue); + if (0 == count($this->_notices)) + return $builder->error(array('code' => 'noRecordsMatch')); } @@ -57,8 +108,10 @@ class Class_WebService_OAI_Response_ListIdentifiers extends Class_WebService_OAI } - public function setNotices($notices) { - $this->_notices = $notices; + public function getCatalogueFromSetSpec($setSpec) { + if (null == $setSpec) + return Class_Catalogue::newCatalogueForAll(); + return current(Class_Catalogue::getLoader()->findAllBy(array('oai_spec' => $setSpec))); } } diff --git a/tests/library/Class/CatalogueTest.php b/tests/library/Class/CatalogueTest.php index 052cc0a676e5c667702f6c79af88c99e46d9e438..752b62631b02921f1347ad8c7aba212eeaa64a4b 100644 --- a/tests/library/Class/CatalogueTest.php +++ b/tests/library/Class/CatalogueTest.php @@ -254,6 +254,13 @@ class CatalogueTestGetPagedNotices extends ModelTestCase { Class_Catalogue::getLoader()->loadNoticesFor($this->_catalogue, 5, 3); } + + /** @test */ + public function withCatalogForAllShouldQueryOneEgalOne() { + $this->_expectNoticeFindAllBy('1=1'); + Class_Catalogue::getLoader()->loadNoticesFor(Class_Catalogue::newCatalogueForAll()); + } + protected function _prepareAndLoadForFacet($lambda, $facet) { $lambda($this->_catalogue); diff --git a/tests/library/Class/WebService/OAI/Response/ListIdentifiersTest.php b/tests/library/Class/WebService/OAI/Response/ListIdentifiersTest.php index 52cf9e6b05b62ab30cfbb20a460804890a13a9be..89416e5ea71501b4b37cc2433f3036bf12d9d7d4 100644 --- a/tests/library/Class/WebService/OAI/Response/ListIdentifiersTest.php +++ b/tests/library/Class/WebService/OAI/Response/ListIdentifiersTest.php @@ -26,18 +26,20 @@ class ListIdentifiersValidTest extends Storm_Test_ModelTestCase { parent::setUp(); $this->_xpath = TestXPathFactory::newOai(); $this->_response = new Class_WebService_OAI_Response_ListIdentifiers('http://moulins.fr/oai2/do'); - $this->_response->setNotices(array(Class_Notice::getLoader() - ->newInstanceWithId(2) - ->setClefAlpha('harrypotter-sorciers') - ->setDateMaj('2001-12-14 11:42:42'), - Class_Notice::getLoader() - ->newInstanceWithId(3) - ->setClefAlpha('harrypotter-chambresecrets') - ->setDateMaj('2005-10-24 11:42:42'), - Class_Notice::getLoader() - ->newInstanceWithId(4) - ->setClefAlpha('harrypotter-azkaban') - ->setDateMaj('2012-04-03 11:42:42'))); + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Catalogue') + ->whenCalled('loadNoticesFor') + ->answers(array(Class_Notice::getLoader() + ->newInstanceWithId(2) + ->setClefAlpha('harrypotter-sorciers') + ->setDateMaj('2001-12-14 11:42:42'), + Class_Notice::getLoader() + ->newInstanceWithId(3) + ->setClefAlpha('harrypotter-chambresecrets') + ->setDateMaj('2005-10-24 11:42:42'), + Class_Notice::getLoader() + ->newInstanceWithId(4) + ->setClefAlpha('harrypotter-azkaban') + ->setDateMaj('2012-04-03 11:42:42'))); $this->_xml = $this->_response->xml(array('metadataPrefix' => 'oai_dc')); } @@ -122,7 +124,7 @@ class ListIdentifiersValidTest extends Storm_Test_ModelTestCase { } -class ListIdentifiersWithoutMetadataPrefixTest extends Storm_Test_ModelTestCase { +class ListIdentifiersInvalidParamsTest extends Storm_Test_ModelTestCase { protected $_xpath; protected $_response; @@ -130,13 +132,57 @@ class ListIdentifiersWithoutMetadataPrefixTest extends Storm_Test_ModelTestCase parent::setUp(); $this->_xpath = TestXPathFactory::newOai(); $this->_response = new Class_WebService_OAI_Response_ListIdentifiers('http://moulins.fr/oai2/do'); - $this->_xml = $this->_response->xml(array()); + } + + + public function tearDown() { + Class_WebService_OAI_ResumptionToken::defaultCache(null); + } + + + /** @test */ + public function withoutMetadataPrefixErrorCodeShouldBeBadArgument() { + $this->_xpath->assertXPath($this->_response->xml(array()), '//oai:error[@code="badArgument"]'); + } + + + /** @test */ + public function withUnknownFormatErrorCodeShouldBeCannotDisseminateFormat() { + $this->_xpath->assertXpath($this->_response->xml(array('metadataPrefix' => 'zork')), + '//oai:error[@code="cannotDisseminateFormat"]'); + } + + + /** @test */ + public function withUnknownSetErrorCodeShouldBeBadArgument() { + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Catalogue') + ->whenCalled('findAllBy') + ->answers(array()); + $this->_xpath->assertXPath($this->_response->xml(array('metadataPrefix' => 'oai_dc', + 'set' => 'jeunesse:bd')), + '//oai:error[@code="badArgument"]'); + } + + + /** @test */ + public function withoutNoticesErrorCodeShouldBeNoRecordsMatch() { + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice') + ->whenCalled('findAllBy') + ->answers(array()); + $this->_xpath->assertXPath($this->_response->xml(array('metadataPrefix' => 'oai_dc')), + '//oai:error[@code="noRecordsMatch"]'); } /** @test */ - public function errorCodeShouldBeBadArgument() { - $this->_xpath->assertXPath($this->_xml, '//oai:error[@code="badArgument"]'); + public function withUnknownResumptionTokenErrorCodeShouldBeBadResumptionToken() { + $cache = Storm_Test_ObjectWrapper::mock() + ->whenCalled('load') + ->answers(false); + Class_WebService_OAI_ResumptionToken::defaultCache($cache); + $this->_xpath->assertXPath($this->_response->xml(array('metadataPrefix' => 'oai_dc', + 'resumptionToken' => 'Zork')), + '//oai:error[@code="badResumptionToken"]'); } } ?> \ No newline at end of file