2016-08-12 00:51:47 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-15 23:31:47 +02:00
|
|
|
namespace SilverStripe\UserForms\Tests\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2020-06-10 06:59:01 +02:00
|
|
|
use SilverStripe\Assets\Folder;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2024-09-23 04:40:23 +02:00
|
|
|
use SilverStripe\Core\Validation\ValidationException;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2016-08-12 00:51:47 +02:00
|
|
|
/**
|
|
|
|
* @package userforms
|
|
|
|
*/
|
|
|
|
class EditableFileFieldTest extends SapphireTest
|
|
|
|
{
|
2017-08-11 02:20:12 +02:00
|
|
|
protected static $fixture_file = '../EditableFormFieldTest.yml';
|
2016-08-12 00:51:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
private $php_max_file_size;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hold the server default max file size upload limit for later
|
|
|
|
*/
|
2021-11-01 21:52:58 +01:00
|
|
|
protected function setUp(): void
|
2016-08-12 00:51:47 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
$editableFileField = singleton(EditableFileField::class);
|
2016-08-12 00:51:47 +02:00
|
|
|
$this->php_max_file_size = $editableFileField::get_php_max_file_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the field validator has the server default as the max file size upload
|
|
|
|
*/
|
|
|
|
public function testDefaultMaxFileSize()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
|
2016-08-12 00:51:47 +02:00
|
|
|
$formField = $fileField->getFormField();
|
|
|
|
|
|
|
|
$this->assertEquals($this->php_max_file_size, $formField->getValidator()->getAllowedMaxFileSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that validation prevents the provided upload size limit to be less than or equal to the max php size
|
|
|
|
*/
|
|
|
|
public function testValidateFileSizeFieldValue()
|
|
|
|
{
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
$fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
|
2021-11-01 21:52:58 +01:00
|
|
|
$this->expectException(ValidationException::class);
|
2016-08-12 00:51:47 +02:00
|
|
|
$fileField->MaxFileSizeMB = $this->php_max_file_size * 2;
|
|
|
|
$fileField->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the field validator has the updated allowed max file size
|
|
|
|
*/
|
|
|
|
public function testUpdatedMaxFileSize()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
|
2016-08-12 00:51:47 +02:00
|
|
|
$fileField->MaxFileSizeMB = .25;
|
|
|
|
$fileField->write();
|
|
|
|
|
|
|
|
$formField = $fileField->getFormField();
|
2017-05-24 08:31:25 +02:00
|
|
|
$this->assertEquals($formField->getValidator()->getAllowedMaxFileSize(), 262144);
|
2016-08-12 00:51:47 +02:00
|
|
|
}
|
2018-03-23 04:18:32 +01:00
|
|
|
|
|
|
|
public function testAllowEmptyTitle()
|
|
|
|
{
|
|
|
|
/** @var EditableFileField $field */
|
|
|
|
$field = EditableFileField::create();
|
|
|
|
$field->Name = 'EditableFormField_123456';
|
|
|
|
$this->assertEmpty($field->getFormField()->Title());
|
|
|
|
}
|
2020-06-10 06:59:01 +02:00
|
|
|
|
|
|
|
public function testOnBeforeWrite()
|
|
|
|
{
|
|
|
|
$this->logOut();
|
|
|
|
|
|
|
|
/** @var EditableFileField $fileField */
|
|
|
|
$fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
|
|
|
|
|
|
|
|
$defaultFolder = Folder::find('Form-submissions');
|
|
|
|
$this->assertNotEmpty($defaultFolder, 'Default Folder was created along with the EditableFileField');
|
|
|
|
$this->assertFalse($defaultFolder->canView(), 'Default Folder default to being restricted');
|
|
|
|
$this->assertFalse((boolean)$fileField->FolderConfirmed, 'EditableFileField are not Folder Confirmed initially');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$defaultFolder->ID,
|
|
|
|
$fileField->FolderID,
|
|
|
|
'EditableFileField default to default form submission folder'
|
|
|
|
);
|
|
|
|
|
|
|
|
$fileField->FolderID = Folder::find_or_make('boom')->ID;
|
|
|
|
$fileField->write();
|
|
|
|
$this->assertTrue(
|
|
|
|
(boolean)$fileField->FolderConfirmed,
|
|
|
|
'EditableFileField are Folder Confirmed once you assigned them a folder'
|
|
|
|
);
|
|
|
|
|
|
|
|
$secondField = EditableFileField::create();
|
|
|
|
$secondField->ParentID = $fileField->ParentID;
|
|
|
|
$secondField->write();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$fileField->FolderID,
|
|
|
|
$secondField->FolderID,
|
|
|
|
'Second EditableFileField defaults to first field FolderID'
|
|
|
|
);
|
|
|
|
}
|
2017-08-11 02:20:12 +02:00
|
|
|
}
|