2017-11-12 23:14:47 +00:00
|
|
|
<?php
|
2017-11-15 18:50:11 +00:00
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
2017-11-12 23:14:47 +00:00
|
|
|
/**
|
|
|
|
* Class TestCollections
|
|
|
|
*
|
|
|
|
* @package Test_Tainacan
|
|
|
|
*/
|
|
|
|
|
2017-12-04 18:20:49 +00:00
|
|
|
use Tainacan\Entities;
|
2017-11-29 18:06:22 +00:00
|
|
|
|
2017-11-12 23:14:47 +00:00
|
|
|
/**
|
|
|
|
* Sample test case.
|
|
|
|
*/
|
2017-12-04 18:20:49 +00:00
|
|
|
class Items extends TAINACAN_UnitTestCase {
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2018-01-18 14:30:57 +00:00
|
|
|
/**
|
2018-01-26 18:44:45 +00:00
|
|
|
* @group permissions2
|
2018-01-18 14:30:57 +00:00
|
|
|
*/
|
|
|
|
public function test_permissions () {
|
2018-01-29 01:34:12 +00:00
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
2018-01-18 14:30:57 +00:00
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'testePerm',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$item = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'testeItem',
|
|
|
|
'collection' => $collection,
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->assertTrue($item->can_read(), 'Administrator cannot read the Item');
|
|
|
|
$this->assertTrue($item->can_edit(), 'Administrator cannot edit the Item');
|
2018-01-29 01:34:12 +00:00
|
|
|
|
|
|
|
// 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!');
|
|
|
|
|
2018-01-26 18:44:45 +00:00
|
|
|
$sub = $this->factory()->user->create(array( 'role' => 'subscriber', 'display_name' => 'Sub' ));
|
|
|
|
|
|
|
|
$collectionM = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'testePermModerator',
|
|
|
|
'moderators_ids' => [$sub]
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2018-01-27 02:23:25 +00:00
|
|
|
|
2018-01-26 18:44:45 +00:00
|
|
|
$itemM = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'testeItemModerator',
|
|
|
|
'collection' => $collectionM,
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->assertEquals([$sub], $collectionM->get_moderators_ids());
|
2018-01-27 02:23:25 +00:00
|
|
|
|
|
|
|
wp_set_current_user($sub);
|
2018-01-29 01:34:12 +00:00
|
|
|
$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!');
|
2018-01-26 18:44:45 +00:00
|
|
|
|
2018-01-18 14:30:57 +00:00
|
|
|
}
|
2017-11-12 23:14:47 +00:00
|
|
|
|
|
|
|
function teste_query(){
|
2017-12-04 18:20:49 +00:00
|
|
|
$type = $this->tainacan_field_factory->create_field('text');
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-12-04 18:20:49 +00:00
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'teste',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-12-01 13:09:08 +00:00
|
|
|
|
2017-12-04 18:20:49 +00:00
|
|
|
$collection2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'teste2',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2017-12-04 18:20:49 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'status' => 'publish',
|
|
|
|
'collection' => $collection,
|
2017-12-05 17:42:15 +00:00
|
|
|
'field_type' => $type
|
2017-12-04 18:20:49 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2017-12-04 18:20:49 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado2',
|
|
|
|
'status' => 'publish',
|
|
|
|
'collection' => $collection,
|
2017-12-05 17:42:15 +00:00
|
|
|
'field_type' => $type
|
2017-12-04 18:20:49 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field3 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2017-12-04 18:20:49 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado3',
|
|
|
|
'status' => 'publish',
|
|
|
|
'collection' => $collection,
|
2017-12-05 17:42:15 +00:00
|
|
|
'field_type' => $type
|
2017-12-04 18:20:49 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-11-13 16:03:04 +00:00
|
|
|
global $Tainacan_Items;
|
2017-12-04 18:20:49 +00:00
|
|
|
|
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'orange',
|
|
|
|
'collection' => $collection,
|
2018-01-29 13:09:44 +00:00
|
|
|
'status' => 'publish'
|
2017-12-04 18:20:49 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2018-02-08 18:28:30 +00:00
|
|
|
|
|
|
|
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field, 'value_1');
|
|
|
|
|
2017-12-04 18:20:49 +00:00
|
|
|
$item = $Tainacan_Items->fetch($i->get_id());
|
2018-02-08 18:28:30 +00:00
|
|
|
$meta_test = new Entities\Item_Metadata_Entity($item, $field);
|
|
|
|
$this->assertTrue( $meta_test instanceof Entities\Item_Metadata_Entity );
|
|
|
|
$this->assertEquals( $field->get_id(), $meta_test->get_field()->get_id() );
|
|
|
|
$this->assertEquals( 'value_1', $meta_test->get_value());
|
2017-12-04 18:20:49 +00:00
|
|
|
|
2018-02-08 18:28:30 +00:00
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
2017-12-04 18:20:49 +00:00
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'apple',
|
|
|
|
'collection' => $collection2,
|
2018-01-29 13:09:44 +00:00
|
|
|
'status' => 'publish'
|
2017-12-04 18:20:49 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2018-02-08 18:28:30 +00:00
|
|
|
|
|
|
|
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field3, 'value_2');
|
|
|
|
|
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
2017-12-04 18:20:49 +00:00
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'lemon',
|
|
|
|
'collection' => $collection2,
|
2018-01-29 13:09:44 +00:00
|
|
|
'status' => 'publish'
|
2017-12-04 18:20:49 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2018-02-08 18:28:30 +00:00
|
|
|
|
|
|
|
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field2, 'value_2');
|
|
|
|
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field2, 'value_3');
|
|
|
|
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field3, 'value_3');
|
2017-12-04 18:20:49 +00:00
|
|
|
|
2018-02-08 18:28:30 +00:00
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
2017-12-04 18:20:49 +00:00
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'pineapple',
|
|
|
|
'collection' => $collection2,
|
2018-01-29 13:09:44 +00:00
|
|
|
'status' => 'publish'
|
2017-12-04 18:20:49 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2018-02-08 18:28:30 +00:00
|
|
|
|
|
|
|
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field2, 'value_3');
|
|
|
|
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field3, 'value_6');
|
2017-12-04 18:20:49 +00:00
|
|
|
|
2017-11-12 23:14:47 +00:00
|
|
|
// should return all 4 items
|
2017-11-30 16:42:11 +00:00
|
|
|
$test_query = $Tainacan_Items->fetch([]);
|
2017-11-29 18:06:22 +00:00
|
|
|
$this->assertEquals(4, $test_query->post_count );
|
2017-11-12 23:14:47 +00:00
|
|
|
|
|
|
|
// should also return all 4 items
|
2017-11-30 16:42:11 +00:00
|
|
|
$test_query = $Tainacan_Items->fetch([], [$collection, $collection2]);
|
2017-11-29 18:06:22 +00:00
|
|
|
$this->assertEquals(4, $test_query->post_count);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-11-30 16:42:11 +00:00
|
|
|
// should return 2 items
|
|
|
|
$test_query = $Tainacan_Items->fetch(['posts_per_page' => 2], [$collection, $collection2]);
|
|
|
|
$this->assertEquals(2, $test_query->post_count);
|
|
|
|
|
2017-11-12 23:14:47 +00:00
|
|
|
// should return only the first item
|
2017-11-30 16:42:11 +00:00
|
|
|
$test_query = $Tainacan_Items->fetch([], $collection);
|
2017-11-29 18:06:22 +00:00
|
|
|
$this->assertEquals(1,$test_query->post_count);
|
|
|
|
|
|
|
|
$test_query->the_post();
|
2017-12-04 18:20:49 +00:00
|
|
|
$item1 = new Entities\Item( get_the_ID() );
|
|
|
|
$this->assertEquals('orange', $item1->get_title() );
|
2017-11-29 18:06:22 +00:00
|
|
|
|
2017-11-30 16:42:11 +00:00
|
|
|
$test_query = $Tainacan_Items->fetch(['title' => 'orange']);
|
2017-11-29 18:06:22 +00:00
|
|
|
$test_query->the_post();
|
2017-12-04 18:20:49 +00:00
|
|
|
$item2 = new Entities\Item( get_the_ID() );
|
2017-11-29 18:06:22 +00:00
|
|
|
|
|
|
|
$this->assertEquals(1, $test_query->post_count);
|
2017-12-04 18:20:49 +00:00
|
|
|
$this->assertEquals('orange', $item2->get_title());
|
2017-11-12 23:14:47 +00:00
|
|
|
|
|
|
|
// should return the other 3 items
|
2017-11-30 16:42:11 +00:00
|
|
|
$test_query = $Tainacan_Items->fetch([], $collection2);
|
2017-11-29 18:06:22 +00:00
|
|
|
$this->assertEquals(3,$test_query->post_count);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-11-30 16:42:11 +00:00
|
|
|
$test_query = $Tainacan_Items->fetch(['title' => 'apple']);
|
2017-11-29 18:06:22 +00:00
|
|
|
$test_query->the_post();
|
2017-12-04 18:20:49 +00:00
|
|
|
$item3 = new Entities\Item( get_the_ID() );
|
2017-11-29 18:06:22 +00:00
|
|
|
|
|
|
|
$this->assertEquals(1, $test_query->post_count);
|
2017-12-04 18:20:49 +00:00
|
|
|
$this->assertEquals('apple', $item3->get_title());
|
2017-11-12 23:14:47 +00:00
|
|
|
|
|
|
|
// should return 1 item
|
2017-11-30 16:42:11 +00:00
|
|
|
$test_query = $Tainacan_Items->fetch([
|
|
|
|
'meta_query' => [
|
2017-11-12 23:14:47 +00:00
|
|
|
[
|
2018-01-31 14:47:59 +00:00
|
|
|
'key' => $field2->get_id(),
|
2018-02-16 17:35:24 +00:00
|
|
|
'value' => 'value_3'
|
2017-11-12 23:14:47 +00:00
|
|
|
]
|
|
|
|
]
|
2017-11-30 16:42:11 +00:00
|
|
|
], $collection2);
|
2018-02-16 17:35:24 +00:00
|
|
|
$this->assertEquals(2, $test_query->post_count);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
|
|
|
// should return 2 items
|
2017-11-30 16:42:11 +00:00
|
|
|
$test_query = $Tainacan_Items->fetch([
|
|
|
|
'meta_query' => [
|
2017-11-12 23:14:47 +00:00
|
|
|
[
|
2018-01-31 14:47:59 +00:00
|
|
|
'key' => $field2->get_id(),
|
2017-11-12 23:14:47 +00:00
|
|
|
'value' => 'value_3'
|
|
|
|
]
|
|
|
|
]
|
2017-11-30 16:42:11 +00:00
|
|
|
], $collection2);
|
2017-11-29 18:06:22 +00:00
|
|
|
$this->assertEquals(2, $test_query->post_count);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2018-02-08 18:28:30 +00:00
|
|
|
// should return 2 items
|
2017-11-30 16:42:11 +00:00
|
|
|
$test_query = $Tainacan_Items->fetch([
|
|
|
|
'meta_query' => [
|
2017-11-12 23:14:47 +00:00
|
|
|
[
|
2018-01-31 14:47:59 +00:00
|
|
|
'key' => $field3->get_id(),
|
2017-11-12 23:14:47 +00:00
|
|
|
'value' => 'value_2',
|
|
|
|
'compare' => '>'
|
|
|
|
]
|
|
|
|
]
|
2017-11-30 16:42:11 +00:00
|
|
|
], $collection2);
|
2017-11-29 18:06:22 +00:00
|
|
|
$this->assertEquals(2, $test_query->post_count);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|