diff --git a/src/classes/api/endpoints/class-tainacan-rest-metadata-section-controller.php b/src/classes/api/endpoints/class-tainacan-rest-metadata-section-controller.php index 43c01c0a0..7724053e5 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-metadata-section-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-metadata-section-controller.php @@ -8,8 +8,8 @@ use Tainacan\Repositories; class REST_Metadata_Section_Controller extends REST_Controller { public function __construct() { - $this->rest_base = 'metadatasection'; parent::__construct(); + $this->rest_base = 'metadata-sections'; add_action('init', array(&$this, 'init_objects'), 11); } @@ -19,7 +19,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { * @throws \Exception */ public function init_objects() { - $this->metadatum_repository = Repositories\Metadata::get_instance(); + $this->metadatum_section_repository = Repositories\Metadata_Section::get_instance(); } /** @@ -83,6 +83,29 @@ class REST_Metadata_Section_Controller extends REST_Controller { 'schema' => [$this, 'get_schema'] ) ); + register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base . '/(?P[\d]+)/metadatum', + array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array($this, 'get_metadatum_list'), + 'permission_callback' => array($this, 'get_items_permissions_check'), + 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE), + ), + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array($this, 'add_metadatum'), + 'permission_callback' => array($this, 'update_item_permissions_check'), + 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE), + ), + array( + 'methods' => \WP_REST_Server::DELETABLE, + 'callback' => array($this, 'delete_metadatum'), + 'permission_callback' => array($this, 'update_item_permissions_check'), + 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE), + ), + 'schema' => [$this, 'get_schema'] + ) + ); } /** @@ -101,7 +124,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { $number = $request['number']; } - $result = $this->metadatum_repository->fetch($metadatum_id, 'OBJECT'); + $result = $this->metadatum_section_repository->fetch($metadatum_id, 'OBJECT'); if (! $result instanceof Entities\Metadatum) { return new \WP_REST_Response([ @@ -120,7 +143,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { * @throws \Exception */ public function get_item_permissions_check( $request ) { - $metadatum = $this->metadatum_repository->fetch($request['metadatum_id']); + $metadatum = $this->metadatum_section_repository->fetch($request['metadatum_id']); if ( $metadatum instanceof Entities\Metadatum ) { return $metadatum->can_read(); @@ -130,30 +153,26 @@ class REST_Metadata_Section_Controller extends REST_Controller { } /** - * @param \WP_REST_Request $request + * @param \WP_REST_Request|string $request * - * @param null $collection_id + * @param $collection_id * * @return object|void|\WP_Error * @throws \Exception */ - public function prepare_item_for_database( $request, $collection_id = null ) { - $metadatum = new Entities\Metadatum(); - + public function prepare_item_for_database( $request, $collection_id = null) { + if($collection_id == null) { + throw new \InvalidArgumentException('You need provide a collection id'); + } + $metadatum_section = new Entities\Metadatum_Section(); $meta = json_decode( $request, true ); foreach ( $meta as $key => $value ) { $set_ = 'set_' . $key; - $metadatum->$set_( $value ); + $metadatum_section->$set_( $value ); } - - if($collection_id) { - $collection = new Entities\Collection( $collection_id ); - $metadatum->set_collection( $collection ); - } else { - $metadatum->set_collection_id( 'default' ); - } - - return $metadatum; + $collection = new Entities\Collection( $collection_id ); + $metadatum_section->set_collection( $collection ); + return $metadatum_section; } /** @@ -168,15 +187,13 @@ class REST_Metadata_Section_Controller extends REST_Controller { try { $prepared = $this->prepare_item_for_database( $request->get_body(), $collection_id ); - } catch (\Exception $exception){ + } catch (\Exception $exception) { return new \WP_REST_Response($exception->getMessage(), 400); } if($prepared->validate()) { - $metadatum = $this->metadatum_repository->insert( $prepared); - - $response = $this->prepare_item_for_response($metadatum, $request); - + $metadatum_section = $this->metadatum_section_repository->insert($prepared); + $response = $this->prepare_item_for_response($metadatum_section, $request); return new \WP_REST_Response($response, 201); } else { return new \WP_REST_Response([ @@ -185,36 +202,14 @@ class REST_Metadata_Section_Controller extends REST_Controller { 'metadatum' => $this->prepare_item_for_response($prepared, $request), ], 400); } - } elseif (!empty($request->get_body())) { - try { - $prepared = $this->prepare_item_for_database( $request->get_body() ); - } catch ( \Exception $exception ) { - return new \WP_REST_Response( $exception->getMessage(), 400 ); - } - - if ( $prepared->validate() ) { - $metadatum = $this->metadatum_repository->insert( $prepared ); - - $response = $this->prepare_item_for_response($metadatum, $request); - - return new \WP_REST_Response($response, 201); - } else { - return new \WP_REST_Response([ - 'error_message' => __('One or more values are invalid.', 'tainacan'), - 'errors' => $prepared->get_errors(), - 'metadatum' => $this->prepare_item_for_response($prepared, $request), - ], 400); - } - } - return new \WP_REST_Response([ 'error_message' => __('Body cannot be empty.', 'tainacan'), 'item' => $request->get_body() ], 400); - } + // @TODO: fix the permissions check /** * @param $request * @@ -222,17 +217,17 @@ class REST_Metadata_Section_Controller extends REST_Controller { * @throws \Exception */ public function create_item_permissions_check( $request ) { - + return true; if( isset($request['collection_id']) ) { $collection = $this->collection_repository->fetch( $request['collection_id'] ); if ( $collection instanceof Entities\Collection ) { - return $collection->user_can( 'edit_metadata' ); + return $collection->user_can( 'edit_metadata_section' ); } } else { - return current_user_can( 'tnc_rep_edit_metadata' ); + return current_user_can( 'tnc_rep_edit_metadata_section' ); } @@ -249,33 +244,10 @@ class REST_Metadata_Section_Controller extends REST_Controller { public function prepare_item_for_response( $item, $request ) { if(!empty($item)){ $item_arr = $item->_toArray(); - $item_arr['metadata_type_object'] = $item->get_metadata_type_object()->_toArray(); - - if ( isset($request['include_options_as_html']) && $request['include_options_as_html'] == 'yes' ) - $item_arr['options_as_html'] = $item->get_metadata_type_object()->get_options_as_html(); - - if ( isset($item_arr['metadata_type_options']) && isset($item_arr['metadata_type_options']['taxonomy_id']) ) { - $taxonomy = Repositories\Taxonomies::get_instance()->get_db_identifier_by_id( $item_arr['metadata_type_options']['taxonomy_id'] ); - //$taxonomy = new Entities\Taxonomy($item_arr['metadata_type_options']['taxonomy_id']); - //$item_arr['metadata_type_options']['taxonomy'] = $taxonomy->get_db_identifier(); - $item_arr['metadata_type_options']['taxonomy'] = $taxonomy; - } - if ($request['context'] === 'edit') { $item_arr['current_user_can_edit'] = $item->can_edit(); $item_arr['current_user_can_delete'] = $item->can_delete(); - ob_start(); - $item->get_metadata_type_object()->form(); - $form = ob_get_clean(); - $item_arr['edit_form'] = $form; - $item_arr['enabled'] = $item->get_enabled_for_collection(); - - if(isset($item_arr['metadata_type_options']) && isset($item_arr['metadata_type_options']['children_objects'])) { - foreach ($item_arr['metadata_type_options']['children_objects'] as $index => $children) { - $item_arr['metadata_type_options']['children_objects'][$index]['current_user_can_edit'] = $item->can_edit(); - $item_arr['metadata_type_options']['children_objects'][$index]['current_user_can_delete'] = $item->can_delete(); - } - } + // $item_arr['enabled'] = $item->get_enabled_for_collection(); } /** @@ -285,16 +257,13 @@ class REST_Metadata_Section_Controller extends REST_Controller { * * Also take care to do any permissions verification before exposing the data */ - $extra_metadata = apply_filters('tainacan-api-response-metadatum-meta', [], $request); + $extra_metadata = apply_filters('tainacan-api-response-metadatum-section-meta', [], $request); foreach ($extra_metadata as $extra_meta) { $item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true); } - $item_arr['inherited'] = $item_arr['collection_id'] != $request['collection_id']; - return $item_arr; } - return $item; } @@ -320,7 +289,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { $collection = new Entities\Collection( $collection_id ); - $result = $this->metadatum_repository->fetch_by_collection( $collection, $args ); + $result = $this->metadatum_section_repository->fetch_by_collection( $collection, $args ); } else { $args = [ 'meta_query' => [ @@ -336,7 +305,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { $args['include_control_metadata_types'] = true; } - $result = $this->metadatum_repository->fetch( $args, 'OBJECT' ); + $result = $this->metadatum_section_repository->fetch( $args, 'OBJECT' ); } $prepared_item = []; @@ -354,7 +323,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { * @throws \Exception */ public function get_items_permissions_check( $request ) { - + return true; if(!isset($request['collection_id'])) { return true; @@ -382,7 +351,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { public function delete_item( $request ) { $metadatum_id = $request['metadatum_id']; - $metadatum = $this->metadatum_repository->fetch($metadatum_id); + $metadatum = $this->metadatum_section_repository->fetch($metadatum_id); if (! $metadatum instanceof Entities\Metadatum) { return new \WP_REST_Response([ @@ -391,7 +360,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { ], 400); } - $metadatum_trashed = $this->metadatum_repository->trash($metadatum); + $metadatum_trashed = $this->metadatum_section_repository->trash($metadatum); $prepared = $this->prepare_item_for_response($metadatum_trashed, $request); @@ -405,7 +374,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { * @throws \Exception */ public function delete_item_permissions_check( $request ) { - $metadatum = $this->metadatum_repository->fetch($request['metadatum_id']); + $metadatum = $this->metadatum_section_repository->fetch($request['metadatum_id']); if ($metadatum instanceof Entities\Metadatum) { return $metadatum->can_delete(); @@ -437,7 +406,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { $attributes[$att] = $value; } - $metadatum = $this->metadatum_repository->fetch($metadatum_id); + $metadatum = $this->metadatum_section_repository->fetch($metadatum_id); $error_message = __('Metadata with this ID was not found', 'tainacan'); @@ -466,7 +435,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { $prepared_metadata = $this->prepare_item_for_updating($metadatum, $attributes); if($prepared_metadata->validate()){ - $updated_metadata = $this->metadatum_repository->update($prepared_metadata); + $updated_metadata = $this->metadatum_section_repository->update($prepared_metadata); $response = $this->prepare_item_for_response($updated_metadata, $request); @@ -492,6 +461,79 @@ class REST_Metadata_Section_Controller extends REST_Controller { ], 400); } + public function add_metadatum( $request ) { + if( !empty($request->get_body()) && isset($request['metadata_section_id']) ){ + $body = json_decode($request->get_body(), true); + $metadata_section_id = $request['metadata_section_id']; + $metadatum_list = $body['metadatum_list']; + + try { + $metadatum_section = $this->metadatum_section_repository->add_metadatum($metadata_section_id, $metadatum_list); + if($metadatum_section == false) { + return new \WP_REST_Response([ + 'error_message' => __('One or more values are invalid.', 'tainacan'), + 'item' => $request->get_body() + ], 400); + } + $response = $this->prepare_item_for_response($metadatum_section, $request); + return new \WP_REST_Response($response, 201); + } catch (\Exception $exception) { + return new \WP_REST_Response($exception->getMessage(), 400); + } + } + return new \WP_REST_Response([ + 'error_message' => __('Body cannot be empty.', 'tainacan'), + 'item' => $request->get_body() + ], 400); + } + + public function delete_metadatum( $request ) { + if( !empty($request->get_body()) && isset($request['metadata_section_id']) ){ + $body = json_decode($request->get_body(), true); + $metadata_section_id = $request['metadata_section_id']; + $metadatum_list = $body['metadatum_list']; + + try { + $metadatum_section = $this->metadatum_section_repository->delete_metadatum($metadata_section_id, $metadatum_list); + if($metadatum_section == false) { + return new \WP_REST_Response([ + 'error_message' => __('One or more values are invalid.', 'tainacan'), + 'item' => $request->get_body() + ], 400); + } + $response = $this->prepare_item_for_response($metadatum_section, $request); + return new \WP_REST_Response($response, 201); + } catch (\Exception $exception) { + return new \WP_REST_Response($exception->getMessage(), 400); + } + } + return new \WP_REST_Response([ + 'error_message' => __('Body cannot be empty.', 'tainacan'), + 'item' => $request->get_body() + ], 400); + } + + public function get_metadatum_list( $request ) { + if(isset($request['metadata_section_id']) ){ + $metadata_section_id = $request['metadata_section_id']; + + try { + $result = $this->metadatum_section_repository->get_metadatum_list($metadata_section_id); + $prepared_item = []; + foreach ( $result as $item ) { + $prepared_item[] = $item->_toArray(); + } + return new \WP_REST_Response($prepared_item, 200); + } catch (\Exception $exception) { + return new \WP_REST_Response($exception->getMessage(), 400); + } + } + return new \WP_REST_Response([ + 'error_message' => __('Metadata section id cannot be empty.', 'tainacan'), + 'item' => $request->get_body() + ], 400); + } + /** * @param \WP_REST_Request $request * @@ -499,7 +541,8 @@ class REST_Metadata_Section_Controller extends REST_Controller { * @throws \Exception */ public function update_item_permissions_check( $request ) { - $metadatum = $this->metadatum_repository->fetch($request['metadatum_id']); + return true; + $metadatum = $this->metadatum_section_repository->fetch($request['metadatum_id']); if ($metadatum instanceof Entities\Metadatum) { return $metadatum->can_edit(); @@ -542,7 +585,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { parent::get_wp_query_params() ); } elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) { - $map = $this->metadatum_repository->get_map(); + $map = $this->metadatum_section_repository->get_map(); foreach ($map as $mapped => $value){ $set_ = 'set_'. $mapped; @@ -566,7 +609,7 @@ class REST_Metadata_Section_Controller extends REST_Controller { 'type' => 'object' ]; - $main_schema = parent::get_repository_schema( $this->metadatum_repository ); + $main_schema = parent::get_repository_schema( $this->metadatum_section_repository ); $permissions_schema = parent::get_permissions_schema(); // $item_metadata_scheme = parent::get_repository_schema( $this->item_metadata_repository ); diff --git a/src/classes/entities/class-tainacan-metadatum-section.php b/src/classes/entities/class-tainacan-metadatum-section.php index ed860d6d0..1ee3df4cb 100644 --- a/src/classes/entities/class-tainacan-metadatum-section.php +++ b/src/classes/entities/class-tainacan-metadatum-section.php @@ -10,20 +10,13 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); class Metadatum_Section extends Entity { // Collection getter and setter declared here use \Tainacan\Traits\Entity_Collection_Relation; - - static $post_type = 'tainacan-metadatum-section'; + + static $post_type = 'tainacan-metasection'; protected $name, $slug, - $order, - $description; - /** - * {@inheritDoc} - * @see \Tainacan\Entities\Entity::capability_type - * @var string - */ - protected static $capability_type = ['tainacan-metadatum-section']; - + $description, + $metadatum_list; /** * {@inheritDoc} @@ -37,7 +30,7 @@ class Metadatum_Section extends Entity { } /** - * Return the metadatum name + * Return the metadatum section name * * @return string */ @@ -46,7 +39,7 @@ class Metadatum_Section extends Entity { } /** - * Get metadatum slug + * Get metadatum section slug * * @return string */ @@ -55,7 +48,7 @@ class Metadatum_Section extends Entity { } /** - * Return the metadatum description + * Return the metadatum section description * * @return string */ @@ -64,7 +57,16 @@ class Metadatum_Section extends Entity { } /** - * Set the metadatum name + * Return the metadatum_list of section + * + * @return [int] + */ + function get_metadatum_list() { + return $this->get_mapped_property('metadatum_list'); + } + + /** + * Set the metadatum section name * * @param [string] $value * @return void @@ -90,7 +92,7 @@ class Metadatum_Section extends Entity { } /** - * Set metadatum description + * Set metadatum section description * * @param [string] $value The text description * @return void @@ -99,6 +101,17 @@ class Metadatum_Section extends Entity { $this->set_mapped_property('description', $value); } + + /** + * Set metadatum list of the section + * + * @param [string|int] $value The array of list metadatum + * @return void + */ + function set_metadatum_list($value) { + $this->set_mapped_property('metadatum_list', array_unique($value)); + } + /** * {@inheritdoc } * @@ -108,7 +121,22 @@ class Metadatum_Section extends Entity { * @throws \Exception */ public function validate() { - $this->add_error($this->get_id(), __("Error", 'tainacan')); - return false; + $no_errors = true; + $metadatum_list = $this->get_metadatum_list(); + $name = $this->get_name(); + + if ( !isset($name) ) { + $this->add_error($this->get_id(), __("name is required", 'tainacan')); + $no_errors = false; + } + if( !empty($metadatum_list) ) { + foreach($metadatum_list as $metadatum_id) { + if(get_post_type($metadatum_id) != \Tainacan\Entities\Metadatum::$post_type ) { + $this->add_error($this->get_id(), __("is not a valid metadata", 'tainacan')); + $no_errors = false; + } + } + } + return $no_errors; } } diff --git a/src/classes/repositories/class-tainacan-metadata-section.php b/src/classes/repositories/class-tainacan-metadata-section.php index 68be3deaf..83799e914 100644 --- a/src/classes/repositories/class-tainacan-metadata-section.php +++ b/src/classes/repositories/class-tainacan-metadata-section.php @@ -48,6 +48,13 @@ class Metadata_Section extends Repository { 'type' => 'string', 'description' => __( 'A unique and sanitized string representation of the metadata sction', 'tainacan' ), ], + 'status' => [ + 'map' => 'post_status', + 'title' => __( 'Status', 'tainacan' ), + 'type' => 'string', + 'default' => 'public', + 'description' => __( 'Status', 'tainacan' ) + ], 'description' => [ 'map' => 'post_content', 'title' => __( 'Description', 'tainacan' ), @@ -60,6 +67,15 @@ class Metadata_Section extends Repository { 'title' => __( 'Collection', 'tainacan' ), 'type' => ['integer', 'string'], 'description' => __( 'The collection ID', 'tainacan' ), + ], + 'metadatum_list' => [ + 'map' => 'meta', + 'title' => __( 'Metadatum list', 'tainacan' ), + 'type' => 'array', + 'items' => [ + 'type' => 'integer' + ], + 'description' => __( 'The metadatum ID list', 'tainacan' ), ] ] ); } @@ -278,6 +294,39 @@ class Metadata_Section extends Repository { return $this->insert( $object ); } + public function add_metadatum($metadata_section_id, $metadatum_list) { + $metadata_section = $this->fetch($metadata_section_id); + $list = $metadata_section->get_metadatum_list(); + $metadatum_list = array_merge($list, $metadatum_list); + $metadata_section->set_metadatum_list($metadatum_list); + if($metadata_section->validate()) { + $metadata_section = $this->update($metadata_section); + return $metadata_section; + } + return false; + } + + public function delete_metadatum($metadata_section_id, $metadatum_list) { + $metadata_section = $this->fetch($metadata_section_id); + $list = $metadata_section->get_metadatum_list(); + $list = array_diff($list, $metadatum_list); + $metadata_section->set_metadatum_list($list); + if($metadata_section->validate()) { + $metadata_section = $this->update($metadata_section); + return $metadata_section; + } + return false; + } + + public function get_metadatum_list($metadata_section_id) { + $metadata_section = $this->fetch($metadata_section_id); + $list = $metadata_section->get_metadatum_list(); + $args = array('post__in' => $list); + $metadata_repository = \Tainacan\Repositories\Metadata::get_instance(); + $metadatum_list = $metadata_repository->fetch($args, 'OBJECT'); + return $metadatum_list; + } + /** * @inheritDoc */ diff --git a/tests/factories/class-tainacan-entity-factory.php b/tests/factories/class-tainacan-entity-factory.php index fb14717d2..194a1bff5 100644 --- a/tests/factories/class-tainacan-entity-factory.php +++ b/tests/factories/class-tainacan-entity-factory.php @@ -58,7 +58,10 @@ class Entity_Factory { $this->repository_type = "\Tainacan\Repositories\\$type".'es'; } elseif($type == 'Metadatum'){ $this->repository_type = "\Tainacan\Repositories\Metadata"; - } else { + } elseif($type == 'Metadatum_Section'){ + $this->repository_type = "\Tainacan\Repositories\Metadata_Section"; + } + else { $this->repository_type = "\Tainacan\Repositories\\$type".'s'; } diff --git a/tests/test-api-metadata-section.php b/tests/test-api-metadata-section.php new file mode 100644 index 000000000..e836faf53 --- /dev/null +++ b/tests/test-api-metadata-section.php @@ -0,0 +1,1188 @@ +tainacan_entity_factory->create_entity('collection', '', true); + + $metadatum = json_encode( + array( + 'name' => 'Dados Pessoais', + 'description' => 'Informações e detalhes.' + ) + ); + + $request = new \WP_REST_Request( + 'POST', + $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections' + ); + $request->set_body($metadatum); + + $response = $this->server->dispatch($request); + + $metadatum_section_added = $response->get_data(); + $this->assertTrue(is_array($metadatum_section_added) && array_key_exists('name', $metadatum_section_added), sprintf('cannot create metadatum section, response: %s', print_r($metadatum_section_added, true))); + $this->assertEquals('Dados Pessoais', $metadatum_section_added['name']); + $this->assertTrue(empty($metadatum_section_added['metadatum_list'])); + } + + public function test_create_fill_metadatum_section() { + $collection = $this->tainacan_entity_factory->create_entity('collection', '', true); + + $metadatum_1 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-1', + 'description' => 'description-1', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_2 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-2', + 'description' => 'description-2', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_section = json_encode( + array( + 'name' => 'Dados Pessoais', + 'description' => 'Informações e detalhes.', + 'metadatum_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_2->get_id()] + ) + ); + + $request = new \WP_REST_Request( + 'POST', + $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections' + ); + $request->set_body($metadatum_section); + + $response = $this->server->dispatch($request); + + $metadatum_section_added = $response->get_data(); + $this->assertTrue(is_array($metadatum_section_added) && array_key_exists('name', $metadatum_section_added), sprintf('cannot create metadatum section, response: %s', print_r($metadatum_section_added, true))); + $this->assertEquals('Dados Pessoais', $metadatum_section_added['name']); + $this->assertEquals(2, count($metadatum_section_added['metadatum_list'])); + } + + public function test_add_metadata_metadatum_section() { + $collection = $this->tainacan_entity_factory->create_entity('collection', '', true); + + $metadatum_1 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-1', + 'description' => 'description-1', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_2 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-2', + 'description' => 'description-2', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_3 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-3', + 'description' => 'description-3', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_section = $this->tainacan_entity_factory->create_entity( + 'Metadatum_Section', + array( + 'name' => 'Section', + 'description' => 'Section Description', + 'metadatum_list' => [$metadatum_1->get_id(), $metadatum_1->get_id()] + ), + true + ); + + $metadatum_list = json_encode( + array( + 'metadatum_list' => [$metadatum_2->get_id(), $metadatum_3->get_id()] + ) + ); + + $request = new \WP_REST_Request( + 'POST', + $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadatum_section->get_id() . '/metadatum' + ); + $request->set_body($metadatum_list); + $response = $this->server->dispatch($request); + $metadatum_section_added = $response->get_data(); + + $this->assertTrue(is_array($metadatum_section_added) && array_key_exists('name', $metadatum_section_added), sprintf('cannot create metadatum section, response: %s', print_r($metadatum_section_added, true))); + $this->assertEquals('Section', $metadatum_section_added['name']); + $this->assertEquals(3, count($metadatum_section_added['metadatum_list'])); + $this->assertContains($metadatum_1->get_id(), $metadatum_section_added['metadatum_list']); + $this->assertContains($metadatum_2->get_id(), $metadatum_section_added['metadatum_list']); + $this->assertContains($metadatum_3->get_id(), $metadatum_section_added['metadatum_list']); + } + + public function test_delete_metadata_metadatum_section() { + $collection = $this->tainacan_entity_factory->create_entity('collection', '', true); + + $metadatum_1 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-1', + 'description' => 'description-1', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_2 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-2', + 'description' => 'description-2', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_3 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-3', + 'description' => 'description-3', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_section = $this->tainacan_entity_factory->create_entity( + 'Metadatum_Section', + array( + 'name' => 'Section', + 'description' => 'Section Description', + 'metadatum_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_3->get_id()] + ), + true + ); + + $metadatum_list = json_encode( + array( + 'metadatum_list' => [$metadatum_1->get_id(), $metadatum_3->get_id()] + ) + ); + + $request = new \WP_REST_Request( + 'DELETE', + $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadatum_section->get_id() . '/metadatum' + ); + $request->set_body($metadatum_list); + $response = $this->server->dispatch($request); + $metadatum_section_added = $response->get_data(); + + $this->assertTrue(is_array($metadatum_section_added) && array_key_exists('name', $metadatum_section_added), sprintf('cannot create metadatum section, response: %s', print_r($metadatum_section_added, true))); + $this->assertEquals('Section', $metadatum_section_added['name']); + $this->assertEquals(1, count($metadatum_section_added['metadatum_list'])); + $this->assertNotContains($metadatum_1->get_id(), $metadatum_section_added['metadatum_list']); + $this->assertContains($metadatum_2->get_id(), $metadatum_section_added['metadatum_list']); + $this->assertNotContains($metadatum_3->get_id(), $metadatum_section_added['metadatum_list']); + + } + + public function test_get_metadata_metadatum_section() { + $collection = $this->tainacan_entity_factory->create_entity('collection', '', true); + + $metadatum_1 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-1', + 'description' => 'description-1', + 'collection' => $collection, + 'status' => 'publish', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_2 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-2', + 'description' => 'description-2', + 'collection' => $collection, + 'status' => 'publish', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_3 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'name-3', + 'description' => 'description-3', + 'collection' => $collection, + 'status' => 'publish', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + ), + true + ); + + $metadatum_section = $this->tainacan_entity_factory->create_entity( + 'Metadatum_Section', + array( + 'name' => 'Section', + 'description' => 'Section Description', + 'metadatum_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_3->get_id()] + ), + true + ); + + $request = new \WP_REST_Request( + 'GET', + $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadatum_section->get_id() . '/metadatum' + ); + $response = $this->server->dispatch($request); + $metadata_list = $response->get_data(); + + $this->assertEquals(3, count($metadata_list)); + } + + + // public function test_update_metadata(){ + // $collection = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'Statement', + // 'description' => 'No Statement', + // 'status' => 'publish' + // ), + // true + // ); + + // $item = $this->tainacan_entity_factory->create_entity( + // 'item', + // array( + // 'title' => 'No name', + // 'description' => 'No description', + // 'collection' => $collection + // ), + // true + // ); + + // $metadatum = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data', + // 'description' => 'Descreve o dado do campo data.', + // 'collection' => $collection, + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // 'multiple' => 'yes' + // ), + // true + // ); + + // $meta_values = json_encode( + // array( + // 'values' => array( + // '19/01/2018', + // '19/02/2018', + // ) + // ) + // ); + + // $request = new \WP_REST_Request( + // 'PATCH', + // $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $metadatum->get_id() + // ); + // $request->set_body($meta_values); + + // $response = $this->server->dispatch($request); + + // $item_metadata_updated = $response->get_data(); + + // $metadatum_updated = $item_metadata_updated['metadatum']; + + // $this->assertEquals($metadatum->get_id(), $metadatum_updated['id']); + + // $this->assertEquals('19/01/2018', $item_metadata_updated['value'][0]); + // $this->assertEquals('19/02/2018', $item_metadata_updated['value'][1]); + + + // #### UPDATE METADATUM IN COLLECTION #### + + // $values = json_encode([ + // 'name' => 'Dia/Mês/Ano', + // 'description' => 'Continua descrevendo o dado do campo.' + // ]); + + // $request = new \WP_REST_Request( + // 'PATCH', + // $this->namespace . '/collection/' . $collection->get_id() . '/metadata/' . $metadatum->get_id() + // ); + + // $request->set_body($values); + + // $response = $this->server->dispatch($request); + + // $data = $response->get_data(); + + // $this->assertEquals($metadatum->get_id(), $data['id']); + // $this->assertEquals('Dia/Mês/Ano', $data['name']); + + // // Mantém-se o valor antigo no item + // $metav = get_post_meta($item->get_id(), $data['id'], true); + + // $this->assertEquals('19/01/2018', $metav); + + // } + + // public function test_fetch_a_metadatum_from_a_collection(){ + // $collection = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'Statement', + // 'description' => 'No Statement' + // ), + // true + // ); + + // $metadatumA = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data', + // 'description' => 'Descreve valor do campo data.', + // 'collection' => $collection, + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ), true + // ); + + // $request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/metadata/' . $metadatumA->get_id()); + + // $response = $this->server->dispatch($request); + + // $data = $response->get_data(); + + // $this->assertEquals('Data', $data['name']); + // $this->assertEquals($metadatumA->get_id(), $data['id']); + // } + + // public function test_create_default_metadatum(){ + // $metadatum = json_encode( + // array( + // 'name' => 'Ano de Publicação', + // 'description' => 'Uma data no formato dd/mm/aaaa.', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ) + // ); + + // $request = new \WP_REST_Request( + // 'POST', + // $this->namespace . '/metadata' + // ); + // $request->set_body($metadatum); + + // $response = $this->server->dispatch($request); + // $metadatum_added = $response->get_data(); + + // $this->assertTrue(is_array($metadatum_added) && array_key_exists('name', $metadatum_added), sprintf('cannot create metadatum, response: %s', print_r($metadatum_added, true))); + // $this->assertEquals('Ano de Publicação', $metadatum_added['name']); + + // $this->assertEquals('default', $metadatum_added['collection_id']); + // } + + // public function test_fetch_default_metadata(){ + // $collection = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'Statement', + // 'description' => 'No Statement' + // ), + // true + // ); + + // $metadatumA = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data 1', + // 'description' => 'Descreve valor do campo data.', + // 'collection' => $collection, + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ), true + // ); + + // $metadatumB = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data 2', + // 'description' => 'Descreve valor do campo data.', + // 'collection_id' => 'default', + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ), true + // ); + + // $request_fetch_defaults = new \WP_REST_Request('GET', $this->namespace . '/metadata'); + + // $response_defaults = $this->server->dispatch($request_fetch_defaults); + + // $data = $response_defaults->get_data(); + + // $this->assertCount(1, $data); + + // $this->assertEquals('default', $data[0]['collection_id']); + // $this->assertEquals('Data 2', $data[0]['name']); + // } + + // public function test_get_item_and_collection_metadata(){ + // $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); + + // $collection = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'Statement', + // 'description' => 'No Statement', + // 'status' => 'publish' + // ), + // true + // ); + + // $item = $this->tainacan_entity_factory->create_entity( + // 'item', + // array( + // 'title' => 'No name', + // 'description' => 'No description', + // 'collection' => $collection + // ), + // true + // ); + + // $metadatum = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data', + // 'description' => 'Descreve valor do campo data.', + // 'collection' => $collection, + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ), + // true + // ); + + // $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadatum); + // $item_metadata->set_value('12/12/2017'); + + // $item_metadata->validate(); + // $Tainacan_Item_Metadata->insert($item_metadata); + + // #################### Get metadatum of collection ###################### + + // $request = new \WP_REST_Request( + // 'GET', + // $this->namespace . '/collection/' . $collection->get_id() . '/metadata' + // ); + + // $response = $this->server->dispatch($request); + + // $data = $response->get_data(); + + // $metadata_names = array_map(function($metadatum) {return $metadatum['name'];}, $data); + + // $this->assertContains('Data', $metadata_names); + + // ################### Get metadatum of item with value ####################### + + // $request = new \WP_REST_Request( + // 'GET', + // $this->namespace . '/item/' . $item->get_id() . '/metadata' + // ); + + // $response = $this->server->dispatch($request); + + // $data = $response->get_data(); + // $this->assertTrue(is_array($data) && array_key_exists(0, $data), sprintf('cannot read metadatum, response: %s', print_r($data, true))); + + + // $metadata_names = array_map(function($item_metadata) {return $item_metadata['metadatum']['name'];}, $data); + // $values = array_map(function($item_metadata) {return $item_metadata['value'];}, $data); + + // $this->assertContains('Data', $metadata_names); + // $this->assertContains('12/12/2017', $values); + // } + + + + // public function test_trash_metadatum_in_collection(){ + // $collection = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'Statement', + // 'description' => 'No Statement' + // ), + // true + // ); + + // $metadatum = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Metadatum Statement', + // 'description' => 'No Statement', + // 'collection' => $collection, + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // 'multiple' => 'yes' + // ), + // true + // ); + + // $trash_metadatum_request = new \WP_REST_Request( + // 'DELETE', + // $this->namespace . '/collection/'. $collection->get_id() . '/metadata/' . $metadatum->get_id() + // ); + + // $trash_metadatum_response = $this->server->dispatch($trash_metadatum_request); + // $data1 = $trash_metadatum_response->get_data(); + + // $this->assertEquals($metadatum->get_id(), $data1['id']); + + // $metadatum_trashed = get_post($data1['id']); + // $this->assertEquals('trash', $metadatum_trashed->post_status); + // } + + // public function test_trash_default_metadatum(){ + // $metadatum = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Metadatum Statement', + // 'description' => 'No Statement', + // 'collection_id' => 'default', + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // 'multiple' => 'yes' + // ), + // true + // ); + + // $trash_metadatum_request = new \WP_REST_Request( + // 'DELETE', + // $this->namespace . '/metadata/' . $metadatum->get_id() + // ); + + // $trash_metadatum_response = $this->server->dispatch($trash_metadatum_request); + // $data1 = $trash_metadatum_response->get_data(); + + // $this->assertEquals($metadatum->get_id(), $data1['id']); + + // $metadatum_trashed = get_post($data1['id']); + // $this->assertEquals('trash', $metadatum_trashed->post_status); + // } + + // public function test_update_default_metadatum(){ + // $metadatum = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Metadatum Statement', + // 'description' => 'No Statement', + // 'collection_id' => 'default', + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // 'multiple' => 'no' + // ), + // true + // ); + + // $new_attributes = json_encode([ + // 'name' => 'No name', + // 'description' => 'NOP!' + // ]); + + // $request = new \WP_REST_Request( + // 'PATCH', + // $this->namespace . '/metadata/' . $metadatum->get_id() + // ); + + // $request->set_body($new_attributes); + + // $response = $this->server->dispatch($request); + + // $data = $response->get_data(); + + // $this->assertEquals($metadatum->get_id(), $data['id']); + // $this->assertEquals('No name', $data['name']); + // } + + // public function test_return_metadata_type_options_in_get_item() { + + // $collection1 = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'test_col', + // 'status' => 'publish' + // ), + // true + // ); + + // $collection2 = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'test_col', + // 'status' => 'publish' + // ), + // true + // ); + + // $core1 = $collection1->get_core_title_metadatum(); + + // $meta_relationship = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'relationship', + // 'status' => 'publish', + // 'collection' => $collection2, + // 'metadata_type' => 'Tainacan\Metadata_Types\Relationship', + // 'metadata_type_options' => [ + // 'repeated' => 'yes', + // 'collection_id' => $collection1->get_id(), + // 'search' => $core1->get_id() + // ] + // ), + // true + // ); + + // $request = new \WP_REST_Request( + // 'GET', + // $this->namespace . '/metadata/' . $meta_relationship->get_id() + // ); + + // $response = $this->server->dispatch($request); + + // $data = $response->get_data(); + + // $this->assertEquals($meta_relationship->get_id(), $data['id']); + // $this->assertEquals('relationship', $data['name']); + // $this->assertEquals('yes', $data['metadata_type_options']['repeated']); + // $this->assertEquals($collection1->get_id(), $data['metadata_type_options']['collection_id']); + // $this->assertEquals($core1->get_id(), $data['metadata_type_options']['search']); + + // } + + // public function test_return_metadata_type_options_in_get_items() { + + // $collection1 = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'test_col', + // 'status' => 'publish' + // ), + // true + // ); + + // $collection2 = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'test_col', + // 'status' => 'publish' + // ), + // true + // ); + + // $core1 = $collection1->get_core_title_metadatum(); + + // $meta_relationship = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'relationship', + // 'status' => 'publish', + // 'collection' => $collection2, + // 'metadata_type' => 'Tainacan\Metadata_Types\Relationship', + // 'metadata_type_options' => [ + // 'repeated' => 'yes', + // 'collection_id' => $collection1->get_id(), + // 'search' => $core1->get_id() + // ] + // ), + // true + // ); + + // $request = new \WP_REST_Request( + // 'GET', + // $this->namespace . '/collection/' . $collection2->get_id() . '/metadata' + // ); + + // $response = $this->server->dispatch($request); + + // $data = $response->get_data(); + + // //var_dump($data, $this->namespace . '/collection/' . $collection2->get_id() . '/metadata/'); + // foreach ($data as $d) { + // if ($d['id'] == $meta_relationship->get_id()) { + // $meta = $d; + // break; + // } + // } + + // $this->assertEquals($meta_relationship->get_id(), $meta['id']); + // $this->assertEquals('relationship', $meta['name']); + // $this->assertEquals('yes', $meta['metadata_type_options']['repeated']); + // $this->assertEquals($collection1->get_id(), $meta['metadata_type_options']['collection_id']); + // $this->assertEquals($core1->get_id(), $meta['metadata_type_options']['search']); + + // } + + // public function test_return_metadata_type_options_in_get_item_default_option() { + + // $collection1 = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'test_col', + // 'status' => 'publish' + // ), + // true + // ); + + // $tax = $this->tainacan_entity_factory->create_entity( + // 'taxonomy', + // array( + // 'name' => 'tax_test', + // 'collections' => [$collection1], + // 'status' => 'publish' + // ), + // true + // ); + + // $meta = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'tax', + // 'status' => 'publish', + // 'collection' => $collection1, + // 'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', + // 'metadata_type_options' => [ + // 'taxonomy_id' => $tax->get_id(), + // ] + // ), + // true + // ); + + // $request = new \WP_REST_Request( + // 'GET', + // $this->namespace . '/metadata/' . $meta->get_id() + // ); + + // $response = $this->server->dispatch($request); + + // $data = $response->get_data(); + + // $this->assertEquals($meta->get_id(), $data['id']); + // $this->assertEquals('tax', $data['name']); + // $this->assertEquals($tax->get_id(), $data['metadata_type_options']['taxonomy_id']); + // $this->assertEquals('no', $data['metadata_type_options']['allow_new_terms']); + + // } + + // public function test_update_taxonomy_metadata() { + // $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); + + // $collection = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'test_col', + // 'status' => 'publish' + // ), + // true + // ); + + // $tax = $this->tainacan_entity_factory->create_entity( + // 'taxonomy', + // array( + // 'name' => 'tax_test', + // 'collections' => [$collection], + // 'status' => 'publish' + // ), + // true + // ); + + // $this->tainacan_entity_factory->create_entity( + // 'term', + // array( + // 'taxonomy' => $tax->get_db_identifier(), + // 'name' => 'Rock', + // 'user' => 56 + // ), + // true + // ); + + // $this->tainacan_entity_factory->create_entity( + // 'term', + // array( + // 'taxonomy' => $tax->get_db_identifier(), + // 'name' => 'Samba', + // 'user' => 56 + // ), + // true + // ); + + // $metadatum = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'tax', + // 'status' => 'publish', + // 'collection' => $collection, + // 'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', + // 'metadata_type_options' => [ + // 'taxonomy_id' => $tax->get_id(), + // ] + // ), + // true + // ); + + // $i1 = $this->tainacan_entity_factory->create_entity( + // 'item', + // array( + // 'title' => 'item teste', + // 'description' => 'adasdasdsa', + // 'collection' => $collection + // ), + // true + // ); + + // $itemMeta1 = new \Tainacan\Entities\Item_Metadata_Entity($i1, $metadatum); + // $itemMeta1->set_value('Rock'); + // $itemMeta1->validate(); + // $Tainacan_Item_Metadata->insert($itemMeta1); + + // $request = new \WP_REST_Request( + // 'GET', + // $this->namespace . '/item/' . $i1->get_id() . '/metadata/' . $metadatum->get_id() + // ); + // $response = $this->server->dispatch($request); + // $data = $response->get_data(); + + // $this->assertEquals(false, empty($data['value'])); + + // $request = new \WP_REST_Request( + // 'PATCH', + // $this->namespace . '/item/' . $i1->get_id() . '/metadata/' . $metadatum->get_id() + // ); + // $attributes = json_encode(['values' => '']); + // $request->set_body($attributes); + // $response = $this->server->dispatch($request); + // $data = $response->get_data(); + + // $this->assertEquals(true, empty($data['value'])); + // } + + // public function test_visibility_the_metadatum_from_in_collection(){ + // $collection = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'Statement', + // 'description' => 'No Statement' + // ), + // true + // ); + + // $metadatumA = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data', + // 'description' => 'Descreve valor do campo data.', + // 'collection' => $collection, + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ), true + // ); + + // $metadatumB = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data', + // 'description' => 'Descreve valor do campo data.', + // 'collection' => $collection, + // 'status' => 'private', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ), true + // ); + + // wp_logout(); + // wp_set_current_user(0); + + // $requestA = new \WP_REST_Request('GET', $this->namespace . '/metadata/' . $metadatumA->get_id()); + // $requestB = new \WP_REST_Request('GET', $this->namespace . '/metadata/' . $metadatumB->get_id()); + + // $response = $this->server->dispatch($requestA); + // $status = $response->status; + // $this->assertEquals(200, $status); + + // $response = $this->server->dispatch($requestB); + // $status = $response->status; + // $this->assertEquals(401, $status); + // } + + // public function test_private_filter_ids_not_in_metadata_list(){ + // $collection = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'Statement', + // 'description' => 'No Statement', + // 'status' => 'publish', + // ), + // true + // ); + + // $metadatumA = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data', + // 'description' => 'Descreve valor do campo data.', + // 'collection' => $collection, + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ), true + // ); + + // $metadatumB = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data', + // 'description' => 'Descreve valor do campo data.', + // 'collection' => $collection, + // 'status' => 'private', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ), true + // ); + + // wp_logout(); + // wp_set_current_user(0); + + // $requestA = new \WP_REST_Request('GET', $this->namespace . '/metadata/' . $metadatumA->get_id()); + // $requestB = new \WP_REST_Request('GET', $this->namespace . '/metadata/' . $metadatumB->get_id()); + // $requestC = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/metadata'); + + // $response = $this->server->dispatch($requestA); + // $status = $response->status; + // $this->assertEquals(200, $status); + + // $response = $this->server->dispatch($requestB); + // $status = $response->status; + // $this->assertEquals(401, $status); + + // $response = $this->server->dispatch($requestC); + // $data = $response->get_data(); + // $this->assertEquals(3, count($data)); + // $this->assertNotEquals($metadatumB->get_id(), $data[0]['id']); + // $this->assertNotEquals($metadatumB->get_id(), $data[1]['id']); + // $this->assertNotEquals($metadatumB->get_id(), $data[2]['id']); + // } + + // public function test_private_meta_ids_not_in_metadata_order(){ + // $collection = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'Statement', + // 'description' => 'No Statement', + // 'status' => 'publish', + // ), + // true + // ); + + // $metadatumA = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data', + // 'description' => 'Descreve valor do campo data.', + // 'collection' => $collection, + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ), true + // ); + + // $metadatumB = $this->tainacan_entity_factory->create_entity( + // 'metadatum', + // array( + // 'name' => 'Data', + // 'description' => 'Descreve valor do campo data.', + // 'collection' => $collection, + // 'status' => 'private', + // 'metadata_type' => 'Tainacan\Metadata_Types\Text', + // ), true + // ); + + // $order = array(); + + // $metas = $collection->get_metadata(); + + // foreach ( $metas as $m ) { + // $order[] = [ + // 'id' => $m->get_id(), + // 'enabled' => true, + // ]; + // } + + // $collection->set_metadata_order($order); + // $collection->validate(); + // \tainacan_collections()->insert($collection); + + + + // $request = new \WP_REST_Request('GET', $this->namespace . '/collections/' . $collection->get_id()); + + // $response = $this->server->dispatch($request); + // $data = $response->get_data(); + + // $this->assertEquals(4, count($data['metadata_order'])); + + // wp_logout(); + // wp_set_current_user(0); + + // $request = new \WP_REST_Request('GET', $this->namespace . '/collections/' . $collection->get_id()); + + // $response = $this->server->dispatch($request); + // $data = $response->get_data(); + + // $this->assertEquals(3, count($data['metadata_order'])); + // $this->assertNotEquals($metadatumB->get_id(), $data['metadata_order'][0]['id']); + // $this->assertNotEquals($metadatumB->get_id(), $data['metadata_order'][1]['id']); + // $this->assertNotEquals($metadatumB->get_id(), $data['metadata_order'][2]['id']); + // } + + // /** + // * @group compound_metadatum + // */ + // public function test_create_compound_metadatum_API() { + // $collection = $this->tainacan_entity_factory->create_entity( + // 'collection', + // array( + // 'name' => 'quadrado', + // 'status' => 'publish' + // ), + // true + // ); + + // $this->tainacan_entity_factory->create_entity( + // 'item', + // array( + // 'title' => 'No name', + // 'description' => 'No description', + // 'collection' => $collection, + // 'status' => 'publish' + // ), + // true + // ); + + // $metadatum_compound = json_encode( + // array( + // 'name' => 'quadrado', + // 'description' => 'Descrição de um quadrado.', + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Compound', + // ) + // ); + + // $request = new \WP_REST_Request( + // 'POST', + // $this->namespace . '/collection/' . $collection->get_id() . '/metadata' + // ); + // $request->set_body($metadatum_compound); + // $response = $this->server->dispatch($request); + + // $metadatum_compound_added = $response->get_data(); + // $this->assertTrue(is_array($metadatum_compound_added) && array_key_exists('name', $metadatum_compound_added), sprintf('cannot create metadatum, response: %s', print_r($metadatum_compound_added, true))); + // $this->assertEquals('quadrado', $metadatum_compound_added['name']); + // $this->assertNotEquals('default', $metadatum_compound_added['collection_id']); + + + // $metadatum_largura = json_encode( + // array( + // 'name' => 'largura', + // 'description' => 'largura.', + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Numeric', + // 'parent' => $metadatum_compound_added['id'] + // ) + // ); + // $metadatum_altura = json_encode( + // array( + // 'name' => 'altura', + // 'description' => 'altura', + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Numeric', + // 'parent' => $metadatum_compound_added['id'] + // ) + // ); + + // $request = new \WP_REST_Request( + // 'POST', + // $this->namespace . '/collection/' . $collection->get_id() . '/metadata' + // ); + // $request->set_body($metadatum_largura); + // $response = $this->server->dispatch($request); + // $metadatum_largura = $response->get_data(); + + // $this->assertTrue(is_array($metadatum_largura) && array_key_exists('name', $metadatum_largura), sprintf('cannot create metadatum, response: %s', print_r($metadatum_largura, true))); + // $this->assertEquals('largura', $metadatum_largura['name']); + // $this->assertNotEquals('default', $metadatum_largura['collection_id']); + + // $request = new \WP_REST_Request( + // 'POST', + // $this->namespace . '/collection/' . $collection->get_id() . '/metadata' + // ); + // $request->set_body($metadatum_altura); + // $response = $this->server->dispatch($request); + // $metadatum_altura = $response->get_data(); + + // $this->assertTrue(is_array($metadatum_altura) && array_key_exists('name', $metadatum_altura), sprintf('cannot create metadatum, response: %s', print_r($metadatum_altura, true))); + // $this->assertEquals('altura', $metadatum_altura['name']); + // $this->assertNotEquals('default', $metadatum_altura['collection_id']); + + + // $metadatum_multiplo = json_encode( + // array( + // 'name' => 'multiplo', + // 'description' => 'multiplo.', + // 'multiple' => 'yes', + // 'status' => 'publish', + // 'metadata_type' => 'Tainacan\Metadata_Types\Numeric', + // 'parent' => $metadatum_compound_added['id'] + // ) + // ); + // $request = new \WP_REST_Request( + // 'POST', + // $this->namespace . '/collection/' . $collection->get_id() . '/metadata' + // ); + // $request->set_body($metadatum_multiplo); + // $response = $this->server->dispatch($request); + // $metadatum_multiplo_data = $response->get_data(); + // $this->assertEquals(400, $response->get_status(), sprintf('cannot create metadatum, response: %s', print_r($metadatum_multiplo_data, true)) ); + // } + +}