From becba0fd2d226e847eb3513982575a921425dca8 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 2 Apr 2018 11:25:18 -0300 Subject: [PATCH] add oai-pmh dublin core format support --- src/exposers/class-tainacan-exposers.php | 1 + src/exposers/types/class-tainacan-oai-pmh.php | 30 +++++++++++++++++++ tests/test-api-exposers.php | 30 ++++++++++++++++--- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/exposers/types/class-tainacan-oai-pmh.php diff --git a/src/exposers/class-tainacan-exposers.php b/src/exposers/class-tainacan-exposers.php index 8d82eaf59..81dd8e5f7 100644 --- a/src/exposers/class-tainacan-exposers.php +++ b/src/exposers/class-tainacan-exposers.php @@ -13,6 +13,7 @@ class Exposers { public function __construct() { $this->register_exposer_type('Tainacan\Exposers\Types\Xml'); + $this->register_exposer_type('Tainacan\Exposers\Types\OAI_PMH'); $this->register_exposer_mapper('Tainacan\Exposers\Mappers\Dublin_Core'); add_filter( 'rest_request_after_callbacks', [$this, 'rest_request_after_callbacks'], 10, 3 ); diff --git a/src/exposers/types/class-tainacan-oai-pmh.php b/src/exposers/types/class-tainacan-oai-pmh.php new file mode 100644 index 000000000..74efc5724 --- /dev/null +++ b/src/exposers/types/class-tainacan-oai-pmh.php @@ -0,0 +1,30 @@ +set_headers( ['Content-Type: application/xml; charset=' . get_option( 'blog_charset' )] ); + $xml = new \SimpleXMLElement(apply_filters('tainacan-exposer-head', ' + + ' + )); + $namespace = apply_filters('tainacan-oai-pmh-namespace', \Tainacan\Exposers\Mappers\Dublin_Core::XML_DC_NAMESPACE); + $this->array_to_xml($response->get_data(), apply_filters('tainacan-oai-pmh-root', $xml), $namespace); + $response->set_data($xml->asXml()); + return $response; + } + +} \ No newline at end of file diff --git a/tests/test-api-exposers.php b/tests/test-api-exposers.php index 5bc5fec81..dcae31f58 100644 --- a/tests/test-api-exposers.php +++ b/tests/test-api-exposers.php @@ -58,8 +58,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase { return ['collection' => $collection, 'item' => $item, 'field' => $field]; } - public function test_exposers() { - $this->create_meta_requirements(); + public function test_xml_exposer() { global $Tainacan_Fields, $Tainacan_Item_Metadata; extract($this->create_meta_requirements()); @@ -77,7 +76,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase { $data = $response->get_data(); - $this->assertEquals($item->get_id() , $data['item']['id']); + $this->assertEquals($item->get_id(), $data['item']['id']); $this->assertEquals('TestValues_exposers', $data['value']); $item_exposer_json = json_encode([ @@ -121,7 +120,30 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase { $this->assertEquals('item_teste_Expose', $dc->title); $this->assertEquals('TestValues_exposers', $dc->language); - //echo $rdf->asXML(); + } + + /** + * @group oai-pmh + */ + public function test_oai_pmh() { + global $Tainacan_Fields, $Tainacan_Item_Metadata; + + extract($this->create_meta_requirements()); + + $item_exposer_json = json_encode([ + 'exposer-type' => 'OAI-PMH', + 'exposer-map' => 'Dublin Core', + ]); + $request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata' ); + $request->set_body($item_exposer_json); + $response = $this->server->dispatch($request); + $this->assertEquals(200, $response->get_status()); + $data = $response->get_data(); + + $xml = new \SimpleXMLElement($data); + $dc = $xml->children(\Tainacan\Exposers\Mappers\Dublin_Core::XML_DC_NAMESPACE); + $this->assertEquals('adasdasdsa', $dc->description); + $this->assertEquals('item_teste_Expose', $dc->title); } }