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, '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 ); $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); $item = $Tainacan_Items->fetch($i->get_id()); $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test); $item_metadata->set_value('teste_value'); $item_metadata->validate(); $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 \Tainacan\Entities\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 ); $i2 = $this->tainacan_entity_factory->create_entity( 'item', array( 'title' => 'other item', 'description' => 'adasdasdsa', 'collection' => $collection, 'status' => 'publish' ), true ); $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); $value = 'teste_val'; $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum); $item_metadata->set_value($value); $this->assertTrue($item_metadata->validate()); $item_metadata->validate(); $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata); $n_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum); $n_item_metadata->set_value($value); $this->assertTrue($n_item_metadata->validate(), 'trying to validate the same item with same value should be ok'); $n_item_metadata2 = new \Tainacan\Entities\Item_Metadata_Entity($i2, $metadatum); $n_item_metadata2->set_value($value); $this->assertFalse($n_item_metadata2->validate(), 'Collection key should not validate another item metadatada with the same value'); } 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) ); } function test_metadata_text_textarea() { $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_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 ); $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_text = 'GOOGLE: www.google.com'; $item_metadata_text = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_text); $item_metadata_text->set_value($value_text); $value_textarea = 'GOOGLE: www.google.com \n GOOGLE: https://www.google.com'; $item_metadata_textarea = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_textarea); $item_metadata_textarea->set_value($value_textarea); $response_text = 'GOOGLE: www.google.com'; $response_textarea = 'GOOGLE: www.google.com \n GOOGLE: https://www.google.com'; $this->assertEquals($item_metadata_text->get_value_as_html(), $response_text); $this->assertEquals($item_metadata_textarea->get_value_as_html(), $response_textarea); // Poor HTML entry tests $malformatted_HTML = "

I started my content

and make something else here
without closing its HTML properly"; $item_metadata_text->set_value($malformatted_HTML); $item_metadata_textarea->set_value($malformatted_HTML); $this->assertEquals($item_metadata_text->get_value_as_html(), $malformatted_HTML ."

"); $this->assertEquals($item_metadata_textarea->get_value_as_html(), $malformatted_HTML ."

"); } /** * @group test_item_metadata_has_value */ function test_item_metadata_has_value() { $test_value = 'has_value'; $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 \Tainacan\Entities\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 \Tainacan\Entities\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()); $item_metadata_text->set_value([ $test_value ]); $item_metadata_text->validate(); $item_metadata_text = $Tainacan_Item_Metadata->insert($item_metadata_text); $this->assertTrue($item_metadata_text->has_value()); } function test_metadata_numeric_html() { $collection = $this->tainacan_entity_factory->create_entity('collection', ['name' => 'My Collection'], true); $item = $this->tainacan_entity_factory->create_entity( 'item', array( 'title' => 'My test item', 'description' => 'item description', 'collection' => $collection, 'status' => 'publish' ), true ); // Simple numeric metadata $metadatum_numeric = $this->tainacan_entity_factory->create_entity( 'metadatum', array( 'name' => 'Numeric important meta', 'description' => 'and its description', 'collection_id' => $collection->get_id(), 'metadata_type' => 'Tainacan\Metadata_Types\Numeric', 'status' => 'publish' ), true ); // Multiple numeric metadata $metadatum_numeric_multiple = $this->tainacan_entity_factory->create_entity( 'metadatum', array( 'name' => 'Numeric important meta', 'description' => 'and its description', 'collection_id' => $collection->get_id(), 'metadata_type' => 'Tainacan\Metadata_Types\Numeric', 'status' => 'publish', 'multiple' => 'yes' ), true ); $item_metadata_numeric = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadatum_numeric); $item_metadata_numeric->set_value(10); $item_metadata_numeric->validate(); $this->assertEquals($item_metadata_numeric->get_value_as_html(), 10); $item_metadata_numeric_mult = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadatum_numeric_multiple); $item_metadata_numeric_mult->set_value([10,22,4]); $item_metadata_numeric_mult->validate(); $this->assertEquals($item_metadata_numeric_mult->get_value_as_html(), '10 | 22 | 4'); } function test_metadata_date_html() { $collection = $this->tainacan_entity_factory->create_entity('collection', ['name' => 'My Collection'], true); $item = $this->tainacan_entity_factory->create_entity( 'item', array( 'title' => 'My test item', 'description' => 'item description', 'collection' => $collection, 'status' => 'publish' ), true ); // Simple date metadata $metadatum_date = $this->tainacan_entity_factory->create_entity( 'metadatum', array( 'name' => 'Date important meta', 'description' => 'and its description', 'collection_id' => $collection->get_id(), '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', 'collection_id' => $collection->get_id(), 'metadata_type' => 'Tainacan\Metadata_Types\Date', 'status' => 'publish', 'multiple' => 'yes' ), true ); $item_metadata_date = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadatum_date); // 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"); $item_metadata_date_mult = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadatum_date_multiple); // Invalid date values $item_metadata_date_mult->set_value([10,22,4]); $item_metadata_date_mult->validate(); $this->assertEquals($item_metadata_date_mult->get_value_as_html(), ' | | '); $item_metadata_date_mult->set_value(["2021-04-05", "2021-12-30"]); $item_metadata_date_mult->validate(); $this->assertEquals($item_metadata_date_mult->get_value_as_html(), 'April 5, 2021 | December 30, 2021'); } }