AvatarManagerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\UserAvatar;
  27. use OC\User\Manager;
  28. use OCP\Files\IAppData;
  29. use OCP\Files\SimpleFS\ISimpleFolder;
  30. use OCP\IConfig;
  31. use OCP\IL10N;
  32. use OCP\ILogger;
  33. use OCP\IUser;
  34. /**
  35. * Class AvatarManagerTest
  36. */
  37. class AvatarManagerTest extends \Test\TestCase {
  38. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
  39. private $userManager;
  40. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  41. private $appData;
  42. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  43. private $l10n;
  44. /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
  45. private $logger;
  46. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  47. private $config;
  48. /** @var AvatarManager | \PHPUnit\Framework\MockObject\MockObject */
  49. private $avatarManager;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->userManager = $this->createMock(Manager::class);
  53. $this->appData = $this->createMock(IAppData::class);
  54. $this->l10n = $this->createMock(IL10N::class);
  55. $this->logger = $this->createMock(ILogger::class);
  56. $this->config = $this->createMock(IConfig::class);
  57. $this->avatarManager = new AvatarManager(
  58. $this->userManager,
  59. $this->appData,
  60. $this->l10n,
  61. $this->logger,
  62. $this->config
  63. );
  64. }
  65. public function testGetAvatarInvalidUser() {
  66. $this->expectException(\Exception::class);
  67. $this->expectExceptionMessage('user does not exist');
  68. $this->userManager
  69. ->expects($this->once())
  70. ->method('get')
  71. ->with('invalidUser')
  72. ->willReturn(null);
  73. $this->avatarManager->getAvatar('invalidUser');
  74. }
  75. public function testGetAvatarValidUser() {
  76. $user = $this->createMock(IUser::class);
  77. $user
  78. ->expects($this->once())
  79. ->method('getUID')
  80. ->willReturn('valid-user');
  81. $this->userManager
  82. ->expects($this->once())
  83. ->method('get')
  84. ->with('valid-user')
  85. ->willReturn($user);
  86. $folder = $this->createMock(ISimpleFolder::class);
  87. $this->appData
  88. ->expects($this->once())
  89. ->method('getFolder')
  90. ->with('valid-user')
  91. ->willReturn($folder);
  92. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  93. $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
  94. }
  95. public function testGetAvatarValidUserDifferentCasing() {
  96. $user = $this->createMock(IUser::class);
  97. $this->userManager->expects($this->once())
  98. ->method('get')
  99. ->with('vaLid-USER')
  100. ->willReturn($user);
  101. $user->expects($this->once())
  102. ->method('getUID')
  103. ->willReturn('valid-user');
  104. $folder = $this->createMock(ISimpleFolder::class);
  105. $this->appData
  106. ->expects($this->once())
  107. ->method('getFolder')
  108. ->with('valid-user')
  109. ->willReturn($folder);
  110. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  111. $this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER'));
  112. }
  113. }