2012-07-16 08:21:48 +02:00
|
|
|
<?php
|
2019-04-23 08:22:18 +02:00
|
|
|
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use Sunnysideup\DMS\DMS;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use Sunnysideup\DMS\Model\DMSDocument;
|
|
|
|
use Sunnysideup\DMS\Model\DMSDocumentSet;
|
|
|
|
use SilverStripe\Assets\Filesystem;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
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
|
2017-05-17 06:24:25 +02:00
|
|
|
*
|
2017-05-01 05:54:48 +02:00
|
|
|
* @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
|
|
|
/**
|
2017-05-17 06:24:25 +02:00
|
|
|
* The test folder to write assets into
|
|
|
|
*
|
|
|
|
* @var string
|
2017-05-01 05:54:48 +02:00
|
|
|
*/
|
2017-05-17 06:24:25 +02:00
|
|
|
protected $testDmsPath = 'assets/_dms-assets-test-1234';
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/**
|
2017-05-17 06:24:25 +02:00
|
|
|
* @var DMSInterace
|
2017-05-02 04:49:41 +02:00
|
|
|
*/
|
|
|
|
protected $dms;
|
|
|
|
|
2017-05-17 06:24:25 +02:00
|
|
|
/**
|
|
|
|
* Use a test DMS folder, so we don't overwrite the live one, and clear it out in case of previous broken tests
|
|
|
|
*
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2015-12-17 19:48:37 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-04-23 08:22:18 +02:00
|
|
|
Config::modify()->update(DMS::class, 'folder_name', $this->testDmsPath);
|
2017-05-17 06:24:25 +02:00
|
|
|
DMSFilesystemTestHelper::delete($this->testDmsPath);
|
2017-05-02 04:49:41 +02:00
|
|
|
$this->dms = DMS::inst();
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
/**
|
2017-05-17 06:24:25 +02:00
|
|
|
* Delete the test folder after the test runs
|
2017-05-01 05:54:48 +02:00
|
|
|
*
|
2017-05-17 06:24:25 +02:00
|
|
|
* {@inheritDoc}
|
2017-05-01 05:54:48 +02:00
|
|
|
*/
|
2017-05-17 06:24:25 +02:00
|
|
|
public function tearDown()
|
2015-12-17 19:48:37 +01:00
|
|
|
{
|
2017-05-17 06:24:25 +02:00
|
|
|
parent::tearDown();
|
|
|
|
DMSFilesystemTestHelper::delete($this->testDmsPath);
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2017-05-17 06:24:25 +02:00
|
|
|
DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $document->Folder
|
2017-05-01 05:54:48 +02:00
|
|
|
. DIRECTORY_SEPARATOR . $document->Filename
|
|
|
|
),
|
|
|
|
"Document file copied into DMS folder"
|
|
|
|
);
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDMSFolderSpanning()
|
|
|
|
{
|
2019-04-23 08:22:18 +02:00
|
|
|
Config::modify()->update(DMS::class, 'folder_size', 5);
|
2015-12-17 19:48:37 +01:00
|
|
|
$file = self::$testFile;
|
|
|
|
|
2019-04-23 08:19:18 +02:00
|
|
|
$documents = [];
|
2015-12-17 19:48:37 +01:00
|
|
|
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");
|
2019-04-23 08:19:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ### @@@@ START REPLACEMENT @@@@ ###
|
|
|
|
* WHY: upgrade to SS4
|
|
|
|
* OLD: ->getFullPath() (case sensitive)
|
|
|
|
* NEW: ->getFilename() (COMPLEX)
|
|
|
|
* EXP: You may need to add ASSETS_PATH."/" in front of this ...
|
|
|
|
* ### @@@@ STOP REPLACEMENT @@@@ ###
|
|
|
|
*/
|
|
|
|
$this->assertTrue(file_exists($document->getFilename()));
|
2015-12-17 19:48:37 +01:00
|
|
|
$documents[] = $document;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
// Test document objects have their folders set
|
2019-04-23 08:19:18 +02:00
|
|
|
$folders = [];
|
2015-12-17 19:48:37 +01:00
|
|
|
for ($i = 0; $i <= 16; $i++) {
|
|
|
|
$folderName = $documents[$i]->Folder;
|
2017-05-01 05:54:48 +02:00
|
|
|
$this->assertTrue(
|
2019-04-23 08:19:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ### @@@@ START REPLACEMENT @@@@ ###
|
|
|
|
* WHY: upgrade to SS4
|
|
|
|
* OLD: ->getFullPath() (case sensitive)
|
|
|
|
* NEW: ->getFilename() (COMPLEX)
|
|
|
|
* EXP: You may need to add ASSETS_PATH."/" in front of this ...
|
|
|
|
* ### @@@@ STOP REPLACEMENT @@@@ ###
|
|
|
|
*/
|
|
|
|
strpos($documents[$i]->getFilename(), DIRECTORY_SEPARATOR . $folderName . DIRECTORY_SEPARATOR) !== false,
|
2017-05-01 05:54:48 +02:00
|
|
|
"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) {
|
2017-05-17 06:24:25 +02:00
|
|
|
$this->assertTrue(
|
|
|
|
is_dir(DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $f),
|
|
|
|
"Document folder '$f' exists"
|
|
|
|
);
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2017-05-17 06:24:25 +02:00
|
|
|
DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $document->Folder
|
2017-05-01 05:54:48 +02:00
|
|
|
. 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()
|
|
|
|
{
|
2019-04-23 08:22:18 +02:00
|
|
|
$pageWithEmbargoes = $this->objFromFixture(SiteTree::class, 's3');
|
2017-05-02 04:49:41 +02:00
|
|
|
$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');
|
2019-04-23 08:22:18 +02:00
|
|
|
$this->assertContainsOnlyInstancesOf(DMSDocument::class, $documents);
|
2017-05-02 04:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that embargoed documents are excluded from getByPage
|
|
|
|
*/
|
|
|
|
public function testGetByPageWithEmbargoedDocuments()
|
|
|
|
{
|
2019-04-23 08:22:18 +02:00
|
|
|
$pageWithEmbargoes = $this->objFromFixture(SiteTree::class, 's3');
|
2017-05-02 04:49:41 +02:00
|
|
|
$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');
|
2019-04-23 08:22:18 +02:00
|
|
|
$this->assertContainsOnlyInstancesOf(DMSDocument::class, $documents);
|
2017-05-02 04:49:41 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 07:24:50 +02:00
|
|
|
/**
|
|
|
|
* Ensure the shortcode handler key is configurable
|
|
|
|
*/
|
|
|
|
public function testShortcodeHandlerKeyIsConfigurable()
|
|
|
|
{
|
2019-04-23 08:22:18 +02:00
|
|
|
Config::modify()->update(DMS::class, 'shortcode_handler_key', 'testing');
|
2017-05-17 07:24:50 +02:00
|
|
|
$this->assertSame('testing', DMS::inst()->getShortcodeHandlerKey());
|
|
|
|
}
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
/**
|
|
|
|
* Test that document sets can be retrieved for a given page
|
|
|
|
*/
|
|
|
|
public function testGetDocumentSetsByPage()
|
|
|
|
{
|
2019-04-23 08:22:18 +02:00
|
|
|
$page = $this->objFromFixture(SiteTree::class, 's1');
|
2017-05-02 04:49:41 +02:00
|
|
|
$sets = $this->dms->getDocumentSetsByPage($page);
|
|
|
|
$this->assertCount(2, $sets);
|
2019-04-23 08:22:18 +02:00
|
|
|
$this->assertContainsOnlyInstancesOf(DMSDocumentSet::class, $sets);
|
2017-05-02 04:49:41 +02:00
|
|
|
}
|
2017-07-31 11:35:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that assets/* folders are not included in filesystem sync operations
|
|
|
|
*/
|
|
|
|
public function testFolderExcludedFromFilesystemSync()
|
|
|
|
{
|
|
|
|
// Undo setup config changes
|
|
|
|
Config::unnest();
|
|
|
|
Config::nest();
|
|
|
|
|
|
|
|
$result = Filesystem::config()->get('sync_blacklisted_patterns');
|
|
|
|
$folderName = substr(DMS::config()->get('folder_name'), 7);
|
|
|
|
$this->assertContains('/^' . $folderName . '$/i', $result);
|
|
|
|
}
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|