EditableFileFieldTest::testUpdatedMaxFileSize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\UserForms\Tests\Model\EditableFormField;
4
5
use SilverStripe\Assets\Folder;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\ORM\ValidationException;
8
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
9
10
/**
11
 * @package userforms
12
 */
13
class EditableFileFieldTest extends SapphireTest
14
{
15
    protected static $fixture_file = '../EditableFormFieldTest.yml';
16
17
    /**
18
     * @var
19
     */
20
    private $php_max_file_size;
21
22
    /**
23
     * Hold the server default max file size upload limit for later
24
     */
25
    protected function setUp()
26
    {
27
        parent::setUp();
28
29
        $editableFileField = singleton(EditableFileField::class);
30
        $this->php_max_file_size = $editableFileField::get_php_max_file_size();
31
    }
32
33
    /**
34
     * Test that the field validator has the server default as the max file size upload
35
     */
36
    public function testDefaultMaxFileSize()
37
    {
38
        $fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
39
        $formField = $fileField->getFormField();
40
41
        $this->assertEquals($this->php_max_file_size, $formField->getValidator()->getAllowedMaxFileSize());
42
    }
43
44
    /**
45
     * Test that validation prevents the provided upload size limit to be less than or equal to the max php size
46
     */
47
    public function testValidateFileSizeFieldValue()
48
    {
49
50
        $fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
51
        $this->setExpectedException(ValidationException::class);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit_Framework_TestCase::setExpectedException() has been deprecated: Method deprecated since Release 5.2.0; use expectException() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

51
        /** @scrutinizer ignore-deprecated */ $this->setExpectedException(ValidationException::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
52
        $fileField->MaxFileSizeMB = $this->php_max_file_size * 2;
53
        $fileField->write();
54
    }
55
56
    /**
57
     * Test the field validator has the updated allowed max file size
58
     */
59
    public function testUpdatedMaxFileSize()
60
    {
61
        $fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
62
        $fileField->MaxFileSizeMB = .25;
63
        $fileField->write();
64
65
        $formField = $fileField->getFormField();
66
        $this->assertEquals($formField->getValidator()->getAllowedMaxFileSize(), 262144);
67
    }
68
69
    public function testAllowEmptyTitle()
70
    {
71
        /** @var EditableFileField $field */
72
        $field = EditableFileField::create();
73
        $field->Name = 'EditableFormField_123456';
74
        $this->assertEmpty($field->getFormField()->Title());
75
    }
76
77
    public function testOnBeforeWrite()
78
    {
79
        $this->logOut();
80
81
        /** @var EditableFileField $fileField */
82
        $fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
83
84
        $defaultFolder = Folder::find('Form-submissions');
85
        $this->assertNotEmpty($defaultFolder, 'Default Folder was created along with the EditableFileField');
86
        $this->assertFalse($defaultFolder->canView(), 'Default Folder default to being restricted');
87
        $this->assertFalse((boolean)$fileField->FolderConfirmed, 'EditableFileField are not Folder Confirmed initially');
88
89
        $this->assertEquals(
90
            $defaultFolder->ID,
91
            $fileField->FolderID,
92
            'EditableFileField default to default form submission folder'
93
        );
94
95
        $fileField->FolderID = Folder::find_or_make('boom')->ID;
96
        $fileField->write();
97
        $this->assertTrue(
98
            (boolean)$fileField->FolderConfirmed,
99
            'EditableFileField are Folder Confirmed once you assigned them a folder'
100
        );
101
102
        $secondField = EditableFileField::create();
103
        $secondField->ParentID = $fileField->ParentID;
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on SilverStripe\UserForms\M...Field\EditableFileField. Since you implemented __get, consider adding a @property annotation.
Loading history...
104
        $secondField->write();
105
106
        $this->assertEquals(
107
            $fileField->FolderID,
108
            $secondField->FolderID,
109
            'Second EditableFileField defaults to first field FolderID'
110
        );
111
    }
112
}
113