2019-10-17 19:36:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
|
|
|
use Tainacan\Entities\Collection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Capabilities
|
|
|
|
*
|
|
|
|
* @package Test_Tainacan
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group permissions
|
|
|
|
*/
|
|
|
|
class Capabilities extends TAINACAN_UnitTestCase {
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
function setUp() {
|
|
|
|
parent::setUp();
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
/**
|
2019-10-25 20:28:50 +00:00
|
|
|
* Test fixtures:
|
|
|
|
*
|
|
|
|
* Repo
|
2019-10-21 22:11:41 +00:00
|
|
|
* - public_taxonomy
|
|
|
|
* - private_taxonomy
|
|
|
|
* - public_repo_metadatum
|
|
|
|
* - private_repo_metadatum
|
|
|
|
* - public_repo_filter
|
|
|
|
* - private_repo_filter
|
2019-10-30 13:59:38 +00:00
|
|
|
* - public_collection (10 items, 5 public, 5 private)
|
2019-10-21 22:11:41 +00:00
|
|
|
* --- (Core Title adn Description)
|
|
|
|
* --- public_metadatum
|
2019-10-25 20:28:50 +00:00
|
|
|
* --- private_metadatum
|
|
|
|
* --- public_filter
|
|
|
|
* --- private_filter
|
2019-10-30 13:59:38 +00:00
|
|
|
* - private_collection (10 items, 5 public, 5 private)
|
2019-10-21 22:11:41 +00:00
|
|
|
* --- (Core Title adn Description)
|
|
|
|
* --- meta_relationshipt (with public collection)
|
|
|
|
*/
|
2019-10-17 19:36:23 +00:00
|
|
|
$subscriber = $this->factory()->user->create(array( 'role' => 'subscriber' ));
|
|
|
|
$this->subscriber = get_userdata( $subscriber );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
wp_set_current_user($subscriber);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$collection1 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Public Col',
|
2019-10-18 23:44:53 +00:00
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->public_collection = $collection1;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$collection2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Private Col',
|
2019-10-18 23:44:53 +00:00
|
|
|
'status' => 'private'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->private_collection = $collection2;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Public Tax',
|
2019-10-18 23:44:53 +00:00
|
|
|
'description' => 'tipos de musica',
|
|
|
|
'allow_insert' => 'yes',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$this->public_taxonomy = $taxonomy;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$taxonomy2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Private Tax',
|
2019-10-18 23:44:53 +00:00
|
|
|
'description' => 'tipos de musica2',
|
|
|
|
'allow_insert' => 'yes',
|
2019-10-21 22:11:41 +00:00
|
|
|
'status' => 'private'
|
2019-10-18 23:44:53 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
$this->private_taxonomy = $taxonomy2;
|
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$term_1 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'name' => 'Term for collection 1'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-31 20:45:20 +00:00
|
|
|
|
|
|
|
$this->term_in_public = $term_1;
|
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$term_2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
2019-10-31 20:45:20 +00:00
|
|
|
'taxonomy' => $taxonomy2->get_db_identifier(),
|
2019-10-18 23:44:53 +00:00
|
|
|
'name' => 'Term for collection 2'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-31 20:45:20 +00:00
|
|
|
|
|
|
|
$this->term_in_private = $term_2;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$metadatum_text = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Public meta',
|
2019-10-18 23:44:53 +00:00
|
|
|
'status' => 'publish',
|
|
|
|
'collection' => $collection1,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$this->public_metadatum = $metadatum_text;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$metadatum_repo = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Public Repo Meta',
|
2019-10-18 23:44:53 +00:00
|
|
|
'status' => 'publish',
|
|
|
|
'collection_id' => 'default',
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$this->public_repo_metadatum = $metadatum_repo;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$metadatum_text2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Private Meta',
|
2019-10-18 23:44:53 +00:00
|
|
|
'status' => 'private',
|
|
|
|
'collection' => $collection1,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$this->private_metadatum = $metadatum_text2;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$metadatum_repo2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Private Repo Meta',
|
2019-10-18 23:44:53 +00:00
|
|
|
'status' => 'private',
|
|
|
|
'collection_id' => 'default',
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$this->private_repo_metadatum = $metadatum_repo2;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$meta_relationship = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'relationship',
|
|
|
|
'status' => 'publish',
|
|
|
|
'collection' => $collection2,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Relationship',
|
|
|
|
'metadata_type_options' => [
|
|
|
|
'allow_new_terms' => 'yes',
|
|
|
|
'collection_id' => $collection1->get_id(),
|
|
|
|
'search' => ''
|
|
|
|
]
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$this->meta_relationship = $meta_relationship;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$filter = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'filter',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Public filter',
|
2019-10-18 23:44:53 +00:00
|
|
|
'collection' => $collection1,
|
|
|
|
'description' => 'Teste Filtro',
|
|
|
|
'status' => 'publish',
|
|
|
|
'metadatum' => $metadatum_text,
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$this->public_filter = $filter;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$filter = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'filter',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Public repo filter',
|
2019-10-18 23:44:53 +00:00
|
|
|
'collection_id' => 'default',
|
|
|
|
'description' => 'Teste Filtro',
|
|
|
|
'status' => 'publish',
|
|
|
|
'metadatum' => $metadatum_repo,
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$this->public_repo_filter = $filter;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$filter = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'filter',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Private Filter',
|
2019-10-18 23:44:53 +00:00
|
|
|
'collection' => $collection1,
|
|
|
|
'description' => 'Teste Filtro',
|
|
|
|
'status' => 'private',
|
|
|
|
'metadatum' => $metadatum_text2,
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$this->private_filter = $filter;
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$filter = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'filter',
|
|
|
|
array(
|
2019-10-21 22:11:41 +00:00
|
|
|
'name' => 'Private repo filter',
|
2019-10-18 23:44:53 +00:00
|
|
|
'collection_id' => 'default',
|
|
|
|
'description' => 'Teste Filtro',
|
|
|
|
'status' => 'private',
|
|
|
|
'metadatum' => $metadatum_repo2,
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$this->private_repo_filter = $filter;
|
2019-10-30 13:59:38 +00:00
|
|
|
$this->items = [];
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
for ($i = 1; $i<=10; $i++) {
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
$title = 'testeItem ' . str_pad($i, 2, "0", STR_PAD_LEFT);
|
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$col = $i <= 5 ? $collection1 : $collection2;
|
2019-10-30 13:59:38 +00:00
|
|
|
$col_slug = $i <= 5 ? 'public_col' : 'private_col';
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$item = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => $title,
|
|
|
|
'collection' => $col,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-30 13:59:38 +00:00
|
|
|
$this->items[$col_slug]['public'][] = $item;
|
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$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);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
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');
|
2019-10-25 20:28:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 13:59:38 +00:00
|
|
|
$private_item = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'private' . $title,
|
|
|
|
'collection' => $col,
|
|
|
|
'status' => 'private'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->items[$col_slug]['private'][] = $private_item;
|
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
}
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$subscriber = $this->factory()->user->create(array( 'role' => 'subscriber' ));
|
|
|
|
$this->subscriber2 = get_userdata( $subscriber );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
}
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
/**
|
2019-10-25 20:28:50 +00:00
|
|
|
*
|
2019-10-17 19:36:23 +00:00
|
|
|
*/
|
|
|
|
function test_super_manage_tainacan () {
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
$this->assertFalse( user_can($this->subscriber, 'tnc_rep_manage_taxonomies') );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
$this->subscriber->add_cap('manage_tainacan');
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
$this->assertTrue( user_can($this->subscriber, 'tnc_rep_manage_taxonomies') );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
}
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
function test_super_manage_tainacan_collection () {
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
$this->assertFalse( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
$this->subscriber->add_cap('manage_tainacan_collection_25');
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
$this->assertTrue( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
|
|
|
|
$this->assertFalse( user_can($this->subscriber, 'tnc_col_36_read_private_filters') );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-17 19:36:23 +00:00
|
|
|
}
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 14:44:26 +00:00
|
|
|
function test_super_all_collection () {
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 14:44:26 +00:00
|
|
|
$this->assertFalse( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 14:44:26 +00:00
|
|
|
$this->subscriber->add_cap('tnc_col_all_read_private_filters');
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 14:44:26 +00:00
|
|
|
$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') );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$this->assertFalse( user_can($this->subscriber2, 'tnc_col_25_read_private_filters') );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$this->subscriber2->add_cap('manage_tainacan_collection_all');
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$this->assertTrue( user_can($this->subscriber2, 'tnc_col_25_read_private_filters') );
|
|
|
|
$this->assertTrue( user_can($this->subscriber2, 'tnc_col_36_read_private_filters') );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 14:44:26 +00:00
|
|
|
}
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
/**
|
|
|
|
* @group metadata
|
|
|
|
*/
|
|
|
|
function test_metadata_metacap() {
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
// edit and delete
|
2019-10-18 23:44:53 +00:00
|
|
|
$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() );
|
2019-10-25 20:28:50 +00:00
|
|
|
$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() );
|
|
|
|
|
|
|
|
|
2019-10-28 22:57:48 +00:00
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_edit_metadata' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$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() );
|
2019-10-25 20:28:50 +00:00
|
|
|
$this->assertFalse( $this->public_metadatum->can_delete() );
|
2019-10-28 22:57:48 +00:00
|
|
|
$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_delete_metadata' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 22:57:48 +00:00
|
|
|
|
|
|
|
$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() . '_edit_metadata' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 22:57:48 +00:00
|
|
|
|
|
|
|
$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->assertFalse( $this->public_metadatum->can_delete() );
|
2019-10-25 20:28:50 +00:00
|
|
|
$this->assertTrue( $this->public_repo_metadatum->can_delete() );
|
|
|
|
$this->assertFalse( $this->private_metadatum->can_delete() );
|
|
|
|
$this->assertTrue( $this->private_repo_metadatum->can_delete() );
|
|
|
|
|
2019-10-28 22:57:48 +00:00
|
|
|
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_delete_metadata' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-18 23:44:53 +00:00
|
|
|
$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() );
|
2019-10-25 20:28:50 +00:00
|
|
|
$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' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
$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' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
$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() );
|
2019-10-17 19:36:23 +00:00
|
|
|
}
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
/**
|
|
|
|
* @group filters
|
|
|
|
*/
|
|
|
|
function test_filters_metacap() {
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
// edit and delete
|
2019-10-21 22:11:41 +00:00
|
|
|
$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() );
|
2019-10-25 20:28:50 +00:00
|
|
|
$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() );
|
|
|
|
|
2019-10-28 22:57:48 +00:00
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_edit_filters' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 22:57:48 +00:00
|
|
|
|
|
|
|
$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->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_delete_filters' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$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() );
|
2019-10-25 20:28:50 +00:00
|
|
|
$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() );
|
|
|
|
|
2019-10-28 22:57:48 +00:00
|
|
|
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_edit_filters' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 22:57:48 +00:00
|
|
|
|
|
|
|
$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->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() . '_delete_filters' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$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() );
|
2019-10-25 20:28:50 +00:00
|
|
|
$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' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
$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' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
$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() );
|
|
|
|
|
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
}
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
/**
|
|
|
|
* @group fetch_by_collection
|
|
|
|
*/
|
|
|
|
function test_fetch_meta_by_collection() {
|
|
|
|
global $current_user;
|
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$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));
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$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
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$meta = tainacan_metadata()->fetch_by_collection($this->public_collection);
|
2020-04-17 12:29:11 +00:00
|
|
|
$this->AssertEquals(5, sizeof($meta));
|
2019-10-21 22:11:41 +00:00
|
|
|
$meta = tainacan_metadata()->fetch_ids_by_collection($this->public_collection);
|
2020-04-17 12:29:11 +00:00
|
|
|
$this->AssertEquals(5, sizeof($meta));
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_read_private_metadata' );
|
|
|
|
$current_user = $this->subscriber2; // force update current user object with new capabilities
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$meta = tainacan_metadata()->fetch_by_collection($this->public_collection);
|
2020-04-17 12:29:11 +00:00
|
|
|
$this->AssertEquals(6, sizeof($meta));
|
2019-10-21 22:11:41 +00:00
|
|
|
$meta = tainacan_metadata()->fetch_ids_by_collection($this->public_collection);
|
2020-04-17 12:29:11 +00:00
|
|
|
$this->AssertEquals(6, sizeof($meta));
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
}
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
/**
|
|
|
|
* @group fetch_by_collection
|
|
|
|
*/
|
|
|
|
function test_fetch_filter_by_collection() {
|
|
|
|
global $current_user;
|
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$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));
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$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
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$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));
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_read_private_filters' );
|
|
|
|
$current_user = $this->subscriber2; // force update current user object with new capabilities
|
2019-10-25 20:28:50 +00:00
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
$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));
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
|
2019-10-21 22:11:41 +00:00
|
|
|
}
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
$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() );
|
|
|
|
|
2019-10-28 22:57:48 +00:00
|
|
|
$this->assertFalse( $tax_public->can_delete() );
|
|
|
|
$this->assertFalse( $tax_private->can_delete() );
|
2019-10-25 20:28:50 +00:00
|
|
|
$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() );
|
|
|
|
|
2019-10-28 22:57:48 +00:00
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_edit_others_taxonomies' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
$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() );
|
|
|
|
|
2019-10-28 22:57:48 +00:00
|
|
|
$this->assertFalse( $tax_public->can_delete() );
|
|
|
|
$this->assertFalse( $tax_private->can_delete() );
|
|
|
|
$this->assertFalse( $this->public_taxonomy->can_delete() );
|
|
|
|
$this->assertFalse( $this->private_taxonomy->can_delete() );
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $this->public_taxonomy->can_read() );
|
|
|
|
$this->assertFalse( $this->private_taxonomy->can_read() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_read_private_taxonomies' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-25 20:28:50 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $this->private_taxonomy->can_read() );
|
|
|
|
|
2019-10-28 22:57:48 +00:00
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_delete_taxonomies' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 22:57:48 +00:00
|
|
|
|
|
|
|
$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->subscriber2->add_cap( 'tnc_rep_delete_others_taxonomies' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 22:57:48 +00:00
|
|
|
|
|
|
|
$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() );
|
|
|
|
|
2019-10-25 20:28:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 20:45:20 +00:00
|
|
|
/**
|
|
|
|
* @group taxonomies
|
|
|
|
*/
|
|
|
|
function test_terms_caps() {
|
|
|
|
|
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
|
|
|
|
|
|
|
$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
|
|
|
|
);
|
|
|
|
|
|
|
|
$term_1 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $tax_public->get_db_identifier(),
|
|
|
|
'name' => 'Term for collection 2'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$term_2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'term',
|
|
|
|
array(
|
|
|
|
'taxonomy' => $tax_private->get_db_identifier(),
|
|
|
|
'name' => 'Term for collection 2'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $term_1->can_delete() );
|
|
|
|
$this->assertFalse( $term_2->can_delete() );
|
|
|
|
$this->assertFalse( $this->term_in_public->can_delete() );
|
|
|
|
$this->assertFalse( $this->term_in_private->can_delete() );
|
|
|
|
|
|
|
|
$this->assertFalse( $term_1->can_edit() );
|
|
|
|
$this->assertFalse( $term_2->can_edit() );
|
|
|
|
$this->assertFalse( $this->term_in_public->can_edit() );
|
|
|
|
$this->assertFalse( $this->term_in_private->can_edit() );
|
|
|
|
|
|
|
|
$this->assertTrue( $term_1->can_read() );
|
|
|
|
$this->assertTrue( $term_2->can_read() );
|
|
|
|
$this->assertTrue( $this->term_in_public->can_read() );
|
|
|
|
$this->assertFalse( $this->term_in_private->can_read() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_edit_taxonomies' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-31 20:45:20 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $term_1->can_delete() );
|
|
|
|
$this->assertTrue( $term_2->can_delete() );
|
|
|
|
$this->assertFalse( $this->term_in_public->can_delete() );
|
|
|
|
$this->assertFalse( $this->term_in_private->can_delete() );
|
|
|
|
|
|
|
|
$this->assertTrue( $term_1->can_edit() );
|
|
|
|
$this->assertTrue( $term_2->can_edit() );
|
|
|
|
$this->assertFalse( $this->term_in_public->can_edit() );
|
|
|
|
$this->assertFalse( $this->term_in_private->can_edit() );
|
|
|
|
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_edit_others_taxonomies' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-31 20:45:20 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $term_1->can_delete() );
|
|
|
|
$this->assertTrue( $term_2->can_delete() );
|
|
|
|
$this->assertTrue( $this->term_in_public->can_delete() );
|
|
|
|
$this->assertTrue( $this->term_in_private->can_delete() );
|
|
|
|
|
|
|
|
$this->assertTrue( $term_1->can_edit() );
|
|
|
|
$this->assertTrue( $term_2->can_edit() );
|
|
|
|
$this->assertTrue( $this->term_in_public->can_edit() );
|
|
|
|
$this->assertTrue( $this->term_in_private->can_edit() );
|
|
|
|
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_read_private_taxonomies' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-31 20:45:20 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $term_1->can_read() );
|
|
|
|
$this->assertTrue( $term_2->can_read() );
|
|
|
|
$this->assertTrue( $this->term_in_public->can_read() );
|
|
|
|
$this->assertTrue( $this->term_in_private->can_read() );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-28 23:17:18 +00:00
|
|
|
/**
|
|
|
|
* @group collections
|
|
|
|
*/
|
|
|
|
function test_collections_metacaps() {
|
|
|
|
|
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_edit_collections' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 23:17:18 +00:00
|
|
|
|
|
|
|
$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' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 23:17:18 +00:00
|
|
|
|
|
|
|
$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' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 23:17:18 +00:00
|
|
|
|
|
|
|
$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' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 23:17:18 +00:00
|
|
|
|
|
|
|
$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' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-28 23:17:18 +00:00
|
|
|
|
|
|
|
$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));
|
|
|
|
|
|
|
|
|
2020-10-25 16:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group collectionss
|
|
|
|
*/
|
|
|
|
function test_manage_collection_can_edit_collection() {
|
|
|
|
global $current_user;
|
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
|
|
|
|
|
|
|
$this->assertFalse( $this->public_collection->can_edit() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( 'manage_tainacan_collection_' . $this->public_collection->get_id() );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2020-10-25 16:23:39 +00:00
|
|
|
$current_user = $this->subscriber2; // force update current user object with new capabilities
|
|
|
|
|
|
|
|
$this->assertTrue( $this->public_collection->can_edit() );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group collections
|
|
|
|
*/
|
|
|
|
function test_manage_all_collections_can_edit_collection() {
|
|
|
|
global $current_user;
|
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
|
|
|
|
|
|
|
$this->assertFalse( $this->public_collection->can_edit() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( 'manage_tainacan_collection_all' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2020-10-25 16:23:39 +00:00
|
|
|
$current_user = $this->subscriber2; // force update current user object with new capabilities
|
|
|
|
|
|
|
|
$this->assertTrue( $this->public_collection->can_edit() );
|
|
|
|
$this->assertTrue( $this->private_collection->can_edit() );
|
|
|
|
|
|
|
|
|
2019-10-28 23:17:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 13:59:38 +00:00
|
|
|
/**
|
|
|
|
* @group items
|
|
|
|
*/
|
|
|
|
function test_items_metacaps() {
|
|
|
|
global $current_user;
|
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
|
|
|
|
|
|
|
$col1prefix = 'tnc_col_' . $this->public_collection->get_id() . '_';
|
|
|
|
$col2prefix = 'tnc_col_' . $this->private_collection->get_id() . '_';
|
|
|
|
|
|
|
|
$public = $this->items['public_col']['public'][0];
|
|
|
|
$public_in_private = $this->items['private_col']['public'][0];
|
|
|
|
|
|
|
|
$private = $this->items['public_col']['private'][0];
|
|
|
|
$private_in_private = $this->items['private_col']['private'][0];
|
|
|
|
|
|
|
|
$this->assertTrue( $public->can_read() );
|
|
|
|
$this->assertFalse( $public_in_private->can_read() );
|
|
|
|
$this->assertFalse( $private->can_read() );
|
|
|
|
$this->assertFalse( $private_in_private->can_read() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col1prefix . 'read_private_items' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $public->can_read() );
|
|
|
|
$this->assertFalse( $public_in_private->can_read() );
|
|
|
|
$this->assertTrue( $private->can_read() );
|
|
|
|
$this->assertFalse( $private_in_private->can_read() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col2prefix . 'read_private_items' );
|
|
|
|
|
|
|
|
$this->assertTrue( $public->can_read() );
|
|
|
|
$this->assertFalse( $public_in_private->can_read() );
|
|
|
|
$this->assertTrue( $private->can_read() );
|
|
|
|
$this->assertFalse( $private_in_private->can_read(), 'user must also have read_private_collections to read items in private collections' );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_read_private_collections' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $public->can_read() );
|
|
|
|
$this->assertTrue( $public_in_private->can_read() );
|
|
|
|
$this->assertTrue( $private->can_read() );
|
|
|
|
$this->assertTrue( $private_in_private->can_read() );
|
|
|
|
|
|
|
|
$own_item = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'my item',
|
|
|
|
'collection' => $this->public_collection,
|
|
|
|
'status' => 'draft'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse( $own_item->can_edit() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col1prefix . 'edit_items' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $own_item->can_edit() );
|
|
|
|
$this->assertFalse( $own_item->can_publish() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col1prefix . 'publish_items' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $own_item->can_publish() );
|
|
|
|
|
|
|
|
$this->assertFalse( $own_item->can_delete() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col1prefix . 'delete_items' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $own_item->can_delete() );
|
|
|
|
|
|
|
|
$own_item->set_status('publish');
|
|
|
|
$own_item->validate();
|
|
|
|
|
|
|
|
$own_item = \tainacan_items()->insert($own_item);
|
|
|
|
|
|
|
|
$this->assertFalse( $own_item->can_edit() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col1prefix . 'edit_published_items' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $own_item->can_edit() );
|
|
|
|
|
|
|
|
$this->assertFalse( $own_item->can_delete() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col1prefix . 'delete_published_items' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $own_item->can_delete() );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group items
|
|
|
|
*/
|
|
|
|
function test_items_edit_others() {
|
|
|
|
global $current_user;
|
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
|
|
|
|
|
|
|
$col1prefix = 'tnc_col_' . $this->public_collection->get_id() . '_';
|
|
|
|
|
|
|
|
$other_draft = $this->items['public_col']['public'][0];
|
|
|
|
$other_draft->set_status('draft');
|
|
|
|
$other_draft->validate();
|
|
|
|
$other_draft = \tainacan_items()->insert($other_draft);
|
|
|
|
|
|
|
|
$other_published = $this->items['public_col']['public'][1];
|
|
|
|
|
|
|
|
$this->assertFalse( $other_draft->can_edit() );
|
|
|
|
$this->assertFalse( $other_published->can_edit() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col1prefix . 'edit_others_items' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $other_draft->can_edit() );
|
|
|
|
$this->assertFalse( $other_published->can_edit() );
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col1prefix . 'edit_published_items' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $other_draft->can_edit() );
|
|
|
|
$this->assertTrue( $other_published->can_edit() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group items
|
|
|
|
*/
|
|
|
|
function test_fetch_items() {
|
|
|
|
global $current_user;
|
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
|
|
|
|
|
|
|
$col1prefix = 'tnc_col_' . $this->public_collection->get_id() . '_';
|
|
|
|
$col2prefix = 'tnc_col_' . $this->private_collection->get_id() . '_';
|
|
|
|
|
|
|
|
$repo = tainacan_items()->fetch(['nopaging' => true], [], 'OBJECT');
|
|
|
|
|
|
|
|
$this->assertEquals(5, sizeof($repo));
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col1prefix . 'read_private_items' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
$current_user = $this->subscriber2; // force update current user object with new capabilities
|
|
|
|
|
|
|
|
|
|
|
|
$repo = tainacan_items()->fetch(['nopaging' => true], [], 'OBJECT');
|
|
|
|
|
|
|
|
$this->assertEquals(10, sizeof($repo));
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( 'tnc_rep_read_private_collections' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
//$this->subscriber2->add_cap( 'read_private_multiple_post_types' );
|
|
|
|
$current_user = $this->subscriber2; // force update current user object with new capabilities
|
|
|
|
|
|
|
|
|
|
|
|
$repo = tainacan_items()->fetch(['nopaging' => true], [], 'OBJECT');
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(15, sizeof($repo));
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col2prefix . 'read_private_items' );
|
2021-07-27 17:35:06 +00:00
|
|
|
clean_user_cache(get_current_user_id());
|
2019-10-30 13:59:38 +00:00
|
|
|
$current_user = $this->subscriber2; // force update current user object with new capabilities
|
|
|
|
|
|
|
|
$repo = tainacan_items()->fetch(['nopaging' => true], [], 'OBJECT');
|
|
|
|
|
|
|
|
$this->assertEquals(20, sizeof($repo));
|
|
|
|
|
|
|
|
|
|
|
|
$col1 = tainacan_items()->fetch(['nopaging' => true], $this->public_collection, 'OBJECT');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group items
|
|
|
|
*/
|
|
|
|
function test_fetch_collection_items() {
|
|
|
|
global $current_user;
|
|
|
|
wp_set_current_user($this->subscriber2->ID);
|
|
|
|
|
|
|
|
$col1prefix = 'tnc_col_' . $this->public_collection->get_id() . '_';
|
|
|
|
$col2prefix = 'tnc_col_' . $this->private_collection->get_id() . '_';
|
|
|
|
|
|
|
|
$col1 = tainacan_items()->fetch(['nopaging' => true], $this->public_collection, 'OBJECT');
|
|
|
|
|
|
|
|
$this->assertEquals(5, sizeof($col1));
|
|
|
|
|
|
|
|
$this->subscriber2->add_cap( $col1prefix . 'read_private_items' );
|
|
|
|
$current_user = $this->subscriber2; // force update current user object with new capabilities
|
|
|
|
|
|
|
|
$col1 = tainacan_items()->fetch(['nopaging' => true], $this->public_collection, 'OBJECT');
|
|
|
|
|
|
|
|
$this->assertEquals(10, sizeof($col1));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:41:19 +00:00
|
|
|
/**
|
|
|
|
* @group users
|
|
|
|
*/
|
|
|
|
public function test_admin_can_edit_user() {
|
|
|
|
global $current_user;
|
|
|
|
wp_set_current_user(1);
|
|
|
|
|
|
|
|
$this->assertTrue(current_user_can('edit_users'));
|
|
|
|
$this->assertTrue(current_user_can('tnc_rep_edit_users'));
|
|
|
|
|
|
|
|
$admin = get_userdata(1);
|
|
|
|
$admin->add_cap('manage_tainacan', false);
|
|
|
|
$current_user = $admin;
|
|
|
|
wp_set_current_user(1);
|
|
|
|
|
|
|
|
$this->assertFalse(current_user_can('manage_tainacan'));
|
|
|
|
$this->assertTrue(current_user_can('edit_users'));
|
|
|
|
$this->assertTrue(current_user_can('tnc_rep_edit_users'));
|
|
|
|
}
|
|
|
|
|
2019-10-25 20:28:50 +00:00
|
|
|
}
|