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;
|
|
|
|
|
2017-10-03 17:48:49 +02:00
|
|
|
use ReflectionMethod;
|
2021-02-18 22:28:03 +01:00
|
|
|
use SilverStripe\Assets\Upload_Validator;
|
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;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
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
|
2017-03-02 03:24:38 +01:00
|
|
|
*
|
|
|
|
* @skipUpgrade
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testUploadRequiredFile()
|
|
|
|
{
|
|
|
|
$form = new Form(
|
2017-06-22 12:50:45 +02:00
|
|
|
Controller::curr(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Form',
|
|
|
|
new FieldList(
|
|
|
|
$fileField = new FileField('cv', 'Upload your CV')
|
|
|
|
),
|
|
|
|
new FieldList()
|
|
|
|
);
|
2020-04-20 19:58:09 +02:00
|
|
|
$fileFieldValue = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'name' => 'aCV.txt',
|
|
|
|
'type' => 'application/octet-stream',
|
|
|
|
'tmp_name' => '/private/var/tmp/phpzTQbqP',
|
|
|
|
'error' => 0,
|
|
|
|
'size' => 3471
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$fileField->setValue($fileFieldValue);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($form->validationResult()->isValid());
|
|
|
|
}
|
2010-12-05 09:35:06 +01:00
|
|
|
|
2021-02-18 22:28:03 +01:00
|
|
|
/**
|
|
|
|
* Test that FileField::validate() is run on FileFields with both single and multi-file syntax
|
|
|
|
* By default FileField::validate() will return true early if the $_FILES super-global does not contain the
|
|
|
|
* corresponding FileField::name. This early return means the files was not fully run through FileField::validate()
|
|
|
|
* So for this test we create an invalid file upload on purpose and test that false was returned which means that
|
|
|
|
* the file was run through FileField::validate() function
|
|
|
|
*/
|
|
|
|
public function testMultiFileSyntaxUploadIsValidated()
|
|
|
|
{
|
|
|
|
$names = [
|
|
|
|
'single_file_syntax',
|
|
|
|
'multi_file_syntax_a[]',
|
|
|
|
'multi_file_syntax_b[0]',
|
|
|
|
'multi_file_syntax_c[key]'
|
|
|
|
];
|
|
|
|
foreach ($names as $name) {
|
|
|
|
$form = new Form(
|
|
|
|
Controller::curr(),
|
|
|
|
'Form',
|
|
|
|
new FieldList($fileField = new FileField($name, 'My desc')),
|
|
|
|
new FieldList()
|
|
|
|
);
|
|
|
|
$fileData = $this->createInvalidUploadedFileData($name, "FileFieldTest.txt");
|
|
|
|
// FileFields with multi_file_syntax[] files will appear in the $_FILES super-global
|
|
|
|
// with the [] brackets trimmed e.g. $_FILES[multi_file_syntax]
|
|
|
|
$_FILES = [preg_replace('#\[(.*?)\]#', '', $name) => $fileData];
|
|
|
|
$fileField->setValue($fileData);
|
|
|
|
$validator = $form->getValidator();
|
|
|
|
$isValid = $fileField->validate($validator);
|
|
|
|
$this->assertFalse($isValid, "$name was run through the validate() function");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createInvalidUploadedFileData($name, $tmpFileName): array
|
|
|
|
{
|
|
|
|
$tmpFilePath = TEMP_PATH . DIRECTORY_SEPARATOR . $tmpFileName;
|
|
|
|
|
|
|
|
// multi_file_syntax
|
2022-04-14 03:12:59 +02:00
|
|
|
if (strpos($name ?? '', '[') !== false) {
|
2021-02-18 22:28:03 +01:00
|
|
|
$key = 0;
|
2022-04-14 03:12:59 +02:00
|
|
|
if (preg_match('#\[(.+?)\]#', $name ?? '', $m)) {
|
2021-02-18 22:28:03 +01:00
|
|
|
$key = $m[1];
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
'name' => [$key => $tmpFileName],
|
|
|
|
'type' => [$key => 'text/plaintext'],
|
|
|
|
'size' => [$key => 0],
|
|
|
|
'tmp_name' => [$key => $tmpFilePath],
|
|
|
|
'error' => [$key => UPLOAD_ERR_NO_FILE],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
// single_file_syntax
|
|
|
|
return [
|
|
|
|
'name' => $tmpFileName,
|
|
|
|
'type' => 'text/plaintext',
|
|
|
|
'size' => 0,
|
|
|
|
'tmp_name' => $tmpFilePath,
|
|
|
|
'error' => UPLOAD_ERR_NO_FILE,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-10-03 17:48:49 +02:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
public function testGetAcceptFileTypes()
|
|
|
|
{
|
|
|
|
$field = new FileField('image', 'Image');
|
|
|
|
$field->setAllowedExtensions('jpg', 'png');
|
|
|
|
|
|
|
|
$method = new ReflectionMethod($field, 'getAcceptFileTypes');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
$allowed = $method->invoke($field);
|
|
|
|
|
|
|
|
$expected = ['.jpg', '.png', 'image/jpeg', 'image/png'];
|
|
|
|
foreach ($expected as $extensionOrMime) {
|
|
|
|
$this->assertContains($extensionOrMime, $allowed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
2021-12-13 09:05:33 +01:00
|
|
|
* Test different scenarii for a failed upload : an error occurred, no files where provided
|
2017-03-02 03:24:38 +01:00
|
|
|
* @skipUpgrade
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testUploadMissingRequiredFile()
|
|
|
|
{
|
|
|
|
$form = new Form(
|
2017-06-22 12:50:45 +02:00
|
|
|
Controller::curr(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Form',
|
|
|
|
new FieldList(
|
|
|
|
$fileField = new FileField('cv', 'Upload your CV')
|
|
|
|
),
|
|
|
|
new FieldList(),
|
|
|
|
new RequiredFields('cv')
|
|
|
|
);
|
2021-12-13 09:05:33 +01:00
|
|
|
// All fields are filled but for some reason an error occurred when uploading the file => fails
|
2020-04-20 19:58:09 +02:00
|
|
|
$fileFieldValue = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'name' => 'aCV.txt',
|
|
|
|
'type' => 'application/octet-stream',
|
|
|
|
'tmp_name' => '/private/var/tmp/phpzTQbqP',
|
|
|
|
'error' => 1,
|
|
|
|
'size' => 3471
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$fileField->setValue($fileFieldValue);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$form->validationResult()->isValid(),
|
2021-12-13 09:05:33 +01:00
|
|
|
'An error occurred when uploading a file, but the validator returned true'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// We pass an empty set of parameters for the uploaded file => fails
|
2020-04-20 19:58:09 +02:00
|
|
|
$fileFieldValue = [];
|
2016-12-16 05:34:21 +01:00
|
|
|
$fileField->setValue($fileFieldValue);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$form->validationResult()->isValid(),
|
|
|
|
'An empty array was passed as parameter for an uploaded file, but the validator returned true'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +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
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$form->validationResult()->isValid(),
|
|
|
|
'A null value was passed as parameter for an uploaded file, but the validator returned true'
|
|
|
|
);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|