diff --git a/build-watch.sh b/build-watch.sh index 6df90f65b..c1e355644 100755 --- a/build-watch.sh +++ b/build-watch.sh @@ -1,6 +1,5 @@ #!/bin/bash -## Run the build script whenever there is a change in src folder - +## Runs build script whenever there is a change in src folder echo 'Watching changes on src/' diff --git a/build.sh b/build.sh index 0217b9435..3408e8c45 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,6 @@ #!/bin/bash +# Loads user-defined variables at build-config file source build-config.cfg ## Only run npm build if there was a change in a .js or .vue file diff --git a/compile-sass.sh b/compile-sass.sh index 0709b407a..2f7ef2022 100644 --- a/compile-sass.sh +++ b/compile-sass.sh @@ -5,8 +5,7 @@ command -v sass >/dev/null 2>&1 || { echo >&2 "Sass parece não estar disponivel."; exit 1; } - -# Define o caminho. + echo "Compilando Sass..." sass -E 'UTF-8' --cache-location .tmp/sass-cache-1 src/views/admin/scss/tainacan-admin.scss:src/assets/css/tainacan-admin.css diff --git a/src/classes/api/endpoints/class-tainacan-rest-background-processes-controller.php b/src/classes/api/endpoints/class-tainacan-rest-background-processes-controller.php index c3131186c..30405f303 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-background-processes-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-background-processes-controller.php @@ -231,7 +231,6 @@ class REST_Background_Processes_Controller extends REST_Controller { $result = $this->prepare_item_for_response($result, $request); return new \WP_REST_Response( $result, 200 ); - } public function prepare_item_for_response($item, $request) { @@ -309,8 +308,6 @@ class REST_Background_Processes_Controller extends REST_Controller { $result = $this->prepare_item_for_response($result, $request); return new \WP_REST_Response( $result, 200 ); - - } public function delete_item( $request ) { @@ -334,7 +331,6 @@ class REST_Background_Processes_Controller extends REST_Controller { // TODO: delete log files return new \WP_REST_Response( $result, 200 ); - } public function get_log_url($id, $action, $type = '') { @@ -352,7 +348,6 @@ class REST_Background_Processes_Controller extends REST_Controller { $logs_url = $upload_url . 'tainacan/' . $filename; return $logs_url; - } } diff --git a/src/classes/entities/class-tainacan-item-metadata-entity.php b/src/classes/entities/class-tainacan-item-metadata-entity.php index 442d122d3..9c87479d9 100644 --- a/src/classes/entities/class-tainacan-item-metadata-entity.php +++ b/src/classes/entities/class-tainacan-item-metadata-entity.php @@ -31,7 +31,6 @@ class Item_Metadata_Entity extends Entity { * @param int $meta_id ID for a specific meta row */ function __construct(Item $item = null, Metadatum $metadatum = null, $meta_id = null, $parent_meta_id = null) { - $this->set_item($item); $this->set_metadatum($metadatum); @@ -136,17 +135,15 @@ class Item_Metadata_Entity extends Entity { * Get the value as a HTML string, with markup and links * @return string */ - public function get_value_as_html(){ + public function get_value_as_html() { $metadatum = $this->get_metadatum(); if (is_object($metadatum)) { $fto = $metadatum->get_metadata_type_object(); if (is_object($fto)) { - if ( method_exists($fto, 'get_value_as_html') ) { return $fto->get_value_as_html($this); } - } } @@ -155,7 +152,6 @@ class Item_Metadata_Entity extends Entity { $return = ''; if ( $this->is_multiple() ) { - $total = sizeof($value); $count = 0; $prefix = $this->get_multivalue_prefix(); @@ -163,25 +159,19 @@ class Item_Metadata_Entity extends Entity { $separator = $this->get_multivalue_separator(); foreach ($value as $v) { - $return .= $prefix; - $return .= (string) $v; - $return .= $suffix; - $count ++; if ($count < $total) $return .= $separator; } - + } else { $return = (string) $value; } return $return; - - } /** diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index ecc5f6d41..79c14d659 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -500,26 +500,20 @@ class Item extends Entity { public function _toHtml() { - $return = ''; $id = $this->get_id(); if ( $id ) { - $link = get_permalink( (int) $id ); if (is_string($link)) { - $return = ""; $return.= $this->get_title(); $return .= ""; - } - } return $return; - } /** diff --git a/src/classes/entities/class-tainacan-term.php b/src/classes/entities/class-tainacan-term.php index 0241c4fb2..7d4b55f2e 100644 --- a/src/classes/entities/class-tainacan-term.php +++ b/src/classes/entities/class-tainacan-term.php @@ -245,25 +245,19 @@ class Term extends Entity { } public function _toHtml() { - $return = ''; $id = $this->get_id(); if ( $id ) { - $link = get_term_link( (int) $id ); if (is_string($link)) { - $return = ""; $return.= $this->get_name(); $return .= ""; - } - } - return apply_filters('tainacan-term-to-html', $return, $this); - + return apply_filters('tainacan-term-to-html', $return, $this); } } diff --git a/src/readme.txt b/src/readme.txt index b8cccfaef..e465e24c2 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -1,5 +1,5 @@ === Tainacan === -Contributors: andrebenedito, daltonmartins, fabianobn, jacsonp, leogermani, weryques, wetah, eduardohumberto, ravipassos, jessicafpx, marinagiolo, omarceloavila, vnmedeiros, tainacan +Contributors: andrebenedito, daltonmartins, fabianobn, jacsonp, leogermani, weryques, wetah, eduardohumberto, ravipassos, jessicafpx, marinagiolo, omarceloavila, vnmedeiros, tainacan, r-guimaraes Tags: museums, libraries, archives, GLAM, collections, repository Requires at least: 4.8 Tested up to: 5.6 diff --git a/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php b/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php index 1669efce5..a21b8467f 100644 --- a/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php +++ b/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php @@ -196,35 +196,45 @@ class Compound extends Metadata_Type { foreach ( $compound_element as $meta_id => $meta ) { $index = array_search( $meta_id, array_column( $order, 'id' ) ); if ( $meta instanceof Item_Metadata_Entity && $meta->get_value_as_html() != '' ) { + $html = $this->get_meta_html($meta); if ( $index !== false ) { - $metadata_value[$index] = '

' . $meta->get_value_as_html() . "

"; + $metadata_value[$index] = $html; } else { - $metadata_value_not_ordinate[] = '

' . $meta->get_value_as_html() . "

"; + $metadata_value_not_ordinate[] = $html; } } } $elements[] = '
' . implode("\n", array_merge($metadata_value, $metadata_value_not_ordinate)) . "
\n" ; } } - $return = '
' . implode($separator, $elements) . '
'; + $return = implode($separator, $elements); } else { - $metadata_value = array_fill(0, count($value), null); + $metadata_value = array_fill(0, count($value), null); $metadata_value_not_ordinate = []; foreach ( $value as $meta_id => $meta ) { $index = array_search( $meta_id, array_column( $order, 'id' ) ); if ( $meta instanceof Item_Metadata_Entity && $meta->get_value_as_html() != '' ) { + $html = $this->get_meta_html($meta); if ( $index !== false ) { - $metadata_value[intval($index)] = '

' . $meta->get_value_as_html() . "

"; + $metadata_value[intval($index)] = $html; } else { - $metadata_value_not_ordinate[] = '

' . $meta->get_value_as_html() . "

"; + $metadata_value_not_ordinate[] = $html; } - } + } } - $return = '
' . implode("\n", array_merge($metadata_value, $metadata_value_not_ordinate)) . "
\n"; + $return = implode("\n", array_merge($metadata_value, $metadata_value_not_ordinate)); } - return $return; - + return "
{$return}
"; + } + + private function get_meta_html(Item_Metadata_Entity $meta) { + $html = ''; + if ($meta instanceof Item_Metadata_Entity && !empty($meta->get_value_as_html())) { + $html = '

' . $meta->get_value_as_html() . "

"; + } + + return $html; } } \ No newline at end of file diff --git a/src/views/admin/components/metadata-types/date/class-tainacan-date.php b/src/views/admin/components/metadata-types/date/class-tainacan-date.php index ed482a5a4..ccb1674c1 100644 --- a/src/views/admin/components/metadata-types/date/class-tainacan-date.php +++ b/src/views/admin/components/metadata-types/date/class-tainacan-date.php @@ -11,7 +11,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); */ class Date extends Metadata_Type { - function __construct(){ + function __construct() { // call metadatum type constructor parent::__construct(); $this->set_primitive_type('date'); @@ -25,7 +25,6 @@ class Date extends Metadata_Type { '); - $this->output_date_format = get_option('date_format'); } @@ -37,31 +36,19 @@ class Date extends Metadata_Type { foreach ($value as $date_value) { $d = \DateTime::createFromFormat($format, $date_value); if (!$d || $d->format($format) !== $date_value) { - $this->add_error( - sprintf( - __('Invalid date format. Expected format is YYYY-MM-DD, got %s.', 'tainacan'), - $date_value - ) - ); + $this->add_error($this->format_error_msg($date_value)); return false; } } - return True; + return true; } - + $d = \DateTime::createFromFormat($format, $value); - if (!$d || $d->format($format) !== $value) { - $this->add_error( - sprintf( - __('Invalid date format. Expected format is YYYY-MM-DD, got %s.', 'tainacan'), - $value - ) - ); + $this->add_error($this->format_error_msg($value)); return false; } return true; - } /** @@ -82,20 +69,30 @@ class Date extends Metadata_Type { if( empty( $el ) ) continue; $return .= $prefix; - $return .= mysql2date($this->output_date_format, ($el)); + $return .= $this->format_date_value($el); $return .= $suffix; $count ++; if ($count < $total) $return .= $separator; } } else { - if( empty( $value ) ) - return ""; - $return = mysql2date($this->output_date_format, ($value)); + $return = $this->format_date_value($value); } + return $return; - } + private function format_date_value($value) { + if (empty($value)) + return ""; + return mysql2date($this->output_date_format, ($value)); + } + + private function format_error_msg($value) { + return sprintf( + __('Invalid date format. Expected format is MM/DD/YYYY, got %s.', 'tainacan'), + $value + ); + } } \ No newline at end of file diff --git a/src/views/admin/components/metadata-types/numeric/class-tainacan-numeric.php b/src/views/admin/components/metadata-types/numeric/class-tainacan-numeric.php index cc496cab0..4ad0584e7 100644 --- a/src/views/admin/components/metadata-types/numeric/class-tainacan-numeric.php +++ b/src/views/admin/components/metadata-types/numeric/class-tainacan-numeric.php @@ -37,5 +37,4 @@ class Numeric extends Metadata_Type { ] ]; } - } \ No newline at end of file diff --git a/src/views/admin/components/metadata-types/relationship/class-tainacan-relationship.php b/src/views/admin/components/metadata-types/relationship/class-tainacan-relationship.php index 9c4680e77..13dcc0622 100644 --- a/src/views/admin/components/metadata-types/relationship/class-tainacan-relationship.php +++ b/src/views/admin/components/metadata-types/relationship/class-tainacan-relationship.php @@ -157,14 +157,11 @@ class Relationship extends Metadata_Type { * @param Item_Metadata_Entity $item_metadata * @return string The HTML representation of the value, containing one or multiple items names, linked to the item page */ - public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { - + public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { $value = $item_metadata->get_value(); $return = ''; - if ( $item_metadata->is_multiple() ) { - $count = 1; $total = sizeof($value); $prefix = $item_metadata->get_multivalue_prefix(); @@ -172,66 +169,47 @@ class Relationship extends Metadata_Type { $separator = $item_metadata->get_multivalue_separator(); foreach ( $value as $item_id ) { - try { - - //$item = new \Tainacan\Entities\Item($item_id); $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); $item = $Tainacan_Items->fetch( (int) $item_id); - - $count ++; + $count++; if ( $item instanceof \Tainacan\Entities\Item ) { - $return .= $prefix; - $return .= $this->get_item_html($item); - $return .= $suffix; - + if ( $count <= $total ) { $return .= $separator; } - } - - + } catch (\Exception $e) { // item not found } - } - } else { - try { - $item = new \Tainacan\Entities\Item($value); - if ( $item instanceof \Tainacan\Entities\Item ) { $return .= $this->get_item_html($item); } - } catch (\Exception $e) { // item not found } - } - + return $return; - } private function get_item_html($item) { - $return = ''; $id = $item->get_id(); $search_meta_id = $this->get_option('search'); if ( $id && $search_meta_id ) { - $link = get_permalink( (int) $id ); $search_meta_id = $this->get_option('search'); @@ -250,17 +228,13 @@ class Relationship extends Metadata_Type { } if (is_string($link)) { - $return = ""; $return.= $label; $return .= ""; - } - } return $return; - } /** @@ -268,7 +242,6 @@ class Relationship extends Metadata_Type { * @return \Tainacan\Entities\Collection|false The Collection object or false */ public function get_collection() { - $collection_id = $this->get_option('collection_id'); if ( is_numeric($collection_id) ) { @@ -279,7 +252,6 @@ class Relationship extends Metadata_Type { } return false; - } /** diff --git a/src/views/admin/components/metadata-types/taxonomy/class-tainacan-taxonomy.php b/src/views/admin/components/metadata-types/taxonomy/class-tainacan-taxonomy.php index e46988e1d..d777d08e0 100644 --- a/src/views/admin/components/metadata-types/taxonomy/class-tainacan-taxonomy.php +++ b/src/views/admin/components/metadata-types/taxonomy/class-tainacan-taxonomy.php @@ -323,7 +323,6 @@ class Taxonomy extends Metadata_Type { } return $valid; - } /** @@ -332,13 +331,10 @@ class Taxonomy extends Metadata_Type { * @return string The HTML representation of the value, containing one or multiple terms, separated by comma, linked to term page */ public function get_value_as_html(Item_Metadata_Entity $item_metadata) { - - $value = $item_metadata->get_value(); - + $value = $item_metadata->get_value(); $return = ''; if ( $item_metadata->is_multiple() ) { - $count = 1; $total = sizeof($value); $prefix = $item_metadata->get_multivalue_prefix(); @@ -346,8 +342,7 @@ class Taxonomy extends Metadata_Type { $separator = $item_metadata->get_multivalue_separator(); foreach ( $value as $term ) { - - $count ++; + $count++; if ( is_integer($term) ) { $term = \Tainacan\Repositories\Terms::get_instance()->fetch($term, $this->get_option('taxonomy_id')); @@ -355,35 +350,25 @@ class Taxonomy extends Metadata_Type { if ( $term instanceof \Tainacan\Entities\Term ) { $return .= $prefix; - $return .= $this->get_term_hierarchy_html($term); - $return .= $suffix; if ( $count <= $total ) { $return .= $separator; } - } - } - } else { - if ( $value instanceof \Tainacan\Entities\Term ) { $return .= $this->get_term_hierarchy_html($value); } - } return $return; - } private function get_term_hierarchy_html( \Tainacan\Entities\Term $term ) { - $terms = []; - $terms[] = $this->term_to_html($term); while ($term->get_parent() > 0) { @@ -392,11 +377,9 @@ class Taxonomy extends Metadata_Type { } $terms = \array_reverse($terms); - $glue = apply_filters('tainacan-terms-hierarchy-html-separator', ' > '); return \implode($glue, $terms); - } private function term_to_html($term) { diff --git a/src/views/admin/components/metadata-types/text/class-tainacan-text.php b/src/views/admin/components/metadata-types/text/class-tainacan-text.php index 9f577c15d..ea723069f 100644 --- a/src/views/admin/components/metadata-types/text/class-tainacan-text.php +++ b/src/views/admin/components/metadata-types/text/class-tainacan-text.php @@ -50,6 +50,6 @@ class Text extends Metadata_Type { } else { $return = $this->make_clickable_links($value); } - return $return; + return force_balance_tags($return); } } \ No newline at end of file diff --git a/src/views/admin/components/metadata-types/textarea/class-tainacan-textarea.php b/src/views/admin/components/metadata-types/textarea/class-tainacan-textarea.php index 40b3590fb..f8bfee1c2 100644 --- a/src/views/admin/components/metadata-types/textarea/class-tainacan-textarea.php +++ b/src/views/admin/components/metadata-types/textarea/class-tainacan-textarea.php @@ -59,6 +59,6 @@ class Textarea extends Metadata_Type { } else { $return = nl2br($this->make_clickable_links($value)); } - return $return; + return force_balance_tags($return); } } \ No newline at end of file diff --git a/tests/__test-bulkedit.php b/tests/__test-bulkedit.php index 3db4df885..c30e15080 100644 --- a/tests/__test-bulkedit.php +++ b/tests/__test-bulkedit.php @@ -1154,8 +1154,6 @@ class BulkEdit extends TAINACAN_UnitApiTestCase { ]); $this->assertEquals(7, $bulk->count_posts()); - - } /** @@ -1206,8 +1204,6 @@ class BulkEdit extends TAINACAN_UnitApiTestCase { $item_id = $bulk->get_item_id_by_index(30); $this->assertFalse($item_id); - - } function test_set_multiple_tax_meta() { @@ -1228,14 +1224,8 @@ class BulkEdit extends TAINACAN_UnitApiTestCase { 'collection_id' => $this->collection->get_id() ]); - - - $bulk->set_value($this->category, ['super', 'dooper']); - - - $items = $Tainacan_Items->fetch([ 'tax_query' => [ [ @@ -1342,6 +1332,4 @@ class BulkEdit extends TAINACAN_UnitApiTestCase { $this->assertEquals(5, $items->found_posts); } - - } diff --git a/tests/factories/class-tainacan-entity-factory.php b/tests/factories/class-tainacan-entity-factory.php index 47dab8fd3..fb14717d2 100644 --- a/tests/factories/class-tainacan-entity-factory.php +++ b/tests/factories/class-tainacan-entity-factory.php @@ -10,7 +10,7 @@ class Entity_Factory { * * @var \Tainacan\Entities\Entity */ - private $entity; + private $entity; /** * @@ -118,5 +118,3 @@ class Entity_Factory { return $this->entity; } } - -?> diff --git a/tests/test-bgprocess.php b/tests/test-bgprocess.php index 91f8ac3d7..a70298a99 100644 --- a/tests/test-bgprocess.php +++ b/tests/test-bgprocess.php @@ -16,12 +16,10 @@ use Tainacan\Entities; class BGProcess extends TAINACAN_UnitApiTestCase { function test_table() { - global $wpdb; $table_name = $wpdb->prefix . 'tnc_bg_process'; - $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '".$table_name."'" ); $this->assertNotEquals(null, $table_exists); @@ -29,10 +27,6 @@ class BGProcess extends TAINACAN_UnitApiTestCase { $column_exists = $wpdb->get_var( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '{$wpdb->prefix}tnc_bg_process' AND column_name = 'output'" ); $this->assertEquals('output', $column_exists, 'BG processes table misconfigured'); - } - - - } \ No newline at end of file diff --git a/tests/test-category-metadatum-types.php b/tests/test-category-metadatum-types.php index 8ace43b58..37c7d7bef 100644 --- a/tests/test-category-metadatum-types.php +++ b/tests/test-category-metadatum-types.php @@ -79,10 +79,6 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { true ); - - - - $term = $this->tainacan_entity_factory->create_entity( 'term', array( @@ -237,10 +233,6 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { ); $this->assertContains($tax3->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Taxonommy used by repository level metadatum must be assigned to all collections post types'); - - - - } function test_relate_taxonomy_match() { @@ -794,9 +786,6 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase { $checkMeta = $Tainacan_Metadata->fetch( $metadatum->get_id() ); $this->assertEquals('no', $checkMeta->get_metadata_type_options()['allow_new_terms']); - - - } } diff --git a/tests/test-compound-metadatum-types.php b/tests/test-compound-metadatum-types.php index 14d758374..4cd03302f 100644 --- a/tests/test-compound-metadatum-types.php +++ b/tests/test-compound-metadatum-types.php @@ -8,7 +8,7 @@ namespace Tainacan\Tests; * @package Test_Tainacan */ -use Tainacan\Entities; +use Tainacan\Entities\Item_Metadata_Entity; /** * Compound Metadatum Types test case. @@ -17,10 +17,7 @@ use Tainacan\Entities; class CompoundMetadatumTypes extends TAINACAN_UnitTestCase { function test_compound_metadata_types() { - - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); $collection = $this->tainacan_entity_factory->create_entity( 'collection', @@ -79,23 +76,21 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase { true ); - $item_metadata1 = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_child1); + $item_metadata1 = new Item_Metadata_Entity($i, $metadatum_child1); $item_metadata1->set_value('Red'); $item_metadata1->validate(); $item_metadata1 = $Tainacan_Item_Metadata->insert($item_metadata1); - $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_child2, null, $item_metadata1->get_parent_meta_id()); + $item_metadata = new Item_Metadata_Entity($i, $metadatum_child2, null, $item_metadata1->get_parent_meta_id()); $item_metadata->set_value('Blue'); $item_metadata->validate(); $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata); - $compoundItem = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum); - - global $wpdb; + $compoundItem = new Item_Metadata_Entity($i, $metadatum); $compoundValue = $compoundItem->get_value(); @@ -105,17 +100,15 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase { $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' ); - $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' ); + $this->assertTrue( $compoundValue[$metadatum_child1->get_id()] instanceof Item_Metadata_Entity , 'First element of value should be an item metadata entity' ); + $this->assertTrue( $compoundValue[$metadatum_child2->get_id()] instanceof Item_Metadata_Entity , 'Second element of value should be an item metadata entity' ); $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' ); } function test_multiple_compound_metadata_types() { - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); $collection = $this->tainacan_entity_factory->create_entity( 'collection', @@ -124,7 +117,7 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase { ), true ); - + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -175,34 +168,30 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase { true ); - global $wpdb; - // First Instance - $item_metadata1 = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_child1); + $item_metadata1 = new Item_Metadata_Entity($i, $metadatum_child1); $item_metadata1->set_value('Red'); $item_metadata1->validate(); $item_metadata1 = $Tainacan_Item_Metadata->insert($item_metadata1); - $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_child2, null, $item_metadata1->get_parent_meta_id()); + $item_metadata = new Item_Metadata_Entity($i, $metadatum_child2, null, $item_metadata1->get_parent_meta_id()); $item_metadata->set_value('Blue'); $item_metadata->validate(); $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata); // Second Instance - $item_metadata3 = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_child1); + $item_metadata3 = new Item_Metadata_Entity($i, $metadatum_child1); $item_metadata3->set_value('Green'); $item_metadata3->validate(); $item_metadata3 = $Tainacan_Item_Metadata->insert($item_metadata3); - $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_child2, null, $item_metadata3->get_parent_meta_id()); + $item_metadata = new Item_Metadata_Entity($i, $metadatum_child2, null, $item_metadata3->get_parent_meta_id()); $item_metadata->set_value('Yellow'); $item_metadata->validate(); $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata); - $compoundItem = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum); - //var_dump($wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE post_id = {$i->get_id()}")); - //var_dump($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE parent = {$metadatum->get_id()}")); + $compoundItem = new Item_Metadata_Entity($i, $metadatum); $compoundValue = $compoundItem->get_value(); $this->assertTrue( is_array($compoundValue), 'value of a compound should return array' ); @@ -214,8 +203,8 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase { $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' ); - $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' ); + $this->assertTrue( $compoundValue[0][$metadatum_child1->get_id()] instanceof Item_Metadata_Entity , 'First element of value should be an item metadata entity' ); + $this->assertTrue( $compoundValue[1][$metadatum_child2->get_id()] instanceof Item_Metadata_Entity , 'Second element of value should be an item metadata entity' ); $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' ); @@ -224,7 +213,7 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase { $this->assertEquals( 'Yellow', $compoundValue[1][$metadatum_child2->get_id()]->get_value() , 'Second element of value should have "Blue" value' ); $item_metadata_removed = $Tainacan_Item_Metadata->remove_compound_value($i, $metadatum, $item_metadata3->get_parent_meta_id()); - $compoundItem = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum); + $compoundItem = new Item_Metadata_Entity($i, $metadatum); $compoundValue = $compoundItem->get_value(); $this->assertTrue( is_array($compoundValue), 'value of a compound should return array' ); @@ -232,13 +221,9 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase { $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' ); - } function test_validations_taxonomy_in_multiple() { - - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -308,9 +293,6 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase { } function test_validations_multiple_metadata() { - - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -343,14 +325,10 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase { $newMetadatum->set_multiple('no'); $this->assertTrue($newMetadatum->validate()); - - } - function teste_validations_metadada_order() { - + function test_validations_metadada_order() { $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); - $collection = $this->tainacan_entity_factory->create_entity( 'collection', diff --git a/tests/test-filters.php b/tests/test-filters.php index d21d4ed36..27517e746 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -14,7 +14,7 @@ namespace Tainacan\Tests; class Filters extends TAINACAN_UnitTestCase { - function teste_add(){ + function test_add(){ $Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance(); $collection = $this->tainacan_entity_factory->create_entity( diff --git a/tests/test-item-metadata.php b/tests/test-item-metadata.php index 9badf85e3..47db77b1a 100644 --- a/tests/test-item-metadata.php +++ b/tests/test-item-metadata.php @@ -2,164 +2,187 @@ namespace Tainacan\Tests; +use Tainacan\Entities\Item_Metadata_Entity; + /** - * Class TestCollections + * Class Item_Metadata * * @package Test_Tainacan */ /** - * Sample test case. + * Item_Metadata test case. */ class Item_Metadata extends TAINACAN_UnitTestCase { - /** - * Teste da insercao de um metadado simples sem o tipo - */ - function test_add() { + private $collection = null; + private $item = null; + private $separator = ' | '; - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); + 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', + array( + 'title' => 'My test item', + 'description' => 'item description', + 'collection' => $c, + 'status' => 'publish' + ), + true + ); - $collection = $this->tainacan_entity_factory->create_entity( - 'collection', - array( - 'name' => 'teste', - 'description' => 'No description', - ), - true - ); + $this->collection = $c; + $this->item = $i; + } - $metadatum = $this->tainacan_entity_factory->create_entity( - 'metadatum', - array( - 'name' => 'metadado', - 'description' => 'descricao', - 'collection' => $collection, - 'metadata_type' => 'Tainacan\Metadata_Types\Text', - ), - true - ); + /** + * 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(); - $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()); + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'test', + 'description' => 'No description', + ), + true + ); - $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test); - $item_metadata->set_value('teste_value'); + $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'); $item_metadata->validate(); - $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata); - - $this->assertEquals('teste_value', $item_metadata->get_value()); - } + $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 teste_required(){ - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); + /** + * 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 - ); + $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 - ); + $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 teste_collection_key(){ - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); + $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 - ); + $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 - ); + $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 - ); + $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', @@ -171,131 +194,129 @@ class Item_Metadata extends TAINACAN_UnitTestCase { ), true ); - - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + + $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); + $value = 'teste_val'; + + $item_metadata = new Item_Metadata_Entity($i, $metadatum); + $item_metadata->set_value($value); - $this->assertTrue($item_metadata->validate()); + $this->assertTrue($item_metadata->validate()); - $item_metadata->validate(); - $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata); + $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); + $n_item_metadata = new 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 = new 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 teste_fetch(){ - $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); + } + + 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 - ); + $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 - ); + $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 - ); + //$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'); + $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)); + $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) ); - + // 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() { - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - - $collection = $this->tainacan_entity_factory->create_entity( - 'collection', - array( - 'name' => 'teste' - ), - true + $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 - ); + '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_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 + '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 = new 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 = new Item_Metadata_Entity($i, $metadatum_textarea); $item_metadata_textarea->set_value($value_textarea); $response_text = 'GOOGLE: www.google.com'; @@ -303,90 +324,442 @@ class Item_Metadata extends TAINACAN_UnitTestCase { $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 + $badFormatted_HTML = "

I started my content

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

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

"); } - /** - * @group test_item_metadata_has_value - */ + /** + * @group test_item_metadata_has_value + */ function test_item_metadata_has_value() { - $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); + $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 - ); + $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 - ); + $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 - ); + $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); + $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); + $item_metadata_textarea->validate(); + $item_metadata_textarea = $Tainacan_Item_Metadata->insert($item_metadata_textarea); - $this->assertFalse($item_metadata_textarea->has_value()); + $this->assertFalse($item_metadata_textarea->has_value()); - $item_metadata_textarea->set_value('has_value'); - $item_metadata_textarea->validate(); - $item_metadata_textarea = $Tainacan_Item_Metadata->insert($item_metadata_textarea); + $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()); + $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 - ); + $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 = new Item_Metadata_Entity($i, $metadatum_text); - $item_metadata_text->set_value([ - '' - ]); + $item_metadata_text->set_value([ $value_textarea ]); - $item_metadata_text->validate(); - $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata_text); + $item_metadata_text->validate(); + $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata_text); - $this->assertFalse($item_metadata->has_value()); + $this->assertFalse($item_metadata->has_value()); - $item_metadata_text->set_value([ - 'has_value' - ]); + $item_metadata_text->set_value([ $test_value ]); - $item_metadata_text->validate(); - $item_metadata_text = $Tainacan_Item_Metadata->insert($item_metadata_text); + $item_metadata_text->validate(); + $item_metadata_text = $Tainacan_Item_Metadata->insert($item_metadata_text); - $this->assertTrue($item_metadata_text->has_value()); - } + $this->assertTrue($item_metadata_text->has_value()); + } + + function test_numeric_metadata_html() { + // Simple numeric metadata + $metadatum_numeric = $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' + ), + 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' => $this->collection->get_id(), + 'metadata_type' => 'Tainacan\Metadata_Types\Numeric', + 'status' => 'publish', + 'multiple' => 'yes' + ), + true + ); + + $item_metadata_numeric = new Item_Metadata_Entity($this->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 Item_Metadata_Entity($this->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" . $this->separator . "22" . $this->separator . "4"); + } + + function test_date_metadata_html() { + // Simple date metadata + $metadatum_date = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'Date important meta', + 'description' => 'and its description', + 'collection_id' => $this->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' => $this->collection->get_id(), + 'metadata_type' => 'Tainacan\Metadata_Types\Date', + 'status' => 'publish', + 'multiple' => 'yes' + ), + true + ); + + $item_metadata_date = new Item_Metadata_Entity($this->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 Item_Metadata_Entity($this->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(), $this->separator . $this->separator ); + + $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' . $this->separator . 'December 30, 2021'); + } + + 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 + ); + + $user_meta = new Item_Metadata_Entity($this->item, $user_metadata); + // 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 + ); + $user_meta_multi = new Item_Metadata_Entity($this->item, $user_metadata_multiple); + $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(); + $this->assertEquals($user_meta_multi->get_value_as_html(), 'User Name' . $this->separator . 'User Name 2'); + } + + 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 + ); + + $sb_meta = new Item_Metadata_Entity($this->item, $selectbox_metadata); + $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'); + + $sb_meta_multi = new Item_Metadata_Entity($this->item, $selectbox_metadata_multiple); + $sb_meta_multi->set_value(['tainacan', 'wordpress']); + $sb_meta_multi->validate(); + $this->assertEquals($sb_meta_multi->get_value_as_html(), 'tainacan' . $this->separator . 'wordpress'); + } + + 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 + ); + $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()); + + $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 + ); + + $item_metadata_relationship = new Item_Metadata_Entity($this->item, $relationship_metadata); + $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); + + $item_metadata_relationship->set_value([$this->collection->get_id()]); + $item_metadata_relationship->validate(); + $this->assertEquals($item_metadata_relationship->get_value_as_html(), ''); + + $relationship_metadata->set_multiple('yes'); + $item_metadata_relationship->set_value([ $mystify->get_id(), $disappear->get_id() ]); + $this->assertEquals($item_metadata_relationship->get_value_as_html(), "${expected_return}" . $this->separator . "${expected_return2}"); + } + + function test_taxonomy_metadata_html() { + $taxonomy = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => 'My Taxonomy test', + 'collections' => [$this->collection], + 'status' => 'publish' + ), + true + ); + + $term = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy->get_db_identifier(), + 'name' => 'first term from my tax', + ), + true + ); + + $term2 = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy->get_db_identifier(), + 'name' => 'Second term from my tax', + ), + true + ); + + $taxonomy_meta = $this->tainacan_entity_factory->create_entity( + '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 + ); + + $item_taxonomy_metadata = new Item_Metadata_Entity($this->item, $taxonomy_meta); + $item_taxonomy_metadata->set_value(''); + $item_taxonomy_metadata->validate(); + $this->assertEmpty($item_taxonomy_metadata->get_value_as_html()); + + $item_taxonomy_metadata->set_value($term); + $item_taxonomy_metadata->validate(); + $term_id = $term->get_id(); + $link = get_term_link( (int) $term_id ); + $this->assertEquals($item_taxonomy_metadata->get_value_as_html(), "first term from my tax"); + + $taxonomy_meta->set_multiple('yes'); + $item_taxonomy_metadata->set_value([ $term, $term2 ]); + $item_taxonomy_metadata->validate(); + $term_id = $term->get_id(); + $term_id2 = $term2->get_id(); + $link = get_term_link( (int) $term_id ); + $link2 = get_term_link( (int) $term_id2 ); + $this->assertEquals($item_taxonomy_metadata->get_value_as_html(), "first term from my tax | Second term from my tax"); + } + + function test_compound_metadata_html() { + $compound_meta = $this->tainacan_entity_factory->create_entity( + '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', + 'parent' => $compound_meta->get_id() + ), + 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', + 'parent' => $compound_meta->get_id() + ), + true + ); + $item_compound_metadata = new Item_Metadata_Entity($this->item, $compound_meta); + $item_compound_metadata->set_value(['invalid meta value!']); + $this->assertEquals($item_compound_metadata->get_value_as_html(), "
"); + } + + private function relationship_expected_return($id, $title) { + $URL = get_permalink($id); + + return "${title}"; + } } \ No newline at end of file diff --git a/tests/test-items.php b/tests/test-items.php index 8cae8b3df..fe8def1c4 100644 --- a/tests/test-items.php +++ b/tests/test-items.php @@ -48,7 +48,7 @@ class Items extends TAINACAN_UnitTestCase { $this->assertTrue(current_user_can($collection->get_items_capabilities()->edit_post, $item->get_id()), 'Administrator cannot edit an item!'); } - function teste_query(){ + function test_query(){ $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -246,7 +246,7 @@ class Items extends TAINACAN_UnitTestCase { } - function teste_meta_query_in(){ + function test_meta_query_in(){ $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( diff --git a/tests/test-metadata.php b/tests/test-metadata.php index 43a47d73b..3e0a7ab6f 100644 --- a/tests/test-metadata.php +++ b/tests/test-metadata.php @@ -200,7 +200,6 @@ class Metadata extends TAINACAN_UnitTestCase { $this->assertEquals( 9, sizeof( $Tainacan_Metadata->fetch_metadata_types() ) ); } - /** * */ @@ -426,9 +425,7 @@ class Metadata extends TAINACAN_UnitTestCase { } - function test_relatioship_get_collection_method() { - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - + function test_relationship_get_collection_method() { $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -460,17 +457,16 @@ class Metadata extends TAINACAN_UnitTestCase { ), true ); - + $object = $metadatum->get_metadata_type_object(); $colCheck = $object->get_collection(); $this->assertTrue( $colCheck instanceof \Tainacan\Entities\Collection ); $this->assertEquals($collection2->get_id(), $colCheck->get_id()); - } - public function test_update_taxonomy_cascate_status() { + function test_update_taxonomy_cascate_status() { $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();