You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectTreeTest.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  29. use OC\Files\FileInfo;
  30. use OC\Files\Filesystem;
  31. use OC\Files\Mount\Manager;
  32. use OC\Files\Storage\Temporary;
  33. use OC\Files\View;
  34. use OCA\DAV\Connector\Sabre\Directory;
  35. use OCA\DAV\Connector\Sabre\ObjectTree;
  36. /**
  37. * Class ObjectTreeTest
  38. *
  39. * @group DB
  40. *
  41. * @package OCA\DAV\Tests\Unit\Connector\Sabre
  42. */
  43. class ObjectTreeTest extends \Test\TestCase {
  44. public function copyDataProvider() {
  45. return [
  46. // copy into same dir
  47. ['a', 'b', ''],
  48. // copy into same dir
  49. ['a/a', 'a/b', 'a'],
  50. // copy into another dir
  51. ['a', 'sub/a', 'sub'],
  52. ];
  53. }
  54. /**
  55. * @dataProvider copyDataProvider
  56. */
  57. public function testCopy($sourcePath, $targetPath, $targetParent) {
  58. $view = $this->createMock(View::class);
  59. $view->expects($this->once())
  60. ->method('verifyPath')
  61. ->with($targetParent)
  62. ->willReturn(true);
  63. $view->expects($this->once())
  64. ->method('file_exists')
  65. ->with($targetPath)
  66. ->willReturn(false);
  67. $view->expects($this->once())
  68. ->method('copy')
  69. ->with($sourcePath, $targetPath)
  70. ->willReturn(true);
  71. $info = $this->createMock(FileInfo::class);
  72. $info->expects($this->once())
  73. ->method('isCreatable')
  74. ->willReturn(true);
  75. $view->expects($this->once())
  76. ->method('getFileInfo')
  77. ->with($targetParent === '' ? '.' : $targetParent)
  78. ->willReturn($info);
  79. $rootDir = new Directory($view, $info);
  80. $objectTree = $this->getMockBuilder(ObjectTree::class)
  81. ->setMethods(['nodeExists', 'getNodeForPath'])
  82. ->setConstructorArgs([$rootDir, $view])
  83. ->getMock();
  84. $objectTree->expects($this->once())
  85. ->method('getNodeForPath')
  86. ->with($this->identicalTo($sourcePath))
  87. ->willReturn(false);
  88. /** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */
  89. $mountManager = Filesystem::getMountManager();
  90. $objectTree->init($rootDir, $view, $mountManager);
  91. $objectTree->copy($sourcePath, $targetPath);
  92. }
  93. /**
  94. * @dataProvider copyDataProvider
  95. */
  96. public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent) {
  97. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  98. $view = $this->createMock(View::class);
  99. $view->expects($this->never())
  100. ->method('verifyPath');
  101. $view->expects($this->once())
  102. ->method('file_exists')
  103. ->with($targetPath)
  104. ->willReturn(false);
  105. $view->expects($this->never())
  106. ->method('copy');
  107. $info = $this->createMock(FileInfo::class);
  108. $info->expects($this->once())
  109. ->method('isCreatable')
  110. ->willReturn(false);
  111. $view->expects($this->once())
  112. ->method('getFileInfo')
  113. ->with($targetParent === '' ? '.' : $targetParent)
  114. ->willReturn($info);
  115. $rootDir = new Directory($view, $info);
  116. $objectTree = $this->getMockBuilder(ObjectTree::class)
  117. ->setMethods(['nodeExists', 'getNodeForPath'])
  118. ->setConstructorArgs([$rootDir, $view])
  119. ->getMock();
  120. $objectTree->expects($this->never())
  121. ->method('getNodeForPath');
  122. /** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */
  123. $mountManager = Filesystem::getMountManager();
  124. $objectTree->init($rootDir, $view, $mountManager);
  125. $objectTree->copy($sourcePath, $targetPath);
  126. }
  127. /**
  128. * @dataProvider nodeForPathProvider
  129. */
  130. public function testGetNodeForPath(
  131. $inputFileName,
  132. $fileInfoQueryPath,
  133. $outputFileName,
  134. $type,
  135. $enableChunkingHeader
  136. ) {
  137. if ($enableChunkingHeader) {
  138. $_SERVER['HTTP_OC_CHUNKED'] = true;
  139. }
  140. $rootNode = $this->getMockBuilder(Directory::class)
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $mountManager = $this->getMockBuilder(Manager::class)
  144. ->disableOriginalConstructor()
  145. ->getMock();
  146. $view = $this->getMockBuilder(View::class)
  147. ->disableOriginalConstructor()
  148. ->getMock();
  149. $fileInfo = $this->getMockBuilder(FileInfo::class)
  150. ->disableOriginalConstructor()
  151. ->getMock();
  152. $fileInfo->expects($this->once())
  153. ->method('getType')
  154. ->willReturn($type);
  155. $fileInfo->expects($this->once())
  156. ->method('getName')
  157. ->willReturn($outputFileName);
  158. $fileInfo->method('getStorage')
  159. ->willReturn($this->createMock(\OC\Files\Storage\Common::class));
  160. $view->expects($this->once())
  161. ->method('getFileInfo')
  162. ->with($fileInfoQueryPath)
  163. ->willReturn($fileInfo);
  164. $tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  165. $tree->init($rootNode, $view, $mountManager);
  166. $node = $tree->getNodeForPath($inputFileName);
  167. $this->assertNotNull($node);
  168. $this->assertEquals($outputFileName, $node->getName());
  169. if ($type === 'file') {
  170. $this->assertTrue($node instanceof \OCA\DAV\Connector\Sabre\File);
  171. } else {
  172. $this->assertTrue($node instanceof \OCA\DAV\Connector\Sabre\Directory);
  173. }
  174. unset($_SERVER['HTTP_OC_CHUNKED']);
  175. }
  176. public function nodeForPathProvider() {
  177. return [
  178. // regular file
  179. [
  180. 'regularfile.txt',
  181. 'regularfile.txt',
  182. 'regularfile.txt',
  183. 'file',
  184. false
  185. ],
  186. // regular directory
  187. [
  188. 'regulardir',
  189. 'regulardir',
  190. 'regulardir',
  191. 'dir',
  192. false
  193. ],
  194. // regular file with chunking
  195. [
  196. 'regularfile.txt',
  197. 'regularfile.txt',
  198. 'regularfile.txt',
  199. 'file',
  200. true
  201. ],
  202. // regular directory with chunking
  203. [
  204. 'regulardir',
  205. 'regulardir',
  206. 'regulardir',
  207. 'dir',
  208. true
  209. ],
  210. // file with chunky file name
  211. [
  212. 'regularfile.txt-chunking-123566789-10-1',
  213. 'regularfile.txt',
  214. 'regularfile.txt',
  215. 'file',
  216. true
  217. ],
  218. // regular file in subdir
  219. [
  220. 'subdir/regularfile.txt',
  221. 'subdir/regularfile.txt',
  222. 'regularfile.txt',
  223. 'file',
  224. false
  225. ],
  226. // regular directory in subdir
  227. [
  228. 'subdir/regulardir',
  229. 'subdir/regulardir',
  230. 'regulardir',
  231. 'dir',
  232. false
  233. ],
  234. // file with chunky file name in subdir
  235. [
  236. 'subdir/regularfile.txt-chunking-123566789-10-1',
  237. 'subdir/regularfile.txt',
  238. 'regularfile.txt',
  239. 'file',
  240. true
  241. ],
  242. ];
  243. }
  244. public function testGetNodeForPathInvalidPath() {
  245. $this->expectException(\OCA\DAV\Connector\Sabre\Exception\InvalidPath::class);
  246. $path = '/foo\bar';
  247. $storage = new Temporary([]);
  248. $view = $this->getMockBuilder(View::class)
  249. ->setMethods(['resolvePath'])
  250. ->getMock();
  251. $view->expects($this->once())
  252. ->method('resolvePath')
  253. ->willReturnCallback(function ($path) use ($storage) {
  254. return [$storage, ltrim($path, '/')];
  255. });
  256. $rootNode = $this->getMockBuilder(Directory::class)
  257. ->disableOriginalConstructor()
  258. ->getMock();
  259. $mountManager = $this->getMockBuilder(Manager::class)
  260. ->getMock();
  261. $tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  262. $tree->init($rootNode, $view, $mountManager);
  263. $tree->getNodeForPath($path);
  264. }
  265. public function testGetNodeForPathRoot() {
  266. $path = '/';
  267. $storage = new Temporary([]);
  268. $view = $this->getMockBuilder(View::class)
  269. ->setMethods(['resolvePath'])
  270. ->getMock();
  271. $view->expects($this->any())
  272. ->method('resolvePath')
  273. ->willReturnCallback(function ($path) use ($storage) {
  274. return [$storage, ltrim($path, '/')];
  275. });
  276. $rootNode = $this->getMockBuilder(Directory::class)
  277. ->disableOriginalConstructor()
  278. ->getMock();
  279. $mountManager = $this->getMockBuilder(Manager::class)
  280. ->getMock();
  281. $tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  282. $tree->init($rootNode, $view, $mountManager);
  283. $this->assertInstanceOf('\Sabre\DAV\INode', $tree->getNodeForPath($path));
  284. }
  285. }