1
0

AvatarManagerTest.php 8.6 KB

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