2021-03-31 04:26:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\UserForms\Tests\Model;
|
|
|
|
|
|
|
|
use SilverStripe\Assets\Dev\TestAssetStore;
|
|
|
|
use SilverStripe\Assets\File;
|
2022-08-11 05:35:39 +02:00
|
|
|
use SilverStripe\Assets\Storage\AssetStore;
|
2023-08-08 04:53:14 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2022-08-11 05:35:39 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2021-03-31 04:26:38 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\UserForms\Model\Submission\SubmittedFileField;
|
|
|
|
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
|
|
|
|
class SubmittedFileFieldTest extends SapphireTest
|
|
|
|
{
|
2022-08-11 05:35:39 +02:00
|
|
|
protected $file;
|
|
|
|
protected $submittedForm;
|
|
|
|
|
2021-11-01 21:52:58 +01:00
|
|
|
protected function setUp(): void
|
2021-03-31 04:26:38 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
TestAssetStore::activate('SubmittedFileFieldTest');
|
2022-08-11 05:35:39 +02:00
|
|
|
|
|
|
|
$this->file = File::create();
|
|
|
|
$this->file->setFromString('ABC', 'test-SubmittedFileFieldTest.txt');
|
|
|
|
$this->file->write();
|
|
|
|
|
|
|
|
$this->submittedForm = SubmittedForm::create();
|
|
|
|
$this->submittedForm->write();
|
|
|
|
|
|
|
|
$this->submittedFile = SubmittedFileField::create();
|
|
|
|
$this->submittedFile->UploadedFileID = $this->file->ID;
|
|
|
|
$this->submittedFile->Name = 'File';
|
|
|
|
$this->submittedFile->ParentID = $this->submittedForm->ID;
|
|
|
|
$this->submittedFile->write();
|
2021-03-31 04:26:38 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 21:52:58 +01:00
|
|
|
protected function tearDown(): void
|
2021-03-31 04:26:38 +02:00
|
|
|
{
|
|
|
|
TestAssetStore::reset();
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeletingSubmissionRemovesFile()
|
|
|
|
{
|
2022-08-11 05:35:39 +02:00
|
|
|
$this->assertStringContainsString('test-SubmittedFileFieldTest', $this->submittedFile->getFileName(), 'Submitted file is linked');
|
2021-03-31 04:26:38 +02:00
|
|
|
|
2022-08-11 05:35:39 +02:00
|
|
|
$this->submittedForm->delete();
|
|
|
|
$fileId = $this->file->ID;
|
2021-03-31 04:26:38 +02:00
|
|
|
|
|
|
|
$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');
|
|
|
|
}
|
2022-08-11 05:35:39 +02:00
|
|
|
|
|
|
|
public function testGetFormattedValue()
|
|
|
|
{
|
2023-08-08 04:53:14 +02:00
|
|
|
// Set an explicit base URL so we get a reliable value for the test
|
|
|
|
Director::config()->set('alternate_base_url', 'http://mysite.com');
|
2022-08-11 05:35:39 +02:00
|
|
|
$fileName = $this->submittedFile->getFileName();
|
2023-09-06 00:54:20 +02:00
|
|
|
$link = 'http://mysite.com/assets/3c01bdbb26/test-SubmittedFileFieldTest.txt';
|
2022-08-11 05:35:39 +02:00
|
|
|
|
|
|
|
$this->file->CanViewType = 'OnlyTheseUsers';
|
|
|
|
$this->file->write();
|
2023-08-08 04:53:14 +02:00
|
|
|
|
2023-09-06 00:54:20 +02:00
|
|
|
// Userforms submission filled in by non-logged in user being emailed to recipient
|
|
|
|
$this->logOut();
|
2022-08-11 05:35:39 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2023-09-06 00:54:20 +02:00
|
|
|
'%s - <a href="%s" target="_blank">%s</a> - <em>%s</em>',
|
|
|
|
$fileName,
|
|
|
|
$link,
|
|
|
|
'Download File',
|
|
|
|
'You must be logged in to view this file'
|
2022-08-11 05:35:39 +02:00
|
|
|
),
|
|
|
|
$this->submittedFile->getFormattedValue()->value
|
|
|
|
);
|
2023-08-08 04:53:14 +02:00
|
|
|
|
2023-09-06 00:54:20 +02:00
|
|
|
// Logged in CMS user without permissions to view file in the CMS
|
|
|
|
$this->logInWithPermission('CMS_ACCESS_CMSMain');
|
2022-08-11 05:35:39 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
|
|
|
'<i class="icon font-icon-lock"></i> %s - <em>%s</em>',
|
|
|
|
$fileName,
|
2023-09-06 00:54:20 +02:00
|
|
|
'You don't have the right permissions to download this file'
|
2022-08-11 05:35:39 +02:00
|
|
|
),
|
|
|
|
$this->submittedFile->getFormattedValue()->value
|
|
|
|
);
|
|
|
|
|
2023-09-06 00:54:20 +02:00
|
|
|
// Logged in CMS user with permissions to view file in the CMS
|
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
|
|
|
'%s - <a href="%s" target="_blank">%s</a>',
|
|
|
|
$fileName,
|
|
|
|
$link,
|
|
|
|
'Download File'
|
|
|
|
),
|
|
|
|
$this->submittedFile->getFormattedValue()->value
|
2022-08-11 05:35:39 +02:00
|
|
|
);
|
|
|
|
}
|
2021-03-31 04:26:38 +02:00
|
|
|
}
|