Completed
Pull Request — master (#647)
by Robbie
05:30 queued 03:19
created

testValidateFileSizeFieldValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

Loading history...
51
        $fileField->MaxFileSizeMB = $this->php_max_file_size * 2;
52
        $fileField->write();
53
    }
54
55
    /**
56
     * Test the field validator has the updated allowed max file size
57
     */
58
    public function testUpdatedMaxFileSize()
59
    {
60
        $fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
61
        $fileField->MaxFileSizeMB = .25;
62
        $fileField->write();
63
64
        $formField = $fileField->getFormField();
65
        $this->assertEquals($formField->getValidator()->getAllowedMaxFileSize(), 262144);
66
    }
67
}
68