RootTest.php 6.2 KB

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