2010-12-05 09:35:06 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\FileField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
|
2010-12-05 09:35:06 +01:00
|
|
|
class FileFieldTest extends FunctionalTest {
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
|
|
|
* Test a valid upload of a required file in a form. Error is set to 0, as the upload went well
|
2010-12-05 09:35:06 +01:00
|
|
|
*/
|
|
|
|
public function testUploadRequiredFile() {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2010-12-05 09:35:06 +01:00
|
|
|
$form = new Form(
|
|
|
|
new Controller(),
|
|
|
|
'Form',
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(
|
2010-12-05 09:35:06 +01:00
|
|
|
$fileField = new FileField('cv', 'Upload your CV')
|
|
|
|
),
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList()
|
2010-12-05 09:35:06 +01:00
|
|
|
);
|
|
|
|
$fileFieldValue = array(
|
2012-12-08 12:20:20 +01:00
|
|
|
'name' => 'aCV.txt',
|
|
|
|
'type' => 'application/octet-stream',
|
|
|
|
'tmp_name' => '/private/var/tmp/phpzTQbqP',
|
|
|
|
'error' => 0,
|
|
|
|
'size' => 3471
|
2010-12-05 09:35:06 +01:00
|
|
|
);
|
|
|
|
$fileField->setValue($fileFieldValue);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:35:06 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$form->validate()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test different scenarii for a failed upload : an error occured, no files where provided
|
|
|
|
*/
|
|
|
|
public function testUploadMissingRequiredFile() {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2010-12-05 09:35:06 +01:00
|
|
|
$form = new Form(
|
|
|
|
new Controller(),
|
|
|
|
'Form',
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(
|
2010-12-05 09:35:06 +01:00
|
|
|
$fileField = new FileField('cv', 'Upload your CV')
|
|
|
|
),
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(),
|
2010-12-05 09:35:06 +01:00
|
|
|
new RequiredFields('cv')
|
|
|
|
);
|
|
|
|
// All fields are filled but for some reason an error occured when uploading the file => fails
|
|
|
|
$fileFieldValue = array(
|
2012-12-08 12:20:20 +01:00
|
|
|
'name' => 'aCV.txt',
|
|
|
|
'type' => 'application/octet-stream',
|
|
|
|
'tmp_name' => '/private/var/tmp/phpzTQbqP',
|
|
|
|
'error' => 1,
|
|
|
|
'size' => 3471
|
2010-12-05 09:35:06 +01:00
|
|
|
);
|
|
|
|
$fileField->setValue($fileFieldValue);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:35:06 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$form->validate(),
|
|
|
|
'An error occured when uploading a file, but the validator returned true'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:35:06 +01:00
|
|
|
// We pass an empty set of parameters for the uploaded file => fails
|
|
|
|
$fileFieldValue = array();
|
|
|
|
$fileField->setValue($fileFieldValue);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:35:06 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$form->validate(),
|
|
|
|
'An empty array was passed as parameter for an uploaded file, but the validator returned true'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:35:06 +01:00
|
|
|
// We pass an null value for the uploaded file => fails
|
|
|
|
$fileFieldValue = null;
|
|
|
|
$fileField->setValue($fileFieldValue);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:35:06 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$form->validate(),
|
|
|
|
'A null value was passed as parameter for an uploaded file, but the validator returned true'
|
|
|
|
);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|