/** testGetSetValue */ public function testGetSetValue() { Zend_Registry::set('modulesEnable', $this->enabledModules); Zend_Registry::set('notifier', new MIDAS_Notifier(false, null)); /** @var Validation_ScalarResultModel $scalarResultModel */ $scalarResultModel = MidasLoader::loadModel('ScalarResult', 'validation'); $daos = $scalarResultModel->getAll(); $sr = $daos[0]; $folder = new FolderDao(); $folder->setName('result'); $folder->setDescription('result'); $folder->setParentId(-1); $this->Folder->save($folder); $trainingItem = new ItemDao(); $trainingItem->setName('img00.mha'); $trainingItem->setDescription('training img 00'); $trainingItem->setType(0); $this->Item->save($trainingItem); $scalarResultModel->setFolder($sr, $folder); $scalarResultModel->setItem($sr, $trainingItem); $sr->setValue(90.009); $scalarResultModel->save($sr); $daos = $scalarResultModel->getAll(); $sr = $daos[0]; $this->assertEquals(90.009, $sr->getValue()); }
/** Import a directory recursively */ private function _recursiveParseDirectory($path, $currentdir) { $it = new DirectoryIterator($path); foreach ($it as $fileInfo) { if ($fileInfo->isDot()) { continue; } // If the file/dir is not readable (permission issue) if (!$fileInfo->isReadable()) { $this->getLogger()->crit($fileInfo->getPathName() . ' cannot be imported. Not readable.'); continue; } // If this is too slow we'll figure something out if ($this->_checkStopImport()) { return false; } if ($fileInfo->isDir()) { // we have a directory // If the the directory actually doesn't exist at this point, // skip it. if (!file_exists($fileInfo->getPathName())) { continue; } // Get the files in the directory and skip the folder if it does not // contain any files and we aren't set to import empty directories. The // count($files) <= 2 is there to account for our our friends . and .. $files = scandir($fileInfo->getPathName()); if (!$this->importemptydirectories && $files && count($files) <= 2) { continue; } // Find if the child exists $child = $this->Folder->getFolderByName($currentdir, $fileInfo->getFilename()); // If the folder does not exist, create one. if (!$child) { $child = new FolderDao(); $child->setName($fileInfo->getFilename()); $child->setParentId($currentdir->getFolderId()); $child->setDateCreation(date('Y-m-d H:i:s')); $child->setDescription(''); $this->Folder->save($child); $this->Folderpolicyuser->createPolicy($this->userSession->Dao, $child, MIDAS_POLICY_ADMIN); } // Keep descending $this->_recursiveParseDirectory($fileInfo->getPathName(), $child); } else { // We have a file $this->_incrementFileProcessed(); $newrevision = true; $item = $this->Folder->getItemByName($currentdir, $fileInfo->getFilename()); if (!$item) { // Create an item $item = new ItemDao(); $item->setName($fileInfo->getFilename()); $item->setDescription(''); $item->setPrivacyStatus(MIDAS_PRIVACY_PRIVATE); // Must set this flag private initially $this->Item->save($item, true); // Set the policy of the item $this->Itempolicyuser->createPolicy($this->userSession->Dao, $item, MIDAS_POLICY_ADMIN); // Add the item to the current directory $this->Folder->addItem($currentdir, $item); } // Check if the bistream has been updated based on the local date $revision = $this->ItemRevision->getLatestRevision($item); if ($revision) { $newrevision = false; $bitstream = $this->ItemRevision->getBitstreamByName($revision, $fileInfo->getFilename()); $curMD5 = UtilityComponent::md5file($fileInfo->getPathName()); $diskFileIsNewer = strtotime($bitstream->getDate()) < filemtime($fileInfo->getPathName()); $md5IsDifferent = $bitstream->getChecksum() != $curMD5; if (!$bitstream || $diskFileIsNewer && $md5IsDifferent) { $newrevision = true; } } if ($newrevision) { // Create a revision for the item $itemRevisionDao = new ItemRevisionDao(); $itemRevisionDao->setChanges('Initial revision'); $itemRevisionDao->setUser_id($this->userSession->Dao->getUserId()); $this->Item->addRevision($item, $itemRevisionDao); // Add bitstreams to the revision $this->getLogger()->debug('create New Bitstream'); $bitstreamDao = new BitstreamDao(); $bitstreamDao->setName($fileInfo->getFilename()); $bitstreamDao->setPath($fileInfo->getPathName()); $bitstreamDao->fillPropertiesFromPath(); // Set the Assetstore $bitstreamDao->setAssetstoreId($this->assetstoreid); // Upload the bitstream $assetstoreDao = $this->Assetstore->load($this->assetstoreid); $this->Component->Upload->uploadBitstream($bitstreamDao, $assetstoreDao, true); $this->ItemRevision->addBitstream($itemRevisionDao, $bitstreamDao); } } } unset($it); return true; }
/** * Test getting all scalar results for a given dashboard. * @SuppressWarnings controlCloseCurly */ public function testGetAllScores() { // Acquire the dashboard from the database /** @var Validation_DashboardModel $dashboardModel */ $dashboardModel = MidasLoader::loadModel('Dashboard', 'validation'); /** @var ItemModel $itemModel */ $itemModel = MidasLoader::loadModel('Item'); /** @var FolderModel $folderModel */ $folderModel = MidasLoader::loadModel('Folder'); $dashboardDao = $dashboardModel->load(1); $folderDao = $folderModel->load(1000); // Create additional result item $resultItem = null; $createdItems = array(); $expected = array(); for ($i = 0; $i < 2; ++$i) { $resultItem = new ItemDao(); $resultItem->setName('img0' . $i . '.mha'); $resultItem->setDescription('result img ' . $i); $resultItem->setType(0); $itemModel->save($resultItem); $createdItems[$resultItem->getKey()] = $i * 5 + 10; $folderModel->addItem($folderDao, $resultItem); } $expected[$folderDao->getKey()] = $createdItems; $dashboardModel->addResult($dashboardDao, $folderDao); $dashboardModel->setScores($dashboardDao, $folderDao, $createdItems); // Add a result folder $this->params['token'] = $this->_loginUsingApiKey(); $this->params['method'] = 'midas.validation.addresultfolder'; $this->params['dashboard_id'] = 1; $this->params['folder_id'] = 1001; $this->request->setMethod('POST'); $resp = $this->_callJsonApi(); $this->_assertStatusOk($resp); $this->assertEquals(1, $resp->data->dashboard_id); $this->resetAll(); // Add a scalar result $this->params['token'] = $this->_loginUsingApiKeyAsAdmin(); $this->params['method'] = 'midas.validation.setscalarresult'; $this->params['dashboard_id'] = 1; $this->params['folder_id'] = 1001; $this->params['item_id'] = 1000; $this->params['value'] = 3; $this->request->setMethod('POST'); $resp = $this->_callJsonApi(); $this->_assertStatusOk($resp); $this->resetAll(); // Add a scalar result $this->params['token'] = $this->_loginUsingApiKeyAsAdmin(); $this->params['method'] = 'midas.validation.setscalarresult'; $this->params['dashboard_id'] = 1; $this->params['folder_id'] = 1001; $this->params['item_id'] = 1001; $this->params['value'] = 7; $this->request->setMethod('POST'); $resp = $this->_callJsonApi(); $this->_assertStatusOk($resp); $this->resetAll(); $this->params['method'] = 'midas.validation.getallscores'; $this->params['dashboard_id'] = 1; $this->params['folder_id'] = 1001; $this->request->setMethod('POST'); $resp = $this->_callJsonApi(); $this->_assertStatusOk($resp); $expected[1001] = array(); $expected[1001][1000] = 3; $expected[1001][1001] = 7; $this->assertEquals(1, $resp->data->dashboard_id); // Crazy looping because the json is parsed in as an object rather than // an array (not really a bad thing, just something to work around) $scores = $resp->data->scores; foreach ($expected as $folderId => $items) { foreach ($items as $itemId => $val) { $this->assertEquals($val, $scores->{$folderId}->{$itemId}); } } }
/** * Save upload item in the database. * * @param UserDao $userDao * @param string $name * @param string $url * @param null|int $parent * @param int $sizebytes * @param string $checksum * @return ItemDao * @throws Zend_Exception */ public function createLinkItem($userDao, $name, $url, $parent = null, $sizebytes = 0, $checksum = ' ') { /** @var ItemModel $itemModel */ $itemModel = MidasLoader::loadModel('Item'); /** @var FolderModel $folderModel */ $folderModel = MidasLoader::loadModel('Folder'); /** @var AssetstoreModel $assetstoreModel */ $assetstoreModel = MidasLoader::loadModel('Assetstore'); /** @var ItemRevisionModel $itemRevisionModel */ $itemRevisionModel = MidasLoader::loadModel('ItemRevision'); if ($userDao == null) { throw new Zend_Exception('Please log in'); } if (is_numeric($parent)) { $parent = $folderModel->load($parent); } if ($parent == false || !$folderModel->policyCheck($parent, $userDao, MIDAS_POLICY_WRITE)) { throw new Zend_Exception('Parent permissions errors'); } Zend_Loader::loadClass('ItemDao', BASE_PATH . '/core/models/dao'); $item = new ItemDao(); $item->setName($name); $item->setDescription(''); $item->setSizebytes($sizebytes); $item->setType(0); $item->setPrivacyStatus(MIDAS_PRIVACY_PRIVATE); // Must set this flag private initially $itemModel->save($item, false); $folderModel->addItem($parent, $item); $itemModel->copyParentPolicies($item, $parent); Zend_Loader::loadClass('ItemRevisionDao', BASE_PATH . '/core/models/dao'); $itemRevisionDao = new ItemRevisionDao(); $itemRevisionDao->setChanges('Initial revision'); $itemRevisionDao->setUser_id($userDao->getKey()); $itemRevisionDao->setDate(date('Y-m-d H:i:s')); $itemRevisionDao->setLicenseId(null); $itemModel->addRevision($item, $itemRevisionDao); // Add bitstreams to the revision Zend_Loader::loadClass('BitstreamDao', BASE_PATH . '/core/models/dao'); $bitstreamDao = new BitstreamDao(); $bitstreamDao->setName($url); $bitstreamDao->setPath($url); $bitstreamDao->setMimetype('url'); $bitstreamDao->setSizebytes($sizebytes); $bitstreamDao->setChecksum($checksum); $assetstoreDao = $assetstoreModel->getDefault(); $bitstreamDao->setAssetstoreId($assetstoreDao->getKey()); $itemRevisionModel->addBitstream($itemRevisionDao, $bitstreamDao); $this->getLogger()->debug('Link item created (' . $item->getName() . ', id=' . $item->getKey() . ')'); return $item; }
/** * Test that calling removeItem from a folder deletes a non-shared item. */ public function testRemoveItemDeletesNonSharedItem() { $folder = $this->Folder->createFolder('TestFolder1', 'Description', 0); $this->assertTrue($folder->saved); $item = new ItemDao(); $item->setName('subItem'); $item->setDescription('test item to be deleted'); $item->setType(0); $this->Item->save($item); $this->assertTrue($item->saved); $this->Folder->addItem($folder, $item); $items = $folder->getItems(); $this->assertEquals(1, count($items)); $this->assertEquals($items[0]->getName(), 'subItem'); $itemid = $items[0]->getKey(); // Make sure item is deleted when it is removed from its only parent $this->Folder->removeItem($folder, $item); $item = $this->Item->load($itemid); $this->assertEquals(false, $item, 'Item should have been deleted'); }
/** * test addResult function. */ public function testSetScore() { // Create training, testing, and truth folders $testingFolder = new FolderDao(); $testingFolder->setName('testing'); $testingFolder->setDescription('testing'); $testingFolder->setParentId(-1); $this->Folder->save($testingFolder); $trainingFolder = new FolderDao(); $trainingFolder->setName('training'); $trainingFolder->setDescription('training'); $trainingFolder->setParentId(-1); $this->Folder->save($trainingFolder); $truthFolder = new FolderDao(); $truthFolder->setName('truth'); $truthFolder->setDescription('truth'); $truthFolder->setParentId(-1); $this->Folder->save($truthFolder); // Create result folder $resultFolder = new FolderDao(); $resultFolder->setName('result'); $resultFolder->setDescription('result'); $resultFolder->setParentId(-1); $this->Folder->save($resultFolder); // Add items to the folders $trainingItem = null; $testingItem = null; $truthItem = null; $resultItem = null; for ($i = 0; $i < 3; ++$i) { $trainingItem = new ItemDao(); $testingItem = new ItemDao(); $truthItem = new ItemDao(); $resultItem = new ItemDao(); $trainingItem->setName('img0' . $i . '.mha'); $testingItem->setName('img0' . $i . '.mha'); $truthItem->setName('img0' . $i . '.mha'); $resultItem->setName('img0' . $i . '.mha'); $trainingItem->setDescription('training img ' . $i); $testingItem->setDescription('testing img ' . $i); $truthItem->setDescription('truth img ' . $i); $resultItem->setDescription('result img ' . $i); $trainingItem->setType(0); $testingItem->setType(0); $truthItem->setType(0); $resultItem->setType(0); $this->Item->save($trainingItem); $this->Item->save($testingItem); $this->Item->save($truthItem); $this->Item->save($resultItem); $this->Folder->addItem($trainingFolder, $trainingItem); $this->Folder->addItem($testingFolder, $testingItem); $this->Folder->addItem($truthFolder, $truthItem); $this->Folder->addItem($resultFolder, $resultItem); } // Acquire the dashboard from the database /** @var Validation_DashboardModel $dashboardModel */ $dashboardModel = MidasLoader::loadModel('Dashboard', 'validation'); $daos = $dashboardModel->getAll(); $dao = $daos[0]; // Add testing, training, and truth to the dashboard $dashboardModel->setTraining($dao, $trainingFolder); $dashboardModel->setTesting($dao, $testingFolder); $dashboardModel->setTruth($dao, $truthFolder); // Reload the dashboard and check it for consistency $daos = $dashboardModel->getAll(); $dao = $daos[0]; $dashboardModel->addResult($dao, $resultFolder); // Get the results $results = $dao->getResults(); $firstResult = $results[2]; $items = $firstResult->getItems(); $values = array(); $count = 0; foreach ($items as $item) { $values[$item->getKey()] = $count * 15; $dashboardModel->setScore($dao, $firstResult, $item, $count * 15); ++$count; } $daos = $dashboardModel->getAll(); $dao = $daos[0]; $this->assertEquals(3, count($dao->getScores())); $scores = $dashboardModel->getScores($dao, $resultFolder); $this->assertEquals($values, $scores); }