tainacan/tests/test-items.php

520 lines
14 KiB
PHP
Raw Normal View History

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-02-09 18:59:57 +00:00
* @group permissions
2018-01-18 14:30:57 +00:00
*/
public function test_permissions () {
$collection = $this->tainacan_entity_factory->create_entity(
2018-01-18 14:30:57 +00:00
'collection',
array(
'name' => 'testePerm',
'status' => 'publish'
2018-01-18 14:30:57 +00:00
),
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');
// 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-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
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'status' => 'publish'
),
true
);
2017-12-04 18:20:49 +00:00
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste2',
'status' => 'publish'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadado',
'status' => 'publish',
'collection' => $collection,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
2017-12-04 18:20:49 +00:00
),
true
);
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadado2',
'status' => 'publish',
'collection' => $collection,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
2017-12-04 18:20:49 +00:00
),
true
);
$metadatum3 = $this->tainacan_entity_factory->create_entity(
'metadatum',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadado3',
'status' => 'publish',
'collection' => $collection,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
2017-12-04 18:20:49 +00:00
),
true
);
2017-11-12 23:14:47 +00:00
2018-04-11 14:18:55 +00:00
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
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
);
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum, 'value_1');
2017-12-04 18:20:49 +00:00
$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 );
$this->assertEquals( $metadatum->get_id(), $meta_test->get_metadatum()->get_id() );
$this->assertEquals( 'value_1', $meta_test->get_value());
2017-12-04 18:20:49 +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
);
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum3, '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
);
$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');
2017-12-04 18:20:49 +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
);
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum2, 'value_3');
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum3, '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-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-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
[
'key' => $metadatum2->get_id(),
'value' => 'value_3'
2017-11-12 23:14:47 +00:00
]
]
2017-11-30 16:42:11 +00:00
], $collection2);
$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
[
'key' => $metadatum2->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);
// 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
[
'key' => $metadatum3->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);
// test fetch ids
$test_query = $Tainacan_Items->fetch_ids([]);
$this->assertTrue( is_array($test_query) );
$this->assertEquals(4, sizeof($test_query) );
$this->assertTrue( is_int($test_query[0]) );
$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',
array(
'name' => 'teste',
'status' => 'publish'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadado',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'teste10',
'collection' => $collection,
'status' => 'publish'
),
true
);
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum, 'value_10');
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'test100',
'collection' => $collection,
'status' => 'publish'
),
true
);
$this->tainacan_item_metadata_factory->create_item_metadata($i, $metadatum, 'value_100');
// should return 1 items
$test_query = $Tainacan_Items->fetch([
'meta_query' => [
[
'key' => $metadatum->get_id(),
'value' => ['value_10'],
'compare' => 'IN'
]
]
], $collection);
$this->assertEquals(1, $test_query->post_count);
2017-11-12 23:14:47 +00:00
}
2018-08-21 03:22:47 +00:00
/**
* @group comments
*/
public function test_items_comment() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collectionComments',
2018-08-28 03:19:55 +00:00
'allow_comments' => 'closed'
2018-08-21 03:22:47 +00:00
),
true,
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'itemComments1',
'collection' => $collection,
'comment_status' => 'open'
),
true,
true
);
global $wp_query;
2018-08-21 03:22:47 +00:00
$wp_query = new \WP_Query();
2018-08-21 03:22:47 +00:00
$this->assertTrue(setup_postdata($item->WP_Post));
2018-08-21 03:22:47 +00:00
$this->assertFalse(comments_open($item->get_id()));
2018-08-21 03:22:47 +00:00
$collections = \Tainacan\Repositories\Collections::get_instance();
2018-08-28 03:19:55 +00:00
$collection->set('allow_comments', 'open');
$this->assertTrue($collection->validate());
2018-08-21 03:22:47 +00:00
$collections->update($collection);
2018-08-21 03:22:47 +00:00
$this->assertTrue(comments_open($item->get_id()));
2018-08-21 03:22:47 +00:00
$items = \Tainacan\Repositories\Items::get_instance();
2018-08-21 03:22:47 +00:00
$item->set('comment_status', 'closed');
2018-08-28 03:19:55 +00:00
$this->assertTrue($item->validate());
2018-08-21 03:22:47 +00:00
$items->update($item);
2018-08-21 03:22:47 +00:00
$this->assertFalse(comments_open($item->get_id()));
}
public function test_delete_item() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collectionComments',
'allow_comments' => 'closed'
),
true,
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'itemComments1',
'collection' => $collection,
'comment_status' => 'open'
),
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());
}
function test_order_by_date_same_date() {
$date = '2019-10-10 10:10:10';
$ItemRepo = \Tainacan\Repositories\Items::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste1',
'description' => 'adasdasdsa',
'status' => 'publish'
),
true
);
$item1 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testeItem',
'collection' => $collection,
'status' => 'publish',
'creation_date' => $date
),
true
);
$item2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testeItem',
'collection' => $collection,
'status' => 'publish',
'creation_date' => $date
),
true
);
$item3 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testeItem',
'collection' => $collection,
'status' => 'publish',
'creation_date' => $date
),
true
);
$item4 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testeItem',
'collection' => $collection,
'status' => 'publish',
'creation_date' => $date
),
true
);
$item5 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testeItem',
'collection' => $collection,
'status' => 'publish',
'creation_date' => $date
),
true
);
// should always return in the same order
for ($i=1; $i<20; $i++) {
$items = $ItemRepo->fetch([], $collection, 'OBJECT');
$this->assertEquals($item5->get_id(), $items[0]->get_id());
$this->assertEquals($item1->get_id(), $items[4]->get_id());
}
// also when ordering by metadata
for ($i=1; $i<20; $i++) {
$items = $ItemRepo->fetch([
'meta_key' => 'collection_id',
'orderby' => 'meta_value'
], $collection, 'OBJECT');
$this->assertEquals($item5->get_id(), $items[0]->get_id());
$this->assertEquals($item1->get_id(), $items[4]->get_id());
}
}
}