1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bummzack\SortableFile\Tests; |
4
|
|
|
|
5
|
|
|
use Bummzack\SortableFile\Forms\SortableUploadField; |
6
|
|
|
use Bummzack\SortableFile\Tests\Model\TestManyManyDataObject; |
7
|
|
|
use SilverStripe\Assets\Dev\TestAssetStore; |
8
|
|
|
use SilverStripe\Assets\File; |
9
|
|
|
use SilverStripe\Assets\Folder; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use SilverStripe\ORM\ArrayList; |
12
|
|
|
use SilverStripe\Versioned\Versioned; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Test Many Many relation functionality |
16
|
|
|
* @package Bummzack\SortableFile\Tests |
17
|
|
|
*/ |
18
|
|
|
class SortableUploadManyManyTest extends SapphireTest |
19
|
|
|
{ |
20
|
|
|
protected static $fixture_file = 'SortableUploadManyManyTest.yml'; |
21
|
|
|
|
22
|
|
|
protected static $extra_dataobjects = [ |
23
|
|
|
TestManyManyDataObject::class |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
parent::setUp(); |
29
|
|
|
|
30
|
|
|
Versioned::set_stage(Versioned::DRAFT); |
31
|
|
|
$this->logInWithPermission('ADMIN'); |
32
|
|
|
TestAssetStore::activate(__DIR__ . '/assets'); |
33
|
|
|
|
34
|
|
|
// Copy test images for each of the fixture references |
35
|
|
|
/** @var File $file */ |
36
|
|
|
$files = File::get()->exclude('ClassName', Folder::class); |
37
|
|
|
foreach ($files as $file) { |
38
|
|
|
$sourcePath = __DIR__ . '/assets/' . $file->Name; |
39
|
|
|
$file->setFromLocalFile($sourcePath, $file->Filename); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function tearDown() |
44
|
|
|
{ |
45
|
|
|
TestAssetStore::reset(); |
46
|
|
|
parent::tearDown(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testExistingSortOrder() |
50
|
|
|
{ |
51
|
|
|
$obj = $this->objFromFixture(TestManyManyDataObject::class, 'obj1'); |
52
|
|
|
|
53
|
|
|
$field = SortableUploadField::create('Files', 'Files', $obj->Files())->setRecord($obj); |
54
|
|
|
$this->assertEquals(['FileA', 'FileB', 'FileC', 'FileD'], $field->getItems()->column('Title')); |
55
|
|
|
|
56
|
|
|
// set field items in other order |
57
|
|
|
$field->setValue($obj->Files()->sort('FileHash')); |
58
|
|
|
// Items should be returned in the correct order though. |
59
|
|
|
$this->assertEquals(['FileA', 'FileB', 'FileC', 'FileD'], $field->getItems()->column('Title')); |
60
|
|
|
|
61
|
|
|
$field = SortableUploadField::create('OtherFiles', 'OtherFiles', $obj->OtherFiles()) |
62
|
|
|
->setRecord($obj) |
63
|
|
|
->setSortColumn('Sort'); |
64
|
|
|
|
65
|
|
|
$this->assertEquals(['FileC', 'FileB', 'FileA'], $field->getItems()->column('Title')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testAddingFilesToNewRecord() |
69
|
|
|
{ |
70
|
|
|
// Create a new DataObject which will have an unsaved relation |
71
|
|
|
$obj = TestManyManyDataObject::create(); |
72
|
|
|
|
73
|
|
|
// The file IDs to add, Should result in D,B,A,C |
74
|
|
|
$data = ['Files' => ['4','2','1','3']]; |
75
|
|
|
|
76
|
|
|
$field = SortableUploadField::create('Files', 'Files', $obj->Files())->setRecord($obj); |
77
|
|
|
$field->setSubmittedValue($data); |
78
|
|
|
$field->saveInto($obj); |
79
|
|
|
$obj->write(); |
80
|
|
|
|
81
|
|
|
$this->assertEquals(['FileD', 'FileB', 'FileA', 'FileC'], $obj->Files()->sort('SortOrder')->column('Title')); |
82
|
|
|
$this->assertEquals(['FileD', 'FileB', 'FileA', 'FileC'], $field->getItems()->column('Title')); |
83
|
|
|
|
84
|
|
|
// change the sort Order and remove an Item |
85
|
|
|
$field->setSubmittedValue(['Files' => ['1','2','3']]); |
86
|
|
|
$field->saveInto($obj); |
87
|
|
|
$obj->write(); |
88
|
|
|
$this->assertEquals(['FileA', 'FileB', 'FileC'], $obj->Files()->sort('SortOrder')->column('Title')); |
89
|
|
|
$this->assertEquals(['FileA', 'FileB', 'FileC'], $field->getItems()->column('Title')); |
90
|
|
|
|
91
|
|
|
// Test persistance of sort-order if the incoming array-list doesn't contain the sort column |
92
|
|
|
$field->setSubmittedValue(['Files' => ['3','1','2']]); |
93
|
|
|
// Set the value from an arraylist without any sort-column |
94
|
|
|
$field->setValue(ArrayList::create( |
95
|
|
|
File::get()->byIDs([1,2,3])->toArray() |
96
|
|
|
)); |
97
|
|
|
$field->saveInto($obj); |
98
|
|
|
$obj->write(); |
99
|
|
|
$this->assertEquals(['FileC', 'FileA', 'FileB'], $obj->Files()->sort('SortOrder')->column('Title')); |
100
|
|
|
$this->assertEquals(['FileC', 'FileA', 'FileB'], $field->getItems()->column('Title')); |
101
|
|
|
|
102
|
|
|
// Test with a newly added file |
103
|
|
|
$field->setSubmittedValue(['Files' => ['3','4','2','1']]); |
104
|
|
|
$field->saveInto($obj); |
105
|
|
|
$obj->write(); |
106
|
|
|
|
107
|
|
|
$this->assertEquals(['FileC', 'FileD', 'FileB', 'FileA'], $obj->Files()->sort('SortOrder')->column('Title')); |
108
|
|
|
$this->assertEquals(['FileC', 'FileD', 'FileB', 'FileA'], $field->getItems()->column('Title')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testAddingFilesWithoutFormSubmission() |
112
|
|
|
{ |
113
|
|
|
// Create a new DataObject which will have an unsaved relation |
114
|
|
|
$obj = TestManyManyDataObject::create(); |
115
|
|
|
|
116
|
|
|
$field = SortableUploadField::create('Files', 'Files', $obj->Files())->setRecord($obj); |
117
|
|
|
// Set the value from a List without sort order |
118
|
|
|
$field->setValue(ArrayList::create( |
119
|
|
|
File::get()->byIDs([1,2,3])->toArray() |
120
|
|
|
)); |
121
|
|
|
$field->saveInto($obj); |
122
|
|
|
$obj->write(); |
123
|
|
|
|
124
|
|
|
// Items should be ordered by ID now |
125
|
|
|
$this->assertEquals(['FileA', 'FileB', 'FileC'], $obj->Files()->sort('SortOrder')->column('Title')); |
126
|
|
|
$this->assertEquals(['FileA', 'FileB', 'FileC'], $field->getItems()->column('Title')); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|