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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sample test case.
|
|
|
|
*/
|
2017-12-04 14:58:19 +00:00
|
|
|
class Item_Metadata extends TAINACAN_UnitTestCase {
|
2017-11-12 23:14:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Teste da insercao de um metadado simples sem o tipo
|
|
|
|
*/
|
|
|
|
function test_add() {
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
global $Tainacan_Fields, $Tainacan_Item_Metadata;
|
2017-12-04 14:58:19 +00:00
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
2017-12-18 12:52:45 +00:00
|
|
|
'name' => 'teste',
|
|
|
|
'description' => 'No description',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2017-12-04 14:58:19 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
2018-02-21 17:04:25 +00:00
|
|
|
'field_type' => 'Tainacan\Field_Types\Text',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$test = $Tainacan_Fields->fetch($field->get_id());
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-12-04 14:58:19 +00:00
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'item teste',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'collection' => $collection
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-11-13 16:03:04 +00:00
|
|
|
global $Tainacan_Items;
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-12-04 14:58:19 +00:00
|
|
|
$item = $Tainacan_Items->fetch($i->get_id());
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-12-04 14:58:19 +00:00
|
|
|
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test);
|
2017-11-12 23:14:47 +00:00
|
|
|
$item_metadata->set_value('teste_value');
|
2017-12-04 14:58:19 +00:00
|
|
|
|
2017-11-12 23:14:47 +00:00
|
|
|
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
|
|
|
|
|
|
|
|
$this->assertEquals('teste_value', $item_metadata->get_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Teste da insercao de um metadado simples com o tipo
|
|
|
|
*/
|
|
|
|
function teste_required(){
|
2018-01-31 14:47:59 +00:00
|
|
|
global $Tainacan_Fields, $Tainacan_Item_Metadata;
|
2017-12-04 14:58:19 +00:00
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
2017-12-18 12:52:45 +00:00
|
|
|
'name' => 'teste',
|
|
|
|
'description' => 'No description',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2017-12-04 14:58:19 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
|
|
|
'required' => 'yes',
|
2018-02-21 17:04:25 +00:00
|
|
|
'field_type' => 'Tainacan\Field_Types\Text',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$test = $Tainacan_Fields->fetch($field->get_id());
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-12-04 14:58:19 +00:00
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'item teste',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'collection' => $collection
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-11-13 16:03:04 +00:00
|
|
|
global $Tainacan_Items;
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-12-04 14:58:19 +00:00
|
|
|
$item = $Tainacan_Items->fetch($i->get_id());
|
|
|
|
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
|
|
|
// false because its required
|
|
|
|
$this->assertFalse($item_metadata->validate());
|
|
|
|
|
|
|
|
$item_metadata->set_value('teste_value');
|
|
|
|
|
|
|
|
$this->assertTrue($item_metadata->validate());
|
|
|
|
|
|
|
|
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
|
|
|
|
|
|
|
|
$this->assertEquals('teste_value', $item_metadata->get_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
function teste_collection_key(){
|
2018-01-31 14:47:59 +00:00
|
|
|
global $Tainacan_Fields, $Tainacan_Item_Metadata;
|
2017-12-04 14:58:19 +00:00
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
2017-12-18 12:52:45 +00:00
|
|
|
'name' => 'teste',
|
|
|
|
'description' => 'No description',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2017-12-04 14:58:19 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
|
|
|
'collection_key' => 'yes',
|
2018-02-21 17:04:25 +00:00
|
|
|
'field_type' => 'Tainacan\Field_Types\Text',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$test = $Tainacan_Fields->fetch($field->get_id());
|
2017-12-04 14:58:19 +00:00
|
|
|
|
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'item teste',
|
|
|
|
'description' => 'adasdasdsa',
|
2018-01-29 13:09:44 +00:00
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-11-13 16:03:04 +00:00
|
|
|
global $Tainacan_Items;
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2017-12-04 14:58:19 +00:00
|
|
|
$item = $Tainacan_Items->fetch($i->get_id());
|
|
|
|
|
2017-11-12 23:14:47 +00:00
|
|
|
$value = 'teste_val';
|
|
|
|
|
2017-12-04 14:58:19 +00:00
|
|
|
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test);
|
2017-11-12 23:14:47 +00:00
|
|
|
$item_metadata->set_value($value);
|
2017-12-04 14:58:19 +00:00
|
|
|
|
2017-11-12 23:14:47 +00:00
|
|
|
$this->assertTrue($item_metadata->validate());
|
2017-12-04 14:58:19 +00:00
|
|
|
|
2017-11-30 18:00:25 +00:00
|
|
|
$item_metadata->validate();
|
2017-11-12 23:14:47 +00:00
|
|
|
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
|
|
|
|
|
2017-12-04 14:58:19 +00:00
|
|
|
$n_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test);
|
2017-11-12 23:14:47 +00:00
|
|
|
$n_item_metadata->set_value($value);
|
2017-12-04 14:58:19 +00:00
|
|
|
|
2017-11-12 23:14:47 +00:00
|
|
|
$this->assertFalse($n_item_metadata->validate());
|
|
|
|
}
|
2017-12-03 21:49:50 +00:00
|
|
|
|
|
|
|
function teste_fetch(){
|
2017-12-04 14:58:19 +00:00
|
|
|
global $Tainacan_Item_Metadata;
|
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
2017-12-18 12:52:45 +00:00
|
|
|
'name' => 'teste',
|
|
|
|
'description' => 'No description',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2017-12-04 18:20:49 +00:00
|
|
|
$this->tainacan_entity_factory->create_entity(
|
2018-01-31 14:47:59 +00:00
|
|
|
'field',
|
2017-12-04 14:58:19 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish',
|
2018-02-21 17:04:25 +00:00
|
|
|
'field_type' => 'Tainacan\Field_Types\Text',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-12-03 21:49:50 +00:00
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
//$test = $Tainacan_Fields->fetch($field->get_id());
|
2017-12-03 21:49:50 +00:00
|
|
|
|
2017-12-04 14:58:19 +00:00
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'item teste',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'collection' => $collection
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item_metadatas = $Tainacan_Item_Metadata->fetch($i, 'OBJECT');
|
2018-02-16 17:35:24 +00:00
|
|
|
|
|
|
|
$names = [];
|
|
|
|
foreach ($item_metadatas as $item_metadata) {
|
|
|
|
$names[] = $item_metadata->get_field()->get_name();
|
|
|
|
}
|
2017-12-04 14:58:19 +00:00
|
|
|
|
|
|
|
$this->assertTrue(is_array($item_metadatas));
|
2018-02-15 18:29:33 +00:00
|
|
|
|
|
|
|
// notice for repository fields
|
|
|
|
$this->assertEquals(3, sizeof($item_metadatas));
|
2018-02-16 17:35:24 +00:00
|
|
|
//first 2 fields are repository fields
|
|
|
|
$this->assertTrue( in_array('metadado', $names) );
|
2017-12-03 21:49:50 +00:00
|
|
|
|
|
|
|
}
|
2017-11-12 23:14:47 +00:00
|
|
|
}
|