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
|
|
|
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\ORM\ValidationException;
|
|
|
|
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
|
|
|
|
*/
|
2017-08-11 02:20:12 +02:00
|
|
|
protected function setUp()
|
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');
|
|
|
|
$this->setExpectedException(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());
|
|
|
|
}
|
2017-08-11 02:20:12 +02:00
|
|
|
}
|