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; |
|
|
|
|
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'); |
|
|
|
|
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(), |
|
|
|
|
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'); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths