Merge branch 'master' of github.com:tainacan/tainacan

This commit is contained in:
Jacson Passold 2018-01-08 19:53:20 -02:00
commit b95161792b
5 changed files with 142 additions and 1 deletions

View File

@ -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.
```

View File

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

View File

@ -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
?>

View File

@ -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',

39
tests/test-api-terms.php Normal file
View File

@ -0,0 +1,39 @@
<?php
namespace Tainacan\Tests;
class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
public function test_create_term(){
$taxonomy = $this->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']);
}
}
?>