diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php index 6d0731ef6..8b369a8c4 100644 --- a/src/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -209,9 +209,15 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller { */ public function delete_item( $request ) { $collection_id = $request['collection_id']; - $this->collections_repository->delete($collection_id); + $is_permanently = json_decode($request->get_body(), true); - return 'Não implementado'; + $args = [$collection_id, $is_permanently]; + + $collection = $this->collections_repository->delete($args); + + $prepared_collection = $this->prepare_item_for_response($collection, $request); + + return new WP_REST_Response($prepared_collection, 200); } /** @@ -222,11 +228,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller { * @return bool|WP_Error */ public function delete_item_permissions_check( $request ) { - if(current_user_can('delete_posts')){ - return true; - } - - return false; + return true; } /** diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index c513cd8ad..9e3b55d4e 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -154,7 +154,7 @@ class Collections extends Repository { } /** - * @param Tainacan\Entities\Collection $collection + * @param \Tainacan\Entities\Collection $collection * @return \Tainacan\Entities\Collection * {@inheritDoc} * @see \Tainacan\Repositories\Repository::insert() @@ -169,8 +169,12 @@ class Collections extends Repository { } - public function delete($object){ + public function delete($args){ + if($args[1]['is_permanently'] === true){ + return new Entities\Collection(wp_delete_post($args[0], $args[1]['is_permanently'])); + } + return new Entities\Collection(wp_trash_post($args[0])); } /** diff --git a/tests/test-api-collections.php b/tests/test-api-collections.php index bb55dd01c..f1e08e6aa 100644 --- a/tests/test-api-collections.php +++ b/tests/test-api-collections.php @@ -97,16 +97,15 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { $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(); + $data = json_decode($response->get_data(), true); - $this->assertTrue($data); + $this->assertEquals($collection1->get_id(), $data['id']); + + $no_post = get_post($collection1->get_id()); + + $this->assertNull($no_post); ####################################################################################### @@ -123,16 +122,15 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { $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']); + $this->assertEquals($collection2->get_id(), $data['id']); + + $post_meta = get_post_meta($collection2->get_id(), '_wp_trash_meta_status', true); + + $this->assertNotEmpty($post_meta); } }