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

UploadFieldTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
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