fetch items returns items from private collections correctly

This commit is contained in:
Leo Germani 2018-02-16 16:48:50 -02:00
parent 853239c0cc
commit f33d474bde
2 changed files with 122 additions and 16 deletions

View File

@ -194,27 +194,46 @@ class Items extends Repository {
if (is_numeric($collections)){
$collections = $Tainacan_Collections->fetch($collections);
}
$collections_objects = [];
$cpt = [];
if ($collections instanceof Entities\Collection) {
$cpt = $collections->get_db_identifier();
$collections_objects[] = $collections;
} elseif (is_array($collections)) {
$cpt = [];
foreach ($collections as $collection) {
if (is_numeric($collection)){
$collection = $Tainacan_Collections->fetch($collection);
foreach ($collections as $col) {
if (is_numeric($col)){
$col = $Tainacan_Collections->fetch($col);
}
if ($collection instanceof Entities\Collection){
$cpt[] = $collection->get_db_identifier();
if ($col instanceof Entities\Collection){
$collections_objects[] = $col;
}
}
} else {
return [];
}
foreach ($collections_objects as $collection) {
/**
* If no specific status is defined in the query, WordPress will fetch
* public items and private items for users withe the correct permission.
*
* If a collection is private, it must have the same behavior, despite its
* items are public or not.
*/
if (!isset($args['post_status'])) {
$status_obj = get_post_status_object( $collection->get_status() );
if ( $status_obj->public || current_user_can( $collection->cap->read_private_posts ) ) {
$cpt[] = $collection->get_db_identifier();
}
}
}
if (empty($cpt)){
return [];
$cpt[] = 'please-return-nothing';
}
//TODO: get collection order and order by options

View File

@ -16,17 +16,20 @@ use Tainacan\Entities;
* Private items should only be visible by logged users who have the rights
*
*/
/**
* @group privateObjects
*/
class PrivateObjects extends TAINACAN_UnitTestCase {
/**
* @group privateObjects
*/
// TODO Test the same things via API
public function test_private_items () {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testePerm',
'status' => 'publish'
),
true
);
@ -63,4 +66,88 @@ class PrivateObjects extends TAINACAN_UnitTestCase {
}
public function test_items_in_private_collections () {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testePerm',
'status' => 'publish'
),
true
);
$privateCollection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testePerm',
'status' => 'private'
),
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testItem',
'collection' => $collection,
'status' => 'publish'
),
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testItem',
'collection' => $collection,
'status' => 'publish'
),
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testItem',
'collection' => $privateCollection,
'status' => 'publish'
),
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testItem',
'collection' => $privateCollection,
'status' => 'publish'
),
true
);
$new_contributor_user = $this->factory()->user->create(array( 'role' => 'contributor' ));
wp_set_current_user($new_contributor_user);
global $Tainacan_Items, $Tainacan_Collections;
$items = $Tainacan_Items->fetch([], $collection);
$this->assertEquals(2, $items->found_posts, 'items of a public collections should be visible');
$items = $Tainacan_Items->fetch([], $privateCollection);
$this->assertEquals(0, $items->found_posts, 'items of a private collection should not be visible');
$privateCollection->set_status('publish');
$privateCollection->validate();
$privateCollection = $Tainacan_Collections->insert($privateCollection);
$items = $Tainacan_Items->fetch([], $privateCollection);
$this->assertEquals(2, $items->found_posts, 'items should be visible after collections is made public');
$privateCollection->set_status('private');
$privateCollection->validate();
$privateCollection = $Tainacan_Collections->insert($privateCollection);
$items = $Tainacan_Items->fetch([], $privateCollection);
$this->assertEquals(0, $items->found_posts, 'items should not be visible after collection is made private');
}
}