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

This commit is contained in:
Jacson Passold 2018-01-18 12:34:23 -02:00
commit 2301029bf8
8 changed files with 542 additions and 2 deletions

View File

@ -38,6 +38,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
'methods' => WP_REST_Server::CREATABLE, 'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'), 'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check') 'permission_callback' => array($this, 'create_item_permissions_check')
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check')
) )
)); ));
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<filter_id>[\d]+)', array( register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<filter_id>[\d]+)', array(
@ -50,6 +55,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
'methods' => WP_REST_Server::EDITABLE, 'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'), 'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check') 'permission_callback' => array($this, 'update_item_permissions_check')
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_item_permissions_check')
) )
)); ));
} }
@ -132,6 +142,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
return $this->filter_repository->can_edit($this->filter); return $this->filter_repository->can_edit($this->filter);
} }
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function delete_item( $request ) { public function delete_item( $request ) {
$filter_id = $request['filter_id']; $filter_id = $request['filter_id'];
@ -151,11 +166,21 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
], 400); ], 400);
} }
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function delete_item_permissions_check( $request ) { public function delete_item_permissions_check( $request ) {
$filter = $this->filter_repository->fetch($request['filter_id']); $filter = $this->filter_repository->fetch($request['filter_id']);
return $this->filter_repository->can_delete($filter); return $this->filter_repository->can_delete($filter);
} }
/**
* @param $request
*
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) { public function update_item( $request ) {
$filter_id = $request['filter_id']; $filter_id = $request['filter_id'];
@ -180,9 +205,80 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
} }
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function update_item_permissions_check( $request ) { public function update_item_permissions_check( $request ) {
$filter = $this->filter_repository->fetch($request['filter_id']); $filter = $this->filter_repository->fetch($request['filter_id']);
return $this->filter_repository->can_edit($filter); return $this->filter_repository->can_edit($filter);
} }
/**
* @param mixed $object
* @param WP_REST_Request $request
*
* @return array|mixed|WP_Error|WP_REST_Response
*/
public function prepare_item_for_response( $object, $request ) {
if(is_array($object)){
$filters = [];
foreach ($object as $item){
$filters[] = $item->__toArray();
}
return $filters;
}
return $object;
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$filters = $this->filter_repository->fetch([], 'OBJECT');
$response = $this->prepare_item_for_response($filters, $request);
return new WP_REST_Response($response, 200);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function get_items_permissions_check( $request ) {
return $this->filter_repository->can_read($this->filter);
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$filter_id = $request['filter_id'];
$filter = $this->filter_repository->fetch($filter_id);
return new WP_REST_Response($filter->__toArray(), 200);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function get_item_permissions_check( $request ) {
$filter = $this->filter_repository->fetch($request['filter_id']);
return $this->filter_repository->can_read($filter);
}
} }
?> ?>

View File

@ -48,6 +48,11 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller {
'methods' => WP_REST_Server::DELETABLE, 'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'), 'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_item_permissions_check'), 'permission_callback' => array($this, 'delete_item_permissions_check'),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check')
) )
) )
); );
@ -227,6 +232,43 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller {
return $this->taxonomy_repository->can_edit($this->taxonomy); return $this->taxonomy_repository->can_edit($this->taxonomy);
} }
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
$taxonomy_id = $request['taxonomy_id'];
$body = json_decode($request->get_body(), true);
if(!empty($body)){
$attributes = ['ID' => $taxonomy_id];
foreach ($body as $att => $value){
$attributes[$att] = $value;
}
$updated_taxonomy = $this->taxonomy_repository->update($attributes);
return new WP_REST_Response($updated_taxonomy->__toArray(), 200);
}
return new WP_REST_Response([
'error_message' => 'The body could not be empty',
'body' => $body
], 400);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function update_item_permissions_check( $request ) {
$taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']);
return $this->taxonomy_repository->can_edit($taxonomy);
}
} }
?> ?>

View File

@ -31,6 +31,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
'methods' => WP_REST_Server::CREATABLE, 'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'), 'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check') 'permission_callback' => array($this, 'create_item_permissions_check')
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check')
) )
) )
); );
@ -39,7 +44,17 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
array( array(
'methods' => WP_REST_Server::DELETABLE, 'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'), 'callback' => array($this, 'delete_item'),
'permission_callbacl' => array($this, 'delete_item_permissions_check') 'permission_callback' => array($this, 'delete_item_permissions_check')
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check')
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_item_permissions_check')
) )
) )
); );
@ -112,6 +127,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
return $this->terms_repository->can_edit($this->term); return $this->terms_repository->can_edit($this->term);
} }
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function delete_item( $request ) { public function delete_item( $request ) {
$term_id = $request['term_id']; $term_id = $request['term_id'];
$taxonomy_id = $request['taxonomy_id']; $taxonomy_id = $request['taxonomy_id'];
@ -131,10 +151,137 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
return new WP_REST_Response($is_deleted, 200); return new WP_REST_Response($is_deleted, 200);
} }
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function delete_item_permissions_check( $request ) { public function delete_item_permissions_check( $request ) {
$term = $this->terms_repository->fetch($request['term_id']); $term = new Entities\Term($this->terms_repository->fetch($request['term_id']));
return $this->terms_repository->can_delete($term); return $this->terms_repository->can_delete($term);
} }
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
$term_id = $request['term_id'];
$taxonomy_id = $request['taxonomy_id'];
$body = json_decode($request->get_body(), true);
if(!empty($body)){
$taxonomy_name = $this->taxonomy_repository->fetch($taxonomy_id)->get_db_identifier();
$identifiers = [
'term_id' => $term_id,
'tax_name' => $taxonomy_name
];
$attributes = [];
foreach ($body as $att => $value){
$attributes[$att] = $value;
}
$updated_term = $this->terms_repository->update([$attributes, $identifiers]);
return new WP_REST_Response($updated_term->__toArray(), 200);
}
return new WP_REST_Response([
'error_message' => 'The body could not be empty',
'body' => $body
], 400);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function update_item_permissions_check( $request ) {
$term = new Entities\Term($this->terms_repository->fetch($request['term_id']));
return $this->terms_repository->can_edit($term);
}
/**
* @param mixed $item
* @param WP_REST_Request $request
*
* @return array|mixed|WP_Error|WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
if(is_array($item)){
$prepared = [];
foreach ($item as $term){
$prepared[] = $term->__toArray();
}
return $prepared;
}
return $item;
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$taxonomy_id = $request['taxonomy_id'];
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
$args = json_decode($request->get_body(), true);
$terms = $this->terms_repository->fetch($args, $taxonomy);
$prepared_terms = $this->prepare_item_for_response($terms, $request);
return new WP_REST_Response($prepared_terms, 200);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function get_items_permissions_check( $request ) {
$taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']);
return $this->taxonomy_repository->can_read($taxonomy);
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$term_id = $request['term_id'];
$tax_id = $request['taxonomy_id'];
$taxonomy = $this->taxonomy_repository->fetch($tax_id);
$term = $this->terms_repository->fetch($term_id, $taxonomy);
return new WP_REST_Response($term->__toArray(), 200);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function get_item_permissions_check( $request ) {
$taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']);
return $this->taxonomy_repository->can_read($taxonomy);
}
} }
?> ?>

View File

@ -180,7 +180,22 @@ class Taxonomies extends Repository {
} }
public function update($object){ public function update($object){
$map = $this->get_map();
$entity = [];
foreach ($object as $key => $value) {
if($key != 'ID') {
$entity[$map[$key]['map']] = $value ;
} elseif ($key == 'ID'){
$entity[$key] = (int) $value;
}
}
$updated_taxonomy = new Entities\Taxonomy(wp_update_post($entity));
$updated_taxonomy->register_taxonomy();
return $updated_taxonomy;
} }
public function delete($args){ public function delete($args){

View File

@ -156,7 +156,20 @@ class Terms extends Repository {
} }
public function update($object){ public function update($object){
$map = $this->get_map();
$entity = [];
foreach ($object[0] as $key => $value) {
if($key != 'ID') {
$entity[$map[$key]['map']] = $value ;
}
}
$term_tax_ids = wp_update_term($object[1]['term_id'], $object[1]['tax_name'], $entity);
$term_id = (int) $term_tax_ids['term_id'];
return new Entities\Term($term_id, $object[1]['tax_name']);
} }
public function delete($args){ public function delete($args){

View File

@ -131,6 +131,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
array( array(
'name' => 'Metadata filtered', 'name' => 'Metadata filtered',
'description' => 'Is filtered', 'description' => 'Is filtered',
'collection_id' => $collection->get_id()
) )
); );
@ -165,6 +166,87 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
$this->assertNotEquals('filtro', $data['name']); $this->assertNotEquals('filtro', $data['name']);
$this->assertEquals('Faceta', $data['name']); $this->assertEquals('Faceta', $data['name']);
} }
public function test_fetch_filters(){
$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',
'collection_id' => $collection->get_id()
)
);
$metadata2 = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'Other filtered',
'description' => 'Is filtered',
'collection_id' => $collection->get_id()
)
);
$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,
'status' => 'publish'
),
true
);
$filter2 = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'filtro2',
'collection' => $collection,
'description' => 'descricao',
'metadata' => $metadata2,
'filter_type' => $filter_type,
'status' => 'publish'
),
true
);
$request = new \WP_REST_Request('GET', $this->namespace . '/filters');
$response = $this->server->dispatch($request);
$data = $response->get_data();
$first_filter = $data[0];
$second_filter = $data[1];
$this->assertEquals($filter2->get_name(), $first_filter['name']);
$this->assertEquals($filter->get_name(), $second_filter['name']);
#### FETCH A FILTER ####
$request = new \WP_REST_Request(
'GET', $this->namespace . '/filters/' . $filter->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals('filtro', $data['name']);
}
} }
?> ?>

View File

@ -122,6 +122,44 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals($taxonomy1->get_name(), $data[1]['name']); $this->assertEquals($taxonomy1->get_name(), $data[1]['name']);
$this->assertEquals($taxonomy2->get_name(), $data[0]['name']); $this->assertEquals($taxonomy2->get_name(), $data[0]['name']);
} }
public function test_update_taxonomy(){
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'Gender',
'description' => 'Music types',
'allow_insert' => 'yes',
'status' => 'publish'
),
true
);
$new_attributes = json_encode([
'name' => 'People',
'description' => 'Male or Female'
]);
$request = new \WP_REST_Request('PATCH', $this->namespace . '/taxonomies/' . $taxonomy->get_id());
$request->set_body($new_attributes);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$args=array(
'name' => $taxonomy->get_db_identifier()
);
$output = 'objects';
$tax = get_taxonomies($args, $output);
$this->assertNotEquals($taxonomy->get_name(), $data['name']);
$this->assertEquals('People', $data['name']);
$this->assertEquals('People', $tax[$taxonomy->get_db_identifier()]->label);
}
} }
?> ?>

View File

@ -70,6 +70,113 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
$this->assertNull($term); $this->assertNull($term);
} }
public function test_update_term(){
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => '1genero',
'description' => 'tipos de musica',
'allow_insert' => 'yes',
'status' => 'publish'
),
true
);
$term = $this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $taxonomy->get_db_identifier(),
'name' => 'Rock',
'user' => get_current_user_id(),
),
true
);
$new_attributes = json_encode([
'name' => 'Trap'
]);
$request = new \WP_REST_Request(
'PATCH', $this->namespace . '/terms/' . $term . '/taxonomy/' . $taxonomy->get_id()
);
$request->set_body($new_attributes);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertNotEquals('Rock', $data['name']);
$this->assertEquals('Trap', $data['name']);
$this->assertEquals($taxonomy->get_db_identifier(), $data['taxonomy']);
}
public function test_fetch_terms(){
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'Gender',
'description' => 'Music types',
'allow_insert' => 'yes',
'status' => 'publish'
),
true
);
$this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $taxonomy->get_db_identifier(),
'name' => 'Rock',
'user' => get_current_user_id(),
),
true
);
$term2 = $this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $taxonomy->get_db_identifier(),
'name' => 'Trap',
'user' => get_current_user_id(),
),
true
);
$show_empty = json_encode([
'hide_empty' => false
]);
$request = new \WP_REST_Request(
'GET', $this->namespace . '/terms/taxonomy/' . $taxonomy->get_id()
);
$request->set_body($show_empty);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$termA = $data[0];
$termB = $data[1];
$this->assertEquals('Trap', $termB['name']);
$this->assertEquals('Rock', $termA['name']);
#### FETCH A TERM ####
$request = new \WP_REST_Request(
'GET', $this->namespace . '/terms/' . $term2 . '/taxonomy/' . $taxonomy->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals('Trap', $data['name']);
}
} }
?> ?>