item exposer

This commit is contained in:
Jacson Passold 2018-04-16 16:28:59 -03:00
parent 0358fd758a
commit cf3c952b42
2 changed files with 71 additions and 0 deletions

View File

@ -92,6 +92,16 @@ class Exposers {
public function rest_response($item_arr, $request) {
if($request->get_method() == 'GET' && substr($request->get_route(), 0, strlen('/tainacan/v2')) == '/tainacan/v2') {
if($exposer = $this->request_has_mapper($request)) {
if(substr($request->get_route(), 0, strlen('/tainacan/v2/items')) == '/tainacan/v2/items') { //TODO do it at rest not here
$repos_items = \Tainacan\Repositories\Items::get_instance();
$item = $repos_items->fetch($item_arr['id']);
$items_metadata = $item->get_fields();
$prepared_item = [];
foreach ($items_metadata as $item_metadata){
array_push($prepared_item, $item_metadata->__toArray());
}
$item_arr = $prepared_item;
}
return $this->map($item_arr, $exposer, $request); //TODO request -> args
}
}

View File

@ -288,6 +288,67 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$this->assertEquals('item_teste_Expose', $csv_lines[0]['Title']);
}
/**
* @group items_exposer
*/
public function test_items_exposer() {
global $Tainacan_Fields, $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/' . $item->get_id() . '/metadata/' . $field->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($item->get_id(), $data['item']['id']);
$this->assertEquals('TestValues_exposers', $data['value']);
$item2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_Expose2',
'description' => 'adasdasdsa2',
'collection' => $collection
),
true,
true
);
$item3 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_Expose3',
'description' => 'adasdasdsa3',
'collection' => $collection
),
true,
true
);
$item_exposer_json = json_encode([
'exposer-map' => 'Value',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/items/' . $item->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('adasdasdsa', $data['Description']);
$this->assertEquals('item_teste_Expose', $data['Title']);
$this->assertEquals('TestValues_exposers', $data['teste_Expose']);
}
}
?>