diff --git a/forms/UploadField.php b/forms/UploadField.php index e10268206..9133c12ff 100644 --- a/forms/UploadField.php +++ b/forms/UploadField.php @@ -501,9 +501,8 @@ class UploadField extends FileField { $record = $this->getRecord(); $fieldName = $this->getName(); return ( - $record->has_one($fieldName) - || $record->has_many($fieldName) - || $record->many_many($fieldName) + $record + && ($record->has_one($fieldName) || $record->has_many($fieldName) || $record->many_many($fieldName)) ); } diff --git a/tests/forms/uploadfield/UploadFieldTest.php b/tests/forms/uploadfield/UploadFieldTest.php index 62bc564ce..51555af83 100644 --- a/tests/forms/uploadfield/UploadFieldTest.php +++ b/tests/forms/uploadfield/UploadFieldTest.php @@ -375,6 +375,29 @@ } + function testManagesRelation() { + $record = $this->objFromFixture('UploadFieldTest_Record', 'record1'); + + $field = new UploadField('ManyManyFiles'); + $this->assertFalse($field->managesRelation(), 'False if no record is set'); + + $field = new UploadField('NoRelationField'); + $field->setRecord($record); + $this->assertFalse($field->managesRelation(), 'False if no relation found by name'); + + $field = new UploadField('HasOneFile'); + $field->setRecord($record); + $this->assertTrue($field->managesRelation(), 'True for has_one'); + + $field = new UploadField('HasManyFiles'); + $field->setRecord($record); + $this->assertTrue($field->managesRelation(), 'True for has_many'); + + $field = new UploadField('ManyManyFiles'); + $field->setRecord($record); + $this->assertTrue($field->managesRelation(), 'True for many_many'); + } + protected function getMockForm() { return new Form(new Controller(), 'Form', new FieldList(), new FieldList()); }