diff --git a/docs/tainacan-api.md b/docs/tainacan-api.md index 87f35c6e6..e7efb2108 100644 --- a/docs/tainacan-api.md +++ b/docs/tainacan-api.md @@ -154,3 +154,10 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API. 2.1. Endpoints supported: 2.1.1 GET (Fetch a taxonomy) + + 2.1.2 DELETE (Delete or trash a taxonomy) + +``` + To delete pass in body of requisition the parameter is_permanently as true. + To only trash pass false. +``` diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index 7ce547160..94d33584d 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -36,8 +36,52 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { ); } - public function create_item( $request ) { + public function prepare_item_for_database( $to_prepare ) { + $attributes = $to_prepare[0]; + $taxonomy = $to_prepare[1]; + foreach ($attributes as $attribute => $value){ + $set_ = 'set_'. $attribute; + + try { + $this->term->$set_( $value ); + } catch (\Error $error){ + // Do nothing + } + } + + $this->term->set_taxonomy($taxonomy); + } + + public function create_item( $request ) { + $taxonomy_id = $request['taxonomy_id']; + $body = json_decode($request->get_body(), true); + + $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id); + $taxonomy_db_identifier = $taxonomy->get_db_identifier(); + + if(!empty($body)){ + $to_prepare = [$body, $taxonomy_db_identifier]; + $this->prepare_item_for_database($to_prepare); + + if($this->term->validate()){ + $term_id = $this->terms_repository->insert($this->term); + + $term_inserted = $this->terms_repository->fetch($term_id, $taxonomy); + + return new WP_REST_Response($term_inserted->__toArray(), 200); + } else { + return new WP_REST_Response([ + 'error_message' => 'One or more attributes are invalid.', + 'errors' => $this->term->get_errors(), + ], 400); + } + } + + return new WP_REST_Response([ + 'error_message' => 'The body couldn\'t be empty.', + 'body' => $body, + ], 400); } /** @@ -52,6 +96,14 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { return false; } + + 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/src/api/tainacan-rest-creator.php b/src/api/tainacan-rest-creator.php index af10e8194..68fb304d1 100644 --- a/src/api/tainacan-rest-creator.php +++ b/src/api/tainacan-rest-creator.php @@ -4,6 +4,7 @@ $rest_collections_controller = new TAINACAN_REST_Collections_Controller(); $rest_items_controller = new TAINACAN_REST_Items_Controller(); $rest_metadata_controller = new TAINACAN_REST_Metadata_Controller(); $rest_taxonomies_controller = new TAINACAN_REST_Taxonomies_Controller(); +$rest_terms_controller = new TAINACAN_REST_Terms_Controller(); // Add here other endpoints imports ?> \ No newline at end of file diff --git a/tests/test-api-taxonomies.php b/tests/test-api-taxonomies.php index df39c0785..d7898b89f 100644 --- a/tests/test-api-taxonomies.php +++ b/tests/test-api-taxonomies.php @@ -4,6 +4,48 @@ namespace Tainacan\Tests; class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_UnitApiTestCase { + public function test_delete_or_trash_a_taxonomy(){ + $taxonomy = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => '1genero', + 'description' => 'tipos de musica', + 'allow_insert' => 'yes', + 'status' => 'publish' + ), + true + ); + + $is_permanently = json_encode(['is_permanently' => false]); + + $request_trash = new \WP_REST_Request( + 'DELETE', $this->namespace . '/taxonomies/' . $taxonomy->get_id() + ); + + $request_trash->set_body($is_permanently); + + $this->server->dispatch($request_trash); + + $taxmy = get_post($taxonomy->get_id()); + + $this->assertEquals('trash', $taxmy->post_status); + $this->assertEquals(true, taxonomy_exists($taxonomy->get_db_identifier())); + + ################ DELETE ### + + $is_permanently = json_encode(['is_permanently' => true]); + + $request_delete = new \WP_REST_Request( + 'DELETE', $this->namespace . '/taxonomies/' . $taxonomy->get_id() + ); + + $request_delete->set_body($is_permanently); + + $this->server->dispatch($request_delete); + + $this->assertEquals(false, taxonomy_exists($taxonomy->get_db_identifier())); + } + public function test_create_taxonomy(){ $taxonomy_json = json_encode([ 'name' => 'Nome', diff --git a/tests/test-api-terms.php b/tests/test-api-terms.php new file mode 100644 index 000000000..a001f6c41 --- /dev/null +++ b/tests/test-api-terms.php @@ -0,0 +1,39 @@ +tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => '1genero', + 'description' => 'tipos de musica', + 'allow_insert' => 'yes', + 'status' => 'publish' + ), + true + ); + + $request = new \WP_REST_Request( + 'POST', $this->namespace . '/terms/' . $taxonomy->get_id() + ); + + $term = json_encode([ + 'name' => 'Termo teste', + 'user' => get_current_user_id() + ]); + + $request->set_body($term); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertEquals('Termo teste', $data['name']); + $this->assertEquals($taxonomy->get_db_identifier(), $data['taxonomy']); + } +} + +?> \ No newline at end of file