2017-11-09 11:01:11 +00:00
|
|
|
<?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-09 11:01:11 +00:00
|
|
|
/**
|
2018-01-31 14:47:59 +00:00
|
|
|
* Class Field
|
2017-11-09 11:01:11 +00:00
|
|
|
*
|
|
|
|
* @package Test_Tainacan
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-01-31 14:47:59 +00:00
|
|
|
* Field test case.
|
2017-11-09 11:01:11 +00:00
|
|
|
*/
|
2018-01-31 14:47:59 +00:00
|
|
|
class Fields extends TAINACAN_UnitTestCase {
|
2017-11-09 11:01:11 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-31 14:47:59 +00:00
|
|
|
* Test insert a regular field with type
|
2017-11-09 11:01:11 +00:00
|
|
|
*/
|
|
|
|
function test_add() {
|
2018-01-31 14:47:59 +00:00
|
|
|
global $Tainacan_Fields;
|
2017-12-04 18:20:49 +00:00
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'teste'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$type = $this->tainacan_field_factory->create_field('text');
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2017-12-04 18:20:49 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
2017-12-05 17:42:15 +00:00
|
|
|
'field_type' => $type
|
2017-12-04 18:20:49 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-09 11:01:11 +00:00
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$test = $Tainacan_Fields->fetch($field->get_id());
|
2017-11-09 11:01:11 +00:00
|
|
|
|
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 11:01:11 +00:00
|
|
|
}
|
2017-11-09 15:51:16 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-31 14:47:59 +00:00
|
|
|
* Test insert a regular field with type
|
2017-11-09 15:51:16 +00:00
|
|
|
*/
|
2017-11-30 13:44:29 +00:00
|
|
|
function test_add_type(){
|
2018-01-31 14:47:59 +00:00
|
|
|
global $Tainacan_Fields;
|
2017-12-04 18:20:49 +00:00
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'teste'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$type = $this->tainacan_field_factory->create_field('text');
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2017-12-04 18:20:49 +00:00
|
|
|
array(
|
|
|
|
'name' => 'metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection_id' => $collection->get_id(),
|
2017-12-05 17:42:15 +00:00
|
|
|
'field_type' => $type
|
2017-12-04 18:20:49 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-09 15:51:16 +00:00
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$test = $Tainacan_Fields->fetch($field->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
|
|
|
/**
|
2018-01-31 14:47:59 +00:00
|
|
|
* test if parent field 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(){
|
2018-01-31 14:47:59 +00:00
|
|
|
global $Tainacan_Fields;
|
2017-12-04 18:20:49 +00:00
|
|
|
|
|
|
|
$type = $this->tainacan_field_factory->create_field('text');
|
|
|
|
|
|
|
|
$this->tainacan_entity_factory->create_entity(
|
2018-01-31 14:47:59 +00:00
|
|
|
'field',
|
2017-12-04 18:20:49 +00:00
|
|
|
array(
|
2018-01-31 14:47:59 +00:00
|
|
|
'name' => 'field default',
|
|
|
|
'collection_id' => $Tainacan_Fields->get_default_metadata_attribute(),
|
2017-12-05 17:42:15 +00:00
|
|
|
'field_type' => $type,
|
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(
|
2018-01-31 14:47:59 +00:00
|
|
|
'field',
|
2017-12-04 18:20:49 +00:00
|
|
|
array(
|
2018-01-31 14:47:59 +00:00
|
|
|
'name' => 'field grandfather',
|
2017-12-04 18:20:49 +00:00
|
|
|
'collection_id' => $collection_grandfather->get_id(),
|
2017-12-05 17:42:15 +00:00
|
|
|
'field_type' => $type,
|
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(
|
2018-01-31 14:47:59 +00:00
|
|
|
'field',
|
2017-12-04 18:20:49 +00:00
|
|
|
array(
|
2018-01-31 14:47:59 +00:00
|
|
|
'name' => 'field father',
|
2017-12-04 18:20:49 +00:00
|
|
|
'collection_id' => $collection_father->get_id(),
|
2017-12-05 17:42:15 +00:00
|
|
|
'field_type' => $type,
|
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(
|
2018-01-31 14:47:59 +00:00
|
|
|
'field',
|
2017-12-04 18:20:49 +00:00
|
|
|
array(
|
2018-01-31 14:47:59 +00:00
|
|
|
'name' => 'field son',
|
2017-12-04 18:20:49 +00:00
|
|
|
'collection_id' => $collection_son->get_id(),
|
2017-12-05 17:42:15 +00:00
|
|
|
'field_type' => $type,
|
2017-12-04 18:20:49 +00:00
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-11-30 13:44:29 +00:00
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$retrieve_metadata = $Tainacan_Fields->fetch_by_collection( $collection_son, [], 'OBJECT' );
|
2018-02-15 18:29:33 +00:00
|
|
|
|
|
|
|
// should return 6
|
|
|
|
$this->assertEquals( 6, sizeof( $retrieve_metadata ) );
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:50:24 +00:00
|
|
|
/**
|
|
|
|
* test remove core fields
|
|
|
|
*/
|
2018-02-15 18:29:33 +00:00
|
|
|
function test_core_fields(){
|
|
|
|
global $Tainacan_Fields;
|
2018-02-16 17:35:24 +00:00
|
|
|
|
|
|
|
$collection_grandfather = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'collection field'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$core_fields = $Tainacan_Fields->get_core_fields( $collection_grandfather );
|
|
|
|
|
|
|
|
if( $core_fields ){
|
|
|
|
foreach( $core_fields as $core_field ){
|
2018-02-16 17:50:24 +00:00
|
|
|
$this->assertFalse(wp_trash_post( $core_field->get_id() ) );
|
2018-02-15 18:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 13:44:29 +00:00
|
|
|
}
|
2017-12-04 18:30:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test if the defaults types are registered
|
|
|
|
*/
|
|
|
|
function test_metadata_field_type(){
|
2018-01-31 14:47:59 +00:00
|
|
|
global $Tainacan_Fields;
|
|
|
|
$this->assertEquals( 8, sizeof( $Tainacan_Fields->fetch_field_types() ) );
|
2017-12-04 18:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test if the defaults types are registered
|
|
|
|
*/
|
2017-12-05 10:37:50 +00:00
|
|
|
function test_metadata_field_type_insert(){
|
2018-01-31 14:47:59 +00:00
|
|
|
global $Tainacan_Fields;
|
2017-12-05 10:37:50 +00:00
|
|
|
$class = new RandomType;
|
2018-01-31 14:47:59 +00:00
|
|
|
$this->assertEquals( 9, sizeof( $Tainacan_Fields->fetch_field_types() ) );
|
2017-12-05 10:37:50 +00:00
|
|
|
}
|
2018-02-19 14:20:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function test_ordenation_fields(){
|
|
|
|
global $Tainacan_Collections, $Tainacan_Fields;
|
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'teste'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$type = $this->tainacan_field_factory->create_field('text');
|
|
|
|
|
|
|
|
$field1 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
|
|
|
array(
|
|
|
|
'name' => 'field1',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
|
|
|
'field_type' => $type,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$field2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
|
|
|
array(
|
|
|
|
'name' => 'field2',
|
|
|
|
'description' => 'field2',
|
|
|
|
'collection' => $collection,
|
|
|
|
'field_type' => $type,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$field3 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
|
|
|
array(
|
|
|
|
'name' => 'field3',
|
|
|
|
'description' => 'field3',
|
|
|
|
'collection' => $collection,
|
|
|
|
'field_type' => $type,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$collection->set_fields_order(
|
|
|
|
[
|
|
|
|
array( 'id' => $field3->get_id(), 'enable' => false ),
|
|
|
|
array( 'id' => $field2->get_id(), 'enable' => true ),
|
|
|
|
array( 'id' => $field1->get_id(), 'enable' => true )
|
|
|
|
]);
|
|
|
|
|
|
|
|
$update_collection = $Tainacan_Collections->update( $collection );
|
|
|
|
|
|
|
|
$fields_ordinate = $Tainacan_Fields->fetch_by_collection( $update_collection, [], 'OBJECT' );
|
|
|
|
$this->assertEquals( 'field3', $fields_ordinate[0]->get_name() );
|
|
|
|
|
|
|
|
$fields_ordinate_enabled = $Tainacan_Fields->fetch_by_collection( $update_collection, [ 'disabled_fields' => true ], 'OBJECT' );
|
|
|
|
$this->assertEquals( 'field2', $fields_ordinate_enabled[0]->get_name() );
|
|
|
|
}
|
2017-12-05 10:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TainacanFieldType
|
|
|
|
*/
|
|
|
|
class RandomType extends Field_Types\Field_Type {
|
|
|
|
|
|
|
|
function __construct(){
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-31 14:47:59 +00:00
|
|
|
* @param $field
|
2017-12-05 10:37:50 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
public function render( $field ){}
|
2017-11-09 11:01:11 +00:00
|
|
|
}
|