Get a field from a collection

This commit is contained in:
weryques 2018-03-05 16:23:28 -03:00
parent d652cde744
commit d87e2363d2
3 changed files with 45 additions and 13 deletions

View File

@ -54,9 +54,9 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_all_field_values'),
'permission_callback' => array($this, 'get_all_field_values_permissions_check')
)
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_item_permissions_check')
),
)
);
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base,
@ -100,7 +100,7 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check')
)
),
)
);
}
@ -110,7 +110,7 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
*
* @return WP_Error|WP_REST_Response
*/
public function get_all_field_values( $request ) {
public function get_item( $request ) {
$collection_id = $request['collection_id'];
$field_id = $request['field_id'];
@ -120,9 +120,9 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
return new WP_REST_Response($results, 200);
}
return new WP_REST_Response([
'error_message' => __('Verify the route. A query parameter is missing', 'tainacan'),
], 400);
$result = $this->field_repository->fetch($field_id, 'OBJECT');
return new WP_REST_Response($this->prepare_item_for_response($result, $request), 200);
}
/**
@ -131,9 +131,10 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
* @return bool|WP_Error
* @throws Exception
*/
public function get_all_field_values_permissions_check( $request ) {
if($request['context'] === 'edit' && !$this->field_repository->can_read(new Entities\Field())){
public function get_item_permissions_check( $request ) {
if($request['context'] === 'edit' && !$this->collection_repository->can_read(new Entities\Collection($request['collection_id']))){
return false;
} elseif($request['context'] === 'edit' && !$this->field_repository->can_read(new Entities\Field())){
return false;
}

View File

@ -326,7 +326,7 @@ class Fields extends Repository {
if( isset( $args['meta_query'] ) ){
$args['meta_query'][] = $meta_query;
}else{
} elseif(is_array($args)){
$args['meta_query'] = array( $meta_query );
}

View File

@ -43,6 +43,37 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$this->assertNotEquals('default', $field_added['collection_id']);
}
public function test_fetch_a_field_from_a_collection(){
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Statement',
'description' => 'No Statement'
),
true
);
$fieldA = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'Data',
'description' => 'Descreve valor do campo data.',
'collection' => $collection,
'status' => 'publish',
'field_type' => 'Tainacan\Field_Types\Text',
), true
);
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/fields/' . $fieldA->get_id());
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals('Data', $data['name']);
$this->assertEquals($fieldA->get_id(), $data['id']);
}
public function test_create_default_field(){
$field = json_encode(
array(
@ -378,7 +409,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
}
public function test_fetch_all_field_values(){
global $Tainacan_Fields, $Tainacan_Item_Metadata;
global $Tainacan_Item_Metadata;
$collection = $this->tainacan_entity_factory->create_entity(
'collection',