UploadFieldTest::testGetAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 68
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 54
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 68
rs 9.0036

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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