|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2013 Thomas Müller <[email protected]> |
|
4
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
5
|
|
|
* later. |
|
6
|
|
|
* See the COPYING-README file. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Test\OC\Connector\Sabre; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use OC\Files\FileInfo; |
|
13
|
|
|
use OC\Connector\Sabre\Directory; |
|
14
|
|
|
use OC\Files\Storage\Temporary; |
|
15
|
|
|
|
|
16
|
|
|
class TestDoubleFileView extends \OC\Files\View { |
|
17
|
|
|
|
|
18
|
|
|
public function __construct($updatables, $deletables, $canRename = true) { |
|
19
|
|
|
$this->updatables = $updatables; |
|
|
|
|
|
|
20
|
|
|
$this->deletables = $deletables; |
|
|
|
|
|
|
21
|
|
|
$this->canRename = $canRename; |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function isUpdatable($path) { |
|
25
|
|
|
return $this->updatables[$path]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function isCreatable($path) { |
|
29
|
|
|
return $this->updatables[$path]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function isDeletable($path) { |
|
33
|
|
|
return $this->deletables[$path]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function rename($path1, $path2) { |
|
37
|
|
|
return $this->canRename; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getRelativePath($path) { |
|
41
|
|
|
return $path; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
class ObjectTree extends \Test\TestCase { |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @dataProvider moveFailedProvider |
|
49
|
|
|
* @expectedException \Sabre\DAV\Exception\Forbidden |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testMoveFailed($source, $destination, $updatables, $deletables) { |
|
52
|
|
|
$this->moveTest($source, $destination, $updatables, $deletables); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @dataProvider moveSuccessProvider |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testMoveSuccess($source, $destination, $updatables, $deletables) { |
|
59
|
|
|
$this->moveTest($source, $destination, $updatables, $deletables); |
|
60
|
|
|
$this->assertTrue(true); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @dataProvider moveFailedInvalidCharsProvider |
|
65
|
|
|
* @expectedException \OC\Connector\Sabre\Exception\InvalidPath |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testMoveFailedInvalidChars($source, $destination, $updatables, $deletables) { |
|
68
|
|
|
$this->moveTest($source, $destination, $updatables, $deletables); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
function moveFailedInvalidCharsProvider() { |
|
72
|
|
|
return array( |
|
73
|
|
|
array('a/b', 'a/*', array('a' => true, 'a/b' => true, 'a/c*' => false), array()), |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
function moveFailedProvider() { |
|
78
|
|
|
return array( |
|
79
|
|
|
array('a/b', 'a/c', array('a' => false, 'a/b' => false, 'a/c' => false), array()), |
|
80
|
|
|
array('a/b', 'b/b', array('a' => false, 'a/b' => false, 'b' => false, 'b/b' => false), array()), |
|
81
|
|
|
array('a/b', 'b/b', array('a' => false, 'a/b' => true, 'b' => false, 'b/b' => false), array()), |
|
82
|
|
|
array('a/b', 'b/b', array('a' => true, 'a/b' => true, 'b' => false, 'b/b' => false), array()), |
|
83
|
|
|
array('a/b', 'b/b', array('a' => true, 'a/b' => true, 'b' => true, 'b/b' => false), array('a/b' => false)), |
|
84
|
|
|
array('a/b', 'a/c', array('a' => false, 'a/b' => true, 'a/c' => false), array()), |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
function moveSuccessProvider() { |
|
89
|
|
|
return array( |
|
90
|
|
|
array('a/b', 'b/b', array('a' => true, 'a/b' => true, 'b' => true, 'b/b' => false), array('a/b' => true)), |
|
91
|
|
|
// older files with special chars can still be renamed to valid names |
|
92
|
|
|
array('a/b*', 'b/b', array('a' => true, 'a/b*' => true, 'b' => true, 'b/b' => false), array('a/b*' => true)), |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param $source |
|
98
|
|
|
* @param $destination |
|
99
|
|
|
* @param $updatables |
|
100
|
|
|
*/ |
|
101
|
|
|
private function moveTest($source, $destination, $updatables, $deletables) { |
|
102
|
|
|
$view = new TestDoubleFileView($updatables, $deletables); |
|
103
|
|
|
|
|
104
|
|
|
$info = new FileInfo('', null, null, array(), null); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$rootDir = new Directory($view, $info); |
|
107
|
|
|
$objectTree = $this->getMock('\OC\Connector\Sabre\ObjectTree', |
|
108
|
|
|
array('nodeExists', 'getNodeForPath'), |
|
109
|
|
|
array($rootDir, $view)); |
|
110
|
|
|
|
|
111
|
|
|
$objectTree->expects($this->once()) |
|
112
|
|
|
->method('getNodeForPath') |
|
113
|
|
|
->with($this->identicalTo($source)) |
|
114
|
|
|
->will($this->returnValue(false)); |
|
115
|
|
|
|
|
116
|
|
|
/** @var $objectTree \OC\Connector\Sabre\ObjectTree */ |
|
117
|
|
|
$mountManager = \OC\Files\Filesystem::getMountManager(); |
|
118
|
|
|
$objectTree->init($rootDir, $view, $mountManager); |
|
119
|
|
|
$objectTree->move($source, $destination); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function copyDataProvider() { |
|
123
|
|
|
return [ |
|
124
|
|
|
// copy into same dir |
|
125
|
|
|
['a', 'b', ''], |
|
126
|
|
|
// copy into same dir |
|
127
|
|
|
['a/a', 'a/b', 'a'], |
|
128
|
|
|
// copy into another dir |
|
129
|
|
|
['a', 'sub/a', 'sub'], |
|
130
|
|
|
]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @dataProvider copyDataProvider |
|
135
|
|
|
*/ |
|
136
|
|
|
public function testCopy($sourcePath, $targetPath, $targetParent) { |
|
137
|
|
|
$view = $this->getMock('\OC\Files\View'); |
|
138
|
|
|
$view->expects($this->once()) |
|
139
|
|
|
->method('verifyPath') |
|
140
|
|
|
->with($targetParent) |
|
141
|
|
|
->will($this->returnValue(true)); |
|
142
|
|
|
$view->expects($this->once()) |
|
143
|
|
|
->method('isCreatable') |
|
144
|
|
|
->with($targetParent) |
|
145
|
|
|
->will($this->returnValue(true)); |
|
146
|
|
|
$view->expects($this->once()) |
|
147
|
|
|
->method('copy') |
|
148
|
|
|
->with($sourcePath, $targetPath) |
|
149
|
|
|
->will($this->returnValue(true)); |
|
150
|
|
|
|
|
151
|
|
|
$info = new FileInfo('', null, null, array(), null); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
$rootDir = new \OC\Connector\Sabre\Directory($view, $info); |
|
154
|
|
|
$objectTree = $this->getMock('\OC\Connector\Sabre\ObjectTree', |
|
155
|
|
|
array('nodeExists', 'getNodeForPath'), |
|
156
|
|
|
array($rootDir, $view)); |
|
157
|
|
|
|
|
158
|
|
|
$objectTree->expects($this->once()) |
|
159
|
|
|
->method('getNodeForPath') |
|
160
|
|
|
->with($this->identicalTo($sourcePath)) |
|
161
|
|
|
->will($this->returnValue(false)); |
|
162
|
|
|
|
|
163
|
|
|
/** @var $objectTree \OC\Connector\Sabre\ObjectTree */ |
|
164
|
|
|
$mountManager = \OC\Files\Filesystem::getMountManager(); |
|
165
|
|
|
$objectTree->init($rootDir, $view, $mountManager); |
|
166
|
|
|
$objectTree->copy($sourcePath, $targetPath); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @dataProvider copyDataProvider |
|
171
|
|
|
* @expectedException \Sabre\DAV\Exception\Forbidden |
|
172
|
|
|
*/ |
|
173
|
|
|
public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent) { |
|
174
|
|
|
$view = $this->getMock('\OC\Files\View'); |
|
175
|
|
|
$view->expects($this->once()) |
|
176
|
|
|
->method('verifyPath') |
|
177
|
|
|
->with($targetParent) |
|
178
|
|
|
->will($this->returnValue(true)); |
|
179
|
|
|
$view->expects($this->once()) |
|
180
|
|
|
->method('isCreatable') |
|
181
|
|
|
->with($targetParent) |
|
182
|
|
|
->will($this->returnValue(false)); |
|
183
|
|
|
$view->expects($this->never()) |
|
184
|
|
|
->method('copy'); |
|
185
|
|
|
|
|
186
|
|
|
$info = new FileInfo('', null, null, array(), null); |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
$rootDir = new \OC\Connector\Sabre\Directory($view, $info); |
|
189
|
|
|
$objectTree = $this->getMock('\OC\Connector\Sabre\ObjectTree', |
|
190
|
|
|
array('nodeExists', 'getNodeForPath'), |
|
191
|
|
|
array($rootDir, $view)); |
|
192
|
|
|
|
|
193
|
|
|
$objectTree->expects($this->once()) |
|
194
|
|
|
->method('getNodeForPath') |
|
195
|
|
|
->with($this->identicalTo($sourcePath)) |
|
196
|
|
|
->will($this->returnValue(false)); |
|
197
|
|
|
|
|
198
|
|
|
/** @var $objectTree \OC\Connector\Sabre\ObjectTree */ |
|
199
|
|
|
$mountManager = \OC\Files\Filesystem::getMountManager(); |
|
200
|
|
|
$objectTree->init($rootDir, $view, $mountManager); |
|
201
|
|
|
$objectTree->copy($sourcePath, $targetPath); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @dataProvider nodeForPathProvider |
|
206
|
|
|
*/ |
|
207
|
|
|
public function testGetNodeForPath( |
|
208
|
|
|
$inputFileName, |
|
209
|
|
|
$fileInfoQueryPath, |
|
210
|
|
|
$outputFileName, |
|
211
|
|
|
$type, |
|
212
|
|
|
$enableChunkingHeader |
|
213
|
|
|
) { |
|
214
|
|
|
|
|
215
|
|
|
if ($enableChunkingHeader) { |
|
216
|
|
|
$_SERVER['HTTP_OC_CHUNKED'] = true; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$rootNode = $this->getMockBuilder('\OC\Connector\Sabre\Directory') |
|
220
|
|
|
->disableOriginalConstructor() |
|
221
|
|
|
->getMock(); |
|
222
|
|
|
$mountManager = $this->getMock('\OC\Files\Mount\Manager'); |
|
223
|
|
|
$view = $this->getMock('\OC\Files\View'); |
|
224
|
|
|
$fileInfo = $this->getMock('\OCP\Files\FileInfo'); |
|
225
|
|
|
$fileInfo->expects($this->once()) |
|
226
|
|
|
->method('getType') |
|
227
|
|
|
->will($this->returnValue($type)); |
|
228
|
|
|
$fileInfo->expects($this->once()) |
|
229
|
|
|
->method('getName') |
|
230
|
|
|
->will($this->returnValue($outputFileName)); |
|
231
|
|
|
|
|
232
|
|
|
$view->expects($this->once()) |
|
233
|
|
|
->method('getFileInfo') |
|
234
|
|
|
->with($fileInfoQueryPath) |
|
235
|
|
|
->will($this->returnValue($fileInfo)); |
|
236
|
|
|
|
|
237
|
|
|
$tree = new \OC\Connector\Sabre\ObjectTree(); |
|
238
|
|
|
$tree->init($rootNode, $view, $mountManager); |
|
239
|
|
|
|
|
240
|
|
|
$node = $tree->getNodeForPath($inputFileName); |
|
241
|
|
|
|
|
242
|
|
|
$this->assertNotNull($node); |
|
243
|
|
|
$this->assertEquals($outputFileName, $node->getName()); |
|
244
|
|
|
|
|
245
|
|
|
if ($type === 'file') { |
|
246
|
|
|
$this->assertTrue($node instanceof \OC\Connector\Sabre\File); |
|
247
|
|
|
} else { |
|
248
|
|
|
$this->assertTrue($node instanceof \OC\Connector\Sabre\Directory); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
unset($_SERVER['HTTP_OC_CHUNKED']); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
function nodeForPathProvider() { |
|
255
|
|
|
return array( |
|
256
|
|
|
// regular file |
|
257
|
|
|
array( |
|
258
|
|
|
'regularfile.txt', |
|
259
|
|
|
'regularfile.txt', |
|
260
|
|
|
'regularfile.txt', |
|
261
|
|
|
'file', |
|
262
|
|
|
false |
|
263
|
|
|
), |
|
264
|
|
|
// regular directory |
|
265
|
|
|
array( |
|
266
|
|
|
'regulardir', |
|
267
|
|
|
'regulardir', |
|
268
|
|
|
'regulardir', |
|
269
|
|
|
'dir', |
|
270
|
|
|
false |
|
271
|
|
|
), |
|
272
|
|
|
// regular file with chunking |
|
273
|
|
|
array( |
|
274
|
|
|
'regularfile.txt', |
|
275
|
|
|
'regularfile.txt', |
|
276
|
|
|
'regularfile.txt', |
|
277
|
|
|
'file', |
|
278
|
|
|
true |
|
279
|
|
|
), |
|
280
|
|
|
// regular directory with chunking |
|
281
|
|
|
array( |
|
282
|
|
|
'regulardir', |
|
283
|
|
|
'regulardir', |
|
284
|
|
|
'regulardir', |
|
285
|
|
|
'dir', |
|
286
|
|
|
true |
|
287
|
|
|
), |
|
288
|
|
|
// file with chunky file name |
|
289
|
|
|
array( |
|
290
|
|
|
'regularfile.txt-chunking-123566789-10-1', |
|
291
|
|
|
'regularfile.txt', |
|
292
|
|
|
'regularfile.txt', |
|
293
|
|
|
'file', |
|
294
|
|
|
true |
|
295
|
|
|
), |
|
296
|
|
|
// regular file in subdir |
|
297
|
|
|
array( |
|
298
|
|
|
'subdir/regularfile.txt', |
|
299
|
|
|
'subdir/regularfile.txt', |
|
300
|
|
|
'regularfile.txt', |
|
301
|
|
|
'file', |
|
302
|
|
|
false |
|
303
|
|
|
), |
|
304
|
|
|
// regular directory in subdir |
|
305
|
|
|
array( |
|
306
|
|
|
'subdir/regulardir', |
|
307
|
|
|
'subdir/regulardir', |
|
308
|
|
|
'regulardir', |
|
309
|
|
|
'dir', |
|
310
|
|
|
false |
|
311
|
|
|
), |
|
312
|
|
|
// file with chunky file name in subdir |
|
313
|
|
|
array( |
|
314
|
|
|
'subdir/regularfile.txt-chunking-123566789-10-1', |
|
315
|
|
|
'subdir/regularfile.txt', |
|
316
|
|
|
'regularfile.txt', |
|
317
|
|
|
'file', |
|
318
|
|
|
true |
|
319
|
|
|
), |
|
320
|
|
|
); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @expectedException \OC\Connector\Sabre\Exception\InvalidPath |
|
325
|
|
|
*/ |
|
326
|
|
View Code Duplication |
public function testGetNodeForPathInvalidPath() { |
|
327
|
|
|
$path = '/foo\bar'; |
|
328
|
|
|
|
|
329
|
|
|
|
|
330
|
|
|
$storage = new Temporary([]); |
|
331
|
|
|
|
|
332
|
|
|
$view = $this->getMock('\OC\Files\View', ['resolvePath']); |
|
333
|
|
|
$view->expects($this->once()) |
|
334
|
|
|
->method('resolvePath') |
|
335
|
|
|
->will($this->returnCallback(function($path) use ($storage){ |
|
336
|
|
|
return [$storage, ltrim($path, '/')]; |
|
337
|
|
|
})); |
|
338
|
|
|
|
|
339
|
|
|
$rootNode = $this->getMockBuilder('\OC\Connector\Sabre\Directory') |
|
340
|
|
|
->disableOriginalConstructor() |
|
341
|
|
|
->getMock(); |
|
342
|
|
|
$mountManager = $this->getMock('\OC\Files\Mount\Manager'); |
|
343
|
|
|
|
|
344
|
|
|
$tree = new \OC\Connector\Sabre\ObjectTree(); |
|
345
|
|
|
$tree->init($rootNode, $view, $mountManager); |
|
346
|
|
|
|
|
347
|
|
|
$tree->getNodeForPath($path); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
View Code Duplication |
public function testGetNodeForPathRoot() { |
|
351
|
|
|
$path = '/'; |
|
352
|
|
|
|
|
353
|
|
|
|
|
354
|
|
|
$storage = new Temporary([]); |
|
355
|
|
|
|
|
356
|
|
|
$view = $this->getMock('\OC\Files\View', ['resolvePath']); |
|
357
|
|
|
$view->expects($this->any()) |
|
358
|
|
|
->method('resolvePath') |
|
359
|
|
|
->will($this->returnCallback(function ($path) use ($storage) { |
|
360
|
|
|
return [$storage, ltrim($path, '/')]; |
|
361
|
|
|
})); |
|
362
|
|
|
|
|
363
|
|
|
$rootNode = $this->getMockBuilder('\OC\Connector\Sabre\Directory') |
|
364
|
|
|
->disableOriginalConstructor() |
|
365
|
|
|
->getMock(); |
|
366
|
|
|
$mountManager = $this->getMock('\OC\Files\Mount\Manager'); |
|
367
|
|
|
|
|
368
|
|
|
$tree = new \OC\Connector\Sabre\ObjectTree(); |
|
369
|
|
|
$tree->init($rootNode, $view, $mountManager); |
|
370
|
|
|
|
|
371
|
|
|
$this->assertInstanceOf('\Sabre\DAV\INode', $tree->getNodeForPath($path)); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* @expectedException \Sabre\DAV\Exception\Forbidden |
|
376
|
|
|
* @expectedExceptionMessage Could not copy directory nameOfSourceNode, target exists |
|
377
|
|
|
*/ |
|
378
|
|
|
public function testFailingMove() { |
|
379
|
|
|
$source = 'a/b'; |
|
380
|
|
|
$destination = 'b/b'; |
|
381
|
|
|
$updatables = array('a' => true, 'a/b' => true, 'b' => true, 'b/b' => false); |
|
382
|
|
|
$deletables = array('a/b' => true); |
|
383
|
|
|
|
|
384
|
|
|
$view = new TestDoubleFileView($updatables, $deletables); |
|
385
|
|
|
|
|
386
|
|
|
$info = new FileInfo('', null, null, array(), null); |
|
|
|
|
|
|
387
|
|
|
|
|
388
|
|
|
$rootDir = new Directory($view, $info); |
|
389
|
|
|
$objectTree = $this->getMock('\OC\Connector\Sabre\ObjectTree', |
|
390
|
|
|
array('nodeExists', 'getNodeForPath'), |
|
391
|
|
|
array($rootDir, $view)); |
|
392
|
|
|
|
|
393
|
|
|
$sourceNode = $this->getMockBuilder('\Sabre\DAV\ICollection') |
|
394
|
|
|
->disableOriginalConstructor() |
|
395
|
|
|
->getMock(); |
|
396
|
|
|
$sourceNode->expects($this->once()) |
|
397
|
|
|
->method('getName') |
|
398
|
|
|
->will($this->returnValue('nameOfSourceNode')); |
|
399
|
|
|
|
|
400
|
|
|
$objectTree->expects($this->once()) |
|
401
|
|
|
->method('nodeExists') |
|
402
|
|
|
->with($this->identicalTo($destination)) |
|
403
|
|
|
->will($this->returnValue(true)); |
|
404
|
|
|
$objectTree->expects($this->once()) |
|
405
|
|
|
->method('getNodeForPath') |
|
406
|
|
|
->with($this->identicalTo($source)) |
|
407
|
|
|
->will($this->returnValue($sourceNode)); |
|
408
|
|
|
|
|
409
|
|
|
/** @var $objectTree \OC\Connector\Sabre\ObjectTree */ |
|
410
|
|
|
$mountManager = \OC\Files\Filesystem::getMountManager(); |
|
411
|
|
|
$objectTree->init($rootDir, $view, $mountManager); |
|
412
|
|
|
$objectTree->move($source, $destination); |
|
413
|
|
|
} |
|
414
|
|
|
} |
|
415
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: