RootTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. use OC\Cache\CappedMemoryCache;
  10. use OC\Files\FileInfo;
  11. use OC\Files\Mount\Manager;
  12. use OC\Files\Node\Folder;
  13. use OC\Files\View;
  14. use OCP\ILogger;
  15. use OCP\IUser;
  16. use OCP\IUserManager;
  17. /**
  18. * Class RootTest
  19. *
  20. * @package Test\Files\Node
  21. */
  22. class RootTest extends \Test\TestCase {
  23. /** @var \OC\User\User */
  24. private $user;
  25. /** @var \OC\Files\Mount\Manager */
  26. private $manager;
  27. /** @var \OCP\Files\Config\IUserMountCache|\PHPUnit_Framework_MockObject_MockObject */
  28. private $userMountCache;
  29. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  30. private $logger;
  31. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  32. private $userManager;
  33. protected function setUp() {
  34. parent::setUp();
  35. $config = $this->getMockBuilder('\OCP\IConfig')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $urlgenerator = $this->getMockBuilder('\OCP\IURLGenerator')
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->user = new \OC\User\User('', new \Test\Util\User\Dummy, null, $config, $urlgenerator);
  42. $this->manager = $this->getMockBuilder('\OC\Files\Mount\Manager')
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->userMountCache = $this->getMockBuilder('\OCP\Files\Config\IUserMountCache')
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->logger = $this->createMock(ILogger::class);
  49. $this->userManager = $this->createMock(IUserManager::class);
  50. }
  51. protected function getFileInfo($data) {
  52. return new FileInfo('', null, '', $data, null);
  53. }
  54. public function testGet() {
  55. /**
  56. * @var \OC\Files\Storage\Storage $storage
  57. */
  58. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. /**
  62. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  63. */
  64. $view = $this->getMockBuilder('\OC\Files\View')
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $root = new \OC\Files\Node\Root(
  68. $this->manager,
  69. $view,
  70. $this->user,
  71. $this->userMountCache,
  72. $this->logger,
  73. $this->userManager
  74. );
  75. $view->expects($this->once())
  76. ->method('getFileInfo')
  77. ->with('/bar/foo')
  78. ->will($this->returnValue($this->getFileInfo(array('fileid' => 10, 'path' => 'bar/foo', 'name', 'mimetype' => 'text/plain'))));
  79. $root->mount($storage, '');
  80. $node = $root->get('/bar/foo');
  81. $this->assertEquals(10, $node->getId());
  82. $this->assertInstanceOf('\OC\Files\Node\File', $node);
  83. }
  84. /**
  85. * @expectedException \OCP\Files\NotFoundException
  86. */
  87. public function testGetNotFound() {
  88. /**
  89. * @var \OC\Files\Storage\Storage $storage
  90. */
  91. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. /**
  95. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  96. */
  97. $view = $this->getMockBuilder('\OC\Files\View')
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $root = new \OC\Files\Node\Root(
  101. $this->manager,
  102. $view,
  103. $this->user,
  104. $this->userMountCache,
  105. $this->logger,
  106. $this->userManager
  107. );
  108. $view->expects($this->once())
  109. ->method('getFileInfo')
  110. ->with('/bar/foo')
  111. ->will($this->returnValue(false));
  112. $root->mount($storage, '');
  113. $root->get('/bar/foo');
  114. }
  115. /**
  116. * @expectedException \OCP\Files\NotPermittedException
  117. */
  118. public function testGetInvalidPath() {
  119. /**
  120. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  121. */
  122. $view = $this->getMockBuilder('\OC\Files\View')
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $root = new \OC\Files\Node\Root(
  126. $this->manager,
  127. $view,
  128. $this->user,
  129. $this->userMountCache,
  130. $this->logger,
  131. $this->userManager
  132. );
  133. $root->get('/../foo');
  134. }
  135. /**
  136. * @expectedException \OCP\Files\NotFoundException
  137. */
  138. public function testGetNoStorages() {
  139. /**
  140. * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
  141. */
  142. $view = $this->getMockBuilder('\OC\Files\View')
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $root = new \OC\Files\Node\Root(
  146. $this->manager,
  147. $view,
  148. $this->user,
  149. $this->userMountCache,
  150. $this->logger,
  151. $this->userManager
  152. );
  153. $root->get('/bar/foo');
  154. }
  155. public function testGetUserFolder() {
  156. $root = new \OC\Files\Node\Root(
  157. $this->manager,
  158. $this->createMock(View::class),
  159. $this->user,
  160. $this->userMountCache,
  161. $this->logger,
  162. $this->userManager
  163. );
  164. $user = $this->createMock(IUser::class);
  165. $user
  166. ->expects($this->once())
  167. ->method('getUID')
  168. ->willReturn('MyUserId');
  169. $this->userManager
  170. ->expects($this->once())
  171. ->method('get')
  172. ->with('MyUserId')
  173. ->willReturn($user);
  174. /** @var CappedMemoryCache|\PHPUnit_Framework_MockObject_MockObject $cappedMemoryCache */
  175. $cappedMemoryCache = $this->createMock(CappedMemoryCache::class);
  176. $cappedMemoryCache
  177. ->expects($this->once())
  178. ->method('hasKey')
  179. ->willReturn(true);
  180. $folder = $this->createMock(Folder::class);
  181. $cappedMemoryCache
  182. ->expects($this->once())
  183. ->method('get')
  184. ->with('MyUserId')
  185. ->willReturn($folder);
  186. $this->invokePrivate($root, 'userFolderCache', [$cappedMemoryCache]);
  187. $this->assertEquals($folder, $root->getUserFolder('MyUserId'));
  188. }
  189. /**
  190. * @expectedException \OC\User\NoUserException
  191. * @expectedExceptionMessage Backends provided no user object
  192. */
  193. public function testGetUserFolderWithNoUserObj() {
  194. $root = new \OC\Files\Node\Root(
  195. $this->createMock(Manager::class),
  196. $this->createMock(View::class),
  197. null,
  198. $this->userMountCache,
  199. $this->logger,
  200. $this->userManager
  201. );
  202. $this->userManager
  203. ->expects($this->once())
  204. ->method('get')
  205. ->with('NotExistingUser')
  206. ->willReturn(null);
  207. $this->logger
  208. ->expects($this->once())
  209. ->method('error')
  210. ->with(
  211. 'Backends provided no user object for NotExistingUser',
  212. [
  213. 'app' => 'files',
  214. ]
  215. );
  216. $root->getUserFolder('NotExistingUser');
  217. }
  218. }