silverstripe-framework/tests/php/Forms/FileFieldTest.php
Damian Mooyman 0c41a97a8b API Refactor Form request handling into FormRequestHandler
API Add HasRequestHandler interface
API Refactor Link() and url handling behaviour from Controller into RequestHandler
API RequestHandler classes now must define url_segment to have a default Link()
API Clean up redirectBack()
2017-03-10 15:04:33 +13:00

91 lines
2.7 KiB
PHP

<?php
namespace SilverStripe\Forms\Tests;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\FileField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields;
class FileFieldTest extends FunctionalTest
{
/**
* Test a valid upload of a required file in a form. Error is set to 0, as the upload went well
*
* @skipUpgrade
*/
public function testUploadRequiredFile()
{
$form = new Form(
new Controller(),
'Form',
new FieldList(
$fileField = new FileField('cv', 'Upload your CV')
),
new FieldList()
);
$fileFieldValue = array(
'name' => 'aCV.txt',
'type' => 'application/octet-stream',
'tmp_name' => '/private/var/tmp/phpzTQbqP',
'error' => 0,
'size' => 3471
);
$fileField->setValue($fileFieldValue);
$this->assertTrue($form->validationResult()->isValid());
}
/**
* Test different scenarii for a failed upload : an error occured, no files where provided
* @skipUpgrade
*/
public function testUploadMissingRequiredFile()
{
$form = new Form(
new Controller(),
'Form',
new FieldList(
$fileField = new FileField('cv', 'Upload your CV')
),
new FieldList(),
new RequiredFields('cv')
);
// All fields are filled but for some reason an error occured when uploading the file => fails
$fileFieldValue = array(
'name' => 'aCV.txt',
'type' => 'application/octet-stream',
'tmp_name' => '/private/var/tmp/phpzTQbqP',
'error' => 1,
'size' => 3471
);
$fileField->setValue($fileFieldValue);
$this->assertFalse(
$form->validationResult()->isValid(),
'An error occured when uploading a file, but the validator returned true'
);
// We pass an empty set of parameters for the uploaded file => fails
$fileFieldValue = array();
$fileField->setValue($fileFieldValue);
$this->assertFalse(
$form->validationResult()->isValid(),
'An empty array was passed as parameter for an uploaded file, but the validator returned true'
);
// We pass an null value for the uploaded file => fails
$fileFieldValue = null;
$fileField->setValue($fileFieldValue);
$this->assertFalse(
$form->validationResult()->isValid(),
'A null value was passed as parameter for an uploaded file, but the validator returned true'
);
}
}