Completed
Pull Request — master (#105)
by Nic
06:58
created

SlideImageTest::testGetCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\FlexSlider\Test\Model;
4
5
use Dynamic\FlexSlider\Test\FlexSliderTest;
6
use Dynamic\FlexSlider\Model\SlideImage;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\ORM\ValidationException;
9
use SilverStripe\Security\Member;
10
use SilverStripe\Core\Config\Config;
11
12
/**
13
 * Class SlideImageTest
14
 * @package Dynamic\FlexSlider\Test\Model
15
 */
16
class SlideImageTest extends FlexSliderTest
17
{
18
    protected static $use_draft_site = true;
19
20
    public function setUp()
21
    {
22
        parent::setUp();
23
    }
24
25
    public function testGetCMSFields()
26
    {
27
        $object = new SlideImage();
28
        $fieldset = $object->getCMSFields();
29
        $this->assertInstanceOf(FieldList::class, $fieldset);
30
        $this->assertNotNull($fieldset->dataFieldByName('Name'));
31
        $this->assertNotNull($fieldset->dataFieldByName('Image'));
32
    }
33
34 View Code Duplication
    public function testValidateName()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $object = $this->objFromFixture(
37
            'Dynamic\\FlexSlider\\Model\\SlideImage',
38
            'slide1'
39
        );
40
        $object->Name = '';
41
        $this->setExpectedException(ValidationException::class);
42
        $object->write();
43
    }
44
45 View Code Duplication
    public function testValidateImage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $object = $this->objFromFixture(
48
            'Dynamic\\FlexSlider\\Model\\SlideImage',
49
            'slide1'
50
        );
51
        $object->ImageID = '';
52
        $this->setExpectedException(ValidationException::class);
53
        $object->write();
54
    }
55
56
    public function testCanView()
57
    {
58
        $object = $this->objFromFixture(
59
            'Dynamic\\FlexSlider\\Model\\SlideImage',
60
            'slide1'
61
        );
62
        /*$image = $this->objFromFixture('Image', 'image1');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
        $object->ImageID = $image->ID;
64
        $object->write();*/
65
        $this->logInWithPermission('ADMIN');
66
        $this->assertTrue($object->canView());
67
        $this->logOut();
68
        $nullMember = Member::create();
69
        $nullMember->write();
70
        $this->assertTrue($object->canView($nullMember));
71
        $nullMember->delete();
72
    }
73
74
    public function testCanEdit()
75
    {
76
        $object = $this->objFromFixture(
77
            'Dynamic\\FlexSlider\\Model\\SlideImage',
78
            'slide1'
79
        );
80
        /*$image = $this->objFromFixture('Image', 'image1');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
        $object->ImageID = $image->ID;
82
        $object->write();*/
83
        $objectID = $object->ID;
84
        $this->logInWithPermission('ADMIN');
85
        $originalName = $object->Name;
86
        $this->assertEquals($originalName, 'Hello World');
87
        $this->assertTrue($object->canEdit());
88
        $object->Name = 'Changed Name';
89
        $object->write();
90
        $testEdit = SlideImage::get()->byID($objectID);
91
        $this->assertEquals($testEdit->Name, 'Changed Name');
92
        $this->logOut();
93
    }
94
95
    public function testCanDelete()
96
    {
97
        $object = $this->objFromFixture(
98
            'Dynamic\\FlexSlider\\Model\\SlideImage',
99
            'slide1'
100
        );
101
        /*$image = $this->objFromFixture('Image', 'image1');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
        $object->ImageID = $image->ID;
103
        $object->write();*/
104
        $this->logInWithPermission('ADMIN');
105
        $this->assertTrue($object->canDelete());
106
        $checkObject = $object;
107
        $object->delete();
108
        $this->assertEquals($checkObject->ID, 0);
109
    }
110
111
    public function testCanCreate()
112
    {
113
        $object = singleton(SlideImage::class);
114
        $this->logInWithPermission('ADMIN');
115
        $this->assertTrue($object->canCreate());
116
        $this->logOut();
117
        $nullMember = Member::create();
118
        $nullMember->write();
119
        $this->assertFalse($object->canCreate($nullMember));
120
        $nullMember->delete();
121
    }
122
123
    public function testProvidePermissions()
124
    {
125
        $object = singleton(SlideImage::class);
126
        $expected = array(
127
            'Slide_EDIT' => 'Slide Edit',
128
            'Slide_DELETE' => 'Slide Delete',
129
            'Slide_CREATE' => 'Slide Create',
130
        );
131
        $this->assertEquals($expected, $object->providePermissions());
132
    }
133
134
    public function testImageSizeLimit()
135
    {
136
137
        $default = 512000;
138
        $this->assertEquals(
139
            Config::inst()->get(SlideImage::class, 'image_size_limit'),
140
            $default
141
        );
142
143
        $new = 1024000;
144
        Config::modify()->set(SlideImage::class, 'image_size_limit', $new);
145
        $this->assertEquals(Config::inst()
146
            ->get(SlideImage::class, 'image_size_limit'), $new);
147
    }
148
149
}
150
151