|
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\Files\Storage\Temporary; |
|
14
|
|
|
use PHPUnit_Framework_TestCase; |
|
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, $dest, $updatables, $deletables) { |
|
52
|
|
|
$this->moveTest($source, $dest, $updatables, $deletables); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @dataProvider moveSuccessProvider |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testMoveSuccess($source, $dest, $updatables, $deletables) { |
|
59
|
|
|
$this->moveTest($source, $dest, $updatables, $deletables); |
|
60
|
|
|
$this->assertTrue(true); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @dataProvider moveFailedInvalidCharsProvider |
|
65
|
|
|
* @expectedException \Sabre\DAV\Exception\BadRequest |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testMoveFailedInvalidChars($source, $dest, $updatables, $deletables) { |
|
68
|
|
|
$this->moveTest($source, $dest, $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 $dest |
|
99
|
|
|
* @param $updatables |
|
100
|
|
|
*/ |
|
101
|
|
|
private function moveTest($source, $dest, $updatables, $deletables) { |
|
102
|
|
|
$view = new TestDoubleFileView($updatables, $deletables); |
|
103
|
|
|
|
|
104
|
|
|
$info = new FileInfo('', null, null, array(), null); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$rootDir = new \OC_Connector_Sabre_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, $dest); |
|
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('is_file') |
|
140
|
|
|
->with($sourcePath) |
|
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->any()) |
|
176
|
|
|
->method('is_file') |
|
177
|
|
|
->with($sourcePath) |
|
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
|
|
|
|
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: