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;
}
// 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 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 );
}
/**
* 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)
*

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

@ -714,6 +714,135 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$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']);
}
}
?>