function testDMSVersionStorage()
 {
     $dms = DMS::inst();
     $document = $dms->storeDocument(self::$testFile);
     $this->assertNotNull($document, "Document object created");
     $this->assertTrue(file_exists(DMS::get_dms_path() . DIRECTORY_SEPARATOR . $document->Folder . DIRECTORY_SEPARATOR . $document->Filename), "Document file copied into DMS folder");
     $document->replaceDocument(self::$testFile2);
     $document->replaceDocument(self::$testFile);
     $document->replaceDocument(self::$testFile2);
     $document->replaceDocument(self::$testFile);
     $versionsList = $document->getVersions();
     $this->assertEquals(4, $versionsList->Count(), "4 Versions created");
     $versionsArray = $versionsList->toArray();
     $this->assertEquals($versionsArray[0]->VersionCounter, 1, "Correct version count");
     $this->assertEquals($versionsArray[1]->VersionCounter, 2, "Correct version count");
     $this->assertEquals($versionsArray[2]->VersionCounter, 3, "Correct version count");
     $this->assertEquals($versionsArray[3]->VersionCounter, 4, "Correct version count");
 }
 function testReplaceDocument()
 {
     $dms = DMS::inst();
     //store the first document
     $document = $dms->storeDocument(self::$testFile);
     $document->Title = "My custom title";
     $document->Description = "My custom description";
     $document->write();
     //then overwrite with a second document
     $document = $document->replaceDocument(self::$testFile2);
     $this->assertNotNull($document, "Document object created");
     $this->assertTrue(file_exists(DMS::get_dms_path() . DIRECTORY_SEPARATOR . $document->Folder . DIRECTORY_SEPARATOR . $document->Filename), "Document file copied into DMS folder");
     $this->assertContains("DMS-test-document-2", $document->Filename, "Original document filename is contain in the new filename");
     $this->assertEquals("My custom title", $document->Title, "Custom title not modified");
     $this->assertEquals("My custom description", $document->Description, "Custom description not modified");
 }
 /**
  * Relate an existing file on the filesystem to the document.
  *
  * Copies the file to the new destination, as defined in {@link get_DMS_path()}.
  *
  * @param string $filePath Path to file, relative to webroot.
  *
  * @return DMSDocument
  */
 public function storeDocument($filePath)
 {
     if (empty($this->ID)) {
         user_error("Document must be written to database before it can store documents", E_USER_ERROR);
     }
     // calculate all the path to copy the file to
     $fromFilename = basename($filePath);
     $toFilename = $this->ID . '~' . $fromFilename;
     //add the docID to the start of the Filename
     $toFolder = DMS::get_storage_folder($this->ID);
     $toPath = DMS::get_dms_path() . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
     DMS::create_storage_folder(DMS::get_dms_path() . DIRECTORY_SEPARATOR . $toFolder);
     //copy the file into place
     $fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath;
     //version the existing file (copy it to a new "very specific" filename
     if (DMSDocument_versions::$enable_versions) {
         DMSDocument_versions::create_version($this);
     } else {
         //otherwise delete the old document file
         $oldPath = $this->getFullPath();
         if (file_exists($oldPath)) {
             unlink($oldPath);
         }
     }
     copy($fromPath, $toPath);
     //this will overwrite the existing file (if present)
     //write the filename of the stored document
     $this->Filename = $toFilename;
     $this->Folder = $toFolder;
     $extension = pathinfo($this->Filename, PATHINFO_EXTENSION);
     if (empty($this->Title)) {
         // don't overwrite existing document titles
         $this->Title = basename($filePath, '.' . $extension);
     }
     $this->LastChanged = SS_Datetime::now()->Rfc2822();
     $this->write();
     return $this;
 }
 /**
  * Relate an existing file on the filesystem to the document.
  * Copies the file to the new destination, as defined in {@link get_DMS_path()}.
  *
  * @param String Path to file, relative to webroot.
  */
 function storeDocument($filePath)
 {
     if (empty($this->ID)) {
         user_error("Document must be written to database before it can store documents", E_USER_ERROR);
     }
     //calculate all the path to copy the file to
     $fromFilename = basename($filePath);
     $toFilename = $this->ID . '~' . $fromFilename;
     //add the docID to the start of the Filename
     $toFolder = DMS::get_storage_folder($this->ID);
     $toPath = DMS::get_dms_path() . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
     DMS::create_storage_folder(DMS::get_dms_path() . DIRECTORY_SEPARATOR . $toFolder);
     //copy the file into place
     $fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath;
     copy($fromPath, $toPath);
     //this will overwrite the existing file (if present)
     //write the filename of the stored document
     $this->Filename = $toFilename;
     $this->Folder = $toFolder;
     $extension = pathinfo($this->Filename, PATHINFO_EXTENSION);
     if (empty($this->Title)) {
         $this->Title = basename($filePath, '.' . $extension);
     }
     //don't overwrite existing document titles
     $this->LastChanged = SS_Datetime::now()->Rfc2822();
     $this->write();
     return $this;
 }
 /**
  * Returns the full filename of the document stored in this object. Can
  * optionally specify which filename to use at the end.
  *
  * @param string
  *
  * @return string
  */
 public function getFullPath($filename = null)
 {
     if (!$filename) {
         $filename = $this->Filename;
     }
     return DMS::get_dms_path() . DIRECTORY_SEPARATOR . $this->Folder . DIRECTORY_SEPARATOR . $filename;
 }