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:
weryques 2017-12-07 10:46:31 -02:00
parent 75a64ebed1
commit b5a247e4e5
2 changed files with 91 additions and 41 deletions

View File

@ -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;
}
}

View File

@ -4,51 +4,78 @@ namespace Tainacan\Tests;
class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
public function test_fetch_items_from_a_collection(){
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Agile',
'description' => 'Agile methods'
),
true
);
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
);
$item1 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'Lean Startup',
'description' => 'Um processo ágio de criação de novos negócios.',
'collection' => $collection
),
true
);
$item_json = json_encode([
'title' => 'Vue JS 2',
'description' => 'The Progressive JavasScript Framework'
]);
$item2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'SCRUM',
'description' => 'Um framework ágio para gerenciamento de tarefas.',
'collection' => $collection
),
true
);
$request = new \WP_REST_Request('POST', $this->namespaced_route . '/items/collection/' . $collection->get_id());
$request->set_body($item_json);
$request = new \WP_REST_Request('GET', $this->namespaced_route . '/items/collection/' . $collection->get_id());
$response = $this->server->dispatch($request);
$response = $this->server->dispatch($request);
$this->assertEquals(201, $response->get_status());
$this->assertEquals(201, $response->get_status());
$data = json_decode($response->get_data(), true);
$data = json_decode($response->get_data(), true);
$this->assertContainsOnly('string', $data);
$this->assertEquals('Vue JS 2', $data['title']);
}
$first_item = json_decode($data[0], true);
$second_item = json_decode($data[1], true);
public function test_fetch_items_from_a_collection(){
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Agile',
'description' => 'Agile methods'
),
true
);
$this->assertEquals($item2->get_title(), $first_item['title']);
$this->assertEquals($item1->get_title(), $second_item['title']);
}
$item1 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'Lean Startup',
'description' => 'Um processo ágio de criação de novos negócios.',
'collection' => $collection
),
true
);
$item2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'SCRUM',
'description' => 'Um framework ágio para gerenciamento de tarefas.',
'collection' => $collection
),
true
);
$request = new \WP_REST_Request('GET', $this->namespaced_route . '/items/collection/' . $collection->get_id());
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = json_decode($response->get_data(), true);
$this->assertContainsOnly('string', $data);
$first_item = json_decode($data[0], true);
$second_item = json_decode($data[1], true);
$this->assertEquals($item2->get_title(), $first_item['title']);
$this->assertEquals($item1->get_title(), $second_item['title']);
}
}