API
Test create item in a collection; Implemented create item in Items controller; Implemented prepare item for database in Item controller;
This commit is contained in:
parent
75a64ebed1
commit
b5a247e4e5
|
@ -97,19 +97,42 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
|
||||
$response = $this->prepare_item_for_response($items, $request);
|
||||
|
||||
return new WP_REST_Response($response, 201);
|
||||
return new WP_REST_Response($response, 200);
|
||||
}
|
||||
|
||||
public function get_item_permissions_check( $request ) {
|
||||
return parent::get_item_permissions_check( $request ); // TODO: Change the autogenerated stub
|
||||
return true;
|
||||
}
|
||||
|
||||
public function prepare_item_for_database( $request, $collection_id ) {
|
||||
$this->item->set_title($request['title']);
|
||||
$this->item->set_description($request['description']);
|
||||
|
||||
$collection_wp_post = get_post($collection_id);
|
||||
$collection = new Entities\Collection($collection_wp_post);
|
||||
|
||||
$this->item->set_collection($collection);
|
||||
|
||||
return $this->item;
|
||||
}
|
||||
|
||||
public function create_item( $request ) {
|
||||
return parent::create_item( $request ); // TODO: Change the autogenerated stub
|
||||
$collection_id = $request['collection_id'];
|
||||
$item = json_decode($request->get_body(), true);
|
||||
|
||||
$item_prepared = $this->prepare_item_for_database($item, $collection_id);
|
||||
|
||||
if($item_prepared->validate()){
|
||||
$item = $this->items_repository->insert($item_prepared);
|
||||
|
||||
return new WP_REST_Response($item->__toJSON(), 201);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($item_prepared->get_errors(), 400);
|
||||
}
|
||||
|
||||
public function create_item_permissions_check( $request ) {
|
||||
return parent::create_item_permissions_check( $request ); // TODO: Change the autogenerated stub
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,33 @@ namespace Tainacan\Tests;
|
|||
|
||||
class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
|
||||
|
||||
public function test_create_item_in_a_collection(){
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'Javascript Frameworks',
|
||||
'description' => 'The best framework to javascript'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$item_json = json_encode([
|
||||
'title' => 'Vue JS 2',
|
||||
'description' => 'The Progressive JavasScript Framework'
|
||||
]);
|
||||
|
||||
$request = new \WP_REST_Request('POST', $this->namespaced_route . '/items/collection/' . $collection->get_id());
|
||||
$request->set_body($item_json);
|
||||
|
||||
$response = $this->server->dispatch($request);
|
||||
|
||||
$this->assertEquals(201, $response->get_status());
|
||||
|
||||
$data = json_decode($response->get_data(), true);
|
||||
|
||||
$this->assertEquals('Vue JS 2', $data['title']);
|
||||
}
|
||||
|
||||
public function test_fetch_items_from_a_collection(){
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
|
@ -37,7 +64,7 @@ public function test_fetch_items_from_a_collection(){
|
|||
$request = new \WP_REST_Request('GET', $this->namespaced_route . '/items/collection/' . $collection->get_id());
|
||||
$response = $this->server->dispatch($request);
|
||||
|
||||
$this->assertEquals(201, $response->get_status());
|
||||
$this->assertEquals(200, $response->get_status());
|
||||
|
||||
$data = json_decode($response->get_data(), true);
|
||||
|
||||
|
|
Loading…
Reference in New Issue