Merge branch 'develop' of https://github.com/tainacan/tainacan into develop

This commit is contained in:
mateuswetah 2018-02-16 17:12:09 -02:00
commit 9800494d49
4 changed files with 191 additions and 24 deletions

View File

@ -194,36 +194,51 @@ 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
$args = $this->parse_fetch_args($args);
$args = array_merge([
'post_status' => 'publish',
], $args);
$args['post_type'] = $cpt;

View File

@ -76,8 +76,8 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
$first_item = $data[0];
$second_item = $data[1];
$this->assertEquals($item2->get_title(), $first_item['title']);
$this->assertEquals($item1->get_title(), $second_item['title']);
$this->assertEquals($item1->get_title(), $first_item['title']);
$this->assertEquals($item2->get_title(), $second_item['title']);
}
public function test_delete_or_trash_item_from_a_collection(){

View File

@ -169,6 +169,9 @@ class Fields extends TAINACAN_UnitTestCase {
$this->assertEquals( 6, sizeof( $retrieve_metadata ) );
}
/**
* test remove core fields
*/
function test_core_fields(){
global $Tainacan_Fields;
@ -182,13 +185,9 @@ class Fields extends TAINACAN_UnitTestCase {
$core_fields = $Tainacan_Fields->get_core_fields( $collection_grandfather );
$this->expectException(\ErrorException::class);
if( $core_fields ){
foreach( $core_fields as $core_field ){
wp_trash_post( $core_field->get_id() );
$this->assertFalse(wp_trash_post( $core_field->get_id() ) );
}
}
}

View File

@ -0,0 +1,153 @@
<?php
namespace Tainacan\Tests;
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
use Tainacan\Entities;
/**
* Test fetch methods to see if they return private objects (items, fiels, collections) correctly
*
* Private items should only be visible by logged users who have the rights
*
*/
/**
* @group privateObjects
*/
class PrivateObjects extends TAINACAN_UnitTestCase {
// 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
);
$privateItem = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testPrivateItem',
'collection' => $collection,
'status' => 'private'
),
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testItem',
'collection' => $collection,
'status' => 'publish'
),
true
);
global $Tainacan_Items;
$items = $Tainacan_Items->fetch([], $collection);
$this->assertEquals(2, $items->found_posts, 'admins should see all 2 items');
$new_contributor_user = $this->factory()->user->create(array( 'role' => 'contributor' ));
wp_set_current_user($new_contributor_user);
$items = $Tainacan_Items->fetch([], $collection);
$this->assertEquals(1, $items->found_posts, 'contributors should not see private items');
}
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');
}
}