tainacan/tests/test-metadata.php

389 lines
12 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 {
/**
* Test insert a regular metadatum with type
*/
function test_add() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
2017-12-04 18:20:49 +00:00
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'accept_suggestion' => true,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
2017-12-04 18:20:49 +00:00
),
true
);
$test = $Tainacan_Metadata->fetch($metadatum->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());
2018-02-14 00:35:12 +00:00
$this->assertTrue((bool) $test->get_accept_suggestion());
}
2017-11-09 15:51:16 +00:00
/**
* Test insert a regular metadatum with type
2017-11-09 15:51:16 +00:00
*/
2017-11-30 13:44:29 +00:00
function test_add_type(){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
2017-12-04 18:20:49 +00:00
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadado',
'description' => 'descricao',
'collection_id' => $collection->get_id(),
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
2017-12-04 18:20:49 +00:00
),
true
);
2017-11-09 15:51:16 +00:00
$test = $Tainacan_Metadata->fetch($metadatum->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());
2018-06-11 17:57:50 +00:00
$this->assertEquals('Tainacan\Metadata_Types\Text', $test->get_metadata_type());
$this->assertEquals($test->get_metadata_type(), 'Tainacan\Metadata_Types\Text');
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 metadatum are visible for children collection
2017-12-04 18:30:54 +00:00
*/
2017-11-30 13:44:29 +00:00
function test_hierarchy_metadata(){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
2017-12-04 18:20:49 +00:00
$this->tainacan_entity_factory->create_entity(
'metadatum',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadatum default',
'collection_id' => $Tainacan_Metadata->get_default_metadata_attribute(),
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
2017-12-04 18:20:49 +00:00
'status' => 'publish'
),
true
);
$collection_grandfather = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection grandfather'
),
true
);
$this->tainacan_entity_factory->create_entity(
'metadatum',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadatum grandfather',
2017-12-04 18:20:49 +00:00
'collection_id' => $collection_grandfather->get_id(),
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
2017-12-04 18:20:49 +00:00
'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',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadatum father',
2017-12-04 18:20:49 +00:00
'collection_id' => $collection_father->get_id(),
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
2017-12-04 18:20:49 +00:00
'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(
'metadatum',
2017-12-04 18:20:49 +00:00
array(
'name' => 'metadatum son',
2017-12-04 18:20:49 +00:00
'collection_id' => $collection_son->get_id(),
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
2017-12-04 18:20:49 +00:00
'status' => 'publish'
),
true
);
2017-11-30 13:44:29 +00:00
$retrieve_metadata = $Tainacan_Metadata->fetch_by_collection( $collection_son, [], 'OBJECT' );
$retrieve_metadata_ids = $Tainacan_Metadata->fetch_ids_by_collection( $collection_son, [] );
// should return 6
$this->assertEquals( 6, sizeof( $retrieve_metadata ) );
$this->assertEquals( 6, sizeof( $retrieve_metadata_ids ) );
}
2018-02-16 17:50:24 +00:00
/**
* test remove core metadata
2018-02-16 17:50:24 +00:00
*/
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() ) );
}
}
2017-11-30 13:44:29 +00:00
}
2017-12-04 18:30:54 +00:00
/**
* test if the defaults types are registered
*/
2018-06-11 17:57:50 +00:00
function test_metadata_metadata_type(){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
2018-07-14 16:16:05 +00:00
$this->assertEquals( 7, sizeof( $Tainacan_Metadata->fetch_metadata_types() ) );
2017-12-05 10:37:50 +00:00
}
/**
*
*/
function test_ordenation_metadata(){
2018-04-11 14:18:55 +00:00
$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,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum2',
'description' => 'metadatum2',
'collection' => $collection,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum3 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadatum3',
'description' => 'metadatum3',
'collection' => $collection,
2018-06-11 17:57:50 +00:00
'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, [], 'OBJECT' );
$this->assertEquals( 'metadatum2', $metadata_ordinate[0]->get_name() );
$metadata_ordinate_enabled = $Tainacan_Metadata->fetch_by_collection( $update_collection, [ 'include_disabled' => true ], 'OBJECT' );
$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());
}
function test_unique_slugs() {
$x = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'slug' => 'duplicated_slug',
'status' => 'publish',
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$y = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'slug' => 'duplicated_slug',
'status' => 'publish',
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$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',
'slug' => 'duplicated_slug',
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$y = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'teste',
'description' => 'adasdasdsa',
'slug' => 'duplicated_slug',
2018-06-11 17:57:50 +00:00
'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());
}
2018-06-11 17:57:50 +00:00
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);
2018-06-11 17:57:50 +00:00
$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');
2018-06-11 17:57:50 +00:00
$invalidMetadatum->set_metadata_type('Tainacan\Metadata_Types\Relationship');
$invalidMetadatum->set_metadata_type_options(['collection_id' => 'string']);
$this->assertFalse($invalidMetadatum->validate());
}
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
function __construct(){
parent::__construct();
}
/**
* @param $metadatum
2017-12-05 10:37:50 +00:00
* @return string
*/
public function render( $metadatum ){}
}