tainacan/tests/__test-api-exposers.php

540 lines
18 KiB
PHP
Raw Normal View History

2018-03-14 00:29:31 +00:00
<?php
namespace Tainacan\Tests;
/**
* @group api_exposers
*/
class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
protected $item;
protected $collection;
2018-07-10 06:20:14 +00:00
/**
* @var \Tainacan\Entities\Metadatum
*/
protected $metadatum;
2018-08-07 23:50:51 +00:00
/**
* @var \Tainacan\Entities\Metadatum
*/
protected $metadatum2;
2018-03-14 00:29:31 +00:00
protected function create_meta_requirements() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testeItemExpose',
'description' => 'No description',
),
true,
true
);
$type = $this->tainacan_metadatum_factory->create_metadatum('text');
2018-03-14 00:29:31 +00:00
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
2018-03-14 00:29:31 +00:00
array(
'name' => 'teste_Expose',
'description' => 'descricao',
'collection' => $collection,
2018-06-11 17:57:50 +00:00
'metadata_type' => $type,
2018-03-16 18:52:50 +00:00
'exposer_mapping' => [
2018-11-25 22:52:04 +00:00
'dublin-core' => 'dc:language'
2018-03-16 18:52:50 +00:00
]
2018-03-14 00:29:31 +00:00
),
true,
true
);
2018-08-07 23:50:51 +00:00
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'space meta',
'description' => 'meta with space',
'collection' => $collection,
'metadata_type' => $type,
'exposer_mapping' => [
2018-11-25 22:52:04 +00:00
'dublin-core' => 'dc:subject'
2018-08-07 23:50:51 +00:00
]
),
true,
true
);
2018-03-14 00:29:31 +00:00
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_Expose',
'description' => 'adasdasdsa',
'collection' => $collection
),
true,
true
);
$this->collection = $collection;
$this->item = $item;
$this->metadatum = $metadatum;
2018-08-07 23:50:51 +00:00
$this->metadatum2 = $metadatum2;
return ['collection' => $collection, 'item' => $item, 'metadatum' => $metadatum, 'metadatum2' => $metadatum2];
2018-03-14 00:29:31 +00:00
}
2018-04-13 18:14:28 +00:00
/**
* @group value_exposer
*/
public function test_value_exposer() {
global $Tainacan_Metadata, $Tainacan_Item_Metadata;
2018-04-13 18:14:28 +00:00
extract($this->create_meta_requirements());
$item__metadata_json = json_encode([
'values' => 'TestValues_exposers',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->metadatum->get_id() );
2018-04-13 18:14:28 +00:00
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals($this->item->get_id(), $data['item']['id']);
2018-04-13 18:14:28 +00:00
$this->assertEquals('TestValues_exposers', $data['value']);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->metadatum->get_id() );
2018-04-13 18:14:28 +00:00
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals('TestValues_exposers', $data['teste_Expose']);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
2018-04-13 18:14:28 +00:00
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals('adasdasdsa', $data['Description']);
$this->assertEquals('item_teste_Expose', $data['Title']);
$this->assertEquals('TestValues_exposers', $data['teste_Expose']);
}
/**
* @group xml_exposer
*/
2018-04-02 14:25:18 +00:00
public function test_xml_exposer() {
global $Tainacan_Metadata, $Tainacan_Item_Metadata;
2018-03-14 00:29:31 +00:00
extract($this->create_meta_requirements());
$item__metadata_json = json_encode([
'values' => 'TestValues_exposers',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->metadatum->get_id() );
2018-03-14 00:29:31 +00:00
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals($this->item->get_id(), $data['item']['id']);
2018-03-14 00:29:31 +00:00
$this->assertEquals('TestValues_exposers', $data['value']);
2018-08-07 23:50:51 +00:00
$item__metadata_json = json_encode([
'values' => '',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->metadatum2->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
2018-03-14 00:29:31 +00:00
$item_exposer_json = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::TYPE_PARAM => 'Xml',
2018-03-14 00:29:31 +00:00
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->metadatum->get_id() );
2018-03-14 00:29:31 +00:00
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertInstanceOf('SimpleXMLElement', @simplexml_load_string($data));
2018-08-07 23:50:51 +00:00
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->metadatum2->get_id() );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertInstanceOf('SimpleXMLElement', @simplexml_load_string($data));
2018-03-16 18:52:50 +00:00
$item_exposer_json = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::MAPPER_PARAM => 'Dublin Core',
2018-03-16 18:52:50 +00:00
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->metadatum->get_id() );
2018-03-16 18:52:50 +00:00
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
2018-03-27 15:48:53 +00:00
$this->assertEquals('TestValues_exposers', $data['dc:language']);
2018-03-27 15:44:23 +00:00
$item_exposer_json = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::TYPE_PARAM => 'Xml',
\Tainacan\Exposers_Handler::MAPPER_PARAM => 'Dublin Core',
2018-03-27 15:44:23 +00:00
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
2018-03-27 15:44:23 +00:00
$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);
$rdf = $xml->children(\Tainacan\Exposers\Mappers\Dublin_Core::XML_RDF_NAMESPACE);
$dc = $rdf->children(\Tainacan\Exposers\Mappers\Dublin_Core::XML_DC_NAMESPACE);
$this->assertEquals('adasdasdsa', $dc->description);
2018-03-27 15:48:53 +00:00
$this->assertEquals('item_teste_Expose', $dc->title);
$this->assertEquals('TestValues_exposers', $dc->language);
2018-08-07 23:50:51 +00:00
$this->assertEquals('', $dc->subject);
2018-03-27 15:44:23 +00:00
2018-04-02 14:25:18 +00:00
}
/**
* @group exposers-slug
*/
public function test_exposer_map_by_slug() {
global $Tainacan_Metadata, $Tainacan_Item_Metadata;
extract($this->create_meta_requirements());
$item__metadata_json = json_encode([
'values' => 'TestValues_exposers_slug',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->metadatum->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals($this->item->get_id(), $data['item']['id']);
$this->assertEquals('TestValues_exposers_slug', $data['value']);
$item_exposer_json = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::MAPPER_PARAM => 'dublin-core',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->metadatum->get_id() );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals('TestValues_exposers_slug', $data['dc:language']);
$item_exposer_json = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::TYPE_PARAM => 'xml',
\Tainacan\Exposers_Handler::MAPPER_PARAM => 'dublin-core',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->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);
$rdf = $xml->children(\Tainacan\Exposers\Mappers\Dublin_Core::XML_RDF_NAMESPACE);
$dc = $rdf->children(\Tainacan\Exposers\Mappers\Dublin_Core::XML_DC_NAMESPACE);
$this->assertEquals('adasdasdsa', $dc->description);
$this->assertEquals('item_teste_Expose', $dc->title);
$this->assertEquals('TestValues_exposers_slug', $dc->language);
}
2018-04-02 14:25:18 +00:00
/**
* @group oai-pmh
*/
public function test_oai_pmh() {
global $Tainacan_Metadata, $Tainacan_Item_Metadata;
2018-04-02 14:25:18 +00:00
extract($this->create_meta_requirements());
$item_exposer_json = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::TYPE_PARAM => 'OAI-PMH',
2018-04-02 14:25:18 +00:00
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
2018-04-02 14:25:18 +00:00
$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);
2018-03-14 00:29:31 +00:00
}
2018-04-04 04:40:17 +00:00
/**
* @group exposer-type-html
*/
public function test_html_type() {
global $Tainacan_Metadata, $Tainacan_Item_Metadata;
2018-04-04 04:40:17 +00:00
extract($this->create_meta_requirements());
$item_exposer_json = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::TYPE_PARAM => 'Html',
\Tainacan\Exposers_Handler::MAPPER_PARAM => 'Value'
2018-04-04 04:40:17 +00:00
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
2018-04-04 04:40:17 +00:00
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
2018-04-05 16:16:31 +00:00
// Parse HTML reponse
$doc = new \DOMDocument();
$this->assertTrue($doc->loadHTML($data));
$headers = $doc->getElementsByTagName('th');
$values = $doc->getElementsByTagName('td');
$htmlheaders = [];
foreach($headers as $nodeHeader) {
$htmlheaders[] = trim($nodeHeader->textContent);
}
$htmlValues = [];
$row = 0;
$col = 0;
foreach ($values as $nodeValue) {
if(!array_key_exists($row, $htmlValues)) $htmlValues[$row] = [];
$htmlValues[$row][$htmlheaders[$col]] = trim($nodeValue->textContent);
$col++;
if(count($htmlValues[$row]) == count($htmlheaders)) {
$row++;
$col = 0;
}
}
// End of Parse HTML reponse
$this->assertEquals('adasdasdsa', $htmlValues[0]['Description']);
$this->assertEquals('', $htmlValues[0]['teste_Expose']);
$this->assertEquals('item_teste_Expose', $htmlValues[0]['Title']);
2018-04-04 04:40:17 +00:00
}
/**
* @group exposer-type-csv
*/
public function test_csv_type() {
global $Tainacan_Metadata, $Tainacan_Item_Metadata;
extract($this->create_meta_requirements());
2018-04-11 19:13:19 +00:00
$item__metadata_json = json_encode([
'values' => 'TestValues_exposers',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->metadatum->get_id() );
2018-04-11 19:13:19 +00:00
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$item_exposer_json = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::TYPE_PARAM => 'Csv',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
2018-04-11 19:13:19 +00:00
$data = $response->get_data();
$lines = explode(PHP_EOL, $data);
$csv_lines = [];
foreach ($lines as $line) {
$csv_lines[] = str_getcsv($line, ';');
}
array_walk($csv_lines, function(&$a) use ($csv_lines) {
2018-08-07 23:50:51 +00:00
if(count($a) == count($csv_lines[0])) {
2018-04-11 19:13:19 +00:00
$a = array_combine($csv_lines[0], $a);
2018-08-07 23:50:51 +00:00
} else {
2018-04-11 19:13:19 +00:00
}
});
array_shift($csv_lines);
$this->assertEquals('adasdasdsa', $csv_lines[0]['Description']);
$this->assertEquals('TestValues_exposers', $csv_lines[0]['teste_Expose']);
$this->assertEquals('item_teste_Expose', $csv_lines[0]['Title']);
}
2018-04-16 19:28:59 +00:00
/**
* @group items_exposer
*/
public function test_items_exposer() {
global $Tainacan_Metadata, $Tainacan_Item_Metadata;
2018-04-16 19:28:59 +00:00
extract($this->create_meta_requirements());
$item__metadata_json = json_encode([
'values' => 'TestValues_exposers',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->metadatum->get_id() );
2018-04-16 19:28:59 +00:00
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals($this->item->get_id(), $data['item']['id']);
2018-04-16 19:28:59 +00:00
$this->assertEquals('TestValues_exposers', $data['value']);
$item2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_Expose2',
'description' => 'adasdasdsa2',
'collection' => $this->collection
2018-04-16 19:28:59 +00:00
),
true,
true
);
$item3 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_Expose3',
'description' => 'adasdasdsa3',
'collection' => $this->collection
2018-04-16 19:28:59 +00:00
),
true,
true
);
$item_exposer_json = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::MAPPER_PARAM => 'Value',
2018-04-16 19:28:59 +00:00
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/items/' . $this->item->get_id() );
2018-04-16 19:28:59 +00:00
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals('adasdasdsa', $data['Description']);
$this->assertEquals('item_teste_Expose', $data['Title']);
$this->assertEquals('TestValues_exposers', $data['teste_Expose']);
}
2018-05-29 22:26:09 +00:00
/**
* @group mapped_new_collection
*/
public function test_mapped_new_collection() {
$collection_JSON = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::MAPPER_PARAM => 'Dublin Core',
2018-05-29 22:26:09 +00:00
'name' => 'TesteJsonAddDublin_Core',
'description' => 'Teste JSON Dublin Core mapped',
'status' => 'publish'
]);
$mapper = new \Tainacan\Exposers\Mappers\Dublin_Core();
$request = new \WP_REST_Request('POST', $this->namespace . '/collections');
$request->set_body($collection_JSON);
$response = $this->server->dispatch($request);
$this->assertEquals(201, $response->get_status(), sprintf('response: %s', print_r($response, true)));
$collection_array = $response->get_data();
$id = $collection_array['id'];
$Tainacan_collections = \Tainacan\Repositories\Collections::get_instance();
$collection = $Tainacan_collections->fetch($id);
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [ 'order' => 'id' ] );
2018-05-29 22:26:09 +00:00
$this->assertEquals(count($mapper->metadata), count($metadata));
foreach ($metadata as $metadatum) {
$this->assertTrue(array_key_exists($metadatum->get_slug(), $mapper->metadata));
if(! array_key_exists('core_metadatum', $mapper->metadata[$metadatum->get_slug()]) || $mapper->metadata[$metadatum->get_slug()]['core_metadatum'] == false) {
2018-07-27 03:32:43 +00:00
$this->assertEquals($mapper->metadata[$metadatum->get_slug()]['URI'], $metadatum->get_semantic_uri());
2018-05-29 22:26:09 +00:00
}
$this->assertEquals($mapper->metadata[$metadatum->get_slug()]['label'], $metadatum->get_name());
2018-05-29 22:26:09 +00:00
}
}
2018-07-09 21:23:45 +00:00
/**
* @group json_dl_exposer
*/
public function test_jsondl_exposer() {
global $Tainacan_Metadata, $Tainacan_Item_Metadata;
extract($this->create_meta_requirements());
$item__metadata_json = json_encode([
'values' => 'TestValues_exposers',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->metadatum->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals($this->item->get_id(), $data['item']['id']);
$this->assertEquals('TestValues_exposers', $data['value']);
$item_exposer_json = json_encode([
2018-11-25 22:52:04 +00:00
\Tainacan\Exposers_Handler::TYPE_PARAM => 'json-ld',
\Tainacan\Exposers_Handler::MAPPER_PARAM => 'dublin-core',
2018-07-09 21:23:45 +00:00
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
2018-07-10 06:20:14 +00:00
$data = json_decode($response->get_data());
2018-07-09 21:23:45 +00:00
2018-07-10 06:20:14 +00:00
$this->assertEquals('http://purl.org/dc/elements/1.1/', $data->{'@context'}->dc);
$this->assertEquals(get_locale(), $data->{'@context'}->{'@language'});
$this->assertEquals($this->item->get('description'), $data->{'dc:description'}->{'@value'});
$this->assertEquals($this->item->get('title'), $data->{'dc:title'}->{'@value'});
$this->assertEquals('TestValues_exposers', $data->{'dc:language'}->{'@value'});
2018-07-09 21:23:45 +00:00
}
/**
* @group exposer_urls
*/
/*public function test_exposer_urls() {
global $Tainacan_Metadata, $Tainacan_Item_Metadata;
extract($this->create_meta_requirements());
$id = $item->get_id();
$base_url = "{$this->namespace}/items/{$id}/";
2018-11-25 22:52:04 +00:00
$urls = \Tainacan\Exposers_Handler::get_exposer_urls($base_url);
2018-08-07 23:50:51 +00:00
var_dump($urls);return;
foreach ($urls as $type => $type_urls) {
foreach ($type_urls as $url) {
$request = new \WP_REST_Request('GET', $url);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status(), $url);
}
}
2018-08-07 23:50:51 +00:00
} */// TODO automate test this
2018-03-14 00:29:31 +00:00
}
?>