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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. use OCP\Files\Mount\IMountManager;
  37. /**
  38. * Class ObjectTreeTest
  39. *
  40. * @group DB
  41. *
  42. * @package OCA\DAV\Tests\Unit\Connector\Sabre
  43. */
  44. class ObjectTreeTest extends \Test\TestCase {
  45. public function copyDataProvider() {
  46. return [
  47. // copy into same dir
  48. ['a', 'b', ''],
  49. // copy into same dir
  50. ['a/a', 'a/b', 'a'],
  51. // copy into another dir
  52. ['a', 'sub/a', 'sub'],
  53. ];
  54. }
  55. /**
  56. * @dataProvider copyDataProvider
  57. */
  58. public function testCopy($sourcePath, $targetPath, $targetParent) {
  59. $view = $this->createMock(View::class);
  60. $view->expects($this->once())
  61. ->method('verifyPath')
  62. ->with($targetParent)
  63. ->willReturn(true);
  64. $view->expects($this->once())
  65. ->method('file_exists')
  66. ->with($targetPath)
  67. ->willReturn(false);
  68. $view->expects($this->once())
  69. ->method('copy')
  70. ->with($sourcePath, $targetPath)
  71. ->willReturn(true);
  72. $info = $this->createMock(FileInfo::class);
  73. $info->expects($this->once())
  74. ->method('isCreatable')
  75. ->willReturn(true);
  76. $view->expects($this->once())
  77. ->method('getFileInfo')
  78. ->with($targetParent === '' ? '.' : $targetParent)
  79. ->willReturn($info);
  80. $rootDir = new Directory($view, $info);
  81. $objectTree = $this->getMockBuilder(ObjectTree::class)
  82. ->setMethods(['nodeExists', 'getNodeForPath'])
  83. ->setConstructorArgs([$rootDir, $view])
  84. ->getMock();
  85. $objectTree->expects($this->once())
  86. ->method('getNodeForPath')
  87. ->with($this->identicalTo($sourcePath))
  88. ->willReturn(false);
  89. /** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */
  90. $mountManager = Filesystem::getMountManager();
  91. $objectTree->init($rootDir, $view, $mountManager);
  92. $objectTree->copy($sourcePath, $targetPath);
  93. }
  94. /**
  95. * @dataProvider copyDataProvider
  96. */
  97. public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent) {
  98. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  99. $view = $this->createMock(View::class);
  100. $view->expects($this->never())
  101. ->method('verifyPath');
  102. $view->expects($this->once())
  103. ->method('file_exists')
  104. ->with($targetPath)
  105. ->willReturn(false);
  106. $view->expects($this->never())
  107. ->method('copy');
  108. $info = $this->createMock(FileInfo::class);
  109. $info->expects($this->once())
  110. ->method('isCreatable')
  111. ->willReturn(false);
  112. $view->expects($this->once())
  113. ->method('getFileInfo')
  114. ->with($targetParent === '' ? '.' : $targetParent)
  115. ->willReturn($info);
  116. $rootDir = new Directory($view, $info);
  117. $objectTree = $this->getMockBuilder(ObjectTree::class)
  118. ->setMethods(['nodeExists', 'getNodeForPath'])
  119. ->setConstructorArgs([$rootDir, $view])
  120. ->getMock();
  121. $objectTree->expects($this->never())
  122. ->method('getNodeForPath');
  123. /** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */
  124. $mountManager = Filesystem::getMountManager();
  125. $objectTree->init($rootDir, $view, $mountManager);
  126. $objectTree->copy($sourcePath, $targetPath);
  127. }
  128. /**
  129. * @dataProvider nodeForPathProvider
  130. */
  131. public function testGetNodeForPath(
  132. $inputFileName,
  133. $fileInfoQueryPath,
  134. $outputFileName,
  135. $type,
  136. $enableChunkingHeader
  137. ) {
  138. if ($enableChunkingHeader) {
  139. $_SERVER['HTTP_OC_CHUNKED'] = true;
  140. }
  141. $rootNode = $this->getMockBuilder(Directory::class)
  142. ->disableOriginalConstructor()
  143. ->getMock();
  144. $mountManager = $this->getMockBuilder(Manager::class)
  145. ->disableOriginalConstructor()
  146. ->getMock();
  147. $view = $this->getMockBuilder(View::class)
  148. ->disableOriginalConstructor()
  149. ->getMock();
  150. $fileInfo = $this->getMockBuilder(FileInfo::class)
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. $fileInfo->method('getType')
  154. ->willReturn($type);
  155. $fileInfo->method('getName')
  156. ->willReturn($outputFileName);
  157. $fileInfo->method('getStorage')
  158. ->willReturn($this->createMock(\OC\Files\Storage\Common::class));
  159. $view->method('getFileInfo')
  160. ->with($fileInfoQueryPath)
  161. ->willReturn($fileInfo);
  162. $tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  163. $tree->init($rootNode, $view, $mountManager);
  164. $node = $tree->getNodeForPath($inputFileName);
  165. $this->assertNotNull($node);
  166. $this->assertEquals($outputFileName, $node->getName());
  167. if ($type === 'file') {
  168. $this->assertTrue($node instanceof \OCA\DAV\Connector\Sabre\File);
  169. } else {
  170. $this->assertTrue($node instanceof \OCA\DAV\Connector\Sabre\Directory);
  171. }
  172. unset($_SERVER['HTTP_OC_CHUNKED']);
  173. }
  174. public function nodeForPathProvider() {
  175. return [
  176. // regular file
  177. [
  178. 'regularfile.txt',
  179. 'regularfile.txt',
  180. 'regularfile.txt',
  181. 'file',
  182. false
  183. ],
  184. // regular directory
  185. [
  186. 'regulardir',
  187. 'regulardir',
  188. 'regulardir',
  189. 'dir',
  190. false
  191. ],
  192. // regular file with chunking
  193. [
  194. 'regularfile.txt',
  195. 'regularfile.txt',
  196. 'regularfile.txt',
  197. 'file',
  198. true
  199. ],
  200. // regular directory with chunking
  201. [
  202. 'regulardir',
  203. 'regulardir',
  204. 'regulardir',
  205. 'dir',
  206. true
  207. ],
  208. // file with chunky file name
  209. [
  210. 'regularfile.txt-chunking-123566789-10-1',
  211. 'regularfile.txt',
  212. 'regularfile.txt',
  213. 'file',
  214. true
  215. ],
  216. // regular file in subdir
  217. [
  218. 'subdir/regularfile.txt',
  219. 'subdir/regularfile.txt',
  220. 'regularfile.txt',
  221. 'file',
  222. false
  223. ],
  224. // regular directory in subdir
  225. [
  226. 'subdir/regulardir',
  227. 'subdir/regulardir',
  228. 'regulardir',
  229. 'dir',
  230. false
  231. ],
  232. // file with chunky file name in subdir
  233. [
  234. 'subdir/regularfile.txt-chunking-123566789-10-1',
  235. 'subdir/regularfile.txt',
  236. 'regularfile.txt',
  237. 'file',
  238. true
  239. ],
  240. ];
  241. }
  242. public function testGetNodeForPathInvalidPath() {
  243. $this->expectException(\OCA\DAV\Connector\Sabre\Exception\InvalidPath::class);
  244. $path = '/foo\bar';
  245. $storage = new Temporary([]);
  246. $view = $this->getMockBuilder(View::class)
  247. ->setMethods(['resolvePath'])
  248. ->getMock();
  249. $view->expects($this->once())
  250. ->method('resolvePath')
  251. ->willReturnCallback(function ($path) use ($storage) {
  252. return [$storage, ltrim($path, '/')];
  253. });
  254. $rootNode = $this->getMockBuilder(Directory::class)
  255. ->disableOriginalConstructor()
  256. ->getMock();
  257. $mountManager = $this->createMock(IMountManager::class);
  258. $tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  259. $tree->init($rootNode, $view, $mountManager);
  260. $tree->getNodeForPath($path);
  261. }
  262. public function testGetNodeForPathRoot() {
  263. $path = '/';
  264. $storage = new Temporary([]);
  265. $view = $this->getMockBuilder(View::class)
  266. ->setMethods(['resolvePath'])
  267. ->getMock();
  268. $view->expects($this->any())
  269. ->method('resolvePath')
  270. ->willReturnCallback(function ($path) use ($storage) {
  271. return [$storage, ltrim($path, '/')];
  272. });
  273. $rootNode = $this->getMockBuilder(Directory::class)
  274. ->disableOriginalConstructor()
  275. ->getMock();
  276. $mountManager = $this->createMock(IMountManager::class);
  277. $tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  278. $tree->init($rootNode, $view, $mountManager);
  279. $this->assertInstanceOf('\Sabre\DAV\INode', $tree->getNodeForPath($path));
  280. }
  281. }