Test for filters delete or trash and started delete endpoint

This commit is contained in:
weryques 2018-01-12 14:17:52 -02:00
parent 225249adc8
commit 43b15b55ba
3 changed files with 122 additions and 5 deletions

View File

@ -35,11 +35,18 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
public function register_routes() {
register_rest_route($this->namespace, '/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check')
)
));
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<filter_id>[\d]+)', array(
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_item_permissions_check')
)
));
}
@ -55,6 +62,20 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
$metadata_id = $body['metadata_id'];
$filter = $body['filter'];
$received_type = $body['filter_type'];
if(empty($received_type)){
throw new \InvalidArgumentException('The type can\'t be empty');
} elseif(!strrchr($received_type, '_')){
$received_type = ucfirst(strtolower($received_type));
} else {
$received_type = ucwords(strtolower($received_type), '_');
}
$type = "Tainacan\Filter_Types\\$received_type";
$filter_type = new $type();
foreach ($filter as $attribute => $value){
try {
$set_ = 'set_'. $attribute;
@ -66,6 +87,7 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
$this->filter->set_collection_id($collection_id);
$this->filter->set_metadata($metadata_id);
$this->filter->set_filter_type($filter_type);
}
/**
@ -104,6 +126,29 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
public function create_item_permissions_check( $request ) {
return $this->filter_repository->can_edit($this->filter);
}
public function delete_item( $request ) {
$filter_id = $request['filter_id'];
$is_permanently = json_decode($request->get_body(), true);
if(!empty($is_permanently)){
$args = [$filter_id, $is_permanently];
$filter = $this->filter_repository->delete($args);
return new WP_REST_Response($filter->__toArray(), 200);
}
return new WP_REST_Response([
'error_message' => 'The body could not be empty',
'body' => $request->get_body()
], 400);
}
public function delete_item_permissions_check( $request ) {
return parent::delete_item_permissions_check( $request ); // TODO: Change the autogenerated stub
}
}
?>

View File

@ -154,7 +154,7 @@ class Filters extends Repository {
return new Entities\Filter($metadata->WP_Post);
}*/
public function delete($object){
public function delete($args){
}

View File

@ -26,10 +26,10 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
array(
'collection_id' => $collection->get_id(),
'metadata_id' => $metadata->get_id(),
'filter_type' => 'range',
'filter' => [
'name' => 'Filter name',
'description' => 'This is RANGE!',
'filter_type' => 'range'
]
)
);
@ -42,10 +42,82 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
$data = $response->get_data();
$this->assertEquals('range', $data['filter_type']);
$this->assertEquals('Tainacan\Filter_Types\Range', $data['filter_type']);
$this->assertEquals('Filter name', $data['name']);
}
public function test_delete_or_trash_filter(){
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Collection filtered',
'description' => 'Is filtered'
),
true
);
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'Metadata filtered',
'description' => 'Is filtered'
)
);
$filter_type = $this->tainacan_filter_factory->create_filter('range');
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'filtro',
'collection' => $collection,
'description' => 'descricao',
'metadata' => $metadata,
'filter_type' => $filter_type
),
true
);
$is_permanently = json_encode([
'is_permanently' => true
]);
$request = new \WP_REST_Request(
'DELETE', $this->namespace . '/filters/' . $filter->get_id());
$request->set_body($is_permanently);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->markTestIncomplete('Incomplete');
$this->assertEquals('filtro', $data['name']);
$filter_status = get_post($filter->get_id())->post_status;
$this->assertEquals('trash', $filter_status);
##### TRASH #####
$is_permanently = json_encode([
'is_permanently' => false
]);
$request = new \WP_REST_Request(
'DELETE', $this->namespace . '/filters/' . $filter->get_id());
$request->set_body($is_permanently);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals('filtro', $data['name']);
$this->assertNull(get_post($filter->get_id()));
}
}
?>