FolderTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 testGetByIdMountRoot() {
  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' => '', '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('getMount')
  563. ->with('/bar')
  564. ->will($this->returnValue($mount));
  565. $node = new \OC\Files\Node\Folder($root, $view, '/bar');
  566. $result = $node->getById(1);
  567. $this->assertEquals(1, count($result));
  568. $this->assertEquals('/bar', $result[0]->getPath());
  569. }
  570. public function testGetByIdOutsideFolder() {
  571. $manager = $this->createMock(Manager::class);
  572. /**
  573. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  574. */
  575. $view = $this->createMock(View::class);
  576. $root = $this->getMockBuilder(Root::class)
  577. ->setMethods(['getMountsIn', 'getMount'])
  578. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  579. ->getMock();
  580. $storage = $this->createMock(\OC\Files\Storage\Storage::class);
  581. $mount = new MountPoint($storage, '/bar');
  582. $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  583. $fileInfo = new CacheEntry(['path' => 'foobar', 'mimetype' => 'text/plain'], null);
  584. $storage->expects($this->once())
  585. ->method('getCache')
  586. ->will($this->returnValue($cache));
  587. $this->userMountCache->expects($this->any())
  588. ->method('getMountsForFileId')
  589. ->with(1)
  590. ->will($this->returnValue([new CachedMountInfo(
  591. $this->user,
  592. 1,
  593. 0,
  594. '/bar/',
  595. 1,
  596. ''
  597. )]));
  598. $cache->expects($this->once())
  599. ->method('get')
  600. ->with(1)
  601. ->will($this->returnValue($fileInfo));
  602. $root->expects($this->once())
  603. ->method('getMountsIn')
  604. ->with('/bar/foo')
  605. ->will($this->returnValue(array()));
  606. $root->expects($this->once())
  607. ->method('getMount')
  608. ->with('/bar/foo')
  609. ->will($this->returnValue($mount));
  610. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  611. $result = $node->getById(1);
  612. $this->assertEquals(0, count($result));
  613. }
  614. public function testGetByIdMultipleStorages() {
  615. $manager = $this->createMock(Manager::class);
  616. /**
  617. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  618. */
  619. $view = $this->createMock(View::class);
  620. $root = $this->getMockBuilder(Root::class)
  621. ->setMethods(['getMountsIn', 'getMount'])
  622. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  623. ->getMock();
  624. $storage = $this->createMock(\OC\Files\Storage\Storage::class);
  625. $mount1 = new MountPoint($storage, '/bar');
  626. $mount2 = new MountPoint($storage, '/bar/foo/asd');
  627. $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
  628. $fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);
  629. $storage->expects($this->exactly(2))
  630. ->method('getCache')
  631. ->will($this->returnValue($cache));
  632. $this->userMountCache->expects($this->any())
  633. ->method('getMountsForFileId')
  634. ->with(1)
  635. ->will($this->returnValue([
  636. new CachedMountInfo(
  637. $this->user,
  638. 1,
  639. 0,
  640. '/bar/',
  641. 1,
  642. ''
  643. ),
  644. new CachedMountInfo(
  645. $this->user,
  646. 1,
  647. 0,
  648. '/bar/foo/asd/',
  649. 1,
  650. ''
  651. )
  652. ]));
  653. $storage->expects($this->any())
  654. ->method('getCache')
  655. ->will($this->returnValue($cache));
  656. $cache->expects($this->any())
  657. ->method('get')
  658. ->with(1)
  659. ->will($this->returnValue($fileInfo));
  660. $root->expects($this->any())
  661. ->method('getMountsIn')
  662. ->with('/bar/foo')
  663. ->will($this->returnValue(array($mount2)));
  664. $root->expects($this->once())
  665. ->method('getMount')
  666. ->with('/bar/foo')
  667. ->will($this->returnValue($mount1));
  668. $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
  669. $result = $node->getById(1);
  670. $this->assertEquals(2, count($result));
  671. $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
  672. $this->assertEquals('/bar/foo/asd/foo/qwerty', $result[1]->getPath());
  673. }
  674. public function uniqueNameProvider() {
  675. return [
  676. // input, existing, expected
  677. ['foo', [], 'foo'],
  678. ['foo', ['foo'], 'foo (2)'],
  679. ['foo', ['foo', 'foo (2)'], 'foo (3)']
  680. ];
  681. }
  682. /**
  683. * @dataProvider uniqueNameProvider
  684. */
  685. public function testGetUniqueName($name, $existingFiles, $expected) {
  686. $manager = $this->createMock(Manager::class);
  687. $folderPath = '/bar/foo';
  688. /**
  689. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  690. */
  691. $view = $this->createMock(View::class);
  692. $root = $this->getMockBuilder(Root::class)
  693. ->setMethods(['getUser', 'getMountsIn', 'getMount'])
  694. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  695. ->getMock();
  696. $view->expects($this->any())
  697. ->method('file_exists')
  698. ->will($this->returnCallback(function ($path) use ($existingFiles, $folderPath) {
  699. foreach ($existingFiles as $existing) {
  700. if ($folderPath . '/' . $existing === $path) {
  701. return true;
  702. }
  703. }
  704. return false;
  705. }));
  706. $node = new \OC\Files\Node\Folder($root, $view, $folderPath);
  707. $this->assertEquals($expected, $node->getNonExistingName($name));
  708. }
  709. public function testRecent() {
  710. $manager = $this->createMock(Manager::class);
  711. $folderPath = '/bar/foo';
  712. /**
  713. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  714. */
  715. $view = $this->createMock(View::class);
  716. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */
  717. $root = $this->getMockBuilder(Root::class)
  718. ->setMethods(['getUser', 'getMountsIn', 'getMount'])
  719. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  720. ->getMock();
  721. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */
  722. $folderInfo = $this->getMockBuilder(FileInfo::class)
  723. ->disableOriginalConstructor()->getMock();
  724. $baseTime = 1000;
  725. $storage = new Temporary();
  726. $mount = new MountPoint($storage, '');
  727. $folderInfo->expects($this->any())
  728. ->method('getMountPoint')
  729. ->will($this->returnValue($mount));
  730. $cache = $storage->getCache();
  731. $id1 = $cache->put('bar/foo/inside.txt', [
  732. 'storage_mtime' => $baseTime,
  733. 'mtime' => $baseTime,
  734. 'mimetype' => 'text/plain',
  735. 'size' => 3
  736. ]);
  737. $id2 = $cache->put('bar/foo/old.txt', [
  738. 'storage_mtime' => $baseTime - 100,
  739. 'mtime' => $baseTime - 100,
  740. 'mimetype' => 'text/plain',
  741. 'size' => 3
  742. ]);
  743. $cache->put('bar/asd/outside.txt', [
  744. 'storage_mtime' => $baseTime,
  745. 'mtime' => $baseTime,
  746. 'mimetype' => 'text/plain',
  747. 'size' => 3
  748. ]);
  749. $id3 = $cache->put('bar/foo/older.txt', [
  750. 'storage_mtime' => $baseTime - 600,
  751. 'mtime' => $baseTime - 600,
  752. 'mimetype' => 'text/plain',
  753. 'size' => 3
  754. ]);
  755. $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo);
  756. $nodes = $node->getRecent(5);
  757. $ids = array_map(function (Node $node) {
  758. return (int)$node->getId();
  759. }, $nodes);
  760. $this->assertEquals([$id1, $id2, $id3], $ids);
  761. }
  762. public function testRecentFolder() {
  763. $manager = $this->createMock(Manager::class);
  764. $folderPath = '/bar/foo';
  765. /**
  766. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  767. */
  768. $view = $this->createMock(View::class);
  769. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */
  770. $root = $this->getMockBuilder(Root::class)
  771. ->setMethods(['getUser', 'getMountsIn', 'getMount'])
  772. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  773. ->getMock();
  774. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */
  775. $folderInfo = $this->getMockBuilder(FileInfo::class)
  776. ->disableOriginalConstructor()->getMock();
  777. $baseTime = 1000;
  778. $storage = new Temporary();
  779. $mount = new MountPoint($storage, '');
  780. $folderInfo->expects($this->any())
  781. ->method('getMountPoint')
  782. ->will($this->returnValue($mount));
  783. $cache = $storage->getCache();
  784. $id1 = $cache->put('bar/foo/folder', [
  785. 'storage_mtime' => $baseTime,
  786. 'mtime' => $baseTime,
  787. 'mimetype' => \OCP\Files\FileInfo::MIMETYPE_FOLDER,
  788. 'size' => 3
  789. ]);
  790. $id2 = $cache->put('bar/foo/folder/bar.txt', [
  791. 'storage_mtime' => $baseTime,
  792. 'mtime' => $baseTime,
  793. 'mimetype' => 'text/plain',
  794. 'size' => 3,
  795. 'parent' => $id1
  796. ]);
  797. $id3 = $cache->put('bar/foo/folder/asd.txt', [
  798. 'storage_mtime' => $baseTime - 100,
  799. 'mtime' => $baseTime - 100,
  800. 'mimetype' => 'text/plain',
  801. 'size' => 3,
  802. 'parent' => $id1
  803. ]);
  804. $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo);
  805. $nodes = $node->getRecent(5);
  806. $ids = array_map(function (Node $node) {
  807. return (int)$node->getId();
  808. }, $nodes);
  809. $this->assertEquals([$id2, $id3], $ids);
  810. $this->assertEquals($baseTime, $nodes[0]->getMTime());
  811. $this->assertEquals($baseTime - 100, $nodes[1]->getMTime());
  812. }
  813. public function testRecentJail() {
  814. $manager = $this->createMock(Manager::class);
  815. $folderPath = '/bar/foo';
  816. /**
  817. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  818. */
  819. $view = $this->createMock(View::class);
  820. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\Node\Root $root */
  821. $root = $this->getMockBuilder(Root::class)
  822. ->setMethods(['getUser', 'getMountsIn', 'getMount'])
  823. ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  824. ->getMock();
  825. /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Files\FileInfo $folderInfo */
  826. $folderInfo = $this->getMockBuilder(FileInfo::class)
  827. ->disableOriginalConstructor()->getMock();
  828. $baseTime = 1000;
  829. $storage = new Temporary();
  830. $jail = new Jail([
  831. 'storage' => $storage,
  832. 'root' => 'folder'
  833. ]);
  834. $mount = new MountPoint($jail, '/bar/foo');
  835. $folderInfo->expects($this->any())
  836. ->method('getMountPoint')
  837. ->will($this->returnValue($mount));
  838. $cache = $storage->getCache();
  839. $id1 = $cache->put('folder/inside.txt', [
  840. 'storage_mtime' => $baseTime,
  841. 'mtime' => $baseTime,
  842. 'mimetype' => 'text/plain',
  843. 'size' => 3
  844. ]);
  845. $cache->put('outside.txt', [
  846. 'storage_mtime' => $baseTime - 100,
  847. 'mtime' => $baseTime - 100,
  848. 'mimetype' => 'text/plain',
  849. 'size' => 3
  850. ]);
  851. $node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo);
  852. $nodes = $node->getRecent(5);
  853. $ids = array_map(function (Node $node) {
  854. return (int)$node->getId();
  855. }, $nodes);
  856. $this->assertEquals([$id1], $ids);
  857. }
  858. }