always get fresh related collection #339
This commit is contained in:
parent
de7391a3a4
commit
fbd6e105ce
|
@ -15,21 +15,19 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
trait Entity_Collection_Relation {
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @return int collection item ID
|
||||
*/
|
||||
public function get_collection_id() {
|
||||
return $this->get_mapped_property('collection_id');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return Collection from relation
|
||||
* @return Entities\Collection|NULL Return Collection or null on errors
|
||||
*/
|
||||
public function get_collection() {
|
||||
if (isset($this->collection) && $this->collection instanceof Entities\Collection)
|
||||
return $this->collection;
|
||||
|
||||
|
||||
if (is_numeric($this->get_collection_id())) {
|
||||
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
|
||||
|
||||
|
@ -39,19 +37,19 @@ trait Entity_Collection_Relation {
|
|||
return $this->collection;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set collection ID
|
||||
* @param int $value
|
||||
*/
|
||||
public function set_collection_id($value) {
|
||||
$this->collection = null;
|
||||
$this->set_mapped_property('collection_id', $value);
|
||||
$this->set_mapped_property('collection_id', $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* set collection object and id
|
||||
* @param Entities\Collection $collection
|
||||
|
@ -61,4 +59,4 @@ trait Entity_Collection_Relation {
|
|||
$this->set_collection_id($collection->get_id());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,16 +10,14 @@ trait Entity_Collections_Relation {
|
|||
public function get_collections_ids() {
|
||||
return $this->get_mapped_property('collections_ids');
|
||||
}
|
||||
|
||||
|
||||
public function get_collections() {
|
||||
if (isset($this->collections))
|
||||
return $this->collections;
|
||||
|
||||
|
||||
if (is_array($this->get_collections_ids()) && !empty($this->get_collections_ids())) {
|
||||
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
|
||||
|
||||
|
||||
$this->collections = [];
|
||||
|
||||
|
||||
foreach ($this->get_collections_ids() as $col_id) {
|
||||
$collection = $Tainacan_Collections->fetch($col_id);
|
||||
|
||||
|
@ -27,28 +25,28 @@ trait Entity_Collections_Relation {
|
|||
$this->collections[] = $collection;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $this->collections;
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function set_collections_ids(Array $value) {
|
||||
$this->set_mapped_property('collections_ids', $value);
|
||||
$this->collections = null;
|
||||
}
|
||||
|
||||
|
||||
public function set_collections(Array $collections) {
|
||||
$collections_ids = [];
|
||||
$this->collections = $collections;
|
||||
|
||||
|
||||
foreach ($collections as $collection){
|
||||
$collections_ids[] = $collection->get_id();
|
||||
}
|
||||
|
||||
|
||||
$this->set_collections_ids($collections_ids);
|
||||
}
|
||||
|
||||
|
@ -59,7 +57,7 @@ trait Entity_Collections_Relation {
|
|||
|
||||
$this->set_collections_ids($collections);
|
||||
}
|
||||
|
||||
|
||||
public function remove_collection_id($collection_id){
|
||||
$collections = $this->get_mapped_property('collections_ids');
|
||||
|
||||
|
@ -70,4 +68,4 @@ trait Entity_Collections_Relation {
|
|||
$this->set_collections_ids($collections);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
* @group permissions
|
||||
*/
|
||||
public function test_permissions () {
|
||||
|
||||
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
|
@ -38,17 +38,17 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
);
|
||||
$this->assertTrue($item->can_read(), 'Administrator cannot read the Item');
|
||||
$this->assertTrue($item->can_edit(), 'Administrator cannot edit the Item');
|
||||
|
||||
|
||||
// another administrator should be able to edit items
|
||||
$new_admin = $this->factory()->user->create(array( 'role' => 'administrator' ));
|
||||
wp_set_current_user($new_admin);
|
||||
|
||||
|
||||
$this->assertTrue($item->can_read(), 'Administrator cannot read the Item');
|
||||
$this->assertTrue($item->can_edit(), 'Administrator cannot edit the Item');
|
||||
$this->assertTrue(current_user_can($collection->get_items_capabilities()->edit_post, $item->get_id()), 'Administrator cannot edit an item!');
|
||||
|
||||
|
||||
$sub = $this->factory()->user->create(array( 'role' => 'subscriber', 'display_name' => 'Sub' ));
|
||||
|
||||
|
||||
$collectionM = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
|
@ -67,13 +67,13 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
true
|
||||
);
|
||||
$this->assertEquals([$sub], $collectionM->get_moderators_ids());
|
||||
|
||||
|
||||
wp_set_current_user($sub);
|
||||
$this->assertTrue(current_user_can($collectionM->get_items_capabilities()->edit_post, $itemM->get_id()), 'Moderators cannot edit an item!');
|
||||
$this->assertTrue($itemM->can_edit($sub), 'Moderators cannot edit an item!');
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function teste_query(){
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
|
@ -137,9 +137,9 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum, 'value_1');
|
||||
|
||||
|
||||
$item = $Tainacan_Items->fetch($i->get_id());
|
||||
$meta_test = new Entities\Item_Metadata_Entity($item, $metadatum);
|
||||
$this->assertTrue( $meta_test instanceof Entities\Item_Metadata_Entity );
|
||||
|
@ -155,9 +155,9 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum3, 'value_2');
|
||||
|
||||
|
||||
$i = $this->tainacan_entity_factory->create_entity(
|
||||
'item',
|
||||
array(
|
||||
|
@ -167,7 +167,7 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum2, 'value_2');
|
||||
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum2, 'value_3');
|
||||
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum3, 'value_3');
|
||||
|
@ -181,22 +181,22 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum2, 'value_3');
|
||||
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum3, 'value_6');
|
||||
|
||||
// should return all 4 items
|
||||
$test_query = $Tainacan_Items->fetch([]);
|
||||
$this->assertEquals(4, $test_query->post_count );
|
||||
|
||||
|
||||
// should also return all 4 items
|
||||
$test_query = $Tainacan_Items->fetch([], [$collection, $collection2]);
|
||||
$this->assertEquals(4, $test_query->post_count);
|
||||
|
||||
|
||||
// should return 2 items
|
||||
$test_query = $Tainacan_Items->fetch(['posts_per_page' => 2], [$collection, $collection2]);
|
||||
$this->assertEquals(2, $test_query->post_count);
|
||||
|
||||
|
||||
// should return only the first item
|
||||
$test_query = $Tainacan_Items->fetch([], $collection);
|
||||
$this->assertEquals(1,$test_query->post_count);
|
||||
|
@ -211,18 +211,18 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
|
||||
$this->assertEquals(1, $test_query->post_count);
|
||||
$this->assertEquals('orange', $item2->get_title());
|
||||
|
||||
|
||||
// should return the other 3 items
|
||||
$test_query = $Tainacan_Items->fetch([], $collection2);
|
||||
$this->assertEquals(3,$test_query->post_count);
|
||||
|
||||
|
||||
$test_query = $Tainacan_Items->fetch(['title' => 'apple']);
|
||||
$test_query->the_post();
|
||||
$item3 = new Entities\Item( get_the_ID() );
|
||||
|
||||
$this->assertEquals(1, $test_query->post_count);
|
||||
$this->assertEquals('apple', $item3->get_title());
|
||||
|
||||
|
||||
// should return 1 item
|
||||
$test_query = $Tainacan_Items->fetch([
|
||||
'meta_query' => [
|
||||
|
@ -233,7 +233,7 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
]
|
||||
], $collection2);
|
||||
$this->assertEquals(2, $test_query->post_count);
|
||||
|
||||
|
||||
// should return 2 items
|
||||
$test_query = $Tainacan_Items->fetch([
|
||||
'meta_query' => [
|
||||
|
@ -244,7 +244,7 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
]
|
||||
], $collection2);
|
||||
$this->assertEquals(2, $test_query->post_count);
|
||||
|
||||
|
||||
// should return 2 items
|
||||
$test_query = $Tainacan_Items->fetch([
|
||||
'meta_query' => [
|
||||
|
@ -256,7 +256,7 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
]
|
||||
], $collection2);
|
||||
$this->assertEquals(2, $test_query->post_count);
|
||||
|
||||
|
||||
// test fetch ids
|
||||
$test_query = $Tainacan_Items->fetch_ids([]);
|
||||
$this->assertTrue( is_array($test_query) );
|
||||
|
@ -265,13 +265,13 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
$this->assertTrue( is_int($test_query[1]) );
|
||||
$this->assertTrue( is_int($test_query[2]) );
|
||||
$this->assertTrue( is_int($test_query[3]) );
|
||||
|
||||
|
||||
$test_query = $Tainacan_Items->fetch_ids(['title' => 'inexistent']);
|
||||
$this->assertTrue( is_array($test_query) );
|
||||
$this->assertEquals(0, sizeof($test_query) );
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function teste_meta_query_in(){
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
|
@ -304,9 +304,9 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum, 'value_10');
|
||||
|
||||
|
||||
$i = $this->tainacan_entity_factory->create_entity(
|
||||
'item',
|
||||
array(
|
||||
|
@ -316,9 +316,9 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum, 'value_100');
|
||||
|
||||
|
||||
// should return 1 items
|
||||
$test_query = $Tainacan_Items->fetch([
|
||||
'meta_query' => [
|
||||
|
@ -330,10 +330,10 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
]
|
||||
], $collection);
|
||||
$this->assertEquals(1, $test_query->post_count);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @group comments
|
||||
*/
|
||||
|
@ -358,31 +358,31 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
true
|
||||
);
|
||||
global $wp_query;
|
||||
|
||||
|
||||
$wp_query = new \WP_Query();
|
||||
|
||||
|
||||
$this->assertTrue(setup_postdata($item->WP_Post));
|
||||
|
||||
|
||||
$this->assertFalse(comments_open($item->get_id()));
|
||||
|
||||
|
||||
$collections = \Tainacan\Repositories\Collections::get_instance();
|
||||
$collection->set('allow_comments', 'open');
|
||||
$this->assertTrue($collection->validate());
|
||||
$collections->update($collection);
|
||||
|
||||
|
||||
$this->assertTrue(comments_open($item->get_id()));
|
||||
|
||||
|
||||
$items = \Tainacan\Repositories\Items::get_instance();
|
||||
|
||||
|
||||
$item->set('comment_status', 'closed');
|
||||
$this->assertTrue($item->validate());
|
||||
$items->update($item);
|
||||
|
||||
|
||||
$this->assertFalse(comments_open($item->get_id()));
|
||||
}
|
||||
|
||||
|
||||
public function test_delete_item() {
|
||||
|
||||
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
|
@ -402,17 +402,51 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
true,
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$item_id = $item->get_id();
|
||||
|
||||
|
||||
$items = \Tainacan\Repositories\Items::get_instance();
|
||||
|
||||
|
||||
$items->delete($item);
|
||||
|
||||
|
||||
$fetch_item = $items->fetch($item_id);
|
||||
|
||||
|
||||
$this->assertEmpty($fetch_item);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_update_collection() {
|
||||
|
||||
$ItemRepo = \Tainacan\Repositories\Items::get_instance();
|
||||
$ColRepo = \Tainacan\Repositories\Collections::get_instance();
|
||||
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'teste1',
|
||||
'description' => 'adasdasdsa',
|
||||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$item = $this->tainacan_entity_factory->create_entity(
|
||||
'item',
|
||||
array(
|
||||
'title' => 'testeItem',
|
||||
'collection' => $collection,
|
||||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$collection->set_status('private');
|
||||
$collection->validate();
|
||||
$collection = $ColRepo->insert($collection);
|
||||
|
||||
$this->assertEquals('private', $item->get_collection()->get_status());
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -255,9 +255,6 @@ class Permissions extends TAINACAN_UnitTestCase {
|
|||
$collection->validate();
|
||||
$collection = $ColRepo->insert($collection);
|
||||
|
||||
// refresh item
|
||||
$item = new \Tainacan\Entities\Item($item->get_id());
|
||||
|
||||
$this->assertFalse($item->can_read());
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue