1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bummzack\SortableFile\Tests; |
4
|
|
|
|
5
|
|
|
use Bummzack\SortableFile\Forms\SortableUploadField; |
6
|
|
|
use Bummzack\SortableFile\Tests\Model\TestManyManyThroughDataObject; |
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 Through relation functionality |
16
|
|
|
* @package Bummzack\SortableFile\Tests |
17
|
|
|
*/ |
18
|
|
|
class SortableUploadManyManyThroughTest extends SapphireTest |
19
|
|
|
{ |
20
|
|
|
protected static $fixture_file = 'SortableUploadManyManyThroughTest.yml'; |
21
|
|
|
|
22
|
|
|
protected static $extra_dataobjects = [ |
23
|
|
|
TestManyManyThroughDataObject::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(TestManyManyThroughDataObject::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
|
|
|
|
62
|
|
|
public function testAddingFilesToNewRecord() |
63
|
|
|
{ |
64
|
|
|
// Create a new DataObject which will have an unsaved relation |
65
|
|
|
$obj = TestManyManyThroughDataObject::create(); |
66
|
|
|
|
67
|
|
|
// The file IDs to add, Should result in D,B,A,C |
68
|
|
|
$data = ['Files' => ['4','2','1','3']]; |
69
|
|
|
|
70
|
|
|
$field = SortableUploadField::create('Files', 'Files', $obj->Files())->setRecord($obj); |
71
|
|
|
$field->setSubmittedValue($data); |
72
|
|
|
$field->saveInto($obj); |
73
|
|
|
$obj->write(); |
74
|
|
|
|
75
|
|
|
$this->assertEquals(['FileD', 'FileB', 'FileA', 'FileC'], $obj->Files()->sort('SortOrder')->column('Title')); |
76
|
|
|
$this->assertEquals(['FileD', 'FileB', 'FileA', 'FileC'], $field->getItems()->column('Title')); |
77
|
|
|
|
78
|
|
|
// change the sort Order and remove an Item |
79
|
|
|
$field->setSubmittedValue(['Files' => ['1','2','3']]); |
80
|
|
|
$field->saveInto($obj); |
81
|
|
|
$obj->write(); |
82
|
|
|
$this->assertEquals(['FileA', 'FileB', 'FileC'], $obj->Files()->sort('SortOrder')->column('Title')); |
83
|
|
|
$this->assertEquals(['FileA', 'FileB', 'FileC'], $field->getItems()->column('Title')); |
84
|
|
|
|
85
|
|
|
// Test persistance of sort-order if the incoming array-list doesn't contain the sort column |
86
|
|
|
$field->setSubmittedValue(['Files' => ['3','1','2']]); |
87
|
|
|
// Set the value from an arraylist without any sort-column |
88
|
|
|
$field->setValue(ArrayList::create( |
89
|
|
|
File::get()->byIDs([1,2,3])->toArray() |
90
|
|
|
)); |
91
|
|
|
$field->saveInto($obj); |
92
|
|
|
$obj->write(); |
93
|
|
|
$this->assertEquals(['FileC', 'FileA', 'FileB'], $obj->Files()->sort('SortOrder')->column('Title')); |
94
|
|
|
$this->assertEquals(['FileC', 'FileA', 'FileB'], $field->getItems()->column('Title')); |
95
|
|
|
|
96
|
|
|
// Test with a newly added file |
97
|
|
|
$field->setSubmittedValue(['Files' => ['3','4','2','1']]); |
98
|
|
|
$field->saveInto($obj); |
99
|
|
|
$obj->write(); |
100
|
|
|
|
101
|
|
|
$this->assertEquals(['FileC', 'FileD', 'FileB', 'FileA'], $obj->Files()->sort('SortOrder')->column('Title')); |
102
|
|
|
$this->assertEquals(['FileC', 'FileD', 'FileB', 'FileA'], $field->getItems()->column('Title')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testVersionedSortOrder() |
106
|
|
|
{ |
107
|
|
|
// Get the fixture object and publish it, but without relations! |
108
|
|
|
$obj = $this->objFromFixture(TestManyManyThroughDataObject::class, 'obj1'); |
109
|
|
|
$obj->publishSingle(); |
110
|
|
|
$id = $obj->ID; |
111
|
|
|
|
112
|
|
|
Versioned::set_stage(Versioned::LIVE); |
113
|
|
|
$obj = TestManyManyThroughDataObject::get()->byID($id); |
114
|
|
|
$field = SortableUploadField::create('Files', 'Files', $obj->Files())->setRecord($obj); |
115
|
|
|
$this->assertEmpty($field->getItems()->column('Title'), 'There shouldn\'t be any published records'); |
116
|
|
|
|
117
|
|
|
// Now publish everything |
118
|
|
|
$obj->publishRecursive(); |
119
|
|
|
$obj = TestManyManyThroughDataObject::get()->byID($id); |
120
|
|
|
$field = SortableUploadField::create('Files', 'Files', $obj->Files())->setRecord($obj); |
121
|
|
|
$this->assertEquals(['FileA', 'FileB', 'FileC', 'FileD'], $field->getItems()->column('Title')); |
122
|
|
|
|
123
|
|
|
Versioned::set_stage(Versioned::DRAFT); |
124
|
|
|
// change the sort Order and remove an Item |
125
|
|
|
$obj = TestManyManyThroughDataObject::get()->byID($id); |
126
|
|
|
$field = SortableUploadField::create('Files', 'Files', $obj->Files())->setRecord($obj); |
127
|
|
|
$field->setSubmittedValue(['Files' => ['3','2','1']]); |
128
|
|
|
$field->saveInto($obj); |
129
|
|
|
$obj->write(); |
130
|
|
|
|
131
|
|
|
// Should now be changed on Draft |
132
|
|
|
$this->assertEquals(['FileC', 'FileB', 'FileA'], $obj->getLinkedFiles()->column('Title')); |
133
|
|
|
|
134
|
|
|
Versioned::set_stage(Versioned::LIVE); |
135
|
|
|
// Should still be unchanged on Live |
136
|
|
|
$obj = TestManyManyThroughDataObject::get()->byID($id); |
137
|
|
|
$this->assertEquals(['FileA', 'FileB', 'FileC', 'FileD'], $obj->getLinkedFiles()->column('Title')); |
138
|
|
|
|
139
|
|
|
// Should be updated after publishing |
140
|
|
|
$obj->publishRecursive(); |
141
|
|
|
$obj = TestManyManyThroughDataObject::get()->byID($id); |
142
|
|
|
$this->assertEquals(['FileC', 'FileB', 'FileA'], $obj->getLinkedFiles()->column('Title')); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|