testing capabilities #274

This commit is contained in:
leogermani 2019-10-25 17:28:50 -03:00
parent fc2898f701
commit c325725990
2 changed files with 271 additions and 135 deletions

View File

@ -22,28 +22,28 @@ class Taxonomy extends Entity {
* @var string
*/
static $post_type = 'tainacan-taxonomy';
/**
* {@inheritDoc}
* @see \Tainacan\Entities\Entity::capability_type
* @var string
*/
protected static $capability_type = ['tainacan-taxonomy', 'tainacan-taxonomies'];
/**
* {@inheritDoc}
* @see \Tainacan\Entities\Entity::repository
* @var string
*/
protected $repository = 'Taxonomies';
/**
* Prefix used to create the db_identifier
*
* @var string
*/
static $db_identifier_prefix = 'tnc_tax_';
public function __toString(){
return apply_filters("tainacan-taxonomy-to-string", $this->get_name(), $this);
}
@ -67,7 +67,7 @@ class Taxonomy extends Entity {
'new_item_name' => __( 'New Genre term', 'tainacan' ),
'menu_name' => $this->get_name(),
);
$enabled_post_types = $this->get_enabled_post_types();
$enabled_post_types = sizeof($enabled_post_types) ? $enabled_post_types : null;
$show_ui = is_array($enabled_post_types) ? true : false;
@ -82,19 +82,19 @@ class Taxonomy extends Entity {
'slug' => $this->get_slug()
],
);
if (taxonomy_exists($this->get_db_identifier())){
unregister_taxonomy($this->get_db_identifier());
}
register_taxonomy(
$this->get_db_identifier(),
$enabled_post_types,
$args
register_taxonomy(
$this->get_db_identifier(),
$enabled_post_types,
$args
);
return true;
}
@ -103,9 +103,9 @@ class Taxonomy extends Entity {
return (object) [
// meta
'edit_post' => "tnc_rep_edit_taxonomy",
'read_post' => "tnc_rep_edit_taxonomy",
'delete_post' => "tnc_rep_edit_taxonomy",
'read_post' => "tnc_rep_read_taxonomy",
'delete_post' => "tnc_rep_delete_taxonomy",
// primitive
'edit_posts' => "tnc_rep_edit_taxonomies",
'edit_others_posts' => "tnc_rep_manage_taxonomies",
@ -120,7 +120,7 @@ class Taxonomy extends Entity {
'edit_published_posts' => "tnc_rep_edit_taxonomies",
'create_posts' => "tnc_rep_edit_taxonomies"
];
}
// Getters
@ -160,7 +160,7 @@ class Taxonomy extends Entity {
function get_slug() {
return $this->get_mapped_property('slug');
}
/**
* Return the enabled post types
*
@ -169,7 +169,7 @@ class Taxonomy extends Entity {
function get_enabled_post_types() {
return $this->get_mapped_property('enabled_post_types');
}
// special Getters
/**
@ -219,7 +219,7 @@ class Taxonomy extends Entity {
function set_allow_insert($value) {
$this->set_mapped_property('allow_insert', $value);
}
/**
* Sets enabled post types
*
@ -246,19 +246,19 @@ class Taxonomy extends Entity {
return parent::validate();
}
/**
* Check if a term already exists
* Check if a term already exists
*
* @param string $term_name The term name
* @param int|null $parent The ID of the parent term to look for children or null to look for terms in any hierarchical position. Default is null
* @param bool $return_term wether to return the term object if it exists. default is to false
*
* @return bool|WP_Term return boolean indicating if term exists. If $return_term is true and term exists, return WP_Term object
* @param string $term_name The term name
* @param int|null $parent The ID of the parent term to look for children or null to look for terms in any hierarchical position. Default is null
* @param bool $return_term wether to return the term object if it exists. default is to false
*
* @return bool|WP_Term return boolean indicating if term exists. If $return_term is true and term exists, return WP_Term object
*/
function term_exists($term_name, $parent = null, $return_term = false) {
$repo = $this->get_repository();
return $repo->term_exists($this, $term_name, $parent, $return_term);
}
}
}

View File

@ -14,14 +14,14 @@ use Tainacan\Entities\Collection;
* @group permissions
*/
class Capabilities extends TAINACAN_UnitTestCase {
function setUp() {
parent::setUp();
/**
* Test fixtures:
*
* Repo
* Test fixtures:
*
* Repo
* - public_taxonomy
* - private_taxonomy
* - public_repo_metadatum
@ -31,18 +31,18 @@ class Capabilities extends TAINACAN_UnitTestCase {
* - public_collection (5 items)
* --- (Core Title adn Description)
* --- public_metadatum
* --- private_metadatum
* --- public_filter
* --- private_filter
* --- 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' ));
$this->subscriber = get_userdata( $subscriber );
wp_set_current_user($subscriber);
$collection1 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
@ -52,7 +52,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
true
);
$this->public_collection = $collection1;
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
@ -62,7 +62,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
true
);
$this->private_collection = $collection2;
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
@ -73,9 +73,9 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->public_taxonomy = $taxonomy;
$taxonomy2 = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
@ -86,9 +86,9 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->private_taxonomy = $taxonomy;
$this->private_taxonomy = $taxonomy2;
$term_1 = $this->tainacan_entity_factory->create_entity(
'term',
array(
@ -113,7 +113,7 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$metadatum_text = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
@ -124,9 +124,9 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->public_metadatum = $metadatum_text;
$metadatum_repo = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
@ -137,9 +137,9 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->public_repo_metadatum = $metadatum_repo;
$metadatum_text2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
@ -150,9 +150,9 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->private_metadatum = $metadatum_text2;
$metadatum_repo2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
@ -163,9 +163,9 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->private_repo_metadatum = $metadatum_repo2;
$meta_relationship = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
@ -181,9 +181,9 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->meta_relationship = $meta_relationship;
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
@ -195,9 +195,9 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->public_filter = $filter;
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
@ -209,9 +209,9 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->public_repo_filter = $filter;
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
@ -223,9 +223,9 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->private_filter = $filter;
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
@ -237,16 +237,16 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->private_repo_filter = $filter;
for ($i = 1; $i<=10; $i++) {
$title = 'testeItem ' . str_pad($i, 2, "0", STR_PAD_LEFT);
$title = 'testeItem ' . str_pad($i, 2, "0", STR_PAD_LEFT);
$col = $i <= 5 ? $collection1 : $collection2;
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
@ -256,67 +256,67 @@ class Capabilities extends TAINACAN_UnitTestCase {
),
true
);
$this->tainacan_item_metadata_factory->create_item_metadata($item, $metadatum_repo, 'Value ' . $i);
$this->tainacan_item_metadata_factory->create_item_metadata($item, $metadatum_repo2, 'Value ' . $i);
if ($i <= 5) {
$this->tainacan_item_metadata_factory->create_item_metadata($item, $metadatum_text, $i % 2 == 0 ? 'even' : 'odd');
$this->tainacan_item_metadata_factory->create_item_metadata($item, $metadatum_text2, $i % 2 == 0 ? 'even' : 'odd');
}
}
}
$subscriber = $this->factory()->user->create(array( 'role' => 'subscriber' ));
$this->subscriber2 = get_userdata( $subscriber );
}
/**
*
*
*/
function test_super_manage_tainacan () {
$this->assertFalse( user_can($this->subscriber, 'tnc_rep_manage_taxonomies') );
$this->subscriber->add_cap('manage_tainacan');
$this->assertTrue( user_can($this->subscriber, 'tnc_rep_manage_taxonomies') );
}
function test_super_manage_tainacan_collection () {
$this->assertFalse( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
$this->subscriber->add_cap('manage_tainacan_collection_25');
$this->assertTrue( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
$this->assertFalse( user_can($this->subscriber, 'tnc_col_36_read_private_filters') );
}
function test_super_all_collection () {
$this->assertFalse( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
$this->subscriber->add_cap('tnc_col_all_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->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() {
//
//
// $collection = $this->tainacan_entity_factory->create_entity(
// 'collection',
// array(
@ -326,129 +326,265 @@ class Capabilities extends TAINACAN_UnitTestCase {
// ),
// true
// );
//
//
// $caps = $collection->get_items_capabilities();
//
//
// }
/**
* @group metadata
*/
function test_metadata_metacap() {
wp_set_current_user($this->subscriber2->ID);
// edit and delete
$this->assertFalse( $this->public_metadatum->can_edit() );
$this->assertFalse( $this->public_repo_metadatum->can_edit() );
$this->assertFalse( $this->private_metadatum->can_edit() );
$this->assertFalse( $this->private_repo_metadatum->can_edit() );
$this->assertFalse( $this->public_metadatum->can_delete() );
$this->assertFalse( $this->public_repo_metadatum->can_delete() );
$this->assertFalse( $this->private_metadatum->can_delete() );
$this->assertFalse( $this->private_repo_metadatum->can_delete() );
$this->subscriber2->add_cap( 'tnc_rep_manage_metadata' );
$this->assertFalse( $this->public_metadatum->can_edit() );
$this->assertTrue( $this->public_repo_metadatum->can_edit() );
$this->assertFalse( $this->private_metadatum->can_edit() );
$this->assertTrue( $this->private_repo_metadatum->can_edit() );
$this->assertFalse( $this->public_metadatum->can_delete() );
$this->assertTrue( $this->public_repo_metadatum->can_delete() );
$this->assertFalse( $this->private_metadatum->can_delete() );
$this->assertTrue( $this->private_repo_metadatum->can_delete() );
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_manage_metadata' );
$this->assertTrue( $this->public_metadatum->can_edit() );
$this->assertTrue( $this->public_repo_metadatum->can_edit() );
$this->assertTrue( $this->private_metadatum->can_edit() );
$this->assertTrue( $this->private_repo_metadatum->can_edit() );
$this->assertTrue( $this->public_metadatum->can_delete() );
$this->assertTrue( $this->public_repo_metadatum->can_delete() );
$this->assertTrue( $this->private_metadatum->can_delete() );
$this->assertTrue( $this->private_repo_metadatum->can_delete() );
// read
$this->assertTrue( $this->public_metadatum->can_read() );
$this->assertTrue( $this->public_repo_metadatum->can_read() );
$this->assertFalse( $this->private_metadatum->can_read() );
$this->assertFalse( $this->private_repo_metadatum->can_read() );
$this->subscriber2->add_cap( 'tnc_rep_read_private_metadata' );
$this->assertTrue( $this->public_metadatum->can_read() );
$this->assertTrue( $this->public_repo_metadatum->can_read() );
$this->assertFalse( $this->private_metadatum->can_read() );
$this->assertTrue( $this->private_repo_metadatum->can_read() );
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_read_private_metadata' );
$this->assertTrue( $this->public_metadatum->can_read() );
$this->assertTrue( $this->public_repo_metadatum->can_read() );
$this->assertTrue( $this->private_metadatum->can_read() );
$this->assertTrue( $this->private_repo_metadatum->can_read() );
}
/**
* @group filters
*/
function test_filters_metacap() {
wp_set_current_user($this->subscriber2->ID);
// edit and delete
$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->assertFalse( $this->public_filter->can_delete() );
$this->assertFalse( $this->public_repo_filter->can_delete() );
$this->assertFalse( $this->private_filter->can_delete() );
$this->assertFalse( $this->private_repo_filter->can_delete() );
$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->assertFalse( $this->public_filter->can_delete() );
$this->assertTrue( $this->public_repo_filter->can_delete() );
$this->assertFalse( $this->private_filter->can_delete() );
$this->assertTrue( $this->private_repo_filter->can_delete() );
$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() );
$this->assertTrue( $this->public_filter->can_delete() );
$this->assertTrue( $this->public_repo_filter->can_delete() );
$this->assertTrue( $this->private_filter->can_delete() );
$this->assertTrue( $this->private_repo_filter->can_delete() );
// read
$this->assertTrue( $this->public_filter->can_read() );
$this->assertTrue( $this->public_repo_filter->can_read() );
$this->assertFalse( $this->private_filter->can_read() );
$this->assertFalse( $this->private_repo_filter->can_read() );
$this->subscriber2->add_cap( 'tnc_rep_read_private_filters' );
$this->assertTrue( $this->public_filter->can_read() );
$this->assertTrue( $this->public_repo_filter->can_read() );
$this->assertFalse( $this->private_filter->can_read() );
$this->assertTrue( $this->private_repo_filter->can_read() );
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_read_private_filters' );
$this->assertTrue( $this->public_filter->can_read() );
$this->assertTrue( $this->public_repo_filter->can_read() );
$this->assertTrue( $this->private_filter->can_read() );
$this->assertTrue( $this->private_repo_filter->can_read() );
}
/**
* @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));
}
}
/**
* @group taxonomies
*/
function test_taxonomies_caps() {
wp_set_current_user($this->subscriber2->ID);
$this->assertFalse( user_can($this->subscriber2->ID, 'tnc_rep_edit_taxonomies') );
$this->subscriber2->add_cap( 'tnc_rep_edit_taxonomies' );
$this->assertTrue( user_can($this->subscriber2->ID, 'tnc_rep_edit_taxonomies') );
$tax_public = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'Subscriber public Tax',
'description' => 'tipos de musica2',
'allow_insert' => 'yes',
'status' => 'publish'
),
true
);
$tax_private = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'Subscriber private Tax',
'description' => 'tipos de musica2',
'allow_insert' => 'yes',
'status' => 'private'
),
true
);
$this->assertTrue( $tax_public->can_edit() );
$this->assertTrue( $tax_private->can_edit() );
$this->assertFalse( $this->public_taxonomy->can_edit() );
$this->assertFalse( $this->private_taxonomy->can_edit() );
$this->assertTrue( $tax_public->can_delete() );
$this->assertTrue( $tax_private->can_delete() );
$this->assertFalse( $this->public_taxonomy->can_delete() );
$this->assertFalse( $this->private_taxonomy->can_delete() );
$this->assertTrue( $tax_public->can_read() );
$this->assertTrue( $tax_private->can_read(), 'user should be able to read own private taxonomies' );
$this->assertTrue( $this->public_taxonomy->can_read() );
$this->assertFalse( $this->private_taxonomy->can_read() );
$this->subscriber2->add_cap( 'tnc_rep_manage_taxonomies' );
$this->assertTrue( $tax_public->can_edit() );
$this->assertTrue( $tax_private->can_edit() );
$this->assertTrue( $this->public_taxonomy->can_edit() );
$this->assertTrue( $this->private_taxonomy->can_edit() );
$this->assertTrue( $tax_public->can_delete() );
$this->assertTrue( $tax_private->can_delete() );
$this->assertTrue( $this->public_taxonomy->can_delete() );
$this->assertTrue( $this->private_taxonomy->can_delete() );
$this->assertTrue( $this->public_taxonomy->can_read() );
$this->assertFalse( $this->private_taxonomy->can_read() );
$this->subscriber2->add_cap( 'tnc_rep_read_private_taxonomies' );
$this->assertTrue( $this->private_taxonomy->can_read() );
}
}