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\UserAvatar;
  26. use OC\Avatar\AvatarManager;
  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. public function setUp() {
  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. /**
  66. * @expectedException \Exception
  67. * @expectedExceptionMessage user does not exist
  68. */
  69. public function testGetAvatarInvalidUser() {
  70. $this->userManager
  71. ->expects($this->once())
  72. ->method('get')
  73. ->with('invalidUser')
  74. ->willReturn(null);
  75. $this->avatarManager->getAvatar('invalidUser');
  76. }
  77. public function testGetAvatarValidUser() {
  78. $user = $this->createMock(IUser::class);
  79. $user
  80. ->expects($this->once())
  81. ->method('getUID')
  82. ->willReturn('valid-user');
  83. $this->userManager
  84. ->expects($this->once())
  85. ->method('get')
  86. ->with('valid-user')
  87. ->willReturn($user);
  88. $folder = $this->createMock(ISimpleFolder::class);
  89. $this->appData
  90. ->expects($this->once())
  91. ->method('getFolder')
  92. ->with('valid-user')
  93. ->willReturn($folder);
  94. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  95. $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
  96. }
  97. public function testGetAvatarValidUserDifferentCasing() {
  98. $user = $this->createMock(IUser::class);
  99. $this->userManager->expects($this->once())
  100. ->method('get')
  101. ->with('vaLid-USER')
  102. ->willReturn($user);
  103. $user->expects($this->once())
  104. ->method('getUID')
  105. ->willReturn('valid-user');
  106. $folder = $this->createMock(ISimpleFolder::class);
  107. $this->appData
  108. ->expects($this->once())
  109. ->method('getFolder')
  110. ->with('valid-user')
  111. ->willReturn($folder);
  112. $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
  113. $this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER'));
  114. }
  115. }