item metadata creation and testing improved

This commit is contained in:
Leo Germani 2018-02-08 16:28:30 -02:00
parent c484b33e19
commit 6fd5aa31b4
9 changed files with 68 additions and 103 deletions

View File

@ -104,7 +104,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
$item = new Entities\Item($item_id); $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); $prepared_item = $this->prepare_item_for_response($item_metadata, $request);

View File

@ -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() { 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 * set meta cap object
*/ */

View File

@ -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);
do_action('tainacan-insert-Item', $item); do_action('tainacan-insert-Item', $item);

View File

@ -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-entity-factory.php');
require_once(__DIR__ . '/factories/class-tainacan-field-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-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-test-case.php');
require_once(__DIR__ . '/tainacan-unit-api-test-case.php'); require_once(__DIR__ . '/tainacan-unit-api-test-case.php');

View File

@ -31,7 +31,9 @@ class Entity_Factory {
*/ */
public function create_entity($type, $args = [], $is_validated_and_in_db = false, $publish = false){ public function create_entity($type, $args = [], $is_validated_and_in_db = false, $publish = false){
ini_set('display_errors', 1); ini_set('display_errors', 1);
global $Tainacan_Item_Metadata;
try { try {
if(empty($type)){ if(empty($type)){
throw new \InvalidArgumentException('The type can\'t be empty'); throw new \InvalidArgumentException('The type can\'t be empty');
@ -61,14 +63,8 @@ class Entity_Factory {
if (!empty($args) && $is_validated_and_in_db) { if (!empty($args) && $is_validated_and_in_db) {
foreach ($args as $attribute => $content) { foreach ($args as $attribute => $content) {
if($attribute == 'add_metadata'){ $set_ = 'set_' . $attribute;
foreach ($content as $in){ $this->entity->$set_( $content );
$this->entity->$attribute($in[0], $in[1]);
}
} else {
$set_ = 'set_' . $attribute;
$this->entity->$set_( $content );
}
} }
if ($this->entity->validate()) { if ($this->entity->validate()) {
@ -79,14 +75,8 @@ class Entity_Factory {
} elseif (!empty($args) && !$is_validated_and_in_db){ } elseif (!empty($args) && !$is_validated_and_in_db){
foreach ($args as $attribute => $content) { foreach ($args as $attribute => $content) {
if($attribute == 'add_metadata'){ $set_ = 'set_' . $attribute;
foreach ($content as $in){ $this->entity->$set_( $content );
$this->entity->$attribute($in[0], $in[1]);
}
} else {
$set_ = 'set_' . $attribute;
$this->entity->$set_( $content );
}
} }
} elseif (empty($args) && !$is_validated_and_in_db) { } elseif (empty($args) && !$is_validated_and_in_db) {

View File

@ -0,0 +1,24 @@
<?php
namespace Tainacan\Tests\Factories;
class Item_Metadata_Factory {
private $item_metadata;
public function create_item_metadata(\Tainacan\Entities\Item $item, \Tainacan\Entities\Field $field, $value = ''){
global $Tainacan_Item_Metadata;
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $field);
if (!empty($value))
$item_metadata->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
}
}
?>

View File

@ -15,6 +15,7 @@ class TAINACAN_UnitTestCase extends \WP_UnitTestCase {
$this->tainacan_entity_factory = new Factories\Entity_Factory(); $this->tainacan_entity_factory = new Factories\Entity_Factory();
$this->tainacan_field_factory = new Factories\Field_Factory(); $this->tainacan_field_factory = new Factories\Field_Factory();
$this->tainacan_filter_factory = new Factories\Filter_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' )); $new_admin_user = $this->factory()->user->create(array( 'role' => 'administrator' ));
wp_set_current_user($new_admin_user); wp_set_current_user($new_admin_user);

View File

@ -134,62 +134,57 @@ class Items extends TAINACAN_UnitTestCase {
array( array(
'title' => 'orange', 'title' => 'orange',
'collection' => $collection, 'collection' => $collection,
'add_metadata' => [
[$field, 'value_1']
],
'status' => 'publish' 'status' => 'publish'
), ),
true true
); );
$this->tainacan_item_metadata_factory->create_item_metadata($i, $field, 'value_1');
$item = $Tainacan_Items->fetch($i->get_id()); $item = $Tainacan_Items->fetch($i->get_id());
$meta_test = $item->get_fields(); $meta_test = new Entities\Item_Metadata_Entity($item, $field);
$this->assertTrue( isset($meta_test[$field->get_id()]) ); $this->assertTrue( $meta_test instanceof Entities\Item_Metadata_Entity );
$this->assertTrue( $meta_test[$field->get_id()] instanceof Entities\Item_Metadata_Entity ); $this->assertEquals( $field->get_id(), $meta_test->get_field()->get_id() );
$this->assertEquals( 'value_1', $meta_test[$field->get_id()]->get_value()); $this->assertEquals( 'value_1', $meta_test->get_value());
$this->tainacan_entity_factory->create_entity( $i = $this->tainacan_entity_factory->create_entity(
'item', 'item',
array( array(
'title' => 'apple', 'title' => 'apple',
'collection' => $collection2, 'collection' => $collection2,
'add_metadata' => [
[$field2, 'value_2'],
[$field3, 'value_2']
],
'status' => 'publish' 'status' => 'publish'
), ),
true 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', 'item',
array( array(
'title' => 'lemon', 'title' => 'lemon',
'collection' => $collection2, 'collection' => $collection2,
'add_metadata' => [
[$field2, 'value_2'],
[$field2, 'value_3'],
[$field3, 'value_3']
],
'status' => 'publish' 'status' => 'publish'
), ),
true 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', 'item',
array( array(
'title' => 'pineapple', 'title' => 'pineapple',
'collection' => $collection2, 'collection' => $collection2,
'add_metadata' => [
[$field2, 'value_3'],
[$field3, 'value_6']
],
'status' => 'publish' 'status' => 'publish'
), ),
true 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 // should return all 4 items
$test_query = $Tainacan_Items->fetch([]); $test_query = $Tainacan_Items->fetch([]);
@ -251,7 +246,7 @@ class Items extends TAINACAN_UnitTestCase {
], $collection2); ], $collection2);
$this->assertEquals(2, $test_query->post_count); $this->assertEquals(2, $test_query->post_count);
// should return 2 item // should return 2 items
$test_query = $Tainacan_Items->fetch([ $test_query = $Tainacan_Items->fetch([
'meta_query' => [ 'meta_query' => [
[ [

View File

@ -87,13 +87,13 @@ class Objects extends TAINACAN_UnitTestCase {
array( array(
'title' => 'orange', 'title' => 'orange',
'collection' => $collection, 'collection' => $collection,
'add_metadata' => [
[$field, 'value_1']
]
), ),
true 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); $entity = Repository::get_entity_by_post($test);
$this->assertEquals($i->get_db_identifier(), $entity->get_db_identifier()); $this->assertEquals($i->get_db_identifier(), $entity->get_db_identifier());