ObjectTreeTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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): void {
  59. $view = $this->createMock(View::class);
  60. $view->expects($this->once())
  61. ->method('verifyPath')
  62. ->with($targetParent);
  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): void {
  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. ): void {
  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->method('getType')
  153. ->willReturn($type);
  154. $fileInfo->method('getName')
  155. ->willReturn($outputFileName);
  156. $fileInfo->method('getStorage')
  157. ->willReturn($this->createMock(\OC\Files\Storage\Common::class));
  158. $view->method('getFileInfo')
  159. ->with($fileInfoQueryPath)
  160. ->willReturn($fileInfo);
  161. $tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  162. $tree->init($rootNode, $view, $mountManager);
  163. $node = $tree->getNodeForPath($inputFileName);
  164. $this->assertNotNull($node);
  165. $this->assertEquals($outputFileName, $node->getName());
  166. if ($type === 'file') {
  167. $this->assertTrue($node instanceof \OCA\DAV\Connector\Sabre\File);
  168. } else {
  169. $this->assertTrue($node instanceof \OCA\DAV\Connector\Sabre\Directory);
  170. }
  171. unset($_SERVER['HTTP_OC_CHUNKED']);
  172. }
  173. public function nodeForPathProvider() {
  174. return [
  175. // regular file
  176. [
  177. 'regularfile.txt',
  178. 'regularfile.txt',
  179. 'regularfile.txt',
  180. 'file',
  181. false
  182. ],
  183. // regular directory
  184. [
  185. 'regulardir',
  186. 'regulardir',
  187. 'regulardir',
  188. 'dir',
  189. false
  190. ],
  191. // regular file with chunking
  192. [
  193. 'regularfile.txt',
  194. 'regularfile.txt',
  195. 'regularfile.txt',
  196. 'file',
  197. true
  198. ],
  199. // regular directory with chunking
  200. [
  201. 'regulardir',
  202. 'regulardir',
  203. 'regulardir',
  204. 'dir',
  205. true
  206. ],
  207. // file with chunky file name
  208. [
  209. 'regularfile.txt-chunking-123566789-10-1',
  210. 'regularfile.txt',
  211. 'regularfile.txt',
  212. 'file',
  213. true
  214. ],
  215. // regular file in subdir
  216. [
  217. 'subdir/regularfile.txt',
  218. 'subdir/regularfile.txt',
  219. 'regularfile.txt',
  220. 'file',
  221. false
  222. ],
  223. // regular directory in subdir
  224. [
  225. 'subdir/regulardir',
  226. 'subdir/regulardir',
  227. 'regulardir',
  228. 'dir',
  229. false
  230. ],
  231. // file with chunky file name in subdir
  232. [
  233. 'subdir/regularfile.txt-chunking-123566789-10-1',
  234. 'subdir/regularfile.txt',
  235. 'regularfile.txt',
  236. 'file',
  237. true
  238. ],
  239. ];
  240. }
  241. public function testGetNodeForPathInvalidPath(): void {
  242. $this->expectException(\OCA\DAV\Connector\Sabre\Exception\InvalidPath::class);
  243. $path = '/foo\bar';
  244. $storage = new Temporary([]);
  245. $view = $this->getMockBuilder(View::class)
  246. ->setMethods(['resolvePath'])
  247. ->getMock();
  248. $view->expects($this->once())
  249. ->method('resolvePath')
  250. ->willReturnCallback(function ($path) use ($storage) {
  251. return [$storage, ltrim($path, '/')];
  252. });
  253. $rootNode = $this->getMockBuilder(Directory::class)
  254. ->disableOriginalConstructor()
  255. ->getMock();
  256. $mountManager = $this->createMock(IMountManager::class);
  257. $tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  258. $tree->init($rootNode, $view, $mountManager);
  259. $tree->getNodeForPath($path);
  260. }
  261. public function testGetNodeForPathRoot(): void {
  262. $path = '/';
  263. $storage = new Temporary([]);
  264. $view = $this->getMockBuilder(View::class)
  265. ->setMethods(['resolvePath'])
  266. ->getMock();
  267. $view->expects($this->any())
  268. ->method('resolvePath')
  269. ->willReturnCallback(function ($path) use ($storage) {
  270. return [$storage, ltrim($path, '/')];
  271. });
  272. $rootNode = $this->getMockBuilder(Directory::class)
  273. ->disableOriginalConstructor()
  274. ->getMock();
  275. $mountManager = $this->createMock(IMountManager::class);
  276. $tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  277. $tree->init($rootNode, $view, $mountManager);
  278. $this->assertInstanceOf('\Sabre\DAV\INode', $tree->getNodeForPath($path));
  279. }
  280. }