/** clean up after tests */
 public function tearDown()
 {
     // remove the temporary tests dir
     $testTmpDir = $this->getTempDirectory() . '/batchmake/tests';
     KWUtils::recursiveRemoveDirectory($testTmpDir);
     // change the current dir back to the saved cwd after each test
     chdir($this->cwd);
 }
 /** clean up after tests */
 public function tearDown()
 {
     // remove the temporary tests dir
     $tmpDir = $this->getTempDirectory() . '/' . $this->testTmpDir;
     KWUtils::recursiveRemoveDirectory($tmpDir);
     $this->Item->delete($this->tmpItem);
     // change the current dir back to the saved cwd after each test
     chdir($this->cwd);
 }
Esempio n. 3
0
 /**
  * Test ExportComponentTest::exportBitstreams function using invalid input.
  *
  * test case 1) input parameter itemIds is not an array; expect an exception
  * test case 2) use valid item id with invalid revision number; expect an exception
  * test case 3) use invalid item id; expect an exception
  */
 public function testExportBitStreamsInvalidCases()
 {
     $midas_exporttest_dir = $this->getTempDirectory() . '/exportTest';
     $usersFile = $this->loadData('User', 'default');
     $userDao = $this->User->load($usersFile[0]->getKey());
     $this->uploadItems($userDao);
     require_once BASE_PATH . '/core/controllers/components/ExportComponent.php';
     $exportCompoenent = new ExportComponent();
     $validFile = 'public.file';
     $validItems = $this->Item->getItemsFromSearch($validFile, $userDao);
     $validItemId = $validItems[0]->getKey();
     $invalidRevision = 100;
     $invalidItemId = 99999;
     // test case 1)
     try {
         $exportCompoenent->exportBitstreams($userDao, $midas_exporttest_dir, $validItemId, true);
         $this->fail('Expected an exception exporting component, but didn not get one');
     } catch (Zend_Exception $ze) {
         // if we got here, this is the correct behavior
         $this->assertTrue(true);
     }
     // test case 2)
     $inputItemIds = array();
     $inputItemIds[] = $validItemId . ',' . $invalidRevision;
     try {
         $exportCompoenent->exportBitstreams($userDao, $midas_exporttest_dir, $inputItemIds, true);
         $this->fail('Expected an exception exporting component, but did not get one');
     } catch (Zend_Exception $ze) {
         // if we got here, this is the correct behavior
         $this->assertTrue(true);
     }
     // test case 3)
     $inputItemIds = array();
     $inputItemIds[] = $invalidItemId;
     try {
         $exportCompoenent->exportBitstreams($userDao, $midas_exporttest_dir, $inputItemIds, true);
         $this->fail('Expected an exception exporting component, but did not get one');
     } catch (Zend_Exception $ze) {
         // if we got here, this is the correct behavior
         $this->assertTrue(true);
     }
     // clean up
     KWUtils::recursiveRemoveDirectory($midas_exporttest_dir);
 }
Esempio n. 4
0
 /**
  * Export bitstreams to target directory.
  *
  * Given itemIds, do policy check on these itemIds,
  * then create symbolic links to bitstreams (or copy the bitstreams)
  * in the "{targetDir}/{itemId}/" directories. If the {itemId} subdirectory
  * has been existed, delete the existing one first.
  * For policy check, we only check if the items are readable by the given user,
  * and don't further distinguish among "can_read", "can_write", and "owner"
  * levels.
  *
  * @param UserDao $userDao
  * @param string $targetDir Target directory to export bitstreams
  * @param array $itemIds Array of itemIds.
  *                               Each element is a comma separated value,
  *                               the 1st column is the actual item_id,
  *                               the 2nd is revision_number (optional)
  * @param bool $shouldSymLink Should we create symbolic links?
  *                               If not, the bitstreams will be copied to the target directory
  * @throws Zend_Exception
  */
 public function exportBitstreams($userDao, $targetDir, $itemIds, $shouldSymLink)
 {
     // if the path has a slash at the end, remove it here
     $targetDir = rtrim($targetDir, '/');
     /** @var ItemModel $itemModel */
     $itemModel = MidasLoader::loadModel('Item');
     // Get items
     $revisions = array();
     if (!is_array($itemIds)) {
         throw new Zend_Exception('Input parameter $itemIds should be an array.');
     }
     if (!empty($itemIds)) {
         foreach ($itemIds as $itemId) {
             // $itemId is a comma separated value,
             // the 1st column is the actual item_id, the 2nd is revision_num (optional)
             $tmpId = explode(',', $itemId);
             if (empty($tmpId[0])) {
                 continue;
             }
             // delete the itemId directory if it exists which means it was exported by
             // other user before
             $item_export_dir = $targetDir . '/' . $itemId;
             if (file_exists($item_export_dir)) {
                 if (!KWUtils::recursiveRemoveDirectory($item_export_dir)) {
                     throw new Zend_Exception($item_export_dir . ' has already existed and we cannot delete it.');
                 }
             }
             $item = $itemModel->load($tmpId[0]);
             if ($item == false) {
                 throw new Zend_Exception('Item ' . $tmpId[0] . ' does not exist. Please check your input.');
             } elseif (!$itemModel->policyCheck($item, $userDao)) {
                 // Do policy check in the ITEM level, ignore items which cannot be exported by the user.
                 continue;
             }
             // Use the given revision_number if it is not empty
             if (isset($tmpId[1])) {
                 $revision = $itemModel->getRevision($item, $tmpId[1]);
                 if ($revision !== false) {
                     $revisions[] = $revision;
                 } else {
                     throw new Zend_Exception('Revision number ' . $tmpId[1] . ' for item ' . $tmpId[0] . ' does not exist. Please check your input.');
                 }
             } else {
                 // Otherwise use the latest revision
                 $revision = $itemModel->getLastRevision($item);
                 if ($revision !== false) {
                     $revisions[] = $revision;
                 }
             }
         }
     }
     // process the items which pass the ITEM level policy check
     if (!empty($revisions)) {
         /** @var RandomComponent $randomComponent */
         $randomComponent = MidasLoader::loadComponent('Random');
         foreach ($revisions as $revision) {
             $itemId = $revision->getItemId();
             $this->_createItemDirectory($targetDir . '/' . $itemId);
             // itemRevision -> bitstream is a one-to-many relation (in bitstream table)
             $bitstreams = $revision->getBitstreams();
             if (!empty($bitstreams)) {
                 foreach ($bitstreams as $bitstream) {
                     // if the bitstream is not an actual file, such as url type, skip it
                     if ($bitstream->getChecksum() == ' ') {
                         continue;
                     }
                     $source = $bitstream->getAssetstore()->getPath() . '/' . $bitstream->getPath();
                     $dest = $targetDir . '/' . $itemId . '/' . $bitstream->getName();
                     // create symbolic links in target directory
                     if ($shouldSymLink) {
                         // for symbolic link option,if multiple bitstreams (in a single item revision)
                         // have the same file name, add a '.new' suffix to distinguish them
                         if (file_exists($dest)) {
                             $dest .= '.' . $randomComponent->generateInt() . '.new';
                         }
                         if (!symlink($source, $dest)) {
                             throw new Zend_Exception('Cannot create symlink: ' . $dest . 'linked to' . $source);
                         }
                     } else {
                         // OR copy bitstreams to target directory
                         // for copy option, if multiple bitstreams (in a single item revision)
                         // have the same file name, new file(s) wil overwrite the existing file(s)
                         if (!copy($source, $dest)) {
                             throw new Zend_Exception('Cannot copy bitstream from: ' . $source . 'to: ' . $dest);
                         }
                     }
                 }
             }
         }
     }
 }
Esempio n. 5
0
 /** tests recursiveRemoveDirectory function */
 public function testRecursiveRemoveDirectory()
 {
     // test some basic exception handling
     $this->assertFalse(KWUtils::recursiveRemoveDirectory(''));
     $this->assertFalse(KWUtils::recursiveRemoveDirectory('thisstringisunlikelytobeadirectory'));
     // create a two-level directory
     $testParentDir = UtilityComponent::getTempDirectory() . '/KWUtilsParentDir';
     mkdir($testParentDir);
     $testChildDir = UtilityComponent::getTempDirectory() . '/KWUtilsParentDir/ChildDir';
     mkdir($testChildDir);
     copy(BASE_PATH . '/tests/testfiles/search.png', $testChildDir . '/testContent.png');
     $this->assertTrue(file_exists($testChildDir . '/testContent.png'));
     // recursively remove the directory
     KWUtils::recursiveRemoveDirectory($testParentDir);
     $this->assertFalse(file_exists($testParentDir));
 }