|
| 1 | +<?php |
| 2 | +/*========================================================================= |
| 3 | + Midas Server |
| 4 | + Copyright Kitware SAS, 26 rue Louis Guérin, 69100 Villeurbanne, France. |
| 5 | + All rights reserved. |
| 6 | + For more information visit http://www.kitware.com/. |
| 7 | +
|
| 8 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + you may not use this file except in compliance with the License. |
| 10 | + You may obtain a copy of the License at |
| 11 | +
|
| 12 | + http://www.apache.org/licenses/LICENSE-2.0.txt |
| 13 | +
|
| 14 | + Unless required by applicable law or agreed to in writing, software |
| 15 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + See the License for the specific language governing permissions and |
| 18 | + limitations under the License. |
| 19 | +=========================================================================*/ |
| 20 | + |
| 21 | +require_once BASE_PATH.'/core/tests/controllers/api/RestCallMethodsTestCase.php'; |
| 22 | + |
| 23 | +/** Tests the functionality of the web API Rest Item methods */ |
| 24 | +class Core_RestCallItemMethodsTest extends RestCallMethodsTestCase |
| 25 | +{ |
| 26 | + /** set up tests */ |
| 27 | + public function setUp() |
| 28 | + { |
| 29 | + parent::setUp(); |
| 30 | + } |
| 31 | + |
| 32 | + /** Set metadata on the item. */ |
| 33 | + public function testItemAddmetadata() |
| 34 | + { |
| 35 | + $itemsFile = $this->loadData('Item', 'default'); |
| 36 | + $itemDao = $this->Item->load($itemsFile[1]->getKey()); |
| 37 | + |
| 38 | + $apipath = '/item/addmetadata/' . $itemDao->getItemId(); |
| 39 | + |
| 40 | + // No user will fail. |
| 41 | + $this->resetAll(); |
| 42 | + $resp = $this->_callRestApi('PUT', $apipath); |
| 43 | + $this->_assertStatusFail($resp); |
| 44 | + |
| 45 | + $usersFile = $this->loadData('User', 'default'); |
| 46 | + $userDao = $this->User->load($usersFile[0]->getKey()); |
| 47 | + |
| 48 | + // Lack of request body will fail. |
| 49 | + $this->resetAll(); |
| 50 | + $this->params['useSession'] = 'true'; |
| 51 | + $resp = $this->_callRestApi('PUT', $apipath, $userDao); |
| 52 | + $this->_assertStatusFail($resp); |
| 53 | + |
| 54 | + // Request body without a 'metadata' key will fail. |
| 55 | + $this->resetAll(); |
| 56 | + $this->params['useSession'] = 'true'; |
| 57 | + $this->params[0] = json_encode(array('murkydata' => array())); |
| 58 | + $resp = $this->_callRestApi('PUT', $apipath, $userDao); |
| 59 | + $this->_assertStatusFail($resp); |
| 60 | + |
| 61 | + // Metadatum needs 'value' key. |
| 62 | + $this->resetAll(); |
| 63 | + $this->params['useSession'] = 'true'; |
| 64 | + $this->params[0] = json_encode(array('metadata' => array('element' => 'key1'))); |
| 65 | + $resp = $this->_callRestApi('PUT', $apipath, $userDao); |
| 66 | + $this->_assertStatusFail($resp); |
| 67 | + |
| 68 | + // Metadatum needs 'element' key. |
| 69 | + $this->resetAll(); |
| 70 | + $this->params['useSession'] = 'true'; |
| 71 | + $this->params[0] = json_encode(array('metadata' => array('value' => 'val1'))); |
| 72 | + $resp = $this->_callRestApi('PUT', $apipath, $userDao); |
| 73 | + $this->_assertStatusFail($resp); |
| 74 | + |
| 75 | + // Write some metadata correctly. |
| 76 | + $this->resetAll(); |
| 77 | + $this->params['useSession'] = 'true'; |
| 78 | + $this->params[0] = json_encode(array('metadata' => array( |
| 79 | + array('element' => 'key1', 'value' => 'val1'), |
| 80 | + array('element' => 'key2', 'value' => 'val2') |
| 81 | + ))); |
| 82 | + $resp = $this->_callRestApi('PUT', $apipath, $userDao); |
| 83 | + $this->_assertStatusOk($resp); |
| 84 | + |
| 85 | + /** @var ItemModel $itemModel */ |
| 86 | + $itemModel = MidasLoader::loadModel('Item'); |
| 87 | + $revisionDao = $itemModel->getLastRevision($itemDao); |
| 88 | + /** @var ItemRevisionModel $itemRevisionModel */ |
| 89 | + $itemRevisionModel = MidasLoader::loadModel('ItemRevision'); |
| 90 | + $metadata = $itemRevisionModel->getMetadata($revisionDao); |
| 91 | + $metadataArray = array(); |
| 92 | + $key1 = false; |
| 93 | + $key2 = false; |
| 94 | + foreach ($metadata as $metadatum) { |
| 95 | + if ($metadatum->element === 'key1' && $metadatum->value === 'val1') { |
| 96 | + $key1 = true; |
| 97 | + } |
| 98 | + if ($metadatum->element === 'key2' && $metadatum->value === 'val2') { |
| 99 | + $key2 = true; |
| 100 | + } |
| 101 | + } |
| 102 | + $this->assertTrue($key1 && $key2, 'Metadata incorrectly set'); |
| 103 | + |
| 104 | + // Update key1, add key3, leave key2 alone. |
| 105 | + $this->resetAll(); |
| 106 | + $this->params['useSession'] = 'true'; |
| 107 | + $this->params[0] = json_encode(array('metadata' => array( |
| 108 | + array('element' => 'key1', 'value' => 'newval1'), |
| 109 | + array('element' => 'key3', 'value' => 'val3') |
| 110 | + ))); |
| 111 | + $resp = $this->_callRestApi('PUT', $apipath, $userDao); |
| 112 | + $this->_assertStatusOk($resp); |
| 113 | + |
| 114 | + $metadata = $itemRevisionModel->getMetadata($revisionDao); |
| 115 | + $metadataArray = array(); |
| 116 | + $key1 = false; |
| 117 | + $key2 = false; |
| 118 | + $key3 = false; |
| 119 | + foreach ($metadata as $metadatum) { |
| 120 | + if ($metadatum->element === 'key1' && $metadatum->value === 'newval1') { |
| 121 | + $key1 = true; |
| 122 | + } |
| 123 | + if ($metadatum->element === 'key2' && $metadatum->value === 'val2') { |
| 124 | + $key2 = true; |
| 125 | + } |
| 126 | + if ($metadatum->element === 'key3' && $metadatum->value === 'val3') { |
| 127 | + $key3 = true; |
| 128 | + } |
| 129 | + } |
| 130 | + $this->assertTrue($key1 && $key2 && $key3, 'Metadata incorrectly set'); |
| 131 | + } |
| 132 | +} |
0 commit comments