add helper to get related collection for relationship metadata #253

This commit is contained in:
Leo Germani 2019-07-30 18:37:27 -03:00
parent a4fe281ed6
commit 3f26f79b44
2 changed files with 63 additions and 0 deletions

View File

@ -174,4 +174,23 @@ class Relationship extends Metadata_Type {
}
/**
* Get related Collection object
* @return \Tainacan\Entities\Collection|false The Collection object or false
*/
public function get_collection() {
$collection_id = $this->get_option('collection_id');
if ( is_numeric($collection_id) ) {
$collection = \Tainacan\Repositories\Collections::get_instance()->fetch( (int) $collection_id );
if ( $collection instanceof \Tainacan\Entities\Collection ) {
return $collection;
}
}
return false;
}
}

View File

@ -425,6 +425,50 @@ class Metadata extends TAINACAN_UnitTestCase {
$this->assertEquals($metadatum->get_id(), $fetch[0]->get_id());
}
function test_relatioship_get_collection_method() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste2'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'meta',
'description' => 'description',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Relationship',
'status' => 'publish',
'metadata_type_options' => [
'collection_id' => $collection2->get_id(),
'search' => [$collection2->get_core_title_metadatum()->get_id()]
]
),
true
);
$object = $metadatum->get_metadata_type_object();
$colCheck = $object->get_collection();
$this->assertTrue( $colCheck instanceof \Tainacan\Entities\Collection );
$this->assertEquals($collection2->get_id(), $colCheck->get_id());
}
}
/**