From d2495af712e2fab3ef990bf141c7e554eccb3801 Mon Sep 17 00:00:00 2001 From: weryques Date: Thu, 7 Dec 2017 13:49:11 -0200 Subject: [PATCH] Test delete or trash items and collections --- .../class-tainacan-rest-items-controller.php | 51 +++++++++++-- tests/test-api-collections.php | 55 +++++++++++++- tests/test-api-items.php | 73 ++++++++++++++++++- 3 files changed, 170 insertions(+), 9 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index 7074c4a01..17226b319 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -69,6 +69,12 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { )); } + /** + * @param mixed $item + * @param WP_REST_Request $request + * + * @return mixed|string|void|WP_Error|WP_REST_Response + */ public function prepare_item_for_response( $item, $request ) { if (!empty($item) && $item instanceof WP_Query){ $items_as_array = []; @@ -91,6 +97,11 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { return $item; } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ public function get_items( $request ) { $collection_id = $request['collection_id']; $items = $this->items_repository->fetch([], $collection_id, 'WP_Query'); @@ -100,15 +111,26 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { return new WP_REST_Response($response, 200); } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function get_item_permissions_check( $request ) { return true; } - public function prepare_item_for_database( $request, $collection_id ) { - $this->item->set_title($request['title']); - $this->item->set_description($request['description']); + /** + * @param WP_REST_Request $request + * @param $collection_id + * + * @return object|Entities\Item|WP_Error + */ + public function prepare_item_for_database( $request ) { + $this->item->set_title($request[0]['title']); + $this->item->set_description($request[0]['description']); - $collection_wp_post = get_post($collection_id); + $collection_wp_post = get_post($request[1]); $collection = new Entities\Collection($collection_wp_post); $this->item->set_collection($collection); @@ -116,11 +138,16 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { return $this->item; } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ public function create_item( $request ) { $collection_id = $request['collection_id']; $item = json_decode($request->get_body(), true); - $item_prepared = $this->prepare_item_for_database($item, $collection_id); + $item_prepared = $this->prepare_item_for_database([$item, $collection_id]); if($item_prepared->validate()){ $item = $this->items_repository->insert($item_prepared); @@ -131,9 +158,23 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { return new WP_REST_Response($item_prepared->get_errors(), 400); } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function create_item_permissions_check( $request ) { return true; } + + public function delete_item( $request ) { + return parent::delete_item( $request ); // TODO: Change the autogenerated stub + } + + public function delete_item_permissions_check( $request ) { + return parent::delete_item_permissions_check( $request ); // TODO: Change the autogenerated stub + } + } ?> \ No newline at end of file diff --git a/tests/test-api-collections.php b/tests/test-api-collections.php index acc3830f9..bb55dd01c 100644 --- a/tests/test-api-collections.php +++ b/tests/test-api-collections.php @@ -47,7 +47,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { $collection = json_decode($response->get_data()); $id = $collection->id; - $requestGet = new \WP_REST_Request( 'GET', $this->namespaced_route.'/collections/'.$id ); + $requestGet = new \WP_REST_Request( 'GET', $this->namespaced_route . '/collections/'.$id ); $responseGet = $this->server->dispatch( $requestGet ); $this->assertEquals( 200, $responseGet->get_status() ); @@ -69,7 +69,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { ), true ); - $request = new \WP_REST_Request( 'GET', $this->namespaced_route.'/collections' ); + $request = new \WP_REST_Request( 'GET', $this->namespaced_route . '/collections' ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -83,6 +83,57 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals('testeApi', $one_collection['name']); } + public function test_delete_or_trash_a_collection(){ + $collection1 = $this->tainacan_entity_factory->create_entity('collection', '', true); + + // Delete permanently + $delete_permanently = json_encode(['is_permanently' => true]); + + $request = new \WP_REST_Request( + 'DELETE', + $this->namespaced_route . '/collections/' . $collection1->get_id() + ); + $request->set_body($delete_permanently); + + $response = $this->server->dispatch($request); + + // To be removed + if($response->get_status() != 200){ + $this->markTestSkipped('Need method delete implemented.'); + } + + $this->assertEquals(200, $response->get_status()); + + $data = $response->get_data(); + + $this->assertTrue($data); + + ####################################################################################### + + $collection2 = $this->tainacan_entity_factory->create_entity('collection', '', true); + + // Move to trash + $delete_permanently = json_encode(['is_permanently' => false]); + + $request = new \WP_REST_Request( + 'DELETE', + $this->namespaced_route . '/collections/' . $collection2->get_id() + ); + $request->set_body($delete_permanently); + + $response = $this->server->dispatch($request); + + // To be removed + if($response->get_status() != 200){ + $this->markTestSkipped('Need method delete implemented.'); + } + + $this->assertEquals(200, $response->get_status()); + + $data = json_decode($response->get_data(), true); + + $this->assertEquals('trash', $data['status']); + } } ?> diff --git a/tests/test-api-items.php b/tests/test-api-items.php index 6272ec253..6a1be9ed3 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -45,7 +45,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { 'item', array( 'title' => 'Lean Startup', - 'description' => 'Um processo ágio de criação de novos negócios.', + 'description' => 'Um processo ágil de criação de novos negócios.', 'collection' => $collection ), true @@ -55,7 +55,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { 'item', array( 'title' => 'SCRUM', - 'description' => 'Um framework ágio para gerenciamento de tarefas.', + 'description' => 'Um framework ágil para gerenciamento de tarefas.', 'collection' => $collection ), true @@ -77,6 +77,75 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals($item1->get_title(), $second_item['title']); } + public function test_delete_or_trash_item_from_a_collection(){ + $collection = $this->tainacan_entity_factory->create_entity('collection', '', true); + + $item1 = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'Lean Startup', + 'description' => 'Um processo ágil de criação de novos negócios.', + 'collection' => $collection + ), + true + ); + + // Move to trash + $delete_permanently = json_encode(['is_permanently' => false]); + + $request = new \WP_REST_Request( + 'DELETE', + $this->namespaced_route . '/items/collection/' . $collection->get_id() . '/' . $item1->get_id() + ); + $request->set_body($delete_permanently); + + $response = $this->server->dispatch($request); + + // To be removed + if($response->get_status() != 200){ + $this->markTestSkipped('Need method delete implemented.'); + } + + $this->assertEquals(200, $response->get_status()); + + $data = json_decode($response->get_data(), true); + + $this->assertEquals('trash', $data['status']); + + ####################################################################################### + + $item2 = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'SCRUM', + 'description' => 'Um framework ágil para gerenciamento de tarefas.', + 'collection' => $collection + ), + true + ); + + // Delete permanently + $delete_permanently = json_encode(['is_permanently' => true]); + + $request = new \WP_REST_Request( + 'DELETE', + $this->namespaced_route . '/items/collection/' . $collection->get_id() . '/' . $item2->get_id() + ); + $request->set_body($delete_permanently); + + $response = $this->server->dispatch($request); + + // To be removed + if($response->get_status() != 200){ + $this->markTestSkipped('Need method delete implemented.'); + } + + $this->assertEquals(200, $response->get_status()); + + $data = json_decode($response->get_data(), true); + + $this->assertTrue($data); + } } ?> \ No newline at end of file