1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package silverstripe-versionedfiles |
4
|
|
|
* @subpackage tests |
5
|
|
|
*/ |
6
|
|
|
class VersionedFileTest extends FunctionalTest |
7
|
|
|
{ |
8
|
|
|
protected $usesDatabase = true; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var Folder |
12
|
|
|
*/ |
13
|
|
|
protected $folder; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var File |
17
|
|
|
*/ |
18
|
|
|
protected $file; |
19
|
|
|
|
20
|
|
|
public function setUp() |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
|
24
|
|
|
$this->folder = Folder::find_or_make(ASSETS_DIR . '/versionedfiles-test'); |
|
|
|
|
25
|
|
|
|
26
|
|
|
$file = $this->folder->getFullPath() . 'test-file.txt'; |
27
|
|
|
file_put_contents($file, 'first-version'); |
28
|
|
|
|
29
|
|
|
$this->file = new File(); |
30
|
|
|
$this->file->ParentID = $this->folder->ID; |
31
|
|
|
$this->file->Filename = $this->folder->getFilename() . 'test-file.txt'; |
32
|
|
|
$this->file->write(); |
33
|
|
|
|
34
|
|
|
SecurityToken::disable(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function tearDown() |
38
|
|
|
{ |
39
|
|
|
SecurityToken::enable(); |
40
|
|
|
|
41
|
|
|
$this->folder->deleteDatabaseOnly(); |
42
|
|
|
Filesystem::removeFolder($this->folder->getFullPath()); |
43
|
|
|
|
44
|
|
|
parent::tearDown(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testMovingFileToAnotherDirectory() |
48
|
|
|
{ |
49
|
|
|
$oldDir = '/OldDir/'; |
50
|
|
|
$newDir = '/NewDir/'; |
51
|
|
|
|
52
|
|
|
// Create a directory |
53
|
|
|
$oldFolder = Folder::find_or_make($oldDir); |
54
|
|
|
|
55
|
|
|
// Create a file |
56
|
|
|
file_put_contents($oldFolder->getFullPath() . 'test-file.txt', 'first-content'); |
57
|
|
|
$oldFile = File::create(); |
58
|
|
|
$oldFile->ParentID = $oldFolder->ID; |
59
|
|
|
$oldFile->Filename = $oldFolder->getFilename() . 'test-file.txt'; |
60
|
|
|
$oldFile->write(); |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
// Create a new directory & move the file to the new directory |
64
|
|
|
$newFolder = Folder::find_or_make($newDir); |
65
|
|
|
$newFile = File::get()->byID($oldFile->ID); |
66
|
|
|
$newFile->ParentID = $newFolder->ID; |
67
|
|
|
$newFile->Filename = $newFolder->getFilename() . 'test-file.txt'; |
68
|
|
|
$newFile->write(); |
69
|
|
|
|
70
|
|
|
// Assert file exists in new directory |
71
|
|
|
$this->assertTrue(is_file($newFile->getFullPath())); |
|
|
|
|
72
|
|
|
|
73
|
|
|
// Delete files |
74
|
|
|
$newFile->delete(); |
75
|
|
|
$oldFile->delete(); |
76
|
|
|
|
77
|
|
|
// Delete directories & files created in this test case |
78
|
|
|
array_map('unlink', glob(BASE_PATH . '/'. ASSETS_DIR . $oldDir . '/*.*')); |
79
|
|
|
if (is_dir($oldDir)) { |
80
|
|
|
rmdir($oldDir); |
81
|
|
|
} |
82
|
|
|
array_map('unlink', glob(BASE_PATH . '/'. ASSETS_DIR . $newDir . '/*.*')); |
83
|
|
|
if (is_dir($newDir)) { |
84
|
|
|
rmdir($newDir); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testInitialSaveCreatesVersion() |
89
|
|
|
{ |
90
|
|
|
$this->assertNull( |
|
|
|
|
91
|
|
|
$this->folder->CurrentVersionID, |
92
|
|
|
'Folders do not have versions created.' |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$this->assertEquals( |
|
|
|
|
96
|
|
|
1, $this->file->getVersionNumber(), |
97
|
|
|
'Files have initial versions vreated.' |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$this->assertEquals( |
|
|
|
|
101
|
|
|
'first-version', |
102
|
|
|
file_get_contents($this->file->CurrentVersion()->getFullPath()), |
103
|
|
|
'Files are copied to a stored version directory.' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testNewVersionIncrementsVersionNumber() |
108
|
|
|
{ |
109
|
|
|
file_put_contents($this->file->getFullPath(), 'second-version'); |
110
|
|
|
$this->file->createVersion(); |
111
|
|
|
$this->assertEquals( |
|
|
|
|
112
|
|
|
2, DataObject::get_by_id('File', $this->file->ID)->getVersionNumber(), |
113
|
|
|
'The version number has incremented.' |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
file_put_contents($this->file->getFullPath(), 'third-version'); |
117
|
|
|
$this->file->createVersion(); |
118
|
|
|
$this->assertEquals( |
|
|
|
|
119
|
|
|
3, DataObject::get_by_id('File', $this->file->ID)->getVersionNumber(), |
120
|
|
|
'The version number has incremented.' |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testRollbackReplacesFile() |
125
|
|
|
{ |
126
|
|
|
file_put_contents($this->file->getFullPath(), 'second-version'); |
127
|
|
|
$this->file->createVersion(); |
128
|
|
|
|
129
|
|
|
$this->logInWithPermission('ADMIN'); |
130
|
|
|
$this->getFileEditForm(); |
131
|
|
|
|
132
|
|
|
$this->assertEquals( |
|
|
|
|
133
|
|
|
'second-version', |
134
|
|
|
file_get_contents($this->file->CurrentVersion()->getFullPath()) |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$form = $this->mainSession->lastPage()->getFormById('Form_ItemEditForm'); |
138
|
|
|
$url = Director::makeRelative($form->getAction()->asString()); |
139
|
|
|
$data = array(); |
140
|
|
|
|
141
|
|
|
foreach ($form->_widgets as $widget) { |
142
|
|
|
$data[$widget->getName()] = $widget->getValue(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->post($url, array_merge($data, array( |
146
|
|
|
'Replace' => 'rollback', |
147
|
|
|
'PreviousVersion' => 1, |
148
|
|
|
'ReplacementFile' => array(), |
149
|
|
|
))); |
150
|
|
|
|
151
|
|
|
$this->assertEquals( |
|
|
|
|
152
|
|
|
'first-version', file_get_contents($this->file->getFullPath()) |
153
|
|
|
); |
154
|
|
|
$this->assertEquals( |
|
|
|
|
155
|
|
|
1, DataObject::get_by_id('File', $this->file->ID)->getVersionNumber() |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* We need to test that the _versions folder isn't completed wiped by |
161
|
|
|
* {@link VersionedFileExtension::onBeforeDelete()} when there is more than the file currently being deleted. |
162
|
|
|
*/ |
163
|
|
|
public function testOnBeforeDelete() |
164
|
|
|
{ |
165
|
|
|
// Create the second file |
166
|
|
|
$file2 = $this->folder->getFullPath() . 'test-file2.txt'; |
167
|
|
|
file_put_contents($file2, 'first-version'); |
168
|
|
|
|
169
|
|
|
$file2Obj = new File(); |
170
|
|
|
$file2Obj->ParentID = $this->folder->ID; |
171
|
|
|
$file2Obj->Filename = $this->folder->getFilename() . 'test-file2.txt'; |
172
|
|
|
$file2Obj->write(); |
173
|
|
|
|
174
|
|
|
// Create a second version of the second file |
175
|
|
|
file_put_contents($file2Obj->getFullPath(), 'second-version'); |
176
|
|
|
$file2Obj->createVersion(); |
177
|
|
|
|
178
|
|
|
// Delete the second file |
179
|
|
|
$file2Obj->delete(); |
180
|
|
|
|
181
|
|
|
// Ensure the _versions folder still exists |
182
|
|
|
$this->assertTrue(is_dir($this->folder->getFullPath())); |
|
|
|
|
183
|
|
|
$this->assertTrue(is_dir($this->folder->getFullPath() . '/_versions')); |
|
|
|
|
184
|
|
|
|
185
|
|
|
// Now delete the first file, and ensure the _versions folder no longer exists |
186
|
|
|
$this->file->delete(); |
187
|
|
|
|
188
|
|
|
$this->assertTrue(is_dir($this->folder->getFullPath())); |
|
|
|
|
189
|
|
|
$this->assertFalse(is_dir($this->folder->getFullPath() . '/_versions')); |
|
|
|
|
190
|
|
|
|
191
|
|
|
// Now create another file to ensure that the _versions folder can be successfully re-created |
192
|
|
|
$file3 = $this->folder->getFullPath() . 'test-file3.txt'; |
193
|
|
|
file_put_contents($file3, 'first-version'); |
194
|
|
|
|
195
|
|
|
$file3Obj = new File(); |
196
|
|
|
$file3Obj->ParentID = $this->folder->ID; |
197
|
|
|
$file3Obj->Filename = $this->folder->getFilename() . 'test-file3.txt'; |
198
|
|
|
$file3Obj->write(); |
199
|
|
|
|
200
|
|
|
$this->assertTrue(is_file($file3Obj->getFullPath())); |
|
|
|
|
201
|
|
|
$this->assertTrue(is_dir($this->folder->getFullPath() . '/_versions')); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
protected function getFileEditForm() |
205
|
|
|
{ |
206
|
|
|
$admin = new AssetAdmin(); |
207
|
|
|
$folder = Controller::join_links( |
208
|
|
|
$admin->Link(), 'show', $this->folder->ID |
209
|
|
|
); |
210
|
|
|
$file = Controller::join_links( |
211
|
|
|
$admin->Link(), 'EditForm/field/File/item', $this->file->ID, 'edit' |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
$this->get($folder); |
215
|
|
|
$this->get($file); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.