Completed
Pull Request — master (#105)
by Nic
12:46
created

SlideImageTest::testValidateImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 1
eloc 7
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
        $this->markTestSkipped('Need better understanding of new File system');
77
        /*$object = $this->objFromFixture(
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% 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...
78
            'Dynamic\\FlexSlider\\Model\\SlideImage',
79
            'slide1'
80
        );
81
        $image = $this->objFromFixture('Image', 'image1');
82
        $object->ImageID = $image->ID;
83
        $object->write();
84
        $objectID = $object->ID;
85
        $this->logInWithPermission('ADMIN');
86
        $originalName = $object->Name;
87
        $this->assertEquals($originalName, 'Hello World');
88
        $this->assertTrue($object->canEdit());
89
        $object->Name = 'Changed Name';
90
        $object->write();
91
        $testEdit = SlideImage::get()->byID($objectID);
92
        $this->assertEquals($testEdit->Name, 'Changed Name');
93
        $this->logOut();*/
94
    }
95
96
    public function testCanDelete()
97
    {
98
        $object = $this->objFromFixture(
99
            'Dynamic\\FlexSlider\\Model\\SlideImage',
100
            'slide1'
101
        );
102
        /*$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...
103
        $object->ImageID = $image->ID;
104
        $object->write();*/
105
        $this->logInWithPermission('ADMIN');
106
        $this->assertTrue($object->canDelete());
107
        $checkObject = $object;
108
        $object->delete();
109
        $this->assertEquals($checkObject->ID, 0);
110
    }
111
112
    public function testCanCreate()
113
    {
114
        $object = singleton(SlideImage::class);
115
        $this->logInWithPermission('ADMIN');
116
        $this->assertTrue($object->canCreate());
117
        $this->logOut();
118
        $nullMember = Member::create();
119
        $nullMember->write();
120
        $this->assertFalse($object->canCreate($nullMember));
121
        $nullMember->delete();
122
    }
123
124
    public function testProvidePermissions()
125
    {
126
        $object = singleton(SlideImage::class);
127
        $expected = array(
128
            'Slide_EDIT' => 'Slide Edit',
129
            'Slide_DELETE' => 'Slide Delete',
130
            'Slide_CREATE' => 'Slide Create',
131
        );
132
        $this->assertEquals($expected, $object->providePermissions());
133
    }
134
135
    public function testImageSizeLimit()
136
    {
137
138
        $default = 512000;
139
        $this->assertEquals(
140
            Config::inst()->get(SlideImage::class, 'image_size_limit'),
141
            $default
142
        );
143
144
        $new = 1024000;
145
        Config::modify()->set(SlideImage::class, 'image_size_limit', $new);
146
        $this->assertEquals(Config::inst()
147
            ->get(SlideImage::class, 'image_size_limit'), $new);
148
    }
149
150
}
151
152