/
FileFieldTest.php
108 lines (95 loc) · 3.28 KB
/
FileFieldTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace SilverStripe\Forms\Tests;
use ReflectionMethod;
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(
Controller::curr(),
'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());
}
/**
* @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);
}
}
/**
* Test different scenarii for a failed upload : an error occured, no files where provided
* @skipUpgrade
*/
public function testUploadMissingRequiredFile()
{
$form = new Form(
Controller::curr(),
'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'
);
}
}