Merge pull request #1051 from wilr/wilr-patch-1

FIX Uploaded files not appearing in emails and cleanup deleted files.
This commit is contained in:
Daniel Hensby 2021-05-17 09:10:31 +01:00 committed by GitHub
commit 41fa8f0718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 7 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(
@ -62,14 +71,30 @@ class SubmittedFileField extends SubmittedFormField
*/
public function getLink()
{
$file = $this->UploadedFile();
if ($file && $file->exists()) {
if (trim($file->getFilename(), '/') != trim(ASSETS_DIR, '/')) {
return $this->UploadedFile()->AbsoluteLink();
if ($file = $this->getUploadedFileFromDraft()) {
if ($file->exists()) {
return $file->getAbsoluteURL();
}
}
}
/**
* 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()
{
$fileId = $this->UploadedFileID;
return Versioned::withVersionedMode(function () use ($fileId) {
Versioned::set_stage(Versioned::DRAFT);
return File::get()->byID($fileId);
});
}
/**
* Return the name of the file, if present
*
@ -77,8 +102,8 @@ class SubmittedFileField extends SubmittedFormField
*/
public function getFileName()
{
if ($this->UploadedFile()) {
return $this->UploadedFile()->Name;
if ($file = $this->getUploadedFileFromDraft()) {
return $file->Name;
}
}
}

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');
}
}