tainacan/tests/test-item-metadata.php

646 lines
21 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
/**
2021-04-05 16:37:15 +00:00
* Class Item_Metadata
2017-11-12 23:14:47 +00:00
*
* @package Test_Tainacan
*/
/**
2021-04-05 14:57:23 +00:00
* Item_Metadata test case.
2017-11-12 23:14:47 +00:00
*/
class Item_Metadata extends TAINACAN_UnitTestCase {
2017-11-12 23:14:47 +00:00
2021-04-05 17:07:57 +00:00
private $collection = null;
private $item = null;
public function setUp() {
parent::setUp();
$c = $this->tainacan_entity_factory->create_entity('collection', ['name' => 'My Collection'], true);
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'My test item',
'description' => 'item description',
'collection' => $c,
'status' => 'publish'
),
true
);
$this->collection = $c;
$this->item = $i;
}
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
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
2018-04-11 14:18:55 +00:00
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'No description',
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
2017-11-12 23:14:47 +00:00
$test = $Tainacan_Metadata->fetch($metadatum->get_id());
2017-11-12 23:14:47 +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
$item = $Tainacan_Items->fetch($i->get_id());
2017-11-12 23:14:47 +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');
$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
*/
2021-03-29 04:44:06 +00:00
function test_required(){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
2018-04-11 14:18:55 +00:00
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'No description',
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'required' => 'yes',
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
2017-11-12 23:14:47 +00:00
$test = $Tainacan_Metadata->fetch($metadatum->get_id());
2017-11-12 23:14:47 +00:00
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item teste',
2018-10-31 18:33:17 +00:00
'status' => 'publish',
'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
$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());
}
2021-03-29 04:44:06 +00:00
function test_collection_key(){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
2018-04-11 14:18:55 +00:00
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'No description',
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'collection_key' => 'yes',
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
2017-11-12 23:14:47 +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'
),
true
);
$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';
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum);
2017-11-12 23:14:47 +00:00
$item_metadata->set_value($value);
2017-11-12 23:14:47 +00:00
$this->assertTrue($item_metadata->validate());
$item_metadata->validate();
2017-11-12 23:14:47 +00:00
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
$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);
$this->assertTrue($n_item_metadata->validate(), 'trying to validate the same item with same value should be ok');
$n_item_metadata2 = new \Tainacan\Entities\Item_Metadata_Entity($i2, $metadatum);
$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
2021-03-29 04:44:06 +00:00
function test_fetch(){
2018-04-11 14:18:55 +00:00
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'description' => 'No description',
),
true
);
2017-12-04 18:20:49 +00:00
$this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'status' => 'publish',
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
2017-12-03 21:49:50 +00:00
//$test = $Tainacan_Metadata->fetch($metadatum->get_id());
2017-12-03 21:49:50 +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');
$names = [];
foreach ($item_metadatas as $item_metadata) {
$names[] = $item_metadata->get_metadatum()->get_name();
}
$this->assertTrue(is_array($item_metadatas));
// notice for repository metadata
$this->assertEquals(3, sizeof($item_metadatas));
//first 2 metadata are repository metadata
$this->assertTrue( in_array('metadado', $names) );
2017-12-03 21:49:50 +00:00
}
function test_metadata_text_textarea() {
$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>';
$this->assertEquals($item_metadata_text->get_value_as_html(), $response_text);
$this->assertEquals($item_metadata_textarea->get_value_as_html(), $response_textarea);
// Poor HTML entry tests
2021-03-29 20:09:17 +00:00
$malformatted_HTML = "<p> I started my content <div> and make something else here </div> without closing its HTML properly";
2021-03-29 20:09:17 +00:00
$item_metadata_text->set_value($malformatted_HTML);
$item_metadata_textarea->set_value($malformatted_HTML);
2021-03-29 20:09:17 +00:00
$this->assertEquals($item_metadata_text->get_value_as_html(), $malformatted_HTML ."</p>");
$this->assertEquals($item_metadata_textarea->get_value_as_html(), $malformatted_HTML ."</p>");
}
/**
2019-04-12 21:46:54 +00:00
* @group test_item_metadata_has_value
*/
2019-04-12 21:46:54 +00:00
function test_item_metadata_has_value() {
$test_value = 'has_value';
$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($test_value);
2019-04-12 21:46:54 +00:00
$item_metadata_textarea->validate();
$item_metadata_textarea = $Tainacan_Item_Metadata->insert($item_metadata_textarea);
$this->assertTrue($item_metadata_textarea->has_value());
$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([ $value_textarea ]);
$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());
$item_metadata_text->set_value([ $test_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());
}
2021-04-05 14:57:23 +00:00
2021-04-05 17:38:09 +00:00
function test_numeric_metadata_html() {
2021-04-05 16:37:15 +00:00
// Simple numeric metadata
2021-04-05 14:57:23 +00:00
$metadatum_numeric = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Numeric important meta',
'description' => 'and its description',
2021-04-05 17:07:57 +00:00
'collection_id' => $this->collection->get_id(),
2021-04-05 14:57:23 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
'status' => 'publish'
),
true
);
2021-04-05 16:37:15 +00:00
// Multiple numeric metadata
2021-04-05 14:57:23 +00:00
$metadatum_numeric_multiple = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Numeric important meta',
'description' => 'and its description',
2021-04-05 17:07:57 +00:00
'collection_id' => $this->collection->get_id(),
2021-04-05 14:57:23 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
'status' => 'publish',
'multiple' => 'yes'
),
true
);
2021-04-05 17:07:57 +00:00
$item_metadata_numeric = new \Tainacan\Entities\Item_Metadata_Entity($this->item, $metadatum_numeric);
2021-04-05 14:57:23 +00:00
$item_metadata_numeric->set_value(10);
$item_metadata_numeric->validate();
$this->assertEquals($item_metadata_numeric->get_value_as_html(), 10);
2021-04-05 17:07:57 +00:00
$item_metadata_numeric_mult = new \Tainacan\Entities\Item_Metadata_Entity($this->item, $metadatum_numeric_multiple);
2021-04-05 14:57:23 +00:00
$item_metadata_numeric_mult->set_value([10,22,4]);
$item_metadata_numeric_mult->validate();
$this->assertEquals($item_metadata_numeric_mult->get_value_as_html(), '10<span class="multivalue-separator"> | </span>22<span class="multivalue-separator"> | </span>4');
}
2021-04-05 16:37:15 +00:00
2021-04-05 17:38:09 +00:00
function test_date_metadata_html() {
2021-04-05 16:37:15 +00:00
// Simple date metadata
$metadatum_date = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Date important meta',
'description' => 'and its description',
2021-04-05 17:07:57 +00:00
'collection_id' => $this->collection->get_id(),
2021-04-05 16:37:15 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Date',
'status' => 'publish'
),
true
);
// Multiple date metadata
$metadatum_date_multiple = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Date meta',
'description' => 'and its description',
2021-04-05 17:07:57 +00:00
'collection_id' => $this->collection->get_id(),
2021-04-05 16:37:15 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Date',
'status' => 'publish',
'multiple' => 'yes'
),
true
);
2021-04-05 17:07:57 +00:00
$item_metadata_date = new \Tainacan\Entities\Item_Metadata_Entity($this->item, $metadatum_date);
2021-04-05 16:37:15 +00:00
// Invalid date value
$item_metadata_date->set_value(10);
$item_metadata_date->validate();
$this->assertFalse($item_metadata_date->get_value_as_html());
$item_metadata_date->set_value("2021-04-05");
$item_metadata_date->validate();
$this->assertEquals($item_metadata_date->get_value_as_html(), "April 5, 2021");
2021-04-05 17:07:57 +00:00
$item_metadata_date_mult = new \Tainacan\Entities\Item_Metadata_Entity($this->item, $metadatum_date_multiple);
2021-04-05 16:37:15 +00:00
// Invalid date values
$item_metadata_date_mult->set_value([10,22,4]);
$item_metadata_date_mult->validate();
$this->assertEquals($item_metadata_date_mult->get_value_as_html(), '<span class="multivalue-separator"> | </span><span class="multivalue-separator"> | </span>');
$item_metadata_date_mult->set_value(["2021-04-05", "2021-12-30"]);
$item_metadata_date_mult->validate();
$this->assertEquals($item_metadata_date_mult->get_value_as_html(), 'April 5, 2021<span class="multivalue-separator"> | </span>December 30, 2021');
}
2021-04-05 17:38:09 +00:00
function test_user_metadata_html() {
$user_metadata = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'User metadata',
'description' => 'and its description',
'collection_id' => $this->collection->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\User',
'status' => 'publish'
),
true
);
$user_meta = new \Tainacan\Entities\Item_Metadata_Entity($this->item, $user_metadata);
// Empty val
$this->assertEmpty($user_meta->get_value_as_html());
$new_user = $this->factory()->user->create(array( 'role' => 'subscriber', 'display_name' => 'User Name' ));
$user_meta->set_value($new_user);
$user_meta->validate();
$this->assertEquals($user_meta->get_value_as_html(), "User Name");
$user_metadata_multiple = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'User metadata',
'description' => 'and its description',
'collection_id' => $this->collection->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\User',
'status' => 'publish',
'multiple' => 'yes'
),
true
);
$user_meta_multi = new \Tainacan\Entities\Item_Metadata_Entity($this->item, $user_metadata_multiple);
$this->assertEmpty($user_meta_multi->get_value_as_html());
$sec_user = $this->factory()->user->create(array( 'role' => 'subscriber', 'display_name' => 'User Name 2' ));
$user_meta_multi->set_value([$new_user, $sec_user]);
$user_meta_multi->validate();
$this->assertEquals($user_meta_multi->get_value_as_html(), 'User Name<span class="multivalue-separator"> | </span>User Name 2');
}
function test_selectbox_metadata_html() {
$selectbox_metadata = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'My selectbox meta',
'status' => 'publish',
'collection' => $this->collection,
'metadata_type' => 'Tainacan\Metadata_Types\Selectbox',
'metadata_type_options' => [
'options' => ['tainacan', 'wordpress', 'php']
]
),
true
);
$selectbox_metadata_multiple = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'My selectbox meta',
'status' => 'publish',
'collection' => $this->collection,
'metadata_type' => 'Tainacan\Metadata_Types\Selectbox',
'multiple' => 'yes',
'metadata_type_options' => [
'options' => ['tainacan', 'wordpress', 'php']
]
),
true
);
$sb_meta = new \Tainacan\Entities\Item_Metadata_Entity($this->item, $selectbox_metadata);
$sb_meta->set_value('tainacan');
$sb_meta->validate();
$this->assertEquals($sb_meta->get_value_as_html(), 'tainacan');
$sb_meta->set_value('php');
$this->assertEquals($sb_meta->get_value_as_html(), 'php');
$sb_meta_multi = new \Tainacan\Entities\Item_Metadata_Entity($this->item, $selectbox_metadata_multiple);
$sb_meta_multi->set_value(['tainacan', 'wordpress']);
$sb_meta_multi->validate();
$this->assertEquals($sb_meta_multi->get_value_as_html(), 'tainacan<span class="multivalue-separator"> | </span>wordpress');
}
2021-04-06 05:04:43 +00:00
function test_relationship_metadata_html() {
$referenced_collection = $this->tainacan_entity_factory->create_entity(
'collection',
['name' => 'INXS Songs'],
true
);
$mystify = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'Mystify',
'description' => '"Mystify" is the 9th track of INXS 6th album "Kick".',
'collection' => $referenced_collection,
'status' => 'publish'
),
true
);
$expected_return = $this->relationship_expected_return($mystify->get_id(), $mystify->get_title());
$relationship_metadata = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Relationship meta',
'description' => 'My desc',
'collection' => $this->collection,
'metadata_type' => 'Tainacan\Metadata_Types\Relationship',
'status' => 'publish',
'metadata_type_options' => [
'collection_id' => $referenced_collection->get_id(),
'search' => $referenced_collection->get_core_title_metadatum()->get_id()
]
),
true
);
$rel_meta = new \Tainacan\Entities\Item_Metadata_Entity($this->item, $relationship_metadata);
$rel_meta->validate();
$this->assertEquals($rel_meta->get_value_as_html(), '');
$rel_meta->set_value($mystify->get_id());
$rel_meta->validate();
$this->assertEquals($rel_meta->get_value_as_html(), $expected_return);
$rel_meta->set_value([$this->collection->get_id()]);
$rel_meta->validate();
$this->assertEquals($rel_meta->get_value_as_html(), '');
}
private function relationship_expected_return($id, $title) {
$URL = get_permalink($id);
return "<a data-linkto='item' data-id='${id}' href='${URL}'>${title}</a>";
}
2017-11-12 23:14:47 +00:00
}