diff --git a/code/DMS.php b/code/DMS.php index aaa9a54..92e6b45 100644 --- a/code/DMS.php +++ b/code/DMS.php @@ -45,14 +45,15 @@ class DMS implements DMSInterface { $fromFilename = basename($fromPath); $toFilename = $docID . '~' . $fromFilename; //add the docID to the start of the Filename $toFolder = self::getStorageFolder($docID); - $toPath = $toFolder . DIRECTORY_SEPARATOR . $toFilename; - $this->createStorageFolder($toFolder); + $toPath = self::$dmsPath . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename; + $this->createStorageFolder(self::$dmsPath . DIRECTORY_SEPARATOR . $toFolder); //copy the file into place - copy($fromPath, self::$dmsPath . DIRECTORY_SEPARATOR . $toPath); + copy($fromPath, $toPath); //write the filename of the stored document - $doc->Filename = $toPath; + $doc->Filename = $toFilename; + $doc->Folder = $toFolder; $doc->write(); //set an initial title for the document from the filename @@ -110,6 +111,6 @@ class DMS implements DMSInterface { */ static function getStorageFolder($id) { $folderName = intval($id / self::$dmsFolderSize); - return self::$dmsPath . DIRECTORY_SEPARATOR . $folderName; + return $folderName; } } \ No newline at end of file diff --git a/code/DMSDocument.php b/code/DMSDocument.php index 90e7bdf..9ac415f 100644 --- a/code/DMSDocument.php +++ b/code/DMSDocument.php @@ -3,6 +3,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { static $db = array( "Filename" => "Text", + "Folder" => "Text" ); /** @@ -184,4 +185,12 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { // TODO: Implement getVersions() method. } + /** + * Returns the full filename of the document stored in this object + * @return string + */ + function getFullPath() { + return DMS::$dmsPath . DIRECTORY_SEPARATOR . $this->Folder . DIRECTORY_SEPARATOR . $this->Filename; + } + } \ No newline at end of file diff --git a/tests/DMSTest.php b/tests/DMSTest.php index 2b7621c..e8b0f85 100644 --- a/tests/DMSTest.php +++ b/tests/DMSTest.php @@ -1,7 +1,7 @@ getBasename(), array('.', '..'))) { - continue; - } elseif ($file->isDir()) { - rmdir($file->getPathname()); - } elseif ($file->isFile() || $file->isLink()) { - unlink($file->getPathname()); - } - } - rmdir($path); + if (file_exists($path) || is_dir($path)) { + $it = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($path), + RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ($it as $file) { + if (in_array($file->getBasename(), array('.', '..'))) { + continue; + } elseif ($file->isDir()) { + rmdir($file->getPathname()); + } elseif ($file->isFile() || $file->isLink()) { + unlink($file->getPathname()); + } + } + rmdir($path); + } } @@ -52,7 +54,7 @@ class DMSTest extends SapphireTest { $document = $dms->storeDocument($file); $this->assertNotNull($document, "Document object created"); - $this->assertTrue(file_exists(DMS::$dmsPath . DIRECTORY_SEPARATOR . $document->Filename),"Document file copied into DMS folder"); + $this->assertTrue(file_exists(DMS::$dmsPath . DIRECTORY_SEPARATOR . $document->Folder . DIRECTORY_SEPARATOR . $document->Filename),"Document file copied into DMS folder"); //$title = $document->getTag('title'); } @@ -63,17 +65,26 @@ class DMSTest extends SapphireTest { $file = BASE_PATH . DIRECTORY_SEPARATOR . self::$testFile; + $documents = array(); for($i = 0; $i <= 16; $i++) { $document = $dms->storeDocument($file); $this->assertNotNull($document, "Document object created on run number: $i"); + $this->assertTrue(file_exists($document->getFullPath())); + $documents[] = $document; + } + + //test document objects have their folders set + $folders = array(); + for($i = 0; $i <= 16; $i++) { + $folderName = $documents[$i]->Folder; + $this->assertTrue(strpos($documents[$i]->getFullPath(), DIRECTORY_SEPARATOR . $folderName . DIRECTORY_SEPARATOR) !== false, "Correct folder name for the documents. Document path contains reference to folder name '$folderName'"); + $folders[] = $folderName; } //test we created 4 folder to contain the 17 files - $this->assertTrue(is_dir(DMS::$dmsPath . DIRECTORY_SEPARATOR . '1')); - $this->assertTrue(is_dir(DMS::$dmsPath . DIRECTORY_SEPARATOR . '2')); - $this->assertTrue(is_dir(DMS::$dmsPath . DIRECTORY_SEPARATOR . '3')); - $this->assertTrue(is_dir(DMS::$dmsPath . DIRECTORY_SEPARATOR . '4')); - $this->assertFalse(is_dir(DMS::$dmsPath . DIRECTORY_SEPARATOR . '5')); + foreach($folders as $f) { + $this->assertTrue(is_dir(DMS::$dmsPath . DIRECTORY_SEPARATOR . $f),"Document folder '$f' exists"); + } }