diff --git a/src/api/endpoints/class-tainacan-rest-fields-controller.php b/src/api/endpoints/class-tainacan-rest-fields-controller.php index 49309d8f5..bb8388a12 100644 --- a/src/api/endpoints/class-tainacan-rest-fields-controller.php +++ b/src/api/endpoints/class-tainacan-rest-fields-controller.php @@ -106,24 +106,16 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller { public function prepare_item_for_database( $request, $collection_id = null ) { $field = new Entities\Field(); + $meta = json_decode( $request, true ); + foreach ( $meta as $key => $value ) { + $set_ = 'set_' . $key; + $field->$set_( $value ); + } + if($collection_id) { - $meta = json_decode( $request, true ); - - foreach ( $meta as $key => $value ) { - $set_ = 'set_' . $key; - $field->$set_( $value ); - } - $collection = new Entities\Collection( $collection_id ); - $field->set_collection( $collection ); } else { - $meta = json_decode( $request, true ); - foreach ( $meta as $key => $value ) { - $set_ = 'set_' . $key; - $field->$set_( $value ); - } - $field->set_collection_id( 'default' ); } diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index aea110d87..879ee5f10 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -2,8 +2,6 @@ namespace Tainacan\Tests; -use Tainacan\Repositories; - /** * @group api */ @@ -41,7 +39,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $field_added = $response->get_data(); $this->assertTrue(is_array($field_added) && array_key_exists('name', $field_added), sprintf('cannot create field, response: %s', print_r($field_added, true))); $this->assertEquals('Moeda', $field_added['name']); - + + $this->assertNotEquals('default', $field_added['collection_id']); } public function test_create_default_field(){ @@ -64,6 +63,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $this->assertTrue(is_array($field_added) && array_key_exists('name', $field_added), sprintf('cannot create field, response: %s', print_r($field_added, true))); $this->assertEquals('Ano de Publicação', $field_added['name']); + + $this->assertEquals('default', $field_added['collection_id']); } public function test_get_item_and_collection_metadata(){