1
0

FileTest.php 8.8 KB

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