FolderTest.php 33 KB

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