2012-07-16 08:21:48 +02:00
|
|
|
<?php
|
2015-12-17 19:48:37 +01:00
|
|
|
class DMSTest extends FunctionalTest
|
|
|
|
{
|
2017-05-02 04:49:41 +02:00
|
|
|
protected static $fixture_file = 'dmstest.yml';
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
/**
|
|
|
|
* Stub PDF files for testing
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
public static $testFile = 'dms/tests/DMS-test-lorum-file.pdf';
|
|
|
|
public static $testFile2 = 'dms/tests/DMS-test-document-2.pdf';
|
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
/**
|
|
|
|
* Store values to reset back to after this test runs
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
public static $dmsFolderOld;
|
|
|
|
public static $dmsFolderSizeOld;
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/**
|
|
|
|
* @var DMS
|
|
|
|
*/
|
|
|
|
protected $dms;
|
|
|
|
|
2015-12-17 19:48:37 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
self::$dmsFolderOld = DMS::$dmsFolder;
|
|
|
|
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');
|
2017-05-02 04:49:41 +02:00
|
|
|
|
|
|
|
$this->dms = DMS::inst();
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
/**
|
|
|
|
* Delete a file that was created during a unit test
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
$file = self::$testFile;
|
2017-05-02 04:49:41 +02:00
|
|
|
$document = $this->dms->storeDocument($file);
|
2015-12-17 19:48:37 +01:00
|
|
|
|
|
|
|
$this->assertNotNull($document, "Document object created");
|
2017-05-01 05:54:48 +02:00
|
|
|
$this->assertTrue(
|
|
|
|
file_exists(
|
|
|
|
DMS::get_dms_path() . DIRECTORY_SEPARATOR . $document->Folder
|
|
|
|
. DIRECTORY_SEPARATOR . $document->Filename
|
|
|
|
),
|
|
|
|
"Document file copied into DMS folder"
|
|
|
|
);
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDMSFolderSpanning()
|
|
|
|
{
|
|
|
|
DMS::$dmsFolderSize = 5;
|
|
|
|
$file = self::$testFile;
|
|
|
|
|
|
|
|
$documents = array();
|
|
|
|
for ($i = 0; $i <= 16; $i++) {
|
2017-05-02 04:49:41 +02:00
|
|
|
$document = $this->dms->storeDocument($file);
|
2015-12-17 19:48:37 +01:00
|
|
|
$this->assertNotNull($document, "Document object created on run number: $i");
|
|
|
|
$this->assertTrue(file_exists($document->getFullPath()));
|
|
|
|
$documents[] = $document;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
// Test document objects have their folders set
|
2015-12-17 19:48:37 +01:00
|
|
|
$folders = array();
|
|
|
|
for ($i = 0; $i <= 16; $i++) {
|
|
|
|
$folderName = $documents[$i]->Folder;
|
2017-05-01 05:54:48 +02:00
|
|
|
$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'"
|
|
|
|
);
|
2015-12-17 19:48:37 +01:00
|
|
|
$folders[] = $folderName;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
// Test we created 4 folder to contain the 17 files
|
2015-12-17 19:48:37 +01:00
|
|
|
foreach ($folders as $f) {
|
|
|
|
$this->assertTrue(is_dir(DMS::get_dms_path() . DIRECTORY_SEPARATOR . $f), "Document folder '$f' exists");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReplaceDocument()
|
|
|
|
{
|
2017-05-01 05:54:48 +02:00
|
|
|
// Store the first document
|
2017-05-02 04:49:41 +02:00
|
|
|
$document = $this->dms->storeDocument(self::$testFile);
|
2015-12-17 19:48:37 +01:00
|
|
|
$document->Title = "My custom title";
|
|
|
|
$document->Description = "My custom description";
|
|
|
|
$document->write();
|
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
// Then overwrite with a second document
|
2015-12-17 19:48:37 +01:00
|
|
|
$document = $document->replaceDocument(self::$testFile2);
|
|
|
|
|
|
|
|
$this->assertNotNull($document, "Document object created");
|
2017-05-01 05:54:48 +02:00
|
|
|
$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"
|
|
|
|
);
|
2015-12-17 19:48:37 +01:00
|
|
|
$this->assertEquals("My custom title", $document->Title, "Custom title not modified");
|
|
|
|
$this->assertEquals("My custom description", $document->Description, "Custom description not modified");
|
|
|
|
}
|
2017-05-02 04:49:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that documents can be returned by a given page
|
|
|
|
*/
|
|
|
|
public function testGetByPageWithoutEmbargoes()
|
|
|
|
{
|
|
|
|
$pageWithEmbargoes = $this->objFromFixture('SiteTree', 's3');
|
|
|
|
$documents = $this->dms->getByPage($pageWithEmbargoes);
|
2017-05-05 05:35:39 +02:00
|
|
|
// Fixture: 6 documents in set, 1 is embargoed
|
|
|
|
$this->assertCount(5, $documents, 'Embargoed documents are excluded by default');
|
2017-05-02 04:49:41 +02:00
|
|
|
$this->assertContainsOnlyInstancesOf('DMSDocument', $documents);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that embargoed documents are excluded from getByPage
|
|
|
|
*/
|
|
|
|
public function testGetByPageWithEmbargoedDocuments()
|
|
|
|
{
|
|
|
|
$pageWithEmbargoes = $this->objFromFixture('SiteTree', 's3');
|
|
|
|
$documents = $this->dms->getByPage($pageWithEmbargoes, true);
|
2017-05-05 05:35:39 +02:00
|
|
|
// Fixture: 6 documents in set, 1 is embargoed
|
|
|
|
$this->assertCount(6, $documents, 'Embargoed documents can be included');
|
2017-05-02 04:49:41 +02:00
|
|
|
$this->assertContainsOnlyInstancesOf('DMSDocument', $documents);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that document sets can be retrieved for a given page
|
|
|
|
*/
|
|
|
|
public function testGetDocumentSetsByPage()
|
|
|
|
{
|
|
|
|
$page = $this->objFromFixture('SiteTree', 's1');
|
|
|
|
$sets = $this->dms->getDocumentSetsByPage($page);
|
|
|
|
$this->assertCount(2, $sets);
|
|
|
|
$this->assertContainsOnlyInstancesOf('DMSDocumentSet', $sets);
|
|
|
|
}
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|