Create term in a taxonomy and test that creation.

This commit is contained in:
weryques 2018-01-08 15:33:47 -02:00
parent a3a8c10e4a
commit a9edd8ef19
3 changed files with 93 additions and 1 deletions

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

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']);
}
}
?>