2012-07-17 07:58:33 +02:00
|
|
|
<?php
|
2015-12-17 19:48:37 +01:00
|
|
|
class DMSDocumentTest extends SapphireTest
|
|
|
|
{
|
2017-05-01 06:29:04 +02:00
|
|
|
protected static $fixture_file = 'dmstest.yml';
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
public function testDefaultDownloadBehabiourCMSFields()
|
|
|
|
{
|
2016-12-20 12:57:19 +01:00
|
|
|
$document = singleton('DMSDocument');
|
|
|
|
Config::inst()->update('DMSDocument', 'default_download_behaviour', 'open');
|
|
|
|
$cmsFields = $document->getCMSFields();
|
|
|
|
$this->assertEquals('open', $cmsFields->dataFieldByName('DownloadBehavior')->Value());
|
|
|
|
|
|
|
|
|
|
|
|
Config::inst()->update('DMSDocument', 'default_download_behaviour', 'download');
|
|
|
|
$cmsFields = $document->getCMSFields();
|
|
|
|
$this->assertEquals('download', $cmsFields->dataFieldByName('DownloadBehavior')->Value());
|
|
|
|
}
|
2017-05-01 06:29:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that related documents can be retrieved for a given DMS document
|
|
|
|
*/
|
|
|
|
public function testRelatedDocuments()
|
|
|
|
{
|
|
|
|
$document = $this->objFromFixture('DMSDocument', 'document_with_relations');
|
|
|
|
$this->assertGreaterThan(0, $document->RelatedDocuments()->count());
|
|
|
|
$this->assertEquals(
|
|
|
|
array('test-file-file-doesnt-exist-1', 'test-file-file-doesnt-exist-2'),
|
|
|
|
$document->getRelatedDocuments()->column('Filename')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the extensibility of getRelatedDocuments
|
|
|
|
*/
|
|
|
|
public function testGetRelatedDocumentsIsExtensible()
|
|
|
|
{
|
|
|
|
DMSDocument::add_extension('StubRelatedDocumentExtension');
|
|
|
|
|
|
|
|
$emptyDocument = new DMSDocument;
|
|
|
|
$relatedDocuments = $emptyDocument->getRelatedDocuments();
|
|
|
|
|
|
|
|
$this->assertCount(1, $relatedDocuments);
|
|
|
|
$this->assertSame('Extended', $relatedDocuments->first()->Filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the DMS Document CMS actions contains a grid field for managing related documents
|
|
|
|
*/
|
|
|
|
public function testDocumentHasCmsFieldForManagingRelatedDocuments()
|
|
|
|
{
|
|
|
|
$document = $this->objFromFixture('DMSDocument', 'document_with_relations');
|
2017-06-14 01:05:19 +02:00
|
|
|
$gridField = $this->getRelatedDocumentsGridField($document);
|
|
|
|
$this->assertInstanceOf('GridField', $gridField);
|
|
|
|
|
2017-05-09 00:59:33 +02:00
|
|
|
$gridFieldConfig = $gridField->getConfig();
|
|
|
|
|
|
|
|
$this->assertNotNull(
|
|
|
|
'GridFieldAddExistingAutocompleter',
|
|
|
|
$addExisting = $gridFieldConfig->getComponentByType('GridFieldAddExistingAutocompleter'),
|
|
|
|
'Related documents GridField has an "add existing" autocompleter'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNull(
|
|
|
|
$gridFieldConfig->getComponentByType('GridFieldAddNewButton'),
|
|
|
|
'Related documents GridField does not have an "add new" button'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-06-14 01:05:19 +02:00
|
|
|
/**
|
|
|
|
* Ensures that the DMS Document CMS Related and Versions fields are removed if user can't edit
|
|
|
|
*/
|
|
|
|
public function testDocumentHasNoCMSFieldsForManagingRelatedDocumentsIfCantEdit()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('another-user');
|
|
|
|
$document = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
|
|
|
|
$gridField = $this->getRelatedDocumentsGridField($document);
|
|
|
|
$this->assertNull($gridField);
|
|
|
|
}
|
|
|
|
|
2017-05-09 00:59:33 +02:00
|
|
|
/**
|
|
|
|
* Ensure that the related documents list does not include the current document itself
|
|
|
|
*/
|
|
|
|
public function testGetRelatedDocumentsForAutocompleter()
|
|
|
|
{
|
|
|
|
$document = $this->objFromFixture('DMSDocument', 'd1');
|
2017-06-14 01:05:19 +02:00
|
|
|
$gridField = $this->getRelatedDocumentsGridField($document);
|
|
|
|
$this->assertInstanceOf('GridField', $gridField);
|
2017-05-09 00:59:33 +02:00
|
|
|
|
|
|
|
$config = $gridField->getConfig();
|
|
|
|
|
|
|
|
$autocompleter = $config->getComponentByType('GridFieldAddExistingAutocompleter');
|
|
|
|
$autocompleter->setResultsFormat('$Filename');
|
2017-05-01 06:29:04 +02:00
|
|
|
|
2017-05-09 00:59:33 +02:00
|
|
|
$jsonResult = $autocompleter->doSearch(
|
|
|
|
$gridField,
|
|
|
|
new SS_HTTPRequest('GET', '/', array('gridfield_relationsearch' => 'test'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNotContains('test-file-file-doesnt-exist-1', $jsonResult);
|
|
|
|
$this->assertContains('test-file-file-doesnt-exist-2', $jsonResult);
|
2017-05-22 04:06:06 +02:00
|
|
|
$this->assertEquals(array('Title:PartialMatch', 'Filename:PartialMatch'), $autocompleter->getSearchFields());
|
2017-05-09 00:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return GridField
|
|
|
|
*/
|
2017-06-14 01:05:19 +02:00
|
|
|
protected function getRelatedDocumentsGridField(DMSDocument $document)
|
2017-05-09 00:59:33 +02:00
|
|
|
{
|
2017-05-01 06:29:04 +02:00
|
|
|
$documentFields = $document->getCMSFields();
|
|
|
|
/** @var FieldGroup $actions */
|
|
|
|
$actions = $documentFields->fieldByName('ActionsPanel');
|
|
|
|
|
|
|
|
$gridField = null;
|
|
|
|
foreach ($actions->getChildren() as $child) {
|
|
|
|
/** @var FieldGroup $child */
|
|
|
|
if ($gridField = $child->fieldByName('RelatedDocuments')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-09 00:59:33 +02:00
|
|
|
return $gridField;
|
2017-05-01 06:29:04 +02:00
|
|
|
}
|
2017-05-08 05:30:24 +02:00
|
|
|
|
2017-05-09 06:47:05 +02:00
|
|
|
/**
|
|
|
|
* Ensure that HTML is returned containing list items with action panel steps
|
|
|
|
*/
|
|
|
|
public function testGetActionTaskHtml()
|
|
|
|
{
|
|
|
|
$document = $this->objFromFixture('DMSDocument', 'd1');
|
|
|
|
$document->addActionPanelTask('example', 'Example');
|
|
|
|
|
|
|
|
$result = $document->getActionTaskHtml();
|
|
|
|
|
|
|
|
$this->assertContains('<label class="left">Actions</label>', $result);
|
2017-05-22 04:06:06 +02:00
|
|
|
$this->assertContains('<li class="ss-ui-button dmsdocument-action" data-panel="', $result);
|
2017-05-09 06:47:05 +02:00
|
|
|
$this->assertContains('permission', $result);
|
|
|
|
$this->assertContains('Example', $result);
|
2017-06-14 01:05:19 +02:00
|
|
|
|
|
|
|
$actions = array('example', 'embargo','find-usage');
|
|
|
|
foreach ($actions as $action) {
|
|
|
|
// Test remove with string
|
|
|
|
$document->removeActionPanelTask($action);
|
|
|
|
}
|
|
|
|
$result = $document->getActionTaskHtml();
|
|
|
|
|
|
|
|
$this->assertNotContains('Example', $result);
|
|
|
|
$this->assertNotContains('embargo', $result);
|
|
|
|
$this->assertNotContains('find-usage', $result);
|
|
|
|
// Positive test to see some action still remains
|
|
|
|
$this->assertContains('find-references', $result);
|
2017-05-09 06:47:05 +02:00
|
|
|
}
|
|
|
|
|
2017-05-08 05:30:24 +02:00
|
|
|
/*
|
|
|
|
* Tests whether the permissions fields are added
|
|
|
|
*/
|
2017-05-09 06:47:05 +02:00
|
|
|
public function testGetPermissionsActionPanel()
|
2017-05-08 05:30:24 +02:00
|
|
|
{
|
2017-05-09 06:47:05 +02:00
|
|
|
$result = $this->objFromFixture('DMSDocument', 'd1')->getPermissionsActionPanel();
|
2017-05-08 05:30:24 +02:00
|
|
|
|
2017-05-09 06:47:05 +02:00
|
|
|
$this->assertInstanceOf('CompositeField', $result);
|
|
|
|
$this->assertNotNull($result->getChildren()->fieldByName('CanViewType'));
|
|
|
|
$this->assertNotNull($result->getChildren()->fieldByName('ViewerGroups'));
|
2017-05-08 05:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test view permissions
|
|
|
|
*/
|
|
|
|
public function testCanView()
|
|
|
|
{
|
|
|
|
/** @var DMSDocument $document */
|
|
|
|
$document = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
|
2017-06-14 01:05:19 +02:00
|
|
|
$this->logoutMember();
|
2017-05-08 05:30:24 +02:00
|
|
|
// Logged out user test
|
|
|
|
$this->assertFalse($document->canView());
|
|
|
|
|
|
|
|
// Logged in user test
|
|
|
|
$adminID = $this->logInWithPermission();
|
|
|
|
$admin = Member::get()->byID($adminID);
|
|
|
|
$this->assertTrue($document->canView($admin));
|
|
|
|
/** @var Member $member */
|
|
|
|
$admin->logout();
|
|
|
|
|
|
|
|
// Check anyone
|
|
|
|
$document = $this->objFromFixture('DMSDocument', 'doc-anyone');
|
|
|
|
$this->assertTrue($document->canView());
|
|
|
|
|
|
|
|
// Check OnlyTheseUsers
|
|
|
|
$document = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
|
|
|
|
$reportAdminID = $this->logInWithPermission('cable-guy');
|
|
|
|
/** @var Member $reportAdmin */
|
|
|
|
$reportAdmin = Member::get()->byID($reportAdminID);
|
|
|
|
$this->assertFalse($document->canView($reportAdmin));
|
|
|
|
// Add reportAdmin to group
|
|
|
|
$reportAdmin->addToGroupByCode('content-author');
|
|
|
|
$this->assertTrue($document->canView($reportAdmin));
|
|
|
|
$reportAdmin->logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests edit permissions
|
|
|
|
*/
|
|
|
|
public function testCanEdit()
|
|
|
|
{
|
2017-06-14 01:05:19 +02:00
|
|
|
$this->logoutMember();
|
2017-05-08 05:30:24 +02:00
|
|
|
|
|
|
|
/** @var DMSDocument $document1 */
|
|
|
|
$document1 = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
|
|
|
|
|
|
|
|
// Logged out user test
|
|
|
|
$this->assertFalse($document1->canEdit());
|
|
|
|
|
|
|
|
//Logged in user test
|
|
|
|
$contentAuthor = $this->objFromFixture('Member', 'editor');
|
|
|
|
$this->assertTrue($document1->canEdit($contentAuthor));
|
|
|
|
|
|
|
|
// Check OnlyTheseUsers
|
|
|
|
/** @var DMSDocument $document2 */
|
|
|
|
$document2 = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
|
|
|
|
/** @var Member $cableGuy */
|
|
|
|
$cableGuy = $this->objFromFixture('Member', 'non-editor');
|
|
|
|
$this->assertFalse($document2->canEdit($cableGuy));
|
|
|
|
|
|
|
|
$cableGuy->addToGroupByCode('content-author');
|
|
|
|
$this->assertTrue($document2->canEdit($cableGuy));
|
|
|
|
}
|
|
|
|
|
2017-06-14 01:05:19 +02:00
|
|
|
/**
|
|
|
|
* Tests delete permissions
|
|
|
|
*/
|
|
|
|
public function testCanDelete()
|
|
|
|
{
|
|
|
|
$this->logoutMember();
|
|
|
|
$document1 = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
|
|
|
|
|
|
|
|
// Logged out user test
|
|
|
|
$this->assertFalse($document1->canDelete());
|
|
|
|
|
|
|
|
// Test editors can delete
|
|
|
|
$contentAuthor = $this->objFromFixture('Member', 'editor');
|
|
|
|
$this->assertTrue($document1->canDelete($contentAuthor));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests create permission
|
|
|
|
*/
|
|
|
|
public function testCanCreate()
|
|
|
|
{
|
|
|
|
$this->logoutMember();
|
|
|
|
$document1 = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
|
|
|
|
$this->logInWithPermission('CMS_ACCESS_DMSDocumentAdmin');
|
|
|
|
// Test CMS access can create
|
|
|
|
$this->assertTrue($document1->canCreate());
|
|
|
|
|
|
|
|
$this->logoutMember();
|
|
|
|
|
|
|
|
// Test editors can create
|
|
|
|
$contentAuthor = $this->objFromFixture('Member', 'editor');
|
|
|
|
$this->assertTrue($document1->canCreate($contentAuthor));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs out any active member
|
|
|
|
*/
|
|
|
|
protected function logoutMember()
|
|
|
|
{
|
|
|
|
if ($member = Member::currentUser()) {
|
|
|
|
$member->logOut();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 05:30:24 +02:00
|
|
|
/**
|
|
|
|
* Test permission denied reasons for documents
|
|
|
|
*/
|
|
|
|
public function testGetPermissionDeniedReason()
|
|
|
|
{
|
|
|
|
/** @var DMSDocument $document1 */
|
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
|
|
|
|
$this->assertContains('Please log in to view this document', $doc1->getPermissionDeniedReason());
|
|
|
|
|
|
|
|
/** @var DMSDocument $doc2 */
|
|
|
|
$doc2 = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
|
|
|
|
$this->assertContains('You are not authorised to view this document', $doc2->getPermissionDeniedReason());
|
|
|
|
|
|
|
|
/** @var DMSDocument $doc3 */
|
|
|
|
$doc3 = $this->objFromFixture('DMSDocument', 'doc-anyone');
|
|
|
|
$this->assertEquals('', $doc3->getPermissionDeniedReason());
|
|
|
|
}
|
2017-05-02 04:49:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that all pages that a document belongs to (via many document sets) can be retrieved in one list
|
|
|
|
*/
|
|
|
|
public function testGetRelatedPages()
|
|
|
|
{
|
|
|
|
$document = $this->objFromFixture('DMSDocument', 'd1');
|
|
|
|
$result = $document->getRelatedPages();
|
|
|
|
$this->assertCount(3, $result, 'Document 1 is related to 3 Pages');
|
|
|
|
$this->assertSame(array('s1', 's2', 's3'), $result->column('URLSegment'));
|
|
|
|
}
|
2017-05-15 07:17:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the title is returned if it is set, otherwise the filename without ID
|
|
|
|
*/
|
|
|
|
public function testGetTitleOrFilenameWithoutId()
|
|
|
|
{
|
|
|
|
$d1 = $this->objFromFixture('DMSDocument', 'd1');
|
|
|
|
$this->assertSame('test-file-file-doesnt-exist-1', $d1->getTitle());
|
|
|
|
|
|
|
|
$d2 = $this->objFromFixture('DMSDocument', 'd2');
|
|
|
|
$this->assertSame('File That Doesn\'t Exist (Title)', $d2->getTitle());
|
|
|
|
}
|
2017-05-17 06:24:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
2017-06-07 23:54:40 +02:00
|
|
|
$filename = $document->getStorageFolder() . '/' . $document->getFilename();
|
2017-05-17 06:24:25 +02:00
|
|
|
|
|
|
|
$this->assertTrue(file_exists($filename));
|
|
|
|
$document->delete();
|
|
|
|
$this->assertFalse($document->exists());
|
|
|
|
$this->assertFalse(file_exists($filename));
|
|
|
|
|
|
|
|
DMSFilesystemTestHelper::delete('assets/_unit-tests');
|
|
|
|
}
|
|
|
|
|
2017-05-24 01:43:23 +02:00
|
|
|
/**
|
|
|
|
* Test that the link contains an ID and URL slug
|
|
|
|
*/
|
|
|
|
public function testGetLink()
|
|
|
|
{
|
|
|
|
Config::inst()->update('DMS', 'folder_name', 'assets/_unit-tests');
|
|
|
|
|
|
|
|
$document = DMS::inst()->storeDocument('dms/tests/DMS-test-lorum-file.pdf');
|
|
|
|
|
|
|
|
$expected = '/dmsdocument/' . $document->ID . '-dms-test-lorum-file-pdf';
|
|
|
|
$this->assertSame($expected, $document->Link());
|
|
|
|
$this->assertSame($expected, $document->getLink());
|
|
|
|
}
|
|
|
|
|
2017-05-17 06:24:25 +02:00
|
|
|
/**
|
|
|
|
* 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());
|
|
|
|
}
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|