diff --git a/library/Class/Notice.php b/library/Class/Notice.php index 5154e3c8b7b750d6e68bfc7e5b427963a7a99844..5783316526d86329a09f6fd86f4444e333e6419d 100644 --- a/library/Class/Notice.php +++ b/library/Class/Notice.php @@ -42,6 +42,16 @@ class NoticeLoader extends Storm_Model_Loader return $notices; } + + public function getNoticeByClefAlpha($clef) { + $result = $this->findAllBy(array('clef_alpha' => $clef, + 'limit' => 1)); + if (0 == count($result)) + return null; + + return $result[0]; + } + } class Class_Notice extends Storm_Model_Abstract diff --git a/library/Class/WebService/OAI/Response/GetRecord.php b/library/Class/WebService/OAI/Response/GetRecord.php index d3f1926595328ca71afe3c19f1078547a8335308..3f559ebfdfd4c9cb87fbdcd318afa543ab048918 100644 --- a/library/Class/WebService/OAI/Response/GetRecord.php +++ b/library/Class/WebService/OAI/Response/GetRecord.php @@ -20,26 +20,66 @@ */ class Class_WebService_OAI_Response_GetRecord extends Class_WebService_OAI_Response_Null { protected $_notice; + protected $_identifier; + protected $_metadataPrefix; + + public function xml($params = array()) { + if (array_key_exists('identifier', $params)) + $this->_identifier = $params['identifier']; + + 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; + } + + return parent::xml(); + } + public function buildXmlOn($builder) { - if (null === $this->_notice) - return ''; + $response = ''; + $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); + + if (null == $this->_identifier) + return $response . $builder->error(array('code' => 'badArgument'), 'Missing identifier'); + + if (null == $this->_metadataPrefix) + return $response . $builder->error(array('code' => 'badArgument'), 'Missing metadataPrefix'); + + if (null == $this->_notice) + return $response . $builder->error(array('code' => 'idDoesNotExist'), ''); + + if ('oai_dc' != $this->_metadataPrefix) + return $response . $builder->error(array('code' => 'cannotDisseminateFormat'), ''); $visitor = new Class_Notice_DublinCoreVisitor(); $visitor->visit($this->_notice); $recordBuilder = new Class_WebService_OAI_Response_RecordBuilder(); - return - $builder->request(array('verb' => 'GetRecord', - 'metadataPrefix' => 'oai_dc'), - $this->_baseUrl) - . $builder->GetRecord($builder->record($recordBuilder->xml($builder, $visitor))); + return $response . $builder->GetRecord($builder->record($recordBuilder->xml($builder, $visitor))); } public function setNotice($notice) { $this->_notice = $notice; } + + + public function request($builder) { + return $builder->request(array('verb' => 'GetRecord', + 'metadataPrefix' => 'oai_dc'), + $this->_baseUrl); + } } diff --git a/tests/application/modules/opac/controllers/OaiControllerTest.php b/tests/application/modules/opac/controllers/OaiControllerTest.php index 5407c1449a6b51cc639568181c4744ac7ca1b988..947d9090b7e561bad9df92001927804879710357 100644 --- a/tests/application/modules/opac/controllers/OaiControllerTest.php +++ b/tests/application/modules/opac/controllers/OaiControllerTest.php @@ -102,6 +102,24 @@ class OaiControllerListSetsRequestTest extends OaiControllerRequestTestCase { +class OaiControllerGetRecordRequestTest extends OaiControllerRequestTestCase { + protected $_xpath; + + public function setUp() { + parent::setUp(); + $this->dispatch('/opac/oai/request?verb=GetRecord&identifier=zork&metadataPrefix=oai_dc'); + } + + + /** @test */ + public function shouldReturnGetRecordResponse() { + $this->_xpath->assertXPath($this->_response->getBody(), + '//oai:request[@verb="GetRecord"]'); + } +} + + + class OaiControllerUnknownVerbRequestTest extends OaiControllerRequestTestCase { protected $_xpath; diff --git a/tests/library/Class/WebService/OAI/Response/GetRecordTest.php b/tests/library/Class/WebService/OAI/Response/GetRecordTest.php index 6c04134a7b15aaada576bb212c922b1dcba14307..8115ec7e34aaf2d565839556a40511d4945c6b02 100644 --- a/tests/library/Class/WebService/OAI/Response/GetRecordTest.php +++ b/tests/library/Class/WebService/OAI/Response/GetRecordTest.php @@ -19,47 +19,167 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ - -class OAIGetRecordTest extends Storm_Test_ModelTestCase { +abstract class OAIGetRecordTestCase extends Storm_Test_ModelTestCase { const OAI_RECORD_PATH = '//oai:GetRecord/oai:record/'; const OAI_HEADER_PATH = 'oai:header/'; protected $_xpath; protected $_response; + protected $_xml; public function setUp() { parent::setUp(); - $this->_xpath = TestXPathFactory::newOai(); - $this->_xpath->registerNameSpace('oai_dc', 'http://www.openarchives.org/OAI/2.0/oai_dc/'); + $this->_xpath = TestXPathFactory::newOaiDc(); $this->_response = new Class_WebService_OAI_Response_GetRecord('http://afi-sa.fr/oai/do'); + } +} + + +class OAIGetRecordNoIdentifierTest extends OAIGetRecordTestCase { + public function setUp() { + parent::setUp(); + $this->_xml = $this->_response->xml(array('metadataPrefix' => 'oai_dc')); + } + + + /** @test */ + public function requestVerbShouldBeGetRecord() { + $this->_xpath->assertXpath($this->_xml, + '//oai:request[@verb="GetRecord"][@metadataPrefix="oai_dc"]'); + } + + + /** @test */ + public function errorCodeShouldBeBadArgument() { + $this->_xpath->assertXpath($this->_xml, + '//oai:error[@code="badArgument"]'); + } +} + + +class OAIGetRecordNoMetadataPrefixTest extends OAIGetRecordTestCase { + public function setUp() { + parent::setUp(); + $this->_xml = $this->_response->xml(array('identifier' => 'toto')); + } + + + /** @test */ + public function requestVerbShouldBeGetRecord() { + $this->_xpath->assertXpath($this->_xml, + '//oai:request[@verb="GetRecord"][@identifier="toto"]'); + } + + + /** @test */ + public function errorCodeShouldBeBadArgument() { + $this->_xpath->assertXpath($this->_xml, + '//oai:error[@code="badArgument"]'); + } +} + + - $potter = Class_Notice::getLoader() - ->newInstanceWithId(4) - ->setClefAlpha('harrypotter-sorciers') - ->setTitrePrincipal('Harry Potter a l\'ecole des sorciers') - ->setDateMaj('2001-12-14 11:39:44'); +class OAIGetRecordNotFoundParamsTest extends OAIGetRecordTestCase { + public function setUp() { + parent::setUp(); + + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice') + ->whenCalled('getNoticeByClefAlpha') + ->with('harrypotter-sorciers') + ->answers(null); + + $this->_xml = $this->_response->xml(array('identifier' => 'harrypotter-sorciers', + 'metadataPrefix' => 'oai_dc')); + + } + + + /** @test */ + public function requestVerbShouldBeGetRecord() { + $this->_xpath->assertXpath($this->_xml, + '//oai:request[@verb="GetRecord"][@identifier="harrypotter-sorciers"][@metadataPrefix="oai_dc"]'); + } + + + /** @test */ + public function errorCodeShouldBeIdDoesNotExist() { + $this->_xpath->assertXpath($this->_xml, + '//oai:error[@code="idDoesNotExist"]'); + } +} + + +class OAIGetRecordNotSupportedPrefixTest extends OAIGetRecordTestCase { + public function setUp() { + parent::setUp(); + + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice') + ->whenCalled('getNoticeByClefAlpha') + ->with('harrypotter-sorciers') + ->answers(Class_Notice::getLoader() + ->newInstanceWithId(4) + ->setClefAlpha('harrypotter-sorciers') + ->setTitrePrincipal('Harry Potter a l\'ecole des sorciers') + ->setDateMaj('2001-12-14 11:39:44')); + + $this->_xml = $this->_response->xml(array('identifier' => 'harrypotter-sorciers', + 'metadataPrefix' => 'not_supported')); + + } + + + /** @test */ + public function requestVerbShouldBeGetRecord() { + $this->_xpath->assertXpath($this->_xml, + '//oai:request[@verb="GetRecord"][@identifier="harrypotter-sorciers"][@metadataPrefix="not_supported"]'); + } + + + /** @test */ + public function errorCodeShouldBeCannotDisseminateFormat() { + $this->_xpath->assertXpath($this->_xml, + '//oai:error[@code="cannotDisseminateFormat"]'); + } +} + + +class OAIGetRecordValidParamsTest extends OAIGetRecordTestCase { + public function setUp() { + parent::setUp(); - $this->_response->setNotice($potter); + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice') + ->whenCalled('getNoticeByClefAlpha') + ->with('harrypotter-sorciers') + ->answers(Class_Notice::getLoader() + ->newInstanceWithId(4) + ->setClefAlpha('harrypotter-sorciers') + ->setTitrePrincipal('Harry Potter a l\'ecole des sorciers') + ->setDateMaj('2001-12-14 11:39:44')); + + $this->_xml = $this->_response->xml(array('identifier' => 'harrypotter-sorciers', + 'metadataPrefix' => 'oai_dc')); + } /** @test */ public function requestVerbShouldBeGetRecord() { - $this->_xpath->assertXpath($this->_response->xml(), - '//oai:request[@verb="GetRecord"]'); + $this->_xpath->assertXpath($this->_xml, + '//oai:request[@verb="GetRecord"][@identifier="harrypotter-sorciers"][@metadataPrefix="oai_dc"]'); } /** @test */ public function requestMetadataPrefixShouldBeOaiDc() { - $this->_xpath->assertXpath($this->_response->xml(), + $this->_xpath->assertXpath($this->_xml, '//oai:request[@metadataPrefix="oai_dc"]'); } /** @test */ public function shouldContainOneHeader() { - $this->_xpath->assertXpathCount($this->_response->xml(), + $this->_xpath->assertXpathCount($this->_xml, self::OAI_RECORD_PATH . 'oai:header', 1); } @@ -67,7 +187,7 @@ class OAIGetRecordTest extends Storm_Test_ModelTestCase { /** @test */ public function headerShouldContainRecordIdentifier() { - $this->_xpath->assertXPathContentContains($this->_response->xml(), + $this->_xpath->assertXPathContentContains($this->_xml, self::OAI_RECORD_PATH . self::OAI_HEADER_PATH . 'oai:identifier', sprintf('http://localhost%s/recherche/notice/harrypotter-sorciers', BASE_URL)); @@ -76,7 +196,7 @@ class OAIGetRecordTest extends Storm_Test_ModelTestCase { /** @test */ public function recordHeaderDateStampShouldBe2001DecemberFourteen() { - $this->_xpath->assertXPathContentContains($this->_response->xml(), + $this->_xpath->assertXPathContentContains($this->_xml, self::OAI_RECORD_PATH . self::OAI_HEADER_PATH . 'oai:datestamp', '2001-12-14'); } @@ -84,14 +204,14 @@ class OAIGetRecordTest extends Storm_Test_ModelTestCase { /** @test */ public function metadataShouldContainOaiDublinCore() { - $this->_xpath->assertXPath($this->_response->xml(), + $this->_xpath->assertXPath($this->_xml, self::OAI_RECORD_PATH . 'oai:metadata/oai_dc:dc'); } /** @test */ public function shouldContainOneMetadata() { - $this->_xpath->assertXpathCount($this->_response->xml(), + $this->_xpath->assertXpathCount($this->_xml, self::OAI_RECORD_PATH . 'oai:metadata', 1); }