Completed
Push — master ( 0eed3f...e827e0 )
by Nic
02:44
created

SlideImageTest::testGetViewerTemplates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\FlexSlider\Test;
4
5
use Dynamic\FlexSlider\Model\SlideImage;
6
use Sheadawson\Linkable\Forms\EmbeddedObjectField;
7
use SilverStripe\AssetAdmin\Forms\UploadField;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\TextareaField;
12
use SilverStripe\Forms\TextField;
13
use SilverStripe\ORM\ValidationException;
14
use SilverStripe\Security\Member;
15
16
/**
17
 * Class SlideImageTest
18
 * @package Dynamic\FlexSlider\Test
19
 */
20
class SlideImageTest extends SapphireTest
21
{
22
    /**
23
     * @var string
24
     */
25
    protected static $fixture_file = 'fixtures.yml';
26
27
    /**
28
     *
29
     */
30
    public function testGetCMSFields()
31
    {
32
        $object = SlideImage::singleton();
33
        $fields = $object->getCMSFields();
34
35
        $this->assertInstanceOf(FieldList::class, $fields);
36
        $this->assertInstanceOf(TextField::class, $fields->dataFieldByName('Name'));
37
        $this->assertInstanceOf(UploadField::class, $fields->dataFieldByName('Image'));
38
        $this->assertInstanceOf(EmbeddedObjectField::class, $fields->dataFieldByName('Video'));
39
        $this->assertInstanceOf(TextareaField::class, $fields->dataFieldByName('Description'));
40
    }
41
42
    /**
43
     * @throws ValidationException
44
     */
45
    public function testValidateName()
46
    {
47
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
48
        $object->Name = '';
49
        $object->ImageID = 1;
50
        $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

50
        /** @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...
51
        $object->write();
52
53
        $object->Name = 'Foo';
54
        $this->assertGreaterThan(0, $object->write());
55
    }
56
57
    /**
58
     * @throws ValidationException
59
     */
60
    public function testValidateImage()
61
    {
62
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
63
        $object->ImageID = null;
64
        $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

64
        /** @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...
65
        $object->write();
66
67
        $base->ImageID = 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $base seems to be never defined.
Loading history...
68
        $this->assertGreaterThan(0, $object->write());
69
    }
70
71
    /**
72
     * @throws ValidationException
73
     */
74
    public function testValidateVideo()
75
    {
76
        $object = $this->objFromFixture(SlideImage::class, 'slide2');
77
        $object->VideoID = null;
78
        $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

78
        /** @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...
79
        $object->write();
80
81
        $object->VideoID = 1;
82
        $this->assertGreaterThan(0, $object->write());
83
    }
84
85
    /**
86
     * @throws ValidationException
87
     */
88
    public function testValidateText()
89
    {
90
        $object = $this->objFromFixture(SlideImage::class, 'slide3');
91
        $description = $object->Description;
92
93
        $object->Description = null;
94
        $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

94
        /** @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...
95
        $object->write();
96
97
        $object->Description = $description;
98
        $this->assertGreaterThan(0, $object->write());
99
    }
100
101
    /**
102
     *
103
     */
104
    public function testProvidePermissions()
105
    {
106
        $object = SlideImage::singleton();
107
108
        $expected = [
109
            'Slide_EDIT' => 'Slide Edit',
110
            'Slide_DELETE' => 'Slide Delete',
111
            'Slide_CREATE' => 'Slide Create',
112
        ];
113
114
        $this->assertEquals($expected, $object->providePermissions());
115
    }
116
117
    /**
118
     *
119
     */
120
    public function testCanCreate()
121
    {
122
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
123
        $admin = $this->objFromFixture(Member::class, 'admin');
124
        $this->assertTrue($object->canCreate($admin));
125
126
        $author = $this->objFromFixture(Member::class, 'author');
127
        $this->assertTrue($object->canCreate($author));
128
129
        $member = $this->objFromFixture(Member::class, 'default');
130
        $this->assertFalse($object->canCreate($member));
131
    }
132
133
    /**
134
     *
135
     */
136
    public function testCanEdit()
137
    {
138
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
139
        $admin = $this->objFromFixture(Member::class, 'admin');
140
        $this->assertTrue($object->canEdit($admin));
141
142
        $author = $this->objFromFixture(Member::class, 'author');
143
        $this->assertTrue($object->canEdit($author));
144
145
        $member = $this->objFromFixture(Member::class, 'default');
146
        $this->assertFalse($object->canEdit($member));
147
    }
148
149
    /**
150
     *
151
     */
152
    public function testCanDelete()
153
    {
154
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
155
        $admin = $this->objFromFixture(Member::class, 'admin');
156
        $this->assertTrue($object->canDelete($admin));
157
158
        $author = $this->objFromFixture(Member::class, 'author');
159
        $this->assertTrue($object->canDelete($author));
160
161
        $member = $this->objFromFixture(Member::class, 'default');
162
        $this->assertFalse($object->canDelete($member));
163
    }
164
165
    /**
166
     *
167
     */
168
    public function testCanView()
169
    {
170
        $object = $this->objFromFixture(SlideImage::class, 'slide1');
171
        $admin = $this->objFromFixture(Member::class, 'admin');
172
        $this->assertTrue($object->canView($admin));
173
174
        $author = $this->objFromFixture(Member::class, 'author');
175
        $this->assertTrue($object->canView($author));
176
177
        $member = $this->objFromFixture(Member::class, 'default');
178
        $this->assertTrue($object->canView($member));
179
    }
180
181
    /**
182
     *
183
     */
184
    public function testImageSizeLimit()
185
    {
186
        $default = 512000;
187
        $this->assertEquals(Config::modify()->get(SlideImage::class, 'image_size_limit'), $default);
188
189
        $new = 1024000;
190
        Config::modify()->update(SlideImage::class, 'image_size_limit', $new);
191
        $this->assertEquals(Config::modify()->get(SlideImage::class, 'image_size_limit'), $new);
192
    }
193
194
    /**
195
     *
196
     */
197
    public function testGetViewerTemplates()
198
    {
199
        $slide = SlideImage::singleton();
200
201
        $slide->SlideType = 'Image';
202
        $imageTemplate = SlideImage::class . "_Image";
203
204
        $this->assertTrue(in_array($imageTemplate, $slide->getViewerTemplates()));
205
206
        $slide->SlideType = 'Video';
207
        $videoTemplate = SlideImage::class . "_Video";
208
209
        $this->assertTrue(in_array($videoTemplate, $slide->getViewerTemplates()));
210
211
        $slide->SlideType = 'Text';
212
        $textTemplate = SlideImage::class . "_Text";
213
214
        $this->assertTrue(in_array($textTemplate, $slide->getViewerTemplates()));
215
    }
216
}
217