FolderTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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\Cache\CacheEntry;
  11. use OC\Files\Config\CachedMountInfo;
  12. use OC\Files\FileInfo;
  13. use OC\Files\Mount\Manager;
  14. use OC\Files\Mount\MountPoint;
  15. use OC\Files\Node\Node;
  16. use OC\Files\Node\Root;
  17. use OC\Files\Storage\Temporary;
  18. use OC\Files\Storage\Wrapper\Jail;
  19. use OC\Files\View;
  20. use OC\User\User;
  21. use OCP\Files\Mount\IMountPoint;
  22. use OCP\Files\NotFoundException;
  23. use OCP\Files\Storage;
  24. /**
  25. * Class FolderTest
  26. *
  27. * @group DB
  28. *
  29. * @package Test\Files\Node
  30. */
  31. class FolderTest extends NodeTest {
  32. protected function createTestNode($root, $view, $path) {
  33. return new \OC\Files\Node\Folder($root, $view, $path);
  34. }
  35. protected function getNodeClass() {
  36. return '\OC\Files\Node\Folder';
  37. }
  38. protected function getNonExistingNodeClass() {
  39. return '\OC\Files\Node\NonExistingFolder';
  40. }
  41. protected function getViewDeleteMethod() {
  42. return 'rmdir';
  43. }
  44. public function testGetDirectoryContent() {
  45. $manager = $this->createMock(Manager::class);
  46. /**
  47. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  48. */
  49. $view = $this->createMock(View::class);
  50. $root = $this->getMockBuilder(Root::class)
  51. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  52. ->getMock();
  53. $root->expects($this->any())
  54. ->method('getUser')
  55. ->will($this->returnValue($this->user));
  56. $view->expects($this->any())
  57. ->method('getDirectoryContent')
  58. ->with('/bar/foo')
  59. ->will($this->returnValue(array(
  60. new FileInfo('/bar/foo/asd', null, 'foo/asd', ['fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'], null),
  61. new FileInfo('/bar/foo/qwerty', null, 'foo/qwerty', ['fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory'], null)
  62. )));
  63. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  64. $children = $node->getDirectoryListing();
  65. $this->assertEquals(2, count($children));
  66. $this->assertInstanceOf('\OC\Files\Node\File', $children[0]);
  67. $this->assertInstanceOf('\OC\Files\Node\Folder', $children[1]);
  68. $this->assertEquals('asd', $children[0]->getName());
  69. $this->assertEquals('qwerty', $children[1]->getName());
  70. $this->assertEquals(2, $children[0]->getId());
  71. $this->assertEquals(3, $children[1]->getId());
  72. }
  73. public function testGet() {
  74. $manager = $this->createMock(Manager::class);
  75. /**
  76. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  77. */
  78. $view = $this->createMock(View::class);
  79. $root = $this->getMockBuilder(Root::class)
  80. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  81. ->getMock();
  82. $root->expects($this->any())
  83. ->method('getUser')
  84. ->will($this->returnValue($this->user));
  85. $root->expects($this->once())
  86. ->method('get')
  87. ->with('/bar/foo/asd');
  88. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  89. $node->get('asd');
  90. }
  91. public function testNodeExists() {
  92. $manager = $this->createMock(Manager::class);
  93. /**
  94. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  95. */
  96. $view = $this->createMock(View::class);
  97. $root = $this->getMockBuilder(Root::class)
  98. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  99. ->getMock();
  100. $root->expects($this->any())
  101. ->method('getUser')
  102. ->will($this->returnValue($this->user));
  103. $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd');
  104. $root->expects($this->once())
  105. ->method('get')
  106. ->with('/bar/foo/asd')
  107. ->will($this->returnValue($child));
  108. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  109. $this->assertTrue($node->nodeExists('asd'));
  110. }
  111. public function testNodeExistsNotExists() {
  112. $manager = $this->createMock(Manager::class);
  113. /**
  114. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  115. */
  116. $view = $this->createMock(View::class);
  117. $root = $this->getMockBuilder(Root::class)
  118. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  119. ->getMock();
  120. $root->expects($this->any())
  121. ->method('getUser')
  122. ->will($this->returnValue($this->user));
  123. $root->expects($this->once())
  124. ->method('get')
  125. ->with('/bar/foo/asd')
  126. ->will($this->throwException(new NotFoundException()));
  127. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  128. $this->assertFalse($node->nodeExists('asd'));
  129. }
  130. public function testNewFolder() {
  131. $manager = $this->createMock(Manager::class);
  132. /**
  133. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  134. */
  135. $view = $this->createMock(View::class);
  136. $root = $this->getMockBuilder(Root::class)
  137. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  138. ->getMock();
  139. $root->expects($this->any())
  140. ->method('getUser')
  141. ->will($this->returnValue($this->user));
  142. $view->expects($this->once())
  143. ->method('getFileInfo')
  144. ->with('/bar/foo')
  145. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
  146. $view->expects($this->once())
  147. ->method('mkdir')
  148. ->with('/bar/foo/asd')
  149. ->will($this->returnValue(true));
  150. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  151. $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd');
  152. $result = $node->newFolder('asd');
  153. $this->assertEquals($child, $result);
  154. }
  155. /**
  156. * @expectedException \OCP\Files\NotPermittedException
  157. */
  158. public function testNewFolderNotPermitted() {
  159. $manager = $this->createMock(Manager::class);
  160. /**
  161. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  162. */
  163. $view = $this->createMock(View::class);
  164. $root = $this->getMockBuilder(Root::class)
  165. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  166. ->getMock();
  167. $root->expects($this->any())
  168. ->method('getUser')
  169. ->will($this->returnValue($this->user));
  170. $view->expects($this->once())
  171. ->method('getFileInfo')
  172. ->with('/bar/foo')
  173. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
  174. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  175. $node->newFolder('asd');
  176. }
  177. public function testNewFile() {
  178. $manager = $this->createMock(Manager::class);
  179. /**
  180. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  181. */
  182. $view = $this->createMock(View::class);
  183. $root = $this->getMockBuilder(Root::class)
  184. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  185. ->getMock();
  186. $root->expects($this->any())
  187. ->method('getUser')
  188. ->will($this->returnValue($this->user));
  189. $view->expects($this->once())
  190. ->method('getFileInfo')
  191. ->with('/bar/foo')
  192. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
  193. $view->expects($this->once())
  194. ->method('touch')
  195. ->with('/bar/foo/asd')
  196. ->will($this->returnValue(true));
  197. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  198. $child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd');
  199. $result = $node->newFile('asd');
  200. $this->assertEquals($child, $result);
  201. }
  202. /**
  203. * @expectedException \OCP\Files\NotPermittedException
  204. */
  205. public function testNewFileNotPermitted() {
  206. $manager = $this->createMock(Manager::class);
  207. /**
  208. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  209. */
  210. $view = $this->createMock(View::class);
  211. $root = $this->getMockBuilder(Root::class)
  212. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  213. ->getMock();
  214. $root->expects($this->any())
  215. ->method('getUser')
  216. ->will($this->returnValue($this->user));
  217. $view->expects($this->once())
  218. ->method('getFileInfo')
  219. ->with('/bar/foo')
  220. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
  221. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  222. $node->newFile('asd');
  223. }
  224. public function testGetFreeSpace() {
  225. $manager = $this->createMock(Manager::class);
  226. /**
  227. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  228. */
  229. $view = $this->createMock(View::class);
  230. $root = $this->getMockBuilder(Root::class)
  231. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  232. ->getMock();
  233. $root->expects($this->any())
  234. ->method('getUser')
  235. ->will($this->returnValue($this->user));
  236. $view->expects($this->once())
  237. ->method('free_space')
  238. ->with('/bar/foo')
  239. ->will($this->returnValue(100));
  240. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  241. $this->assertEquals(100, $node->getFreeSpace());
  242. }
  243. public function testSearch() {
  244. $manager = $this->createMock(Manager::class);
  245. /**
  246. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  247. */
  248. $view = $this->createMock(View::class);
  249. $root = $this->getMockBuilder(Root::class)
  250. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  251. ->getMock();
  252. $root->expects($this->any())
  253. ->method('getUser')
  254. ->will($this->returnValue($this->user));
  255. $storage = $this->createMock(Storage::class);
  256. $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  257. $storage->expects($this->once())
  258. ->method('getCache')
  259. ->will($this->returnValue($cache));
  260. $mount = $this->createMock(IMountPoint::class);
  261. $mount->expects($this->once())
  262. ->method('getStorage')
  263. ->will($this->returnValue($storage));
  264. $mount->expects($this->once())
  265. ->method('getInternalPath')
  266. ->will($this->returnValue('foo'));
  267. $cache->expects($this->once())
  268. ->method('search')
  269. ->with('%qw%')
  270. ->will($this->returnValue(array(
  271. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  272. )));
  273. $root->expects($this->once())
  274. ->method('getMountsIn')
  275. ->with('/bar/foo')
  276. ->will($this->returnValue(array()));
  277. $root->expects($this->once())
  278. ->method('getMount')
  279. ->with('/bar/foo')
  280. ->will($this->returnValue($mount));
  281. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  282. $result = $node->search('qw');
  283. $this->assertEquals(1, count($result));
  284. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  285. }
  286. public function testSearchInRoot() {
  287. $manager = $this->createMock(Manager::class);
  288. /**
  289. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  290. */
  291. $view = $this->createMock(View::class);
  292. $root = $this->getMockBuilder(Root::class)
  293. ->setMethods(['getUser', 'getMountsIn', 'getMount'])
  294. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  295. ->getMock();
  296. $root->expects($this->any())
  297. ->method('getUser')
  298. ->will($this->returnValue($this->user));
  299. $storage = $this->createMock(Storage::class);
  300. $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  301. $mount = $this->createMock(IMountPoint::class);
  302. $mount->expects($this->once())
  303. ->method('getStorage')
  304. ->will($this->returnValue($storage));
  305. $mount->expects($this->once())
  306. ->method('getInternalPath')
  307. ->will($this->returnValue('files'));
  308. $storage->expects($this->once())
  309. ->method('getCache')
  310. ->will($this->returnValue($cache));
  311. $cache->expects($this->once())
  312. ->method('search')
  313. ->with('%qw%')
  314. ->will($this->returnValue(array(
  315. array('fileid' => 3, 'path' => 'files/foo', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'),
  316. array('fileid' => 3, 'path' => 'files_trashbin/foo2.d12345', 'name' => 'foo2.d12345', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'),
  317. )));
  318. $root->expects($this->once())
  319. ->method('getMountsIn')
  320. ->with('')
  321. ->will($this->returnValue(array()));
  322. $root->expects($this->once())
  323. ->method('getMount')
  324. ->with('')
  325. ->will($this->returnValue($mount));
  326. $result = $root->search('qw');
  327. $this->assertEquals(1, count($result));
  328. $this->assertEquals('/foo', $result[0]->getPath());
  329. }
  330. public function testSearchInStorageRoot() {
  331. $manager = $this->createMock(Manager::class);
  332. /**
  333. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  334. */
  335. $view = $this->createMock(View::class);
  336. $root = $this->getMockBuilder(Root::class)
  337. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  338. ->getMock();
  339. $root->expects($this->any())
  340. ->method('getUser')
  341. ->will($this->returnValue($this->user));
  342. $storage = $this->createMock(Storage::class);
  343. $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  344. $mount = $this->createMock(IMountPoint::class);
  345. $mount->expects($this->once())
  346. ->method('getStorage')
  347. ->will($this->returnValue($storage));
  348. $mount->expects($this->once())
  349. ->method('getInternalPath')
  350. ->will($this->returnValue(''));
  351. $storage->expects($this->once())
  352. ->method('getCache')
  353. ->will($this->returnValue($cache));
  354. $cache->expects($this->once())
  355. ->method('search')
  356. ->with('%qw%')
  357. ->will($this->returnValue(array(
  358. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  359. )));
  360. $root->expects($this->once())
  361. ->method('getMountsIn')
  362. ->with('/bar')
  363. ->will($this->returnValue(array()));
  364. $root->expects($this->once())
  365. ->method('getMount')
  366. ->with('/bar')
  367. ->will($this->returnValue($mount));
  368. $node = new \OC\Files\Node\Folder($root, $view, '/bar');
  369. $result = $node->search('qw');
  370. $this->assertEquals(1, count($result));
  371. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  372. }
  373. public function testSearchByTag() {
  374. $manager = $this->createMock(Manager::class);
  375. /**
  376. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  377. */
  378. $view = $this->createMock(View::class);
  379. $root = $this->getMockBuilder(Root::class)
  380. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  381. ->getMock();
  382. $root->expects($this->any())
  383. ->method('getUser')
  384. ->will($this->returnValue($this->user));
  385. $storage = $this->createMock(Storage::class);
  386. $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  387. $mount = $this->createMock(IMountPoint::class);
  388. $mount->expects($this->once())
  389. ->method('getStorage')
  390. ->will($this->returnValue($storage));
  391. $mount->expects($this->once())
  392. ->method('getInternalPath')
  393. ->will($this->returnValue('foo'));
  394. $storage->expects($this->once())
  395. ->method('getCache')
  396. ->will($this->returnValue($cache));
  397. $cache->expects($this->once())
  398. ->method('searchByTag')
  399. ->with('tag1', 'user1')
  400. ->will($this->returnValue(array(
  401. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  402. )));
  403. $root->expects($this->once())
  404. ->method('getMountsIn')
  405. ->with('/bar/foo')
  406. ->will($this->returnValue(array()));
  407. $root->expects($this->once())
  408. ->method('getMount')
  409. ->with('/bar/foo')
  410. ->will($this->returnValue($mount));
  411. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  412. $result = $node->searchByTag('tag1', 'user1');
  413. $this->assertEquals(1, count($result));
  414. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  415. }
  416. public function testSearchSubStorages() {
  417. $manager = $this->createMock(Manager::class);
  418. /**
  419. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  420. */
  421. $view = $this->createMock(View::class);
  422. $root = $this->getMockBuilder(Root::class)
  423. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  424. ->getMock();
  425. $root->expects($this->any())
  426. ->method('getUser')
  427. ->will($this->returnValue($this->user));
  428. $storage = $this->createMock(Storage::class);
  429. $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  430. $subCache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  431. $subStorage = $this->createMock(Storage::class);
  432. $subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
  433. $mount = $this->createMock(IMountPoint::class);
  434. $mount->expects($this->once())
  435. ->method('getStorage')
  436. ->will($this->returnValue($storage));
  437. $mount->expects($this->once())
  438. ->method('getInternalPath')
  439. ->will($this->returnValue('foo'));
  440. $subMount->expects($this->once())
  441. ->method('getStorage')
  442. ->will($this->returnValue($subStorage));
  443. $subMount->expects($this->once())
  444. ->method('getMountPoint')
  445. ->will($this->returnValue('/bar/foo/bar/'));
  446. $storage->expects($this->once())
  447. ->method('getCache')
  448. ->will($this->returnValue($cache));
  449. $subStorage->expects($this->once())
  450. ->method('getCache')
  451. ->will($this->returnValue($subCache));
  452. $cache->expects($this->once())
  453. ->method('search')
  454. ->with('%qw%')
  455. ->will($this->returnValue(array(
  456. array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  457. )));
  458. $subCache->expects($this->once())
  459. ->method('search')
  460. ->with('%qw%')
  461. ->will($this->returnValue(array(
  462. array('fileid' => 4, 'path' => 'asd/qweasd', 'name' => 'qweasd', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
  463. )));
  464. $root->expects($this->once())
  465. ->method('getMountsIn')
  466. ->with('/bar/foo')
  467. ->will($this->returnValue(array($subMount)));
  468. $root->expects($this->once())
  469. ->method('getMount')
  470. ->with('/bar/foo')
  471. ->will($this->returnValue($mount));
  472. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  473. $result = $node->search('qw');
  474. $this->assertEquals(2, count($result));
  475. }
  476. public function testIsSubNode() {
  477. $file = new Node(null, null, '/foo/bar');
  478. $folder = new \OC\Files\Node\Folder(null, null, '/foo');
  479. $this->assertTrue($folder->isSubNode($file));
  480. $this->assertFalse($folder->isSubNode($folder));
  481. $file = new Node(null, null, '/foobar');
  482. $this->assertFalse($folder->isSubNode($file));
  483. }
  484. public function testGetById() {
  485. $manager = $this->createMock(Manager::class);
  486. /**
  487. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  488. */
  489. $view = $this->createMock(View::class);
  490. $root = $this->getMockBuilder(Root::class)
  491. ->setMethods(['getMountsIn', 'getMount'])
  492. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  493. ->getMock();
  494. $storage = $this->createMock(\OC\Files\Storage\Storage::class);
  495. $mount = new MountPoint($storage, '/bar');
  496. $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  497. $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);
  498. $storage->expects($this->once())
  499. ->method('getCache')
  500. ->will($this->returnValue($cache));
  501. $this->userMountCache->expects($this->any())
  502. ->method('getMountsForFileId')
  503. ->with(1)
  504. ->will($this->returnValue([new CachedMountInfo(
  505. $this->user,
  506. 1,
  507. 0,
  508. '/bar/',
  509. 1,
  510. ''
  511. )]));
  512. $cache->expects($this->once())
  513. ->method('get')
  514. ->with(1)
  515. ->will($this->returnValue($fileInfo));
  516. $root->expects($this->once())
  517. ->method('getMountsIn')
  518. ->with('/bar/foo')
  519. ->will($this->returnValue(array()));
  520. $root->expects($this->once())
  521. ->method('getMount')
  522. ->with('/bar/foo')
  523. ->will($this->returnValue($mount));
  524. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  525. $result = $node->getById(1);
  526. $this->assertEquals(1, count($result));
  527. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  528. }
  529. public function testGetByIdOutsideFolder() {
  530. $manager = $this->createMock(Manager::class);
  531. /**
  532. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  533. */
  534. $view = $this->createMock(View::class);
  535. $root = $this->getMockBuilder(Root::class)
  536. ->setMethods(['getMountsIn', 'getMount'])
  537. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  538. ->getMock();
  539. $storage = $this->createMock(\OC\Files\Storage\Storage::class);
  540. $mount = new MountPoint($storage, '/bar');
  541. $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  542. $fileInfo = new CacheEntry(['path' => 'foobar', 'mimetype' => 'text/plain'], null);
  543. $storage->expects($this->once())
  544. ->method('getCache')
  545. ->will($this->returnValue($cache));
  546. $this->userMountCache->expects($this->any())
  547. ->method('getMountsForFileId')
  548. ->with(1)
  549. ->will($this->returnValue([new CachedMountInfo(
  550. $this->user,
  551. 1,
  552. 0,
  553. '/bar/',
  554. 1,
  555. ''
  556. )]));
  557. $cache->expects($this->once())
  558. ->method('get')
  559. ->with(1)
  560. ->will($this->returnValue($fileInfo));
  561. $root->expects($this->once())
  562. ->method('getMountsIn')
  563. ->with('/bar/foo')
  564. ->will($this->returnValue(array()));
  565. $root->expects($this->once())
  566. ->method('getMount')
  567. ->with('/bar/foo')
  568. ->will($this->returnValue($mount));
  569. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  570. $result = $node->getById(1);
  571. $this->assertEquals(0, count($result));
  572. }
  573. public function testGetByIdMultipleStorages() {
  574. $manager = $this->createMock(Manager::class);
  575. /**
  576. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  577. */
  578. $view = $this->createMock(View::class);
  579. $root = $this->getMockBuilder(Root::class)
  580. ->setMethods(['getMountsIn', 'getMount'])
  581. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  582. ->getMock();
  583. $storage = $this->createMock(\OC\Files\Storage\Storage::class);
  584. $mount1 = new MountPoint($storage, '/bar');
  585. $mount2 = new MountPoint($storage, '/bar/foo/asd');
  586. $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  587. $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);
  588. $storage->expects($this->once())
  589. ->method('getCache')
  590. ->will($this->returnValue($cache));
  591. $this->userMountCache->expects($this->any())
  592. ->method('getMountsForFileId')
  593. ->with(1)
  594. ->will($this->returnValue([
  595. new CachedMountInfo(
  596. $this->user,
  597. 1,
  598. 0,
  599. '/bar/',
  600. 1,
  601. ''
  602. ),
  603. new CachedMountInfo(
  604. $this->user,
  605. 1,
  606. 0,
  607. '/bar/foo/asd/',
  608. 1,
  609. ''
  610. )
  611. ]));
  612. $storage->expects($this->any())
  613. ->method('getCache')
  614. ->will($this->returnValue($cache));
  615. $cache->expects($this->any())
  616. ->method('get')
  617. ->with(1)
  618. ->will($this->returnValue($fileInfo));
  619. $root->expects($this->any())
  620. ->method('getMountsIn')
  621. ->with('/bar/foo')
  622. ->will($this->returnValue(array($mount2)));
  623. $root->expects($this->once())
  624. ->method('getMount')
  625. ->with('/bar/foo')
  626. ->will($this->returnValue($mount1));
  627. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  628. $result = $node->getById(1);
  629. $this->assertEquals(2, count($result));
  630. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  631. $this->assertEquals('/bar/foo/asd/foo/qwerty', $result[1]->getPath());
  632. }
  633. public function uniqueNameProvider() {
  634. return [
  635. // input, existing, expected
  636. ['foo', [], 'foo'],
  637. ['foo', ['foo'], 'foo (2)'],
  638. ['foo', ['foo', 'foo (2)'], 'foo (3)']
  639. ];
  640. }
  641. /**
  642. * @dataProvider uniqueNameProvider
  643. */
  644. public function testGetUniqueName($name, $existingFiles, $expected) {
  645. $manager = $this->createMock(Manager::class);
  646. $folderPath = '/bar/foo';
  647. /**
  648. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  649. */
  650. $view = $this->createMock(View::class);
  651. $root = $this->getMockBuilder(Root::class)
  652. ->setMethods(['getUser', 'getMountsIn', 'getMount'])
  653. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  654. ->getMock();
  655. $view->expects($this->any())
  656. ->method('file_exists')
  657. ->will($this->returnCallback(function ($path) use ($existingFiles, $folderPath) {
  658. foreach ($existingFiles as $existing) {
  659. if ($folderPath . '/' . $existing === $path) {
  660. return true;
  661. }
  662. }
  663. return false;
  664. }));
  665. $node = new \OC\Files\Node\Folder($root, $view, $folderPath);
  666. $this->assertEquals($expected, $node->getNonExistingName($name));
  667. }
  668. public function testRecent() {
  669. $manager = $this->createMock(Manager::class);
  670. $folderPath = '/bar/foo';
  671. /**
  672. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  673. */
  674. $view = $this->createMock(View::class);
  675. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */
  676. $root = $this->getMockBuilder(Root::class)
  677. ->setMethods(['getUser', 'getMountsIn', 'getMount'])
  678. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  679. ->getMock();
  680. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */
  681. $folderInfo = $this->getMockBuilder('\OC\Files\FileInfo')
  682. ->disableOriginalConstructor()->getMock();
  683. $baseTime = 1000;
  684. $storage = new Temporary();
  685. $mount = new MountPoint($storage, '');
  686. $folderInfo->expects($this->any())
  687. ->method('getMountPoint')
  688. ->will($this->returnValue($mount));
  689. $cache = $storage->getCache();
  690. $id1 = $cache->put('bar/foo/inside.txt', [
  691. 'storage_mtime' => $baseTime,
  692. 'mtime' => $baseTime,
  693. 'mimetype' => 'text/plain',
  694. 'size' => 3
  695. ]);
  696. $id2 = $cache->put('bar/foo/old.txt', [
  697. 'storage_mtime' => $baseTime - 100,
  698. 'mtime' => $baseTime - 100,
  699. 'mimetype' => 'text/plain',
  700. 'size' => 3
  701. ]);
  702. $cache->put('bar/asd/outside.txt', [
  703. 'storage_mtime' => $baseTime,
  704. 'mtime' => $baseTime,
  705. 'mimetype' => 'text/plain',
  706. 'size' => 3
  707. ]);
  708. $id3 = $cache->put('bar/foo/older.txt', [
  709. 'storage_mtime' => $baseTime - 600,
  710. 'mtime' => $baseTime - 600,
  711. 'mimetype' => 'text/plain',
  712. 'size' => 3
  713. ]);
  714. $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo);
  715. $nodes = $node->getRecent(5);
  716. $ids = array_map(function (Node $node) {
  717. return (int)$node->getId();
  718. }, $nodes);
  719. $this->assertEquals([$id1, $id2, $id3], $ids);
  720. }
  721. public function testRecentFolder() {
  722. $manager = $this->createMock(Manager::class);
  723. $folderPath = '/bar/foo';
  724. /**
  725. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  726. */
  727. $view = $this->createMock(View::class);
  728. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */
  729. $root = $this->getMockBuilder(Root::class)
  730. ->setMethods(['getUser', 'getMountsIn', 'getMount'])
  731. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  732. ->getMock();
  733. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */
  734. $folderInfo = $this->getMockBuilder('\OC\Files\FileInfo')
  735. ->disableOriginalConstructor()->getMock();
  736. $baseTime = 1000;
  737. $storage = new Temporary();
  738. $mount = new MountPoint($storage, '');
  739. $folderInfo->expects($this->any())
  740. ->method('getMountPoint')
  741. ->will($this->returnValue($mount));
  742. $cache = $storage->getCache();
  743. $id1 = $cache->put('bar/foo/folder', [
  744. 'storage_mtime' => $baseTime,
  745. 'mtime' => $baseTime,
  746. 'mimetype' => \OCP\Files\FileInfo::MIMETYPE_FOLDER,
  747. 'size' => 3
  748. ]);
  749. $id2 = $cache->put('bar/foo/folder/bar.txt', [
  750. 'storage_mtime' => $baseTime,
  751. 'mtime' => $baseTime,
  752. 'mimetype' => 'text/plain',
  753. 'size' => 3,
  754. 'parent' => $id1
  755. ]);
  756. $id3 = $cache->put('bar/foo/folder/asd.txt', [
  757. 'storage_mtime' => $baseTime - 100,
  758. 'mtime' => $baseTime - 100,
  759. 'mimetype' => 'text/plain',
  760. 'size' => 3,
  761. 'parent' => $id1
  762. ]);
  763. $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo);
  764. $nodes = $node->getRecent(5);
  765. $ids = array_map(function (Node $node) {
  766. return (int)$node->getId();
  767. }, $nodes);
  768. $this->assertEquals([$id2, $id3], $ids);
  769. $this->assertEquals($baseTime, $nodes[0]->getMTime());
  770. $this->assertEquals($baseTime - 100, $nodes[1]->getMTime());
  771. }
  772. public function testRecentJail() {
  773. $manager = $this->createMock(Manager::class);
  774. $folderPath = '/bar/foo';
  775. /**
  776. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  777. */
  778. $view = $this->createMock(View::class);
  779. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */
  780. $root = $this->getMockBuilder(Root::class)
  781. ->setMethods(['getUser', 'getMountsIn', 'getMount'])
  782. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  783. ->getMock();
  784. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */
  785. $folderInfo = $this->getMockBuilder('\OC\Files\FileInfo')
  786. ->disableOriginalConstructor()->getMock();
  787. $baseTime = 1000;
  788. $storage = new Temporary();
  789. $jail = new Jail([
  790. 'storage' => $storage,
  791. 'root' => 'folder'
  792. ]);
  793. $mount = new MountPoint($jail, '/bar/foo');
  794. $folderInfo->expects($this->any())
  795. ->method('getMountPoint')
  796. ->will($this->returnValue($mount));
  797. $cache = $storage->getCache();
  798. $id1 = $cache->put('folder/inside.txt', [
  799. 'storage_mtime' => $baseTime,
  800. 'mtime' => $baseTime,
  801. 'mimetype' => 'text/plain',
  802. 'size' => 3
  803. ]);
  804. $cache->put('outside.txt', [
  805. 'storage_mtime' => $baseTime - 100,
  806. 'mtime' => $baseTime - 100,
  807. 'mimetype' => 'text/plain',
  808. 'size' => 3
  809. ]);
  810. $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo);
  811. $nodes = $node->getRecent(5);
  812. $ids = array_map(function (Node $node) {
  813. return (int)$node->getId();
  814. }, $nodes);
  815. $this->assertEquals([$id1], $ids);
  816. }
  817. }