AvatarManagerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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-only
  6. */
  7. namespace Test\Avatar;
  8. use OC\Avatar\AvatarManager;
  9. use OC\Avatar\PlaceholderAvatar;
  10. use OC\Avatar\UserAvatar;
  11. use OC\KnownUser\KnownUserService;
  12. use OC\User\Manager;
  13. use OC\User\User;
  14. use OCP\Accounts\IAccount;
  15. use OCP\Accounts\IAccountManager;
  16. use OCP\Accounts\IAccountProperty;
  17. use OCP\Files\IAppData;
  18. use OCP\Files\SimpleFS\ISimpleFolder;
  19. use OCP\IConfig;
  20. use OCP\IL10N;
  21. use OCP\IUser;
  22. use OCP\IUserSession;
  23. use Psr\Log\LoggerInterface;
  24. /**
  25. * Class AvatarManagerTest
  26. */
  27. class AvatarManagerTest extends \Test\TestCase {
  28. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  29. private $userSession;
  30. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
  31. private $userManager;
  32. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  33. private $appData;
  34. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  35. private $l10n;
  36. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  37. private $logger;
  38. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  39. private $config;
  40. /** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */
  41. private $accountManager;
  42. /** @var AvatarManager | \PHPUnit\Framework\MockObject\MockObject */
  43. private $avatarManager;
  44. /** @var KnownUserService | \PHPUnit\Framework\MockObject\MockObject */
  45. private $knownUserService;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->userSession = $this->createMock(IUserSession::class);
  49. $this->userManager = $this->createMock(Manager::class);
  50. $this->appData = $this->createMock(IAppData::class);
  51. $this->l10n = $this->createMock(IL10N::class);
  52. $this->logger = $this->createMock(LoggerInterface::class);
  53. $this->config = $this->createMock(IConfig::class);
  54. $this->accountManager = $this->createMock(IAccountManager::class);
  55. $this->knownUserService = $this->createMock(KnownUserService::class);
  56. $this->avatarManager = new AvatarManager(
  57. $this->userSession,
  58. $this->userManager,
  59. $this->appData,
  60. $this->l10n,
  61. $this->logger,
  62. $this->config,
  63. $this->accountManager,
  64. $this->knownUserService
  65. );
  66. }
  67. public function testGetAvatarInvalidUser() {
  68. $this->expectException(\Exception::class);
  69. $this->expectExceptionMessage('user does not exist');
  70. $this->userManager
  71. ->expects($this->once())
  72. ->method('get')
  73. ->with('invalidUser')
  74. ->willReturn(null);
  75. $this->avatarManager->getAvatar('invalidUser');
  76. }
  77. public function testGetAvatarForSelf() {
  78. $user = $this->createMock(User::class);
  79. $user
  80. ->expects($this->any())
  81. ->method('getUID')
  82. ->willReturn('valid-user');
  83. $user
  84. ->expects($this->any())
  85. ->method('isEnabled')
  86. ->willReturn(true);
  87. // requesting user
  88. $this->userSession->expects($this->once())
  89. ->method('getUser')
  90. ->willReturn($user);
  91. $this->userManager
  92. ->expects($this->once())
  93. ->method('get')
  94. ->with('valid-user')
  95. ->willReturn($user);
  96. $account = $this->createMock(IAccount::class);
  97. $this->accountManager->expects($this->once())
  98. ->method('getAccount')
  99. ->with($user)
  100. ->willReturn($account);
  101. $property = $this->createMock(IAccountProperty::class);
  102. $account->expects($this->once())
  103. ->method('getProperty')
  104. ->with(IAccountManager::PROPERTY_AVATAR)
  105. ->willReturn($property);
  106. $property->expects($this->once())
  107. ->method('getScope')
  108. ->willReturn(IAccountManager::SCOPE_PRIVATE);
  109. $this->knownUserService->expects($this->any())
  110. ->method('isKnownToUser')
  111. ->with('valid-user', 'valid-user')
  112. ->willReturn(true);
  113. $folder = $this->createMock(ISimpleFolder::class);
  114. $this->appData
  115. ->expects($this->once())
  116. ->method('getFolder')
  117. ->with('valid-user')
  118. ->willReturn($folder);
  119. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  120. $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
  121. }
  122. public function testGetAvatarValidUserDifferentCasing() {
  123. $user = $this->createMock(User::class);
  124. $this->userManager->expects($this->once())
  125. ->method('get')
  126. ->with('vaLid-USER')
  127. ->willReturn($user);
  128. $user->expects($this->once())
  129. ->method('getUID')
  130. ->willReturn('valid-user');
  131. $user
  132. ->expects($this->any())
  133. ->method('isEnabled')
  134. ->willReturn(true);
  135. $this->userSession->expects($this->once())
  136. ->method('getUser')
  137. ->willReturn($user);
  138. $folder = $this->createMock(ISimpleFolder::class);
  139. $this->appData
  140. ->expects($this->once())
  141. ->method('getFolder')
  142. ->with('valid-user')
  143. ->willReturn($folder);
  144. $account = $this->createMock(IAccount::class);
  145. $this->accountManager->expects($this->once())
  146. ->method('getAccount')
  147. ->with($user)
  148. ->willReturn($account);
  149. $property = $this->createMock(IAccountProperty::class);
  150. $account->expects($this->once())
  151. ->method('getProperty')
  152. ->with(IAccountManager::PROPERTY_AVATAR)
  153. ->willReturn($property);
  154. $property->expects($this->once())
  155. ->method('getScope')
  156. ->willReturn(IAccountManager::SCOPE_FEDERATED);
  157. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  158. $this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER'));
  159. }
  160. public function dataGetAvatarScopes() {
  161. return [
  162. // public access cannot see real avatar
  163. [IAccountManager::SCOPE_PRIVATE, true, false, true],
  164. // unknown users cannot see real avatar
  165. [IAccountManager::SCOPE_PRIVATE, false, false, true],
  166. // known users can see real avatar
  167. [IAccountManager::SCOPE_PRIVATE, false, true, false],
  168. [IAccountManager::SCOPE_LOCAL, false, false, false],
  169. [IAccountManager::SCOPE_LOCAL, true, false, false],
  170. [IAccountManager::SCOPE_FEDERATED, false, false, false],
  171. [IAccountManager::SCOPE_FEDERATED, true, false, false],
  172. [IAccountManager::SCOPE_PUBLISHED, false, false, false],
  173. [IAccountManager::SCOPE_PUBLISHED, true, false, false],
  174. ];
  175. }
  176. /**
  177. * @dataProvider dataGetAvatarScopes
  178. */
  179. public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $expectedPlaceholder) {
  180. if ($isPublicCall) {
  181. $requestingUser = null;
  182. } else {
  183. $requestingUser = $this->createMock(IUser::class);
  184. $requestingUser->method('getUID')->willReturn('requesting-user');
  185. }
  186. // requesting user
  187. $this->userSession->expects($this->once())
  188. ->method('getUser')
  189. ->willReturn($requestingUser);
  190. $user = $this->createMock(User::class);
  191. $user
  192. ->expects($this->once())
  193. ->method('getUID')
  194. ->willReturn('valid-user');
  195. $user
  196. ->expects($this->any())
  197. ->method('isEnabled')
  198. ->willReturn(true);
  199. $this->userManager
  200. ->expects($this->once())
  201. ->method('get')
  202. ->with('valid-user')
  203. ->willReturn($user);
  204. $account = $this->createMock(IAccount::class);
  205. $this->accountManager->expects($this->once())
  206. ->method('getAccount')
  207. ->with($user)
  208. ->willReturn($account);
  209. $property = $this->createMock(IAccountProperty::class);
  210. $account->expects($this->once())
  211. ->method('getProperty')
  212. ->with(IAccountManager::PROPERTY_AVATAR)
  213. ->willReturn($property);
  214. $property->expects($this->once())
  215. ->method('getScope')
  216. ->willReturn($avatarScope);
  217. $folder = $this->createMock(ISimpleFolder::class);
  218. $this->appData
  219. ->expects($this->once())
  220. ->method('getFolder')
  221. ->with('valid-user')
  222. ->willReturn($folder);
  223. if (!$isPublicCall) {
  224. $this->knownUserService->expects($this->any())
  225. ->method('isKnownToUser')
  226. ->with('requesting-user', 'valid-user')
  227. ->willReturn($isKnownUser);
  228. } else {
  229. $this->knownUserService->expects($this->never())
  230. ->method('isKnownToUser');
  231. }
  232. if ($expectedPlaceholder) {
  233. $expected = new PlaceholderAvatar($folder, $user, $this->createMock(LoggerInterface::class));
  234. } else {
  235. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  236. }
  237. $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
  238. }
  239. }