1
0

NodeTest.php 22 KB

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