From beaf90120865b9f5da7d3e46631eb881b96f1a75 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Wed, 31 Mar 2021 15:26:38 +1300 Subject: [PATCH] FIX When deleting form submission, delete linked file --- code/Model/Submission/SubmittedFileField.php | 17 ++++-- tests/php/Model/SubmittedFileFieldTest.php | 64 ++++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 tests/php/Model/SubmittedFileFieldTest.php diff --git a/code/Model/Submission/SubmittedFileField.php b/code/Model/Submission/SubmittedFileField.php index 78fcb3d..1d8e078 100755 --- a/code/Model/Submission/SubmittedFileField.php +++ b/code/Model/Submission/SubmittedFileField.php @@ -4,6 +4,7 @@ namespace SilverStripe\UserForms\Model\Submission; use SilverStripe\Assets\File; use SilverStripe\ORM\FieldType\DBField; +use SilverStripe\Versioned\Versioned; /** * A file uploaded on a {@link UserDefinedForm} and attached to a single @@ -21,6 +22,14 @@ class SubmittedFileField extends SubmittedFormField private static $table_name = 'SubmittedFileField'; + private static $owns = [ + 'UploadedFile' + ]; + + private static $cascade_deletes = [ + 'UploadedFile' + ]; + /** * Return the value of this field for inclusion into things such as * reports. @@ -31,7 +40,7 @@ class SubmittedFileField extends SubmittedFormField { $name = $this->getFileName(); $link = $this->getLink(); - $title = _t(__CLASS__.'.DOWNLOADFILE', 'Download File'); + $title = _t(__CLASS__ . '.DOWNLOADFILE', 'Download File'); if ($link) { return DBField::create_field('HTMLText', sprintf( @@ -72,14 +81,14 @@ class SubmittedFileField extends SubmittedFormField /** * As uploaded files are stored in draft by default, this retrieves the * uploaded file from draft mode rather than using the current stage. - * + * * @return File */ - public function getUploadedFileFromDraft(): ?File + public function getUploadedFileFromDraft() { $fileId = $this->UploadedFileID; - return Versioned::withVersionedMode(function() use ($fileId) { + return Versioned::withVersionedMode(function () use ($fileId) { Versioned::set_stage(Versioned::DRAFT); return File::get()->byID($fileId); diff --git a/tests/php/Model/SubmittedFileFieldTest.php b/tests/php/Model/SubmittedFileFieldTest.php new file mode 100644 index 0000000..91db2f9 --- /dev/null +++ b/tests/php/Model/SubmittedFileFieldTest.php @@ -0,0 +1,64 @@ +setFromString('ABC', 'test-SubmittedFileFieldTest.txt'); + $file->write(); + + $submittedForm = SubmittedForm::create(); + $submittedForm->write(); + + $submittedFile = SubmittedFileField::create(); + $submittedFile->UploadedFileID = $file->ID; + $submittedFile->Name = 'File'; + $submittedFile->ParentID = $submittedForm->ID; + $submittedFile->write(); + + $this->assertContains('test-SubmittedFileFieldTest', $submittedFile->getFileName(), 'Submitted file is linked'); + + $submittedForm->delete(); + $fileId = $file->ID; + + $draftVersion = Versioned::withVersionedMode(function () use ($fileId) { + Versioned::set_stage(Versioned::DRAFT); + + return File::get()->byID($fileId); + }); + + $this->assertNull($draftVersion, 'Draft file has been deleted'); + + $liveVersion = Versioned::withVersionedMode(function () use ($fileId) { + Versioned::set_stage(Versioned::LIVE); + + return File::get()->byID($fileId); + }); + + $this->assertNull($liveVersion, 'Live file has been deleted'); + } +}