/**
  * Creates a new version of a document by moving the current file and
  * renaming it to the versioned filename.
  *
  * This method assumes that the method calling this is just about to upload
  * a new file to replace the old file.
  *
  * @static
  * @param DMSDocument $doc
  *
  * @return bool Success or failure
  */
 public static function create_version(DMSDocument $doc)
 {
     $success = false;
     $existingPath = $doc->getFullPath();
     if (is_file($existingPath)) {
         $docData = $doc->toMap();
         unset($docData['ID']);
         $version = new DMSDocument_versions($docData);
         //create a copy of the current DMSDocument as a version
         $previousVersionCounter = 0;
         $newestExistingVersion = self::get_versions($doc)->sort(array('Created' => 'DESC', 'ID' => 'DESC'))->limit(1);
         if ($newestExistingVersion && $newestExistingVersion->Count() > 0) {
             $previousVersionCounter = $newestExistingVersion->first()->VersionCounter;
         }
         //change the filename field to a field containing the new soon-to-be versioned file
         $version->VersionCounter = $previousVersionCounter + 1;
         //start versions at 1
         $newFilename = $version->generateVersionedFilename($doc, $version->VersionCounter);
         $version->Filename = $newFilename;
         //add a relation back to the origin ID;
         $version->DocumentID = $doc->ID;
         $id = $version->write();
         if (!empty($id)) {
             rename($existingPath, $version->getFullPath());
             $success = true;
         }
     }
     return $success;
 }
 function testBasicEmbargo()
 {
     $oldDMSFolder = DMS::$dmsFolder;
     DMS::$dmsFolder = DMS_DIR;
     //sneakily setting the DMS folder to the folder where the test file lives
     $doc = new DMSDocument();
     $doc->Filename = "DMS-test-lorum-file.pdf";
     $doc->Folder = "tests";
     $docID = $doc->write();
     //fake a request for a document
     $controller = new DMSDocument_Controller();
     DMSDocument_Controller::$testMode = true;
     $result = $controller->index($this->createFakeHTTPRequest($docID));
     $this->assertEquals($doc->getFullPath(), $result, "Correct underlying file returned (in test mode)");
     $doc->embargoIndefinitely();
     $result = $controller->index($this->createFakeHTTPRequest($docID));
     $this->assertNotEquals($doc->getFullPath(), $result, "File no longer returned (in test mode)");
     DMS::$dmsFolder = $oldDMSFolder;
 }
 function testBasicEmbargo()
 {
     $oldDMSFolder = DMS::$dmsFolder;
     DMS::$dmsFolder = DMS_DIR;
     //sneakily setting the DMS folder to the folder where the test file lives
     $doc = new DMSDocument();
     $doc->Filename = "DMS-test-lorum-file.pdf";
     $doc->Folder = "tests";
     $docID = $doc->write();
     //fake a request for a document
     $controller = new DMSDocument_Controller();
     DMSDocument_Controller::$testMode = true;
     $result = $controller->index($this->createFakeHTTPRequest($docID));
     $this->assertEquals($doc->getFullPath(), $result, "Correct underlying file returned (in test mode)");
     $doc->embargoIndefinitely();
     $this->logInWithPermission('ADMIN');
     $result = $controller->index($this->createFakeHTTPRequest($docID));
     $this->assertEquals($doc->getFullPath(), $result, "Admins can still download embargoed files");
     $this->logInWithPermission('random-user-group');
     $result = $controller->index($this->createFakeHTTPRequest($docID));
     $this->assertNotEquals($doc->getFullPath(), $result, "File no longer returned (in test mode) when switching to other user group");
     DMS::$dmsFolder = $oldDMSFolder;
 }