diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index 8b6c48d86..0c638f218 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -186,23 +186,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { $this->item->set_collection($collection); - $field = get_post_meta($collection->get_id()); - - if(!empty($field)) { - foreach ($field as $key => $value){ - $new_field = new Entities\Field(); - - try { - $set_ = 'set_' . $key; - $new_field->$set_( $value ); - } catch (\Error $exception){ - // Do nothing - } - } - - } - - return $new_field; + return $this->item; } /** @@ -223,7 +207,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { } try { - $field = $this->prepare_item_for_database( [ $item, $collection_id ] ); + $this->prepare_item_for_database( [ $item, $collection_id ] ); } catch (\Error $exception){ return new WP_REST_Response($exception->getMessage(), 400); } @@ -231,10 +215,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { if($this->item->validate()) { $item = $this->items_repository->insert($this->item ); - $item_metadata = new Entities\Item_Metadata_Entity($item, $field ); - $field_added = $this->item_metadata->insert( $item_metadata ); - - return new WP_REST_Response($field_added->get_item()->__toArray(), 201 ); + return new WP_REST_Response($this->item->__toArray(), 201 ); } @@ -255,7 +236,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { $collection = $this->collections_repository->fetch($request['collection_id']); if ($collection instanceof Entities\Collection) { - return $collection->get_items_capabilities()->edit_posts; + return current_user_can($collection->get_items_capabilities()->edit_posts); } return false; diff --git a/src/classes/class-tainacan-capabilities.php b/src/classes/class-tainacan-capabilities.php index 0c19abb8c..4d9c448e9 100644 --- a/src/classes/class-tainacan-capabilities.php +++ b/src/classes/class-tainacan-capabilities.php @@ -332,6 +332,10 @@ class Capabilities { $role->add_cap($collection_items_caps->$cap); } } + + // Refresh roles capabilities for current user to have instant effect + global $current_user; + $current_user->get_role_caps(); } /** diff --git a/tests/test-api-items.php b/tests/test-api-items.php index b312d659b..170f0b45c 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -39,7 +39,8 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { 'collection', array( 'name' => 'Agile', - 'description' => 'Agile methods' + 'description' => 'Agile methods', + 'status' => 'publish' ), true ); @@ -70,7 +71,6 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $this->assertEquals(200, $response->get_status()); - $data = $response->get_data(); $first_item = $data[0]; diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index d50cec809..77dcaffb8 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -69,8 +69,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { true ); - $field = $this->tainacan_field_factory->create_field('text', '', true); - $field = $this->tainacan_entity_factory->create_entity( 'field', array( @@ -78,7 +76,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { 'description' => 'Descreve valor do campo data.', 'collection' => $collection, 'status' => 'publish', - 'field_type' => $field->get_primitive_type(), + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); @@ -142,8 +140,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { true ); - $field = $this->tainacan_field_factory->create_field('text', '', true); - $field = $this->tainacan_entity_factory->create_entity( 'field', array( @@ -151,7 +147,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { 'description' => 'Descreve o dado do campo data.', 'collection' => $collection, 'status' => 'publish', - 'field_type' => $field->get_primitive_type(), + 'field_type' => 'Tainacan\Field_Types\Text', ), true ); diff --git a/tests/test-permissions.php b/tests/test-permissions.php index 0f3487639..72851184a 100644 --- a/tests/test-permissions.php +++ b/tests/test-permissions.php @@ -49,7 +49,24 @@ class Permissions extends TAINACAN_UnitTestCase { wp_set_current_user($new_contributor_user); $this->assertTrue($collection->can_read()); $this->assertFalse($collection->can_publish()); - + + + $this->assertTrue(user_can($new_admin_user, $collection->get_items_capabilities()->edit_posts, $collection->get_id()), 'admin should be able to edit items in the collection'); + + $privateCollection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'testePermsCC', + 'description' => 'adasdasdsa', + 'status' => 'private' + ), + true + ); + + $this->assertTrue(user_can($new_admin_user, $collection->cap->read_post, $collection->get_id()), 'admin should be able read private collection'); + + // subsciber should not be able to + $this->assertFalse(user_can($new_user, $collection->cap->read_post, $collection->get_id()), 'subscriber should not be able read private collection'); } } \ No newline at end of file