2017-11-12 23:14:47 +00:00
< ? php
2017-11-15 18:50:11 +00:00
namespace Tainacan\Tests ;
2021-04-08 02:16:56 +00:00
use Tainacan\Entities\Item_Metadata_Entity ;
2017-11-12 23:14:47 +00:00
/**
2021-04-05 16:37:15 +00:00
* Class Item_Metadata
2017-11-12 23:14:47 +00:00
*
* @ package Test_Tainacan
*/
/**
2021-04-05 14:57:23 +00:00
* Item_Metadata test case .
2017-11-12 23:14:47 +00:00
*/
2017-12-04 14:58:19 +00:00
class Item_Metadata extends TAINACAN_UnitTestCase {
2017-11-12 23:14:47 +00:00
2021-04-05 17:07:57 +00:00
private $collection = null ;
private $item = null ;
2021-04-06 13:01:14 +00:00
private $separator = '<span class="multivalue-separator"> | </span>' ;
2021-04-05 17:07:57 +00:00
public function setUp () {
parent :: setUp ();
$c = $this -> tainacan_entity_factory -> create_entity ( 'collection' , [ 'name' => 'My Collection' ], true );
$i = $this -> tainacan_entity_factory -> create_entity (
'item' ,
2021-04-08 02:26:33 +00:00
array (
2021-04-05 17:07:57 +00:00
'title' => 'My test item' ,
'description' => 'item description' ,
'collection' => $c ,
'status' => 'publish'
),
true
);
$this -> collection = $c ;
$this -> item = $i ;
}
2021-04-09 14:20:17 +00:00
/**
* Teste da insercao de um metadado simples sem o tipo
*/
function test_add () {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata :: get_instance ();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata :: get_instance ();
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'test' ,
'description' => 'No description' ,
),
true
);
$metadatum = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'metadado' ,
'description' => 'descricao' ,
'collection' => $collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
),
true
);
$test = $Tainacan_Metadata -> fetch ( $metadatum -> get_id ());
$i = $this -> tainacan_entity_factory -> create_entity (
'item' ,
array (
'title' => 'item teste title' ,
'description' => 'item description' ,
'collection' => $collection
),
true
);
$Tainacan_Items = \Tainacan\Repositories\Items :: get_instance ();
$item = $Tainacan_Items -> fetch ( $i -> get_id ());
$item_metadata = new Item_Metadata_Entity ( $item , $test );
$item_metadata -> set_value ( 'teste_value' );
2018-04-09 14:32:20 +00:00
$item_metadata -> validate ();
2021-04-09 14:20:17 +00:00
$item_metadata = $Tainacan_Item_Metadata -> insert ( $item_metadata );
$this -> assertEquals ( 'teste_value' , $item_metadata -> get_value ());
}
/**
* Teste da insercao de um metadado simples com o tipo
*/
function test_required (){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata :: get_instance ();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata :: get_instance ();
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'teste' ,
'description' => 'No description' ,
),
true
);
$metadatum = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'metadado' ,
'description' => 'descricao' ,
'collection' => $collection ,
'required' => 'yes' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
),
true
);
$test = $Tainacan_Metadata -> fetch ( $metadatum -> get_id ());
$i = $this -> tainacan_entity_factory -> create_entity (
'item' ,
array (
'title' => 'item teste' ,
'status' => 'publish' ,
'description' => 'adasdasdsa' ,
'collection' => $collection
),
true
);
$Tainacan_Items = \Tainacan\Repositories\Items :: get_instance ();
$item = $Tainacan_Items -> fetch ( $i -> get_id ());
$item_metadata = new Item_Metadata_Entity ( $item , $test );
// false because its required
$this -> assertFalse ( $item_metadata -> validate ());
$item_metadata -> set_value ( 'teste_value' );
$this -> assertTrue ( $item_metadata -> validate ());
$item_metadata = $Tainacan_Item_Metadata -> insert ( $item_metadata );
$this -> assertEquals ( 'teste_value' , $item_metadata -> get_value ());
}
function test_collection_key (){
$Tainacan_Metadata = \Tainacan\Repositories\Metadata :: get_instance ();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata :: get_instance ();
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'teste' ,
'description' => 'No description' ,
),
true
);
$metadatum = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'metadado' ,
'description' => 'descricao' ,
'collection' => $collection ,
'collection_key' => 'yes' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
),
true
);
$i = $this -> tainacan_entity_factory -> create_entity (
'item' ,
array (
'title' => 'item teste' ,
'description' => 'adasdasdsa' ,
'collection' => $collection ,
'status' => 'publish'
),
true
);
2018-04-22 21:39:54 +00:00
$i2 = $this -> tainacan_entity_factory -> create_entity (
'item' ,
array (
'title' => 'other item' ,
'description' => 'adasdasdsa' ,
'collection' => $collection ,
'status' => 'publish'
),
true
);
2021-04-09 14:20:17 +00:00
$Tainacan_Items = \Tainacan\Repositories\Items :: get_instance ();
2017-11-12 23:14:47 +00:00
2021-04-09 14:20:17 +00:00
$value = 'teste_val' ;
$item_metadata = new Item_Metadata_Entity ( $i , $metadatum );
$item_metadata -> set_value ( $value );
2017-12-04 14:58:19 +00:00
2021-04-09 14:20:17 +00:00
$this -> assertTrue ( $item_metadata -> validate ());
2017-12-04 14:58:19 +00:00
2021-04-09 14:20:17 +00:00
$item_metadata -> validate ();
$item_metadata = $Tainacan_Item_Metadata -> insert ( $item_metadata );
2017-11-12 23:14:47 +00:00
2021-04-09 14:20:17 +00:00
$n_item_metadata = new Item_Metadata_Entity ( $i , $metadatum );
$n_item_metadata -> set_value ( $value );
2018-04-22 21:39:54 +00:00
$this -> assertTrue ( $n_item_metadata -> validate (), 'trying to validate the same item with same value should be ok' );
2017-12-04 14:58:19 +00:00
2021-04-08 02:16:56 +00:00
$n_item_metadata2 = new Item_Metadata_Entity ( $i2 , $metadatum );
2018-04-22 21:39:54 +00:00
$n_item_metadata2 -> set_value ( $value );
$this -> assertFalse ( $n_item_metadata2 -> validate (), 'Collection key should not validate another item metadatada with the same value' );
2021-04-09 14:20:17 +00:00
}
function test_fetch (){
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata :: get_instance ();
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'teste' ,
'description' => 'No description' ,
),
true
);
$this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'metadado' ,
'description' => 'descricao' ,
'collection' => $collection ,
'status' => 'publish' ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
),
true
);
//$test = $Tainacan_Metadata->fetch($metadatum->get_id());
$i = $this -> tainacan_entity_factory -> create_entity (
'item' ,
array (
'title' => 'item teste' ,
'description' => 'adasdasdsa' ,
'collection' => $collection
),
true
);
$item_metadatas = $Tainacan_Item_Metadata -> fetch ( $i , 'OBJECT' );
$names = [];
foreach ( $item_metadatas as $item_metadata ) {
$names [] = $item_metadata -> get_metadatum () -> get_name ();
}
$this -> assertTrue ( is_array ( $item_metadatas ));
// notice for repository metadata
$this -> assertEquals ( 3 , sizeof ( $item_metadatas ));
//first 2 metadata are repository metadata
$this -> assertTrue ( in_array ( 'metadado' , $names ) );
2018-09-13 20:07:32 +00:00
}
function test_metadata_text_textarea () {
2021-04-09 14:20:17 +00:00
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'teste'
),
true
2018-09-13 20:07:32 +00:00
);
$i = $this -> tainacan_entity_factory -> create_entity (
2021-04-09 14:20:17 +00:00
'item' ,
array (
'title' => 'item teste' ,
'description' => 'description' ,
'collection' => $collection ,
'status' => 'publish'
),
true
);
$metadatum_text = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'metadadoText' ,
'description' => 'descricao' ,
'collection_id' => $collection -> get_id (),
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
),
true
2018-09-13 20:07:32 +00:00
);
$metadatum_textarea = $this -> tainacan_entity_factory -> create_entity (
2021-04-09 14:20:17 +00:00
'metadatum' ,
array (
'name' => 'metadadoTextarea' ,
'description' => 'descricao' ,
'collection_id' => $collection -> get_id (),
'metadata_type' => 'Tainacan\Metadata_Types\Textarea' ,
),
true
2018-09-13 20:07:32 +00:00
);
$value_text = 'GOOGLE: www.google.com' ;
2021-04-08 02:16:56 +00:00
$item_metadata_text = new Item_Metadata_Entity ( $i , $metadatum_text );
2018-09-13 20:07:32 +00:00
$item_metadata_text -> set_value ( $value_text );
$value_textarea = 'GOOGLE: www.google.com \n GOOGLE: https://www.google.com' ;
2021-04-08 02:16:56 +00:00
$item_metadata_textarea = new Item_Metadata_Entity ( $i , $metadatum_textarea );
2018-09-13 20:07:32 +00:00
$item_metadata_textarea -> set_value ( $value_textarea );
2019-06-18 20:52:42 +00:00
$response_text = 'GOOGLE: <a href="http://www.google.com" target="_blank" title="www.google.com">www.google.com</a>' ;
$response_textarea = 'GOOGLE: <a href="http://www.google.com" target="_blank" title="www.google.com">www.google.com</a> \n GOOGLE: <a href="https://www.google.com" target="_blank" title="https://www.google.com">https://www.google.com</a>' ;
2018-09-13 20:07:32 +00:00
$this -> assertEquals ( $item_metadata_text -> get_value_as_html (), $response_text );
$this -> assertEquals ( $item_metadata_textarea -> get_value_as_html (), $response_textarea );
2021-03-29 06:20:52 +00:00
// Poor HTML entry tests
2021-04-08 02:16:56 +00:00
$badFormatted_HTML = " <p> I started my content <div> and make something else here </div> without closing its HTML properly " ;
2021-03-29 06:20:52 +00:00
2021-04-08 02:16:56 +00:00
$item_metadata_text -> set_value ( $badFormatted_HTML );
$item_metadata_textarea -> set_value ( $badFormatted_HTML );
2021-03-29 20:09:17 +00:00
2021-04-08 02:16:56 +00:00
$this -> assertEquals ( $item_metadata_text -> get_value_as_html (), $badFormatted_HTML . " </p> " );
$this -> assertEquals ( $item_metadata_textarea -> get_value_as_html (), $badFormatted_HTML . " </p> " );
2018-09-13 20:07:32 +00:00
}
2019-04-12 19:32:33 +00:00
2021-04-09 14:20:17 +00:00
/**
* @ group test_item_metadata_has_value
*/
2019-04-12 21:46:54 +00:00
function test_item_metadata_has_value () {
2021-03-29 06:20:52 +00:00
$test_value = 'has_value' ;
2021-04-09 14:20:17 +00:00
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata :: get_instance ();
$collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
array (
'name' => 'teste'
),
true
);
$i = $this -> tainacan_entity_factory -> create_entity (
'item' ,
array (
'title' => 'item teste' ,
'description' => 'description' ,
'collection' => $collection ,
'status' => 'publish'
),
true
);
$metadatum_textarea = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'metadadoTextarea' ,
'description' => 'descricao' ,
'collection_id' => $collection -> get_id (),
'metadata_type' => 'Tainacan\Metadata_Types\Textarea' ,
),
true
);
$value_textarea = '' ;
$item_metadata_textarea = new Item_Metadata_Entity ( $i , $metadatum_textarea );
$item_metadata_textarea -> set_value ( $value_textarea );
$item_metadata_textarea -> validate ();
$item_metadata_textarea = $Tainacan_Item_Metadata -> insert ( $item_metadata_textarea );
$this -> assertFalse ( $item_metadata_textarea -> has_value ());
$item_metadata_textarea -> set_value ( $test_value );
$item_metadata_textarea -> validate ();
$item_metadata_textarea = $Tainacan_Item_Metadata -> insert ( $item_metadata_textarea );
$this -> assertTrue ( $item_metadata_textarea -> has_value ());
$metadatum_text = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'metadadoText' ,
'description' => 'descricao' ,
'collection_id' => $collection -> get_id (),
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
'multiple' => 'yes'
),
true
);
$item_metadata_text = new Item_Metadata_Entity ( $i , $metadatum_text );
$item_metadata_text -> set_value ([ $value_textarea ]);
$item_metadata_text -> validate ();
$item_metadata = $Tainacan_Item_Metadata -> insert ( $item_metadata_text );
$this -> assertFalse ( $item_metadata -> has_value ());
2019-04-12 21:46:54 +00:00
2021-03-29 06:20:52 +00:00
$item_metadata_text -> set_value ([ $test_value ]);
2019-04-12 21:46:54 +00:00
2021-04-09 14:20:17 +00:00
$item_metadata_text -> validate ();
$item_metadata_text = $Tainacan_Item_Metadata -> insert ( $item_metadata_text );
2019-04-12 21:46:54 +00:00
2021-04-09 14:20:17 +00:00
$this -> assertTrue ( $item_metadata_text -> has_value ());
}
2021-04-05 14:57:23 +00:00
2021-04-08 01:32:17 +00:00
function test_numeric_metadata_html () {
2021-04-05 16:37:15 +00:00
// Simple numeric metadata
2021-04-05 14:57:23 +00:00
$metadatum_numeric = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'Numeric important meta' ,
'description' => 'and its description' ,
2021-04-05 17:07:57 +00:00
'collection_id' => $this -> collection -> get_id (),
2021-04-05 14:57:23 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Numeric' ,
'status' => 'publish'
),
true
);
2021-04-05 16:37:15 +00:00
// Multiple numeric metadata
2021-04-05 14:57:23 +00:00
$metadatum_numeric_multiple = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'Numeric important meta' ,
'description' => 'and its description' ,
2021-04-05 17:07:57 +00:00
'collection_id' => $this -> collection -> get_id (),
2021-04-05 14:57:23 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Numeric' ,
'status' => 'publish' ,
'multiple' => 'yes'
),
true
);
2021-04-08 02:16:56 +00:00
$item_metadata_numeric = new Item_Metadata_Entity ( $this -> item , $metadatum_numeric );
2021-04-05 14:57:23 +00:00
$item_metadata_numeric -> set_value ( 10 );
$item_metadata_numeric -> validate ();
$this -> assertEquals ( $item_metadata_numeric -> get_value_as_html (), 10 );
2021-04-08 02:16:56 +00:00
$item_metadata_numeric_mult = new Item_Metadata_Entity ( $this -> item , $metadatum_numeric_multiple );
2021-04-05 14:57:23 +00:00
$item_metadata_numeric_mult -> set_value ([ 10 , 22 , 4 ]);
$item_metadata_numeric_mult -> validate ();
2021-04-06 13:01:14 +00:00
$this -> assertEquals ( $item_metadata_numeric_mult -> get_value_as_html (), " 10 " . $this -> separator . " 22 " . $this -> separator . " 4 " );
2021-04-05 14:57:23 +00:00
}
2021-04-05 16:37:15 +00:00
2021-04-05 17:38:09 +00:00
function test_date_metadata_html () {
2021-04-05 16:37:15 +00:00
// Simple date metadata
$metadatum_date = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'Date important meta' ,
'description' => 'and its description' ,
2021-04-05 17:07:57 +00:00
'collection_id' => $this -> collection -> get_id (),
2021-04-05 16:37:15 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Date' ,
'status' => 'publish'
),
true
);
// Multiple date metadata
$metadatum_date_multiple = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'Date meta' ,
'description' => 'and its description' ,
2021-04-05 17:07:57 +00:00
'collection_id' => $this -> collection -> get_id (),
2021-04-05 16:37:15 +00:00
'metadata_type' => 'Tainacan\Metadata_Types\Date' ,
'status' => 'publish' ,
'multiple' => 'yes'
),
true
);
2021-04-08 02:16:56 +00:00
$item_metadata_date = new Item_Metadata_Entity ( $this -> item , $metadatum_date );
2021-04-05 16:37:15 +00:00
// Invalid date value
$item_metadata_date -> set_value ( 10 );
$item_metadata_date -> validate ();
$this -> assertFalse ( $item_metadata_date -> get_value_as_html ());
$item_metadata_date -> set_value ( " 2021-04-05 " );
$item_metadata_date -> validate ();
$this -> assertEquals ( $item_metadata_date -> get_value_as_html (), " April 5, 2021 " );
2021-04-08 02:16:56 +00:00
$item_metadata_date_mult = new Item_Metadata_Entity ( $this -> item , $metadatum_date_multiple );
2021-04-05 16:37:15 +00:00
// Invalid date values
$item_metadata_date_mult -> set_value ([ 10 , 22 , 4 ]);
$item_metadata_date_mult -> validate ();
2021-04-06 13:01:14 +00:00
$this -> assertEquals ( $item_metadata_date_mult -> get_value_as_html (), $this -> separator . $this -> separator );
2021-04-05 16:37:15 +00:00
$item_metadata_date_mult -> set_value ([ " 2021-04-05 " , " 2021-12-30 " ]);
$item_metadata_date_mult -> validate ();
2021-04-06 13:01:14 +00:00
$this -> assertEquals ( $item_metadata_date_mult -> get_value_as_html (), 'April 5, 2021' . $this -> separator . 'December 30, 2021' );
2021-04-05 16:37:15 +00:00
}
2021-04-05 17:38:09 +00:00
function test_user_metadata_html () {
$user_metadata = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'User metadata' ,
'description' => 'and its description' ,
'collection_id' => $this -> collection -> get_id (),
'metadata_type' => 'Tainacan\Metadata_Types\User' ,
'status' => 'publish'
),
true
);
2021-04-08 02:16:56 +00:00
$user_meta = new Item_Metadata_Entity ( $this -> item , $user_metadata );
2021-04-05 17:38:09 +00:00
// Empty val
$this -> assertEmpty ( $user_meta -> get_value_as_html ());
$new_user = $this -> factory () -> user -> create ( array ( 'role' => 'subscriber' , 'display_name' => 'User Name' ));
$user_meta -> set_value ( $new_user );
$user_meta -> validate ();
$this -> assertEquals ( $user_meta -> get_value_as_html (), " User Name " );
$user_metadata_multiple = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'User metadata' ,
'description' => 'and its description' ,
'collection_id' => $this -> collection -> get_id (),
'metadata_type' => 'Tainacan\Metadata_Types\User' ,
'status' => 'publish' ,
'multiple' => 'yes'
),
true
);
2021-04-08 02:16:56 +00:00
$user_meta_multi = new Item_Metadata_Entity ( $this -> item , $user_metadata_multiple );
2021-04-05 17:38:09 +00:00
$this -> assertEmpty ( $user_meta_multi -> get_value_as_html ());
$sec_user = $this -> factory () -> user -> create ( array ( 'role' => 'subscriber' , 'display_name' => 'User Name 2' ));
$user_meta_multi -> set_value ([ $new_user , $sec_user ]);
$user_meta_multi -> validate ();
2021-04-06 13:01:14 +00:00
$this -> assertEquals ( $user_meta_multi -> get_value_as_html (), 'User Name' . $this -> separator . 'User Name 2' );
2021-04-05 17:38:09 +00:00
}
2021-04-05 18:46:51 +00:00
function test_selectbox_metadata_html () {
$selectbox_metadata = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'My selectbox meta' ,
'status' => 'publish' ,
'collection' => $this -> collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Selectbox' ,
'metadata_type_options' => [
'options' => [ 'tainacan' , 'wordpress' , 'php' ]
]
),
true
);
$selectbox_metadata_multiple = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'My selectbox meta' ,
'status' => 'publish' ,
'collection' => $this -> collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Selectbox' ,
'multiple' => 'yes' ,
'metadata_type_options' => [
'options' => [ 'tainacan' , 'wordpress' , 'php' ]
]
),
true
);
2021-04-08 02:16:56 +00:00
$sb_meta = new Item_Metadata_Entity ( $this -> item , $selectbox_metadata );
2021-04-05 18:46:51 +00:00
$sb_meta -> set_value ( 'tainacan' );
$sb_meta -> validate ();
$this -> assertEquals ( $sb_meta -> get_value_as_html (), 'tainacan' );
$sb_meta -> set_value ( 'php' );
$this -> assertEquals ( $sb_meta -> get_value_as_html (), 'php' );
2021-04-08 02:16:56 +00:00
$sb_meta_multi = new Item_Metadata_Entity ( $this -> item , $selectbox_metadata_multiple );
2021-04-05 18:46:51 +00:00
$sb_meta_multi -> set_value ([ 'tainacan' , 'wordpress' ]);
$sb_meta_multi -> validate ();
2021-04-06 13:01:14 +00:00
$this -> assertEquals ( $sb_meta_multi -> get_value_as_html (), 'tainacan' . $this -> separator . 'wordpress' );
2021-04-05 18:46:51 +00:00
}
2021-04-06 05:04:43 +00:00
function test_relationship_metadata_html () {
$referenced_collection = $this -> tainacan_entity_factory -> create_entity (
'collection' ,
[ 'name' => 'INXS Songs' ],
true
);
$mystify = $this -> tainacan_entity_factory -> create_entity (
'item' ,
array (
'title' => 'Mystify' ,
'description' => '"Mystify" is the 9th track of INXS 6th album "Kick".' ,
'collection' => $referenced_collection ,
'status' => 'publish'
),
true
);
2021-04-06 09:32:15 +00:00
$disappear = $this -> tainacan_entity_factory -> create_entity (
'item' ,
array (
'title' => 'Disappear' ,
'description' => '"Disappear" is the second single from INXS 7th album "X".It was released in December 1990.' ,
'collection' => $referenced_collection ,
'status' => 'publish'
),
true
);
$expected_return = $this -> relationship_expected_return ( $mystify -> get_id (), $mystify -> get_title ());
$expected_return2 = $this -> relationship_expected_return ( $disappear -> get_id (), $disappear -> get_title ());
2021-04-06 05:04:43 +00:00
$relationship_metadata = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'Relationship meta' ,
'description' => 'My desc' ,
'collection' => $this -> collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Relationship' ,
'status' => 'publish' ,
'metadata_type_options' => [
'collection_id' => $referenced_collection -> get_id (),
'search' => $referenced_collection -> get_core_title_metadatum () -> get_id ()
]
),
true
);
2021-04-08 02:16:56 +00:00
$item_metadata_relationship = new Item_Metadata_Entity ( $this -> item , $relationship_metadata );
2021-04-06 09:32:15 +00:00
$item_metadata_relationship -> validate ();
$this -> assertEquals ( $item_metadata_relationship -> get_value_as_html (), '' );
$item_metadata_relationship -> set_value ( $mystify -> get_id ());
$item_metadata_relationship -> validate ();
$this -> assertEquals ( $item_metadata_relationship -> get_value_as_html (), $expected_return );
2021-04-06 05:04:43 +00:00
2021-04-06 09:32:15 +00:00
$item_metadata_relationship -> set_value ([ $this -> collection -> get_id ()]);
$item_metadata_relationship -> validate ();
$this -> assertEquals ( $item_metadata_relationship -> get_value_as_html (), '' );
2021-04-06 05:04:43 +00:00
2021-04-06 09:32:15 +00:00
$relationship_metadata -> set_multiple ( 'yes' );
$item_metadata_relationship -> set_value ([ $mystify -> get_id (), $disappear -> get_id () ]);
2021-04-06 13:01:14 +00:00
$this -> assertEquals ( $item_metadata_relationship -> get_value_as_html (), " ${ expected_return } " . $this -> separator . " ${ expected_return2 } " );
2021-04-06 05:04:43 +00:00
}
2021-04-06 11:40:05 +00:00
function test_taxonomy_metadata_html () {
$taxonomy = $this -> tainacan_entity_factory -> create_entity (
'taxonomy' ,
array (
'name' => 'My Taxonomy test' ,
'collections' => [ $this -> collection ],
'status' => 'publish'
),
true
);
2021-04-06 12:41:32 +00:00
$term = $this -> tainacan_entity_factory -> create_entity (
'term' ,
array (
'taxonomy' => $taxonomy -> get_db_identifier (),
'name' => 'first term from my tax' ,
),
true
);
2021-04-06 12:51:26 +00:00
$term2 = $this -> tainacan_entity_factory -> create_entity (
'term' ,
array (
'taxonomy' => $taxonomy -> get_db_identifier (),
'name' => 'Second term from my tax' ,
),
true
);
2021-04-06 12:41:32 +00:00
$taxonomy_meta = $this -> tainacan_entity_factory -> create_entity (
2021-04-06 11:40:05 +00:00
'metadatum' ,
array (
'name' => 'My tax meta' ,
'status' => 'publish' ,
'collection' => $this -> collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy' ,
'metadata_type_options' => [
'taxonomy_id' => $taxonomy -> get_id (),
]
),
true
);
2021-04-06 12:41:32 +00:00
2021-04-08 02:16:56 +00:00
$item_taxonomy_metadata = new Item_Metadata_Entity ( $this -> item , $taxonomy_meta );
2021-04-06 12:51:26 +00:00
$item_taxonomy_metadata -> set_value ( '' );
$item_taxonomy_metadata -> validate ();
$this -> assertEmpty ( $item_taxonomy_metadata -> get_value_as_html ());
2021-04-06 12:41:32 +00:00
$item_taxonomy_metadata -> set_value ( $term );
$item_taxonomy_metadata -> validate ();
2021-04-09 14:27:22 +00:00
$term_id = $term -> get_id ();
2021-04-09 15:00:44 +00:00
$link = get_term_link ( ( int ) $term_id );
$this -> assertEquals ( $item_taxonomy_metadata -> get_value_as_html (), " <a data-linkto='term' data-id=' { $term_id } ' href=' { $link } '>first term from my tax</a> " );
2021-04-06 12:51:26 +00:00
$taxonomy_meta -> set_multiple ( 'yes' );
$item_taxonomy_metadata -> set_value ([ $term , $term2 ]);
$item_taxonomy_metadata -> validate ();
2021-04-09 14:27:22 +00:00
$term_id = $term -> get_id ();
$term_id2 = $term2 -> get_id ();
2021-04-09 15:00:44 +00:00
$link = get_term_link ( ( int ) $term_id );
$link2 = get_term_link ( ( int ) $term_id2 );
$this -> assertEquals ( $item_taxonomy_metadata -> get_value_as_html (), " <a data-linkto='term' data-id=' { $term_id } ' href=' { $link } '>first term from my tax</a><span class= \" multivalue-separator \" > | </span><a data-linkto='term' data-id=' { $term_id2 } ' href=' { $link2 } '>Second term from my tax</a> " );
2021-04-06 12:51:26 +00:00
}
2021-04-08 01:32:17 +00:00
function test_compound_metadata_html () {
2021-04-08 02:16:56 +00:00
$compound_meta = $this -> tainacan_entity_factory -> create_entity (
2021-04-08 01:32:17 +00:00
'metadatum' ,
array (
'name' => 'My compound Meta' ,
'status' => 'publish' ,
'collection' => $this -> collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Compound' ,
),
true
);
$text_meta = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'My Text meta' ,
'status' => 'publish' ,
'collection' => $this -> collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Text' ,
2021-04-08 02:16:56 +00:00
'parent' => $compound_meta -> get_id ()
2021-04-08 01:32:17 +00:00
),
true
);
$textarea_meta = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'My Textarea meta!' ,
'status' => 'publish' ,
'collection' => $this -> collection ,
'metadata_type' => 'Tainacan\Metadata_Types\Textarea' ,
2021-04-08 02:16:56 +00:00
'parent' => $compound_meta -> get_id ()
2021-04-08 01:32:17 +00:00
),
true
);
2021-04-08 02:16:56 +00:00
$item_compound_metadata = new Item_Metadata_Entity ( $this -> item , $compound_meta );
2021-04-08 01:32:17 +00:00
$item_compound_metadata -> set_value ([ 'invalid meta value!' ]);
$this -> assertEquals ( $item_compound_metadata -> get_value_as_html (), " <div class='tainacan-compound-group'> </div> " );
}
2021-04-06 12:51:26 +00:00
private function relationship_expected_return ( $id , $title ) {
$URL = get_permalink ( $id );
return " <a data-linkto='item' data-id=' ${ id } ' href=' ${ URL}'>${title } </a> " ;
2021-04-06 11:40:05 +00:00
}
2021-09-01 17:47:20 +00:00
function test_multiple_with_cardinality () {
// Multiple numeric metadata
$metadatum_numeric_multiple = $this -> tainacan_entity_factory -> create_entity (
'metadatum' ,
array (
'name' => 'Numeric important meta' ,
'description' => 'and its description' ,
'collection_id' => $this -> collection -> get_id (),
'metadata_type' => 'Tainacan\Metadata_Types\Numeric' ,
'status' => 'publish' ,
'multiple' => 'yes' ,
'cardinality' => 3
),
true
);
$item_metadata_numeric_mult = new Item_Metadata_Entity ( $this -> item , $metadatum_numeric_multiple );
$item_metadata_numeric_mult -> set_value ([ 10 , 22 , 4 ]);
$ok = $item_metadata_numeric_mult -> validate ();
$this -> assertTrue ( $ok );
$item_metadata_numeric_mult -> set_value ([ 10 , 22 , 4 , 4 ]);
$fail = $item_metadata_numeric_mult -> validate ();
$this -> assertFalse ( $fail );
}
2017-11-12 23:14:47 +00:00
}