add helper to get related taxonomy for tax metadata #253

This commit is contained in:
Leo Germani 2019-07-30 18:25:46 -03:00
parent 9b95b346ff
commit a4fe281ed6
2 changed files with 66 additions and 0 deletions

View File

@ -275,4 +275,23 @@ class Taxonomy extends Metadata_Type {
}
/**
* Get related taxonomy object
* @return \Tainacan\Entities\Taxonomy|false The Taxonomy object or false
*/
public function get_taxonomy() {
$taxonomy_id = $this->get_option('taxonomy_id');
if ( is_numeric($taxonomy_id) ) {
$taxonomy = \Tainacan\Repositories\Taxonomies::get_instance()->fetch( (int) $taxonomy_id );
if ( $taxonomy instanceof \Tainacan\Entities\Taxonomy ) {
return $taxonomy;
}
}
return false;
}
}

View File

@ -652,5 +652,52 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
}
function test_get_taxonomy_method() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
),
true
);
$tax = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'tax_test',
'collections' => [$collection],
'status' => 'publish'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'meta',
'description' => 'description',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'status' => 'publish',
'metadata_type_options' => [
'taxonomy_id' => $tax->get_id(),
'allow_new_terms' => 'no'
]
),
true
);
$object = $metadatum->get_metadata_type_object();
$taxCheck = $object->get_taxonomy();
$this->assertTrue( $taxCheck instanceof \Tainacan\Entities\Taxonomy );
$this->assertEquals($tax->get_id(), $taxCheck->get_id());
}
}