mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
FIX When deleting form submission, delete linked file
This commit is contained in:
parent
e925aa1979
commit
beaf901208
@ -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);
|
||||
|
64
tests/php/Model/SubmittedFileFieldTest.php
Normal file
64
tests/php/Model/SubmittedFileFieldTest.php
Normal 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');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user