tainacan/tests/test-capabilities.php

741 lines
24 KiB
PHP
Raw Normal View History

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-25 20:28:50 +00:00
* 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
2019-10-25 20:28:50 +00:00
* --- private_metadatum
* --- public_filter
* --- private_filter
* - private_collection (5 items)
* --- (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(
'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(
'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(
'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(
'name' => 'Private Tax',
2019-10-18 23:44:53 +00:00
'description' => 'tipos de musica2',
'allow_insert' => 'yes',
'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
);
$term_2 = $this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $taxonomy->get_db_identifier(),
'name' => 'Term for collection 2'
),
true
);
$term_all = $this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $taxonomy->get_db_identifier(),
'name' => 'Term for all'
),
true
);
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(
'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(
'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(
'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(
'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(
'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(
'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(
'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(
'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-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-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-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-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
function test_super_all_collection () {
2019-10-25 20:28:50 +00:00
$this->assertFalse( user_can($this->subscriber, 'tnc_col_25_read_private_filters') );
2019-10-25 20:28:50 +00:00
$this->subscriber->add_cap('tnc_col_all_read_private_filters');
2019-10-25 20:28:50 +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
$this->assertFalse( user_can($this->subscriber2, 'tnc_col_25_read_private_filters') );
2019-10-25 20:28:50 +00:00
$this->subscriber2->add_cap('manage_tainacan_collection_all');
2019-10-25 20:28:50 +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-25 20:28:50 +00:00
2019-10-18 23:44:53 +00:00
// function test_items_capabilities() {
2019-10-25 20:28:50 +00:00
//
2019-10-18 23:44:53 +00:00
// $collection = $this->tainacan_entity_factory->create_entity(
// 'collection',
// array(
// 'name' => 'Javascript Frameworks',
// 'description' => 'The best framework to javascript',
// 'status' => 'publish'
// ),
// true
// );
2019-10-25 20:28:50 +00:00
//
2019-10-18 23:44:53 +00:00
// $caps = $collection->get_items_capabilities();
2019-10-25 20:28:50 +00:00
//
2019-10-18 23:44:53 +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() );
$this->subscriber2->add_cap( 'tnc_rep_edit_metadata' );
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() );
$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' );
$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' );
$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() );
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_delete_metadata' );
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' );
$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() );
2019-10-17 19:36:23 +00:00
}
2019-10-25 20:28:50 +00:00
/**
* @group filters
*/
function test_filters_metacap() {
2019-10-25 20:28:50 +00:00
wp_set_current_user($this->subscriber2->ID);
2019-10-25 20:28:50 +00:00
// 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() );
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() );
$this->subscriber2->add_cap( 'tnc_rep_edit_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->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' );
2019-10-25 20:28:50 +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() );
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_edit_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->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' );
2019-10-25 20:28:50 +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' );
$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() );
}
2019-10-25 20:28:50 +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
$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
$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
$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));
2019-10-25 20:28:50 +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
$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));
2019-10-25 20:28:50 +00:00
}
2019-10-25 20:28:50 +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
$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
$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
$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
$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
$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-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' );
$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->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() );
$this->subscriber2->add_cap( 'tnc_rep_edit_others_taxonomies' );
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() );
$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' );
$this->assertTrue( $this->private_taxonomy->can_read() );
$this->subscriber2->add_cap( 'tnc_rep_delete_taxonomies' );
$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' );
$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-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' );
$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));
}
2019-10-25 20:28:50 +00:00
}