From 3d19b9fc7595fb7619678eb5f1839b076382ff2f Mon Sep 17 00:00:00 2001 From: Franco Springveldt Date: Fri, 2 Jun 2017 15:50:07 +1200 Subject: [PATCH] FIX title validation on DMSDocumentSet --- code/model/DMSDocumentSet.php | 13 ++++++++++++- tests/DMSDocumentSetTest.php | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/code/model/DMSDocumentSet.php b/code/model/DMSDocumentSet.php index 005524f..12f3664 100644 --- a/code/model/DMSDocumentSet.php +++ b/code/model/DMSDocumentSet.php @@ -122,7 +122,8 @@ class DMSDocumentSet extends DataObject ->setFieldCasting(array('LastEdited' => 'Datetime->Ago')) ->setFieldFormatting( array( - 'FilenameWithoutID' => '$FilenameWithoutID', + 'FilenameWithoutID' => '$FilenameWithoutID', 'ManuallyAdded' => function ($value) { if ($value) { return _t('DMSDocumentSet.MANUAL', 'Manually'); @@ -311,4 +312,14 @@ class DMSDocumentSet extends DataObject array('ManuallyAdded' => _t('DMSDocumentSet.ADDEDMETHOD', 'Added')) ); } + + protected function validate() + { + $result = parent::validate(); + + if (!$this->getTitle()) { + $result->error(_t('DMSDocumentSet.VALIDATION_NO_TITLE', '\'Title\' is required.')); + } + return $result; + } } diff --git a/tests/DMSDocumentSetTest.php b/tests/DMSDocumentSetTest.php index 4204db8..cf46e32 100644 --- a/tests/DMSDocumentSetTest.php +++ b/tests/DMSDocumentSetTest.php @@ -163,7 +163,7 @@ class DMSDocumentSetTest extends SapphireTest { Config::inst()->update('DMS', 'shortcode_handler_key', 'unit-test'); - $set = DMSDocumentSet::create(); + $set = DMSDocumentSet::create(array('Title' => 'TestSet')); $set->write(); $fields = $set->getCMSFields(); @@ -187,4 +187,18 @@ class DMSDocumentSetTest extends SapphireTest $set->saveLinkedDocuments(); $this->assertEquals(2, $set->getDocuments()->count(), 'Set has 2 documents'); } + + /** + * Tests that an exception is thrown if no title entered for a DMSDocumentSet. + * @expectedException ValidationException + */ + public function testExceptionOnNoTitleGiven() + { + $set = DMSDocumentSet::create(array('Title' => '')); + try { + $set->write(); + } catch (ValidationException $e) { + throw $e; + } + } }