test collections capabilites #274

This commit is contained in:
leogermani 2019-10-28 20:17:18 -03:00
parent 277f061512
commit 79813f79c9
3 changed files with 123 additions and 27 deletions

View File

@ -47,6 +47,10 @@ class Roles {
'display_name' => __('Create Collections', 'tainacan'), 'display_name' => __('Create Collections', 'tainacan'),
'description' => __('Create new collections to the repository', 'tainacan') 'description' => __('Create new collections to the repository', 'tainacan')
], ],
'tnc_rep_delete_collections' => [
'display_name' => __('Delete Collections', 'tainacan'),
'description' => __('Delete their own collections from the repository', 'tainacan')
],
'tnc_rep_edit_taxonomies' => [ 'tnc_rep_edit_taxonomies' => [
'display_name' => __('Create and edit taxonomies', 'tainacan'), 'display_name' => __('Create and edit taxonomies', 'tainacan'),
'description' => __('Create new taxonomies and edit its terms', 'tainacan') 'description' => __('Create new taxonomies and edit its terms', 'tainacan')

View File

@ -151,7 +151,7 @@ class Collection extends Entity {
* @return object Object with all the capabilities as member variables. * @return object Object with all the capabilities as member variables.
*/ */
function get_items_capabilities() { function get_items_capabilities() {
$id = $this->get_id(); $id = $this->get_id();
return (object) [ return (object) [
@ -159,7 +159,7 @@ class Collection extends Entity {
'edit_post' => "tnc_col_{$id}_edit_item", 'edit_post' => "tnc_col_{$id}_edit_item",
'read_post' => "tnc_col_{$id}_read_item", 'read_post' => "tnc_col_{$id}_read_item",
'delete_post' => "tnc_col_{$id}_delete_item", 'delete_post' => "tnc_col_{$id}_delete_item",
// primitive // primitive
'edit_posts' => "tnc_col_{$id}_edit_items", 'edit_posts' => "tnc_col_{$id}_edit_items",
'edit_others_posts' => "tnc_col_{$id}_edit_others_items", 'edit_others_posts' => "tnc_col_{$id}_edit_others_items",
@ -181,24 +181,24 @@ class Collection extends Entity {
return (object) [ return (object) [
// meta // meta
'edit_post' => "tnc_col_edit_item", 'edit_post' => "tnc_col_edit_item",
'read_post' => "tnc_col_edit_item", 'read_post' => "tnc_col_read_item",
'delete_post' => "tnc_col_edit_item", 'delete_post' => "tnc_col_delete_item",
// primitive // primitive
'edit_posts' => "tnc_rep_edit_collections", 'edit_posts' => "tnc_rep_edit_collections",
'edit_others_posts' => "manage_tainacan", 'edit_others_posts' => "manage_tainacan",
'publish_posts' => "tnc_rep_edit_collections", 'publish_posts' => "tnc_rep_edit_collections",
'read_private_posts' => "tnc_rep_read_private_collections", 'read_private_posts' => "tnc_rep_read_private_collections",
'read' => "read", 'read' => "read",
'delete_posts' => "tnc_rep_edit_collections", 'delete_posts' => "tnc_rep_delete_collections",
'delete_private_posts' => "tnc_rep_edit_collections", 'delete_private_posts' => "tnc_rep_delete_collections",
'delete_published_posts' => "tnc_rep_edit_collections", 'delete_published_posts' => "tnc_rep_delete_collections",
'delete_others_posts' => "manage_tainacan", 'delete_others_posts' => "manage_tainacan",
'edit_private_posts' => "tnc_rep_edit_collections", 'edit_private_posts' => "tnc_rep_edit_collections",
'edit_published_posts' => "tnc_rep_edit_collections", 'edit_published_posts' => "tnc_rep_edit_collections",
'create_posts' => "tnc_rep_edit_collections" 'create_posts' => "tnc_rep_edit_collections"
]; ];
} }
/** /**
@ -243,7 +243,7 @@ class Collection extends Entity {
$sizes = get_intermediate_image_sizes(); $sizes = get_intermediate_image_sizes();
array_unshift($sizes, 'full'); array_unshift($sizes, 'full');
foreach ( $sizes as $size ) { foreach ( $sizes as $size ) {
$thumbs[$size] = wp_get_attachment_image_src( $this->get__thumbnail_id(), $size ); $thumbs[$size] = wp_get_attachment_image_src( $this->get__thumbnail_id(), $size );
} }
@ -405,7 +405,7 @@ class Collection extends Entity {
function get_metadata_order() { function get_metadata_order() {
return $this->get_mapped_property( 'metadata_order' ); return $this->get_mapped_property( 'metadata_order' );
} }
/** /**
* Get enable cover page attribute * Get enable cover page attribute
* *
@ -414,7 +414,7 @@ class Collection extends Entity {
function get_enable_cover_page() { function get_enable_cover_page() {
return $this->get_mapped_property( 'enable_cover_page' ); return $this->get_mapped_property( 'enable_cover_page' );
} }
/** /**
* Get Header Image ID attribute * Get Header Image ID attribute
* *
@ -423,7 +423,7 @@ class Collection extends Entity {
function get_header_image_id() { function get_header_image_id() {
return $this->get_mapped_property( 'header_image_id' ); return $this->get_mapped_property( 'header_image_id' );
} }
/** /**
* Return true if enabled cover page is set to yes * Return true if enabled cover page is set to yes
* *
@ -432,7 +432,7 @@ class Collection extends Entity {
function is_cover_page_enabled() { function is_cover_page_enabled() {
return $this->get_enable_cover_page() === 'yes'; return $this->get_enable_cover_page() === 'yes';
} }
/** /**
* Get enable cover page attribute * Get enable cover page attribute
* *
@ -489,7 +489,7 @@ class Collection extends Entity {
/** /**
* Get the two core metadata of the collection (title and description) * Get the two core metadata of the collection (title and description)
* *
* @return array[\Tainacan\Entities\Metadatum] * @return array[\Tainacan\Entities\Metadatum]
*/ */
function get_core_metadata() { function get_core_metadata() {
@ -501,7 +501,7 @@ class Collection extends Entity {
/** /**
* Get the Core Title Metadatum for this collection * Get the Core Title Metadatum for this collection
* *
* @return \Tainacan\Entities\Metadatum The Core Title Metadatum * @return \Tainacan\Entities\Metadatum The Core Title Metadatum
*/ */
function get_core_title_metadatum() { function get_core_title_metadatum() {
@ -512,7 +512,7 @@ class Collection extends Entity {
/** /**
* Get the Core Description Metadatum for this collection * Get the Core Description Metadatum for this collection
* *
* @return \Tainacan\Entities\Metadatum The Core Description Metadatum * @return \Tainacan\Entities\Metadatum The Core Description Metadatum
*/ */
function get_core_description_metadatum() { function get_core_description_metadatum() {
@ -520,7 +520,7 @@ class Collection extends Entity {
return $repo->get_core_description_metadatum($this); return $repo->get_core_description_metadatum($this);
} }
/** /**
* Checks if comments are allowed for the current Collection. * Checks if comments are allowed for the current Collection.
* @return string "open"|"closed" * @return string "open"|"closed"
@ -528,7 +528,7 @@ class Collection extends Entity {
public function get_comment_status() { public function get_comment_status() {
return $this->get_mapped_property('comment_status'); return $this->get_mapped_property('comment_status');
} }
/** /**
* Checks if comments are allowed for the current Collection Items. * Checks if comments are allowed for the current Collection Items.
* @return bool * @return bool
@ -674,7 +674,7 @@ class Collection extends Entity {
function set_filters_order( $value ) { function set_filters_order( $value ) {
$this->set_mapped_property( 'filters_order', $value ); $this->set_mapped_property( 'filters_order', $value );
} }
/** /**
* Set enable cover page attribute * Set enable cover page attribute
* *
@ -685,7 +685,7 @@ class Collection extends Entity {
function set_enable_cover_page( $value ) { function set_enable_cover_page( $value ) {
$this->set_mapped_property( 'enable_cover_page', $value ); $this->set_mapped_property( 'enable_cover_page', $value );
} }
/** /**
* Set cover page ID * Set cover page ID
* *
@ -696,7 +696,7 @@ class Collection extends Entity {
function set_cover_page_id( $value ) { function set_cover_page_id( $value ) {
$this->set_mapped_property( 'cover_page_id', $value ); $this->set_mapped_property( 'cover_page_id', $value );
} }
/** /**
* Set Header Image ID * Set Header Image ID
* *
@ -723,21 +723,21 @@ class Collection extends Entity {
throw new \Exception('moderators_ids must be a array of users ids'); throw new \Exception('moderators_ids must be a array of users ids');
} }
} }
// make sure you never have duplicated moderators // make sure you never have duplicated moderators
$value = array_unique($value); $value = array_unique($value);
$this->set_mapped_property( 'moderators_ids', $value ); $this->set_mapped_property( 'moderators_ids', $value );
} }
/** /**
* Sets if comments are allowed for the current Collection. * Sets if comments are allowed for the current Collection.
* *
* @param $value string "open"|"closed" * @param $value string "open"|"closed"
*/ */
public function set_comment_status( $value ) { public function set_comment_status( $value ) {
$this->set_mapped_property('comment_status', $value); $this->set_mapped_property('comment_status', $value);
} }
/** /**
* Sets if comments are allowed for the current Collection Items. * Sets if comments are allowed for the current Collection Items.
* *

View File

@ -645,4 +645,96 @@ class Capabilities extends TAINACAN_UnitTestCase {
} }
/**
* @group collections
*/
function test_collections_metacaps() {
wp_set_current_user($this->subscriber2->ID);
$this->subscriber2->add_cap( 'tnc_rep_edit_collections' );
$my_collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'My Col',
'status' => 'publish'
),
true
);
$this->assertFalse( $this->public_collection->can_edit() );
$this->assertTrue( $this->public_collection->can_read() );
$this->assertFalse( $this->public_collection->can_delete() );
$this->assertFalse( $this->private_collection->can_edit() );
$this->assertFalse( $this->private_collection->can_read() );
$this->assertFalse( $this->private_collection->can_delete() );
$this->assertTrue( $my_collection->can_edit() );
$this->assertTrue( $my_collection->can_read() );
$this->assertFalse( $my_collection->can_delete() );
$this->subscriber2->add_cap( 'tnc_rep_delete_collections' );
$this->assertFalse( $this->public_collection->can_edit() );
$this->assertTrue( $this->public_collection->can_read() );
$this->assertFalse( $this->public_collection->can_delete() );
$this->assertFalse( $this->private_collection->can_edit() );
$this->assertFalse( $this->private_collection->can_read() );
$this->assertFalse( $this->private_collection->can_delete() );
$this->assertTrue( $my_collection->can_edit() );
$this->assertTrue( $my_collection->can_read() );
$this->assertTrue( $my_collection->can_delete() );
$this->subscriber2->add_cap( 'tnc_rep_read_private_collections' );
$this->assertFalse( $this->public_collection->can_edit() );
$this->assertTrue( $this->public_collection->can_read() );
$this->assertFalse( $this->public_collection->can_delete() );
$this->assertFalse( $this->private_collection->can_edit() );
$this->assertTrue( $this->private_collection->can_read() );
$this->assertFalse( $this->private_collection->can_delete() );
$this->subscriber2->add_cap( 'manage_tainacan' );
$this->assertTrue( $this->public_collection->can_edit() );
$this->assertTrue( $this->public_collection->can_read() );
$this->assertTrue( $this->public_collection->can_delete() );
$this->assertTrue( $this->private_collection->can_edit() );
$this->assertTrue( $this->private_collection->can_read() );
$this->assertTrue( $this->private_collection->can_delete() );
}
/**
* @group collections
*/
function test_fetch_collections() {
global $current_user;
wp_set_current_user($this->subscriber2->ID);
$this->subscriber2->add_cap( 'tnc_rep_edit_collections' );
$my_collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'My Col',
'status' => 'publish'
),
true
);
$cols = tainacan_collections()->fetch([], 'OBJECT');
$this->assertEquals(2, sizeof($cols));
$this->subscriber2->add_cap( 'tnc_rep_read_private_collections' );
$current_user = $this->subscriber2; // force update current user object with new capabilities
$cols = tainacan_collections()->fetch([], 'OBJECT');
$this->assertEquals(3, sizeof($cols));
}
} }