NodeTest.php 21 KB

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