diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php index ea6f8d3c7..99776cf51 100644 --- a/src/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -101,6 +101,20 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller { return new WP_REST_Response($response, 200); } + protected function get_only_needed_attributes($collection){ + $collection_temp = [ + 'id' => $collection->get_id(), + 'name' => $collection->get_name(), + 'description' => $collection->get_description(), + 'featured_image' => $collection->get_featured_img(), + 'columns' => $collection->get_columns(), + 'creation_date' => $collection->get_creation_date(), + 'modification_date' => $collection->get_modification_date(), + ]; + + return $collection_temp; + } + /** * * Receive a WP_Query or a Collection object and return both in JSON @@ -112,22 +126,25 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller { */ public function prepare_item_for_response($item, $request){ if($item instanceof WP_Query){ - $collections_as_json = []; + $collections = []; if ($item->have_posts()) { while ( $item->have_posts() ) { $item->the_post(); $collection = new Entities\Collection($item->post); - array_push($collections_as_json, $collection->__toArray()); + + $collection_resumed = $this->get_only_needed_attributes($collection); + + array_push($collections, $collection_resumed); } wp_reset_postdata(); } - return $collections_as_json; + return $collections; } elseif(!empty($item)){ - return $item->__toArray(); + return $this->get_only_needed_attributes($item); } return $item; diff --git a/src/classes/tainacan-creator.php b/src/classes/tainacan-creator.php index 88f4a0492..e4ea0de81 100644 --- a/src/classes/tainacan-creator.php +++ b/src/classes/tainacan-creator.php @@ -9,7 +9,7 @@ const REPOSITORIES_DIR = __DIR__ . '/repositories/'; const TRAITS_DIR = __DIR__ . '/traits/'; const VENDOR_DIR = __DIR__ . '/../vendor/'; const ENDPOINTS_DIR = __DIR__ . '/../api/endpoints/'; -const HELPERS_DIR = __DIR__ . '/../helpers/'; +const HELPERS_DIR = __DIR__ . '/../helpers/'; const DIRS = [ CLASSES_DIR, diff --git a/tests/test-api-collections.php b/tests/test-api-collections.php index f572101f2..19fd02886 100644 --- a/tests/test-api-collections.php +++ b/tests/test-api-collections.php @@ -55,7 +55,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { } public function test_fetch_collections(){ - $x = $this->tainacan_entity_factory->create_entity( + $this->tainacan_entity_factory->create_entity( 'collection', array( 'name' => 'testeApi', @@ -66,6 +66,17 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { true ); + $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Other', + 'description' => 'adasdasdsa', + 'default_order' => 'DESC', + 'status' => 'publish' + ), + true + ); + $request = new \WP_REST_Request( 'GET', $this->namespace . '/collections' ); $response = $this->server->dispatch( $request ); @@ -76,8 +87,11 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { //$data is a valid json? //$this->assertTrue(json_last_error() === JSON_ERROR_NONE); - $one_collection = $data[0]; + $other_collection = $data[0]; + $one_collection = $data[1]; + $this->assertEquals('testeApi', $one_collection['name']); + $this->assertEquals('Other', $other_collection['name']); } public function test_delete_or_trash_a_collection(){