NodeTest.php 21 KB

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