refactor filters and meta fetch_by_collection considering perms #274

This commit is contained in:
Leo Germani 2019-10-21 19:11:41 -03:00
parent 6a5c60ed44
commit 6e7bd268ee
18 changed files with 413 additions and 144 deletions

View File

@ -201,24 +201,20 @@ class REST_Filters_Controller extends REST_Controller {
* @throws \Exception * @throws \Exception
*/ */
public function create_item_permissions_check( $request ) { public function create_item_permissions_check( $request ) {
if(isset($request['collection_id']) && isset($request['metadatum_id'])) { if( isset($request['collection_id']) ) {
$metadata = $this->metadatum_repository->fetch( $request['metadatum_id'] );
$collection = $this->collection_repository->fetch( $request['collection_id'] );
if ( ( $metadata instanceof Entities\Metadatum ) && ( $collection instanceof Entities\Collection ) ) {
return $this->filter_repository->can_edit( new Entities\Filter() ) && $metadata->can_edit() && $collection->can_edit();
}
} elseif (isset($request['collection_id'])){
$collection = $this->collection_repository->fetch( $request['collection_id'] ); $collection = $this->collection_repository->fetch( $request['collection_id'] );
if ( $collection instanceof Entities\Collection ) { if ( $collection instanceof Entities\Collection ) {
return $collection->can_edit(); return current_user_can( 'tnc_col_' . $collection->get_id() . '_manage_filters' );
} }
} else {
return current_user_can( 'tnc_rep_manage_filters' );
} }
return $this->filter_repository->can_edit(new Entities\Metadatum()); return false;
} }
/** /**
@ -386,7 +382,7 @@ class REST_Filters_Controller extends REST_Controller {
$filters = $this->filter_repository->fetch( $args, 'OBJECT' ); $filters = $this->filter_repository->fetch( $args, 'OBJECT' );
} else { } else {
$collection = $this->collection_repository->fetch($request['collection_id']); $collection = $this->collection_repository->fetch($request['collection_id']);
$filters = $this->filter_repository->fetch_by_collection($collection, $args, 'OBJECT'); $filters = $this->filter_repository->fetch_by_collection($collection, $args);
} }
$response = []; $response = [];

View File

@ -352,7 +352,7 @@ class REST_Metadata_Controller extends REST_Controller {
$collection = new Entities\Collection( $collection_id ); $collection = new Entities\Collection( $collection_id );
$result = $this->metadatum_repository->fetch_by_collection( $collection, $args, 'OBJECT' ); $result = $this->metadatum_repository->fetch_by_collection( $collection, $args );
} else { } else {
$args = [ $args = [
'meta_query' => [ 'meta_query' => [

View File

@ -140,7 +140,7 @@ class Elastic_Press {
if ($col) { if ($col) {
$metadata = $Tainacan_Metadata->fetch_by_collection($col, ['posts_per_page' => -1], 'OBJECT'); $metadata = $Tainacan_Metadata->fetch_by_collection($col, ['posts_per_page' => -1]);
foreach ($metadata as $meta) { foreach ($metadata as $meta) {
$meta_ids[] = $meta->get_id(); $meta_ids[] = $meta->get_id();
@ -200,7 +200,7 @@ class Elastic_Press {
foreach ( $args['post_type'] as $cpt ) { foreach ( $args['post_type'] as $cpt ) {
$col = $Tainacan_Collections->fetch_by_db_identifier($cpt); $col = $Tainacan_Collections->fetch_by_db_identifier($cpt);
$_filters = $Tainacan_Filters->fetch_by_collection($col, ['posts_per_page' => -1], 'OBJECT'); $_filters = $Tainacan_Filters->fetch_by_collection($col, ['posts_per_page' => -1]);
foreach ($_filters as $filter) { foreach ($_filters as $filter) {
$include = []; $include = [];
$filter_id = $filter->get_id(); $filter_id = $filter->get_id();

View File

@ -484,7 +484,7 @@ class Collection extends Entity {
function get_metadata() { function get_metadata() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
return $Tainacan_Metadata->fetch_by_collection( $this, [], 'OBJECT' ); return $Tainacan_Metadata->fetch_by_collection( $this );
} }
/** /**

View File

@ -351,41 +351,87 @@ class Filters extends Repository {
* *
* @param Entities\Collection $collection * @param Entities\Collection $collection
* @param array $args WP_Query args plus disabled_metadata * @param array $args WP_Query args plus disabled_metadata
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
* *
* @return array Entities\Metadatum * @return array Entities\Metadatum
* @throws \Exception * @throws \Exception
*/ */
public function fetch_by_collection( Entities\Collection $collection, $args = [], $output = null ) { public function fetch_by_collection( Entities\Collection $collection, $args = [] ) {
$collection_id = $collection->get_id(); $collection_id = $collection->get_id();
//get parent collections //get parent collections
$parents = get_post_ancestors( $collection_id ); $parents = get_post_ancestors( $collection_id );
//insert the actual collection //insert the actual collection
$parents[] = $collection_id; if ( is_numeric($collection_id) ) {
$parents[] = $collection_id;
}
//search for default metadatum //search for default metadatum
$parents[] = 'default'; $parents[] = 'default';
$meta_query = array( $results = [];
'key' => 'collection_id',
'value' => $parents,
'compare' => 'IN',
);
$args = array_merge( [ $args = array_merge( [
'parent' => 0 'parent' => 0
], $args ); ], $args );
if ( isset( $args['meta_query'] ) ) { $original_meta_q = isset( $args['meta_query'] ) ? $args['meta_query'] : [];
$args['meta_query'][] = $meta_query;
/**
* Since we introduced roles & capabalities management, we can not rely
* on WordPress behavior when handling default post status values.
* WordPress checks if the current user can read_priva_posts, but this is
* not enough for us. We have to handle this ourselves to mimic WordPress behavior
* considering how tainacan manages metadata capabilities
*/
if ( ! isset($args['post_status']) ) {
foreach ( $parents as $parent_id ) {
// Add public states.
$statuses = get_post_stati( array( 'public' => true ) );
$read_private_cap = 'default' == $parent_id ? 'tnc_rep_read_private_filters' : 'tnc_col_' . $parent_id . '_read_private_filters';
if ( current_user_can($read_private_cap) ) {
$statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) );
}
$args['post_status'] = $statuses;
$meta_query = array(
'key' => 'collection_id',
'value' => $parent_id,
);
$args['meta_query'] = $original_meta_q;
$args['meta_query'][] = $meta_query;
//var_dump($args);
$results = array_merge($results, $this->fetch( $args, 'OBJECT' ));
}
} else { } else {
$args['meta_query'] = array( $meta_query ); $meta_query = array(
'key' => 'collection_id',
'value' => $parents,
'compare' => 'IN',
);
$args = array_merge( [
'parent' => 0
], $args );
$args['meta_query'] = $original_meta_q;
$args['meta_query'][] = $meta_query;
$results = $this->fetch( $args, 'OBJECT' );
} }
return $this->order_result( return $this->order_result(
$this->fetch( $args, $output ), $results,
$collection, $collection,
isset( $args['include_disabled'] ) ? $args['include_disabled'] : false isset( $args['include_disabled'] ) ? $args['include_disabled'] : false
); );
@ -414,28 +460,72 @@ class Filters extends Repository {
$parents = get_post_ancestors( $collection_id ); $parents = get_post_ancestors( $collection_id );
//insert the actual collection //insert the actual collection
$parents[] = $collection_id; if ( is_numeric($collection_id) ) {
$parents[] = $collection_id;
}
//search for default metadatum //search for default metadatum
$parents[] = 'default'; $parents[] = 'default';
$meta_query = array( $results = [];
'key' => 'collection_id',
'value' => $parents,
'compare' => 'IN',
);
$args = array_merge( [ $args = array_merge( [
'parent' => 0 'parent' => 0
], $args ); ], $args );
if ( isset( $args['meta_query'] ) ) { $original_meta_q = isset( $args['meta_query'] ) ? $args['meta_query'] : [];
/**
* Since we introduced roles & capabalities management, we can not rely
* on WordPress behavior when handling default post status values.
* WordPress checks if the current user can read_priva_posts, but this is
* not enough for us. We have to handle this ourselves to mimic WordPress behavior
* considering how tainacan manages metadata capabilities
*/
if ( ! isset($args['post_status']) ) {
foreach ( $parents as $parent_id ) {
// Add public states.
$statuses = get_post_stati( array( 'public' => true ) );
$read_private_cap = 'default' == $parent_id ? 'tnc_rep_read_private_filters' : 'tnc_col_' . $parent_id . '_read_private_filters';
if ( current_user_can($read_private_cap) ) {
$statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) );
}
$args['post_status'] = $statuses;
$meta_query = array(
'key' => 'collection_id',
'value' => $parent_id,
);
$args['meta_query'] = $original_meta_q;
$args['meta_query'][] = $meta_query;
$results = array_merge($results, $this->fetch_ids( $args ));
}
} else {
$meta_query = array(
'key' => 'collection_id',
'value' => $parents,
'compare' => 'IN',
);
$args = array_merge( [
'parent' => 0
], $args );
$args['meta_query'] = $original_meta_q;
$args['meta_query'][] = $meta_query; $args['meta_query'][] = $meta_query;
} elseif ( is_array( $args ) ) {
$args['meta_query'] = array( $meta_query ); $results = $this->fetch_ids( $args );
} }
return $this->fetch_ids( $args ); return $results;
} }
/** /**

View File

@ -229,7 +229,7 @@ class Item_Metadata extends Repository {
return []; return [];
} }
$meta_list = $Tainacan_Metadata->fetch_by_collection( $collection, $args, 'OBJECT' ); $meta_list = $Tainacan_Metadata->fetch_by_collection( $collection, $args );
$return = []; $return = [];

View File

@ -364,41 +364,87 @@ class Metadata extends Repository {
* *
* @param Entities\Collection $collection * @param Entities\Collection $collection
* @param array $args WP_Query args plus disabled_metadata * @param array $args WP_Query args plus disabled_metadata
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
* *
* @return array Entities\Metadatum * @return array Entities\Metadatum
* @throws \Exception * @throws \Exception
*/ */
public function fetch_by_collection( Entities\Collection $collection, $args = [], $output = null ) { public function fetch_by_collection( Entities\Collection $collection, $args = [] ) {
$collection_id = $collection->get_id(); $collection_id = $collection->get_id();
//get parent collections //get parent collections
$parents = get_post_ancestors( $collection_id ); $parents = get_post_ancestors( $collection_id );
//insert the actual collection //insert the actual collection
$parents[] = $collection_id; if ( is_numeric($collection_id) ) {
$parents[] = $collection_id;
}
//search for default metadatum //search for default metadatum
$parents[] = $this->get_default_metadata_attribute(); $parents[] = $this->get_default_metadata_attribute();
$meta_query = array( $results = [];
'key' => 'collection_id',
'value' => $parents,
'compare' => 'IN',
);
$args = array_merge( [ $args = array_merge( [
'parent' => 0 'parent' => 0
], $args ); ], $args );
if ( isset( $args['meta_query'] ) ) { $original_meta_q = isset( $args['meta_query'] ) ? $args['meta_query'] : [];
/**
* Since we introduced roles & capabalities management, we can not rely
* on WordPress behavior when handling default post status values.
* WordPress checks if the current user can read_priva_posts, but this is
* not enough for us. We have to handle this ourselves to mimic WordPress behavior
* considering how tainacan manages metadata capabilities
*/
if ( ! isset($args['post_status']) ) {
foreach ( $parents as $parent_id ) {
// Add public states.
$statuses = get_post_stati( array( 'public' => true ) );
$read_private_cap = $this->get_default_metadata_attribute() == $parent_id ? 'tnc_rep_read_private_metadata' : 'tnc_col_' . $parent_id . '_read_private_metadata';
if ( current_user_can($read_private_cap) ) {
$statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) );
}
$args['post_status'] = $statuses;
$meta_query = array(
'key' => 'collection_id',
'value' => $parent_id,
);
$args['meta_query'] = $original_meta_q;
$args['meta_query'][] = $meta_query;
//var_dump($args);
$results = array_merge($results, $this->fetch( $args, 'OBJECT' ));
}
} else {
$meta_query = array(
'key' => 'collection_id',
'value' => $parents,
'compare' => 'IN',
);
$args = array_merge( [
'parent' => 0
], $args );
$args['meta_query'] = $original_meta_q;
$args['meta_query'][] = $meta_query; $args['meta_query'][] = $meta_query;
} elseif ( is_array( $args ) ) {
$args['meta_query'] = array( $meta_query ); $results = $this->fetch( $args, 'OBJECT' );
} }
return $this->order_result( return $this->order_result(
$this->fetch( $args, $output ), $results,
$collection, $collection,
isset( $args['include_disabled'] ) ? $args['include_disabled'] : false isset( $args['include_disabled'] ) ? $args['include_disabled'] : false
); );
@ -427,28 +473,72 @@ class Metadata extends Repository {
$parents = get_post_ancestors( $collection_id ); $parents = get_post_ancestors( $collection_id );
//insert the actual collection //insert the actual collection
$parents[] = $collection_id; if ( is_numeric($collection_id) ) {
$parents[] = $collection_id;
}
//search for default metadatum //search for default metadatum
$parents[] = $this->get_default_metadata_attribute(); $parents[] = $this->get_default_metadata_attribute();
$meta_query = array( $results = [];
'key' => 'collection_id',
'value' => $parents,
'compare' => 'IN',
);
$args = array_merge( [ $args = array_merge( [
'parent' => 0 'parent' => 0
], $args ); ], $args );
if ( isset( $args['meta_query'] ) ) { $original_meta_q = isset( $args['meta_query'] ) ? $args['meta_query'] : [];
/**
* Since we introduced roles & capabalities management, we can not rely
* on WordPress behavior when handling default post status values.
* WordPress checks if the current user can read_priva_posts, but this is
* not enough for us. We have to handle this ourselves to mimic WordPress behavior
* considering how tainacan manages metadata capabilities
*/
if ( ! isset($args['post_status']) ) {
foreach ( $parents as $parent_id ) {
// Add public states.
$statuses = get_post_stati( array( 'public' => true ) );
$read_private_cap = $this->get_default_metadata_attribute() == $parent_id ? 'tnc_rep_read_private_metadata' : 'tnc_col_' . $parent_id . '_read_private_metadata';
if ( current_user_can($read_private_cap) ) {
$statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) );
}
$args['post_status'] = $statuses;
$meta_query = array(
'key' => 'collection_id',
'value' => $parent_id,
);
$args['meta_query'] = $original_meta_q;
$args['meta_query'][] = $meta_query;
$results = array_merge($results, $this->fetch_ids( $args ));
}
} else {
$meta_query = array(
'key' => 'collection_id',
'value' => $parents,
'compare' => 'IN',
);
$args = array_merge( [
'parent' => 0
], $args );
$args['meta_query'] = $original_meta_q;
$args['meta_query'][] = $meta_query; $args['meta_query'][] = $meta_query;
} elseif ( is_array( $args ) ) {
$args['meta_query'] = array( $meta_query ); $results = $this->fetch_ids( $args );
} }
return $this->fetch_ids( $args ); return $results;
} }
/** /**
@ -456,11 +546,11 @@ class Metadata extends Repository {
* metadata not ordinated appear on the end of the list * metadata not ordinated appear on the end of the list
* *
* *
* @param \WP_Query|array $result Response from method fetch * @param array $result Response from method fetch_by_collection
* @param Entities\Collection $collection * @param Entities\Collection $collection
* @param bool $include_disabled Wether to include disabled metadata in the results or not * @param bool $include_disabled Wether to include disabled metadata in the results or not
* *
* @return array or WP_Query ordinate * @return array
*/ */
public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) { public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) {
$order = $collection->get_metadata_order(); $order = $collection->get_metadata_order();
@ -495,29 +585,8 @@ class Metadata extends Repository {
$result_ordinate = array_merge( $result_ordinate, $not_ordinate ); $result_ordinate = array_merge( $result_ordinate, $not_ordinate );
return $result_ordinate; return $result_ordinate;
} // if the result is a wp query object
else {
$posts = $result->posts;
$result_ordinate = [];
$not_ordinate = [];
foreach ( $posts as $item ) {
$id = $item->ID;
$index = array_search( $id, array_column( $order, 'id' ) );
if ( $index !== false ) {
$result_ordinate[ $index ] = $item;
} else {
$not_ordinate[] = $item;
}
}
ksort( $result_ordinate );
$result->posts = $result_ordinate;
$result->posts = array_merge( $result->posts, $not_ordinate );
return $result;
} }
} }
return $result; return $result;
@ -761,7 +830,7 @@ class Metadata extends Repository {
] ]
], ],
'include_disabled' => true 'include_disabled' => true
], 'OBJECT' ); ] );
} }
@ -783,7 +852,7 @@ class Metadata extends Repository {
] ]
], ],
'posts_per_page' => 1 'posts_per_page' => 1
], 'OBJECT' ); ] );
if ( is_array( $results ) && sizeof( $results ) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum ) { if ( is_array( $results ) && sizeof( $results ) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum ) {
return $results[0]; return $results[0];
@ -810,7 +879,7 @@ class Metadata extends Repository {
] ]
], ],
'posts_per_page' => 1 'posts_per_page' => 1
], 'OBJECT' ); ] );
if ( is_array( $results ) && sizeof( $results ) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum ) { if ( is_array( $results ) && sizeof( $results ) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum ) {
return $results[0]; return $results[0];

View File

@ -206,18 +206,17 @@ class Taxonomies extends Repository {
* *
* @param Entities\Collection $collection * @param Entities\Collection $collection
* @param array $args WP_Query args plus disabled_metadata * @param array $args WP_Query args plus disabled_metadata
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
* *
* @return array Entities\Taxonomy * @return array Entities\Taxonomy
* @throws \Exception * @throws \Exception
*/ */
public function fetch_by_collection( Entities\Collection $collection, $args = [], $output = null ) { public function fetch_by_collection( Entities\Collection $collection, $args = [] ) {
$collection_id = $collection->get_id(); $collection_id = $collection->get_id();
$Tainacan_Metadata = Metadata::get_instance(); $Tainacan_Metadata = Metadata::get_instance();
// get all taxonomy metadata in this collection // get all taxonomy metadata in this collection
$taxonomy_metas = $Tainacan_Metadata->fetch_by_collection($collection, ['metadata_type' => 'Tainacan\Metadata_Types\Taxonomy'], 'OBJECT'); $taxonomy_metas = $Tainacan_Metadata->fetch_by_collection($collection, ['metadata_type' => 'Tainacan\Metadata_Types\Taxonomy']);
$tax_ids = []; $tax_ids = [];
@ -237,7 +236,7 @@ class Taxonomies extends Repository {
]; ];
$args = array_merge($args, $newargs); $args = array_merge($args, $newargs);
return $this->fetch($args, $output); return $this->fetch($args, 'OBJECT');
} }
@ -339,7 +338,7 @@ class Taxonomies extends Repository {
// register taxonomies to other collections considering metadata inheritance // register taxonomies to other collections considering metadata inheritance
foreach ( $collections as $collection ) { foreach ( $collections as $collection ) {
$taxonomies = $this->fetch_by_collection($collection, ['nopaging' => true], 'OBJECT'); $taxonomies = $this->fetch_by_collection($collection, ['nopaging' => true]);
foreach ( $taxonomies as $taxonomy ) { foreach ( $taxonomies as $taxonomy ) {
register_taxonomy_for_object_type( $taxonomy->get_db_identifier(), $collection->get_db_identifier() ); register_taxonomy_for_object_type( $taxonomy->get_db_identifier(), $collection->get_db_identifier() );
} }

View File

@ -505,7 +505,7 @@ class DevInterface {
} }
$metalist = $Tainacan_Metadata->fetch_by_collection($cpts[$post_type], [], 'OBJECT'); $metalist = $Tainacan_Metadata->fetch_by_collection( $cpts[$post_type] );
foreach ($metalist as $meta) { foreach ($metalist as $meta) {
$item_meta = new \Tainacan\Entities\Item_Metadata_Entity($entity, $meta); $item_meta = new \Tainacan\Entities\Item_Metadata_Entity($entity, $meta);

View File

@ -53,7 +53,7 @@ use Tainacan\Entities;
public static function metadata_dropdown( $collection , $selected, $name_metadatum = 'tnc_prop_metadatum_id', $args = []){ public static function metadata_dropdown( $collection , $selected, $name_metadatum = 'tnc_prop_metadatum_id', $args = []){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = ( is_numeric( $collection ) ) ? new Entities\Collection( $collection ) : $collection; $collection = ( is_numeric( $collection ) ) ? new Entities\Collection( $collection ) : $collection;
$metadatum = $Tainacan_Metadata->fetch_by_collection( $collection, $args, 'OBJECT'); $metadatum = $Tainacan_Metadata->fetch_by_collection( $collection, $args);
?> ?>
<select name="<?php echo $name_metadatum ?>"> <select name="<?php echo $name_metadatum ?>">
<option value=""><?php echo __('Select an option','tainacan') ?>...</option> <option value=""><?php echo __('Select an option','tainacan') ?>...</option>
@ -76,7 +76,7 @@ use Tainacan\Entities;
public static function metadata_checkbox_list( $collection , $selected,$name_metadatum = 'tnc_prop_tnc_metadatum_ids[]', $args = []) { public static function metadata_checkbox_list( $collection , $selected,$name_metadatum = 'tnc_prop_tnc_metadatum_ids[]', $args = []) {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = ( is_numeric( $collection ) ) ? new Entities\Collection( $collection ) : $collection; $collection = ( is_numeric( $collection ) ) ? new Entities\Collection( $collection ) : $collection;
$metadatum = $Tainacan_Metadata->fetch_by_collection( $collection, $args, 'OBJECT'); $metadatum = $Tainacan_Metadata->fetch_by_collection( $collection, $args);
$selected = ( is_array( $selected) ) ? $selected : json_decode($selected); $selected = ( is_array( $selected) ) ? $selected : json_decode($selected);
$selected = ( $selected ) ? $selected : []; $selected = ( $selected ) ? $selected : [];
?> ?>

View File

@ -459,7 +459,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$collection = $Tainacan_collections->fetch($id); $collection = $Tainacan_collections->fetch($id);
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [ 'order' => 'id' ], 'OBJECT' ); $metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [ 'order' => 'id' ] );
$this->assertEquals(count($mapper->metadata), count($metadata)); $this->assertEquals(count($mapper->metadata), count($metadata));
foreach ($metadata as $metadatum) { foreach ($metadata as $metadatum) {

View File

@ -667,7 +667,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
true true
); );
// $metas = \Tainacan\Repositories\Metadata::get_instance()->fetch_by_collection($collection, [], 'OBJECT'); // $metas = \Tainacan\Repositories\Metadata::get_instance()->fetch_by_collection($collection);
// //
// foreach ($metas as $m) { // foreach ($metas as $m) {
// var_dump($m->get_name()); // var_dump($m->get_name());

View File

@ -18,6 +18,26 @@ class Capabilities extends TAINACAN_UnitTestCase {
function setUp() { function setUp() {
parent::setUp(); parent::setUp();
/**
* Test fixtures:
*
* Repo
* - public_taxonomy
* - private_taxonomy
* - public_repo_metadatum
* - private_repo_metadatum
* - public_repo_filter
* - private_repo_filter
* - public_collection (5 items)
* --- (Core Title adn Description)
* --- public_metadatum
* --- private_metadatum
* --- public_filter
* --- private_filter
* - private_collection (5 items)
* --- (Core Title adn Description)
* --- meta_relationshipt (with public collection)
*/
$subscriber = $this->factory()->user->create(array( 'role' => 'subscriber' )); $subscriber = $this->factory()->user->create(array( 'role' => 'subscriber' ));
$this->subscriber = get_userdata( $subscriber ); $this->subscriber = get_userdata( $subscriber );
@ -26,7 +46,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$collection1 = $this->tainacan_entity_factory->create_entity( $collection1 = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
'name' => 'test_col', 'name' => 'Public Col',
'status' => 'publish' 'status' => 'publish'
), ),
true true
@ -36,7 +56,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$collection2 = $this->tainacan_entity_factory->create_entity( $collection2 = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
'name' => 'test_col', 'name' => 'Private Col',
'status' => 'private' 'status' => 'private'
), ),
true true
@ -46,7 +66,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$taxonomy = $this->tainacan_entity_factory->create_entity( $taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy', 'taxonomy',
array( array(
'name' => 'genero', 'name' => 'Public Tax',
'description' => 'tipos de musica', 'description' => 'tipos de musica',
'allow_insert' => 'yes', 'allow_insert' => 'yes',
'status' => 'publish' 'status' => 'publish'
@ -59,10 +79,10 @@ class Capabilities extends TAINACAN_UnitTestCase {
$taxonomy2 = $this->tainacan_entity_factory->create_entity( $taxonomy2 = $this->tainacan_entity_factory->create_entity(
'taxonomy', 'taxonomy',
array( array(
'name' => 'genero2', 'name' => 'Private Tax',
'description' => 'tipos de musica2', 'description' => 'tipos de musica2',
'allow_insert' => 'yes', 'allow_insert' => 'yes',
'status' => 'publish' 'status' => 'private'
), ),
true true
); );
@ -97,7 +117,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$metadatum_text = $this->tainacan_entity_factory->create_entity( $metadatum_text = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
'name' => 'text', 'name' => 'Public meta',
'status' => 'publish', 'status' => 'publish',
'collection' => $collection1, 'collection' => $collection1,
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
@ -110,7 +130,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$metadatum_repo = $this->tainacan_entity_factory->create_entity( $metadatum_repo = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
'name' => 'repo', 'name' => 'Public Repo Meta',
'status' => 'publish', 'status' => 'publish',
'collection_id' => 'default', 'collection_id' => 'default',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
@ -123,7 +143,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$metadatum_text2 = $this->tainacan_entity_factory->create_entity( $metadatum_text2 = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
'name' => 'text', 'name' => 'Private Meta',
'status' => 'private', 'status' => 'private',
'collection' => $collection1, 'collection' => $collection1,
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
@ -136,7 +156,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$metadatum_repo2 = $this->tainacan_entity_factory->create_entity( $metadatum_repo2 = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
array( array(
'name' => 'repo', 'name' => 'Private Repo Meta',
'status' => 'private', 'status' => 'private',
'collection_id' => 'default', 'collection_id' => 'default',
'metadata_type' => 'Tainacan\Metadata_Types\Text', 'metadata_type' => 'Tainacan\Metadata_Types\Text',
@ -167,7 +187,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$filter = $this->tainacan_entity_factory->create_entity( $filter = $this->tainacan_entity_factory->create_entity(
'filter', 'filter',
array( array(
'name' => 'filtro', 'name' => 'Public filter',
'collection' => $collection1, 'collection' => $collection1,
'description' => 'Teste Filtro', 'description' => 'Teste Filtro',
'status' => 'publish', 'status' => 'publish',
@ -181,7 +201,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$filter = $this->tainacan_entity_factory->create_entity( $filter = $this->tainacan_entity_factory->create_entity(
'filter', 'filter',
array( array(
'name' => 'filtro', 'name' => 'Public repo filter',
'collection_id' => 'default', 'collection_id' => 'default',
'description' => 'Teste Filtro', 'description' => 'Teste Filtro',
'status' => 'publish', 'status' => 'publish',
@ -195,7 +215,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$filter = $this->tainacan_entity_factory->create_entity( $filter = $this->tainacan_entity_factory->create_entity(
'filter', 'filter',
array( array(
'name' => 'filtro', 'name' => 'Private Filter',
'collection' => $collection1, 'collection' => $collection1,
'description' => 'Teste Filtro', 'description' => 'Teste Filtro',
'status' => 'private', 'status' => 'private',
@ -209,7 +229,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
$filter = $this->tainacan_entity_factory->create_entity( $filter = $this->tainacan_entity_factory->create_entity(
'filter', 'filter',
array( array(
'name' => 'filtro', 'name' => 'Private repo filter',
'collection_id' => 'default', 'collection_id' => 'default',
'description' => 'Teste Filtro', 'description' => 'Teste Filtro',
'status' => 'private', 'status' => 'private',
@ -260,7 +280,6 @@ class Capabilities extends TAINACAN_UnitTestCase {
$this->assertFalse( user_can($this->subscriber, 'tnc_rep_manage_taxonomies') ); $this->assertFalse( user_can($this->subscriber, 'tnc_rep_manage_taxonomies') );
$this->subscriber->add_cap('manage_tainacan'); $this->subscriber->add_cap('manage_tainacan');
$this->subscriber->get_role_caps();
$this->assertTrue( user_can($this->subscriber, 'tnc_rep_manage_taxonomies') ); $this->assertTrue( user_can($this->subscriber, 'tnc_rep_manage_taxonomies') );
@ -271,7 +290,6 @@ class Capabilities extends TAINACAN_UnitTestCase {
$this->assertFalse( user_can($this->subscriber, 'tnc_col_25_read_private_filters') ); $this->assertFalse( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
$this->subscriber->add_cap('manage_tainacan_collection_25'); $this->subscriber->add_cap('manage_tainacan_collection_25');
$this->subscriber->get_role_caps();
$this->assertTrue( user_can($this->subscriber, 'tnc_col_25_read_private_filters') ); $this->assertTrue( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
$this->assertFalse( user_can($this->subscriber, 'tnc_col_36_read_private_filters') ); $this->assertFalse( user_can($this->subscriber, 'tnc_col_36_read_private_filters') );
@ -283,12 +301,18 @@ class Capabilities extends TAINACAN_UnitTestCase {
$this->assertFalse( user_can($this->subscriber, 'tnc_col_25_read_private_filters') ); $this->assertFalse( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
$this->subscriber->add_cap('tnc_col_all_read_private_filters'); $this->subscriber->add_cap('tnc_col_all_read_private_filters');
$this->subscriber->get_role_caps();
$this->assertTrue( user_can($this->subscriber, 'tnc_col_25_read_private_filters') ); $this->assertTrue( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
$this->assertTrue( user_can($this->subscriber, 'tnc_col_36_read_private_filters') ); $this->assertTrue( user_can($this->subscriber, 'tnc_col_36_read_private_filters') );
$this->assertFalse( user_can($this->subscriber, 'tnc_col_25_edit_posts') ); $this->assertFalse( user_can($this->subscriber, 'tnc_col_25_edit_posts') );
$this->assertFalse( user_can($this->subscriber2, 'tnc_col_25_read_private_filters') );
$this->subscriber2->add_cap('manage_tainacan_collection_all');
$this->assertTrue( user_can($this->subscriber2, 'tnc_col_25_read_private_filters') );
$this->assertTrue( user_can($this->subscriber2, 'tnc_col_36_read_private_filters') );
} }
// function test_items_capabilities() { // function test_items_capabilities() {
@ -336,4 +360,95 @@ class Capabilities extends TAINACAN_UnitTestCase {
} }
/**
* @group filters
*/
function test_filters_metacap() {
wp_set_current_user($this->subscriber2->ID);
$this->assertFalse( $this->public_filter->can_edit() );
$this->assertFalse( $this->public_repo_filter->can_edit() );
$this->assertFalse( $this->private_filter->can_edit() );
$this->assertFalse( $this->private_repo_filter->can_edit() );
$this->subscriber2->add_cap( 'tnc_rep_manage_filters' );
$this->assertFalse( $this->public_filter->can_edit() );
$this->assertTrue( $this->public_repo_filter->can_edit() );
$this->assertFalse( $this->private_filter->can_edit() );
$this->assertTrue( $this->private_repo_filter->can_edit() );
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_manage_filters' );
$this->assertTrue( $this->public_filter->can_edit() );
$this->assertTrue( $this->public_repo_filter->can_edit() );
$this->assertTrue( $this->private_filter->can_edit() );
$this->assertTrue( $this->private_repo_filter->can_edit() );
}
/**
* @group fetch_by_collection
*/
function test_fetch_meta_by_collection() {
global $current_user;
wp_set_current_user($this->subscriber2->ID);
$meta = tainacan_metadata()->fetch_by_collection($this->public_collection);
$this->AssertEquals(4, sizeof($meta));
$meta = tainacan_metadata()->fetch_ids_by_collection($this->public_collection);
$this->AssertEquals(4, sizeof($meta));
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_read_private_metadata' );
$current_user = $this->subscriber2; // force update current user object with new capabilities
$meta = tainacan_metadata()->fetch_by_collection($this->public_collection);
$this->AssertEquals(5, sizeof($meta));
$meta = tainacan_metadata()->fetch_ids_by_collection($this->public_collection);
$this->AssertEquals(5, sizeof($meta));
$this->subscriber2->add_cap( 'tnc_rep_read_private_metadata' );
$current_user = $this->subscriber2; // force update current user object with new capabilities
$meta = tainacan_metadata()->fetch_by_collection($this->public_collection);
$this->AssertEquals(6, sizeof($meta));
$meta = tainacan_metadata()->fetch_ids_by_collection($this->public_collection);
$this->AssertEquals(6, sizeof($meta));
}
/**
* @group fetch_by_collection
*/
function test_fetch_filter_by_collection() {
global $current_user;
wp_set_current_user($this->subscriber2->ID);
$meta = tainacan_filters()->fetch_by_collection($this->public_collection);
$this->AssertEquals(2, sizeof($meta));
$meta = tainacan_filters()->fetch_ids_by_collection($this->public_collection);
$this->AssertEquals(2, sizeof($meta));
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_read_private_filters' );
$current_user = $this->subscriber2; // force update current user object with new capabilities
$meta = tainacan_filters()->fetch_by_collection($this->public_collection);
$this->AssertEquals(3, sizeof($meta));
$meta = tainacan_filters()->fetch_ids_by_collection($this->public_collection);
$this->AssertEquals(3, sizeof($meta));
$this->subscriber2->add_cap( 'tnc_rep_read_private_filters' );
$current_user = $this->subscriber2; // force update current user object with new capabilities
$meta = tainacan_filters()->fetch_by_collection($this->public_collection);
$this->AssertEquals(4, sizeof($meta));
$meta = tainacan_filters()->fetch_ids_by_collection($this->public_collection);
$this->AssertEquals(4, sizeof($meta));
}
} }

View File

@ -525,16 +525,16 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
true true
); );
$taxonomies_1 = $Tainacan_Taxonomies->fetch_by_collection($collection, [], 'OBJECT'); $taxonomies_1 = $Tainacan_Taxonomies->fetch_by_collection($collection);
$this->assertEquals(2, sizeof($taxonomies_1)); $this->assertEquals(2, sizeof($taxonomies_1));
$taxonomies_2 = $Tainacan_Taxonomies->fetch_by_collection($collection2, [], 'OBJECT'); $taxonomies_2 = $Tainacan_Taxonomies->fetch_by_collection($collection2);
$this->assertEquals(2, sizeof($taxonomies_2)); $this->assertEquals(2, sizeof($taxonomies_2));
$taxonomies_3 = $Tainacan_Taxonomies->fetch_by_collection($collection2_c, [], 'OBJECT'); $taxonomies_3 = $Tainacan_Taxonomies->fetch_by_collection($collection2_c);
$this->assertEquals(3, sizeof($taxonomies_3)); $this->assertEquals(3, sizeof($taxonomies_3));
$taxonomies_4 = $Tainacan_Taxonomies->fetch_by_collection($collection2_gc, [], 'OBJECT'); $taxonomies_4 = $Tainacan_Taxonomies->fetch_by_collection($collection2_gc);
$this->assertEquals(3, sizeof($taxonomies_4)); $this->assertEquals(3, sizeof($taxonomies_4));

View File

@ -115,7 +115,7 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase {
true true
); );
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ; $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ;
foreach ( $metadata as $index => $metadatum ){ foreach ( $metadata as $index => $metadatum ){
if ( $metadatum->get_metadata_type_object()->get_core() && $metadatum->get_metadata_type_object()->get_related_mapped_prop() == 'title') { if ( $metadatum->get_metadata_type_object()->get_core() && $metadatum->get_metadata_type_object()->get_related_mapped_prop() == 'title') {
@ -150,7 +150,7 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase {
true true
); );
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ; $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ;
foreach ( $metadata as $index => $metadatum ){ foreach ( $metadata as $index => $metadatum ){
if ( $metadatum->get_metadata_type_object()->get_core() && $metadatum->get_metadata_type_object()->get_related_mapped_prop() == 'title') { if ( $metadatum->get_metadata_type_object()->get_core() && $metadatum->get_metadata_type_object()->get_related_mapped_prop() == 'title') {

View File

@ -281,7 +281,7 @@ class Filters extends TAINACAN_UnitTestCase {
true true
); );
$retrieve_filters = $Tainacan_Filters->fetch_by_collection( $collection_son, [], 'OBJECT' ); $retrieve_filters = $Tainacan_Filters->fetch_by_collection( $collection_son );
$retrieve_filters_ids = $Tainacan_Filters->fetch_ids_by_collection( $collection_son, [] ); $retrieve_filters_ids = $Tainacan_Filters->fetch_ids_by_collection( $collection_son, [] );

View File

@ -109,7 +109,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$_SESSION['tainacan_importer'][$id]->set_collection( $collection ); $_SESSION['tainacan_importer'][$id]->set_collection( $collection );
// get collection metadata to map // get collection metadata to map
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ; $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ;
//create a random mapping //create a random mapping
$map = []; $map = [];
@ -202,7 +202,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
]; ];
// get collection metadata to map // get collection metadata to map
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ; $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ;
//create a random mapping //create a random mapping
$map = []; $map = [];
@ -364,7 +364,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
]; ];
// get collection metadata to map // get collection metadata to map
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ; $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ;
//create a random mapping //create a random mapping
$map = []; $map = [];
@ -507,7 +507,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
]; ];
// get collection metadata to map // get collection metadata to map
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ; $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ;
//create a random mapping //create a random mapping
$map = []; $map = [];
@ -646,7 +646,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
]; ];
// get collection metadata to map // get collection metadata to map
$metadata = $Tainacan_Metadata->fetch_by_collection( $collection, [], 'OBJECT' ) ; $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ;
//create a random mapping //create a random mapping
$map = []; $map = [];

View File

@ -160,7 +160,7 @@ class Metadata extends TAINACAN_UnitTestCase {
true true
); );
$retrieve_metadata = $Tainacan_Metadata->fetch_by_collection( $collection_son, [], 'OBJECT' ); $retrieve_metadata = $Tainacan_Metadata->fetch_by_collection( $collection_son );
$retrieve_metadata_ids = $Tainacan_Metadata->fetch_ids_by_collection( $collection_son, [] ); $retrieve_metadata_ids = $Tainacan_Metadata->fetch_ids_by_collection( $collection_son, [] );
@ -262,10 +262,10 @@ class Metadata extends TAINACAN_UnitTestCase {
$update_collection = $Tainacan_Collections->update( $collection ); $update_collection = $Tainacan_Collections->update( $collection );
$metadata_ordinate = $Tainacan_Metadata->fetch_by_collection( $update_collection, [], 'OBJECT' ); $metadata_ordinate = $Tainacan_Metadata->fetch_by_collection( $update_collection );
$this->assertEquals( 'metadatum2', $metadata_ordinate[0]->get_name() ); $this->assertEquals( 'metadatum2', $metadata_ordinate[0]->get_name() );
$metadata_ordinate_enabled = $Tainacan_Metadata->fetch_by_collection( $update_collection, [ 'include_disabled' => true ], 'OBJECT' ); $metadata_ordinate_enabled = $Tainacan_Metadata->fetch_by_collection( $update_collection, [ 'include_disabled' => true ] );
$this->assertEquals( 'metadatum3', $metadata_ordinate_enabled[0]->get_name() ); $this->assertEquals( 'metadatum3', $metadata_ordinate_enabled[0]->get_name() );
$this->assertFalse($metadata_ordinate_enabled[0]->get_enabled_for_collection()); $this->assertFalse($metadata_ordinate_enabled[0]->get_enabled_for_collection());