1
0

FileTest.php 8.1 KB

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