Completed
Push — master ( 644ae6...bba86b )
by Daniel
10:38
created

FileFieldTest::testUploadRequiredFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms\Tests;
4
5
use SilverStripe\Dev\FunctionalTest;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\FileField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\RequiredFields;
11
12
class FileFieldTest extends FunctionalTest
13
{
14
15
    /**
16
     * Test a valid upload of a required file in a form. Error is set to 0, as the upload went well
17
     *
18
     * @skipUpgrade
19
     */
20
    public function testUploadRequiredFile()
21
    {
22
        $form = new Form(
23
            new Controller(),
24
            'Form',
25
            new FieldList(
26
                $fileField = new FileField('cv', 'Upload your CV')
27
            ),
28
            new FieldList()
29
        );
30
        $fileFieldValue = array(
31
            'name' => 'aCV.txt',
32
            'type' => 'application/octet-stream',
33
            'tmp_name' => '/private/var/tmp/phpzTQbqP',
34
            'error' => 0,
35
            'size' => 3471
36
        );
37
        $fileField->setValue($fileFieldValue);
38
39
        $this->assertTrue($form->validationResult()->isValid());
40
    }
41
42
    /**
43
     * Test different scenarii for a failed upload : an error occured, no files where provided
44
     * @skipUpgrade
45
     */
46
    public function testUploadMissingRequiredFile()
47
    {
48
        $form = new Form(
49
            new Controller(),
50
            'Form',
51
            new FieldList(
52
                $fileField = new FileField('cv', 'Upload your CV')
53
            ),
54
            new FieldList(),
55
            new RequiredFields('cv')
56
        );
57
        // All fields are filled but for some reason an error occured when uploading the file => fails
58
        $fileFieldValue = array(
59
            'name' => 'aCV.txt',
60
            'type' => 'application/octet-stream',
61
            'tmp_name' => '/private/var/tmp/phpzTQbqP',
62
            'error' => 1,
63
            'size' => 3471
64
        );
65
        $fileField->setValue($fileFieldValue);
66
67
        $this->assertFalse(
68
            $form->validationResult()->isValid(),
69
            'An error occured when uploading a file, but the validator returned true'
70
        );
71
72
        // We pass an empty set of parameters for the uploaded file => fails
73
        $fileFieldValue = array();
74
        $fileField->setValue($fileFieldValue);
75
76
        $this->assertFalse(
77
            $form->validationResult()->isValid(),
78
            'An empty array was passed as parameter for an uploaded file, but the validator returned true'
79
        );
80
81
        // We pass an null value for the uploaded file => fails
82
        $fileFieldValue = null;
83
        $fileField->setValue($fileFieldValue);
84
85
        $this->assertFalse(
86
            $form->validationResult()->isValid(),
87
            'A null value was passed as parameter for an uploaded file, but the validator returned true'
88
        );
89
    }
90
}
91