2018-03-27 03:53:51 +00:00
< ? php
namespace Tainacan\Tests ;
/**
* Class TestCollections
*
* @ package Test_Tainacan
*/
use Tainacan\Entities ;
/**
* Sample test case .
*/
2018-06-11 15:10:07 +00:00
class CompoundMetadatumTypes extends TAINACAN_UnitTestCase {
2018-03-27 03:53:51 +00:00
2018-06-11 17:57:50 +00:00
function test_compound_metadata_types () {
2018-03-27 03:53:51 +00:00
2018-06-11 15:10:07 +00:00
$Tainacan_Metadata = \Tainacan\Repositories\Metadata :: get_instance ();
2018-04-11 14:18:55 +00:00
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata :: get_instance ();
$Tainacan_Items = \Tainacan\Repositories\Items :: get_instance ();
2018-03-27 03:53:51 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'test' ,
),
true
);
2018-06-11 15:10:07 +00:00
$metadatum = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
2018-03-27 03:53:51 +00:00
array (
'name' => 'meta' ,
'description' => 'description' ,
'collection' => $collection ,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Compound' ,
2018-03-27 03:53:51 +00:00
'status' => 'publish' ,
),
true
);
2018-06-11 15:10:07 +00:00
$metadatum_child1 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
2018-03-27 03:53:51 +00:00
array (
'name' => 'meta2' ,
'description' => 'description' ,
'collection' => $collection ,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2018-03-27 03:53:51 +00:00
'status' => 'publish' ,
2018-06-11 15:10:07 +00:00
'parent' => $metadatum -> get_id (),
2018-03-27 03:53:51 +00:00
),
true
);
2018-06-11 15:10:07 +00:00
$metadatum_child2 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
2018-03-27 03:53:51 +00:00
array (
'name' => 'meta3' ,
'description' => 'description' ,
'collection' => $collection ,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2018-03-27 03:53:51 +00:00
'status' => 'publish' ,
2018-06-11 15:10:07 +00:00
'parent' => $metadatum -> get_id (),
2018-03-27 03:53:51 +00:00
),
true
);
$i = $this -> tainacan_entity_factory -> create_entity (
'item' ,
array (
'title' => 'item test' ,
'description' => 'adasdasdsa' ,
'collection' => $collection ,
'status' => 'publish' ,
),
true
);
2018-06-11 15:10:07 +00:00
$item_metadata1 = new \Tainacan\Entities\Item_Metadata_Entity ( $i , $metadatum_child1 );
2018-03-27 03:53:51 +00:00
$item_metadata1 -> set_value ( 'Red' );
$item_metadata1 -> validate ();
$item_metadata1 = $Tainacan_Item_Metadata -> insert ( $item_metadata1 );
2018-06-11 15:10:07 +00:00
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity ( $i , $metadatum_child2 , null , $item_metadata1 -> get_parent_meta_id ());
2018-03-27 03:53:51 +00:00
$item_metadata -> set_value ( 'Blue' );
$item_metadata -> validate ();
$item_metadata = $Tainacan_Item_Metadata -> insert ( $item_metadata );
2018-06-11 15:10:07 +00:00
$compoundItem = new \Tainacan\Entities\Item_Metadata_Entity ( $i , $metadatum );
2018-03-27 03:53:51 +00:00
global $wpdb ;
$compoundValue = $compoundItem -> get_value ();
$this -> assertTrue ( is_array ( $compoundValue ), 'value of a compound should return array' );
$this -> assertEquals ( 2 , sizeof ( $compoundValue ), 'value should have 2 item metadata' );
2018-06-11 15:10:07 +00:00
$this -> assertTrue ( isset ( $compoundValue [ $metadatum_child1 -> get_id ()]), 'First element of value must be set' );
$this -> assertTrue ( isset ( $compoundValue [ $metadatum_child2 -> get_id ()]), 'Second element of value must be set' );
2018-03-27 03:53:51 +00:00
2018-06-11 15:10:07 +00:00
$this -> assertTrue ( $compoundValue [ $metadatum_child1 -> get_id ()] instanceof \Tainacan\Entities\Item_Metadata_Entity , 'First element of value should be an item metadata entity' );
$this -> assertTrue ( $compoundValue [ $metadatum_child2 -> get_id ()] instanceof \Tainacan\Entities\Item_Metadata_Entity , 'Second element of value should be an item metadata entity' );
2018-03-27 03:53:51 +00:00
2018-03-28 01:23:55 +00:00
2018-06-11 15:10:07 +00:00
$this -> assertEquals ( 'Red' , $compoundValue [ $metadatum_child1 -> get_id ()] -> get_value () , 'First element of value should have "Red" value' );
$this -> assertEquals ( 'Blue' , $compoundValue [ $metadatum_child2 -> get_id ()] -> get_value () , 'Second element of value should have "Blue" value' );
2018-03-28 01:23:55 +00:00
}
2018-06-11 17:57:50 +00:00
function test_multiple_compound_metadata_types () {
2018-03-28 01:23:55 +00:00
2018-06-11 15:10:07 +00:00
$Tainacan_Metadata = \Tainacan\Repositories\Metadata :: get_instance ();
2018-04-11 14:18:55 +00:00
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata :: get_instance ();
$Tainacan_Items = \Tainacan\Repositories\Items :: get_instance ();
2018-03-28 01:23:55 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'test' ,
),
true
);
2018-06-11 15:10:07 +00:00
$metadatum = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
2018-03-28 01:23:55 +00:00
array (
'name' => 'meta' ,
'description' => 'description' ,
'collection' => $collection ,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Compound' ,
2018-03-28 01:23:55 +00:00
'status' => 'publish' ,
'multiple' => 'yes'
),
true
);
2018-06-11 15:10:07 +00:00
$metadatum_child1 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
2018-03-28 01:23:55 +00:00
array (
'name' => 'meta2' ,
'description' => 'description' ,
'collection' => $collection ,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2018-03-28 01:23:55 +00:00
'status' => 'publish' ,
2018-06-11 15:10:07 +00:00
'parent' => $metadatum -> get_id (),
2018-03-28 01:23:55 +00:00
),
true
);
2018-06-11 15:10:07 +00:00
$metadatum_child2 = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
2018-03-28 01:23:55 +00:00
array (
'name' => 'meta3' ,
'description' => 'description' ,
'collection' => $collection ,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2018-03-28 01:23:55 +00:00
'status' => 'publish' ,
2018-06-11 15:10:07 +00:00
'parent' => $metadatum -> get_id (),
2018-03-28 01:23:55 +00:00
),
true
);
$i = $this -> tainacan_entity_factory -> create_entity (
'item' ,
array (
'title' => 'item test' ,
'description' => 'adasdasdsa' ,
'collection' => $collection ,
'status' => 'publish' ,
),
true
);
global $wpdb ;
// First Instance
2018-06-11 15:10:07 +00:00
$item_metadata1 = new \Tainacan\Entities\Item_Metadata_Entity ( $i , $metadatum_child1 );
2018-03-28 01:23:55 +00:00
$item_metadata1 -> set_value ( 'Red' );
$item_metadata1 -> validate ();
$item_metadata1 = $Tainacan_Item_Metadata -> insert ( $item_metadata1 );
2018-06-11 15:10:07 +00:00
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity ( $i , $metadatum_child2 , null , $item_metadata1 -> get_parent_meta_id ());
2018-03-28 01:23:55 +00:00
$item_metadata -> set_value ( 'Blue' );
$item_metadata -> validate ();
$item_metadata = $Tainacan_Item_Metadata -> insert ( $item_metadata );
// Second Instance
2018-06-11 15:10:07 +00:00
$item_metadata3 = new \Tainacan\Entities\Item_Metadata_Entity ( $i , $metadatum_child1 );
2018-03-28 01:23:55 +00:00
$item_metadata3 -> set_value ( 'Green' );
$item_metadata3 -> validate ();
$item_metadata3 = $Tainacan_Item_Metadata -> insert ( $item_metadata3 );
2018-06-11 15:10:07 +00:00
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity ( $i , $metadatum_child2 , null , $item_metadata3 -> get_parent_meta_id ());
2018-03-28 01:23:55 +00:00
$item_metadata -> set_value ( 'Yellow' );
$item_metadata -> validate ();
$item_metadata = $Tainacan_Item_Metadata -> insert ( $item_metadata );
2018-06-11 15:10:07 +00:00
$compoundItem = new \Tainacan\Entities\Item_Metadata_Entity ( $i , $metadatum );
2018-03-28 01:23:55 +00:00
//var_dump($wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE post_id = {$i->get_id()}"));
2018-06-11 15:10:07 +00:00
//var_dump($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE parent = {$metadatum->get_id()}"));
2018-03-28 01:23:55 +00:00
$compoundValue = $compoundItem -> get_value ();
$this -> assertTrue ( is_array ( $compoundValue ), 'value of a compound should return array' );
$this -> assertEquals ( 2 , sizeof ( $compoundValue ), 'value should have 2 values' );
$this -> assertTrue ( is_array ( $compoundValue [ 0 ]), 'value of a compound should return array' );
$this -> assertTrue ( is_array ( $compoundValue [ 1 ]), 'value of a compound should return array' );
2018-06-11 15:10:07 +00:00
$this -> assertTrue ( isset ( $compoundValue [ 0 ][ $metadatum_child1 -> get_id ()]), 'First element of value must be set' );
$this -> assertTrue ( isset ( $compoundValue [ 1 ][ $metadatum_child2 -> get_id ()]), 'Second element of value must be set' );
2018-03-28 01:23:55 +00:00
2018-06-11 15:10:07 +00:00
$this -> assertTrue ( $compoundValue [ 0 ][ $metadatum_child1 -> get_id ()] instanceof \Tainacan\Entities\Item_Metadata_Entity , 'First element of value should be an item metadata entity' );
$this -> assertTrue ( $compoundValue [ 1 ][ $metadatum_child2 -> get_id ()] instanceof \Tainacan\Entities\Item_Metadata_Entity , 'Second element of value should be an item metadata entity' );
2018-03-28 01:23:55 +00:00
2018-06-11 15:10:07 +00:00
$this -> assertEquals ( 'Red' , $compoundValue [ 0 ][ $metadatum_child1 -> get_id ()] -> get_value () , 'First element of value should have "Red" value' );
$this -> assertEquals ( 'Blue' , $compoundValue [ 0 ][ $metadatum_child2 -> get_id ()] -> get_value () , 'Second element of value should have "Blue" value' );
2018-03-28 01:23:55 +00:00
2018-06-11 15:10:07 +00:00
$this -> assertEquals ( 'Green' , $compoundValue [ 1 ][ $metadatum_child1 -> get_id ()] -> get_value () , 'First element of value should have "Red" value' );
$this -> assertEquals ( 'Yellow' , $compoundValue [ 1 ][ $metadatum_child2 -> get_id ()] -> get_value () , 'Second element of value should have "Blue" value' );
2018-03-27 03:53:51 +00:00
}
2018-04-11 02:13:13 +00:00
function test_validations_category_in_multiple () {
2018-06-11 15:10:07 +00:00
$Tainacan_Metadata = \Tainacan\Repositories\Metadata :: get_instance ();
2018-04-11 02:13:13 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'test' ,
),
true
);
2018-06-11 15:10:07 +00:00
$metadatum = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
2018-04-11 02:13:13 +00:00
array (
'name' => 'meta' ,
'description' => 'description' ,
'collection' => $collection ,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Compound' ,
2018-04-11 02:13:13 +00:00
'status' => 'publish' ,
'multiple' => 'yes'
),
true
);
2018-06-11 15:10:07 +00:00
$newMetadatum = new \Tainacan\Entities\Metadatum ();
$newMetadatum -> set_name ( 'test_multiple' );
2018-06-11 17:57:50 +00:00
$newMetadatum -> set_metadata_type ( 'Tainacan\Metadata_Types\Category' );
2018-06-11 15:10:07 +00:00
$newMetadatum -> set_parent ( $metadatum -> get_id ());
2018-04-11 02:13:13 +00:00
2018-06-11 15:10:07 +00:00
$this -> assertFalse ( $newMetadatum -> validate (), 'You cant add a category metadatum inside a multiple compound metadatum' );
2018-04-11 02:13:13 +00:00
2018-06-11 17:57:50 +00:00
$newMetadatum -> set_metadata_type ( 'Tainacan\Metadata_Types\Text' );
2018-06-11 15:10:07 +00:00
$this -> assertTrue ( $newMetadatum -> validate ());
2018-04-11 02:13:13 +00:00
}
function test_validations_category_in_multiple_2 () {
2018-06-11 15:10:07 +00:00
$Tainacan_Metadata = \Tainacan\Repositories\Metadata :: get_instance ();
2018-04-11 02:13:13 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'test' ,
),
true
);
2018-06-11 15:10:07 +00:00
$metadatum = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
2018-04-11 02:13:13 +00:00
array (
'name' => 'meta' ,
'description' => 'description' ,
'collection' => $collection ,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Compound' ,
2018-04-11 02:13:13 +00:00
'status' => 'publish' ,
'multiple' => 'no'
),
true
);
2018-06-11 15:10:07 +00:00
$newMetadatum = new \Tainacan\Entities\Metadatum ();
$newMetadatum -> set_name ( 'test_multiple' );
2018-06-11 17:57:50 +00:00
$newMetadatum -> set_metadata_type ( 'Tainacan\Metadata_Types\Category' );
2018-06-11 15:10:07 +00:00
$newMetadatum -> set_parent ( $metadatum -> get_id ());
2018-04-11 02:13:13 +00:00
2018-06-11 15:10:07 +00:00
$this -> assertTrue ( $newMetadatum -> validate (), 'You can add a category metadatum inside a not multiple compound metadatum' );
$newMetadatum = $Tainacan_Metadata -> insert ( $newMetadatum );
2018-04-11 02:13:13 +00:00
2018-06-11 15:10:07 +00:00
$metadatum -> set_multiple ( 'yes' );
2018-04-11 02:13:13 +00:00
2018-06-11 15:10:07 +00:00
$this -> assertFalse ( $metadatum -> validate (), 'You cant turn a compound metadatum into multiple when there is a category metadatum inside it' );
2018-04-11 02:13:13 +00:00
}
2018-06-11 15:10:07 +00:00
function test_validations_multiple_metadata () {
2018-04-11 02:13:13 +00:00
2018-06-11 15:10:07 +00:00
$Tainacan_Metadata = \Tainacan\Repositories\Metadata :: get_instance ();
2018-04-11 02:13:13 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'test' ,
),
true
);
2018-06-11 15:10:07 +00:00
$metadatum = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
2018-04-11 02:13:13 +00:00
array (
'name' => 'meta' ,
'description' => 'description' ,
'collection' => $collection ,
2018-06-11 17:57:50 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Compound' ,
2018-04-11 02:13:13 +00:00
'status' => 'publish' ,
'multiple' => 'no'
),
true
);
2018-06-11 15:10:07 +00:00
$newMetadatum = new \Tainacan\Entities\Metadatum ();
$newMetadatum -> set_name ( 'test_multiple' );
$newMetadatum -> set_multiple ( 'yes' );
2018-06-11 17:57:50 +00:00
$newMetadatum -> set_metadata_type ( 'Tainacan\Metadata_Types\Text' );
2018-06-11 15:10:07 +00:00
$newMetadatum -> set_parent ( $metadatum -> get_id ());
2018-04-11 02:13:13 +00:00
2018-06-11 15:10:07 +00:00
$this -> assertFalse ( $newMetadatum -> validate (), 'You cant add a multiple metadatum inside a compound metadatum' );
2018-04-11 02:13:13 +00:00
2018-06-11 15:10:07 +00:00
$newMetadatum -> set_multiple ( 'no' );
2018-04-11 02:13:13 +00:00
2018-06-11 15:10:07 +00:00
$this -> assertTrue ( $newMetadatum -> validate ());
2018-04-11 02:13:13 +00:00
}
2018-03-27 03:53:51 +00:00
}