1
0

FileTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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])
  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])
  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])
  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])
  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])
  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. );
  127. $hook = function ($file) {
  128. throw new \Exception('Hooks are not supposed to be called');
  129. };
  130. $root->listen('\OC\Files', 'preWrite', $hook);
  131. $root->listen('\OC\Files', 'postWrite', $hook);
  132. $this->view->expects($this->once())
  133. ->method('fopen')
  134. ->with('/bar/foo', 'r')
  135. ->willReturn($stream);
  136. $this->view->expects($this->once())
  137. ->method('getFileInfo')
  138. ->with('/bar/foo')
  139. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
  140. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  141. $fh = $node->fopen('r');
  142. $this->assertEquals($stream, $fh);
  143. $this->assertEquals('bar', fread($fh, 3));
  144. }
  145. public function testFOpenWrite() {
  146. $stream = fopen('php://memory', 'w+');
  147. $root = new \OC\Files\Node\Root(
  148. $this->manager,
  149. $this->view,
  150. $this->user,
  151. $this->userMountCache,
  152. $this->logger,
  153. $this->userManager,
  154. $this->eventDispatcher
  155. );
  156. $hooksCalled = 0;
  157. $hook = function ($file) use (&$hooksCalled) {
  158. $hooksCalled++;
  159. };
  160. $root->listen('\OC\Files', 'preWrite', $hook);
  161. $root->listen('\OC\Files', 'postWrite', $hook);
  162. $this->view->expects($this->once())
  163. ->method('fopen')
  164. ->with('/bar/foo', 'w')
  165. ->willReturn($stream);
  166. $this->view->expects($this->once())
  167. ->method('getFileInfo')
  168. ->with('/bar/foo')
  169. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
  170. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  171. $fh = $node->fopen('w');
  172. $this->assertEquals($stream, $fh);
  173. fwrite($fh, 'bar');
  174. rewind($fh);
  175. $this->assertEquals('bar', fread($stream, 3));
  176. $this->assertEquals(2, $hooksCalled);
  177. }
  178. public function testFOpenReadNotPermitted() {
  179. $this->expectException(\OCP\Files\NotPermittedException::class);
  180. $root = new \OC\Files\Node\Root(
  181. $this->manager,
  182. $this->view,
  183. $this->user,
  184. $this->userMountCache,
  185. $this->logger,
  186. $this->userManager,
  187. $this->eventDispatcher
  188. );
  189. $hook = function ($file) {
  190. throw new \Exception('Hooks are not supposed to be called');
  191. };
  192. $this->view->expects($this->once())
  193. ->method('getFileInfo')
  194. ->with('/bar/foo')
  195. ->willReturn($this->getFileInfo(['permissions' => 0]));
  196. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  197. $node->fopen('r');
  198. }
  199. public function testFOpenReadWriteNoReadPermissions() {
  200. $this->expectException(\OCP\Files\NotPermittedException::class);
  201. $root = new \OC\Files\Node\Root(
  202. $this->manager,
  203. $this->view,
  204. $this->user,
  205. $this->userMountCache,
  206. $this->logger,
  207. $this->userManager,
  208. $this->eventDispatcher
  209. );
  210. $hook = function () {
  211. throw new \Exception('Hooks are not supposed to be called');
  212. };
  213. $this->view->expects($this->once())
  214. ->method('getFileInfo')
  215. ->with('/bar/foo')
  216. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_UPDATE]));
  217. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  218. $node->fopen('w');
  219. }
  220. public function testFOpenReadWriteNoWritePermissions() {
  221. $this->expectException(\OCP\Files\NotPermittedException::class);
  222. $root = new \OC\Files\Node\Root(
  223. $this->manager,
  224. $this->view,
  225. $this->user,
  226. $this->userMountCache,
  227. $this->logger,
  228. $this->userManager,
  229. $this->eventDispatcher
  230. );
  231. $hook = function () {
  232. throw new \Exception('Hooks are not supposed to be called');
  233. };
  234. $this->view->expects($this->once())
  235. ->method('getFileInfo')
  236. ->with('/bar/foo')
  237. ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
  238. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  239. $node->fopen('w');
  240. }
  241. }