diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php index 64665fc33..1f4d4e532 100644 --- a/src/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -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 diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index 5237127e6..0848a34c8 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -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) * diff --git a/tests/test-api-filters.php b/tests/test-api-filters.php index 03bebda5a..e742adcbe 100644 --- a/tests/test-api-filters.php +++ b/tests/test-api-filters.php @@ -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']); + + } + } ?> diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index 838ddb4fe..a1b2e8a01 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -27,7 +27,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { 'metadata_type' => 'Tainacan\Metadata_Types\Text', ) ); - + $request = new \WP_REST_Request( 'POST', $this->namespace . '/collection/' . $collection->get_id() . '/metadata' @@ -280,7 +280,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { #### UPDATE METADATUM IN COLLECTION #### - + $values = json_encode([ 'name' => 'Dia/Mês/Ano', 'description' => 'Continua descrevendo o dado do campo.' @@ -296,7 +296,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $data = $response->get_data(); - + $this->assertEquals($metadatum->get_id(), $data['id']); $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); $this->assertEquals('19/01/2018', $metav); - + } 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('No name', $data['name']); } - + public function test_return_metadata_type_options_in_get_item() { - + $collection1 = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -416,7 +416,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $collection2 = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -425,9 +425,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $core1 = $collection1->get_core_title_metadatum(); - + $meta_relationship = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -443,7 +443,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $request = new \WP_REST_Request( 'GET', $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($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( @@ -471,7 +471,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $collection2 = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -480,9 +480,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $core1 = $collection1->get_core_title_metadatum(); - + $meta_relationship = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -498,7 +498,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $request = new \WP_REST_Request( 'GET', $this->namespace . '/collection/' . $collection2->get_id() . '/metadata' @@ -507,7 +507,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $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()) { @@ -515,17 +515,17 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { 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( @@ -534,7 +534,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $tax = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -544,7 +544,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $meta = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -558,7 +558,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $request = new \WP_REST_Request( 'GET', $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->get_id(), $data['metadata_type_options']['taxonomy_id']); $this->assertEquals('no', $data['metadata_type_options']['allow_new_terms']); - + } public function test_update_taxonomy_metadata() { @@ -586,7 +586,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $tax = $this->tainacan_entity_factory->create_entity( 'taxonomy', array( @@ -616,7 +616,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { ), true ); - + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -645,7 +645,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $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() @@ -708,12 +708,141 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $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']); + + } + + } -?> \ No newline at end of file +?>