FIX When deleting form submission, delete linked file

This commit is contained in:
Will Rossiter 2021-03-31 15:26:38 +13:00
parent e925aa1979
commit beaf901208
No known key found for this signature in database
GPG Key ID: 7FD2A809B22259EF
2 changed files with 77 additions and 4 deletions

View File

@ -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(
@ -75,11 +84,11 @@ class SubmittedFileField extends SubmittedFormField
*
* @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);

View File

@ -0,0 +1,64 @@
<?php
namespace SilverStripe\UserForms\Tests\Model;
use SilverStripe\Assets\Dev\TestAssetStore;
use SilverStripe\Assets\File;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\UserForms\Model\Submission\SubmittedFileField;
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
use SilverStripe\Versioned\Versioned;
class SubmittedFileFieldTest extends SapphireTest
{
public function setUp()
{
parent::setUp();
TestAssetStore::activate('SubmittedFileFieldTest');
}
public function tearDown()
{
TestAssetStore::reset();
parent::tearDown();
}
public function testDeletingSubmissionRemovesFile()
{
$file = File::create();
$file->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');
}
}