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-03-21 12:20:00 +00:00
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
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-06-11 15:10:07 +00:00
|
|
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
2017-12-04 14:58:19 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
2018-06-11 17:57:50 +00:00
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
$test = $Tainacan_Metadata->fetch($metadatum->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
|
|
|
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
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');
|
2018-04-09 14:32:20 +00:00
|
|
|
|
|
|
|
$item_metadata->validate();
|
|
|
|
|
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-06-11 15:10:07 +00:00
|
|
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
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-06-11 15:10:07 +00:00
|
|
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
2017-12-04 14:58:19 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
|
|
|
'required' => 'yes',
|
2018-06-11 17:57:50 +00:00
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-12 23:14:47 +00:00
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
$test = $Tainacan_Metadata->fetch($metadatum->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',
|
2018-10-31 18:33:17 +00:00
|
|
|
'status' => 'publish',
|
2017-12-04 14:58:19 +00:00
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'collection' => $collection
|
|
|
|
),
|
|
|
|
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-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-06-11 15:10:07 +00:00
|
|
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
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-06-11 15:10:07 +00:00
|
|
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
2017-12-04 14:58:19 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
|
|
|
'collection_key' => 'yes',
|
2018-06-11 17:57:50 +00:00
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
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',
|
2018-01-29 13:09:44 +00:00
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2018-04-22 21:39:54 +00:00
|
|
|
|
|
|
|
$i2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'other item',
|
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
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-11-12 23:14:47 +00:00
|
|
|
|
|
|
|
$value = 'teste_val';
|
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum);
|
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);
|
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
$n_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum);
|
2017-11-12 23:14:47 +00:00
|
|
|
$n_item_metadata->set_value($value);
|
2018-04-22 21:39:54 +00:00
|
|
|
$this->assertTrue($n_item_metadata->validate(), 'trying to validate the same item with same value should be ok');
|
2017-12-04 14:58:19 +00:00
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
$n_item_metadata2 = new \Tainacan\Entities\Item_Metadata_Entity($i2, $metadatum);
|
2018-04-22 21:39:54 +00:00
|
|
|
$n_item_metadata2->set_value($value);
|
|
|
|
$this->assertFalse($n_item_metadata2->validate(), 'Collection key should not validate another item metadatada with the same value');
|
2017-11-12 23:14:47 +00:00
|
|
|
}
|
2017-12-03 21:49:50 +00:00
|
|
|
|
|
|
|
function teste_fetch(){
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2017-12-04 18:20:49 +00:00
|
|
|
$this->tainacan_entity_factory->create_entity(
|
2018-06-11 15:10:07 +00:00
|
|
|
'metadatum',
|
2017-12-04 14:58:19 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish',
|
2018-06-11 17:57:50 +00:00
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
2017-12-04 14:58:19 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-12-03 21:49:50 +00:00
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
//$test = $Tainacan_Metadata->fetch($metadatum->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) {
|
2018-06-11 15:10:07 +00:00
|
|
|
$names[] = $item_metadata->get_metadatum()->get_name();
|
2018-02-16 17:35:24 +00:00
|
|
|
}
|
2017-12-04 14:58:19 +00:00
|
|
|
|
|
|
|
$this->assertTrue(is_array($item_metadatas));
|
2018-02-15 18:29:33 +00:00
|
|
|
|
2018-06-11 15:10:07 +00:00
|
|
|
// notice for repository metadata
|
2020-04-17 12:29:11 +00:00
|
|
|
$this->assertEquals(3, sizeof($item_metadatas));
|
2018-06-11 15:10:07 +00:00
|
|
|
//first 2 metadata are repository metadata
|
2018-02-16 17:35:24 +00:00
|
|
|
$this->assertTrue( in_array('metadado', $names) );
|
2017-12-03 21:49:50 +00:00
|
|
|
|
2018-09-13 20:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_metadata_text_textarea() {
|
|
|
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'teste'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'item teste',
|
|
|
|
'description' => 'description',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$metadatum_text = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'metadadoText',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection_id' => $collection->get_id(),
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$metadatum_textarea = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'metadadoTextarea',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection_id' => $collection->get_id(),
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Textarea',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$value_text = 'GOOGLE: www.google.com';
|
|
|
|
$item_metadata_text = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_text);
|
|
|
|
$item_metadata_text->set_value($value_text);
|
|
|
|
|
|
|
|
$value_textarea = 'GOOGLE: www.google.com \n GOOGLE: https://www.google.com';
|
|
|
|
$item_metadata_textarea = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_textarea);
|
|
|
|
$item_metadata_textarea->set_value($value_textarea);
|
|
|
|
|
2019-06-18 20:52:42 +00:00
|
|
|
$response_text = 'GOOGLE: <a href="http://www.google.com" target="_blank" title="www.google.com">www.google.com</a>';
|
|
|
|
$response_textarea = 'GOOGLE: <a href="http://www.google.com" target="_blank" title="www.google.com">www.google.com</a> \n GOOGLE: <a href="https://www.google.com" target="_blank" title="https://www.google.com">https://www.google.com</a>';
|
2018-09-13 20:07:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals($item_metadata_text->get_value_as_html(), $response_text);
|
|
|
|
$this->assertEquals($item_metadata_textarea->get_value_as_html(), $response_textarea);
|
|
|
|
}
|
2019-04-12 19:32:33 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-12 21:46:54 +00:00
|
|
|
* @group test_item_metadata_has_value
|
2019-04-12 19:32:33 +00:00
|
|
|
*/
|
2019-04-12 21:46:54 +00:00
|
|
|
function test_item_metadata_has_value() {
|
2019-04-12 19:32:33 +00:00
|
|
|
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'teste'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$i = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'item teste',
|
|
|
|
'description' => 'description',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2019-04-12 21:46:54 +00:00
|
|
|
|
|
|
|
$metadatum_textarea = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'metadadoTextarea',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection_id' => $collection->get_id(),
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Textarea',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$value_textarea = '';
|
|
|
|
$item_metadata_textarea = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_textarea);
|
|
|
|
$item_metadata_textarea->set_value($value_textarea);
|
|
|
|
|
|
|
|
$item_metadata_textarea->validate();
|
|
|
|
$item_metadata_textarea = $Tainacan_Item_Metadata->insert($item_metadata_textarea);
|
|
|
|
|
|
|
|
$this->assertFalse($item_metadata_textarea->has_value());
|
|
|
|
|
|
|
|
$item_metadata_textarea->set_value('has_value');
|
|
|
|
$item_metadata_textarea->validate();
|
|
|
|
$item_metadata_textarea = $Tainacan_Item_Metadata->insert($item_metadata_textarea);
|
|
|
|
|
|
|
|
$this->assertTrue($item_metadata_textarea->has_value());
|
|
|
|
|
2019-04-12 19:32:33 +00:00
|
|
|
$metadatum_text = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'metadadoText',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection_id' => $collection->get_id(),
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
'multiple' => 'yes'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item_metadata_text = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_text);
|
|
|
|
|
|
|
|
$item_metadata_text->set_value([
|
|
|
|
''
|
|
|
|
]);
|
|
|
|
|
|
|
|
$item_metadata_text->validate();
|
|
|
|
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata_text);
|
2019-04-12 21:46:54 +00:00
|
|
|
|
|
|
|
$this->assertFalse($item_metadata->has_value());
|
|
|
|
|
2019-04-16 18:15:44 +00:00
|
|
|
$item_metadata_text->set_value([
|
|
|
|
'has_value'
|
2019-04-12 21:46:54 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$item_metadata_text->validate();
|
|
|
|
$item_metadata_text = $Tainacan_Item_Metadata->insert($item_metadata_text);
|
|
|
|
|
|
|
|
$this->assertTrue($item_metadata_text->has_value());
|
2019-04-12 19:32:33 +00:00
|
|
|
}
|
2017-11-12 23:14:47 +00:00
|
|
|
}
|