From 6fd5aa31b4fb44412fb79c629b72f129d7db61ca Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Thu, 8 Feb 2018 16:28:30 -0200 Subject: [PATCH] item metadata creation and testing improved --- ...tainacan-rest-item-metadata-controller.php | 2 +- src/classes/entities/class-tainacan-item.php | 56 +++---------------- .../repositories/class-tainacan-items.php | 8 --- tests/bootstrap.php | 1 + .../class-tainacan-entity-factory.php | 24 +++----- .../class-tainacan-item-metadata-factory.php | 24 ++++++++ tests/tainacan-unit-test-case.php | 1 + tests/test-items.php | 47 +++++++--------- tests/test-objects.php | 8 +-- 9 files changed, 68 insertions(+), 103 deletions(-) create mode 100644 tests/factories/class-tainacan-item-metadata-factory.php diff --git a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php index cfbddd99b..c39233949 100644 --- a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php @@ -104,7 +104,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller { $item = new Entities\Item($item_id); - $item_metadata = $this->item_metadata_repository->fetch($item, 'OBJECT'); + $item_metadata = $item->get_fields(); $prepared_item = $this->prepare_item_for_response($item_metadata, $request); diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index a04cc468b..3b9302fe5 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -165,58 +165,20 @@ class Item extends Entity { } /** - * Return a Field or a List of Field + * Return a List of ItemMetadata objects * - * @return array || Field + * It will return all fields associeated with the collection this item is part of. + * + * If the item already has a value for any of the fields, it will be available. + * + * @return array Array of ItemMetadata objects */ function get_fields() { - global $Tainacan_Fields; + global $Tainacan_Item_Metadata; + return $Tainacan_Item_Metadata->fetch($this, 'OBJECT'); - if (isset($this->field)) - return $this->field; - - $collection = $this->get_collection(); - $all_metadata = []; - if ($collection) { - $meta_list = $Tainacan_Fields->fetch_by_collection( $collection, [], 'OBJECT' ); - - foreach ($meta_list as $meta) { - $all_metadata[$meta->get_id()] = new Item_Metadata_Entity($this, $meta); - } - } - return $all_metadata; } - - /** - * Define the Field - * - * @param Field $new_metadata - * @param [string || integer || array] $value - * @return void - */ - function add_metadata(Field $new_metadata, $value) { - - //TODO Multiple field must receive an array as value - $item_metadata = new Item_Metadata_Entity($this, $new_metadata); - - $item_metadata->set_value($value); - - $current_meta = $this->get_fields(); - $current_meta[$new_metadata->get_id()] = $item_metadata; - - $this->set_metadata($current_meta); - } - - /** - * Aux function for @method add_metadata - * - * @param array $field - * @return void - */ - function set_metadata(Array $field) { - $this->field = $field; - } - + /** * set meta cap object */ diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 1ec0b445d..19f03e3ae 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -150,14 +150,6 @@ class Items extends Repository { } } - // save field - $field = $item->get_fields(); - global $Tainacan_Item_Metadata; - - foreach ($field as $meta) { - $Tainacan_Item_Metadata->insert($meta); - } - do_action('tainacan-insert', $item); do_action('tainacan-insert-Item', $item); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 12d467ecc..7623551d2 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -31,6 +31,7 @@ require $_tests_dir . '/includes/bootstrap.php'; require_once(__DIR__ . '/factories/class-tainacan-entity-factory.php'); require_once(__DIR__ . '/factories/class-tainacan-field-factory.php'); require_once(__DIR__ . '/factories/class-tainacan-filter-factory.php'); +require_once(__DIR__ . '/factories/class-tainacan-item-metadata-factory.php'); require_once(__DIR__ . '/tainacan-unit-test-case.php'); require_once(__DIR__ . '/tainacan-unit-api-test-case.php'); diff --git a/tests/factories/class-tainacan-entity-factory.php b/tests/factories/class-tainacan-entity-factory.php index e3c27474a..a9964a4c2 100644 --- a/tests/factories/class-tainacan-entity-factory.php +++ b/tests/factories/class-tainacan-entity-factory.php @@ -31,7 +31,9 @@ class Entity_Factory { */ public function create_entity($type, $args = [], $is_validated_and_in_db = false, $publish = false){ ini_set('display_errors', 1); - + + global $Tainacan_Item_Metadata; + try { if(empty($type)){ throw new \InvalidArgumentException('The type can\'t be empty'); @@ -61,14 +63,8 @@ class Entity_Factory { if (!empty($args) && $is_validated_and_in_db) { foreach ($args as $attribute => $content) { - if($attribute == 'add_metadata'){ - foreach ($content as $in){ - $this->entity->$attribute($in[0], $in[1]); - } - } else { - $set_ = 'set_' . $attribute; - $this->entity->$set_( $content ); - } + $set_ = 'set_' . $attribute; + $this->entity->$set_( $content ); } if ($this->entity->validate()) { @@ -79,14 +75,8 @@ class Entity_Factory { } elseif (!empty($args) && !$is_validated_and_in_db){ foreach ($args as $attribute => $content) { - if($attribute == 'add_metadata'){ - foreach ($content as $in){ - $this->entity->$attribute($in[0], $in[1]); - } - } else { - $set_ = 'set_' . $attribute; - $this->entity->$set_( $content ); - } + $set_ = 'set_' . $attribute; + $this->entity->$set_( $content ); } } elseif (empty($args) && !$is_validated_and_in_db) { diff --git a/tests/factories/class-tainacan-item-metadata-factory.php b/tests/factories/class-tainacan-item-metadata-factory.php new file mode 100644 index 000000000..0fa2b75af --- /dev/null +++ b/tests/factories/class-tainacan-item-metadata-factory.php @@ -0,0 +1,24 @@ +set_value($value); + + if ($item_metadata->validate()) { + $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata); + } + + return $item_metadata; // If not validated, get_error() method should return the errors. Its up to the tests to use it or not + + } +} + +?> \ No newline at end of file diff --git a/tests/tainacan-unit-test-case.php b/tests/tainacan-unit-test-case.php index aa325b756..68095debe 100644 --- a/tests/tainacan-unit-test-case.php +++ b/tests/tainacan-unit-test-case.php @@ -15,6 +15,7 @@ class TAINACAN_UnitTestCase extends \WP_UnitTestCase { $this->tainacan_entity_factory = new Factories\Entity_Factory(); $this->tainacan_field_factory = new Factories\Field_Factory(); $this->tainacan_filter_factory = new Factories\Filter_Factory(); + $this->tainacan_item_metadata_factory = new Factories\Item_Metadata_Factory(); $new_admin_user = $this->factory()->user->create(array( 'role' => 'administrator' )); wp_set_current_user($new_admin_user); diff --git a/tests/test-items.php b/tests/test-items.php index 1a3e25b4c..6a34ea960 100644 --- a/tests/test-items.php +++ b/tests/test-items.php @@ -134,62 +134,57 @@ class Items extends TAINACAN_UnitTestCase { array( 'title' => 'orange', 'collection' => $collection, - 'add_metadata' => [ - [$field, 'value_1'] - ], 'status' => 'publish' ), true ); - + + $this->tainacan_item_metadata_factory->create_item_metadata($i, $field, 'value_1'); + $item = $Tainacan_Items->fetch($i->get_id()); - $meta_test = $item->get_fields(); - $this->assertTrue( isset($meta_test[$field->get_id()]) ); - $this->assertTrue( $meta_test[$field->get_id()] instanceof Entities\Item_Metadata_Entity ); - $this->assertEquals( 'value_1', $meta_test[$field->get_id()]->get_value()); + $meta_test = new Entities\Item_Metadata_Entity($item, $field); + $this->assertTrue( $meta_test instanceof Entities\Item_Metadata_Entity ); + $this->assertEquals( $field->get_id(), $meta_test->get_field()->get_id() ); + $this->assertEquals( 'value_1', $meta_test->get_value()); - $this->tainacan_entity_factory->create_entity( + $i = $this->tainacan_entity_factory->create_entity( 'item', array( 'title' => 'apple', 'collection' => $collection2, - 'add_metadata' => [ - [$field2, 'value_2'], - [$field3, 'value_2'] - ], 'status' => 'publish' ), true ); - - $this->tainacan_entity_factory->create_entity( + + $this->tainacan_item_metadata_factory->create_item_metadata($i, $field3, 'value_2'); + + $i = $this->tainacan_entity_factory->create_entity( 'item', array( 'title' => 'lemon', 'collection' => $collection2, - 'add_metadata' => [ - [$field2, 'value_2'], - [$field2, 'value_3'], - [$field3, 'value_3'] - ], 'status' => 'publish' ), true ); + + $this->tainacan_item_metadata_factory->create_item_metadata($i, $field2, 'value_2'); + $this->tainacan_item_metadata_factory->create_item_metadata($i, $field2, 'value_3'); + $this->tainacan_item_metadata_factory->create_item_metadata($i, $field3, 'value_3'); - $this->tainacan_entity_factory->create_entity( + $i = $this->tainacan_entity_factory->create_entity( 'item', array( 'title' => 'pineapple', 'collection' => $collection2, - 'add_metadata' => [ - [$field2, 'value_3'], - [$field3, 'value_6'] - ], 'status' => 'publish' ), true ); + + $this->tainacan_item_metadata_factory->create_item_metadata($i, $field2, 'value_3'); + $this->tainacan_item_metadata_factory->create_item_metadata($i, $field3, 'value_6'); // should return all 4 items $test_query = $Tainacan_Items->fetch([]); @@ -251,7 +246,7 @@ class Items extends TAINACAN_UnitTestCase { ], $collection2); $this->assertEquals(2, $test_query->post_count); - // should return 2 item + // should return 2 items $test_query = $Tainacan_Items->fetch([ 'meta_query' => [ [ diff --git a/tests/test-objects.php b/tests/test-objects.php index 7e4dfa0fa..da0e2e71b 100644 --- a/tests/test-objects.php +++ b/tests/test-objects.php @@ -87,13 +87,13 @@ class Objects extends TAINACAN_UnitTestCase { array( 'title' => 'orange', 'collection' => $collection, - 'add_metadata' => [ - [$field, 'value_1'] - ] ), true ); - $test = get_post($i->get_id()); + + $this->tainacan_item_metadata_factory->create_item_metadata($i, $field, 'value_1'); + + $test = get_post($i->get_id()); $entity = Repository::get_entity_by_post($test); $this->assertEquals($i->get_db_identifier(), $entity->get_db_identifier());