mirror of
https://github.com/silverstripe/silverstripe-dms
synced 2024-10-22 14:05:56 +02:00
Merge pull request #127 from robbieaverill/feature/change-storage-path
API Make storage path configurable. Fix instance singleton and remove static methods.
This commit is contained in:
commit
e4ab2b0861
12
_config/config.yml
Normal file
12
_config/config.yml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
---
|
||||||
|
Name: dmsconfig
|
||||||
|
---
|
||||||
|
SiteTree:
|
||||||
|
extensions:
|
||||||
|
- DMSSiteTreeExtension
|
||||||
|
# Whether to show the document sets tab in the CMS for the page type this extension is applied to
|
||||||
|
documents_enabled: true
|
||||||
|
|
||||||
|
HtmlEditorField_Toolbar:
|
||||||
|
extensions:
|
||||||
|
- DocumentHtmlEditorFieldToolbar
|
@ -6,15 +6,5 @@ Director:
|
|||||||
rules:
|
rules:
|
||||||
'dmsdocument/$ID' : 'DMSDocument_Controller'
|
'dmsdocument/$ID' : 'DMSDocument_Controller'
|
||||||
|
|
||||||
SiteTree:
|
|
||||||
extensions:
|
|
||||||
- DMSSiteTreeExtension
|
|
||||||
# Whether to show the document sets tab in the CMS for the page type this extension is applied to
|
|
||||||
documents_enabled: true
|
|
||||||
|
|
||||||
HtmlEditorField_Toolbar:
|
|
||||||
extensions:
|
|
||||||
- DocumentHtmlEditorFieldToolbar
|
|
||||||
|
|
||||||
DMSDocument_versions:
|
DMSDocument_versions:
|
||||||
enable_versions: true
|
enable_versions: true
|
||||||
|
99
code/DMS.php
99
code/DMS.php
@ -1,21 +1,30 @@
|
|||||||
<?php
|
<?php
|
||||||
class DMS implements DMSInterface
|
class DMS extends Object implements DMSInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Folder to store the documents in
|
* Folder to store the documents in
|
||||||
*
|
*
|
||||||
|
* @config
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public static $dmsFolder = 'dms-assets';
|
private static $folder_name = 'assets/_dmsassets';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How many documents to store in a single folder. The square of this number is the maximum number of documents.
|
* How many documents to store in a single folder. The square of this number is the maximum number of documents.
|
||||||
*
|
*
|
||||||
* The number should be a multiple of 10
|
* The number should be a multiple of 10
|
||||||
*
|
*
|
||||||
|
* @config
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
public static $dmsFolderSize = 1000;
|
private static $folder_size = 1000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton instance of a DMSInterface
|
||||||
|
*
|
||||||
|
* @var DMSInterface
|
||||||
|
*/
|
||||||
|
private static $instance;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The shortcode handler key. Can be changed by user code.
|
* The shortcode handler key. Can be changed by user code.
|
||||||
@ -27,44 +36,56 @@ class DMS implements DMSInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory method that returns an instance of the DMS. This could be any class that implements the DMSInterface.
|
* Factory method that returns an instance of the DMS. This could be any class that implements the DMSInterface.
|
||||||
* @static
|
*
|
||||||
* @return DMSInterface An instance of the Document Management System
|
* @return DMSInterface An instance of the Document Management System
|
||||||
*/
|
*/
|
||||||
public static function inst()
|
public static function inst()
|
||||||
{
|
{
|
||||||
$dmsPath = self::get_dms_path();
|
if (!self::$instance) {
|
||||||
|
self::$instance = new static();
|
||||||
|
|
||||||
$dms = new DMS();
|
$dmsPath = self::$instance->getStoragePath();
|
||||||
if (!is_dir($dmsPath)) {
|
|
||||||
self::create_storage_folder($dmsPath);
|
if (!is_dir($dmsPath)) {
|
||||||
|
self::$instance->createStorageFolder($dmsPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file_exists($dmsPath . DIRECTORY_SEPARATOR . '.htaccess')) {
|
||||||
|
// Restrict access to the storage folder
|
||||||
|
copy(
|
||||||
|
BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR . DIRECTORY_SEPARATOR
|
||||||
|
. 'resources' . DIRECTORY_SEPARATOR . '.htaccess',
|
||||||
|
$dmsPath . DIRECTORY_SEPARATOR . '.htaccess'
|
||||||
|
);
|
||||||
|
|
||||||
|
copy(
|
||||||
|
BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR . DIRECTORY_SEPARATOR
|
||||||
|
. 'resources' . DIRECTORY_SEPARATOR . 'web.config',
|
||||||
|
$dmsPath . DIRECTORY_SEPARATOR . 'web.config'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return self::$instance;
|
||||||
if (!file_exists($dmsPath . DIRECTORY_SEPARATOR . '.htaccess')) {
|
|
||||||
// Restrict access to the storage folder
|
|
||||||
copy(
|
|
||||||
BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR . DIRECTORY_SEPARATOR
|
|
||||||
. 'resources' . DIRECTORY_SEPARATOR . '.htaccess',
|
|
||||||
$dmsPath . DIRECTORY_SEPARATOR . '.htaccess'
|
|
||||||
);
|
|
||||||
|
|
||||||
copy(
|
|
||||||
BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR . DIRECTORY_SEPARATOR
|
|
||||||
. 'resources' . DIRECTORY_SEPARATOR . 'web.config',
|
|
||||||
$dmsPath . DIRECTORY_SEPARATOR . 'web.config'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return $dms;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Get the storage path for DMS documents
|
||||||
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function get_dms_path()
|
public function getStoragePath()
|
||||||
{
|
{
|
||||||
return BASE_PATH . DIRECTORY_SEPARATOR . self::$dmsFolder;
|
return BASE_PATH . DIRECTORY_SEPARATOR . $this->config()->get('folder_name');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function transform_file_to_file_path($file)
|
/**
|
||||||
|
* Gets a file path from either a File or a string
|
||||||
|
*
|
||||||
|
* @param string|File $file
|
||||||
|
* @return string
|
||||||
|
* @throws FileNotFoundException If an unexpected value was provided, or the filename was null
|
||||||
|
*/
|
||||||
|
public function transformFileToFilePath($file)
|
||||||
{
|
{
|
||||||
//confirm we have a file
|
//confirm we have a file
|
||||||
$filePath = null;
|
$filePath = null;
|
||||||
@ -84,16 +105,16 @@ class DMS implements DMSInterface
|
|||||||
/**
|
/**
|
||||||
* Takes a File object or a String (path to a file) and copies it into the DMS. The original file remains unchanged.
|
* Takes a File object or a String (path to a file) and copies it into the DMS. The original file remains unchanged.
|
||||||
* When storing a document, sets the fields on the File has "tag" metadata.
|
* When storing a document, sets the fields on the File has "tag" metadata.
|
||||||
* @param $file File object, or String that is path to a file to store,
|
* @param File|string $file File object, or String that is path to a file to store,
|
||||||
* e.g. "assets/documents/industry/supplied-v1-0.pdf"
|
* e.g. "assets/documents/industry/supplied-v1-0.pdf"
|
||||||
* @return DMSDocument
|
* @return DMSDocument
|
||||||
*/
|
*/
|
||||||
public function storeDocument($file)
|
public function storeDocument($file)
|
||||||
{
|
{
|
||||||
$filePath = self::transform_file_to_file_path($file);
|
$filePath = $this->transformFileToFilePath($file);
|
||||||
|
|
||||||
//create a new document and get its ID
|
// Create a new document and get its ID
|
||||||
$doc = new DMSDocument();
|
$doc = DMSDocument::create();
|
||||||
$doc->write();
|
$doc->write();
|
||||||
$doc->storeDocument($filePath);
|
$doc->storeDocument($filePath);
|
||||||
|
|
||||||
@ -156,22 +177,26 @@ class DMS implements DMSInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a storage folder for the given path
|
* Creates a storage folder for the given path
|
||||||
* @param $path Path to create a folder for
|
*
|
||||||
|
* @param string $path Path to create a folder for
|
||||||
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public static function create_storage_folder($path)
|
public function createStorageFolder($path)
|
||||||
{
|
{
|
||||||
if (!is_dir($path)) {
|
if (!is_dir($path)) {
|
||||||
mkdir($path, 0777);
|
mkdir($path, 0777, true);
|
||||||
}
|
}
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the storage path from a database DMSDocument ID
|
* Calculates the storage path from a database DMSDocument ID
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
public static function get_storage_folder($id)
|
public function getStorageFolder($id)
|
||||||
{
|
{
|
||||||
$folderName = intval($id / self::$dmsFolderSize);
|
return intval($id / self::config()->get('folder_size'));
|
||||||
return $folderName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -667,7 +667,8 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
|
|||||||
public function getFullPath()
|
public function getFullPath()
|
||||||
{
|
{
|
||||||
if ($this->Filename) {
|
if ($this->Filename) {
|
||||||
return DMS::get_dms_path() . DIRECTORY_SEPARATOR . $this->Folder . DIRECTORY_SEPARATOR . $this->Filename;
|
return DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR
|
||||||
|
. $this->Folder . DIRECTORY_SEPARATOR . $this->Filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@ -712,7 +713,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
|
|||||||
*/
|
*/
|
||||||
public function getStorageFolder()
|
public function getStorageFolder()
|
||||||
{
|
{
|
||||||
return DMS::get_dms_path() . DIRECTORY_SEPARATOR . DMS::get_storage_folder($this->ID);
|
return DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . DMS::inst()->getStorageFolder($this->ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -770,7 +771,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
|
|||||||
/**
|
/**
|
||||||
* Relate an existing file on the filesystem to the document.
|
* Relate an existing file on the filesystem to the document.
|
||||||
*
|
*
|
||||||
* Copies the file to the new destination, as defined in {@link get_DMS_path()}.
|
* Copies the file to the new destination, as defined in {@link DMS::getStoragePath()}.
|
||||||
*
|
*
|
||||||
* @param string $filePath Path to file, relative to webroot.
|
* @param string $filePath Path to file, relative to webroot.
|
||||||
*
|
*
|
||||||
@ -785,10 +786,10 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
|
|||||||
// calculate all the path to copy the file to
|
// calculate all the path to copy the file to
|
||||||
$fromFilename = basename($filePath);
|
$fromFilename = basename($filePath);
|
||||||
$toFilename = $this->ID. '~' . $fromFilename; //add the docID to the start of the Filename
|
$toFilename = $this->ID. '~' . $fromFilename; //add the docID to the start of the Filename
|
||||||
$toFolder = DMS::get_storage_folder($this->ID);
|
$toFolder = DMS::inst()->getStorageFolder($this->ID);
|
||||||
$toPath = DMS::get_dms_path() . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
|
$toPath = DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
|
||||||
|
|
||||||
DMS::create_storage_folder(DMS::get_dms_path() . DIRECTORY_SEPARATOR . $toFolder);
|
DMS::inst()->createStorageFolder(DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $toFolder);
|
||||||
|
|
||||||
//copy the file into place
|
//copy the file into place
|
||||||
$fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath;
|
$fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath;
|
||||||
@ -832,7 +833,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
|
|||||||
*/
|
*/
|
||||||
public function replaceDocument($file)
|
public function replaceDocument($file)
|
||||||
{
|
{
|
||||||
$filePath = DMS::transform_file_to_file_path($file);
|
$filePath = DMS::inst()->transformFileToFilePath($file);
|
||||||
$doc = $this->storeDocument($filePath); // replace the document
|
$doc = $this->storeDocument($filePath); // replace the document
|
||||||
|
|
||||||
return $doc;
|
return $doc;
|
||||||
|
@ -179,7 +179,7 @@ class DMSDocument_versions extends DataObject
|
|||||||
if (!$filename) {
|
if (!$filename) {
|
||||||
$filename = $this->Filename;
|
$filename = $this->Filename;
|
||||||
}
|
}
|
||||||
return DMS::get_dms_path() . DIRECTORY_SEPARATOR . $this->Folder . DIRECTORY_SEPARATOR . $filename;
|
return DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $this->Folder . DIRECTORY_SEPARATOR . $filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,6 +23,12 @@ having a Document Set intermediary (@todo Add a build task for this).
|
|||||||
* `DMSDocumentAddController::add_allowed_extensions` removed, use YAML configuration `DMSDocumentAddController::allowed_extensions` instead
|
* `DMSDocumentAddController::add_allowed_extensions` removed, use YAML configuration `DMSDocumentAddController::allowed_extensions` instead
|
||||||
* `DMSInterface` (and `DMS`) are stricter in the `getByPage` method, enforcing a `SiteTree` type hint
|
* `DMSInterface` (and `DMS`) are stricter in the `getByPage` method, enforcing a `SiteTree` type hint
|
||||||
* New method `DMSInterface::getDocumentSetsByPage` (and in `DMS`)
|
* New method `DMSInterface::getDocumentSetsByPage` (and in `DMS`)
|
||||||
|
* `DMS::$dmsFolder` removed, use YAML configuration `DMS.folder_name` instead
|
||||||
|
* `DMS::$dmsFolderSize` removed, use YAML configuration `DMS.folder_size` instead
|
||||||
|
* `DMS::get_dms_path` made non-static, use `DMS::inst()->getStoragePath()` instead
|
||||||
|
* `DMS::transform_file_to_file_path` made non-static, use `DMS::inst()->transformFileToFilePath()` instead
|
||||||
|
* `DMS::create_storage_folder` made non-static, use `DMS::inst()->createStorageFolder()` instead
|
||||||
|
* `DMS::get_storage_folder` made non-static, use `DMS::inst()->getStorageFolder()` instead
|
||||||
|
|
||||||
## Template changes
|
## Template changes
|
||||||
|
|
||||||
|
@ -1,6 +1,20 @@
|
|||||||
# Configuration
|
# Configuration
|
||||||
|
|
||||||
The file location is set via the `DMS::$dmsFolder` static, and points to a location in the webroot.
|
The file location is set via the `DMS.folder_name` configuation property, and points to a location in the webroot. By
|
||||||
|
default, this resides in an underscores folder within the assets folder. This means that automated snapshots/backups
|
||||||
|
(e.g. using [sspak](https://github.com/silverstripe/sspak)) can still handle DMS documents, but they will not show up
|
||||||
|
when navigating asset folders in the CMS.
|
||||||
|
|
||||||
|
## Changing the default storage folder
|
||||||
|
|
||||||
|
You can change the default storage folder location using YAML configuration. This folder would be relative to your
|
||||||
|
project root directory:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
DMS:
|
||||||
|
folder_name: my-custom-folder
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Enable/disable documents/sets for a specific page type
|
## Enable/disable documents/sets for a specific page type
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
class DMSDocumentControllerTest extends SapphireTest
|
class DMSDocumentControllerTest extends SapphireTest
|
||||||
{
|
{
|
||||||
protected static $fixture_file = "dmstest.yml";
|
protected static $fixture_file = 'dmstest.yml';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test that the download behaviour is either "open" or "download"
|
* Test that the download behaviour is either "open" or "download"
|
||||||
@ -16,7 +16,7 @@ class DMSDocumentControllerTest extends SapphireTest
|
|||||||
*/
|
*/
|
||||||
public function testDownloadBehaviourOpen($behaviour, $expectedDisposition)
|
public function testDownloadBehaviourOpen($behaviour, $expectedDisposition)
|
||||||
{
|
{
|
||||||
DMS::$dmsFolder = DMS_DIR; //sneakily setting the DMS folder to the folder where the test file lives
|
Config::inst()->update('DMS', 'folder_name', 'assets/_unit-test-123');
|
||||||
|
|
||||||
$this->logInWithPermission('ADMIN');
|
$this->logInWithPermission('ADMIN');
|
||||||
|
|
||||||
@ -33,15 +33,16 @@ class DMSDocumentControllerTest extends SapphireTest
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
$openDoc = new DMSDocument();
|
$openDoc = DMS::inst()->storeDocument('dms/tests/DMS-test-lorum-file.pdf');
|
||||||
$openDoc->Filename = "DMS-test-lorum-file.pdf";
|
|
||||||
$openDoc->Folder = "tests";
|
|
||||||
$openDoc->DownloadBehavior = $behaviour;
|
$openDoc->DownloadBehavior = $behaviour;
|
||||||
$openDoc->clearEmbargo(false);
|
$openDoc->clearEmbargo(false);
|
||||||
$openDoc->write();
|
$openDoc->write();
|
||||||
|
|
||||||
$request = new SS_HTTPRequest('GET', 'index/' . $openDoc->ID);
|
$request = new SS_HTTPRequest('GET', 'index/' . $openDoc->ID);
|
||||||
$request->match('index/$ID');
|
$request->match('index/$ID');
|
||||||
$controller->index($request);
|
$controller->index($request);
|
||||||
|
|
||||||
|
DMSFilesystemTestHelper::delete('assets/_unit-test-123');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,22 +3,6 @@ class DMSDocumentTest extends SapphireTest
|
|||||||
{
|
{
|
||||||
protected static $fixture_file = 'dmstest.yml';
|
protected static $fixture_file = 'dmstest.yml';
|
||||||
|
|
||||||
public function tearDownOnce()
|
|
||||||
{
|
|
||||||
self::$is_running_test = true;
|
|
||||||
|
|
||||||
$d = DataObject::get('DMSDocument');
|
|
||||||
foreach ($d as $d1) {
|
|
||||||
$d1->delete();
|
|
||||||
}
|
|
||||||
$t = DataObject::get('DMSTag');
|
|
||||||
foreach ($t as $t1) {
|
|
||||||
$t1->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
self::$is_running_test = $this->originalIsRunningTest;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testDefaultDownloadBehabiourCMSFields()
|
public function testDefaultDownloadBehabiourCMSFields()
|
||||||
{
|
{
|
||||||
$document = singleton('DMSDocument');
|
$document = singleton('DMSDocument');
|
||||||
@ -259,4 +243,35 @@ class DMSDocumentTest extends SapphireTest
|
|||||||
$d2 = $this->objFromFixture('DMSDocument', 'd2');
|
$d2 = $this->objFromFixture('DMSDocument', 'd2');
|
||||||
$this->assertSame('File That Doesn\'t Exist (Title)', $d2->getTitle());
|
$this->assertSame('File That Doesn\'t Exist (Title)', $d2->getTitle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure that the folder a document's file is stored in can be retrieved, and that delete() will also delete
|
||||||
|
* the file and the record
|
||||||
|
*/
|
||||||
|
public function testGetStorageFolderThenDelete()
|
||||||
|
{
|
||||||
|
Config::inst()->update('DMS', 'folder_name', 'assets/_unit-tests');
|
||||||
|
|
||||||
|
$document = DMS::inst()->storeDocument('dms/tests/DMS-test-lorum-file.pdf');
|
||||||
|
$filename = $document->getStorageFolder() . '/' . $document->getFileName();
|
||||||
|
|
||||||
|
$this->assertTrue(file_exists($filename));
|
||||||
|
$document->delete();
|
||||||
|
$this->assertFalse($document->exists());
|
||||||
|
$this->assertFalse(file_exists($filename));
|
||||||
|
|
||||||
|
DMSFilesystemTestHelper::delete('assets/_unit-tests');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure that the description can be returned in HTML format
|
||||||
|
*/
|
||||||
|
public function testGetDescriptionWithLineBreak()
|
||||||
|
{
|
||||||
|
$document = DMSDocument::create();
|
||||||
|
$document->Description = "Line 1\nLine 2\nLine 3";
|
||||||
|
$document->write();
|
||||||
|
|
||||||
|
$this->assertSame("Line 1<br />\nLine 2<br />\nLine 3", $document->getDescriptionWithLineBreak());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
class DMSEmbargoTest extends SapphireTest
|
class DMSEmbargoTest extends SapphireTest
|
||||||
{
|
{
|
||||||
public static $fixture_file = "dmsembargotest.yml";
|
protected static $fixture_file = 'dmsembargotest.yml';
|
||||||
|
|
||||||
public function tearDownOnce()
|
|
||||||
{
|
|
||||||
self::$is_running_test = true;
|
|
||||||
|
|
||||||
$d = DataObject::get("DMSDocument");
|
|
||||||
foreach ($d as $d1) {
|
|
||||||
$d1->delete();
|
|
||||||
}
|
|
||||||
$t = DataObject::get("DMSTag");
|
|
||||||
foreach ($t as $t1) {
|
|
||||||
$t1->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
self::$is_running_test = $this->originalIsRunningTest;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function createFakeHTTPRequest($id)
|
public function createFakeHTTPRequest($id)
|
||||||
{
|
{
|
||||||
@ -28,13 +12,10 @@ class DMSEmbargoTest extends SapphireTest
|
|||||||
|
|
||||||
public function testBasicEmbargo()
|
public function testBasicEmbargo()
|
||||||
{
|
{
|
||||||
$oldDMSFolder = DMS::$dmsFolder;
|
|
||||||
$oldTestMode = DMSDocument_Controller::$testMode;
|
$oldTestMode = DMSDocument_Controller::$testMode;
|
||||||
DMS::$dmsFolder = DMS_DIR; //sneakily setting the DMS folder to the folder where the test file lives
|
Config::inst()->update('DMS', 'folder_name', 'assets/_unit-test-123');
|
||||||
|
|
||||||
$doc = new DMSDocument();
|
$doc = DMS::inst()->storeDocument('dms/tests/DMS-test-lorum-file.pdf');
|
||||||
$doc->Filename = "DMS-test-lorum-file.pdf";
|
|
||||||
$doc->Folder = "tests";
|
|
||||||
$doc->CanViewType = 'LoggedInUsers';
|
$doc->CanViewType = 'LoggedInUsers';
|
||||||
$docID = $doc->write();
|
$docID = $doc->write();
|
||||||
|
|
||||||
@ -42,24 +23,24 @@ class DMSEmbargoTest extends SapphireTest
|
|||||||
$controller = new DMSDocument_Controller();
|
$controller = new DMSDocument_Controller();
|
||||||
DMSDocument_Controller::$testMode = true;
|
DMSDocument_Controller::$testMode = true;
|
||||||
$result = $controller->index($this->createFakeHTTPRequest($docID));
|
$result = $controller->index($this->createFakeHTTPRequest($docID));
|
||||||
$this->assertEquals($doc->getFullPath(), $result, "Correct underlying file returned (in test mode)");
|
$this->assertEquals($doc->getFullPath(), $result, 'Correct underlying file returned (in test mode)');
|
||||||
|
|
||||||
$doc->embargoIndefinitely();
|
$doc->embargoIndefinitely();
|
||||||
|
|
||||||
$this->logInWithPermission('ADMIN');
|
$this->logInWithPermission('ADMIN');
|
||||||
$result = $controller->index($this->createFakeHTTPRequest($docID));
|
$result = $controller->index($this->createFakeHTTPRequest($docID));
|
||||||
$this->assertEquals($doc->getFullPath(), $result, "Admins can still download embargoed files");
|
$this->assertEquals($doc->getFullPath(), $result, 'Admins can still download embargoed files');
|
||||||
|
|
||||||
$this->logInWithPermission('random-user-group');
|
$this->logInWithPermission('random-user-group');
|
||||||
$result = $controller->index($this->createFakeHTTPRequest($docID));
|
$result = $controller->index($this->createFakeHTTPRequest($docID));
|
||||||
$this->assertNotEquals(
|
$this->assertNotEquals(
|
||||||
$doc->getFullPath(),
|
$doc->getFullPath(),
|
||||||
$result,
|
$result,
|
||||||
"File no longer returned (in test mode) when switching to other user group"
|
'File no longer returned (in test mode) when switching to other user group'
|
||||||
);
|
);
|
||||||
|
|
||||||
DMS::$dmsFolder = $oldDMSFolder;
|
|
||||||
DMSDocument_Controller::$testMode = $oldTestMode;
|
DMSDocument_Controller::$testMode = $oldTestMode;
|
||||||
|
DMSFilesystemTestHelper::delete('assets/_unit-test-123');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEmbargoIndefinitely()
|
public function testEmbargoIndefinitely()
|
||||||
|
42
tests/DMSFilesystemTestHelper.php
Normal file
42
tests/DMSFilesystemTestHelper.php
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class DMSFilesystemTestHelper
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Files that are added to the DMS asset directory by the DMS instance. They will be removed after running tests.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected static $dmsFiles = array('.htaccess', 'web.config');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a directory and all files within it, or a file. Will automatically prepend the base path.
|
||||||
|
*
|
||||||
|
* This only work while a unit test is running for safety reasons.
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
*/
|
||||||
|
public static function delete($path)
|
||||||
|
{
|
||||||
|
if (!SapphireTest::is_running_test() || !file_exists($path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = BASE_PATH . DIRECTORY_SEPARATOR . $path;
|
||||||
|
if (is_dir($path)) {
|
||||||
|
$files = new RecursiveIteratorIterator(
|
||||||
|
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
|
||||||
|
RecursiveIteratorIterator::CHILD_FIRST
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($files as $fileinfo) {
|
||||||
|
$action = $fileinfo->isDir() ? 'rmdir' : 'unlink';
|
||||||
|
$action($fileinfo->getRealPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
rmdir($path);
|
||||||
|
} else {
|
||||||
|
unlink($path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,8 @@ class DMSShortcodeTest extends SapphireTest
|
|||||||
{
|
{
|
||||||
public function testShortcodeOperation()
|
public function testShortcodeOperation()
|
||||||
{
|
{
|
||||||
|
Config::inst()->update('DMS', 'folder_name', 'assets/_unit-test-123');
|
||||||
|
|
||||||
$file = 'dms/tests/DMS-test-lorum-file.pdf';
|
$file = 'dms/tests/DMS-test-lorum-file.pdf';
|
||||||
$document = DMS::inst()->storeDocument($file);
|
$document = DMS::inst()->storeDocument($file);
|
||||||
|
|
||||||
@ -23,5 +25,7 @@ class DMSShortcodeTest extends SapphireTest
|
|||||||
$this->assertStringEndsWith("/dmsdocument/$document->ID", $link->getAttribute('href'));
|
$this->assertStringEndsWith("/dmsdocument/$document->ID", $link->getAttribute('href'));
|
||||||
$this->assertEquals($document->getExtension(), $link->getAttribute('data-ext'));
|
$this->assertEquals($document->getExtension(), $link->getAttribute('data-ext'));
|
||||||
$this->assertEquals($document->getFileSizeFormatted(), $link->getAttribute('data-size'));
|
$this->assertEquals($document->getFileSizeFormatted(), $link->getAttribute('data-size'));
|
||||||
|
|
||||||
|
DMSFilesystemTestHelper::delete('assets/_unit-test-123');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,86 +5,46 @@ class DMSTest extends FunctionalTest
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Stub PDF files for testing
|
* Stub PDF files for testing
|
||||||
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public static $testFile = 'dms/tests/DMS-test-lorum-file.pdf';
|
public static $testFile = 'dms/tests/DMS-test-lorum-file.pdf';
|
||||||
public static $testFile2 = 'dms/tests/DMS-test-document-2.pdf';
|
public static $testFile2 = 'dms/tests/DMS-test-document-2.pdf';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store values to reset back to after this test runs
|
* The test folder to write assets into
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
public static $dmsFolderOld;
|
protected $testDmsPath = 'assets/_dms-assets-test-1234';
|
||||||
public static $dmsFolderSizeOld;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var DMS
|
* @var DMSInterace
|
||||||
*/
|
*/
|
||||||
protected $dms;
|
protected $dms;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use a test DMS folder, so we don't overwrite the live one, and clear it out in case of previous broken tests
|
||||||
|
*
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
Config::inst()->update('DMS', 'folder_name', $this->testDmsPath);
|
||||||
self::$dmsFolderOld = DMS::$dmsFolder;
|
DMSFilesystemTestHelper::delete($this->testDmsPath);
|
||||||
self::$dmsFolderSizeOld = DMS::$dmsFolderSize;
|
|
||||||
|
|
||||||
//use a test DMS folder, so we don't overwrite the live one
|
|
||||||
DMS::$dmsFolder = 'dms-assets-test-1234';
|
|
||||||
|
|
||||||
//clear out the test folder (in case a broken test doesn't delete it)
|
|
||||||
$this->delete(BASE_PATH . DIRECTORY_SEPARATOR . 'dms-assets-test-1234');
|
|
||||||
|
|
||||||
$this->dms = DMS::inst();
|
$this->dms = DMS::inst();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete the test folder after the test runs
|
||||||
|
*
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
public function tearDown()
|
public function tearDown()
|
||||||
{
|
{
|
||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
|
DMSFilesystemTestHelper::delete($this->testDmsPath);
|
||||||
self::$is_running_test = true;
|
|
||||||
|
|
||||||
$d = DataObject::get("DMSDocument");
|
|
||||||
foreach ($d as $d1) {
|
|
||||||
$d1->delete();
|
|
||||||
}
|
|
||||||
$t = DataObject::get("DMSTag");
|
|
||||||
foreach ($t as $t1) {
|
|
||||||
$t1->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
//delete the test folder after the test runs
|
|
||||||
$this->delete(BASE_PATH . DIRECTORY_SEPARATOR . 'dms-assets-test-1234');
|
|
||||||
|
|
||||||
//set the old DMS folder back again
|
|
||||||
DMS::$dmsFolder = self::$dmsFolderOld;
|
|
||||||
DMS::$dmsFolderSize = self::$dmsFolderSizeOld;
|
|
||||||
|
|
||||||
self::$is_running_test = $this->originalIsRunningTest;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete a file that was created during a unit test
|
|
||||||
*
|
|
||||||
* @param string $path
|
|
||||||
*/
|
|
||||||
public function delete($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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDMSStorage()
|
public function testDMSStorage()
|
||||||
@ -95,7 +55,7 @@ class DMSTest extends FunctionalTest
|
|||||||
$this->assertNotNull($document, "Document object created");
|
$this->assertNotNull($document, "Document object created");
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
file_exists(
|
file_exists(
|
||||||
DMS::get_dms_path() . DIRECTORY_SEPARATOR . $document->Folder
|
DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $document->Folder
|
||||||
. DIRECTORY_SEPARATOR . $document->Filename
|
. DIRECTORY_SEPARATOR . $document->Filename
|
||||||
),
|
),
|
||||||
"Document file copied into DMS folder"
|
"Document file copied into DMS folder"
|
||||||
@ -104,7 +64,7 @@ class DMSTest extends FunctionalTest
|
|||||||
|
|
||||||
public function testDMSFolderSpanning()
|
public function testDMSFolderSpanning()
|
||||||
{
|
{
|
||||||
DMS::$dmsFolderSize = 5;
|
Config::inst()->update('DMS', 'folder_size', 5);
|
||||||
$file = self::$testFile;
|
$file = self::$testFile;
|
||||||
|
|
||||||
$documents = array();
|
$documents = array();
|
||||||
@ -128,7 +88,10 @@ class DMSTest extends FunctionalTest
|
|||||||
|
|
||||||
// Test we created 4 folder to contain the 17 files
|
// Test we created 4 folder to contain the 17 files
|
||||||
foreach ($folders as $f) {
|
foreach ($folders as $f) {
|
||||||
$this->assertTrue(is_dir(DMS::get_dms_path() . DIRECTORY_SEPARATOR . $f), "Document folder '$f' exists");
|
$this->assertTrue(
|
||||||
|
is_dir(DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $f),
|
||||||
|
"Document folder '$f' exists"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +109,7 @@ class DMSTest extends FunctionalTest
|
|||||||
$this->assertNotNull($document, "Document object created");
|
$this->assertNotNull($document, "Document object created");
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
file_exists(
|
file_exists(
|
||||||
DMS::get_dms_path() . DIRECTORY_SEPARATOR . $document->Folder
|
DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $document->Folder
|
||||||
. DIRECTORY_SEPARATOR . $document->Filename
|
. DIRECTORY_SEPARATOR . $document->Filename
|
||||||
),
|
),
|
||||||
"Document file copied into DMS folder"
|
"Document file copied into DMS folder"
|
||||||
|
@ -10,74 +10,48 @@ class DMSVersioningTest extends SapphireTest
|
|||||||
public static $testFile = 'dms/tests/DMS-test-lorum-file.pdf';
|
public static $testFile = 'dms/tests/DMS-test-lorum-file.pdf';
|
||||||
public static $testFile2 = 'dms/tests/DMS-test-document-2.pdf';
|
public static $testFile2 = 'dms/tests/DMS-test-document-2.pdf';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The test folder to write assets into
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $testDmsPath = 'assets/_dms-assets-test-versions';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store values to reset back to after this test runs
|
* Store values to reset back to after this test runs
|
||||||
*/
|
*/
|
||||||
public static $dmsFolderOld;
|
|
||||||
public static $dmsFolderSizeOld;
|
|
||||||
public static $dmsEnableVersionsOld;
|
public static $dmsEnableVersionsOld;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use a test DMS folder, so we don't overwrite the live one, and clear out the test folder (in case a broken
|
||||||
|
* test doesn't delete it)
|
||||||
|
*
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
self::$dmsFolderOld = DMS::$dmsFolder;
|
|
||||||
self::$dmsFolderSizeOld = DMS::$dmsFolderSize;
|
|
||||||
self::$dmsEnableVersionsOld = DMSDocument_versions::$enable_versions;
|
self::$dmsEnableVersionsOld = DMSDocument_versions::$enable_versions;
|
||||||
DMSDocument_versions::$enable_versions = true;
|
DMSDocument_versions::$enable_versions = true;
|
||||||
|
|
||||||
//use a test DMS folder, so we don't overwrite the live one
|
Config::inst()->update('DMS', 'folder_name', $this->testDmsPath);
|
||||||
DMS::$dmsFolder = 'dms-assets-test-versions';
|
DMSFilesystemTestHelper::delete($this->testDmsPath);
|
||||||
|
|
||||||
//clear out the test folder (in case a broken test doesn't delete it)
|
|
||||||
$this->delete(BASE_PATH . DIRECTORY_SEPARATOR . 'dms-assets-test-versions');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function tearDown()
|
|
||||||
{
|
|
||||||
$d = DataObject::get("DMSDocument");
|
|
||||||
foreach ($d as $d1) {
|
|
||||||
$d1->delete();
|
|
||||||
}
|
|
||||||
$t = DataObject::get("DMSTag");
|
|
||||||
foreach ($t as $t1) {
|
|
||||||
$t1->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete the test folder after the test runs
|
|
||||||
$this->delete(BASE_PATH . DIRECTORY_SEPARATOR . 'dms-assets-test-versions');
|
|
||||||
|
|
||||||
parent::tearDown();
|
|
||||||
|
|
||||||
// Set the old DMS folder back again
|
|
||||||
DMS::$dmsFolder = self::$dmsFolderOld;
|
|
||||||
DMS::$dmsFolderSize = self::$dmsFolderSizeOld;
|
|
||||||
DMSDocument_versions::$enable_versions = self::$dmsEnableVersionsOld;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a file that was created during a unit test
|
* Delete the test folder after the tests run
|
||||||
*
|
*
|
||||||
* @param string $path
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
public function delete($path)
|
public function tearDown()
|
||||||
{
|
{
|
||||||
if (file_exists($path) || is_dir($path)) {
|
parent::tearDown();
|
||||||
$it = new RecursiveIteratorIterator(
|
|
||||||
new RecursiveDirectoryIterator($path),
|
DMSFilesystemTestHelper::delete($this->testDmsPath);
|
||||||
RecursiveIteratorIterator::CHILD_FIRST
|
|
||||||
);
|
// Set the old DMS folder back again
|
||||||
foreach ($it as $file) {
|
DMSDocument_versions::$enable_versions = self::$dmsEnableVersionsOld;
|
||||||
if (in_array($file->getBasename(), array('.', '..'))) {
|
|
||||||
continue;
|
|
||||||
} elseif ($file->isDir()) {
|
|
||||||
rmdir($file->getPathname());
|
|
||||||
} elseif ($file->isFile() || $file->isLink()) {
|
|
||||||
unlink($file->getPathname());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rmdir($path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDMSVersionStorage()
|
public function testDMSVersionStorage()
|
||||||
@ -91,7 +65,7 @@ class DMSVersioningTest extends SapphireTest
|
|||||||
$this->assertNotNull($document, "Document object created");
|
$this->assertNotNull($document, "Document object created");
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
file_exists(
|
file_exists(
|
||||||
DMS::get_dms_path() . DIRECTORY_SEPARATOR . $document->Folder
|
DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $document->Folder
|
||||||
. DIRECTORY_SEPARATOR . $document->Filename
|
. DIRECTORY_SEPARATOR . $document->Filename
|
||||||
),
|
),
|
||||||
"Document file copied into DMS folder"
|
"Document file copied into DMS folder"
|
||||||
|
Loading…
Reference in New Issue
Block a user