From a0d583291cce28a472f9f0dcd98d03fc085e3944 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 8 Feb 2012 11:27:10 +0100 Subject: [PATCH] MINOR Fixed UploadField->managesRelation() when no record is set --- forms/UploadField.php | 5 ++--- tests/forms/uploadfield/UploadFieldTest.php | 23 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) 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()); }