Merge pull request #115 from robbieaverill/bugfix/documents-relate-to-themselves

FIX Do not allow documents to be related to themselves
This commit is contained in:
sachajudd 2017-05-09 12:08:53 +12:00 committed by GitHub
commit 9c5693dab0
2 changed files with 62 additions and 16 deletions

View File

@ -1436,13 +1436,31 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
$gridField->getConfig()->removeComponentsByType('GridFieldAddNewButton');
// Move the autocompleter to the left
$gridField->getConfig()->removeComponentsByType('GridFieldAddExistingAutocompleter');
$gridField->getConfig()->addComponent(new GridFieldAddExistingAutocompleter('buttons-before-left'));
$gridField->getConfig()->addComponent(
$addExisting = new GridFieldAddExistingAutocompleter('buttons-before-left')
);
// Ensure that current document doesn't get returned in the autocompleter
$addExisting->setSearchList($this->getRelatedDocumentsForAutocompleter());
$this->extend('updateRelatedDocumentsGridField', $gridField);
return $gridField;
}
/**
* Get the list of documents to show in "related documents". This can be modified via the extension point, for
* example if you wanted to exclude embargoed documents or something similar.
*
* @return DataList
*/
protected function getRelatedDocumentsForAutocompleter()
{
$documents = DMSDocument::get()->exclude('ID', $this->ID);
$this->extend('updateRelatedDocumentsForAutocompleter', $documents);
return $documents;
}
/**
* Checks at least one group is selected if CanViewType || CanEditType == 'OnlyTheseUsers'
*

View File

@ -209,21 +209,7 @@ class DMSDocumentTest extends SapphireTest
public function testDocumentHasCmsFieldForManagingRelatedDocuments()
{
$document = $this->objFromFixture('DMSDocument', 'document_with_relations');
$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;
}
}
$this->assertInstanceOf('GridField', $gridField);
/** @var GridFieldConfig $gridFieldConfig */
$gridField = $this->getGridFieldFromDocument($document);
$gridFieldConfig = $gridField->getConfig();
$this->assertNotNull(
@ -238,6 +224,48 @@ class DMSDocumentTest extends SapphireTest
);
}
/**
* Ensure that the related documents list does not include the current document itself
*/
public function testGetRelatedDocumentsForAutocompleter()
{
$document = $this->objFromFixture('DMSDocument', 'd1');
$gridField = $this->getGridFieldFromDocument($document);
$config = $gridField->getConfig();
$autocompleter = $config->getComponentByType('GridFieldAddExistingAutocompleter');
$autocompleter->setResultsFormat('$Filename');
$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);
}
/**
* @return GridField
*/
protected function getGridFieldFromDocument(DMSDocument $document)
{
$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;
}
}
$this->assertInstanceOf('GridField', $gridField);
return $gridField;
}
/*
* Tests whether the permissions fields are added
*/