AvatarManagerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // requesting user
  100. $this->userSession->expects($this->once())
  101. ->method('getUser')
  102. ->willReturn($user);
  103. $this->userManager
  104. ->expects($this->once())
  105. ->method('get')
  106. ->with('valid-user')
  107. ->willReturn($user);
  108. $account = $this->createMock(IAccount::class);
  109. $this->accountManager->expects($this->once())
  110. ->method('getAccount')
  111. ->with($user)
  112. ->willReturn($account);
  113. $property = $this->createMock(IAccountProperty::class);
  114. $account->expects($this->once())
  115. ->method('getProperty')
  116. ->with(IAccountManager::PROPERTY_AVATAR)
  117. ->willReturn($property);
  118. $property->expects($this->once())
  119. ->method('getScope')
  120. ->willReturn(IAccountManager::SCOPE_PRIVATE);
  121. $this->knownUserService->expects($this->any())
  122. ->method('isKnownToUser')
  123. ->with('valid-user', 'valid-user')
  124. ->willReturn(true);
  125. $folder = $this->createMock(ISimpleFolder::class);
  126. $this->appData
  127. ->expects($this->once())
  128. ->method('getFolder')
  129. ->with('valid-user')
  130. ->willReturn($folder);
  131. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  132. $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
  133. }
  134. public function testGetAvatarValidUserDifferentCasing() {
  135. $user = $this->createMock(IUser::class);
  136. $this->userManager->expects($this->once())
  137. ->method('get')
  138. ->with('vaLid-USER')
  139. ->willReturn($user);
  140. $user->expects($this->once())
  141. ->method('getUID')
  142. ->willReturn('valid-user');
  143. $this->userSession->expects($this->once())
  144. ->method('getUser')
  145. ->willReturn($user);
  146. $folder = $this->createMock(ISimpleFolder::class);
  147. $this->appData
  148. ->expects($this->once())
  149. ->method('getFolder')
  150. ->with('valid-user')
  151. ->willReturn($folder);
  152. $account = $this->createMock(IAccount::class);
  153. $this->accountManager->expects($this->once())
  154. ->method('getAccount')
  155. ->with($user)
  156. ->willReturn($account);
  157. $property = $this->createMock(IAccountProperty::class);
  158. $account->expects($this->once())
  159. ->method('getProperty')
  160. ->with(IAccountManager::PROPERTY_AVATAR)
  161. ->willReturn($property);
  162. $property->expects($this->once())
  163. ->method('getScope')
  164. ->willReturn(IAccountManager::SCOPE_FEDERATED);
  165. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  166. $this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER'));
  167. }
  168. public function dataGetAvatarScopes() {
  169. return [
  170. // public access cannot see real avatar
  171. [IAccountManager::SCOPE_PRIVATE, true, false, true],
  172. // unknown users cannot see real avatar
  173. [IAccountManager::SCOPE_PRIVATE, false, false, true],
  174. // known users can see real avatar
  175. [IAccountManager::SCOPE_PRIVATE, false, true, false],
  176. [IAccountManager::SCOPE_LOCAL, false, false, false],
  177. [IAccountManager::SCOPE_LOCAL, true, false, false],
  178. [IAccountManager::SCOPE_FEDERATED, false, false, false],
  179. [IAccountManager::SCOPE_FEDERATED, true, false, false],
  180. [IAccountManager::SCOPE_PUBLISHED, false, false, false],
  181. [IAccountManager::SCOPE_PUBLISHED, true, false, false],
  182. ];
  183. }
  184. /**
  185. * @dataProvider dataGetAvatarScopes
  186. */
  187. public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $expectedPlaceholder) {
  188. if ($isPublicCall) {
  189. $requestingUser = null;
  190. } else {
  191. $requestingUser = $this->createMock(IUser::class);
  192. $requestingUser->method('getUID')->willReturn('requesting-user');
  193. }
  194. // requesting user
  195. $this->userSession->expects($this->once())
  196. ->method('getUser')
  197. ->willReturn($requestingUser);
  198. $user = $this->createMock(IUser::class);
  199. $user
  200. ->expects($this->once())
  201. ->method('getUID')
  202. ->willReturn('valid-user');
  203. $this->userManager
  204. ->expects($this->once())
  205. ->method('get')
  206. ->with('valid-user')
  207. ->willReturn($user);
  208. $account = $this->createMock(IAccount::class);
  209. $this->accountManager->expects($this->once())
  210. ->method('getAccount')
  211. ->with($user)
  212. ->willReturn($account);
  213. $property = $this->createMock(IAccountProperty::class);
  214. $account->expects($this->once())
  215. ->method('getProperty')
  216. ->with(IAccountManager::PROPERTY_AVATAR)
  217. ->willReturn($property);
  218. $property->expects($this->once())
  219. ->method('getScope')
  220. ->willReturn($avatarScope);
  221. $folder = $this->createMock(ISimpleFolder::class);
  222. $this->appData
  223. ->expects($this->once())
  224. ->method('getFolder')
  225. ->with('valid-user')
  226. ->willReturn($folder);
  227. if (!$isPublicCall) {
  228. $this->knownUserService->expects($this->any())
  229. ->method('isKnownToUser')
  230. ->with('requesting-user', 'valid-user')
  231. ->willReturn($isKnownUser);
  232. } else {
  233. $this->knownUserService->expects($this->never())
  234. ->method('isKnownToUser');
  235. }
  236. if ($expectedPlaceholder) {
  237. $expected = new PlaceholderAvatar($folder, $user, $this->createMock(LoggerInterface::class));
  238. } else {
  239. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  240. }
  241. $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
  242. }
  243. }