FileTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. /**
  10. * Class FileTest
  11. *
  12. * @group DB
  13. *
  14. * @package Test\Files\Node
  15. */
  16. class FileTest extends NodeTest {
  17. protected function createTestNode($root, $view, $path, array $data = [], $internalPath = '', $storage = null) {
  18. if ($data || $internalPath || $storage) {
  19. return new \OC\Files\Node\File($root, $view, $path, $this->getFileInfo($data, $internalPath, $storage));
  20. } else {
  21. return new \OC\Files\Node\File($root, $view, $path);
  22. }
  23. }
  24. protected function getNodeClass() {
  25. return '\OC\Files\Node\File';
  26. }
  27. protected function getNonExistingNodeClass() {
  28. return '\OC\Files\Node\NonExistingFile';
  29. }
  30. protected function getViewDeleteMethod() {
  31. return 'unlink';
  32. }
  33. public function testGetContent() {
  34. /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
  35. $root = $this->getMockBuilder('\OC\Files\Node\Root')
  36. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory])
  37. ->getMock();
  38. $hook = function ($file) {
  39. throw new \Exception('Hooks are not supposed to be called');
  40. };
  41. $root->listen('\OC\Files', 'preWrite', $hook);
  42. $root->listen('\OC\Files', 'postWrite', $hook);
  43. $this->view->expects($this->once())
  44. ->method('file_get_contents')
  45. ->with('/bar/foo')
  46. ->willReturn('bar');
  47. $this->view->expects($this->once())
  48. ->method('getFileInfo')
  49. ->with('/bar/foo')
  50. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
  51. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  52. $this->assertEquals('bar', $node->getContent());
  53. }
  54. public function testGetContentNotPermitted() {
  55. $this->expectException(\OCP\Files\NotPermittedException::class);
  56. /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
  57. $root = $this->getMockBuilder('\OC\Files\Node\Root')
  58. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory])
  59. ->getMock();
  60. $root->expects($this->any())
  61. ->method('getUser')
  62. ->willReturn($this->user);
  63. $this->view->expects($this->once())
  64. ->method('getFileInfo')
  65. ->with('/bar/foo')
  66. ->willReturn($this->getFileInfo(['permissions' => 0]));
  67. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  68. $node->getContent();
  69. }
  70. public function testPutContent() {
  71. /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
  72. $root = $this->getMockBuilder('\OC\Files\Node\Root')
  73. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory])
  74. ->getMock();
  75. $root->expects($this->any())
  76. ->method('getUser')
  77. ->willReturn($this->user);
  78. $this->view->expects($this->once())
  79. ->method('getFileInfo')
  80. ->with('/bar/foo')
  81. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
  82. $this->view->expects($this->once())
  83. ->method('file_put_contents')
  84. ->with('/bar/foo', 'bar')
  85. ->willReturn(true);
  86. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  87. $node->putContent('bar');
  88. }
  89. public function testPutContentNotPermitted() {
  90. $this->expectException(\OCP\Files\NotPermittedException::class);
  91. /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
  92. $root = $this->getMockBuilder('\OC\Files\Node\Root')
  93. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory])
  94. ->getMock();
  95. $this->view->expects($this->once())
  96. ->method('getFileInfo')
  97. ->with('/bar/foo')
  98. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
  99. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  100. $node->putContent('bar');
  101. }
  102. public function testGetMimeType() {
  103. /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
  104. $root = $this->getMockBuilder('\OC\Files\Node\Root')
  105. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory])
  106. ->getMock();
  107. $this->view->expects($this->once())
  108. ->method('getFileInfo')
  109. ->with('/bar/foo')
  110. ->willReturn($this->getFileInfo(['mimetype' => 'text/plain']));
  111. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  112. $this->assertEquals('text/plain', $node->getMimeType());
  113. }
  114. public function testFOpenRead() {
  115. $stream = fopen('php://memory', 'w+');
  116. fwrite($stream, 'bar');
  117. rewind($stream);
  118. $root = new \OC\Files\Node\Root(
  119. $this->manager,
  120. $this->view,
  121. $this->user,
  122. $this->userMountCache,
  123. $this->logger,
  124. $this->userManager,
  125. $this->eventDispatcher,
  126. $this->cacheFactory,
  127. );
  128. $hook = function ($file) {
  129. throw new \Exception('Hooks are not supposed to be called');
  130. };
  131. $root->listen('\OC\Files', 'preWrite', $hook);
  132. $root->listen('\OC\Files', 'postWrite', $hook);
  133. $this->view->expects($this->once())
  134. ->method('fopen')
  135. ->with('/bar/foo', 'r')
  136. ->willReturn($stream);
  137. $this->view->expects($this->once())
  138. ->method('getFileInfo')
  139. ->with('/bar/foo')
  140. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
  141. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  142. $fh = $node->fopen('r');
  143. $this->assertEquals($stream, $fh);
  144. $this->assertEquals('bar', fread($fh, 3));
  145. }
  146. public function testFOpenWrite() {
  147. $stream = fopen('php://memory', 'w+');
  148. $root = new \OC\Files\Node\Root(
  149. $this->manager,
  150. $this->view,
  151. $this->user,
  152. $this->userMountCache,
  153. $this->logger,
  154. $this->userManager,
  155. $this->eventDispatcher,
  156. $this->cacheFactory,
  157. );
  158. $hooksCalled = 0;
  159. $hook = function ($file) use (&$hooksCalled) {
  160. $hooksCalled++;
  161. };
  162. $root->listen('\OC\Files', 'preWrite', $hook);
  163. $root->listen('\OC\Files', 'postWrite', $hook);
  164. $this->view->expects($this->once())
  165. ->method('fopen')
  166. ->with('/bar/foo', 'w')
  167. ->willReturn($stream);
  168. $this->view->expects($this->once())
  169. ->method('getFileInfo')
  170. ->with('/bar/foo')
  171. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
  172. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  173. $fh = $node->fopen('w');
  174. $this->assertEquals($stream, $fh);
  175. fwrite($fh, 'bar');
  176. rewind($fh);
  177. $this->assertEquals('bar', fread($stream, 3));
  178. $this->assertEquals(2, $hooksCalled);
  179. }
  180. public function testFOpenReadNotPermitted() {
  181. $this->expectException(\OCP\Files\NotPermittedException::class);
  182. $root = new \OC\Files\Node\Root(
  183. $this->manager,
  184. $this->view,
  185. $this->user,
  186. $this->userMountCache,
  187. $this->logger,
  188. $this->userManager,
  189. $this->eventDispatcher,
  190. $this->cacheFactory,
  191. );
  192. $hook = function ($file) {
  193. throw new \Exception('Hooks are not supposed to be called');
  194. };
  195. $this->view->expects($this->once())
  196. ->method('getFileInfo')
  197. ->with('/bar/foo')
  198. ->willReturn($this->getFileInfo(['permissions' => 0]));
  199. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  200. $node->fopen('r');
  201. }
  202. public function testFOpenReadWriteNoReadPermissions() {
  203. $this->expectException(\OCP\Files\NotPermittedException::class);
  204. $root = new \OC\Files\Node\Root(
  205. $this->manager,
  206. $this->view,
  207. $this->user,
  208. $this->userMountCache,
  209. $this->logger,
  210. $this->userManager,
  211. $this->eventDispatcher,
  212. $this->cacheFactory,
  213. );
  214. $hook = function () {
  215. throw new \Exception('Hooks are not supposed to be called');
  216. };
  217. $this->view->expects($this->once())
  218. ->method('getFileInfo')
  219. ->with('/bar/foo')
  220. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_UPDATE]));
  221. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  222. $node->fopen('w');
  223. }
  224. public function testFOpenReadWriteNoWritePermissions() {
  225. $this->expectException(\OCP\Files\NotPermittedException::class);
  226. $root = new \OC\Files\Node\Root(
  227. $this->manager,
  228. $this->view,
  229. $this->user,
  230. $this->userMountCache,
  231. $this->logger,
  232. $this->userManager,
  233. $this->eventDispatcher,
  234. $this->cacheFactory,
  235. );
  236. $hook = function () {
  237. throw new \Exception('Hooks are not supposed to be called');
  238. };
  239. $this->view->expects($this->once())
  240. ->method('getFileInfo')
  241. ->with('/bar/foo')
  242. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
  243. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  244. $node->fopen('w');
  245. }
  246. }