diff --git a/library/Class/WebService/OAI/Response/GetRecord.php b/library/Class/WebService/OAI/Response/GetRecord.php index 04f7463790ff54b8cbb3bbecdc741ff5ddcdf0b8..abc76d7ce2153a1e95205738029206f2c4e9bd06 100644 --- a/library/Class/WebService/OAI/Response/GetRecord.php +++ b/library/Class/WebService/OAI/Response/GetRecord.php @@ -22,44 +22,63 @@ class Class_WebService_OAI_Response_GetRecord extends Class_WebService_OAI_Respo protected $_notice; protected $_identifier; protected $_metadataPrefix; + protected $_params; public function xml($params = array()) { - if (array_key_exists('identifier', $params)) - $this->_identifier = $params['identifier']; + $this->_params = array_merge(array('identifier' => null, + 'metadataPrefix' => null), + $params); - if (array_key_exists('metadataPrefix', $params)) - $this->_metadataPrefix = $params['metadataPrefix']; - - if (null != $this->_identifier) { - $parts = explode('/', $this->_identifier); - if (null !== ($notice = Class_Notice::getLoader()->getNoticeByClefAlpha(end($parts)))) - $this->_notice = $notice; - } + $this->_identifier = $this->_params['identifier']; + $this->_metadataPrefix = $this->_params['metadataPrefix']; + $this->_notice = $this->getNoticeFromIdentifier($this->_identifier); + return parent::xml(); } - public function buildXmlOn($builder) { - $response = ''; + public function requestTagOn($builder) { $requestOptions = array('verb' => 'GetRecord'); if (null !== $this->_metadataPrefix) $requestOptions['metadataPrefix'] = $this->_metadataPrefix; if (null !== $this->_identifier) $requestOptions['identifier'] = $this->_identifier; - $response .= $builder->request($requestOptions, $this->_baseUrl); + return $builder->request($requestOptions, $this->_baseUrl); + } + + public function buildErrorsOn($builder) { if (null == $this->_identifier) - return $response . $builder->error(array('code' => 'badArgument'), 'Missing identifier'); + return $builder->error(array('code' => 'badArgument'), 'Missing identifier'); if (null == $this->_metadataPrefix) - return $response . $builder->error(array('code' => 'badArgument'), 'Missing metadataPrefix'); + return $builder->error(array('code' => 'badArgument'), 'Missing metadataPrefix'); if (null == $this->_notice) - return $response . $builder->error(array('code' => 'idDoesNotExist')); + return $builder->error(array('code' => 'idDoesNotExist')); if ('oai_dc' != $this->_metadataPrefix) - return $response . $builder->error(array('code' => 'cannotDisseminateFormat')); + return $builder->error(array('code' => 'cannotDisseminateFormat')); + + return ''; + } + + + public function getNoticeFromIdentifier($identifier) { + if (null != $this->_identifier) { + $parts = explode('/', $this->_identifier); + return Class_Notice::getLoader()->getNoticeByClefAlpha(end($parts)); + } + return null; + } + + + public function buildXmlOn($builder) { + $response = $this->requestTagOn($builder); + + if ($errors = $this->buildErrorsOn($builder)) + return $response . $errors; $visitor = new Class_Notice_DublinCoreVisitor(); $visitor->visit($this->_notice); diff --git a/library/Class/WebService/OAI/Response/ListIdentifiers.php b/library/Class/WebService/OAI/Response/ListIdentifiers.php index fc09344a3c4cd2518097a0d3a9b9df8ef215ff75..a7a1e1c4ec3cd9f5e8669e36d56f0c8fb5ea8329 100644 --- a/library/Class/WebService/OAI/Response/ListIdentifiers.php +++ b/library/Class/WebService/OAI/Response/ListIdentifiers.php @@ -19,13 +19,23 @@ * 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; + protected $_notices = array(); public function buildXmlOn($builder) { - return - $builder->request(array('verb' => 'ListIdentifiers'), - $this->_baseUrl) - . $this->listIdentifiers($builder); + $response = $builder->request(array('verb' => 'ListIdentifiers'), + $this->_baseUrl); + + if ($errors = $this->buildErrorsOn($builder)) + return $response . $errors; + + return $response . $this->listIdentifiers($builder); + } + + + public function buildErrorsOn($builder) { + if (!isset($this->_params['metadataPrefix'])) + return $builder->error(array('code' => 'badArgument'), 'Missing metadataPrefix'); + } diff --git a/library/Class/WebService/OAI/Response/Null.php b/library/Class/WebService/OAI/Response/Null.php index d35df11cfdd64b1863a41bbc90eb5efcea3a5b3f..d186f4db24510ceea39a805a2fd64933014a472e 100644 --- a/library/Class/WebService/OAI/Response/Null.php +++ b/library/Class/WebService/OAI/Response/Null.php @@ -22,13 +22,15 @@ class Class_WebService_OAI_Response_Null { const PROLOG = '<?xml version="1.0" encoding="UTF-8"?>'; protected $_baseUrl; protected $_protocolVersion = '2.0'; + protected $_params; public function __construct($baseUrl) { $this->_baseUrl = $baseUrl; } - public function xml() { + public function xml($params = array()) { + $this->_params = $params; $builder = new Class_Xml_Builder(); return self::PROLOG . "\n" diff --git a/tests/library/Class/WebService/OAI/Response/ListIdentifiersTest.php b/tests/library/Class/WebService/OAI/Response/ListIdentifiersTest.php index 1b7889e26682daeef5b99fe22d4978809569a679..52cf9e6b05b62ab30cfbb20a460804890a13a9be 100644 --- a/tests/library/Class/WebService/OAI/Response/ListIdentifiersTest.php +++ b/tests/library/Class/WebService/OAI/Response/ListIdentifiersTest.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 ListIdentifiersTest extends Storm_Test_ModelTestCase { +class ListIdentifiersValidTest extends Storm_Test_ModelTestCase { protected $_xpath; protected $_response; @@ -38,12 +38,13 @@ class ListIdentifiersTest extends Storm_Test_ModelTestCase { ->newInstanceWithId(4) ->setClefAlpha('harrypotter-azkaban') ->setDateMaj('2012-04-03 11:42:42'))); + $this->_xml = $this->_response->xml(array('metadataPrefix' => 'oai_dc')); } /** @test */ public function requestVerbShouldBeListIdentifiers() { - $this->_xpath->assertXPathContentContains($this->_response->xml(), + $this->_xpath->assertXPathContentContains($this->_xml, '//oai:request[@verb="ListIdentifiers"]', 'http://moulins.fr/oai2/do'); } @@ -51,7 +52,7 @@ class ListIdentifiersTest extends Storm_Test_ModelTestCase { /** @test */ public function shouldHaveThreeHeaders() { - $this->_xpath->assertXpathCount($this->_response->xml(), + $this->_xpath->assertXpathCount($this->_xml, '//oai:ListIdentifiers/oai:header', 3); } @@ -59,7 +60,7 @@ class ListIdentifiersTest extends Storm_Test_ModelTestCase { /** @test */ public function shouldNotHaveMetadata() { - $this->_xpath->assertNotXpath($this->_response->xml(), + $this->_xpath->assertNotXpath($this->_xml, '//oai:ListIdentifiers/oai:metadata'); } @@ -116,7 +117,26 @@ class ListIdentifiersTest extends Storm_Test_ModelTestCase { protected function _assertHeaderContentAt($header, $content, $position) { $path = sprintf('//oai:ListIdentifiers/oai:header[%s]/oai:%s', $position, $header); - $this->_xpath->assertXPathContentContains($this->_response->xml(), $path, $content); + $this->_xpath->assertXPathContentContains($this->_xml, $path, $content); + } +} + + +class ListIdentifiersWithoutMetadataPrefixTest extends Storm_Test_ModelTestCase { + protected $_xpath; + protected $_response; + + public function setUp() { + 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()); + } + + + /** @test */ + public function errorCodeShouldBeBadArgument() { + $this->_xpath->assertXPath($this->_xml, '//oai:error[@code="badArgument"]'); } } ?> \ No newline at end of file