Passed
Push — master ( 9cc904...ac1dbc )
by Jason
02:25
created

SlideImageTest::testValidateName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\FlexSlider\Test;
4
5
use Dynamic\FlexSlider\Model\SlideImage;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\ORM\ValidationException;
10
use SilverStripe\Security\Member;
11
12
class SlideImageTest extends SapphireTest
13
{
14
    /**
15
     * @var string
16
     */
17
    protected static $fixture_file = 'fixtures.yml';
18
19
    /**
20
     *
21
     */
22
    public function testGetCMSFields()
23
    {
24
        $object = new SlideImage();
25
        $fieldset = $object->getCMSFields();
26
        $this->assertInstanceOf(FieldList::class, $fieldset);
27
        $this->assertNotNull($fieldset->dataFieldByName('Name'));
28
        $this->assertNotNull($fieldset->dataFieldByName('Image'));
29
    }
30
31
    /**
32
     *
33
     */
34
    public function testValidateName()
35
    {
36
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
37
        $object->Name = '';
38
        $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

38
        /** @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...
39
        $object->write();
40
    }
41
42
    /**
43
     *
44
     */
45
    public function testValidateImage()
46
    {
47
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
48
        $object->ImageID = '';
49
        $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

49
        /** @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...
50
        $object->write();
51
    }
52
53
    /**
54
     *
55
     */
56
    public function testProvidePermissions()
57
    {
58
        $object = singleton(SlideImage::class);
59
        $expected = array(
60
            'Slide_EDIT' => 'Slide Edit',
61
            'Slide_DELETE' => 'Slide Delete',
62
            'Slide_CREATE' => 'Slide Create',
63
        );
64
        $this->assertEquals($expected, $object->providePermissions());
65
    }
66
67
    /**
68
     *
69
     */
70
    public function testCanCreate()
71
    {
72
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
73
        $admin = $this->objFromFixture(Member::class, 'admin');
74
        $this->assertTrue($object->canCreate($admin));
75
76
        $author = $this->objFromFixture(Member::class, 'author');
77
        $this->assertTrue($object->canCreate($author));
78
79
        $member = $this->objFromFixture(Member::class, 'default');
80
        $this->assertFalse($object->canCreate($member));
81
    }
82
83
    /**
84
     *
85
     */
86
    public function testCanEdit()
87
    {
88
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
89
        $admin = $this->objFromFixture(Member::class, 'admin');
90
        $this->assertTrue($object->canEdit($admin));
91
92
        $author = $this->objFromFixture(Member::class, 'author');
93
        $this->assertTrue($object->canEdit($author));
94
95
        $member = $this->objFromFixture(Member::class, 'default');
96
        $this->assertFalse($object->canEdit($member));
97
    }
98
99
    /**
100
     *
101
     */
102
    public function testCanDelete()
103
    {
104
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
105
        $admin = $this->objFromFixture(Member::class, 'admin');
106
        $this->assertTrue($object->canDelete($admin));
107
108
        $author = $this->objFromFixture(Member::class, 'author');
109
        $this->assertTrue($object->canDelete($author));
110
111
        $member = $this->objFromFixture(Member::class, 'default');
112
        $this->assertFalse($object->canDelete($member));
113
    }
114
115
    /**
116
     *
117
     */
118
    public function testCanView()
119
    {
120
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
121
        $admin = $this->objFromFixture(Member::class, 'admin');
122
        $this->assertTrue($object->canView($admin));
123
124
        $author = $this->objFromFixture(Member::class, 'author');
125
        $this->assertTrue($object->canView($author));
126
127
        $member = $this->objFromFixture(Member::class, 'default');
128
        $this->assertTrue($object->canView($member));
129
    }
130
131
    /**
132
     *
133
     */
134
    public function testImageSizeLimit()
135
    {
136
        $default = 512000;
137
        $this->assertEquals(Config::modify()->get(SlideImage::class, 'image_size_limit'), $default);
138
139
        $new = 1024000;
140
        Config::modify()->update(SlideImage::class, 'image_size_limit', $new);
141
        $this->assertEquals(Config::modify()->get(SlideImage::class, 'image_size_limit'), $new);
142
    }
143
}
144