ObjectTreeTest.php 8.1 KB

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