add get field value route, test aproved itemMetadata suggestion

This commit is contained in:
Jacson Passold 2018-03-01 15:19:46 -03:00
parent 8da11fba99
commit 6d6e4fbe23
2 changed files with 54 additions and 9 deletions

View File

@ -57,6 +57,15 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
//'args' => $this->get_collection_params(),
)
)
);
register_rest_route($this->namespace, '/item/(?P<item_id>[\d]+)/'. $this->rest_base. '/(?P<field_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_item_field_value'),
'permission_callback' => array($this, 'get_items_permissions_check'),
)
)
);
}
@ -115,6 +124,32 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
return new WP_REST_Response($prepared_item, 200);
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function get_item_field_value( $request ) {
$item_id = $request['item_id'];
$field_id = $request['field_id'];
$item = $this->item_repository->fetch($item_id);
$items_metadata = $item->get_fields();
$prepared_item = '';
foreach ($items_metadata as $item_metadata){
$field = $item_metadata->get_field();
if($field->get_id() == $field_id) {
$prepared_item = $this->prepare_item_for_response($item_metadata, $request);
$prepared_item['field']['field_type_object'] = $this->prepare_item_for_response( $field->get_field_type_object(), $request);
}
}
return new WP_REST_Response($prepared_item, 200);
}
/**
* @param WP_REST_Request $request
@ -219,7 +254,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
else {
$field_id = $request['metadata_id'];
$field = $this->field_repository->fetch( $field_id );
return $field->get_accept_suggestion();
return 'publish' === $field->get_status() && $field->get_accept_suggestion();
}
}

View File

@ -16,6 +16,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
'name' => 'testeItemMetadata',
'description' => 'No description',
),
true,
true
);
@ -30,21 +31,23 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
'field_type' => $type,
'accept_suggestion' => true
),
true,
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste',
'title' => 'item_teste_metadado',
'description' => 'adasdasdsa',
'collection' => $collection
),
true,
true
);
$item__metadata_json = json_encode([
'values' => 'TestValues',
'values' => 'TestValues_metadado',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id() );
@ -57,20 +60,20 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$data = $response->get_data();
$this->assertEquals($item->get_id() , $data['item']['id']);
$this->assertEquals('TestValues', $data['value']);
$this->assertEquals('TestValues_metadado', $data['value']);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata' );
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $field->get_id() );
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 'TestValues', $data['value'] );
$data = $response->get_data();
$this->assertEquals( 'TestValues_metadado', $data['value'] );
// Test Suggestion
$new_user = $this->factory()->user->create(array( 'role' => 'subscriber' ));
wp_set_current_user($new_user);
$item__metadata_json = json_encode([
'values' => 'TestValuesSuggestion',
'values' => 'TestValuesSuggestion_metadado',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id() );
@ -92,7 +95,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$pending = $log->get_value();
$this->assertEquals('TestValuesSuggestion', $pending->value);
$this->assertEquals('TestValuesSuggestion_metadado', $pending->value);
wp_set_current_user($this->user_id);
@ -100,6 +103,13 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $field->get_id() );
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals( 'TestValuesSuggestion_metadado', $data['value'] );
}