AvatarManagerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * @author Roeland Jago Douma <rullzer@owncloud.com>
  4. * @author Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace Test\Avatar;
  25. use OC\Avatar\AvatarManager;
  26. use OC\Avatar\PlaceholderAvatar;
  27. use OC\Avatar\UserAvatar;
  28. use OC\KnownUser\KnownUserService;
  29. use OC\User\Manager;
  30. use OCP\Accounts\IAccount;
  31. use OCP\Accounts\IAccountManager;
  32. use OCP\Accounts\IAccountProperty;
  33. use OCP\Files\IAppData;
  34. use OCP\Files\SimpleFS\ISimpleFolder;
  35. use OCP\IConfig;
  36. use OCP\IL10N;
  37. use OCP\IUser;
  38. use OC\User\User;
  39. use OCP\IUserSession;
  40. use Psr\Log\LoggerInterface;
  41. /**
  42. * Class AvatarManagerTest
  43. */
  44. class AvatarManagerTest extends \Test\TestCase {
  45. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  46. private $userSession;
  47. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
  48. private $userManager;
  49. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  50. private $appData;
  51. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  52. private $l10n;
  53. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  54. private $logger;
  55. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  56. private $config;
  57. /** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */
  58. private $accountManager;
  59. /** @var AvatarManager | \PHPUnit\Framework\MockObject\MockObject */
  60. private $avatarManager;
  61. /** @var KnownUserService | \PHPUnit\Framework\MockObject\MockObject */
  62. private $knownUserService;
  63. protected function setUp(): void {
  64. parent::setUp();
  65. $this->userSession = $this->createMock(IUserSession::class);
  66. $this->userManager = $this->createMock(Manager::class);
  67. $this->appData = $this->createMock(IAppData::class);
  68. $this->l10n = $this->createMock(IL10N::class);
  69. $this->logger = $this->createMock(LoggerInterface::class);
  70. $this->config = $this->createMock(IConfig::class);
  71. $this->accountManager = $this->createMock(IAccountManager::class);
  72. $this->knownUserService = $this->createMock(KnownUserService::class);
  73. $this->avatarManager = new AvatarManager(
  74. $this->userSession,
  75. $this->userManager,
  76. $this->appData,
  77. $this->l10n,
  78. $this->logger,
  79. $this->config,
  80. $this->accountManager,
  81. $this->knownUserService
  82. );
  83. }
  84. public function testGetAvatarInvalidUser() {
  85. $this->expectException(\Exception::class);
  86. $this->expectExceptionMessage('user does not exist');
  87. $this->userManager
  88. ->expects($this->once())
  89. ->method('get')
  90. ->with('invalidUser')
  91. ->willReturn(null);
  92. $this->avatarManager->getAvatar('invalidUser');
  93. }
  94. public function testGetAvatarForSelf() {
  95. $user = $this->createMock(User::class);
  96. $user
  97. ->expects($this->any())
  98. ->method('getUID')
  99. ->willReturn('valid-user');
  100. // requesting user
  101. $this->userSession->expects($this->once())
  102. ->method('getUser')
  103. ->willReturn($user);
  104. $this->userManager
  105. ->expects($this->once())
  106. ->method('get')
  107. ->with('valid-user')
  108. ->willReturn($user);
  109. $account = $this->createMock(IAccount::class);
  110. $this->accountManager->expects($this->once())
  111. ->method('getAccount')
  112. ->with($user)
  113. ->willReturn($account);
  114. $property = $this->createMock(IAccountProperty::class);
  115. $account->expects($this->once())
  116. ->method('getProperty')
  117. ->with(IAccountManager::PROPERTY_AVATAR)
  118. ->willReturn($property);
  119. $property->expects($this->once())
  120. ->method('getScope')
  121. ->willReturn(IAccountManager::SCOPE_PRIVATE);
  122. $this->knownUserService->expects($this->any())
  123. ->method('isKnownToUser')
  124. ->with('valid-user', 'valid-user')
  125. ->willReturn(true);
  126. $folder = $this->createMock(ISimpleFolder::class);
  127. $this->appData
  128. ->expects($this->once())
  129. ->method('getFolder')
  130. ->with('valid-user')
  131. ->willReturn($folder);
  132. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  133. $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
  134. }
  135. public function testGetAvatarValidUserDifferentCasing() {
  136. $user = $this->createMock(User::class);
  137. $this->userManager->expects($this->once())
  138. ->method('get')
  139. ->with('vaLid-USER')
  140. ->willReturn($user);
  141. $user->expects($this->once())
  142. ->method('getUID')
  143. ->willReturn('valid-user');
  144. $this->userSession->expects($this->once())
  145. ->method('getUser')
  146. ->willReturn($user);
  147. $folder = $this->createMock(ISimpleFolder::class);
  148. $this->appData
  149. ->expects($this->once())
  150. ->method('getFolder')
  151. ->with('valid-user')
  152. ->willReturn($folder);
  153. $account = $this->createMock(IAccount::class);
  154. $this->accountManager->expects($this->once())
  155. ->method('getAccount')
  156. ->with($user)
  157. ->willReturn($account);
  158. $property = $this->createMock(IAccountProperty::class);
  159. $account->expects($this->once())
  160. ->method('getProperty')
  161. ->with(IAccountManager::PROPERTY_AVATAR)
  162. ->willReturn($property);
  163. $property->expects($this->once())
  164. ->method('getScope')
  165. ->willReturn(IAccountManager::SCOPE_FEDERATED);
  166. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  167. $this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER'));
  168. }
  169. public function dataGetAvatarScopes() {
  170. return [
  171. // public access cannot see real avatar
  172. [IAccountManager::SCOPE_PRIVATE, true, false, true],
  173. // unknown users cannot see real avatar
  174. [IAccountManager::SCOPE_PRIVATE, false, false, true],
  175. // known users can see real avatar
  176. [IAccountManager::SCOPE_PRIVATE, false, true, false],
  177. [IAccountManager::SCOPE_LOCAL, false, false, false],
  178. [IAccountManager::SCOPE_LOCAL, true, false, false],
  179. [IAccountManager::SCOPE_FEDERATED, false, false, false],
  180. [IAccountManager::SCOPE_FEDERATED, true, false, false],
  181. [IAccountManager::SCOPE_PUBLISHED, false, false, false],
  182. [IAccountManager::SCOPE_PUBLISHED, true, false, false],
  183. ];
  184. }
  185. /**
  186. * @dataProvider dataGetAvatarScopes
  187. */
  188. public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $expectedPlaceholder) {
  189. if ($isPublicCall) {
  190. $requestingUser = null;
  191. } else {
  192. $requestingUser = $this->createMock(IUser::class);
  193. $requestingUser->method('getUID')->willReturn('requesting-user');
  194. }
  195. // requesting user
  196. $this->userSession->expects($this->once())
  197. ->method('getUser')
  198. ->willReturn($requestingUser);
  199. $user = $this->createMock(User::class);
  200. $user
  201. ->expects($this->once())
  202. ->method('getUID')
  203. ->willReturn('valid-user');
  204. $this->userManager
  205. ->expects($this->once())
  206. ->method('get')
  207. ->with('valid-user')
  208. ->willReturn($user);
  209. $account = $this->createMock(IAccount::class);
  210. $this->accountManager->expects($this->once())
  211. ->method('getAccount')
  212. ->with($user)
  213. ->willReturn($account);
  214. $property = $this->createMock(IAccountProperty::class);
  215. $account->expects($this->once())
  216. ->method('getProperty')
  217. ->with(IAccountManager::PROPERTY_AVATAR)
  218. ->willReturn($property);
  219. $property->expects($this->once())
  220. ->method('getScope')
  221. ->willReturn($avatarScope);
  222. $folder = $this->createMock(ISimpleFolder::class);
  223. $this->appData
  224. ->expects($this->once())
  225. ->method('getFolder')
  226. ->with('valid-user')
  227. ->willReturn($folder);
  228. if (!$isPublicCall) {
  229. $this->knownUserService->expects($this->any())
  230. ->method('isKnownToUser')
  231. ->with('requesting-user', 'valid-user')
  232. ->willReturn($isKnownUser);
  233. } else {
  234. $this->knownUserService->expects($this->never())
  235. ->method('isKnownToUser');
  236. }
  237. if ($expectedPlaceholder) {
  238. $expected = new PlaceholderAvatar($folder, $user, $this->createMock(LoggerInterface::class));
  239. } else {
  240. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  241. }
  242. $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
  243. }
  244. }