add update fields mappers fature to field-mappers api

This commit is contained in:
Jacson Passold 2018-06-08 23:03:49 -03:00
parent f88bc738b8
commit 97b8d42061
2 changed files with 182 additions and 1 deletions

View File

@ -21,7 +21,12 @@ class REST_Field_Mappers_Controller extends REST_Controller {
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
)
),
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check'),
),
)
);
}
@ -39,6 +44,24 @@ class REST_Field_Mappers_Controller extends REST_Controller {
return $field_arr;
}
/**
* @param mixed $item
* @param \WP_REST_Request $request
*
* @return array|\WP_Error|\WP_REST_Response
*/
public function prepare_field_for_response( $item, $request ) {
if(!empty($item)){
$item_arr = $item->_toArray();
$item_arr['field_type_object'] = $item->get_field_type_object()->_toArray();
return $item_arr;
}
return $item;
}
/**
* @param \WP_REST_Request $request
*
@ -65,6 +88,75 @@ class REST_Field_Mappers_Controller extends REST_Controller {
public function get_items_permissions_check( $request ) {
return true;
}
/**
* Verify if current user has permission to update a fields mappers
*
* @param \WP_REST_Request $request
*
* @return bool|\WP_Error
* @throws \Exception
*/
public function update_item_permissions_check( $request ) {
$body = json_decode( $request->get_body(), true );
if(
is_array($body) &&
array_key_exists('fields_mappers', $body) &&
is_array($body['fields_mappers']) &&
count($body['fields_mappers']) > 0 &&
array_key_exists('exposer-map', $body)
) {
$field_mapper = $body['fields_mappers'][0];
$field = \Tainacan\Repositories\Repository::get_entity_by_post($field_mapper['field_id']);
if($field instanceof \Tainacan\Entities\Field && $field->can_edit()) {
return true;
}
}
return false;
}
/**
* @param \WP_REST_Request $request
*
* @return \WP_Error|\WP_REST_Response
*/
public function update_item( $request ) {
$Tainacan_Exposers = \Tainacan\Exposers\Exposers::get_instance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance();
$body = json_decode( $request->get_body(), true );
if($mapper = $Tainacan_Exposers::request_has_mapper($request)) {
if(count($body['fields_mappers']) > 0) {
$response = [];
foreach ($body['fields_mappers'] as $field_mapper) {
$field = $Tainacan_Fields->fetch($field_mapper['field_id']);
$exposer_mapping = $field->get('exposer_mapping');
if($field_mapper['mapper_metadata'] == '') {
if(array_key_exists($exposer_mapping, $mapper->slug) ) unset($exposer_mapping[$mapper->slug]);
} else {
$exposer_mapping[$mapper->slug] = $field_mapper['mapper_metadata'];
}
$field->set('exposer_mapping', $exposer_mapping);
if($field->validate()) {
$Tainacan_Fields->update($field);
$response[] = $this->prepare_field_for_response($field, $request);
} else {
return new \WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared->get_errors(),
'field' => $this->prepare_item_for_response($prepared, $request),
], 400);
}
}
return new \WP_REST_Response($response, 200);
}
}
return new \WP_REST_Response([
'error_message' => __('Body can not be empty.', 'tainacan'),
'item' => $request->get_body()
], 400);
}
}
?>

View File

@ -7,6 +7,62 @@ namespace Tainacan\Tests;
*/
class TAINACAN_REST_Field_Mappers_Controller extends TAINACAN_UnitApiTestCase {
protected function create_meta_requirements() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testItemFieldMappers',
'description' => 'No description',
),
true,
true
);
$type = $this->tainacan_field_factory->create_field('text');
$field = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'test_FieldMappers',
'description' => 'descricao',
'collection' => $collection,
'field_type' => $type,
'exposer_mapping' => [
'dublin-core' => 'language'
]
),
true,
true
);
$field2 = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'test_FieldMappers2',
'description' => 'descricao2',
'collection' => $collection,
'field_type' => $type
),
true,
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_FieldMappers',
'description' => 'adasdasdsaadsf',
'collection' => $collection
),
true,
true
);
$this->collection = $collection;
$this->item = $item;
$this->field = $field;
return ['collection' => $collection, 'item' => $item, 'field' => $field, 'field2' => $field2];
}
public function test_get_field_mappers(){
$field_mapper_request = new \WP_REST_Request('GET', $this->namespace . '/field-mappers');
@ -32,6 +88,39 @@ class TAINACAN_REST_Field_Mappers_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals($field_mappers[$i]->header, $data[$i]['header']);
}
}
public function test_update_field_mappers(){
extract($this->create_meta_requirements());
$dc = new \Tainacan\Exposers\Mappers\Dublin_Core();
$field_mapper_request = new \WP_REST_Request('POST', $this->namespace . '/field-mappers');
$field_mapper_json = json_encode([
'fields_mappers' => [
['field_id' => $field->get_id(), 'mapper_metadata' => 'contributor'],
['field_id' => $field2->get_id(), 'mapper_metadata' => 'coverage']
],
'exposer-map' => $dc->slug
]);
$field_mapper_request->set_body($field_mapper_json);
$field_mapper_response = $this->server->dispatch($field_mapper_request);
$this->assertEquals(200, $field_mapper_response->get_status());
$data = $field_mapper_response->get_data();
$this->assertEquals('contributor', $data[0]['exposer_mapping']['dublin-core']);
$this->assertEquals('coverage', $data[1]['exposer_mapping']['dublin-core']);
$Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance();
$db_field = $Tainacan_Fields->fetch($field->get_id());
$exposer_mapping = $db_field->get('exposer_mapping');
$this->assertEquals('contributor', $exposer_mapping['dublin-core']);
$db_field = $Tainacan_Fields->fetch($field2->get_id());
$exposer_mapping = $db_field->get('exposer_mapping');
$this->assertEquals('coverage', $exposer_mapping['dublin-core']);
}
}
?>