1
0

RootTest.php 5.7 KB

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