folder.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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\FileInfo;
  11. use OC\Files\Mount\MountPoint;
  12. use OC\Files\Node\Node;
  13. use OCP\Files\NotFoundException;
  14. use OCP\Files\NotPermittedException;
  15. use OC\Files\View;
  16. class Folder extends \Test\TestCase {
  17. private $user;
  18. protected function setUp() {
  19. parent::setUp();
  20. $this->user = new \OC\User\User('', new \OC_User_Dummy);
  21. }
  22. protected function getFileInfo($data) {
  23. return new FileInfo('', null, '', $data, null);
  24. }
  25. public function testDelete() {
  26. $manager = $this->getMock('\OC\Files\Mount\Manager');
  27. /**
  28. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  29. */
  30. $view = $this->getMock('\OC\Files\View');
  31. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  32. $root->expects($this->any())
  33. ->method('getUser')
  34. ->will($this->returnValue($this->user));
  35. $root->expects($this->exactly(2))
  36. ->method('emit')
  37. ->will($this->returnValue(true));
  38. $view->expects($this->any())
  39. ->method('getFileInfo')
  40. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
  41. $view->expects($this->once())
  42. ->method('rmdir')
  43. ->with('/bar/foo')
  44. ->will($this->returnValue(true));
  45. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  46. $node->delete();
  47. }
  48. public function testDeleteHooks() {
  49. $test = $this;
  50. $hooksRun = 0;
  51. /**
  52. * @param \OC\Files\Node\File $node
  53. */
  54. $preListener = function ($node) use (&$test, &$hooksRun) {
  55. $test->assertInstanceOf('\OC\Files\Node\Folder', $node);
  56. $test->assertEquals('foo', $node->getInternalPath());
  57. $test->assertEquals('/bar/foo', $node->getPath());
  58. $hooksRun++;
  59. };
  60. /**
  61. * @param \OC\Files\Node\File $node
  62. */
  63. $postListener = function ($node) use (&$test, &$hooksRun) {
  64. $test->assertInstanceOf('\OC\Files\Node\NonExistingFolder', $node);
  65. $test->assertEquals('foo', $node->getInternalPath());
  66. $test->assertEquals('/bar/foo', $node->getPath());
  67. $hooksRun++;
  68. };
  69. /**
  70. * @var \OC\Files\Mount\Manager $manager
  71. */
  72. $manager = $this->getMock('\OC\Files\Mount\Manager');
  73. /**
  74. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  75. */
  76. $view = $this->getMock('\OC\Files\View');
  77. $root = new \OC\Files\Node\Root($manager, $view, $this->user);
  78. $root->listen('\OC\Files', 'preDelete', $preListener);
  79. $root->listen('\OC\Files', 'postDelete', $postListener);
  80. $view->expects($this->any())
  81. ->method('getFileInfo')
  82. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1))));
  83. $view->expects($this->once())
  84. ->method('rmdir')
  85. ->with('/bar/foo')
  86. ->will($this->returnValue(true));
  87. $view->expects($this->any())
  88. ->method('resolvePath')
  89. ->with('/bar/foo')
  90. ->will($this->returnValue(array(null, 'foo')));
  91. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  92. $node->delete();
  93. $this->assertEquals(2, $hooksRun);
  94. }
  95. /**
  96. * @expectedException \OCP\Files\NotPermittedException
  97. */
  98. public function testDeleteNotPermitted() {
  99. $manager = $this->getMock('\OC\Files\Mount\Manager');
  100. /**
  101. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  102. */
  103. $view = $this->getMock('\OC\Files\View');
  104. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  105. $root->expects($this->any())
  106. ->method('getUser')
  107. ->will($this->returnValue($this->user));
  108. $view->expects($this->once())
  109. ->method('getFileInfo')
  110. ->with('/bar/foo')
  111. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
  112. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  113. $node->delete();
  114. }
  115. public function testGetDirectoryContent() {
  116. $manager = $this->getMock('\OC\Files\Mount\Manager');
  117. /**
  118. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  119. */
  120. $view = $this->getMock('\OC\Files\View');
  121. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  122. $root->expects($this->any())
  123. ->method('getUser')
  124. ->will($this->returnValue($this->user));
  125. /**
  126. * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
  127. */
  128. $storage = $this->getMock('\OC\Files\Storage\Storage');
  129. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  130. $cache->expects($this->any())
  131. ->method('getStatus')
  132. ->with('foo')
  133. ->will($this->returnValue(Cache::COMPLETE));
  134. $cache->expects($this->once())
  135. ->method('getFolderContents')
  136. ->with('foo')
  137. ->will($this->returnValue(array(
  138. array('fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'),
  139. array('fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory')
  140. )));
  141. $root->expects($this->once())
  142. ->method('getMountsIn')
  143. ->with('/bar/foo')
  144. ->will($this->returnValue(array()));
  145. $storage->expects($this->any())
  146. ->method('getCache')
  147. ->will($this->returnValue($cache));
  148. $view->expects($this->any())
  149. ->method('resolvePath')
  150. ->with('/bar/foo')
  151. ->will($this->returnValue(array($storage, 'foo')));
  152. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  153. $children = $node->getDirectoryListing();
  154. $this->assertEquals(2, count($children));
  155. $this->assertInstanceOf('\OC\Files\Node\File', $children[0]);
  156. $this->assertInstanceOf('\OC\Files\Node\Folder', $children[1]);
  157. $this->assertEquals('asd', $children[0]->getName());
  158. $this->assertEquals('qwerty', $children[1]->getName());
  159. }
  160. public function testGet() {
  161. $manager = $this->getMock('\OC\Files\Mount\Manager');
  162. /**
  163. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  164. */
  165. $view = $this->getMock('\OC\Files\View');
  166. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  167. $root->expects($this->any())
  168. ->method('getUser')
  169. ->will($this->returnValue($this->user));
  170. $root->expects($this->once())
  171. ->method('get')
  172. ->with('/bar/foo/asd');
  173. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  174. $node->get('asd');
  175. }
  176. public function testNodeExists() {
  177. $manager = $this->getMock('\OC\Files\Mount\Manager');
  178. /**
  179. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  180. */
  181. $view = $this->getMock('\OC\Files\View');
  182. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  183. $root->expects($this->any())
  184. ->method('getUser')
  185. ->will($this->returnValue($this->user));
  186. $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd');
  187. $root->expects($this->once())
  188. ->method('get')
  189. ->with('/bar/foo/asd')
  190. ->will($this->returnValue($child));
  191. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  192. $this->assertTrue($node->nodeExists('asd'));
  193. }
  194. public function testNodeExistsNotExists() {
  195. $manager = $this->getMock('\OC\Files\Mount\Manager');
  196. /**
  197. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  198. */
  199. $view = $this->getMock('\OC\Files\View');
  200. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  201. $root->expects($this->any())
  202. ->method('getUser')
  203. ->will($this->returnValue($this->user));
  204. $root->expects($this->once())
  205. ->method('get')
  206. ->with('/bar/foo/asd')
  207. ->will($this->throwException(new NotFoundException()));
  208. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  209. $this->assertFalse($node->nodeExists('asd'));
  210. }
  211. public function testNewFolder() {
  212. $manager = $this->getMock('\OC\Files\Mount\Manager');
  213. /**
  214. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  215. */
  216. $view = $this->getMock('\OC\Files\View');
  217. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  218. $root->expects($this->any())
  219. ->method('getUser')
  220. ->will($this->returnValue($this->user));
  221. $view->expects($this->once())
  222. ->method('getFileInfo')
  223. ->with('/bar/foo')
  224. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
  225. $view->expects($this->once())
  226. ->method('mkdir')
  227. ->with('/bar/foo/asd')
  228. ->will($this->returnValue(true));
  229. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  230. $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd');
  231. $result = $node->newFolder('asd');
  232. $this->assertEquals($child, $result);
  233. }
  234. /**
  235. * @expectedException \OCP\Files\NotPermittedException
  236. */
  237. public function testNewFolderNotPermitted() {
  238. $manager = $this->getMock('\OC\Files\Mount\Manager');
  239. /**
  240. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  241. */
  242. $view = $this->getMock('\OC\Files\View');
  243. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  244. $root->expects($this->any())
  245. ->method('getUser')
  246. ->will($this->returnValue($this->user));
  247. $view->expects($this->once())
  248. ->method('getFileInfo')
  249. ->with('/bar/foo')
  250. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
  251. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  252. $node->newFolder('asd');
  253. }
  254. public function testNewFile() {
  255. $manager = $this->getMock('\OC\Files\Mount\Manager');
  256. /**
  257. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  258. */
  259. $view = $this->getMock('\OC\Files\View');
  260. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  261. $root->expects($this->any())
  262. ->method('getUser')
  263. ->will($this->returnValue($this->user));
  264. $view->expects($this->once())
  265. ->method('getFileInfo')
  266. ->with('/bar/foo')
  267. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
  268. $view->expects($this->once())
  269. ->method('touch')
  270. ->with('/bar/foo/asd')
  271. ->will($this->returnValue(true));
  272. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  273. $child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd');
  274. $result = $node->newFile('asd');
  275. $this->assertEquals($child, $result);
  276. }
  277. /**
  278. * @expectedException \OCP\Files\NotPermittedException
  279. */
  280. public function testNewFileNotPermitted() {
  281. $manager = $this->getMock('\OC\Files\Mount\Manager');
  282. /**
  283. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  284. */
  285. $view = $this->getMock('\OC\Files\View');
  286. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  287. $root->expects($this->any())
  288. ->method('getUser')
  289. ->will($this->returnValue($this->user));
  290. $view->expects($this->once())
  291. ->method('getFileInfo')
  292. ->with('/bar/foo')
  293. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
  294. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  295. $node->newFile('asd');
  296. }
  297. public function testGetFreeSpace() {
  298. $manager = $this->getMock('\OC\Files\Mount\Manager');
  299. /**
  300. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  301. */
  302. $view = $this->getMock('\OC\Files\View');
  303. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  304. $root->expects($this->any())
  305. ->method('getUser')
  306. ->will($this->returnValue($this->user));
  307. $view->expects($this->once())
  308. ->method('free_space')
  309. ->with('/bar/foo')
  310. ->will($this->returnValue(100));
  311. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  312. $this->assertEquals(100, $node->getFreeSpace());
  313. }
  314. public function testSearch() {
  315. $manager = $this->getMock('\OC\Files\Mount\Manager');
  316. /**
  317. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  318. */
  319. $view = $this->getMock('\OC\Files\View');
  320. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  321. $root->expects($this->any())
  322. ->method('getUser')
  323. ->will($this->returnValue($this->user));
  324. $storage = $this->getMock('\OC\Files\Storage\Storage');
  325. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  326. $storage->expects($this->once())
  327. ->method('getCache')
  328. ->will($this->returnValue($cache));
  329. $cache->expects($this->once())
  330. ->method('search')
  331. ->with('%qw%')
  332. ->will($this->returnValue(array(
  333. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  334. )));
  335. $root->expects($this->once())
  336. ->method('getMountsIn')
  337. ->with('/bar/foo')
  338. ->will($this->returnValue(array()));
  339. $view->expects($this->once())
  340. ->method('resolvePath')
  341. ->will($this->returnValue(array($storage, 'foo')));
  342. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  343. $result = $node->search('qw');
  344. $this->assertEquals(1, count($result));
  345. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  346. }
  347. public function testSearchInRoot() {
  348. $manager = $this->getMock('\OC\Files\Mount\Manager');
  349. /**
  350. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  351. */
  352. $view = $this->getMock('\OC\Files\View');
  353. $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn'), array($manager, $view, $this->user));
  354. $root->expects($this->any())
  355. ->method('getUser')
  356. ->will($this->returnValue($this->user));
  357. $storage = $this->getMock('\OC\Files\Storage\Storage');
  358. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  359. $storage->expects($this->once())
  360. ->method('getCache')
  361. ->will($this->returnValue($cache));
  362. $cache->expects($this->once())
  363. ->method('search')
  364. ->with('%qw%')
  365. ->will($this->returnValue(array(
  366. array('fileid' => 3, 'path' => 'files/foo', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'),
  367. array('fileid' => 3, 'path' => 'files_trashbin/foo2.d12345', 'name' => 'foo2.d12345', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'),
  368. )));
  369. $root->expects($this->once())
  370. ->method('getMountsIn')
  371. ->with('')
  372. ->will($this->returnValue(array()));
  373. $view->expects($this->once())
  374. ->method('resolvePath')
  375. ->will($this->returnValue(array($storage, 'files')));
  376. $result = $root->search('qw');
  377. $this->assertEquals(1, count($result));
  378. $this->assertEquals('/foo', $result[0]->getPath());
  379. }
  380. public function testSearchByTag() {
  381. $manager = $this->getMock('\OC\Files\Mount\Manager');
  382. /**
  383. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  384. */
  385. $view = $this->getMock('\OC\Files\View');
  386. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  387. $root->expects($this->any())
  388. ->method('getUser')
  389. ->will($this->returnValue($this->user));
  390. $storage = $this->getMock('\OC\Files\Storage\Storage');
  391. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  392. $storage->expects($this->once())
  393. ->method('getCache')
  394. ->will($this->returnValue($cache));
  395. $cache->expects($this->once())
  396. ->method('searchByTag')
  397. ->with('tag1', 'user1')
  398. ->will($this->returnValue(array(
  399. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  400. )));
  401. $root->expects($this->once())
  402. ->method('getMountsIn')
  403. ->with('/bar/foo')
  404. ->will($this->returnValue(array()));
  405. $view->expects($this->once())
  406. ->method('resolvePath')
  407. ->will($this->returnValue(array($storage, 'foo')));
  408. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  409. $result = $node->searchByTag('tag1', 'user1');
  410. $this->assertEquals(1, count($result));
  411. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  412. }
  413. public function testSearchSubStorages() {
  414. $manager = $this->getMock('\OC\Files\Mount\Manager');
  415. /**
  416. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  417. */
  418. $view = $this->getMock('\OC\Files\View');
  419. $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
  420. $root->expects($this->any())
  421. ->method('getUser')
  422. ->will($this->returnValue($this->user));
  423. $storage = $this->getMock('\OC\Files\Storage\Storage');
  424. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  425. $subCache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  426. $subStorage = $this->getMock('\OC\Files\Storage\Storage');
  427. $subMount = $this->getMock('\OC\Files\Mount\MountPoint', array(), array(null, ''));
  428. $subMount->expects($this->once())
  429. ->method('getStorage')
  430. ->will($this->returnValue($subStorage));
  431. $subMount->expects($this->once())
  432. ->method('getMountPoint')
  433. ->will($this->returnValue('/bar/foo/bar/'));
  434. $storage->expects($this->once())
  435. ->method('getCache')
  436. ->will($this->returnValue($cache));
  437. $subStorage->expects($this->once())
  438. ->method('getCache')
  439. ->will($this->returnValue($subCache));
  440. $cache->expects($this->once())
  441. ->method('search')
  442. ->with('%qw%')
  443. ->will($this->returnValue(array(
  444. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  445. )));
  446. $subCache->expects($this->once())
  447. ->method('search')
  448. ->with('%qw%')
  449. ->will($this->returnValue(array(
  450. array('fileid' => 4, 'path' => 'asd/qweasd', 'name' => 'qweasd', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  451. )));
  452. $root->expects($this->once())
  453. ->method('getMountsIn')
  454. ->with('/bar/foo')
  455. ->will($this->returnValue(array($subMount)));
  456. $view->expects($this->once())
  457. ->method('resolvePath')
  458. ->will($this->returnValue(array($storage, 'foo')));
  459. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  460. $result = $node->search('qw');
  461. $this->assertEquals(2, count($result));
  462. }
  463. public function testIsSubNode() {
  464. $file = new Node(null, null, '/foo/bar');
  465. $folder = new \OC\Files\Node\Folder(null, null, '/foo');
  466. $this->assertTrue($folder->isSubNode($file));
  467. $this->assertFalse($folder->isSubNode($folder));
  468. $file = new Node(null, null, '/foobar');
  469. $this->assertFalse($folder->isSubNode($file));
  470. }
  471. public function testGetById() {
  472. $manager = $this->getMock('\OC\Files\Mount\Manager');
  473. /**
  474. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  475. */
  476. $view = $this->getMock('\OC\Files\View');
  477. $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user));
  478. $root->expects($this->any())
  479. ->method('getUser')
  480. ->will($this->returnValue($this->user));
  481. $storage = $this->getMock('\OC\Files\Storage\Storage');
  482. $mount = new MountPoint($storage, '/bar');
  483. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  484. $view->expects($this->once())
  485. ->method('file_exists')
  486. ->will($this->returnValue(true));
  487. $storage->expects($this->once())
  488. ->method('getCache')
  489. ->will($this->returnValue($cache));
  490. $cache->expects($this->once())
  491. ->method('getPathById')
  492. ->with('1')
  493. ->will($this->returnValue('foo/qwerty'));
  494. $root->expects($this->once())
  495. ->method('getMountsIn')
  496. ->with('/bar/foo')
  497. ->will($this->returnValue(array()));
  498. $root->expects($this->once())
  499. ->method('getMount')
  500. ->with('/bar/foo')
  501. ->will($this->returnValue($mount));
  502. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  503. $result = $node->getById(1);
  504. $this->assertEquals(1, count($result));
  505. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  506. }
  507. public function testGetByIdOutsideFolder() {
  508. $manager = $this->getMock('\OC\Files\Mount\Manager');
  509. /**
  510. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  511. */
  512. $view = $this->getMock('\OC\Files\View');
  513. $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user));
  514. $root->expects($this->any())
  515. ->method('getUser')
  516. ->will($this->returnValue($this->user));
  517. $storage = $this->getMock('\OC\Files\Storage\Storage');
  518. $mount = new MountPoint($storage, '/bar');
  519. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  520. $storage->expects($this->once())
  521. ->method('getCache')
  522. ->will($this->returnValue($cache));
  523. $cache->expects($this->once())
  524. ->method('getPathById')
  525. ->with('1')
  526. ->will($this->returnValue('foobar'));
  527. $root->expects($this->once())
  528. ->method('getMountsIn')
  529. ->with('/bar/foo')
  530. ->will($this->returnValue(array()));
  531. $root->expects($this->once())
  532. ->method('getMount')
  533. ->with('/bar/foo')
  534. ->will($this->returnValue($mount));
  535. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  536. $result = $node->getById(1);
  537. $this->assertCount(0, $result);
  538. }
  539. public function testGetByIdMultipleStorages() {
  540. $manager = $this->getMock('\OC\Files\Mount\Manager');
  541. /**
  542. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  543. */
  544. $view = $this->getMock('\OC\Files\View');
  545. $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user));
  546. $root->expects($this->any())
  547. ->method('getUser')
  548. ->will($this->returnValue($this->user));
  549. $storage = $this->getMock('\OC\Files\Storage\Storage');
  550. $mount1 = new MountPoint($storage, '/bar');
  551. $mount2 = new MountPoint($storage, '/bar/foo/asd');
  552. $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
  553. $view->expects($this->any())
  554. ->method('file_exists')
  555. ->will($this->returnValue(true));
  556. $storage->expects($this->any())
  557. ->method('getCache')
  558. ->will($this->returnValue($cache));
  559. $cache->expects($this->any())
  560. ->method('getPathById')
  561. ->with('1')
  562. ->will($this->returnValue('foo/qwerty'));
  563. $root->expects($this->any())
  564. ->method('getMountsIn')
  565. ->with('/bar/foo')
  566. ->will($this->returnValue(array($mount2)));
  567. $root->expects($this->once())
  568. ->method('getMount')
  569. ->with('/bar/foo')
  570. ->will($this->returnValue($mount1));
  571. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  572. $result = $node->getById(1);
  573. $this->assertEquals(2, count($result));
  574. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  575. $this->assertEquals('/bar/foo/asd/foo/qwerty', $result[1]->getPath());
  576. }
  577. public function uniqueNameProvider() {
  578. return [
  579. // input, existing, expected
  580. ['foo', [] , 'foo'],
  581. ['foo', ['foo'] , 'foo (2)'],
  582. ['foo', ['foo', 'foo (2)'] , 'foo (3)']
  583. ];
  584. }
  585. /**
  586. * @dataProvider uniqueNameProvider
  587. */
  588. public function testGetUniqueName($name, $existingFiles, $expected) {
  589. $manager = $this->getMock('\OC\Files\Mount\Manager');
  590. $folderPath = '/bar/foo';
  591. /**
  592. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  593. */
  594. $view = $this->getMock('\OC\Files\View');
  595. $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user));
  596. $view->expects($this->any())
  597. ->method('file_exists')
  598. ->will($this->returnCallback(function ($path) use ($existingFiles, $folderPath) {
  599. foreach ($existingFiles as $existing) {
  600. if ($folderPath . '/' . $existing === $path){
  601. return true;
  602. }
  603. }
  604. return false;
  605. }));
  606. $node = new \OC\Files\Node\Folder($root, $view, $folderPath);
  607. $this->assertEquals($expected, $node->getNonExistingName($name));
  608. }
  609. }