RootTest.php 6.0 KB

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