From 167867620e8f3f258c208e36388264fec7991f36 Mon Sep 17 00:00:00 2001 From: weryques Date: Tue, 19 Dec 2017 14:24:30 -0200 Subject: [PATCH] Api updates Created taxonomies Controller; Now the entities have the method __toArray(); Created test for taxonomies Controller; Added more exceptions treatments; terms Controller was initiated; And other improvements. --- ...s-tainacan-rest-collections-controller.php | 19 ++- .../class-tainacan-rest-items-controller.php | 27 ++- ...lass-tainacan-rest-metadata-controller.php | 27 +-- ...ss-tainacan-rest-taxonomies-controller.php | 154 ++++++++++++++++++ .../class-tainacan-rest-terms-controller.php | 49 ++++++ src/api/tainacan-rest-creator.php | 1 + .../entities/class-tainacan-entity.php | 21 ++- .../class-tainacan-item-metadata-entity.php | 10 +- src/classes/entities/class-tainacan-item.php | 2 +- tests/test-api-collections.php | 22 ++- tests/test-api-items.php | 14 +- tests/test-api-metadata.php | 17 +- tests/test-api-taxonomies.php | 85 ++++++++++ 13 files changed, 380 insertions(+), 68 deletions(-) create mode 100644 src/api/endpoints/class-tainacan-rest-taxonomies-controller.php create mode 100644 src/api/endpoints/class-tainacan-rest-terms-controller.php create mode 100644 tests/test-api-taxonomies.php diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php index b606b8dcd..1f1241614 100644 --- a/src/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -89,7 +89,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller { $collection_id = $request['collection_id']; $collection = $this->collections_repository->fetch($collection_id); - $response = $this->prepare_item_for_response( $collection, $request ); + $response = $this->prepare_item_for_response($collection, $request ); return new WP_REST_Response($response, 200); } @@ -111,16 +111,16 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller { while ( $item->have_posts() ) { $item->the_post(); $collection = new Entities\Collection($item->post); - array_push($collections_as_json, $collection->__toJSON()); + array_push($collections_as_json, $collection->__toArray()); } wp_reset_postdata(); } - return json_encode($collections_as_json); + return $collections_as_json; } elseif(!empty($item)){ - return $item->__toJSON(); + return $item->__toArray(); } return $item; @@ -165,6 +165,13 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller { public function create_item( $request ) { $request = json_decode($request->get_body(), true); + if(empty($request)){ + return new WP_REST_Response([ + 'error_message' => __('Body can not be empty.', 'tainacan'), + 'collection' => $request + ], 400); + } + try { $prepared_post = $this->prepare_item_for_database( $request ); } catch (\Error $exception){ @@ -174,13 +181,13 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller { if($prepared_post->validate()) { $collection = $this->collections_repository->insert( $prepared_post ); - return new WP_REST_Response($collection->__toJSON(), 201); + return new WP_REST_Response($collection->__toArray(), 201); } return new WP_REST_Response([ 'error_message' => __('One or more values are invalid.', 'tainacan'), 'errors' => $prepared_post->get_errors(), - 'collection' => $prepared_post->__toJSON() + 'collection' => $prepared_post->__toArray() ], 400); } diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index e45906889..7eb5d23b6 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -37,7 +37,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { array( 'methods' => WP_REST_Server::READABLE, 'callback' => array($this, 'get_items'), - //'permission_callback' => array($this, 'get_items_permissions_check'), + 'permission_callback' => array($this, 'get_items_permissions_check'), 'args' => $this->get_collection_params(), ), array( @@ -83,15 +83,15 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { while ( $item->have_posts() ) { $item->the_post(); $ite = new Entities\Item($item->post); - array_push($items_as_array, $ite->__toJSON()); + array_push($items_as_array, $ite->__toArray()); } wp_reset_postdata(); } - return json_encode($items_as_array); + return $items_as_array; } elseif(!empty($item)){ - return $item->__toJSON(); + return $item->__toArray(); } return $item; @@ -139,6 +139,14 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { return false; } + public function get_items_permissions_check( $request ) { + if(current_user_can('read')){ + return true; + } + + return false; + } + /** * @param WP_REST_Request $request * @@ -185,6 +193,13 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { $collection_id = $request['collection_id']; $item = json_decode($request->get_body(), true); + if(empty($item)){ + return new WP_REST_Response([ + 'error_message' => __('Body can not be empty.', 'tainacan'), + 'item' => $item + ], 400); + } + try { $metadata = $this->prepare_item_for_database( [ $item, $collection_id ] ); } catch (\Error $exception){ @@ -197,14 +212,14 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { $item_metadata = new Entities\Item_Metadata_Entity($item, $metadata ); $metadata_added = $this->item_metadata->insert( $item_metadata ); - return new WP_REST_Response($metadata_added->get_item()->__toJSON(), 201 ); + return new WP_REST_Response($metadata_added->get_item()->__toArray(), 201 ); } return new WP_REST_Response([ 'error_message' => __('One or more values are invalid.', 'tainacan'), 'errors' => $this->item->get_errors(), - 'item' => $this->item->__toJSON() + 'item' => $this->item->__toArray() ], 400); } diff --git a/src/api/endpoints/class-tainacan-rest-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-metadata-controller.php index 560d5d263..90a376758 100644 --- a/src/api/endpoints/class-tainacan-rest-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-metadata-controller.php @@ -118,16 +118,16 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { $metadata_added = $this->item_metadata_repository->insert($item_meta); } - return new WP_REST_Response($metadata_added->get_metadata()->__toJSON(), 201); + return new WP_REST_Response($metadata_added->get_metadata()->__toArray(), 201); } else { - return new WP_REST_Response($this->metadata->__toJSON(), 201); + return new WP_REST_Response($this->metadata->__toArray(), 201); } } else { return new WP_REST_Response([ 'error_message' => __('One or more values are invalid.', 'tainacan'), 'errors' => $this->metadata->get_errors(), - 'metadata' => $this->metadata->__toJSON(), + 'metadata' => $this->metadata->__toArray(), ], 400); } } elseif (!empty($request['item_id']) && !empty($request->get_body())){ @@ -146,16 +146,19 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { if($item_metadata->validate()) { $metadata_updated = $this->item_metadata_repository->insert( $item_metadata ); - return new WP_REST_Response( $metadata_updated->__toJSON(), 201 ); + return new WP_REST_Response( $metadata_updated->__toArray(), 201 ); } else { return new WP_REST_Response([ 'error_message' => __('One or more values are invalid.', 'tainacan'), 'errors' => $item_metadata->get_errors(), - 'item_metadata' => $item_metadata->__toJSON(), + 'item_metadata' => $item_metadata->__toArray(), ], 400); } } else { - return new WP_REST_Response($request->get_body(), 400); + return new WP_REST_Response([ + 'error_message' => __('Body can not be empty.', 'tainacan'), + 'item' => $request->get_body() + ], 400); } } @@ -168,19 +171,21 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { } public function prepare_item_for_response( $item, $request ) { - $metadata_as_json = []; + $metadata_as = []; if($request['item_id']) { foreach ( $item as $metadata ) { - $metadata_as_json[] = $metadata->__toJSON(); + $metadata_as[] = $metadata->__toArray(); } - } else { + } else if($request['collection_id']){ + foreach ( $item as $metadata ) { - $metadata_as_json[] = $metadata->__toJSON(); + $metadata_as[] = $metadata->__toArray(); } } - return $metadata_as_json; + //var_dump($metadata_as); + return $metadata_as; } public function get_items( $request ) { diff --git a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php new file mode 100644 index 000000000..5a72f6759 --- /dev/null +++ b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php @@ -0,0 +1,154 @@ +namespace = 'tainacan/v2'; + $this->rest_base = 'taxonomies'; + + $this->taxonomy = new Entities\Taxonomy(); + $this->taxonomy_repository = new Repositories\Taxonomies(); + + add_action('rest_api_init', array($this, 'register_routes')); + } + + public function register_routes() { + register_rest_route( + $this->namespace, '/' . $this->rest_base, + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array($this, 'get_items'), + 'permission_callback' => array($this, 'get_items_permissions_check'), + ), + array( + '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[\d]+)', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array($this, 'get_item'), + 'permission_callback' => array($this, 'get_item_permissions_check'), + ), + array( + 'methods' => WP_REST_Server::DELETABLE, + 'callback' => array($this, 'delete_item'), + 'permission_callback' => array($this, 'delete_item_permissions_check'), + ) + ) + ); + } + + public function prepare_item_for_response( $item, $request ) { + $taxonomies = []; + + if($request['taxonomy_id']){ + return $item->__toArray(); + } + + foreach ( $item as $it ) { + $taxonomies[] = $it->__toArray(); + } + + return $taxonomies; + } + + public function prepare_item_for_database( $request ) { + foreach ($request as $key => $value){ + $set_ = 'set_' . $key; + $this->taxonomy->$set_($value); + } + } + + public function get_item( $request ) { + $taxonomy_id = $request['taxonomy_id']; + + $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id); + + $taxonomy_prepared = $this->prepare_item_for_response($taxonomy, $request); + + return new WP_REST_Response($taxonomy_prepared, 200); + } + + public function get_item_permissions_check( $request ) { + if(current_user_can('read')){ + return true; + } + + return false; + } + + public function delete_item( $request ) { + return parent::delete_item( $request ); // TODO: Change the autogenerated stub + } + + public function delete_item_permissions_check( $request ) { + if (current_user_can('delete_posts')){ + return true; + } + + return false; + } + + public function get_items( $request ) { + $taxonomies = $this->taxonomy_repository->fetch([], 'OBJECT'); + + $taxonomies_prepared = $this->prepare_item_for_response($taxonomies, $request); + + return new WP_REST_Response($taxonomies_prepared, 200); + } + + public function get_items_permissions_check( $request ) { + if (current_user_can('read')){ + return true; + } + + return false; + } + + public function create_item( $request ) { + $body = json_decode($request->get_body(), true); + + if(!empty($body)){ + $this->prepare_item_for_database($body); + + if($this->taxonomy->validate()){ + $taxonomy = $this->taxonomy_repository->insert($this->taxonomy); + + return new WP_REST_Response($taxonomy->__toArray(), 201); + } else { + return new WP_REST_Response([ + 'error_message' => __('One or more values are invalid.', 'tainacan'), + 'errors' => $this->taxonomy->get_errors(), + 'item_metadata' => $this->taxonomy->__toArray(), + ], 400); + } + } else { + return new WP_REST_Response([ + 'error_message' => __('Body can not be empty.', 'tainacan'), + ], 400); + } + } + + public function create_item_permissions_check( $request ) { + if(current_user_can('edit_posts')){ + return true; + } + + return false; + } + +} + +?> \ No newline at end of file diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php new file mode 100644 index 000000000..970d155e4 --- /dev/null +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -0,0 +1,49 @@ +namespace = 'tainacan/v2'; + $this->rest_base = 'terms'; + + $this->term = new Entities\Term(); + $this->terms_repository = new Repositories\Terms(); + $this->taxonomy = new Entities\Taxonomy(); + $this->taxonomy_repository = new Repositories\Taxonomies(); + + add_action('rest_api_init', array($this, 'register_routes')); + } + + public function register_routes() { + register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P[\d]+)', + array( + array( + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => array($this, 'create_item'), + 'permission_callback' => array($this, 'create_item_permissions_check') + ) + ) + ); + } + + public function create_item( $request ) { + return parent::create_item( $request ); // TODO: Change the autogenerated stub + } + + public function create_item_permissions_check( $request ) { + if(current_user_can('edit_posts')){ + return true; + } + + return false; + } +} + +?> \ No newline at end of file diff --git a/src/api/tainacan-rest-creator.php b/src/api/tainacan-rest-creator.php index 3a5203e50..af10e8194 100644 --- a/src/api/tainacan-rest-creator.php +++ b/src/api/tainacan-rest-creator.php @@ -3,6 +3,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(); // Add here other endpoints imports ?> \ No newline at end of file diff --git a/src/classes/entities/class-tainacan-entity.php b/src/classes/entities/class-tainacan-entity.php index ac6ea53af..9f48de755 100644 --- a/src/classes/entities/class-tainacan-entity.php +++ b/src/classes/entities/class-tainacan-entity.php @@ -242,16 +242,19 @@ class Entity { return true; } + public function __toArray(){ + global ${$this->repository}; + $map = ${$this->repository}->get_map(); + + $attributes = []; + foreach($map as $prop => $content) { + $attributes[$prop] = $this->get_mapped_property($prop); + } + + return $attributes; + } public function __toJSON(){ - global ${$this->repository}; - $map = ${$this->repository}->get_map(); - - $attributes = []; - foreach($map as $prop => $content) { - $attributes[$prop] = $this->get_mapped_property($prop); - } - - return json_encode($attributes, JSON_NUMERIC_CHECK); + return json_encode($this->__toArray(), JSON_NUMERIC_CHECK); } } \ No newline at end of file diff --git a/src/classes/entities/class-tainacan-item-metadata-entity.php b/src/classes/entities/class-tainacan-item-metadata-entity.php index ca1a90f12..7a1e13328 100644 --- a/src/classes/entities/class-tainacan-item-metadata-entity.php +++ b/src/classes/entities/class-tainacan-item-metadata-entity.php @@ -26,12 +26,12 @@ class Item_Metadata_Entity extends Entity { return 'Hello, I\'m the Item Metadata Entity'; } - public function __toJSON(){ - $json['value'] = $this->get_value(); - $json['item'] = $this->get_item()->__toJSON(); - $json['metadata'] = $this->get_metadata()->__toJSON(); + public function __toArray(){ + $as_array['value'] = $this->get_value(); + $as_array['item'] = $this->get_item()->__toArray(); + $as_array['metadata'] = $this->get_metadata()->__toArray(); - return json_encode($json); + return $as_array; } /** diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index 251c25f5e..572b335d7 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -17,7 +17,7 @@ class Item extends Entity { protected $repository = 'Tainacan_Items'; public function __toString(){ - return 'Hello, my name is '. $this->get_name(); + return 'Hello, my name is '. $this->get_title(); } /** diff --git a/tests/test-api-collections.php b/tests/test-api-collections.php index a9d1f8625..82ece09f4 100644 --- a/tests/test-api-collections.php +++ b/tests/test-api-collections.php @@ -40,16 +40,16 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch( $request ); $this->assertEquals( 201, $response->get_status() ); - - $collection = json_decode($response->get_data()); - $id = $collection->id; + + $collection = $response->get_data(); + $id = $collection['id']; $requestGet = new \WP_REST_Request( 'GET', $this->namespace . '/collections/' . $id ); $responseGet = $this->server->dispatch( $requestGet ); $this->assertEquals( 200, $responseGet->get_status() ); - $data = json_decode($responseGet->get_data(), true); + $data = $responseGet->get_data(); $this->assertEquals('TesteJsonAdd', $data['name']); } @@ -72,13 +72,11 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals( 200, $response->get_status() ); - $data = json_decode($response->get_data()); + $data = $response->get_data(); //$data is a valid json? - $this->assertTrue(json_last_error() === JSON_ERROR_NONE); - - $this->assertContainsOnly('string', $data); - - $one_collection = json_decode($data[0], true); + //$this->assertTrue(json_last_error() === JSON_ERROR_NONE); + + $one_collection = $data[0]; $this->assertEquals('testeApi', $one_collection['name']); } @@ -98,7 +96,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals(200, $response->get_status()); - $data = json_decode($response->get_data(), true); + $data = $response->get_data(); $this->assertEquals($collection1->get_id(), $data['id']); @@ -123,7 +121,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals(200, $response->get_status()); - $data = json_decode($response->get_data(), true); + $data = $response->get_data(); $this->assertEquals($collection2->get_id(), $data['id']); diff --git a/tests/test-api-items.php b/tests/test-api-items.php index bd5adc47b..3f3b69d88 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -26,7 +26,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals(201, $response->get_status()); - $data = json_decode($response->get_data(), true); + $data = $response->get_data(); $this->assertEquals('Vue JS 2', $data['title']); } @@ -66,12 +66,10 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals(200, $response->get_status()); - $data = json_decode($response->get_data(), true); + $data = $response->get_data(); - $this->assertContainsOnly('string', $data); - - $first_item = json_decode($data[0], true); - $second_item = json_decode($data[1], true); + $first_item = $data[0]; + $second_item = $data[1]; $this->assertEquals($item2->get_title(), $first_item['title']); $this->assertEquals($item1->get_title(), $second_item['title']); @@ -103,7 +101,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals(200, $response->get_status()); - $data = json_decode($response->get_data(), true); + $data = $response->get_data(); $this->assertEquals($item1->get_title(), $data['title']); @@ -136,7 +134,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals(200, $response->get_status()); - $data = json_decode($response->get_data(), true); + $data = $response->get_data(); $this->assertEquals($item2->get_title(), $data['title']); diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index 937108dd7..73c2fa7b1 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -37,7 +37,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); - $metadata_added = json_decode($response->get_data(), true); + $metadata_added = $response->get_data(); $this->assertEquals('Moeda', $metadata_added['name']); @@ -58,8 +58,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); - $item_metadata_updated = json_decode($response->get_data(), true); - $metadata = json_decode($item_metadata_updated['metadata'], true); + $item_metadata_updated = $response->get_data(); + + $metadata = $item_metadata_updated['metadata']; $this->assertEquals($metadata_added['id'], $metadata['id']); @@ -122,9 +123,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $data = $response->get_data(); - $this->assertContainsOnly('string', $data); - - $metadata = json_decode($data[0], true); + $metadata = $data[0]; $this->assertEquals('Data', $metadata['name']); @@ -139,10 +138,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $data = $response->get_data(); - $this->assertContainsOnly('string', $data); - - $item_metadata = json_decode($data[0], true); - $metadata = json_decode($item_metadata['metadata'], true); + $item_metadata = $data[0]; + $metadata = $item_metadata['metadata']; $this->assertEquals('Data', $metadata['name']); $this->assertEquals('12/12/2017', $item_metadata['value']); diff --git a/tests/test-api-taxonomies.php b/tests/test-api-taxonomies.php new file mode 100644 index 000000000..df39c0785 --- /dev/null +++ b/tests/test-api-taxonomies.php @@ -0,0 +1,85 @@ + 'Nome', + 'description' => 'desc', + 'allow_insert' => 'yes', + 'status' => 'publish' + ]); + + $request = new \WP_REST_Request( + 'POST', $this->namespace . '/taxonomies' + ); + $request->set_body($taxonomy_json); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertEquals('Nome', $data['name']); + } + + public function test_get_taxonomy_by_id(){ + $taxonomy = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => '1genero', + 'description' => 'tipos', + 'allow_insert' => 'yes' + ), + true + ); + + $request = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomies/' . $taxonomy->get_id() + ); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertEquals($taxonomy->get_name(), $data['name']); + } + + public function test_get_all_taxonomies(){ + $taxonomy1 = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => '1genero', + 'description' => 'tipos de musica', + 'allow_insert' => 'yes', + 'status' => 'publish' + ), + true + ); + + $taxonomy2 = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => '2genero', + 'description' => 'tipos', + 'allow_insert' => 'yes', + 'status' => 'publish' + ), + true + ); + + $request = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomies' + ); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertEquals($taxonomy1->get_name(), $data[1]['name']); + $this->assertEquals($taxonomy2->get_name(), $data[0]['name']); + } +} + +?> \ No newline at end of file