mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
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:
commit
41fa8f0718
@ -4,6 +4,7 @@ namespace SilverStripe\UserForms\Model\Submission;
|
|||||||
|
|
||||||
use SilverStripe\Assets\File;
|
use SilverStripe\Assets\File;
|
||||||
use SilverStripe\ORM\FieldType\DBField;
|
use SilverStripe\ORM\FieldType\DBField;
|
||||||
|
use SilverStripe\Versioned\Versioned;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A file uploaded on a {@link UserDefinedForm} and attached to a single
|
* 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 $table_name = 'SubmittedFileField';
|
||||||
|
|
||||||
|
private static $owns = [
|
||||||
|
'UploadedFile'
|
||||||
|
];
|
||||||
|
|
||||||
|
private static $cascade_deletes = [
|
||||||
|
'UploadedFile'
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the value of this field for inclusion into things such as
|
* Return the value of this field for inclusion into things such as
|
||||||
* reports.
|
* reports.
|
||||||
@ -62,14 +71,30 @@ class SubmittedFileField extends SubmittedFormField
|
|||||||
*/
|
*/
|
||||||
public function getLink()
|
public function getLink()
|
||||||
{
|
{
|
||||||
$file = $this->UploadedFile();
|
if ($file = $this->getUploadedFileFromDraft()) {
|
||||||
if ($file && $file->exists()) {
|
if ($file->exists()) {
|
||||||
if (trim($file->getFilename(), '/') != trim(ASSETS_DIR, '/')) {
|
return $file->getAbsoluteURL();
|
||||||
return $this->UploadedFile()->AbsoluteLink();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* Return the name of the file, if present
|
||||||
*
|
*
|
||||||
@ -77,8 +102,8 @@ class SubmittedFileField extends SubmittedFormField
|
|||||||
*/
|
*/
|
||||||
public function getFileName()
|
public function getFileName()
|
||||||
{
|
{
|
||||||
if ($this->UploadedFile()) {
|
if ($file = $this->getUploadedFileFromDraft()) {
|
||||||
return $this->UploadedFile()->Name;
|
return $file->Name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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