adds html tests to relationship meta

This commit is contained in:
Rodrigo de Oliveira 2021-04-06 02:04:43 -03:00
parent 5e4dd01e08
commit 2132505095
3 changed files with 56 additions and 6 deletions

View File

@ -31,7 +31,6 @@ class Item_Metadata_Entity extends Entity {
* @param int $meta_id ID for a specific meta row
*/
function __construct(Item $item = null, Metadatum $metadatum = null, $meta_id = null, $parent_meta_id = null) {
$this->set_item($item);
$this->set_metadatum($metadatum);

View File

@ -157,7 +157,7 @@ class Relationship extends Metadata_Type {
* @param Item_Metadata_Entity $item_metadata
* @return string The HTML representation of the value, containing one or multiple items names, linked to the item page
*/
public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$return = '';
@ -170,11 +170,10 @@ class Relationship extends Metadata_Type {
foreach ( $value as $item_id ) {
try {
//$item = new \Tainacan\Entities\Item($item_id);
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$item = $Tainacan_Items->fetch( (int) $item_id);
$count ++;
$count++;
if ( $item instanceof \Tainacan\Entities\Item ) {
$return .= $prefix;
@ -196,12 +195,11 @@ class Relationship extends Metadata_Type {
if ( $item instanceof \Tainacan\Entities\Item ) {
$return .= $this->get_item_html($item);
}
} catch (\Exception $e) {
// item not found
}
}
return $return;
}

View File

@ -590,4 +590,57 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
$sb_meta_multi->validate();
$this->assertEquals($sb_meta_multi->get_value_as_html(), 'tainacan<span class="multivalue-separator"> | </span>wordpress');
}
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>";
}
}