Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Return item from item addmetadata PUT rest endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrauer committed Dec 9, 2015
1 parent fd566cb commit db95271
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions core/controllers/components/ApiitemComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function itemSetmultiplemetadata($args)
* @http PUT
* @param id The id of the item
* @param revision (Optional) Item Revision number to set metadata on, defaults to latest revision.
* @return true on success,
* @return item on success,
* will fail if there are no revisions or the specified revision is not found.
*
* @param array $args parameters
Expand All @@ -207,14 +207,14 @@ public function itemAddmetadata($args)
if (!array_key_exists(0, $args)) {
throw new Exception('Missing request body data.', MIDAS_INVALID_PARAMETER);
}
$json_body = json_decode($args[0]);
if (null === $json_body) {
$jsonBody = json_decode($args[0]);
if ($jsonBody === null) {
throw new Exception('Request body data must be valid JSON.', MIDAS_INVALID_PARAMETER);
}
if (!array_key_exists('metadata', $json_body)) {
if (!array_key_exists('metadata', $jsonBody)) {
throw new Exception("Request body data missing key 'metadata'.", MIDAS_INVALID_PARAMETER);
}
$metadata = $json_body->metadata;
$metadata = $jsonBody->metadata;
foreach ($metadata as $metadatum) {
if (!isset($metadatum->element) || !isset($metadatum->value)) {
throw new Exception("All metadata must have 'element' and 'value' keys.", MIDAS_INVALID_PARAMETER);
Expand All @@ -232,7 +232,7 @@ public function itemAddmetadata($args)
);
}

return true;
return $item;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/tests/controllers/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
# limitations under the License.
#=============================================================================

add_midas_test(CoreRestCallItemMethodsTest RestCallItemMethodsTest.php)
add_midas_test(CoreRestCallUserMethodsTest RestCallUserMethodsTest.php)
add_midas_test(CoreRestKeyControllerTest RestKeyControllerTest.php)
add_midas_test(CoreRestCallItemMethodsTest RestCallItemMethodsTest.php)
20 changes: 10 additions & 10 deletions core/tests/controllers/api/RestCallItemMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

require_once BASE_PATH.'/core/tests/controllers/api/RestCallMethodsTestCase.php';

/** Tests the functionality of the web API Rest Item methods */
/** Tests the functionality of the web API Rest Item methods. */
class Core_RestCallItemMethodsTest extends RestCallMethodsTestCase
{
/** set up tests */
/** Set up tests. */
public function setUp()
{
parent::setUp();
Expand All @@ -35,11 +35,11 @@ public function testItemAddmetadata()
$itemsFile = $this->loadData('Item', 'default');
$itemDao = $this->Item->load($itemsFile[1]->getKey());

$apipath = '/item/addmetadata/'.$itemDao->getItemId();
$apiPath = '/item/addmetadata/'.$itemDao->getItemId();

// No user will fail.
$this->resetAll();
$resp = $this->_callRestApi('PUT', $apipath);
$resp = $this->_callRestApi('PUT', $apiPath);
$this->_assertStatusFail($resp);

$usersFile = $this->loadData('User', 'default');
Expand All @@ -48,28 +48,28 @@ public function testItemAddmetadata()
// Lack of request body will fail.
$this->resetAll();
$this->params['useSession'] = 'true';
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
$resp = $this->_callRestApi('PUT', $apiPath, $userDao);
$this->_assertStatusFail($resp);

// Request body without a 'metadata' key will fail.
$this->resetAll();
$this->params['useSession'] = 'true';
$this->params[0] = json_encode(array('murkydata' => array()));
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
$resp = $this->_callRestApi('PUT', $apiPath, $userDao);
$this->_assertStatusFail($resp);

// Metadatum needs 'value' key.
$this->resetAll();
$this->params['useSession'] = 'true';
$this->params[0] = json_encode(array('metadata' => array('element' => 'key1')));
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
$resp = $this->_callRestApi('PUT', $apiPath, $userDao);
$this->_assertStatusFail($resp);

// Metadatum needs 'element' key.
$this->resetAll();
$this->params['useSession'] = 'true';
$this->params[0] = json_encode(array('metadata' => array('value' => 'val1')));
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
$resp = $this->_callRestApi('PUT', $apiPath, $userDao);
$this->_assertStatusFail($resp);

// Write some metadata correctly.
Expand All @@ -79,7 +79,7 @@ public function testItemAddmetadata()
array('element' => 'key1', 'value' => 'val1'),
array('element' => 'key2', 'value' => 'val2'),
)));
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
$resp = $this->_callRestApi('PUT', $apiPath, $userDao);
$this->_assertStatusOk($resp);

/** @var ItemModel $itemModel */
Expand Down Expand Up @@ -108,7 +108,7 @@ public function testItemAddmetadata()
array('element' => 'key1', 'value' => 'newval1'),
array('element' => 'key3', 'value' => 'val3'),
)));
$resp = $this->_callRestApi('PUT', $apipath, $userDao);
$resp = $this->_callRestApi('PUT', $apiPath, $userDao);
$this->_assertStatusOk($resp);

$metadata = $itemRevisionModel->getMetadata($revisionDao);
Expand Down
2 changes: 1 addition & 1 deletion modules/api/controllers/components/ApiComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ public function itemSetmultiplemetadata($args)
* @param token Authentication token
* @param itemid The id of the item
* @param revision (Optional) Item Revision number to set metadata on, defaults to latest revision.
* @return true on success,
* @return item on success,
* will fail if there are no revisions or the specified revision is not found.
*/
public function itemAddmetadata($args)
Expand Down

0 comments on commit db95271

Please sign in to comment.