1
0

FileTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. ->will($this->returnValue('bar'));
  43. $this->view->expects($this->once())
  44. ->method('getFileInfo')
  45. ->with('/bar/foo')
  46. ->will($this->returnValue($this->getFileInfo(array('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. /**
  51. * @expectedException \OCP\Files\NotPermittedException
  52. */
  53. public function testGetContentNotPermitted() {
  54. /** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
  55. $root = $this->getMockBuilder('\OC\Files\Node\Root')
  56. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  57. ->getMock();
  58. $root->expects($this->any())
  59. ->method('getUser')
  60. ->will($this->returnValue($this->user));
  61. $this->view->expects($this->once())
  62. ->method('getFileInfo')
  63. ->with('/bar/foo')
  64. ->will($this->returnValue($this->getFileInfo(array('permissions' => 0))));
  65. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  66. $node->getContent();
  67. }
  68. public function testPutContent() {
  69. /** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
  70. $root = $this->getMockBuilder('\OC\Files\Node\Root')
  71. ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
  72. ->getMock();
  73. $root->expects($this->any())
  74. ->method('getUser')
  75. ->will($this->returnValue($this->user));
  76. $this->view->expects($this->once())
  77. ->method('getFileInfo')
  78. ->with('/bar/foo')
  79. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
  80. $this->view->expects($this->once())
  81. ->method('file_put_contents')
  82. ->with('/bar/foo', 'bar')
  83. ->will($this->returnValue(true));
  84. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  85. $node->putContent('bar');
  86. }
  87. /**
  88. * @expectedException \OCP\Files\NotPermittedException
  89. */
  90. public function testPutContentNotPermitted() {
  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])
  94. ->getMock();
  95. $this->view->expects($this->once())
  96. ->method('getFileInfo')
  97. ->with('/bar/foo')
  98. ->will($this->returnValue($this->getFileInfo(array('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])
  106. ->getMock();
  107. $this->view->expects($this->once())
  108. ->method('getFileInfo')
  109. ->with('/bar/foo')
  110. ->will($this->returnValue($this->getFileInfo(array('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. );
  126. $hook = function ($file) {
  127. throw new \Exception('Hooks are not supposed to be called');
  128. };
  129. $root->listen('\OC\Files', 'preWrite', $hook);
  130. $root->listen('\OC\Files', 'postWrite', $hook);
  131. $this->view->expects($this->once())
  132. ->method('fopen')
  133. ->with('/bar/foo', 'r')
  134. ->will($this->returnValue($stream));
  135. $this->view->expects($this->once())
  136. ->method('getFileInfo')
  137. ->with('/bar/foo')
  138. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
  139. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  140. $fh = $node->fopen('r');
  141. $this->assertEquals($stream, $fh);
  142. $this->assertEquals('bar', fread($fh, 3));
  143. }
  144. public function testFOpenWrite() {
  145. $stream = fopen('php://memory', 'w+');
  146. $root = new \OC\Files\Node\Root(
  147. $this->manager,
  148. new $this->view,
  149. $this->user,
  150. $this->userMountCache,
  151. $this->logger,
  152. $this->userManager
  153. );
  154. $hooksCalled = 0;
  155. $hook = function ($file) use (&$hooksCalled) {
  156. $hooksCalled++;
  157. };
  158. $root->listen('\OC\Files', 'preWrite', $hook);
  159. $root->listen('\OC\Files', 'postWrite', $hook);
  160. $this->view->expects($this->once())
  161. ->method('fopen')
  162. ->with('/bar/foo', 'w')
  163. ->will($this->returnValue($stream));
  164. $this->view->expects($this->once())
  165. ->method('getFileInfo')
  166. ->with('/bar/foo')
  167. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
  168. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  169. $fh = $node->fopen('w');
  170. $this->assertEquals($stream, $fh);
  171. fwrite($fh, 'bar');
  172. rewind($fh);
  173. $this->assertEquals('bar', fread($stream, 3));
  174. $this->assertEquals(2, $hooksCalled);
  175. }
  176. /**
  177. * @expectedException \OCP\Files\NotPermittedException
  178. */
  179. public function testFOpenReadNotPermitted() {
  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. );
  188. $hook = function ($file) {
  189. throw new \Exception('Hooks are not supposed to be called');
  190. };
  191. $this->view->expects($this->once())
  192. ->method('getFileInfo')
  193. ->with('/bar/foo')
  194. ->will($this->returnValue($this->getFileInfo(array('permissions' => 0))));
  195. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  196. $node->fopen('r');
  197. }
  198. /**
  199. * @expectedException \OCP\Files\NotPermittedException
  200. */
  201. public function testFOpenReadWriteNoReadPermissions() {
  202. $root = new \OC\Files\Node\Root(
  203. $this->manager,
  204. $this->view,
  205. $this->user,
  206. $this->userMountCache,
  207. $this->logger,
  208. $this->userManager
  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. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_UPDATE))));
  217. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  218. $node->fopen('w');
  219. }
  220. /**
  221. * @expectedException \OCP\Files\NotPermittedException
  222. */
  223. public function testFOpenReadWriteNoWritePermissions() {
  224. $root = new \OC\Files\Node\Root(
  225. $this->manager,
  226. new $this->view,
  227. $this->user,
  228. $this->userMountCache,
  229. $this->logger,
  230. $this->userManager
  231. );
  232. $hook = function () {
  233. throw new \Exception('Hooks are not supposed to be called');
  234. };
  235. $this->view->expects($this->once())
  236. ->method('getFileInfo')
  237. ->with('/bar/foo')
  238. ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
  239. $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
  240. $node->fopen('w');
  241. }
  242. }