From 2329ba4f40d3a2b498b950306d601a47a798f4d0 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 9 May 2017 10:59:33 +1200 Subject: [PATCH] FIX Do not allow documents to be related to themselves --- code/model/DMSDocument.php | 20 ++++++++++++- tests/DMSDocumentTest.php | 58 ++++++++++++++++++++++++++++---------- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/code/model/DMSDocument.php b/code/model/DMSDocument.php index 6a565e9..e7422f6 100644 --- a/code/model/DMSDocument.php +++ b/code/model/DMSDocument.php @@ -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' * diff --git a/tests/DMSDocumentTest.php b/tests/DMSDocumentTest.php index 395a281..e98504d 100644 --- a/tests/DMSDocumentTest.php +++ b/tests/DMSDocumentTest.php @@ -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 */