tainacan/tests/test-metadata.php

630 lines
16 KiB
PHP
Raw Normal View History

<?php
2017-11-15 18:50:11 +00:00
namespace Tainacan\Tests;
2018-06-11 17:57:50 +00:00
use Tainacan\Metadata_Types;
/**
* Class Metadatum
*
* @package Test_Tainacan
*/
/**
* Metadatum test case.
* @group metadata
*/
class Metadata extends TAINACAN_UnitTestCase {
2020-02-19 18:46:18 +00:00
/**
* Test insert a regular metadatum with type
*/
function test_add() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'accept_suggestion' => true,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$test = $Tainacan_Metadata->fetch($metadatum->get_id());
$this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_description(), 'descricao');
$this->assertEquals($test->get_collection_id(), $collection->get_id());
$this->assertTrue((bool) $test->get_accept_suggestion());
}
/**
* Test insert a regular metadatum with type
*/
function test_add_type(){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection_id' => $collection->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$test = $Tainacan_Metadata->fetch($metadatum->get_id());
$this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_collection_id(), $collection->get_id());
$this->assertEquals('Tainacan\Metadata_Types\Text', $test->get_metadata_type());
$this->assertEquals($test->get_metadata_type(), 'Tainacan\Metadata_Types\Text');
}
/**
* test if parent metadatum are visible for children collection
*/
function test_hierarchy_metadata(){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum default',
'collection_id' => $Tainacan_Metadata->get_default_metadata_attribute(),
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$collection_grandfather = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection grandfather'
),
true
);
$this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum grandfather',
'collection_id' => $collection_grandfather->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$collection_father = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection father',
'parent' => $collection_grandfather->get_id()
),
true
);
$this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum father',
'collection_id' => $collection_father->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$collection_son = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection son',
'parent' => $collection_father->get_id()
),
true
);
$this->assertEquals( $collection_grandfather->get_id(), $collection_father->get_parent() );
$this->assertEquals( $collection_father->get_id(), $collection_son->get_parent() );
$this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum son',
'collection_id' => $collection_son->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$retrieve_metadata = $Tainacan_Metadata->fetch_by_collection( $collection_son );
$retrieve_metadata_ids = $Tainacan_Metadata->fetch_ids_by_collection( $collection_son, [] );
2020-02-19 18:46:18 +00:00
// should return 6
$this->assertEquals( 6, sizeof( $retrieve_metadata ) );
$this->assertEquals( 6, sizeof( $retrieve_metadata_ids ) );
2020-02-19 18:46:18 +00:00
}
/**
* test remove core metadata
*/
function test_core_metadata(){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection_grandfather = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection metadatum'
),
true
);
$core_metadata = $Tainacan_Metadata->get_core_metadata( $collection_grandfather );
if( $core_metadata ){
foreach( $core_metadata as $core_metadatum ){
$this->assertFalse(wp_trash_post( $core_metadatum->get_id() ) );
}
}
}
/**
* test if the defaults types are registered
*/
function test_metadata_metadata_type(){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
2022-12-02 19:24:14 +00:00
$this->assertEquals( 10, sizeof( $Tainacan_Metadata->fetch_metadata_types() ) );
2020-02-19 18:46:18 +00:00
}
/**
*
*/
function test_ordenation_metadata(){
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$metadatum1 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum1',
'description' => 'descricao',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum2',
'description' => 'metadatum2',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum3 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum3',
'description' => 'metadatum3',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$collection->set_metadata_order(
[
array( 'id' => $metadatum3->get_id(), 'enabled' => false ),
array( 'id' => $metadatum2->get_id(), 'enabled' => true ),
array( 'id' => $metadatum1->get_id(), 'enabled' => true )
]);
$update_collection = $Tainacan_Collections->update( $collection );
$metadata_ordinate = $Tainacan_Metadata->fetch_by_collection( $update_collection );
$this->assertEquals( 'metadatum2', $metadata_ordinate[0]->get_name() );
$metadata_ordinate_enabled = $Tainacan_Metadata->fetch_by_collection( $update_collection, [ 'include_disabled' => true ] );
$this->assertEquals( 'metadatum3', $metadata_ordinate_enabled[0]->get_name() );
$this->assertFalse($metadata_ordinate_enabled[0]->get_enabled_for_collection());
$this->assertTrue($metadata_ordinate_enabled[1]->get_enabled_for_collection());
$this->assertTrue($metadata_ordinate_enabled[2]->get_enabled_for_collection());
2020-02-19 18:46:18 +00:00
}
function test_unique_slugs() {
$x = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
2020-02-19 18:46:18 +00:00
'slug' => 'duplicated_slug',
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
2020-02-19 18:46:18 +00:00
$y = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
2020-02-19 18:46:18 +00:00
'slug' => 'duplicated_slug',
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
2020-02-19 18:46:18 +00:00
$this->assertNotEquals($x->get_slug(), $y->get_slug());
// Create as draft and publish later
$x = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
2020-02-19 18:46:18 +00:00
'slug' => 'duplicated_slug',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
2020-02-19 18:46:18 +00:00
$y = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
2020-02-19 18:46:18 +00:00
'slug' => 'duplicated_slug',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$this->assertEquals($x->get_slug(), $y->get_slug());
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$x->set_status('publish');
$x->validate();
$x = $Tainacan_Metadata->insert($x);
$y->set_status('private'); // or publish shoud behave the same
$y->validate();
$y = $Tainacan_Metadata->insert($y);
$this->assertNotEquals($x->get_slug(), $y->get_slug());
}
function test_validation_of_metadata_types() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$validMetadatum = new \Tainacan\Entities\Metadatum();
$validMetadatum->set_name('test');
$validMetadatum->set_description('test');
$validMetadatum->set_collection($collection);
$validMetadatum->set_metadata_type('Tainacan\Metadata_Types\Relationship');
$validMetadatum->set_metadata_type_options(['collection_id' => 12]);
$this->assertTrue($validMetadatum->validate());
$invalidMetadatum = new \Tainacan\Entities\Metadatum();
$invalidMetadatum->set_name('test');
$invalidMetadatum->set_description('test');
$invalidMetadatum->set_collection($collection);
$invalidMetadatum->set_status('publish');
$invalidMetadatum->set_metadata_type('Tainacan\Metadata_Types\Relationship');
$invalidMetadatum->set_metadata_type_options(['collection_id' => 'string']);
$this->assertFalse($invalidMetadatum->validate());
}
function test_metadata_type_option_index() {
2020-02-19 18:46:18 +00:00
$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
);
2020-02-19 18:46:18 +00:00
$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'
]
2020-02-19 18:46:18 +00:00
),
true
);
$test_meta = get_post_meta($metadatum->get_id(), '_option_taxonomy_id', true);
$this->assertEquals( $tax->get_id(), $test_meta );
$fetch = $Tainacan_Metadata->fetch([
'meta_query' => [
[
'key' => '_option_taxonomy_id',
'value' => $tax->get_id()
]
]
], 'OBJECT');
$this->assertEquals(1, count($fetch));
$this->assertEquals($metadatum->get_id(), $fetch[0]->get_id());
2020-02-19 18:39:27 +00:00
}
2021-04-06 04:21:22 +00:00
function test_relationship_get_collection_method() {
$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
);
2021-04-06 04:21:22 +00:00
$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());
}
2020-02-19 18:39:27 +00:00
2021-04-06 04:21:22 +00:00
function test_update_taxonomy_cascate_status() {
2020-02-19 18:39:27 +00:00
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$another_collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_another',
'status' => 'publish'
),
true
);
$tax_public = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'tax_public',
'collections' => [$collection, $another_collection],
'status' => 'publish'
),
true
);
$tax = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'tax_private',
'collections' => [$collection, $another_collection],
'status' => 'publish'
),
true
);
$metadatum1 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'tax_public',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'metadata_type_options' => [
'taxonomy_id' => $tax_public->get_id(),
]
),
true
);
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'tax_public',
'status' => 'publish',
'collection' => $another_collection,
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'metadata_type_options' => [
'taxonomy_id' => $tax_public->get_id(),
]
),
true
);
$metadatum3 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'tax',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'metadata_type_options' => [
'taxonomy_id' => $tax->get_id(),
]
),
true
);
$metadatum4 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'tax',
'status' => 'publish',
'collection' => $another_collection,
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'metadata_type_options' => [
'taxonomy_id' => $tax->get_id(),
]
),
true
);
$metadatum1 = $Tainacan_Metadata->fetch($metadatum1->get_id());
$metadatum2 = $Tainacan_Metadata->fetch($metadatum2->get_id());
$metadatum3 = $Tainacan_Metadata->fetch($metadatum3->get_id());
$metadatum4 = $Tainacan_Metadata->fetch($metadatum4->get_id());
$this->assertEquals($metadatum1->get_status(), 'publish');
$this->assertEquals($metadatum2->get_status(), 'publish');
$this->assertEquals($metadatum3->get_status(), 'publish');
$this->assertEquals($metadatum4->get_status(), 'publish');
$tax->set_status('private');
$tax->validate();
$tax = $Tainacan_Taxonomies->update($tax);
$metadatum1 = $Tainacan_Metadata->fetch($metadatum1->get_id());
$metadatum2 = $Tainacan_Metadata->fetch($metadatum2->get_id());
$metadatum3 = $Tainacan_Metadata->fetch($metadatum3->get_id());
$metadatum4 = $Tainacan_Metadata->fetch($metadatum4->get_id());
$this->assertEquals($metadatum1->get_status(), 'publish');
$this->assertEquals($metadatum2->get_status(), 'publish');
$this->assertEquals($metadatum3->get_status(), 'private');
$this->assertEquals($metadatum4->get_status(), 'private');
$tax_draft = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => '',
'collections' => [],
'status' => 'auto-draft'
),
true
);
$metadatum1 = $Tainacan_Metadata->fetch($metadatum1->get_id());
$metadatum2 = $Tainacan_Metadata->fetch($metadatum2->get_id());
$metadatum3 = $Tainacan_Metadata->fetch($metadatum3->get_id());
$metadatum4 = $Tainacan_Metadata->fetch($metadatum4->get_id());
$this->assertEquals($metadatum1->get_status(), 'publish');
$this->assertEquals($metadatum2->get_status(), 'publish');
$this->assertEquals($metadatum3->get_status(), 'private');
$this->assertEquals($metadatum4->get_status(), 'private');
}
2017-12-05 10:37:50 +00:00
}
/**
* Class TainacanMetadatumType
2017-12-05 10:37:50 +00:00
*/
2018-06-11 17:57:50 +00:00
class RandomType extends Metadata_Types\Metadata_Type {
2017-12-05 10:37:50 +00:00
2020-02-19 18:46:18 +00:00
function __construct(){
parent::__construct();
}
2017-12-05 10:37:50 +00:00
2020-02-19 18:46:18 +00:00
/**
* @param $metadatum
* @return string
*/
2017-12-05 10:37:50 +00:00
2020-02-19 18:46:18 +00:00
public function render( $metadatum ){}
}