Passed
Push — master ( 091915...c8a845 )
by Roman
03:50
created

SortableUploadFieldTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 139
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 2
A testSortColumn() 0 9 1
A testExistingSortOrder() 0 17 1
A tearDown() 0 4 1
A testSchemaDefaults() 0 17 1
A testAddingFilesWithoutFormSubmission() 0 16 1
B testAddingFilesToNewRecord() 0 41 1
1
<?php
2
3
namespace Bummzack\SortableFile\Tests;
4
5
use Bummzack\SortableFile\Forms\SortableUploadField;
6
use Bummzack\SortableFile\Tests\Model\TestDataObject;
7
use SilverStripe\Assets\Dev\TestAssetStore;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Assets\Dev\TestAssetStore was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SilverStripe\Assets\File;
9
use SilverStripe\Assets\Folder;
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\Forms\FormAction;
16
use SilverStripe\ORM\ArrayList;
17
use SilverStripe\Versioned\Versioned;
18
19
class SortableUploadFieldTest extends SapphireTest
20
{
21
    protected static $fixture_file = 'SortableUploadFieldTest.yml';
22
23
    protected static $extra_dataobjects = [
24
        TestDataObject::class
25
    ];
26
27
    public function setUp()
28
    {
29
        parent::setUp();
30
31
        Versioned::set_stage(Versioned::DRAFT);
32
        $this->logInWithPermission('ADMIN');
33
        TestAssetStore::activate(__DIR__ . '/assets');
34
35
        // Copy test images for each of the fixture references
36
        /** @var File $file */
37
        $files = File::get()->exclude('ClassName', Folder::class);
38
        foreach ($files as $file) {
39
            $sourcePath = __DIR__ . '/assets/' . $file->Name;
40
            $file->setFromLocalFile($sourcePath, $file->Filename);
41
        }
42
    }
43
44
    public function tearDown()
45
    {
46
        TestAssetStore::reset();
47
        parent::tearDown();
48
    }
49
50
    public function testSchemaDefaults()
51
    {
52
        $field = SortableUploadField::create('Files');
0 ignored issues
show
Bug introduced by
'Files' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $field = SortableUploadField::create(/** @scrutinizer ignore-type */ 'Files');
Loading history...
53
54
        // Setup a dummy form, so that `getSchemaDataDefaults` doesn't error out
55
        Controller::config()->set('url_segment', 'dummy');
56
        Form::create(
57
            Controller::curr(),
0 ignored issues
show
Bug introduced by
SilverStripe\Control\Controller::curr() of type SilverStripe\Control\Controller is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
            /** @scrutinizer ignore-type */ Controller::curr(),
Loading history...
58
            'TestForm',
59
            FieldList::create($field),
60
            FieldList::create(FormAction::create('test'))
61
        );
62
63
        $data = $field->getSchemaDataDefaults();
64
65
        $this->assertArrayHasKey('sortable', $data);
66
        $this->assertTrue($data['sortable']);
67
    }
68
69
    public function testSortColumn()
70
    {
71
        $field = SortableUploadField::create('Files');
0 ignored issues
show
Bug introduced by
'Files' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $field = SortableUploadField::create(/** @scrutinizer ignore-type */ 'Files');
Loading history...
72
73
        $this->assertEquals('SortOrder', $field->getSortColumn(), 'Default value should be "SortOrder"');
74
75
        $field->setSortColumn('Sort');
76
77
        $this->assertEquals('Sort', $field->getSortColumn(), 'Changed value should be "Sort"');
78
    }
79
80
    public function testExistingSortOrder()
81
    {
82
        $obj = $this->objFromFixture(TestDataObject::class, 'obj1');
83
84
        $field = SortableUploadField::create('Files', 'Files', $obj->Files())->setRecord($obj);
0 ignored issues
show
Bug introduced by
'Files' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $field = SortableUploadField::create(/** @scrutinizer ignore-type */ 'Files', 'Files', $obj->Files())->setRecord($obj);
Loading history...
85
        $this->assertEquals(['FileA', 'FileB', 'FileC', 'FileD'], $field->getItems()->column('Title'));
86
87
        // set field items in other order
88
        $field->setValue($obj->Files()->sort('FileHash'));
89
        // Items should be returned in the correct order though.
90
        $this->assertEquals(['FileA', 'FileB', 'FileC', 'FileD'], $field->getItems()->column('Title'));
91
92
        $field = SortableUploadField::create('OtherFiles', 'OtherFiles', $obj->OtherFiles())
93
            ->setRecord($obj)
94
            ->setSortColumn('Sort');
95
96
        $this->assertEquals(['FileC', 'FileB', 'FileA'], $field->getItems()->column('Title'));
97
    }
98
99
    public function testAddingFilesToNewRecord()
100
    {
101
        // Create a new DataObject which will have an unsaved relation
102
        $obj = TestDataObject::create();
103
104
        // The file IDs to add, Should result in D,B,A,C
105
        $data = ['Files' => ['4','2','1','3']];
106
107
        $field = SortableUploadField::create('Files', 'Files', $obj->Files())->setRecord($obj);
0 ignored issues
show
Bug introduced by
'Files' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        $field = SortableUploadField::create(/** @scrutinizer ignore-type */ 'Files', 'Files', $obj->Files())->setRecord($obj);
Loading history...
108
        $field->setSubmittedValue($data);
109
        $field->saveInto($obj);
110
        $obj->write();
111
112
        $this->assertEquals(['FileD', 'FileB', 'FileA', 'FileC'], $obj->Files()->sort('SortOrder')->column('Title'));
113
        $this->assertEquals(['FileD', 'FileB', 'FileA', 'FileC'], $field->getItems()->column('Title'));
114
115
        // change the sort Order and remove an Item
116
        $field->setSubmittedValue(['Files' => ['1','2','3']]);
117
        $field->saveInto($obj);
118
        $obj->write();
119
        $this->assertEquals(['FileA', 'FileB', 'FileC'], $obj->Files()->sort('SortOrder')->column('Title'));
120
        $this->assertEquals(['FileA', 'FileB', 'FileC'], $field->getItems()->column('Title'));
121
122
        // Test persistance of sort-order if the incoming array-list doesn't contain the sort column
123
        $field->setSubmittedValue(['Files' => ['3','1','2']]);
124
        // Set the value from an arraylist without any sort-column
125
        $field->setValue(ArrayList::create(
126
            File::get()->byIDs([1,2,3])->toArray()
127
        ));
128
        $field->saveInto($obj);
129
        $obj->write();
130
        $this->assertEquals(['FileC', 'FileA', 'FileB'], $obj->Files()->sort('SortOrder')->column('Title'));
131
        $this->assertEquals(['FileC', 'FileA', 'FileB'], $field->getItems()->column('Title'));
132
133
        // Test with a newly added file
134
        $field->setSubmittedValue(['Files' => ['3','4','2','1']]);
135
        $field->saveInto($obj);
136
        $obj->write();
137
138
        $this->assertEquals(['FileC', 'FileD', 'FileB', 'FileA'], $obj->Files()->sort('SortOrder')->column('Title'));
139
        $this->assertEquals(['FileC', 'FileD', 'FileB', 'FileA'], $field->getItems()->column('Title'));
140
    }
141
142
    public function testAddingFilesWithoutFormSubmission()
143
    {
144
        // Create a new DataObject which will have an unsaved relation
145
        $obj = TestDataObject::create();
146
147
        $field = SortableUploadField::create('Files', 'Files', $obj->Files())->setRecord($obj);
0 ignored issues
show
Bug introduced by
'Files' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
        $field = SortableUploadField::create(/** @scrutinizer ignore-type */ 'Files', 'Files', $obj->Files())->setRecord($obj);
Loading history...
148
        // Set the value from a List without sort order
149
        $field->setValue(ArrayList::create(
150
            File::get()->byIDs([1,2,3])->toArray()
151
        ));
152
        $field->saveInto($obj);
153
        $obj->write();
154
155
        // Items should be ordered by ID now
156
        $this->assertEquals(['FileA', 'FileB', 'FileC'], $obj->Files()->sort('SortOrder')->column('Title'));
157
        $this->assertEquals(['FileA', 'FileB', 'FileC'], $field->getItems()->column('Title'));
158
    }
159
}
160