1
0

RootTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Node;
  8. use OC\Files\FileInfo;
  9. use OC\Files\Mount\Manager;
  10. use OC\Files\Node\Folder;
  11. use OC\Files\View;
  12. use OC\Memcache\ArrayCache;
  13. use OCP\Cache\CappedMemoryCache;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\ICacheFactory;
  16. use OCP\IUser;
  17. use OCP\IUserManager;
  18. use Psr\Log\LoggerInterface;
  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 LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  32. private $logger;
  33. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  34. private $userManager;
  35. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  36. private $eventDispatcher;
  37. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $cacheFactory;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->user = $this->createMock(IUser::class);
  42. $this->manager = $this->getMockBuilder(Manager::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->userMountCache = $this->getMockBuilder('\OCP\Files\Config\IUserMountCache')
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->logger = $this->createMock(LoggerInterface::class);
  49. $this->userManager = $this->createMock(IUserManager::class);
  50. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  51. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  52. $this->cacheFactory->method('createLocal')
  53. ->willReturnCallback(function () {
  54. return new ArrayCache();
  55. });
  56. }
  57. /**
  58. * @return \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
  59. */
  60. protected function getRootViewMock() {
  61. $view = $this->createMock(View::class);
  62. $view->expects($this->any())
  63. ->method('getRoot')
  64. ->willReturn('');
  65. return $view;
  66. }
  67. protected function getFileInfo($data) {
  68. return new FileInfo('', null, '', $data, null);
  69. }
  70. public function testGet(): void {
  71. /**
  72. * @var \OC\Files\Storage\Storage $storage
  73. */
  74. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $view = $this->getRootViewMock();
  78. $root = new \OC\Files\Node\Root(
  79. $this->manager,
  80. $view,
  81. $this->user,
  82. $this->userMountCache,
  83. $this->logger,
  84. $this->userManager,
  85. $this->eventDispatcher,
  86. $this->cacheFactory,
  87. );
  88. $view->expects($this->once())
  89. ->method('getFileInfo')
  90. ->with('/bar/foo')
  91. ->willReturn($this->getFileInfo(['fileid' => 10, 'path' => 'bar/foo', 'name', 'mimetype' => 'text/plain']));
  92. $root->mount($storage, '');
  93. $node = $root->get('/bar/foo');
  94. $this->assertEquals(10, $node->getId());
  95. $this->assertInstanceOf('\OC\Files\Node\File', $node);
  96. }
  97. public function testGetNotFound(): void {
  98. $this->expectException(\OCP\Files\NotFoundException::class);
  99. /**
  100. * @var \OC\Files\Storage\Storage $storage
  101. */
  102. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $view = $this->getRootViewMock();
  106. $root = new \OC\Files\Node\Root(
  107. $this->manager,
  108. $view,
  109. $this->user,
  110. $this->userMountCache,
  111. $this->logger,
  112. $this->userManager,
  113. $this->eventDispatcher,
  114. $this->cacheFactory,
  115. );
  116. $view->expects($this->once())
  117. ->method('getFileInfo')
  118. ->with('/bar/foo')
  119. ->willReturn(false);
  120. $root->mount($storage, '');
  121. $root->get('/bar/foo');
  122. }
  123. public function testGetInvalidPath(): void {
  124. $this->expectException(\OCP\Files\NotPermittedException::class);
  125. $view = $this->getRootViewMock();
  126. $root = new \OC\Files\Node\Root(
  127. $this->manager,
  128. $view,
  129. $this->user,
  130. $this->userMountCache,
  131. $this->logger,
  132. $this->userManager,
  133. $this->eventDispatcher,
  134. $this->cacheFactory,
  135. );
  136. $root->get('/../foo');
  137. }
  138. public function testGetNoStorages(): void {
  139. $this->expectException(\OCP\Files\NotFoundException::class);
  140. $view = $this->getRootViewMock();
  141. $root = new \OC\Files\Node\Root(
  142. $this->manager,
  143. $view,
  144. $this->user,
  145. $this->userMountCache,
  146. $this->logger,
  147. $this->userManager,
  148. $this->eventDispatcher,
  149. $this->cacheFactory,
  150. );
  151. $root->get('/bar/foo');
  152. }
  153. public function testGetUserFolder(): void {
  154. $root = new \OC\Files\Node\Root(
  155. $this->manager,
  156. $this->getRootViewMock(),
  157. $this->user,
  158. $this->userMountCache,
  159. $this->logger,
  160. $this->userManager,
  161. $this->eventDispatcher,
  162. $this->cacheFactory,
  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. public function testGetUserFolderWithNoUserObj(): void {
  190. $this->expectException(\OC\User\NoUserException::class);
  191. $this->expectExceptionMessage('Backends provided no user object');
  192. $root = new \OC\Files\Node\Root(
  193. $this->createMock(Manager::class),
  194. $this->getRootViewMock(),
  195. null,
  196. $this->userMountCache,
  197. $this->logger,
  198. $this->userManager,
  199. $this->eventDispatcher,
  200. $this->cacheFactory,
  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. $this->anything()
  213. );
  214. $root->getUserFolder('NotExistingUser');
  215. }
  216. }