AvatarManagerTest.php 8.5 KB

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