Merge branch 'release/0.15' of https://github.com/tainacan/tainacan into release/0.15

This commit is contained in:
Vinícius Nunes 2020-02-11 11:27:41 -03:00
commit 8dc2761c44
4 changed files with 456 additions and 31 deletions

View File

@ -255,6 +255,44 @@ class REST_Collections_Controller extends REST_Controller {
$item_arr['total_items']['private'] = $total_items->private; $item_arr['total_items']['private'] = $total_items->private;
} }
// Clear private metadata from metadata_order
if ( is_array( $item_arr['metadata_order'] ) && ! current_user_can( 'tnc_col_' . $item->get_id() . '_read_private_metadata' ) ) {
$metadata = $item->get_metadata();
$meta_ids = array_map(
function($m) {
return $m->get_id();
},
$metadata
);
$item_arr['metadata_order'] = \array_values( \array_filter(
$item_arr['metadata_order'],
function($el) use ($meta_ids) {
return in_array($el['id'], $meta_ids);
}
) );
}
// Clear private filters from filters_order
if ( is_array( $item_arr['filters_order'] ) && ! current_user_can( 'tnc_col_' . $item->get_id() . '_read_private_filters' ) ) {
$filters = $item->get_filters();
$filters_ids = array_map(
function($f) {
return $f->get_id();
},
$filters
);
$item_arr['filters_order'] = \array_values( \array_filter(
$item_arr['filters_order'],
function($el) use ($filters_ids) {
return in_array($el['id'], $filters_ids);
}
) );
}
/** /**
* Use this filter to add additional post_meta to the api response * 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 * Use the $request object to get the context of the request and other variables

View File

@ -477,6 +477,22 @@ class Collection extends Entity {
return $Tainacan_Metadata->fetch_by_collection( $this ); return $Tainacan_Metadata->fetch_by_collection( $this );
} }
/**
* Get collection filters.
*
* Returns an array of \Entity\Filter objects, representing all the filters of the collection.
*
* @see \Tainacan\Repositories\Filters->fetch()
*
* @return [\Tainacan\Entities\Filter] array
* @throws \Exception
*/
function get_filters() {
$Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance();
return $Tainacan_Filters->fetch_by_collection( $this );
}
/** /**
* Get the two core metadata of the collection (title and description) * Get the two core metadata of the collection (title and description)
* *

View File

@ -594,6 +594,248 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
} }
public function test_visibility_the_filter_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
);
$filterA = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'test',
'status' => 'publish',
'collection' => $collection,
'metadatum' => $metadatumA,
'filter_type' => 'Tainacan\Filter_Types\Autocomplete',
),
true
);
$filterB = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'test',
'status' => 'private',
'collection' => $collection,
'metadatum' => $metadatumA,
'filter_type' => 'Tainacan\Filter_Types\Autocomplete',
),
true
);
wp_logout();
wp_set_current_user(0);
$requestA = new \WP_REST_Request('GET', $this->namespace . '/filters/' . $filterA->get_id());
$requestB = new \WP_REST_Request('GET', $this->namespace . '/filters/' . $filterB->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_filters_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
);
$filterA = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'test',
'status' => 'publish',
'collection' => $collection,
'metadatum' => $metadatumA,
'filter_type' => 'Tainacan\Filter_Types\Autocomplete',
),
true
);
$filterB = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'test',
'status' => 'private',
'collection' => $collection,
'metadatum' => $metadatumA,
'filter_type' => 'Tainacan\Filter_Types\Autocomplete',
),
true
);
wp_logout();
wp_set_current_user(0);
$requestA = new \WP_REST_Request('GET', $this->namespace . '/filters/' . $filterA->get_id());
$requestB = new \WP_REST_Request('GET', $this->namespace . '/filters/' . $filterB->get_id());
$requestC = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/filters');
$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(1, count($data));
$this->assertEquals($filterA->get_id(), $data[0]['id']);
}
public function test_private_filter_ids_not_in_filter_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
);
$filterA = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'test',
'status' => 'publish',
'collection' => $collection,
'metadatum' => $metadatumA,
'filter_type' => 'Tainacan\Filter_Types\Autocomplete',
),
true
);
$filterB = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'test',
'status' => 'private',
'collection' => $collection,
'metadatum' => $metadatumA,
'filter_type' => 'Tainacan\Filter_Types\Autocomplete',
),
true
);
$order = array();
$filters = $collection->get_filters();
foreach ( $filters as $f ) {
$order[] = [
'id' => $f->get_id(),
'enabled' => true,
];
}
$collection->set_filters_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(2, count($data['filters_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(1, count($data['filters_order']));
$this->assertNotEquals($filterB->get_id(), $data['filters_order'][0]['id']);
}
} }
?> ?>

View File

@ -27,7 +27,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
) )
); );
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'POST', 'POST',
$this->namespace . '/collection/' . $collection->get_id() . '/metadata' $this->namespace . '/collection/' . $collection->get_id() . '/metadata'
@ -280,7 +280,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
#### UPDATE METADATUM IN COLLECTION #### #### UPDATE METADATUM IN COLLECTION ####
$values = json_encode([ $values = json_encode([
'name' => 'Dia/Mês/Ano', 'name' => 'Dia/Mês/Ano',
'description' => 'Continua descrevendo o dado do campo.' 'description' => 'Continua descrevendo o dado do campo.'
@ -296,7 +296,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals($metadatum->get_id(), $data['id']); $this->assertEquals($metadatum->get_id(), $data['id']);
$this->assertEquals('Dia/Mês/Ano', $data['name']); $this->assertEquals('Dia/Mês/Ano', $data['name']);
@ -304,7 +304,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$metav = get_post_meta($item->get_id(), $data['id'], true); $metav = get_post_meta($item->get_id(), $data['id'], true);
$this->assertEquals('19/01/2018', $metav); $this->assertEquals('19/01/2018', $metav);
} }
public function test_trash_metadatum_in_collection(){ public function test_trash_metadatum_in_collection(){
@ -405,9 +405,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals($metadatum->get_id(), $data['id']); $this->assertEquals($metadatum->get_id(), $data['id']);
$this->assertEquals('No name', $data['name']); $this->assertEquals('No name', $data['name']);
} }
public function test_return_metadata_type_options_in_get_item() { public function test_return_metadata_type_options_in_get_item() {
$collection1 = $this->tainacan_entity_factory->create_entity( $collection1 = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -416,7 +416,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$collection2 = $this->tainacan_entity_factory->create_entity( $collection2 = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -425,9 +425,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$core1 = $collection1->get_core_title_metadatum(); $core1 = $collection1->get_core_title_metadatum();
$meta_relationship = $this->tainacan_entity_factory->create_entity( $meta_relationship = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
@ -443,7 +443,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'GET', 'GET',
$this->namespace . '/metadata/' . $meta_relationship->get_id() $this->namespace . '/metadata/' . $meta_relationship->get_id()
@ -458,11 +458,11 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals('yes', $data['metadata_type_options']['repeated']); $this->assertEquals('yes', $data['metadata_type_options']['repeated']);
$this->assertEquals($collection1->get_id(), $data['metadata_type_options']['collection_id']); $this->assertEquals($collection1->get_id(), $data['metadata_type_options']['collection_id']);
$this->assertEquals($core1->get_id(), $data['metadata_type_options']['search']); $this->assertEquals($core1->get_id(), $data['metadata_type_options']['search']);
} }
public function test_return_metadata_type_options_in_get_items() { public function test_return_metadata_type_options_in_get_items() {
$collection1 = $this->tainacan_entity_factory->create_entity( $collection1 = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -471,7 +471,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$collection2 = $this->tainacan_entity_factory->create_entity( $collection2 = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -480,9 +480,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$core1 = $collection1->get_core_title_metadatum(); $core1 = $collection1->get_core_title_metadatum();
$meta_relationship = $this->tainacan_entity_factory->create_entity( $meta_relationship = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
@ -498,7 +498,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'GET', 'GET',
$this->namespace . '/collection/' . $collection2->get_id() . '/metadata' $this->namespace . '/collection/' . $collection2->get_id() . '/metadata'
@ -507,7 +507,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$data = $response->get_data(); $data = $response->get_data();
//var_dump($data, $this->namespace . '/collection/' . $collection2->get_id() . '/metadata/'); //var_dump($data, $this->namespace . '/collection/' . $collection2->get_id() . '/metadata/');
foreach ($data as $d) { foreach ($data as $d) {
if ($d['id'] == $meta_relationship->get_id()) { if ($d['id'] == $meta_relationship->get_id()) {
@ -515,17 +515,17 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
break; break;
} }
} }
$this->assertEquals($meta_relationship->get_id(), $meta['id']); $this->assertEquals($meta_relationship->get_id(), $meta['id']);
$this->assertEquals('relationship', $meta['name']); $this->assertEquals('relationship', $meta['name']);
$this->assertEquals('yes', $meta['metadata_type_options']['repeated']); $this->assertEquals('yes', $meta['metadata_type_options']['repeated']);
$this->assertEquals($collection1->get_id(), $meta['metadata_type_options']['collection_id']); $this->assertEquals($collection1->get_id(), $meta['metadata_type_options']['collection_id']);
$this->assertEquals($core1->get_id(), $meta['metadata_type_options']['search']); $this->assertEquals($core1->get_id(), $meta['metadata_type_options']['search']);
} }
public function test_return_metadata_type_options_in_get_item_default_option() { public function test_return_metadata_type_options_in_get_item_default_option() {
$collection1 = $this->tainacan_entity_factory->create_entity( $collection1 = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
@ -534,7 +534,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$tax = $this->tainacan_entity_factory->create_entity( $tax = $this->tainacan_entity_factory->create_entity(
'taxonomy', 'taxonomy',
array( array(
@ -544,7 +544,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$meta = $this->tainacan_entity_factory->create_entity( $meta = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
@ -558,7 +558,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'GET', 'GET',
$this->namespace . '/metadata/' . $meta->get_id() $this->namespace . '/metadata/' . $meta->get_id()
@ -572,7 +572,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals('tax', $data['name']); $this->assertEquals('tax', $data['name']);
$this->assertEquals($tax->get_id(), $data['metadata_type_options']['taxonomy_id']); $this->assertEquals($tax->get_id(), $data['metadata_type_options']['taxonomy_id']);
$this->assertEquals('no', $data['metadata_type_options']['allow_new_terms']); $this->assertEquals('no', $data['metadata_type_options']['allow_new_terms']);
} }
public function test_update_taxonomy_metadata() { public function test_update_taxonomy_metadata() {
@ -586,7 +586,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$tax = $this->tainacan_entity_factory->create_entity( $tax = $this->tainacan_entity_factory->create_entity(
'taxonomy', 'taxonomy',
array( array(
@ -616,7 +616,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
), ),
true true
); );
$metadatum = $this->tainacan_entity_factory->create_entity( $metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
@ -645,7 +645,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$itemMeta1->set_value('Rock'); $itemMeta1->set_value('Rock');
$itemMeta1->validate(); $itemMeta1->validate();
$Tainacan_Item_Metadata->insert($itemMeta1); $Tainacan_Item_Metadata->insert($itemMeta1);
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'GET', 'GET',
$this->namespace . '/item/' . $i1->get_id() . '/metadata/' . $metadatum->get_id() $this->namespace . '/item/' . $i1->get_id() . '/metadata/' . $metadatum->get_id()
@ -708,12 +708,141 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($requestA); $response = $this->server->dispatch($requestA);
$status = $response->status; $status = $response->status;
$this->assertEquals(200, $status); $this->assertEquals(200, $status);
$response = $this->server->dispatch($requestB); $response = $this->server->dispatch($requestB);
$status = $response->status; $status = $response->status;
$this->assertEquals(401, $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']);
}
} }
?> ?>