Completed
Pull Request — master (#331)
by Damian
01:58
created

UploadFieldTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 91
Duplicated Lines 17.58 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 10
dl 16
loc 91
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 16 16 1
A tearDown() 0 5 1
A testGetAttributes() 0 63 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\AssetAdmin\Tests\Forms;
4
5
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
6
use SilverStripe\Assets\File;
7
use SilverStripe\AssetAdmin\Forms\UploadField;
8
use SilverStripe\Assets\Image;
9
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
10
use SilverStripe\Control\Controller;
11
use SilverStripe\Dev\Debug;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\Forms\FieldList;
14
use SilverStripe\Forms\Form;
15
use SilverStripe\ORM\ArrayList;
16
17
class UploadFieldTest extends SapphireTest
18
{
19
    protected static $fixture_file = 'FileFormBuilderTest.yml';
20
21 View Code Duplication
    public function setUp()
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...
22
    {
23
        parent::setUp();
24
25
        // Set backend and base url
26
        TestAssetStore::activate('FileFormBuilderTest');
27
28
        /** @var File $testfile */
29
        $testfile = $this->objFromFixture(File::class, 'file1');
30
        $testfile->setFromLocalFile(__DIR__ . '/fixtures/testfile.txt', 'files/testfile.txt');
31
        $testfile->write();
32
33
        /** @var Image $testimage */
34
        $testimage = $this->objFromFixture(Image::class, 'image1');
35
        $testimage->setFromLocalFile(__DIR__ . '/fixtures/testimage.png', 'files/testimage.png');
36
    }
37
38
    public function tearDown()
39
    {
40
        TestAssetStore::reset();
41
        parent::tearDown();
42
    }
43
44
    public function testGetAttributes()
45
    {
46
        $field = UploadField::create('MyField');
47
        $field->addExtraClass('myfield');
48
        $field->setIsMultiUpload(false);
49
        $field->setFolderName('/');
50
        /** @var Image $image */
51
        $image = $this->objFromFixture(Image::class, 'image1');
52
        $field->setItems(new ArrayList([$image]));
53
        Form::create(new Controller(), 'MyForm', FieldList::create($field), FieldList::create());
54
        $admin = new AssetAdmin();
55
56
        $attributes = $field->getAttributes();
57
        $schema = [
58
            'name' => 'MyField',
59
            'id' => 'Form_MyForm_MyField',
60
            'type' => 'Custom',
61
            'component' => 'UploadField',
62
            'holderId' => 'Form_MyForm_MyField_Holder',
63
            'title' => 'My Field',
64
            'source' => null,
65
            'extraClass' => 'entwine-uploadfield uploadfield myfield',
66
            'description' => null,
67
            'rightTitle' => null,
68
            'leftTitle' => null,
69
            'readOnly' => false,
70
            'disabled' => false,
71
            'customValidationMessage' => '',
72
            'validation' => [],
73
            'attributes' => [],
74
            'data' => [
75
                'createFileEndpoint' => [
76
                    'url' => 'admin/assets/api/createFile',
77
                    'method' => 'post',
78
                    'payloadFormat' => 'urlencoded',
79
                ],
80
                'multi' => false,
81
                'parentid' => 0,
82
            ],
83
        ];
84
        $state = [
85
            'name' => 'MyField',
86
            'id' => 'Form_MyForm_MyField',
87
            'value' => [ 'Files' => [$image->ID] ],
88
            'message' => null,
89
            'data' => [
90
                'files' => [ $admin->getObjectFromData($image) ],
91
            ],
92
        ];
93
        $this->assertArraySubset(
94
            [
95
                'class' => 'entwine-uploadfield uploadfield myfield',
96
                'type' => 'file',
97
                'multiple' => false,
98
                'id' => 'Form_MyForm_MyField'
99
            ],
100
            $attributes
101
        );
102
103
        // Check schema / state are encoded in this field
104
        $this->assertEquals($schema, json_decode($attributes['data-schema'], true));
105
        $this->assertEquals($state, json_decode($attributes['data-state'], true));
106
    }
107
}
108