From 46df1f95882b612b0e374dbceb8539899c2f67a9 Mon Sep 17 00:00:00 2001 From: leogermani Date: Thu, 18 Jul 2019 17:57:16 -0300 Subject: [PATCH] fix set_value() passing an Term Entity #250 --- .../class-tainacan-item-metadata.php | 5 ++- tests/test-api-items.php | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index bd700376d..3a09cd76e 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -175,7 +175,10 @@ class Item_Metadata extends Repository { // $success = wp_set_object_terms( $item_metadata->get_item()->get_id(), $new_terms, $taxonomy->get_db_identifier() ); $insert = []; - foreach ( (array) $new_terms as $new_term ) { + if ( !is_array($new_terms) ) { + $new_terms = [ $new_terms ]; + } + foreach ( $new_terms as $new_term ) { if ( \is_object($new_term) && $new_term instanceof Entities\Term ) { $exists = $new_term->WP_Term; } else { diff --git a/tests/test-api-items.php b/tests/test-api-items.php index 2fe4e4182..591a4fe1b 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -477,6 +477,9 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals(201, $response->get_status()); } + /** + * @group duplicate + */ function test_duplicate() { $collection = $this->tainacan_entity_factory->create_entity( @@ -523,6 +526,16 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { true ); + $taxonomy2 = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => 'taxonomy_public2', + 'description' => 'taxonomy_public2', + 'status' => 'publish' + ), + true + ); + $tax_meta = $this->tainacan_entity_factory->create_entity( 'metadatum', array( @@ -539,6 +552,22 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { true ); + $tax_meta_single = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'metadata-public-single', + 'status' => 'publish', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', + 'metadata_type_options' => [ + 'allow_new_terms' => 'yes', + 'taxonomy_id' => $taxonomy2->get_id() + ], + 'multiple' => 'no' + ), + true + ); + $item1 = $this->tainacan_entity_factory->create_entity( 'item', array( @@ -567,6 +596,11 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $newMeta->validate(); $itemMetaRepo->insert($newMeta); + $newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $tax_meta_single); + $newMeta->set_value('test term'); + $newMeta->validate(); + $itemMetaRepo->insert($newMeta); + $request = new \WP_REST_Request('POST', $this->namespace . '/collection/' . $collection->get_id() . '/items/' . $item1->get_id() . '/duplicate'); $response = $this->server->dispatch($request);