Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

folder.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Files\Node;
  9. use OC\Files\Cache\Cache;
  10. use OC\Files\Node\Node;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\NotPermittedException;
  13. use OC\Files\View;
  14. class Folder extends \PHPUnit_Framework_TestCase {
  15. private $user;
  16. public function setUp() {
  17. $this->user = new \OC\User\User('', new \OC_User_Dummy);
  18. }
  19. public function testDelete() {
  20. $manager = $this->getMock('\OC\Files\Mount\Manager');
  21. /**
  22. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  23. */
  24. $view = $this->getMock('\OC\Files\View');
  25. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  26. $root->expects($this->any())
  27. ->method('getUser')
  28. ->will($this->returnValue($this->user));
  29. $root->expects($this->exactly(2))
  30. ->method('emit')
  31. ->will($this->returnValue(true));
  32. $view->expects($this->any())
  33. ->method('getFileInfo')
  34. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_ALL)));
  35. $view->expects($this->once())
  36. ->method('rmdir')
  37. ->with('/bar/foo')
  38. ->will($this->returnValue(true));
  39. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  40. $node->delete();
  41. }
  42. public function testDeleteHooks() {
  43. $test = $this;
  44. $hooksRun = 0;
  45. /**
  46. * @param \OC\Files\Node\File $node
  47. */
  48. $preListener = function ($node) use (&$test, &$hooksRun) {
  49. $test->assertInstanceOf('\OC\Files\Node\Folder', $node);
  50. $test->assertEquals('foo', $node->getInternalPath());
  51. $test->assertEquals('/bar/foo', $node->getPath());
  52. $hooksRun++;
  53. };
  54. /**
  55. * @param \OC\Files\Node\File $node
  56. */
  57. $postListener = function ($node) use (&$test, &$hooksRun) {
  58. $test->assertInstanceOf('\OC\Files\Node\NonExistingFolder', $node);
  59. $test->assertEquals('foo', $node->getInternalPath());
  60. $test->assertEquals('/bar/foo', $node->getPath());
  61. $hooksRun++;
  62. };
  63. /**
  64. * @var \OC\Files\Mount\Manager $manager
  65. */
  66. $manager = $this->getMock('\OC\Files\Mount\Manager');
  67. /**
  68. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  69. */
  70. $view = $this->getMock('\OC\Files\View');
  71. $root = new \OC\Files\Node\Root($manager, $view, $this->user);
  72. $root->listen('\OC\Files', 'preDelete', $preListener);
  73. $root->listen('\OC\Files', 'postDelete', $postListener);
  74. $view->expects($this->any())
  75. ->method('getFileInfo')
  76. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_ALL, 'fileid' => 1)));
  77. $view->expects($this->once())
  78. ->method('rmdir')
  79. ->with('/bar/foo')
  80. ->will($this->returnValue(true));
  81. $view->expects($this->any())
  82. ->method('resolvePath')
  83. ->with('/bar/foo')
  84. ->will($this->returnValue(array(null, 'foo')));
  85. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  86. $node->delete();
  87. $this->assertEquals(2, $hooksRun);
  88. }
  89. /**
  90. * @expectedException \OCP\Files\NotPermittedException
  91. */
  92. public function testDeleteNotPermitted() {
  93. $manager = $this->getMock('\OC\Files\Mount\Manager');
  94. /**
  95. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  96. */
  97. $view = $this->getMock('\OC\Files\View');
  98. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  99. $root->expects($this->any())
  100. ->method('getUser')
  101. ->will($this->returnValue($this->user));
  102. $view->expects($this->once())
  103. ->method('getFileInfo')
  104. ->with('/bar/foo')
  105. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_READ)));
  106. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  107. $node->delete();
  108. }
  109. public function testGetDirectoryContent() {
  110. $manager = $this->getMock('\OC\Files\Mount\Manager');
  111. /**
  112. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  113. */
  114. $view = $this->getMock('\OC\Files\View');
  115. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  116. $root->expects($this->any())
  117. ->method('getUser')
  118. ->will($this->returnValue($this->user));
  119. /**
  120. * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
  121. */
  122. $storage = $this->getMock('\OC\Files\Storage\Storage');
  123. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  124. $cache->expects($this->any())
  125. ->method('getStatus')
  126. ->with('foo')
  127. ->will($this->returnValue(Cache::COMPLETE));
  128. $cache->expects($this->once())
  129. ->method('getFolderContents')
  130. ->with('foo')
  131. ->will($this->returnValue(array(
  132. array('fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'),
  133. array('fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory')
  134. )));
  135. $root->expects($this->once())
  136. ->method('getMountsIn')
  137. ->with('/bar/foo')
  138. ->will($this->returnValue(array()));
  139. $storage->expects($this->any())
  140. ->method('getCache')
  141. ->will($this->returnValue($cache));
  142. $view->expects($this->any())
  143. ->method('resolvePath')
  144. ->with('/bar/foo')
  145. ->will($this->returnValue(array($storage, 'foo')));
  146. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  147. $children = $node->getDirectoryListing();
  148. $this->assertEquals(2, count($children));
  149. $this->assertInstanceOf('\OC\Files\Node\File', $children[0]);
  150. $this->assertInstanceOf('\OC\Files\Node\Folder', $children[1]);
  151. $this->assertEquals('asd', $children[0]->getName());
  152. $this->assertEquals('qwerty', $children[1]->getName());
  153. }
  154. public function testGet() {
  155. $manager = $this->getMock('\OC\Files\Mount\Manager');
  156. /**
  157. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  158. */
  159. $view = $this->getMock('\OC\Files\View');
  160. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  161. $root->expects($this->any())
  162. ->method('getUser')
  163. ->will($this->returnValue($this->user));
  164. $root->expects($this->once())
  165. ->method('get')
  166. ->with('/bar/foo/asd');
  167. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  168. $node->get('asd');
  169. }
  170. public function testNodeExists() {
  171. $manager = $this->getMock('\OC\Files\Mount\Manager');
  172. /**
  173. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  174. */
  175. $view = $this->getMock('\OC\Files\View');
  176. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  177. $root->expects($this->any())
  178. ->method('getUser')
  179. ->will($this->returnValue($this->user));
  180. $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd');
  181. $root->expects($this->once())
  182. ->method('get')
  183. ->with('/bar/foo/asd')
  184. ->will($this->returnValue($child));
  185. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  186. $this->assertTrue($node->nodeExists('asd'));
  187. }
  188. public function testNodeExistsNotExists() {
  189. $manager = $this->getMock('\OC\Files\Mount\Manager');
  190. /**
  191. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  192. */
  193. $view = $this->getMock('\OC\Files\View');
  194. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  195. $root->expects($this->any())
  196. ->method('getUser')
  197. ->will($this->returnValue($this->user));
  198. $root->expects($this->once())
  199. ->method('get')
  200. ->with('/bar/foo/asd')
  201. ->will($this->throwException(new NotFoundException()));
  202. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  203. $this->assertFalse($node->nodeExists('asd'));
  204. }
  205. public function testNewFolder() {
  206. $manager = $this->getMock('\OC\Files\Mount\Manager');
  207. /**
  208. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  209. */
  210. $view = $this->getMock('\OC\Files\View');
  211. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  212. $root->expects($this->any())
  213. ->method('getUser')
  214. ->will($this->returnValue($this->user));
  215. $view->expects($this->once())
  216. ->method('getFileInfo')
  217. ->with('/bar/foo')
  218. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_ALL)));
  219. $view->expects($this->once())
  220. ->method('mkdir')
  221. ->with('/bar/foo/asd')
  222. ->will($this->returnValue(true));
  223. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  224. $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd');
  225. $result = $node->newFolder('asd');
  226. $this->assertEquals($child, $result);
  227. }
  228. /**
  229. * @expectedException \OCP\Files\NotPermittedException
  230. */
  231. public function testNewFolderNotPermitted() {
  232. $manager = $this->getMock('\OC\Files\Mount\Manager');
  233. /**
  234. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  235. */
  236. $view = $this->getMock('\OC\Files\View');
  237. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  238. $root->expects($this->any())
  239. ->method('getUser')
  240. ->will($this->returnValue($this->user));
  241. $view->expects($this->once())
  242. ->method('getFileInfo')
  243. ->with('/bar/foo')
  244. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_READ)));
  245. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  246. $node->newFolder('asd');
  247. }
  248. public function testNewFile() {
  249. $manager = $this->getMock('\OC\Files\Mount\Manager');
  250. /**
  251. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  252. */
  253. $view = $this->getMock('\OC\Files\View');
  254. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  255. $root->expects($this->any())
  256. ->method('getUser')
  257. ->will($this->returnValue($this->user));
  258. $view->expects($this->once())
  259. ->method('getFileInfo')
  260. ->with('/bar/foo')
  261. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_ALL)));
  262. $view->expects($this->once())
  263. ->method('touch')
  264. ->with('/bar/foo/asd')
  265. ->will($this->returnValue(true));
  266. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  267. $child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd');
  268. $result = $node->newFile('asd');
  269. $this->assertEquals($child, $result);
  270. }
  271. /**
  272. * @expectedException \OCP\Files\NotPermittedException
  273. */
  274. public function testNewFileNotPermitted() {
  275. $manager = $this->getMock('\OC\Files\Mount\Manager');
  276. /**
  277. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  278. */
  279. $view = $this->getMock('\OC\Files\View');
  280. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  281. $root->expects($this->any())
  282. ->method('getUser')
  283. ->will($this->returnValue($this->user));
  284. $view->expects($this->once())
  285. ->method('getFileInfo')
  286. ->with('/bar/foo')
  287. ->will($this->returnValue(array('permissions' => \OCP\PERMISSION_READ)));
  288. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  289. $node->newFile('asd');
  290. }
  291. public function testGetFreeSpace() {
  292. $manager = $this->getMock('\OC\Files\Mount\Manager');
  293. /**
  294. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  295. */
  296. $view = $this->getMock('\OC\Files\View');
  297. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  298. $root->expects($this->any())
  299. ->method('getUser')
  300. ->will($this->returnValue($this->user));
  301. $view->expects($this->once())
  302. ->method('free_space')
  303. ->with('/bar/foo')
  304. ->will($this->returnValue(100));
  305. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  306. $this->assertEquals(100, $node->getFreeSpace());
  307. }
  308. public function testSearch() {
  309. $manager = $this->getMock('\OC\Files\Mount\Manager');
  310. /**
  311. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  312. */
  313. $view = $this->getMock('\OC\Files\View');
  314. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  315. $root->expects($this->any())
  316. ->method('getUser')
  317. ->will($this->returnValue($this->user));
  318. $storage = $this->getMock('\OC\Files\Storage\Storage');
  319. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  320. $storage->expects($this->once())
  321. ->method('getCache')
  322. ->will($this->returnValue($cache));
  323. $cache->expects($this->once())
  324. ->method('search')
  325. ->with('%qw%')
  326. ->will($this->returnValue(array(
  327. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  328. )));
  329. $root->expects($this->once())
  330. ->method('getMountsIn')
  331. ->with('/bar/foo')
  332. ->will($this->returnValue(array()));
  333. $view->expects($this->once())
  334. ->method('resolvePath')
  335. ->will($this->returnValue(array($storage, 'foo')));
  336. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  337. $result = $node->search('qw');
  338. $this->assertEquals(1, count($result));
  339. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  340. }
  341. public function testSearchSubStorages() {
  342. $manager = $this->getMock('\OC\Files\Mount\Manager');
  343. /**
  344. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  345. */
  346. $view = $this->getMock('\OC\Files\View');
  347. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  348. $root->expects($this->any())
  349. ->method('getUser')
  350. ->will($this->returnValue($this->user));
  351. $storage = $this->getMock('\OC\Files\Storage\Storage');
  352. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  353. $subCache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  354. $subStorage = $this->getMock('\OC\Files\Storage\Storage');
  355. $subMount = $this->getMock('\OC\Files\Mount\Mount', array(), array(null, ''));
  356. $subMount->expects($this->once())
  357. ->method('getStorage')
  358. ->will($this->returnValue($subStorage));
  359. $subMount->expects($this->once())
  360. ->method('getMountPoint')
  361. ->will($this->returnValue('/bar/foo/bar/'));
  362. $storage->expects($this->once())
  363. ->method('getCache')
  364. ->will($this->returnValue($cache));
  365. $subStorage->expects($this->once())
  366. ->method('getCache')
  367. ->will($this->returnValue($subCache));
  368. $cache->expects($this->once())
  369. ->method('search')
  370. ->with('%qw%')
  371. ->will($this->returnValue(array(
  372. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  373. )));
  374. $subCache->expects($this->once())
  375. ->method('search')
  376. ->with('%qw%')
  377. ->will($this->returnValue(array(
  378. array('fileid' => 4, 'path' => 'asd/qweasd', 'name' => 'qweasd', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  379. )));
  380. $root->expects($this->once())
  381. ->method('getMountsIn')
  382. ->with('/bar/foo')
  383. ->will($this->returnValue(array($subMount)));
  384. $view->expects($this->once())
  385. ->method('resolvePath')
  386. ->will($this->returnValue(array($storage, 'foo')));
  387. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  388. $result = $node->search('qw');
  389. $this->assertEquals(2, count($result));
  390. }
  391. public function testIsSubNode() {
  392. $file = new Node(null, null, '/foo/bar');
  393. $folder = new \OC\Files\Node\Folder(null, null, '/foo');
  394. $this->assertTrue($folder->isSubNode($file));
  395. $this->assertFalse($folder->isSubNode($folder));
  396. $file = new Node(null, null, '/foobar');
  397. $this->assertFalse($folder->isSubNode($file));
  398. }
  399. }