ObjectTreeTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  8. use OC\Files\FileInfo;
  9. use OC\Files\Filesystem;
  10. use OC\Files\Mount\Manager;
  11. use OC\Files\Storage\Common;
  12. use OC\Files\Storage\Temporary;
  13. use OC\Files\View;
  14. use OCA\DAV\Connector\Sabre\Directory;
  15. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  16. use OCA\DAV\Connector\Sabre\File;
  17. use OCA\DAV\Connector\Sabre\ObjectTree;
  18. use OCP\Files\Mount\IMountManager;
  19. /**
  20. * Class ObjectTreeTest
  21. *
  22. * @group DB
  23. *
  24. * @package OCA\DAV\Tests\Unit\Connector\Sabre
  25. */
  26. class ObjectTreeTest extends \Test\TestCase {
  27. public function copyDataProvider() {
  28. return [
  29. // copy into same dir
  30. ['a', 'b', ''],
  31. // copy into same dir
  32. ['a/a', 'a/b', 'a'],
  33. // copy into another dir
  34. ['a', 'sub/a', 'sub'],
  35. ];
  36. }
  37. /**
  38. * @dataProvider copyDataProvider
  39. */
  40. public function testCopy($sourcePath, $targetPath, $targetParent): void {
  41. $view = $this->createMock(View::class);
  42. $view->expects($this->once())
  43. ->method('verifyPath')
  44. ->with($targetParent);
  45. $view->expects($this->once())
  46. ->method('file_exists')
  47. ->with($targetPath)
  48. ->willReturn(false);
  49. $view->expects($this->once())
  50. ->method('copy')
  51. ->with($sourcePath, $targetPath)
  52. ->willReturn(true);
  53. $info = $this->createMock(FileInfo::class);
  54. $info->expects($this->once())
  55. ->method('isCreatable')
  56. ->willReturn(true);
  57. $view->expects($this->once())
  58. ->method('getFileInfo')
  59. ->with($targetParent === '' ? '.' : $targetParent)
  60. ->willReturn($info);
  61. $rootDir = new Directory($view, $info);
  62. $objectTree = $this->getMockBuilder(ObjectTree::class)
  63. ->setMethods(['nodeExists', 'getNodeForPath'])
  64. ->setConstructorArgs([$rootDir, $view])
  65. ->getMock();
  66. $objectTree->expects($this->once())
  67. ->method('getNodeForPath')
  68. ->with($this->identicalTo($sourcePath))
  69. ->willReturn(false);
  70. /** @var ObjectTree $objectTree */
  71. $mountManager = Filesystem::getMountManager();
  72. $objectTree->init($rootDir, $view, $mountManager);
  73. $objectTree->copy($sourcePath, $targetPath);
  74. }
  75. /**
  76. * @dataProvider copyDataProvider
  77. */
  78. public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent): void {
  79. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  80. $view = $this->createMock(View::class);
  81. $view->expects($this->never())
  82. ->method('verifyPath');
  83. $view->expects($this->once())
  84. ->method('file_exists')
  85. ->with($targetPath)
  86. ->willReturn(false);
  87. $view->expects($this->never())
  88. ->method('copy');
  89. $info = $this->createMock(FileInfo::class);
  90. $info->expects($this->once())
  91. ->method('isCreatable')
  92. ->willReturn(false);
  93. $view->expects($this->once())
  94. ->method('getFileInfo')
  95. ->with($targetParent === '' ? '.' : $targetParent)
  96. ->willReturn($info);
  97. $rootDir = new Directory($view, $info);
  98. $objectTree = $this->getMockBuilder(ObjectTree::class)
  99. ->setMethods(['nodeExists', 'getNodeForPath'])
  100. ->setConstructorArgs([$rootDir, $view])
  101. ->getMock();
  102. $objectTree->expects($this->never())
  103. ->method('getNodeForPath');
  104. /** @var ObjectTree $objectTree */
  105. $mountManager = Filesystem::getMountManager();
  106. $objectTree->init($rootDir, $view, $mountManager);
  107. $objectTree->copy($sourcePath, $targetPath);
  108. }
  109. /**
  110. * @dataProvider nodeForPathProvider
  111. */
  112. public function testGetNodeForPath(
  113. $inputFileName,
  114. $fileInfoQueryPath,
  115. $outputFileName,
  116. $type,
  117. ): void {
  118. $rootNode = $this->getMockBuilder(Directory::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $mountManager = $this->getMockBuilder(Manager::class)
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $view = $this->getMockBuilder(View::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $fileInfo = $this->getMockBuilder(FileInfo::class)
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. $fileInfo->method('getType')
  131. ->willReturn($type);
  132. $fileInfo->method('getName')
  133. ->willReturn($outputFileName);
  134. $fileInfo->method('getStorage')
  135. ->willReturn($this->createMock(Common::class));
  136. $view->method('getFileInfo')
  137. ->with($fileInfoQueryPath)
  138. ->willReturn($fileInfo);
  139. $tree = new ObjectTree();
  140. $tree->init($rootNode, $view, $mountManager);
  141. $node = $tree->getNodeForPath($inputFileName);
  142. $this->assertNotNull($node);
  143. $this->assertEquals($outputFileName, $node->getName());
  144. if ($type === 'file') {
  145. $this->assertTrue($node instanceof File);
  146. } else {
  147. $this->assertTrue($node instanceof Directory);
  148. }
  149. }
  150. public function nodeForPathProvider() {
  151. return [
  152. // regular file
  153. [
  154. 'regularfile.txt',
  155. 'regularfile.txt',
  156. 'regularfile.txt',
  157. 'file',
  158. ],
  159. // regular directory
  160. [
  161. 'regulardir',
  162. 'regulardir',
  163. 'regulardir',
  164. 'dir',
  165. ],
  166. // regular file in subdir
  167. [
  168. 'subdir/regularfile.txt',
  169. 'subdir/regularfile.txt',
  170. 'regularfile.txt',
  171. 'file',
  172. ],
  173. // regular directory in subdir
  174. [
  175. 'subdir/regulardir',
  176. 'subdir/regulardir',
  177. 'regulardir',
  178. 'dir',
  179. ],
  180. ];
  181. }
  182. public function testGetNodeForPathInvalidPath(): void {
  183. $this->expectException(InvalidPath::class);
  184. $path = '/foo\bar';
  185. $storage = new Temporary([]);
  186. $view = $this->getMockBuilder(View::class)
  187. ->setMethods(['resolvePath'])
  188. ->getMock();
  189. $view->expects($this->once())
  190. ->method('resolvePath')
  191. ->willReturnCallback(function ($path) use ($storage) {
  192. return [$storage, ltrim($path, '/')];
  193. });
  194. $rootNode = $this->getMockBuilder(Directory::class)
  195. ->disableOriginalConstructor()
  196. ->getMock();
  197. $mountManager = $this->createMock(IMountManager::class);
  198. $tree = new ObjectTree();
  199. $tree->init($rootNode, $view, $mountManager);
  200. $tree->getNodeForPath($path);
  201. }
  202. public function testGetNodeForPathRoot(): void {
  203. $path = '/';
  204. $storage = new Temporary([]);
  205. $view = $this->getMockBuilder(View::class)
  206. ->setMethods(['resolvePath'])
  207. ->getMock();
  208. $view->expects($this->any())
  209. ->method('resolvePath')
  210. ->willReturnCallback(function ($path) use ($storage) {
  211. return [$storage, ltrim($path, '/')];
  212. });
  213. $rootNode = $this->getMockBuilder(Directory::class)
  214. ->disableOriginalConstructor()
  215. ->getMock();
  216. $mountManager = $this->createMock(IMountManager::class);
  217. $tree = new ObjectTree();
  218. $tree->init($rootNode, $view, $mountManager);
  219. $this->assertInstanceOf('\Sabre\DAV\INode', $tree->getNodeForPath($path));
  220. }
  221. }