NodeTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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\FileInfo;
  10. use OC\Files\Mount\Manager;
  11. use OC\Files\View;
  12. use OCP\Files\IRootFolder;
  13. use OCP\Files\Node;
  14. use OCP\Files\NotFoundException;
  15. use OCP\Files\Storage;
  16. use OCP\ILogger;
  17. use OCP\IUser;
  18. use OCP\IUserManager;
  19. /**
  20. * Class NodeTest
  21. *
  22. * @package Test\Files\Node
  23. */
  24. abstract class NodeTest extends \Test\TestCase {
  25. /** @var \OC\User\User */
  26. protected $user;
  27. /** @var \OC\Files\Mount\Manager */
  28. protected $manager;
  29. /** @var \OC\Files\View|\PHPUnit\Framework\MockObject\MockObject */
  30. protected $view;
  31. /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject */
  32. protected $root;
  33. /** @var \OCP\Files\Config\IUserMountCache|\PHPUnit\Framework\MockObject\MockObject */
  34. protected $userMountCache;
  35. /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
  36. protected $logger;
  37. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $userManager;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->user = $this->createMock(IUser::class);
  42. $this->manager = $this->getMockBuilder(Manager::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->view = $this->getMockBuilder(View::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->userMountCache = $this->getMockBuilder('\OCP\Files\Config\IUserMountCache')
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->logger = $this->createMock(ILogger::class);
  52. $this->userManager = $this->createMock(IUserManager::class);
  53. $this->root = $this->getMockBuilder('\OC\Files\Node\Root')
  54. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  55. ->getMock();
  56. }
  57. /**
  58. * @param IRootFolder $root
  59. * @param View $view
  60. * @param string $path
  61. * @return Node
  62. */
  63. abstract protected function createTestNode($root, $view, $path);
  64. /**
  65. * @return string
  66. */
  67. abstract protected function getNodeClass();
  68. /**
  69. * @return string
  70. */
  71. abstract protected function getNonExistingNodeClass();
  72. /**
  73. * @return string
  74. */
  75. abstract protected function getViewDeleteMethod();
  76. protected function getMockStorage() {
  77. $storage = $this->getMockBuilder(Storage::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $storage->expects($this->any())
  81. ->method('getId')
  82. ->willReturn('home::someuser');
  83. return $storage;
  84. }
  85. protected function getFileInfo($data) {
  86. return new FileInfo('', $this->getMockStorage(), '', $data, null);
  87. }
  88. public function testDelete() {
  89. $this->root->expects($this->exactly(2))
  90. ->method('emit')
  91. ->willReturn(true);
  92. $this->root->expects($this->any())
  93. ->method('getUser')
  94. ->willReturn($this->user);
  95. $this->view->expects($this->once())
  96. ->method('getFileInfo')
  97. ->with('/bar/foo')
  98. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
  99. $this->view->expects($this->once())
  100. ->method($this->getViewDeleteMethod())
  101. ->with('/bar/foo')
  102. ->willReturn(true);
  103. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  104. $node->delete();
  105. }
  106. public function testDeleteHooks() {
  107. $test = $this;
  108. $hooksRun = 0;
  109. /**
  110. * @param \OC\Files\Node\File $node
  111. */
  112. $preListener = function ($node) use (&$test, &$hooksRun) {
  113. $test->assertInstanceOf($this->getNodeClass(), $node);
  114. $test->assertEquals('foo', $node->getInternalPath());
  115. $test->assertEquals('/bar/foo', $node->getPath());
  116. $test->assertEquals(1, $node->getId());
  117. $hooksRun++;
  118. };
  119. /**
  120. * @param \OC\Files\Node\File $node
  121. */
  122. $postListener = function ($node) use (&$test, &$hooksRun) {
  123. $test->assertInstanceOf($this->getNonExistingNodeClass(), $node);
  124. $test->assertEquals('foo', $node->getInternalPath());
  125. $test->assertEquals('/bar/foo', $node->getPath());
  126. $test->assertEquals(1, $node->getId());
  127. $test->assertEquals('text/plain', $node->getMimeType());
  128. $hooksRun++;
  129. };
  130. $root = new \OC\Files\Node\Root(
  131. $this->manager,
  132. $this->view,
  133. $this->user,
  134. $this->userMountCache,
  135. $this->logger,
  136. $this->userManager
  137. );
  138. $root->listen('\OC\Files', 'preDelete', $preListener);
  139. $root->listen('\OC\Files', 'postDelete', $postListener);
  140. $this->view->expects($this->any())
  141. ->method('getFileInfo')
  142. ->with('/bar/foo')
  143. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1, 'mimetype' => 'text/plain']));
  144. $this->view->expects($this->once())
  145. ->method($this->getViewDeleteMethod())
  146. ->with('/bar/foo')
  147. ->willReturn(true);
  148. $this->view->expects($this->any())
  149. ->method('resolvePath')
  150. ->with('/bar/foo')
  151. ->willReturn([null, 'foo']);
  152. $node = $this->createTestNode($root, $this->view, '/bar/foo');
  153. $node->delete();
  154. $this->assertEquals(2, $hooksRun);
  155. }
  156. public function testDeleteNotPermitted() {
  157. $this->expectException(\OCP\Files\NotPermittedException::class);
  158. $this->root->expects($this->any())
  159. ->method('getUser')
  160. ->willReturn($this->user);
  161. $this->view->expects($this->once())
  162. ->method('getFileInfo')
  163. ->with('/bar/foo')
  164. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
  165. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  166. $node->delete();
  167. }
  168. public function testStat() {
  169. $this->root->expects($this->any())
  170. ->method('getUser')
  171. ->willReturn($this->user);
  172. $stat = [
  173. 'fileid' => 1,
  174. 'size' => 100,
  175. 'etag' => 'qwerty',
  176. 'mtime' => 50,
  177. 'permissions' => 0
  178. ];
  179. $this->view->expects($this->once())
  180. ->method('stat')
  181. ->with('/bar/foo')
  182. ->willReturn($stat);
  183. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  184. $this->assertEquals($stat, $node->stat());
  185. }
  186. public function testGetId() {
  187. $this->root->expects($this->any())
  188. ->method('getUser')
  189. ->willReturn($this->user);
  190. $stat = $this->getFileInfo([
  191. 'fileid' => 1,
  192. 'size' => 100,
  193. 'etag' => 'qwerty',
  194. 'mtime' => 50
  195. ]);
  196. $this->view->expects($this->once())
  197. ->method('getFileInfo')
  198. ->with('/bar/foo')
  199. ->willReturn($stat);
  200. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  201. $this->assertEquals(1, $node->getId());
  202. }
  203. public function testGetSize() {
  204. $this->root->expects($this->any())
  205. ->method('getUser')
  206. ->willReturn($this->user);
  207. $stat = $this->getFileInfo([
  208. 'fileid' => 1,
  209. 'size' => 100,
  210. 'etag' => 'qwerty',
  211. 'mtime' => 50
  212. ]);
  213. $this->view->expects($this->once())
  214. ->method('getFileInfo')
  215. ->with('/bar/foo')
  216. ->willReturn($stat);
  217. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  218. $this->assertEquals(100, $node->getSize());
  219. }
  220. public function testGetEtag() {
  221. $this->root->expects($this->any())
  222. ->method('getUser')
  223. ->willReturn($this->user);
  224. $stat = $this->getFileInfo([
  225. 'fileid' => 1,
  226. 'size' => 100,
  227. 'etag' => 'qwerty',
  228. 'mtime' => 50
  229. ]);
  230. $this->view->expects($this->once())
  231. ->method('getFileInfo')
  232. ->with('/bar/foo')
  233. ->willReturn($stat);
  234. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  235. $this->assertEquals('qwerty', $node->getEtag());
  236. }
  237. public function testGetMTime() {
  238. $this->root->expects($this->any())
  239. ->method('getUser')
  240. ->willReturn($this->user);
  241. $stat = $this->getFileInfo([
  242. 'fileid' => 1,
  243. 'size' => 100,
  244. 'etag' => 'qwerty',
  245. 'mtime' => 50
  246. ]);
  247. $this->view->expects($this->once())
  248. ->method('getFileInfo')
  249. ->with('/bar/foo')
  250. ->willReturn($stat);
  251. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  252. $this->assertEquals(50, $node->getMTime());
  253. }
  254. public function testGetStorage() {
  255. $this->root->expects($this->any())
  256. ->method('getUser')
  257. ->willReturn($this->user);
  258. /**
  259. * @var \OC\Files\Storage\Storage | \PHPUnit\Framework\MockObject\MockObject $storage
  260. */
  261. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  262. ->disableOriginalConstructor()
  263. ->getMock();
  264. $this->view->expects($this->once())
  265. ->method('resolvePath')
  266. ->with('/bar/foo')
  267. ->willReturn([$storage, 'foo']);
  268. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  269. $this->assertEquals($storage, $node->getStorage());
  270. }
  271. public function testGetPath() {
  272. $this->root->expects($this->any())
  273. ->method('getUser')
  274. ->willReturn($this->user);
  275. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  276. $this->assertEquals('/bar/foo', $node->getPath());
  277. }
  278. public function testGetInternalPath() {
  279. $this->root->expects($this->any())
  280. ->method('getUser')
  281. ->willReturn($this->user);
  282. /**
  283. * @var \OC\Files\Storage\Storage | \PHPUnit\Framework\MockObject\MockObject $storage
  284. */
  285. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  286. ->disableOriginalConstructor()
  287. ->getMock();
  288. $this->view->expects($this->once())
  289. ->method('resolvePath')
  290. ->with('/bar/foo')
  291. ->willReturn([$storage, 'foo']);
  292. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  293. $this->assertEquals('foo', $node->getInternalPath());
  294. }
  295. public function testGetName() {
  296. $this->root->expects($this->any())
  297. ->method('getUser')
  298. ->willReturn($this->user);
  299. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  300. $this->assertEquals('foo', $node->getName());
  301. }
  302. public function testTouchSetMTime() {
  303. $this->root->expects($this->any())
  304. ->method('getUser')
  305. ->willReturn($this->user);
  306. $this->view->expects($this->once())
  307. ->method('touch')
  308. ->with('/bar/foo', 100)
  309. ->willReturn(true);
  310. $this->view->expects($this->once())
  311. ->method('getFileInfo')
  312. ->with('/bar/foo')
  313. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
  314. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  315. $node->touch(100);
  316. $this->assertEquals(100, $node->getMTime());
  317. }
  318. public function testTouchHooks() {
  319. $test = $this;
  320. $hooksRun = 0;
  321. /**
  322. * @param \OC\Files\Node\File $node
  323. */
  324. $preListener = function ($node) use (&$test, &$hooksRun) {
  325. $test->assertEquals('foo', $node->getInternalPath());
  326. $test->assertEquals('/bar/foo', $node->getPath());
  327. $hooksRun++;
  328. };
  329. /**
  330. * @param \OC\Files\Node\File $node
  331. */
  332. $postListener = function ($node) use (&$test, &$hooksRun) {
  333. $test->assertEquals('foo', $node->getInternalPath());
  334. $test->assertEquals('/bar/foo', $node->getPath());
  335. $hooksRun++;
  336. };
  337. $root = new \OC\Files\Node\Root(
  338. $this->manager,
  339. $this->view,
  340. $this->user,
  341. $this->userMountCache,
  342. $this->logger,
  343. $this->userManager
  344. );
  345. $root->listen('\OC\Files', 'preTouch', $preListener);
  346. $root->listen('\OC\Files', 'postTouch', $postListener);
  347. $this->view->expects($this->once())
  348. ->method('touch')
  349. ->with('/bar/foo', 100)
  350. ->willReturn(true);
  351. $this->view->expects($this->any())
  352. ->method('resolvePath')
  353. ->with('/bar/foo')
  354. ->willReturn([null, 'foo']);
  355. $this->view->expects($this->any())
  356. ->method('getFileInfo')
  357. ->with('/bar/foo')
  358. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
  359. $node = $this->createTestNode($root, $this->view, '/bar/foo');
  360. $node->touch(100);
  361. $this->assertEquals(2, $hooksRun);
  362. }
  363. public function testTouchNotPermitted() {
  364. $this->expectException(\OCP\Files\NotPermittedException::class);
  365. $this->root->expects($this->any())
  366. ->method('getUser')
  367. ->willReturn($this->user);
  368. $this->view->expects($this->any())
  369. ->method('getFileInfo')
  370. ->with('/bar/foo')
  371. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
  372. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  373. $node->touch(100);
  374. }
  375. public function testInvalidPath() {
  376. $this->expectException(\OCP\Files\InvalidPathException::class);
  377. $node = $this->createTestNode($this->root, $this->view, '/../foo');
  378. $node->getFileInfo();
  379. }
  380. public function testCopySameStorage() {
  381. $this->view->expects($this->any())
  382. ->method('copy')
  383. ->with('/bar/foo', '/bar/asd')
  384. ->willReturn(true);
  385. $this->view->expects($this->any())
  386. ->method('getFileInfo')
  387. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 3]));
  388. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  389. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  390. $newNode = $this->createTestNode($this->root, $this->view, '/bar/asd');
  391. $this->root->expects($this->exactly(2))
  392. ->method('get')
  393. ->willReturnMap([
  394. ['/bar/asd', $newNode],
  395. ['/bar', $parentNode]
  396. ]);
  397. $target = $node->copy('/bar/asd');
  398. $this->assertInstanceOf($this->getNodeClass(), $target);
  399. $this->assertEquals(3, $target->getId());
  400. }
  401. public function testCopyNotPermitted() {
  402. $this->expectException(\OCP\Files\NotPermittedException::class);
  403. /**
  404. * @var \OC\Files\Storage\Storage | \PHPUnit\Framework\MockObject\MockObject $storage
  405. */
  406. $storage = $this->createMock('\OC\Files\Storage\Storage');
  407. $this->root->expects($this->never())
  408. ->method('getMount');
  409. $storage->expects($this->never())
  410. ->method('copy');
  411. $this->view->expects($this->any())
  412. ->method('getFileInfo')
  413. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ, 'fileid' => 3]));
  414. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  415. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  416. $this->root->expects($this->once())
  417. ->method('get')
  418. ->willReturnMap([
  419. ['/bar', $parentNode]
  420. ]);
  421. $node->copy('/bar/asd');
  422. }
  423. public function testCopyNoParent() {
  424. $this->expectException(\OCP\Files\NotFoundException::class);
  425. $this->view->expects($this->never())
  426. ->method('copy');
  427. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  428. $this->root->expects($this->once())
  429. ->method('get')
  430. ->with('/bar/asd')
  431. ->will($this->throwException(new NotFoundException()));
  432. $node->copy('/bar/asd/foo');
  433. }
  434. public function testCopyParentIsFile() {
  435. $this->expectException(\OCP\Files\NotPermittedException::class);
  436. $this->view->expects($this->never())
  437. ->method('copy');
  438. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  439. $parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar');
  440. $this->root->expects($this->once())
  441. ->method('get')
  442. ->willReturnMap([
  443. ['/bar', $parentNode]
  444. ]);
  445. $node->copy('/bar/asd');
  446. }
  447. public function testMoveSameStorage() {
  448. $this->view->expects($this->any())
  449. ->method('rename')
  450. ->with('/bar/foo', '/bar/asd')
  451. ->willReturn(true);
  452. $this->view->expects($this->any())
  453. ->method('getFileInfo')
  454. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1]));
  455. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  456. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  457. $this->root->expects($this->any())
  458. ->method('get')
  459. ->willReturnMap([['/bar', $parentNode], ['/bar/asd', $node]]);
  460. $target = $node->move('/bar/asd');
  461. $this->assertInstanceOf($this->getNodeClass(), $target);
  462. $this->assertEquals(1, $target->getId());
  463. $this->assertEquals('/bar/asd', $node->getPath());
  464. }
  465. public function moveOrCopyProvider() {
  466. return [
  467. ['move', 'rename', 'preRename', 'postRename'],
  468. ['copy', 'copy', 'preCopy', 'postCopy'],
  469. ];
  470. }
  471. /**
  472. * @dataProvider moveOrCopyProvider
  473. * @param string $operationMethod
  474. * @param string $viewMethod
  475. * @param string $preHookName
  476. * @param string $postHookName
  477. */
  478. public function testMoveCopyHooks($operationMethod, $viewMethod, $preHookName, $postHookName) {
  479. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject $root */
  480. $root = $this->getMockBuilder('\OC\Files\Node\Root')
  481. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  482. ->setMethods(['get'])
  483. ->getMock();
  484. $this->view->expects($this->any())
  485. ->method($viewMethod)
  486. ->with('/bar/foo', '/bar/asd')
  487. ->willReturn(true);
  488. $this->view->expects($this->any())
  489. ->method('getFileInfo')
  490. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1]));
  491. /**
  492. * @var \OC\Files\Node\File|\PHPUnit\Framework\MockObject\MockObject $node
  493. */
  494. $node = $this->createTestNode($root, $this->view, '/bar/foo');
  495. $parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
  496. $targetTestNode = $this->createTestNode($root, $this->view, '/bar/asd');
  497. $root->expects($this->any())
  498. ->method('get')
  499. ->willReturnMap([['/bar', $parentNode], ['/bar/asd', $targetTestNode]]);
  500. $hooksRun = 0;
  501. $preListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node) {
  502. $this->assertSame($node, $sourceNode);
  503. $this->assertInstanceOf($this->getNodeClass(), $sourceNode);
  504. $this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode);
  505. $this->assertEquals('/bar/asd', $targetNode->getPath());
  506. $hooksRun++;
  507. };
  508. $postListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node, $targetTestNode) {
  509. $this->assertSame($node, $sourceNode);
  510. $this->assertNotSame($node, $targetNode);
  511. $this->assertSame($targetTestNode, $targetNode);
  512. $this->assertInstanceOf($this->getNodeClass(), $sourceNode);
  513. $this->assertInstanceOf($this->getNodeClass(), $targetNode);
  514. $hooksRun++;
  515. };
  516. $preWriteListener = function (Node $targetNode) use (&$hooksRun) {
  517. $this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode);
  518. $this->assertEquals('/bar/asd', $targetNode->getPath());
  519. $hooksRun++;
  520. };
  521. $postWriteListener = function (Node $targetNode) use (&$hooksRun, $targetTestNode) {
  522. $this->assertSame($targetTestNode, $targetNode);
  523. $hooksRun++;
  524. };
  525. $root->listen('\OC\Files', $preHookName, $preListener);
  526. $root->listen('\OC\Files', 'preWrite', $preWriteListener);
  527. $root->listen('\OC\Files', $postHookName, $postListener);
  528. $root->listen('\OC\Files', 'postWrite', $postWriteListener);
  529. $node->$operationMethod('/bar/asd');
  530. $this->assertEquals(4, $hooksRun);
  531. }
  532. public function testMoveNotPermitted() {
  533. $this->expectException(\OCP\Files\NotPermittedException::class);
  534. $this->view->expects($this->any())
  535. ->method('getFileInfo')
  536. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
  537. $this->view->expects($this->never())
  538. ->method('rename');
  539. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  540. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  541. $this->root->expects($this->once())
  542. ->method('get')
  543. ->with('/bar')
  544. ->willReturn($parentNode);
  545. $node->move('/bar/asd');
  546. }
  547. public function testMoveNoParent() {
  548. $this->expectException(\OCP\Files\NotFoundException::class);
  549. /**
  550. * @var \OC\Files\Storage\Storage | \PHPUnit\Framework\MockObject\MockObject $storage
  551. */
  552. $storage = $this->createMock('\OC\Files\Storage\Storage');
  553. $storage->expects($this->never())
  554. ->method('rename');
  555. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  556. $this->root->expects($this->once())
  557. ->method('get')
  558. ->with('/bar')
  559. ->will($this->throwException(new NotFoundException()));
  560. $node->move('/bar/asd');
  561. }
  562. public function testMoveParentIsFile() {
  563. $this->expectException(\OCP\Files\NotPermittedException::class);
  564. $this->view->expects($this->never())
  565. ->method('rename');
  566. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  567. $parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar');
  568. $this->root->expects($this->once())
  569. ->method('get')
  570. ->with('/bar')
  571. ->willReturn($parentNode);
  572. $node->move('/bar/asd');
  573. }
  574. public function testMoveFailed() {
  575. $this->expectException(\OCP\Files\NotPermittedException::class);
  576. $this->view->expects($this->any())
  577. ->method('rename')
  578. ->with('/bar/foo', '/bar/asd')
  579. ->willReturn(false);
  580. $this->view->expects($this->any())
  581. ->method('getFileInfo')
  582. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1]));
  583. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  584. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  585. $this->root->expects($this->any())
  586. ->method('get')
  587. ->willReturnMap([['/bar', $parentNode], ['/bar/asd', $node]]);
  588. $node->move('/bar/asd');
  589. }
  590. public function testCopyFailed() {
  591. $this->expectException(\OCP\Files\NotPermittedException::class);
  592. $this->view->expects($this->any())
  593. ->method('copy')
  594. ->with('/bar/foo', '/bar/asd')
  595. ->willReturn(false);
  596. $this->view->expects($this->any())
  597. ->method('getFileInfo')
  598. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1]));
  599. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  600. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  601. $this->root->expects($this->any())
  602. ->method('get')
  603. ->willReturnMap([['/bar', $parentNode], ['/bar/asd', $node]]);
  604. $node->copy('/bar/asd');
  605. }
  606. }