1
0

NodeTest.php 22 KB

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