RootTest.php 5.7 KB

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