tainacan/tests/test-metadata.php

203 lines
5.4 KiB
PHP
Raw Normal View History

<?php
2017-11-15 18:50:11 +00:00
namespace Tainacan\Tests;
2017-12-05 10:37:50 +00:00
use Tainacan\Field_Types;
/**
2017-11-30 13:44:29 +00:00
* Class Metadata
*
* @package Test_Tainacan
*/
/**
2017-11-30 13:44:29 +00:00
* Metadata test case.
*/
2017-12-04 18:20:49 +00:00
class Metadata extends TAINACAN_UnitTestCase {
/**
2017-11-30 13:44:29 +00:00
* Test insert a regular metadata with type
*/
function test_add() {
2017-12-04 18:20:49 +00:00
global $Tainacan_Metadatas;
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$type = $this->tainacan_field_factory->create_field('text');
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'field_type_object' => $type
),
true
);
$test = $Tainacan_Metadatas->fetch($metadata->get_id());
2017-11-09 15:51:16 +00:00
$this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_description(), 'descricao');
2017-11-12 23:14:47 +00:00
$this->assertEquals($test->get_collection_id(), $collection->get_id());
}
2017-11-09 15:51:16 +00:00
/**
2017-11-30 13:44:29 +00:00
* Test insert a regular metadata with type
2017-11-09 15:51:16 +00:00
*/
2017-11-30 13:44:29 +00:00
function test_add_type(){
2017-12-04 18:20:49 +00:00
global $Tainacan_Metadatas;
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$type = $this->tainacan_field_factory->create_field('text');
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection_id' => $collection->get_id(),
'field_type_object' => $type
),
true
);
2017-11-09 15:51:16 +00:00
$test = $Tainacan_Metadatas->fetch($metadata->get_id());
2017-11-09 15:51:16 +00:00
$this->assertEquals($test->get_name(), 'metadado');
2017-11-12 23:14:47 +00:00
$this->assertEquals($test->get_collection_id(), $collection->get_id());
2017-11-20 14:37:01 +00:00
$this->assertEquals('Tainacan\Field_Types\Text', $test->get_field_type());
2017-11-14 18:44:04 +00:00
$this->assertEquals($test->get_field_type_object(), $type);
2017-11-09 15:51:16 +00:00
}
2017-11-30 13:44:29 +00:00
2017-12-04 18:30:54 +00:00
/**
* test if parent metadata are visible for children collection
*/
2017-11-30 13:44:29 +00:00
function test_hierarchy_metadata(){
2017-12-04 18:20:49 +00:00
global $Tainacan_Metadatas;
$type = $this->tainacan_field_factory->create_field('text');
$this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadata default',
'collection_id' => $Tainacan_Metadatas->get_default_metadata_attribute(),
'field_type_object' => $type,
'status' => 'publish'
),
true
);
$collection_grandfather = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection grandfather'
),
true
);
$this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadata grandfather',
'collection_id' => $collection_grandfather->get_id(),
'field_type_object' => $type,
'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(
'metadata',
array(
'name' => 'metadata father',
'collection_id' => $collection_father->get_id(),
'field_type_object' => $type,
'status' => 'publish'
),
true
);
$collection_son = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection son',
'parent' => $collection_father->get_id()
),
true
);
2017-11-30 13:44:29 +00:00
$this->assertEquals( $collection_grandfather->get_id(), $collection_father->get_parent() );
$this->assertEquals( $collection_father->get_id(), $collection_son->get_parent() );
2017-12-04 18:20:49 +00:00
$this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadata son',
'collection_id' => $collection_son->get_id(),
'field_type_object' => $type,
'status' => 'publish'
),
true
);
2017-11-30 13:44:29 +00:00
$retrieve_metadata = $Tainacan_Metadatas->fetch_by_collection( $collection_son, [], 'OBJECT' );
$this->assertEquals( 4, sizeof( $retrieve_metadata ) );
}
2017-12-04 18:30:54 +00:00
/**
* test if the defaults types are registered
*/
function test_metadata_field_type(){
global $Tainacan_Metadatas;
$this->assertEquals( 8, sizeof( $Tainacan_Metadatas->fetch_field_types() ) );
}
/**
* test if the defaults types are registered
*/
2017-12-05 10:37:50 +00:00
function test_metadata_field_type_insert(){
global $Tainacan_Metadatas;
$class = new RandomType;
$this->assertEquals( 9, sizeof( $Tainacan_Metadatas->fetch_field_types() ) );
}
}
/**
* Class TainacanFieldType
*/
class RandomType extends Field_Types\Field_Type {
function __construct(){
parent::__construct();
}
/**
* @param $metadata
* @return string
*/
public function render( $metadata ){}
}