tainacan/tests/test-items.php

251 lines
6.7 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-01-26 18:44:45 +00:00
* @group permissions2
2018-01-18 14:30:57 +00:00
*/
public function test_permissions () {
$collection = $this->tainacan_entity_factory->create_entity(
'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-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
);
$itemM = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'testeItemModerator',
'collection' => $collectionM,
),
true
);
$this->assertEquals([$sub], $collectionM->get_moderators_ids());
$this->assertTrue($itemM->can_edit($sub), 'Moderators cannot edit a 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
$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-04 18:20:49 +00:00
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste2',
'status' => 'publish'
),
true
);
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',
'status' => 'publish',
'collection' => $collection,
'field_type' => $type
2017-12-04 18:20:49 +00:00
),
true
);
$metadata2 = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado2',
'status' => 'publish',
'collection' => $collection,
'field_type' => $type
2017-12-04 18:20:49 +00:00
),
true
);
$metadata3 = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado3',
'status' => 'publish',
'collection' => $collection,
'field_type' => $type
2017-12-04 18:20:49 +00:00
),
true
);
2017-11-12 23:14:47 +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,
'add_metadata' => [
[$metadata, 'value_1']
]
),
true
);
$item = $Tainacan_Items->fetch($i->get_id());
2017-11-12 23:14:47 +00:00
$meta_test = $item->get_metadata();
2017-12-04 18:20:49 +00:00
2017-11-12 23:14:47 +00:00
$this->assertTrue( isset($meta_test[$metadata->get_id()]) );
2017-12-04 18:20:49 +00:00
$this->assertTrue( $meta_test[$metadata->get_id()] instanceof Entities\Item_Metadata_Entity );
2017-11-24 18:54:52 +00:00
$this->assertEquals( 'value_1', $meta_test[$metadata->get_id()]->get_value());
2017-12-04 18:20:49 +00:00
$this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'apple',
'collection' => $collection2,
'add_metadata' => [
[$metadata2, 'value_2'],
[$metadata3, 'value_2']
]
),
true
);
$this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'lemon',
'collection' => $collection2,
'add_metadata' => [
[$metadata2, 'value_2'],
[$metadata2, 'value_3'],
[$metadata3, 'value_3']
]
),
true
);
$this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'pineapple',
'collection' => $collection2,
'add_metadata' => [
[$metadata2, 'value_3'],
[$metadata3, 'value_6']
]
),
true
);
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
[
'key' => $metadata2->get_id(),
'value' => 'value_2'
]
]
2017-11-30 16:42:11 +00:00
], $collection2);
2017-11-29 18:06:22 +00:00
$this->assertEquals(1, $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' => $metadata2->get_id(),
'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
// should return 2 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' => $metadata3->get_id(),
'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
}
}