NodeTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Files\Node;
  9. use OC\Files\FileInfo;
  10. use OC\Files\Mount\Manager;
  11. use OC\Files\View;
  12. use OCP\Files\Config\IUserMountCache;
  13. use OCP\Files\IRootFolder;
  14. use OCP\Files\Node;
  15. use OCP\Files\Storage;
  16. use OCP\IConfig;
  17. use OCP\ILogger;
  18. use OCP\IURLGenerator;
  19. use OCP\IUserManager;
  20. use OCP\Files\NotFoundException;
  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 ILogger|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $logger;
  39. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $userManager;
  41. protected function setUp() {
  42. parent::setUp();
  43. $config = $this->getMockBuilder(IConfig::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $urlGenerator = $this->getMockBuilder(IURLGenerator
  47. ::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->user = new \OC\User\User('', new \Test\Util\User\Dummy, null, $config, $urlGenerator);
  51. $this->manager = $this->getMockBuilder(Manager::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->view = $this->getMockBuilder(View::class)
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->userMountCache = $this->getMockBuilder('\OCP\Files\Config\IUserMountCache')
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->logger = $this->createMock(ILogger::class);
  61. $this->userManager = $this->createMock(IUserManager::class);
  62. $this->root = $this->getMockBuilder('\OC\Files\Node\Root')
  63. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  64. ->getMock();
  65. }
  66. /**
  67. * @param IRootFolder $root
  68. * @param View $view
  69. * @param string $path
  70. * @return Node
  71. */
  72. protected abstract function createTestNode($root, $view, $path);
  73. /**
  74. * @return string
  75. */
  76. protected abstract function getNodeClass();
  77. /**
  78. * @return string
  79. */
  80. protected abstract function getNonExistingNodeClass();
  81. /**
  82. * @return string
  83. */
  84. protected abstract function getViewDeleteMethod();
  85. protected function getMockStorage() {
  86. $storage = $this->getMockBuilder(Storage::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $storage->expects($this->any())
  90. ->method('getId')
  91. ->will($this->returnValue('home::someuser'));
  92. return $storage;
  93. }
  94. protected function getFileInfo($data) {
  95. return new FileInfo('', $this->getMockStorage(), '', $data, null);
  96. }
  97. public function testDelete() {
  98. $this->root->expects($this->exactly(2))
  99. ->method('emit')
  100. ->will($this->returnValue(true));
  101. $this->root->expects($this->any())
  102. ->method('getUser')
  103. ->will($this->returnValue($this->user));
  104. $this->view->expects($this->once())
  105. ->method('getFileInfo')
  106. ->with('/bar/foo')
  107. ->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])));
  108. $this->view->expects($this->once())
  109. ->method($this->getViewDeleteMethod())
  110. ->with('/bar/foo')
  111. ->will($this->returnValue(true));
  112. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  113. $node->delete();
  114. }
  115. public function testDeleteHooks() {
  116. $test = $this;
  117. $hooksRun = 0;
  118. /**
  119. * @param \OC\Files\Node\File $node
  120. */
  121. $preListener = function ($node) use (&$test, &$hooksRun) {
  122. $test->assertInstanceOf($this->getNodeClass(), $node);
  123. $test->assertEquals('foo', $node->getInternalPath());
  124. $test->assertEquals('/bar/foo', $node->getPath());
  125. $test->assertEquals(1, $node->getId());
  126. $hooksRun++;
  127. };
  128. /**
  129. * @param \OC\Files\Node\File $node
  130. */
  131. $postListener = function ($node) use (&$test, &$hooksRun) {
  132. $test->assertInstanceOf($this->getNonExistingNodeClass(), $node);
  133. $test->assertEquals('foo', $node->getInternalPath());
  134. $test->assertEquals('/bar/foo', $node->getPath());
  135. $test->assertEquals(1, $node->getId());
  136. $test->assertEquals('text/plain', $node->getMimeType());
  137. $hooksRun++;
  138. };
  139. $root = new \OC\Files\Node\Root(
  140. $this->manager,
  141. $this->view,
  142. $this->user,
  143. $this->userMountCache,
  144. $this->logger,
  145. $this->userManager
  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. ->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1, 'mimetype' => 'text/plain'])));
  153. $this->view->expects($this->once())
  154. ->method($this->getViewDeleteMethod())
  155. ->with('/bar/foo')
  156. ->will($this->returnValue(true));
  157. $this->view->expects($this->any())
  158. ->method('resolvePath')
  159. ->with('/bar/foo')
  160. ->will($this->returnValue([null, 'foo']));
  161. $node = $this->createTestNode($root, $this->view, '/bar/foo');
  162. $node->delete();
  163. $this->assertEquals(2, $hooksRun);
  164. }
  165. /**
  166. * @expectedException \OCP\Files\NotPermittedException
  167. */
  168. public function testDeleteNotPermitted() {
  169. $this->root->expects($this->any())
  170. ->method('getUser')
  171. ->will($this->returnValue($this->user));
  172. $this->view->expects($this->once())
  173. ->method('getFileInfo')
  174. ->with('/bar/foo')
  175. ->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])));
  176. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  177. $node->delete();
  178. }
  179. public function testStat() {
  180. $this->root->expects($this->any())
  181. ->method('getUser')
  182. ->will($this->returnValue($this->user));
  183. $stat = array(
  184. 'fileid' => 1,
  185. 'size' => 100,
  186. 'etag' => 'qwerty',
  187. 'mtime' => 50,
  188. 'permissions' => 0
  189. );
  190. $this->view->expects($this->once())
  191. ->method('stat')
  192. ->with('/bar/foo')
  193. ->will($this->returnValue($stat));
  194. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  195. $this->assertEquals($stat, $node->stat());
  196. }
  197. public function testGetId() {
  198. $this->root->expects($this->any())
  199. ->method('getUser')
  200. ->will($this->returnValue($this->user));
  201. $stat = $this->getFileInfo(array(
  202. 'fileid' => 1,
  203. 'size' => 100,
  204. 'etag' => 'qwerty',
  205. 'mtime' => 50
  206. ));
  207. $this->view->expects($this->once())
  208. ->method('getFileInfo')
  209. ->with('/bar/foo')
  210. ->will($this->returnValue($stat));
  211. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  212. $this->assertEquals(1, $node->getId());
  213. }
  214. public function testGetSize() {
  215. $this->root->expects($this->any())
  216. ->method('getUser')
  217. ->will($this->returnValue($this->user));
  218. $stat = $this->getFileInfo(array(
  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. ->will($this->returnValue($stat));
  228. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  229. $this->assertEquals(100, $node->getSize());
  230. }
  231. public function testGetEtag() {
  232. $this->root->expects($this->any())
  233. ->method('getUser')
  234. ->will($this->returnValue($this->user));
  235. $stat = $this->getFileInfo(array(
  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. ->will($this->returnValue($stat));
  245. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  246. $this->assertEquals('qwerty', $node->getEtag());
  247. }
  248. public function testGetMTime() {
  249. $this->root->expects($this->any())
  250. ->method('getUser')
  251. ->will($this->returnValue($this->user));
  252. $stat = $this->getFileInfo(array(
  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. ->will($this->returnValue($stat));
  262. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  263. $this->assertEquals(50, $node->getMTime());
  264. }
  265. public function testGetStorage() {
  266. $this->root->expects($this->any())
  267. ->method('getUser')
  268. ->will($this->returnValue($this->user));
  269. /**
  270. * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
  271. */
  272. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  273. ->disableOriginalConstructor()
  274. ->getMock();
  275. $this->view->expects($this->once())
  276. ->method('resolvePath')
  277. ->with('/bar/foo')
  278. ->will($this->returnValue(array($storage, 'foo')));
  279. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  280. $this->assertEquals($storage, $node->getStorage());
  281. }
  282. public function testGetPath() {
  283. $this->root->expects($this->any())
  284. ->method('getUser')
  285. ->will($this->returnValue($this->user));
  286. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  287. $this->assertEquals('/bar/foo', $node->getPath());
  288. }
  289. public function testGetInternalPath() {
  290. $this->root->expects($this->any())
  291. ->method('getUser')
  292. ->will($this->returnValue($this->user));
  293. /**
  294. * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
  295. */
  296. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  297. ->disableOriginalConstructor()
  298. ->getMock();
  299. $this->view->expects($this->once())
  300. ->method('resolvePath')
  301. ->with('/bar/foo')
  302. ->will($this->returnValue(array($storage, 'foo')));
  303. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  304. $this->assertEquals('foo', $node->getInternalPath());
  305. }
  306. public function testGetName() {
  307. $this->root->expects($this->any())
  308. ->method('getUser')
  309. ->will($this->returnValue($this->user));
  310. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  311. $this->assertEquals('foo', $node->getName());
  312. }
  313. public function testTouchSetMTime() {
  314. $this->root->expects($this->any())
  315. ->method('getUser')
  316. ->will($this->returnValue($this->user));
  317. $this->view->expects($this->once())
  318. ->method('touch')
  319. ->with('/bar/foo', 100)
  320. ->will($this->returnValue(true));
  321. $this->view->expects($this->once())
  322. ->method('getFileInfo')
  323. ->with('/bar/foo')
  324. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
  325. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  326. $node->touch(100);
  327. $this->assertEquals(100, $node->getMTime());
  328. }
  329. public function testTouchHooks() {
  330. $test = $this;
  331. $hooksRun = 0;
  332. /**
  333. * @param \OC\Files\Node\File $node
  334. */
  335. $preListener = function ($node) use (&$test, &$hooksRun) {
  336. $test->assertEquals('foo', $node->getInternalPath());
  337. $test->assertEquals('/bar/foo', $node->getPath());
  338. $hooksRun++;
  339. };
  340. /**
  341. * @param \OC\Files\Node\File $node
  342. */
  343. $postListener = function ($node) use (&$test, &$hooksRun) {
  344. $test->assertEquals('foo', $node->getInternalPath());
  345. $test->assertEquals('/bar/foo', $node->getPath());
  346. $hooksRun++;
  347. };
  348. $root = new \OC\Files\Node\Root(
  349. $this->manager,
  350. $this->view,
  351. $this->user,
  352. $this->userMountCache,
  353. $this->logger,
  354. $this->userManager
  355. );
  356. $root->listen('\OC\Files', 'preTouch', $preListener);
  357. $root->listen('\OC\Files', 'postTouch', $postListener);
  358. $this->view->expects($this->once())
  359. ->method('touch')
  360. ->with('/bar/foo', 100)
  361. ->will($this->returnValue(true));
  362. $this->view->expects($this->any())
  363. ->method('resolvePath')
  364. ->with('/bar/foo')
  365. ->will($this->returnValue(array(null, 'foo')));
  366. $this->view->expects($this->any())
  367. ->method('getFileInfo')
  368. ->with('/bar/foo')
  369. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
  370. $node = $this->createTestNode($root, $this->view, '/bar/foo');
  371. $node->touch(100);
  372. $this->assertEquals(2, $hooksRun);
  373. }
  374. /**
  375. * @expectedException \OCP\Files\NotPermittedException
  376. */
  377. public function testTouchNotPermitted() {
  378. $this->root->expects($this->any())
  379. ->method('getUser')
  380. ->will($this->returnValue($this->user));
  381. $this->view->expects($this->any())
  382. ->method('getFileInfo')
  383. ->with('/bar/foo')
  384. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
  385. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  386. $node->touch(100);
  387. }
  388. /**
  389. * @expectedException \OCP\Files\InvalidPathException
  390. */
  391. public function testInvalidPath() {
  392. $node = $this->createTestNode($this->root, $this->view, '/../foo');
  393. $node->getFileInfo();
  394. }
  395. public function testCopySameStorage() {
  396. $this->view->expects($this->any())
  397. ->method('copy')
  398. ->with('/bar/foo', '/bar/asd')
  399. ->will($this->returnValue(true));
  400. $this->view->expects($this->any())
  401. ->method('getFileInfo')
  402. ->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 3])));
  403. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  404. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  405. $newNode = $this->createTestNode($this->root, $this->view, '/bar/asd');
  406. $this->root->expects($this->exactly(2))
  407. ->method('get')
  408. ->will($this->returnValueMap([
  409. ['/bar/asd', $newNode],
  410. ['/bar', $parentNode]
  411. ]));
  412. $target = $node->copy('/bar/asd');
  413. $this->assertInstanceOf($this->getNodeClass(), $target);
  414. $this->assertEquals(3, $target->getId());
  415. }
  416. /**
  417. * @expectedException \OCP\Files\NotPermittedException
  418. */
  419. public function testCopyNotPermitted() {
  420. /**
  421. * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
  422. */
  423. $storage = $this->createMock('\OC\Files\Storage\Storage');
  424. $this->root->expects($this->never())
  425. ->method('getMount');
  426. $storage->expects($this->never())
  427. ->method('copy');
  428. $this->view->expects($this->any())
  429. ->method('getFileInfo')
  430. ->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ, 'fileid' => 3])));
  431. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  432. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  433. $this->root->expects($this->once())
  434. ->method('get')
  435. ->will($this->returnValueMap([
  436. ['/bar', $parentNode]
  437. ]));
  438. $node->copy('/bar/asd');
  439. }
  440. /**
  441. * @expectedException \OCP\Files\NotFoundException
  442. */
  443. public function testCopyNoParent() {
  444. $this->view->expects($this->never())
  445. ->method('copy');
  446. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  447. $this->root->expects($this->once())
  448. ->method('get')
  449. ->with('/bar/asd')
  450. ->will($this->throwException(new NotFoundException()));
  451. $node->copy('/bar/asd/foo');
  452. }
  453. /**
  454. * @expectedException \OCP\Files\NotPermittedException
  455. */
  456. public function testCopyParentIsFile() {
  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. ->will($this->returnValueMap([
  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. ->will($this->returnValue(true));
  473. $this->view->expects($this->any())
  474. ->method('getFileInfo')
  475. ->will($this->returnValue($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. ->will($this->returnValueMap([['/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])
  503. ->setMethods(['get'])
  504. ->getMock();
  505. $this->view->expects($this->any())
  506. ->method($viewMethod)
  507. ->with('/bar/foo', '/bar/asd')
  508. ->will($this->returnValue(true));
  509. $this->view->expects($this->any())
  510. ->method('getFileInfo')
  511. ->will($this->returnValue($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. ->will($this->returnValueMap([['/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. /**
  554. * @expectedException \OCP\Files\NotPermittedException
  555. */
  556. public function testMoveNotPermitted() {
  557. $this->view->expects($this->any())
  558. ->method('getFileInfo')
  559. ->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])));
  560. $this->view->expects($this->never())
  561. ->method('rename');
  562. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  563. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  564. $this->root->expects($this->once())
  565. ->method('get')
  566. ->with('/bar')
  567. ->will($this->returnValue($parentNode));
  568. $node->move('/bar/asd');
  569. }
  570. /**
  571. * @expectedException \OCP\Files\NotFoundException
  572. */
  573. public function testMoveNoParent() {
  574. /**
  575. * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
  576. */
  577. $storage = $this->createMock('\OC\Files\Storage\Storage');
  578. $storage->expects($this->never())
  579. ->method('rename');
  580. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  581. $this->root->expects($this->once())
  582. ->method('get')
  583. ->with('/bar')
  584. ->will($this->throwException(new NotFoundException()));
  585. $node->move('/bar/asd');
  586. }
  587. /**
  588. * @expectedException \OCP\Files\NotPermittedException
  589. */
  590. public function testMoveParentIsFile() {
  591. $this->view->expects($this->never())
  592. ->method('rename');
  593. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  594. $parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar');
  595. $this->root->expects($this->once())
  596. ->method('get')
  597. ->with('/bar')
  598. ->will($this->returnValue($parentNode));
  599. $node->move('/bar/asd');
  600. }
  601. /**
  602. * @expectedException \OCP\Files\NotPermittedException
  603. */
  604. public function testMoveFailed() {
  605. $this->view->expects($this->any())
  606. ->method('rename')
  607. ->with('/bar/foo', '/bar/asd')
  608. ->will($this->returnValue(false));
  609. $this->view->expects($this->any())
  610. ->method('getFileInfo')
  611. ->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])));
  612. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  613. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  614. $this->root->expects($this->any())
  615. ->method('get')
  616. ->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $node]]));
  617. $node->move('/bar/asd');
  618. }
  619. /**
  620. * @expectedException \OCP\Files\NotPermittedException
  621. */
  622. public function testCopyFailed() {
  623. $this->view->expects($this->any())
  624. ->method('copy')
  625. ->with('/bar/foo', '/bar/asd')
  626. ->will($this->returnValue(false));
  627. $this->view->expects($this->any())
  628. ->method('getFileInfo')
  629. ->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])));
  630. $node = $this->createTestNode($this->root, $this->view, '/bar/foo');
  631. $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
  632. $this->root->expects($this->any())
  633. ->method('get')
  634. ->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $node]]));
  635. $node->copy('/bar/asd');
  636. }
  637. }