feat: remove metadata_list mapped property #184

This commit is contained in:
vnmedeiros 2022-05-12 01:31:17 -03:00
parent c16f37a3a7
commit a93899529e
8 changed files with 419 additions and 466 deletions

View File

@ -148,22 +148,14 @@ class REST_Metadata_Controller extends REST_Controller {
* @return \WP_Error|\WP_REST_Response * @return \WP_Error|\WP_REST_Response
*/ */
public function get_item( $request ) { public function get_item( $request ) {
$collection_id = isset($request['collection_id']) ? $request['collection_id'] : false;
$metadatum_id = $request['metadatum_id']; $metadatum_id = $request['metadatum_id'];
$offset = '';
$number = '';
if($request['offset'] >= 0 && $request['number'] >= 1){
$offset = $request['offset'];
$number = $request['number'];
}
$result = $this->metadatum_repository->fetch($metadatum_id, 'OBJECT'); $result = $this->metadatum_repository->fetch($metadatum_id, 'OBJECT');
if (! $result instanceof Entities\Metadatum) { if (! $result instanceof Entities\Metadatum) {
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('Metadata with this ID was not found', 'tainacan'), 'error_message' => __('Metadata with this ID was not found', 'tainacan'),
'item_id' => $item_id 'item_id' => $metadatum_id
], 400); ], 400);
} }
@ -187,7 +179,7 @@ class REST_Metadata_Controller extends REST_Controller {
} }
/** /**
* @param \WP_REST_Request $request * @param String $request
* *
* @param null $collection_id * @param null $collection_id
* *
@ -444,7 +436,7 @@ class REST_Metadata_Controller extends REST_Controller {
if (! $metadatum instanceof Entities\Metadatum) { if (! $metadatum instanceof Entities\Metadatum) {
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('Metadata with this ID was not found', 'tainacan'), 'error_message' => __('Metadata with this ID was not found', 'tainacan'),
'item_id' => $item_id 'item_id' => $metadatum_id
], 400); ], 400);
} }
@ -595,9 +587,9 @@ class REST_Metadata_Controller extends REST_Controller {
$endpoint_args = []; $endpoint_args = [];
if($method === \WP_REST_Server::READABLE) { if($method === \WP_REST_Server::READABLE) {
$endpoint_args = array_merge( $endpoint_args = array_merge(
$endpoint_args, $endpoint_args,
parent::get_wp_query_params() parent::get_wp_query_params()
); );
} elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) { } elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) {
$map = $this->metadatum_repository->get_map(); $map = $this->metadatum_repository->get_map();

View File

@ -21,6 +21,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
public function init_objects() { public function init_objects() {
$this->metadata_sections_repository = Repositories\Metadata_Sections::get_instance(); $this->metadata_sections_repository = Repositories\Metadata_Sections::get_instance();
$this->metadata_repository = Repositories\Metadata::get_instance(); $this->metadata_repository = Repositories\Metadata::get_instance();
$this->collection_repository = Repositories\Collections::get_instance();
} }
/** /**
@ -35,7 +36,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
* @throws \Exception * @throws \Exception
*/ */
public function register_routes() { public function register_routes() {
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadatum_id>[\d]+)', register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadata_section_id>[\d]+)',
array( array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
@ -109,50 +110,6 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
); );
} }
/**
* @param \WP_REST_Request $request
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_item( $request ) {
$collection_id = isset($request['collection_id']) ? $request['collection_id'] : false;
$metadatum_id = $request['metadatum_id'];
$offset = '';
$number = '';
if($request['offset'] >= 0 && $request['number'] >= 1){
$offset = $request['offset'];
$number = $request['number'];
}
$result = $this->metadata_sections_repository->fetch($metadatum_id, 'OBJECT');
if (! $result instanceof Entities\Metadatum) {
return new \WP_REST_Response([
'error_message' => __('Metadata with this ID was not found', 'tainacan'),
'item_id' => $item_id
], 400);
}
return new \WP_REST_Response($this->prepare_item_for_response($result, $request), 200);
}
/**
* @param \WP_REST_Request $request
*
* @return bool|\WP_Error
* @throws \Exception
*/
public function get_item_permissions_check( $request ) {
$metadatum = $this->metadata_sections_repository->fetch($request['metadatum_id']);
if ( $metadatum instanceof Entities\Metadatum ) {
return $metadatum->can_read();
}
return false;
}
/** /**
* @param \WP_REST_Request|string $request * @param \WP_REST_Request|string $request
* *
@ -176,6 +133,137 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
return $metadata_section; return $metadata_section;
} }
/**
* @param Entities\Metadata_Section $item
* @param \WP_REST_Request $request
*
* @return array|\WP_Error|\WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
if (!empty($item)){
$item_arr = $item->_toArray();
if ($request['context'] === 'edit') {
$item_arr['current_user_can_edit'] = $item->can_edit();
$item_arr['current_user_can_delete'] = $item->can_delete();
$item_arr['enabled'] = $item->get_enabled_for_collection();
}
$metadata_list = $item->get_metadata_object_list();
$item_arr['metadata_object_list'] = [];
if($metadata_list != false) {
foreach($metadata_list as $metadata) {
$meta_arr = $this->prepare_metadata_for_response($metadata, $request);
$item_arr['metadata_object_list'][] = $meta_arr;
}
}
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-metadata-section-meta', [], $request);
foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true);
}
return $item_arr;
}
return $item;
}
/**
* @param Entities\Metadata $item
* @param \WP_REST_Request $request
*
* @return array|\WP_Error|\WP_REST_Response
*/
public function prepare_metadata_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'] );
$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();
}
}
}
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-metadatum-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;
}
/**
* @param \WP_REST_Request $request
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_item( $request ) {
$metadata_section_id = $request['metadata_section_id'];
$result = $this->metadata_sections_repository->fetch($metadata_section_id, 'OBJECT');
if (! $result instanceof Entities\Metadata_Section) {
return new \WP_REST_Response([
'error_message' => __('Metadata section with this ID was not found', 'tainacan'),
'item_id' => $metadata_section_id
], 400);
}
return new \WP_REST_Response($this->prepare_item_for_response($result, $request), 200);
}
/**
* @param \WP_REST_Request $request
*
* @return bool|\WP_Error
* @throws \Exception
*/
public function get_item_permissions_check( $request ) {
$metadatum_section = $this->metadata_sections_repository->fetch($request['metadata_section_id']);
if ( $metadatum_section instanceof Entities\Metadata_Section ) {
return $metadatum_section->can_read();
}
return false;
}
/** /**
* @param \WP_REST_Request $request * @param \WP_REST_Request $request
* *
@ -236,104 +324,6 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
} }
/**
* @param mixed $item
* @param \WP_REST_Request $request
*
* @return array|\WP_Error|\WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
if (!empty($item)){
$item_arr = $item->_toArray();
if ($request['context'] === 'edit') {
$item_arr['current_user_can_edit'] = $item->can_edit();
$item_arr['current_user_can_delete'] = $item->can_delete();
$item_arr['enabled'] = $item->get_enabled_for_collection();
}
$metadata_list = !empty($item_arr['metadata_list']) ? $item_arr['metadata_list'] : [];
$item_arr['metadata_object_list'] = [];
foreach($metadata_list as $metadatum_id) {
$meta = $this->metadata_repository->fetch($metadatum_id, 'OBJECT');
$meta_arr = $this->prepare_metadata_for_response($meta, $request);
$item_arr['metadata_object_list'][] = $meta_arr;
}
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-metadata-section-meta', [], $request);
foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true);
}
return $item_arr;
}
return $item;
}
/**
* @param mixed $item
* @param \WP_REST_Request $request
*
* @return array|\WP_Error|\WP_REST_Response
*/
public function prepare_metadata_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();
}
}
}
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-metadatum-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;
}
/** /**
* @param \WP_REST_Request $request * @param \WP_REST_Request $request
* *
@ -370,7 +360,6 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
* @throws \Exception * @throws \Exception
*/ */
public function get_items_permissions_check( $request ) { public function get_items_permissions_check( $request ) {
return true;
if(!isset($request['collection_id'])) { if(!isset($request['collection_id'])) {
return true; return true;
@ -396,21 +385,17 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
* @return \WP_Error|\WP_REST_Response * @return \WP_Error|\WP_REST_Response
*/ */
public function delete_item( $request ) { public function delete_item( $request ) {
$metadatum_id = $request['metadatum_id']; $metadata_section_id = $request['metadata_section_id'];
$metadatum_section = $this->metadata_sections_repository->fetch($metadata_section_id);
$metadatum = $this->metadata_sections_repository->fetch($metadatum_id); if (! $metadatum_section instanceof Entities\Metadatum) {
if (! $metadatum instanceof Entities\Metadatum) {
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('Metadata with this ID was not found', 'tainacan'), 'error_message' => __('Metadata with this ID was not found', 'tainacan'),
'item_id' => $item_id 'item_id' => $metadata_section_id
], 400); ], 400);
} }
$metadatum_trashed = $this->metadata_sections_repository->trash($metadatum); $metadatum_section_trashed = $this->metadata_sections_repository->trash($metadatum_section);
$prepared = $this->prepare_item_for_response($metadatum_section_trashed, $request);
$prepared = $this->prepare_item_for_response($metadatum_trashed, $request);
return new \WP_REST_Response($prepared, 200); return new \WP_REST_Response($prepared, 200);
} }
@ -421,10 +406,10 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
* @throws \Exception * @throws \Exception
*/ */
public function delete_item_permissions_check( $request ) { public function delete_item_permissions_check( $request ) {
$metadatum = $this->metadata_sections_repository->fetch($request['metadatum_id']); $metadata_section = $this->metadata_sections_repository->fetch($request['metadata_section_id']);
if ($metadatum instanceof Entities\Metadatum) { if ($metadata_section instanceof Entities\Metadata_Section) {
return $metadatum->can_delete(); return $metadata_section->can_delete();
} }
return false; return false;
@ -441,64 +426,39 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
if(!empty($body)){ if(!empty($body)){
$attributes = []; $metadata_section_id = $request['metadata_section_id'];
$metadata_section = $this->metadata_sections_repository->fetch($metadata_section_id);
$metadatum_id = $request['metadatum_id']; if ( $collection_id != $metadata_section->get_collection_id() ) {
$confirm_repository = false; return new \WP_REST_Response( [
foreach ($body as $att => $value){ 'error_message' => __('This metadata section not found in collection', 'tainacan'),
if ($att === "repository_level" && $value === "yes") { 'metadata_section_id' => $metadata_section_id
$confirm_repository = true; ] );
continue;
}
$attributes[$att] = $value;
} }
$metadatum = $this->metadata_sections_repository->fetch($metadatum_id); if ($metadata_section) {
$attributes = [];
$error_message = __('Metadata with this ID was not found', 'tainacan'); foreach ($body as $att => $value) {
$attributes[$att] = $value;
if ($metadatum) {
// These conditions are for verify if endpoints are used correctly
if(!$collection_id && $metadatum->get_collection_id() !== 'default') {
$error_message = __('This metadata is not a default metadata', 'tainacan');
return new \WP_REST_Response( [
'error_message' => $error_message,
'metadatum_id' => $metadatum_id
] );
} elseif ($collection_id && $metadatum->get_collection_id() === 'default'){
$error_message = __('This metadata is not a collection metadata', 'tainacan');
return new \WP_REST_Response( [
'error_message' => $error_message,
'metadatum_id' => $metadatum_id
] );
} }
if (isset($request['repository_level']) && $confirm_repository) { $prepared = $this->prepare_item_for_updating($metadata_section, $attributes);
$attributes['collection_id'] = "default"; if($prepared->validate()) {
} $updated_metadata_section = $this->metadata_sections_repository->update($prepared);
$response = $this->prepare_item_for_response($updated_metadata_section, $request);
$prepared_metadata = $this->prepare_item_for_updating($metadatum, $attributes);
if($prepared_metadata->validate()){
$updated_metadata = $this->metadata_sections_repository->update($prepared_metadata);
$response = $this->prepare_item_for_response($updated_metadata, $request);
return new \WP_REST_Response($response, 200); return new \WP_REST_Response($response, 200);
} }
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_metadata->get_errors(), 'errors' => $prepared->get_errors(),
'metadatum' => $this->prepare_item_for_response($prepared_metadata, $request) 'metadatum_section' => $this->prepare_item_for_response($prepared, $request)
], 400); ], 400);
} }
return new \WP_REST_Response( [ return new \WP_REST_Response( [
'error_message' => $error_message, 'error_message' => __('Metadata with this ID was not found', 'tainacan'),
'metadatum_id' => $metadatum_id 'metadata_section_id' => $metadata_section_id
] ); ] );
} }
@ -565,7 +525,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
$metadata_section_id = $request['metadata_section_id']; $metadata_section_id = $request['metadata_section_id'];
try { try {
$result = $this->metadata_sections_repository->get_metadata_list($metadata_section_id); $result = $this->metadata_sections_repository->get_metadata_object_list($metadata_section_id);
$prepared_item = []; $prepared_item = [];
foreach ( $result as $item ) { foreach ( $result as $item ) {
$prepared_item[] = $item->_toArray(); $prepared_item[] = $item->_toArray();
@ -628,9 +588,9 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
$endpoint_args = []; $endpoint_args = [];
if($method === \WP_REST_Server::READABLE) { if($method === \WP_REST_Server::READABLE) {
$endpoint_args = array_merge( $endpoint_args = array_merge(
$endpoint_args, $endpoint_args,
parent::get_wp_query_params() parent::get_wp_query_params()
); );
} elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) { } elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) {
$map = $this->metadata_sections_repository->get_map(); $map = $this->metadata_sections_repository->get_map();

View File

@ -15,8 +15,7 @@ class Metadata_Section extends Entity {
protected protected
$name, $name,
$slug, $slug,
$description, $description;
$metadata_list;
/** /**
* {@inheritDoc} * {@inheritDoc}
@ -63,8 +62,9 @@ class Metadata_Section extends Entity {
* *
* @return [int] * @return [int]
*/ */
function get_metadata_list() { function get_metadata_object_list() {
return $this->get_mapped_property('metadata_list'); $tainacan_metadata_sections = \Tainacan\Repositories\Metadata_Sections::get_instance();
return $tainacan_metadata_sections->get_metadata_object_list($this->get_id());
} }
/** /**
@ -104,16 +104,6 @@ class Metadata_Section extends Entity {
} }
/**
* Set metadata list of the section
*
* @param [string|int] $value The array of metadata in this section
* @return void
*/
function set_metadata_list($value) {
$this->set_mapped_property('metadata_list', array_unique($value));
}
/** /**
* Transient property used to store the status of the metadatum section for a particular collection * Transient property used to store the status of the metadatum section for a particular collection
* *
@ -138,7 +128,6 @@ class Metadata_Section extends Entity {
*/ */
public function validate() { public function validate() {
$no_errors = true; $no_errors = true;
$metadata_list = $this->get_metadata_list();
$name = $this->get_name(); $name = $this->get_name();
$collection = $this->get_collection(); $collection = $this->get_collection();
@ -151,14 +140,6 @@ class Metadata_Section extends Entity {
$this->add_error($this->get_id(), __("name is required", 'tainacan')); $this->add_error($this->get_id(), __("name is required", 'tainacan'));
$no_errors = false; $no_errors = false;
} }
if( !empty($metadata_list) ) {
foreach($metadata_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;
}
}
}
if($no_errors) { if($no_errors) {
$this->set_as_valid(); $this->set_as_valid();
} }

View File

@ -67,16 +67,6 @@ class Metadata_Sections extends Repository {
'title' => __( 'Collection', 'tainacan' ), 'title' => __( 'Collection', 'tainacan' ),
'type' => ['integer', 'string'], 'type' => ['integer', 'string'],
'description' => __( 'The collection ID', 'tainacan' ), 'description' => __( 'The collection ID', 'tainacan' ),
],
'metadata_list' => [
'map' => 'meta',
'title' => __( 'Inner metadata list', 'tainacan' ),
'type' => 'array',
'items' => [
'type' => 'integer'
],
'description' => __( 'The list of metadata IDs inside this section.', 'tainacan' ),
'default' => []
] ]
] ); ] );
} }
@ -279,15 +269,6 @@ class Metadata_Sections extends Repository {
*/ */
public function insert( $metadata_section ) { public function insert( $metadata_section ) {
$new_metadata_section = parent::insert( $metadata_section ); $new_metadata_section = parent::insert( $metadata_section );
$metadata_list = $new_metadata_section->get_metadata_list();
$id = strval($new_metadata_section->get_id());
foreach($metadata_list as $metadata_id) {
// $metadata_section_ids = get_post_meta( $metadata_id, 'metadata_section_id');
// $metadata_section_ids = array_values(array_filter($metadata_section_ids, function($value) { return !is_null($value) && !empty($value); }));
// $metadata_section_ids = $metadata_section_ids === false ? [$id] : array_merge($metadata_section_ids, [$id]) ;
//add_post_meta($metadata_id, 'metadata_section_id', $id);
update_post_meta($metadata_id, 'metadata_section_id', $id);
}
return $new_metadata_section; return $new_metadata_section;
} }
@ -303,40 +284,45 @@ class Metadata_Sections extends Repository {
} }
public function add_metadata($metadata_section_id, $metadata_list) { public function add_metadata($metadata_section_id, $metadata_list) {
$metadata_section = $this->fetch($metadata_section_id); $metadata_section = $this->fetch($metadata_section_id, 'OBJECT');
if ($metadata_section) { if ($metadata_section) {
$list = $metadata_section->get_metadata_list(); foreach($metadata_list as $metadata_id) {
$metadata_list = array_merge($list, $metadata_list); //update_post_meta($metadata_id, 'metadata_section_id', $metadata_section_id);
$metadata_section->set_metadata_list($metadata_list); add_post_meta($metadata_id, 'metadata_section_id', $metadata_section_id);
if ($metadata_section->validate()) {
$metadata_section = $this->update($metadata_section);
return $metadata_section;
} }
return $metadata_section;
} }
return false; return false;
} }
public function delete_metadata($metadata_section_id, $metadata_list) { public function delete_metadata($metadata_section_id, $metadata_list) {
$metadata_section = $this->fetch($metadata_section_id); $metadata_section = $this->fetch($metadata_section_id, 'OBJECT');
if ($metadata_section) { if ($metadata_section) {
$list = $metadata_section->get_metadata_list(); foreach($metadata_list as $metadata_id) {
$list = array_diff($list, $metadata_list); delete_post_meta($metadata_id, 'metadata_section_id', $metadata_section_id);
$metadata_section->set_metadata_list($list);
if ($metadata_section->validate()) {
$metadata_section = $this->update($metadata_section);
return $metadata_section;
} }
return $metadata_section;
} }
return false; return false;
} }
public function get_metadata_list($metadata_section_id) { public function get_metadata_object_list($metadata_section_id) {
$metadata_section = $this->fetch($metadata_section_id); $metadata_section = $this->fetch($metadata_section_id);
$list = $metadata_section->get_metadata_list(); if ($metadata_section) {
$args = array('post__in' => $list); $args = array(
$metadata_repository = \Tainacan\Repositories\Metadata::get_instance(); 'meta_query' => [
$metadata_list = $metadata_repository->fetch($args, 'OBJECT'); [
return $metadata_list; 'key' => 'metadata_section_id',
'value' => $metadata_section_id,
'compare' => '='
]
]
);
$metadata_repository = \Tainacan\Repositories\Metadata::get_instance();
$metadata_list = $metadata_repository->fetch($args, 'OBJECT');
return $metadata_list;
}
return false;
} }
/** /**

View File

@ -49,7 +49,7 @@ class Metadata extends Repository {
* {@inheritDoc} * {@inheritDoc}
* @see \Tainacan\Repositories\Repository::get_map() * @see \Tainacan\Repositories\Repository::get_map()
*/ */
protected function _get_map() { protected function _get_map() {
return apply_filters( 'tainacan-get-map-' . $this->get_name(), [ return apply_filters( 'tainacan-get-map-' . $this->get_name(), [
'name' => [ 'name' => [
'map' => 'post_title', 'map' => 'post_title',
@ -667,13 +667,9 @@ class Metadata extends Repository {
*/ */
public function insert( $metadatum ) { public function insert( $metadatum ) {
$this->pre_update_taxonomy_metadatum( $metadatum ); $this->pre_update_taxonomy_metadatum( $metadatum );
$this->pre_update_metadata_section( $metadatum );
$new_metadatum = parent::insert( $metadatum ); $new_metadatum = parent::insert( $metadatum );
$this->update_taxonomy_metadatum( $new_metadatum ); $this->update_taxonomy_metadatum( $new_metadatum );
$this->update_metadata_type_index( $new_metadatum ); $this->update_metadata_type_index( $new_metadatum );
$this->update_metadata_section( $new_metadatum );
return $new_metadatum; return $new_metadatum;
} }
@ -773,7 +769,7 @@ class Metadata extends Repository {
*/ */
private function get_data_core_metadata( Entities\Collection $collection ) { private function get_data_core_metadata( Entities\Collection $collection ) {
return $data_core_metadata = [ $data_core_metadata = [
'core_title' => [ 'core_title' => [
'name' => __('Title', 'tainacan'), 'name' => __('Title', 'tainacan'),
'collection_id' => $collection->get_id(), 'collection_id' => $collection->get_id(),
@ -788,6 +784,7 @@ class Metadata extends Repository {
'status' => 'publish', 'status' => 'publish',
] ]
]; ];
return $data_core_metadata;
} }
@ -1607,7 +1604,6 @@ class Metadata extends Repository {
*/ */
public function delete( Entities\Entity $entity, $permanent = true ) { public function delete( Entities\Entity $entity, $permanent = true ) {
$this->delete_taxonomy_metadatum($entity); $this->delete_taxonomy_metadatum($entity);
$this->update_metadata_section($entity, true);
return parent::delete($entity, $permanent); return parent::delete($entity, $permanent);
} }
@ -1738,21 +1734,4 @@ class Metadata extends Repository {
return false; return false;
} }
public function pre_update_metadata_section(Entities\Metadatum $metadatum) {
if($metadatum->get_id() && $metadatum->get_collection_id() != 'default') {
$meta = $this->fetch($metadatum->get_id(), 'OBJECT');
$this->update_metadata_section( $meta, true );
}
}
public function update_metadata_section( Entities\Metadatum $metadatum, $remove = false ) {
$metadata_section_repository = Metadata_Sections::get_instance();
if (!$remove) {
$metadata_section_repository->add_metadata($metadatum->get_metadata_section_id(), [$metadatum->get_id()]);
} else {
$metadata_section_repository->delete_metadata($metadatum->get_metadata_section_id(), [$metadatum->get_id()]);
}
}
} }

View File

@ -6,7 +6,8 @@
*/ */
$bootstrap_cfg = require('bootstrap-config.php'); $bootstrap_cfg = require('bootstrap-config.php');
$_tests_dir = getenv( 'WP_TESTS_DIR' ); // $_tests_dir = getenv( 'WP_TESTS_DIR' );
$_tests_dir = '/tainacan_test/wordpress-tests-lib';
if ( ! $_tests_dir ) { if ( ! $_tests_dir ) {
$_tests_dir = $bootstrap_cfg['tests_dir']; $_tests_dir = $bootstrap_cfg['tests_dir'];
} }

View File

@ -29,7 +29,7 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
$metadata_section_added = $response->get_data(); $metadata_section_added = $response->get_data();
$this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true))); $this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true)));
$this->assertEquals('Dados Pessoais', $metadata_section_added['name']); $this->assertEquals('Dados Pessoais', $metadata_section_added['name']);
$this->assertTrue(empty($metadata_section_added['metadata_list'])); $this->assertTrue(empty($metadata_section_added['metadata_object_list']));
} }
public function test_create_fill_metadata_section() { public function test_create_fill_metadata_section() {
@ -42,6 +42,7 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'description' => 'description-1', 'description' => 'description-1',
'collection' => $collection, 'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
), ),
true true
); );
@ -53,6 +54,7 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'description' => 'description-2', 'description' => 'description-2',
'collection' => $collection, 'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
), ),
true true
); );
@ -62,7 +64,6 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'name' => 'Dados Pessoais', 'name' => 'Dados Pessoais',
'description' => 'Informações e detalhes.', 'description' => 'Informações e detalhes.',
'collection_id' => $collection->get_id(), 'collection_id' => $collection->get_id(),
'metadata_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_2->get_id()]
) )
); );
@ -71,18 +72,41 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
$this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections' $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections'
); );
$request->set_body($metadata_section); $request->set_body($metadata_section);
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$metadata_section_added = $response->get_data(); $metadata_section_added = $response->get_data();
$this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true))); $this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true)));
$this->assertEquals('Dados Pessoais', $metadata_section_added['name']); $this->assertEquals('Dados Pessoais', $metadata_section_added['name']);
$this->assertEquals(2, count($metadata_section_added['metadata_list']));
$metadata_list = json_encode(
array(
'metadata_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/' . $metadata_section_added['id'] . '/metadata'
);
$request->set_body($metadata_list);
$response = $this->server->dispatch($request);
$metadata_section = $response->get_data();
$this->assertEquals(2, count($metadata_section['metadata_object_list']));
} }
public function test_add_metadata_metadata_section() { public function test_add_metadata_metadata_section() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$metadata_section = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
'status' => 'publish',
),
true
);
$metadatum_1 = $this->tainacan_entity_factory->create_entity( $metadatum_1 = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
@ -91,6 +115,7 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $metadata_section->get_id()
), ),
true true
); );
@ -119,18 +144,6 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
true true
); );
$metadata_section = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
'status' => 'publish',
'metadata_list' => [$metadatum_1->get_id(), $metadatum_1->get_id()]
),
true
);
$metadata_list = json_encode( $metadata_list = json_encode(
array( array(
'metadata_list' => [$metadatum_2->get_id(), $metadatum_3->get_id()] 'metadata_list' => [$metadatum_2->get_id(), $metadatum_3->get_id()]
@ -147,10 +160,10 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
$this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true))); $this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true)));
$this->assertEquals('Section', $metadata_section_added['name']); $this->assertEquals('Section', $metadata_section_added['name']);
$this->assertEquals(3, count($metadata_section_added['metadata_list'])); $this->assertEquals(3, count($metadata_section_added['metadata_object_list']));
$this->assertContains($metadatum_1->get_id(), $metadata_section_added['metadata_list']); $this->assertContains($metadatum_1->get_id(), array_column($metadata_section_added['metadata_object_list'],'id'));
$this->assertContains($metadatum_2->get_id(), $metadata_section_added['metadata_list']); $this->assertContains($metadatum_2->get_id(), array_column($metadata_section_added['metadata_object_list'],'id'));
$this->assertContains($metadatum_3->get_id(), $metadata_section_added['metadata_list']); $this->assertContains($metadatum_3->get_id(), array_column($metadata_section_added['metadata_object_list'],'id'));
$metadatum = $this->tainacan_entity_factory->create_entity( $metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
@ -200,13 +213,25 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
public function test_delete_metadata_metadata_section() { public function test_delete_metadata_metadata_section() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$metadata_section = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
),
true
);
$metadatum_1 = $this->tainacan_entity_factory->create_entity( $metadatum_1 = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
'name' => 'name-1', 'name' => 'name-1',
'description' => 'description-1', 'description' => 'description-1',
'collection' => $collection, 'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
'metadata_section_id' => $metadata_section->get_id()
), ),
true true
); );
@ -218,6 +243,8 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'description' => 'description-2', 'description' => 'description-2',
'collection' => $collection, 'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
'metadata_section_id' => $metadata_section->get_id()
), ),
true true
); );
@ -229,17 +256,8 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'description' => 'description-3', 'description' => 'description-3',
'collection' => $collection, 'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
), 'status' => 'publish',
true 'metadata_section_id' => $metadata_section->get_id()
);
$metadata_section = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
'metadata_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_3->get_id()]
), ),
true true
); );
@ -260,17 +278,27 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
$this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true))); $this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true)));
$this->assertEquals('Section', $metadata_section_added['name']); $this->assertEquals('Section', $metadata_section_added['name']);
$this->assertEquals(1, count($metadata_section_added['metadata_list'])); $this->assertEquals(1, count($metadata_section_added['metadata_object_list']));
$this->assertNotContains($metadatum_1->get_id(), $metadata_section_added['metadata_list']); $this->assertNotContains($metadatum_1->get_id(), array_column($metadata_section_added['metadata_object_list'], 'id'));
$this->assertContains($metadatum_2->get_id(), $metadata_section_added['metadata_list']); $this->assertContains($metadatum_2->get_id(), array_column($metadata_section_added['metadata_object_list'], 'id'));
$this->assertNotContains($metadatum_3->get_id(), $metadata_section_added['metadata_list']); $this->assertNotContains($metadatum_3->get_id(), array_column($metadata_section_added['metadata_object_list'], 'id'));
} }
public function test_get_metadata_metadata_section() { public function test_get_metadata_metadata_section() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$metadatum_1 = $this->tainacan_entity_factory->create_entity( $metadata_section = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection
),
true
);
$this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
'name' => 'name-1', 'name' => 'name-1',
@ -278,11 +306,12 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $metadata_section->get_id()
), ),
true true
); );
$metadatum_2 = $this->tainacan_entity_factory->create_entity( $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
'name' => 'name-2', 'name' => 'name-2',
@ -290,11 +319,12 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $metadata_section->get_id()
), ),
true true
); );
$metadatum_3 = $this->tainacan_entity_factory->create_entity( $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
'name' => 'name-3', 'name' => 'name-3',
@ -302,17 +332,7 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
), 'metadata_section_id' => $metadata_section->get_id()
true
);
$metadata_section = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
'metadata_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_3->get_id()]
), ),
true true
); );
@ -330,6 +350,31 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
public function test_get_metadata_section() { public function test_get_metadata_section() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$section1 = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
// 'metadata_list' => [$metadatum_1->get_id(), $metadatum_2->get_id()]
),
true,
true
);
$section2 = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
// 'metadata_list' => [$metadatum_3->get_id(), $metadatum_4->get_id()]
),
true,
true
);
$metadatum_1 = $this->tainacan_entity_factory->create_entity( $metadatum_1 = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
@ -338,6 +383,8 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $section1->get_id()
), ),
true true
); );
@ -350,6 +397,7 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $section1->get_id()
), ),
true true
); );
@ -362,6 +410,7 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $section2->get_id()
), ),
true true
); );
@ -374,34 +423,11 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $section2->get_id()
), ),
true true
); );
$this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
'metadata_list' => [$metadatum_1->get_id(), $metadatum_2->get_id()]
),
true,
true
);
$this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
'metadata_list' => [$metadatum_3->get_id(), $metadatum_4->get_id()]
),
true,
true
);
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'GET', 'GET',
$this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections' $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections'
@ -410,5 +436,13 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas
$response_data = $response->get_data(); $response_data = $response->get_data();
$this->assertEquals(2, count($response_data)); $this->assertEquals(2, count($response_data));
$metadata_list_1 = $response_data[0]['metadata_object_list'];
$metadata_list_2 = $response_data[1]['metadata_object_list'];
$this->assertContains($metadatum_1->get_id(), array_column($metadata_list_1, 'id'));
$this->assertContains($metadatum_2->get_id(), array_column($metadata_list_1, 'id'));
$this->assertContains($metadatum_3->get_id(), array_column($metadata_list_2, 'id'));
$this->assertContains($metadatum_4->get_id(), array_column($metadata_list_2, 'id'));
} }
} }

View File

@ -50,6 +50,7 @@ class MetadataSection extends TAINACAN_UnitTestCase {
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $metadata_section->get_id(), 'metadata_section_id' => $metadata_section->get_id(),
), ),
true,
true true
); );
@ -61,9 +62,9 @@ class MetadataSection extends TAINACAN_UnitTestCase {
$this->assertEquals($test->get_collection_id(), $collection->get_id()); $this->assertEquals($test->get_collection_id(), $collection->get_id());
$this->assertEquals($test->get_metadata_section_id(), $metadata_section->get_id()); $this->assertEquals($test->get_metadata_section_id(), $metadata_section->get_id());
$metadata_list = $metadata_section->get_metadata_list(); $metadata_list = $metadata_section->get_metadata_object_list();
$this->assertEquals(count($metadata_list), 1); $this->assertEquals(count($metadata_list), 1);
$this->assertEquals($test->get_id(), $metadata_list[0]); $this->assertEquals($test->get_id(), $metadata_list[0]->get_id());
$this->assertTrue((bool) $test->get_accept_suggestion()); $this->assertTrue((bool) $test->get_accept_suggestion());
} }
@ -112,6 +113,7 @@ class MetadataSection extends TAINACAN_UnitTestCase {
'name' => 'metadado', 'name' => 'metadado',
'description' => 'descricao', 'description' => 'descricao',
'collection' => $collection, 'collection' => $collection,
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $metadata_section_a->get_id(), 'metadata_section_id' => $metadata_section_a->get_id(),
), ),
@ -123,10 +125,17 @@ class MetadataSection extends TAINACAN_UnitTestCase {
$section_a = $Tainacan_Metadata_Section->fetch($metadata_section_a->get_id()); $section_a = $Tainacan_Metadata_Section->fetch($metadata_section_a->get_id());
$section_b = $Tainacan_Metadata_Section->fetch($metadata_section_b->get_id()); $section_b = $Tainacan_Metadata_Section->fetch($metadata_section_b->get_id());
$metadata_list_a = $section_a->get_metadata_list(); $metadata_list_a = $section_a->get_metadata_object_list();
$metadata_list_b = $section_b->get_metadata_list(); $metadata_list_b = $section_b->get_metadata_object_list();
$this->assertContains($test->get_id(), $metadata_list_a); $metadata_list_a = array_map(function($e) {
$this->assertNotContains($test->get_id(), $metadata_list_b); return $e->_toArray();
}, $metadata_list_a);
$metadata_list_b = array_map(function($e) {
return $e->_toArray();
}, $metadata_list_b);
$this->assertContains($test->get_id(), array_column($metadata_list_a, 'id'));
$this->assertNotContains($test->get_id(), array_column($metadata_list_b, 'id'));
$test->set_metadata_section_id($metadata_section_b->get_id()); $test->set_metadata_section_id($metadata_section_b->get_id());
$this->assertTrue($test->validate(), json_encode($test->get_errors())); $this->assertTrue($test->validate(), json_encode($test->get_errors()));
@ -134,10 +143,17 @@ class MetadataSection extends TAINACAN_UnitTestCase {
$test = $Tainacan_Metadata->fetch($metadatum->get_id()); $test = $Tainacan_Metadata->fetch($metadatum->get_id());
$metadata_list_a = $section_a->get_metadata_list(); $metadata_list_a = $section_a->get_metadata_object_list();
$metadata_list_b = $section_b->get_metadata_list(); $metadata_list_b = $section_b->get_metadata_object_list();
$this->assertNotContains($test->get_id(), $metadata_list_a); $metadata_list_a = array_map(function($e) {
$this->assertContains($test->get_id(), $metadata_list_b); return $e->_toArray();
}, $metadata_list_a);
$metadata_list_b = array_map(function($e) {
return $e->_toArray();
}, $metadata_list_b);
$this->assertNotContains($test->get_id(), array_column($metadata_list_a, 'id'));
$this->assertContains($test->get_id(), array_column($metadata_list_b, 'id'));
} }
@ -156,78 +172,6 @@ class MetadataSection extends TAINACAN_UnitTestCase {
true true
); );
$metadatum1 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum1',
'description' => 'descricao',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum2',
'description' => 'metadatum2',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum3 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum3',
'description' => 'metadatum3',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum_a = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_a',
'description' => 'descricao_a',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum_b = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_b',
'description' => 'metadatum_b',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum_c = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_c',
'description' => 'metadatum_c',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadata_section_1 = $this->tainacan_entity_factory->create_entity( $metadata_section_1 = $this->tainacan_entity_factory->create_entity(
'Metadata_Section', 'Metadata_Section',
array( array(
@ -235,7 +179,6 @@ class MetadataSection extends TAINACAN_UnitTestCase {
'description' => 'Section 1 Description', 'description' => 'Section 1 Description',
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadata_list' => [$metadatum1->get_id(), $metadatum3->get_id(), $metadatum2->get_id()]
), ),
true true
); );
@ -247,7 +190,84 @@ class MetadataSection extends TAINACAN_UnitTestCase {
'description' => 'Section A Description', 'description' => 'Section A Description',
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadata_list' => [$metadatum_a->get_id(), $metadatum_b->get_id(), $metadatum_c->get_id()] ),
true
);
$metadatum1 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum1',
'description' => 'descricao',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
'metadata_section_id' => $metadata_section_1->get_id()
),
true
);
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum2',
'description' => 'metadatum2',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
'metadata_section_id' => $metadata_section_1->get_id()
),
true
);
$metadatum3 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum3',
'description' => 'metadatum3',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
'metadata_section_id' => $metadata_section_1->get_id()
),
true
);
$metadatum_a = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_a',
'description' => 'descricao_a',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
'metadata_section_id' => $metadata_section_a->get_id()
),
true
);
$metadatum_b = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_b',
'description' => 'metadatum_b',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
'metadata_section_id' => $metadata_section_a->get_id()
),
true
);
$metadatum_c = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum_c',
'description' => 'metadatum_c',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
'metadata_section_id' => $metadata_section_a->get_id()
), ),
true true
); );