NodeTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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->expects($this->exactly(2))
  403. ->method('get')
  404. ->willReturnMap([
  405. ['/bar/asd', $newNode],
  406. ['/bar', $parentNode]
  407. ]);
  408. $target = $node->copy('/bar/asd');
  409. $this->assertInstanceOf($this->getNodeClass(), $target);
  410. $this->assertEquals(3, $target->getId());
  411. }
  412. public function testCopyNotPermitted() {
  413. $this->expectException(\OCP\Files\NotPermittedException::class);
  414. /**
  415. * @var \OC\Files\Storage\Storage | \PHPUnit\Framework\MockObject\MockObject $storage
  416. */
  417. $storage = $this->createMock('\OC\Files\Storage\Storage');
  418. $this->root->expects($this->never())
  419. ->method('getMount');
  420. $storage->expects($this->never())
  421. ->method('copy');
  422. $this->view->expects($this->any())
  423. ->method('getFileInfo')
  424. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ, 'fileid' => 3]));
  425. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  426. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  427. $this->root->expects($this->once())
  428. ->method('get')
  429. ->willReturnMap([
  430. ['/bar', $parentNode]
  431. ]);
  432. $node->copy('/bar/asd');
  433. }
  434. public function testCopyNoParent() {
  435. $this->expectException(\OCP\Files\NotFoundException::class);
  436. $this->view->expects($this->never())
  437. ->method('copy');
  438. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  439. $this->root->expects($this->once())
  440. ->method('get')
  441. ->with('/bar/asd')
  442. ->will($this->throwException(new NotFoundException()));
  443. $node->copy('/bar/asd/foo');
  444. }
  445. public function testCopyParentIsFile() {
  446. $this->expectException(\OCP\Files\NotPermittedException::class);
  447. $this->view->expects($this->never())
  448. ->method('copy');
  449. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  450. $parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar');
  451. $this->root->expects($this->once())
  452. ->method('get')
  453. ->willReturnMap([
  454. ['/bar', $parentNode]
  455. ]);
  456. $node->copy('/bar/asd');
  457. }
  458. public function testMoveSameStorage() {
  459. $this->view->expects($this->any())
  460. ->method('rename')
  461. ->with('/bar/foo', '/bar/asd')
  462. ->willReturn(true);
  463. $this->view->expects($this->any())
  464. ->method('getFileInfo')
  465. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1]));
  466. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  467. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  468. $this->root->expects($this->any())
  469. ->method('get')
  470. ->willReturnMap([['/bar', $parentNode], ['/bar/asd', $node]]);
  471. $target = $node->move('/bar/asd');
  472. $this->assertInstanceOf($this->getNodeClass(), $target);
  473. $this->assertEquals(1, $target->getId());
  474. $this->assertEquals('/bar/asd', $node->getPath());
  475. }
  476. public function moveOrCopyProvider() {
  477. return [
  478. ['move', 'rename', 'preRename', 'postRename'],
  479. ['copy', 'copy', 'preCopy', 'postCopy'],
  480. ];
  481. }
  482. /**
  483. * @dataProvider moveOrCopyProvider
  484. * @param string $operationMethod
  485. * @param string $viewMethod
  486. * @param string $preHookName
  487. * @param string $postHookName
  488. */
  489. public function testMoveCopyHooks($operationMethod, $viewMethod, $preHookName, $postHookName) {
  490. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject $root */
  491. $root = $this->getMockBuilder('\OC\Files\Node\Root')
  492. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher])
  493. ->setMethods(['get'])
  494. ->getMock();
  495. $this->view->expects($this->any())
  496. ->method($viewMethod)
  497. ->with('/bar/foo', '/bar/asd')
  498. ->willReturn(true);
  499. $this->view->expects($this->any())
  500. ->method('getFileInfo')
  501. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1]));
  502. /**
  503. * @var \OC\Files\Node\File|\PHPUnit\Framework\MockObject\MockObject $node
  504. */
  505. $node = $this->createTestNode($root, $this->view, '/bar/foo');
  506. $parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
  507. $targetTestNode = $this->createTestNode($root, $this->view, '/bar/asd');
  508. $root->expects($this->any())
  509. ->method('get')
  510. ->willReturnMap([['/bar', $parentNode], ['/bar/asd', $targetTestNode]]);
  511. $hooksRun = 0;
  512. $preListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node) {
  513. $this->assertSame($node, $sourceNode);
  514. $this->assertInstanceOf($this->getNodeClass(), $sourceNode);
  515. $this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode);
  516. $this->assertEquals('/bar/asd', $targetNode->getPath());
  517. $hooksRun++;
  518. };
  519. $postListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node, $targetTestNode) {
  520. $this->assertSame($node, $sourceNode);
  521. $this->assertNotSame($node, $targetNode);
  522. $this->assertSame($targetTestNode, $targetNode);
  523. $this->assertInstanceOf($this->getNodeClass(), $sourceNode);
  524. $this->assertInstanceOf($this->getNodeClass(), $targetNode);
  525. $hooksRun++;
  526. };
  527. $preWriteListener = function (Node $targetNode) use (&$hooksRun) {
  528. $this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode);
  529. $this->assertEquals('/bar/asd', $targetNode->getPath());
  530. $hooksRun++;
  531. };
  532. $postWriteListener = function (Node $targetNode) use (&$hooksRun, $targetTestNode) {
  533. $this->assertSame($targetTestNode, $targetNode);
  534. $hooksRun++;
  535. };
  536. $root->listen('\OC\Files', $preHookName, $preListener);
  537. $root->listen('\OC\Files', 'preWrite', $preWriteListener);
  538. $root->listen('\OC\Files', $postHookName, $postListener);
  539. $root->listen('\OC\Files', 'postWrite', $postWriteListener);
  540. $node->$operationMethod('/bar/asd');
  541. $this->assertEquals(4, $hooksRun);
  542. }
  543. public function testMoveNotPermitted() {
  544. $this->expectException(\OCP\Files\NotPermittedException::class);
  545. $this->view->expects($this->any())
  546. ->method('getFileInfo')
  547. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
  548. $this->view->expects($this->never())
  549. ->method('rename');
  550. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  551. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  552. $this->root->expects($this->once())
  553. ->method('get')
  554. ->with('/bar')
  555. ->willReturn($parentNode);
  556. $node->move('/bar/asd');
  557. }
  558. public function testMoveNoParent() {
  559. $this->expectException(\OCP\Files\NotFoundException::class);
  560. /**
  561. * @var \OC\Files\Storage\Storage | \PHPUnit\Framework\MockObject\MockObject $storage
  562. */
  563. $storage = $this->createMock('\OC\Files\Storage\Storage');
  564. $storage->expects($this->never())
  565. ->method('rename');
  566. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  567. $this->root->expects($this->once())
  568. ->method('get')
  569. ->with('/bar')
  570. ->will($this->throwException(new NotFoundException()));
  571. $node->move('/bar/asd');
  572. }
  573. public function testMoveParentIsFile() {
  574. $this->expectException(\OCP\Files\NotPermittedException::class);
  575. $this->view->expects($this->never())
  576. ->method('rename');
  577. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  578. $parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar');
  579. $this->root->expects($this->once())
  580. ->method('get')
  581. ->with('/bar')
  582. ->willReturn($parentNode);
  583. $node->move('/bar/asd');
  584. }
  585. public function testMoveFailed() {
  586. $this->expectException(\OCP\Files\NotPermittedException::class);
  587. $this->view->expects($this->any())
  588. ->method('rename')
  589. ->with('/bar/foo', '/bar/asd')
  590. ->willReturn(false);
  591. $this->view->expects($this->any())
  592. ->method('getFileInfo')
  593. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1]));
  594. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  595. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  596. $this->root->expects($this->any())
  597. ->method('get')
  598. ->willReturnMap([['/bar', $parentNode], ['/bar/asd', $node]]);
  599. $node->move('/bar/asd');
  600. }
  601. public function testCopyFailed() {
  602. $this->expectException(\OCP\Files\NotPermittedException::class);
  603. $this->view->expects($this->any())
  604. ->method('copy')
  605. ->with('/bar/foo', '/bar/asd')
  606. ->willReturn(false);
  607. $this->view->expects($this->any())
  608. ->method('getFileInfo')
  609. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1]));
  610. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  611. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  612. $this->root->expects($this->any())
  613. ->method('get')
  614. ->willReturnMap([['/bar', $parentNode], ['/bar/asd', $node]]);
  615. $node->copy('/bar/asd');
  616. }
  617. }