You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.3 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022, Vitor Mattos <vitor@php.rio>
  5. *
  6. * @author Vitor Mattos <vitor@php.rio>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Talk\Tests\php\Service;
  25. use OC\EmojiHelper;
  26. use OCA\Talk\Room;
  27. use OCA\Talk\Service\AvatarService;
  28. use OCA\Talk\Service\RoomService;
  29. use OCP\Files\IAppData;
  30. use OCP\IAvatarManager;
  31. use OCP\IL10N;
  32. use OCP\IURLGenerator;
  33. use OCP\Security\ISecureRandom;
  34. use OCP\Server;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Test\TestCase;
  37. /**
  38. * @group DB
  39. */
  40. class AvatarServiceTest extends TestCase {
  41. protected IAppData&MockObject $appData;
  42. protected IL10N&MockObject $l;
  43. protected IURLGenerator&MockObject $url;
  44. protected ISecureRandom&MockObject $random;
  45. protected RoomService&MockObject $roomService;
  46. protected IAvatarManager&MockObject $avatarManager;
  47. protected EmojiHelper $emojiHelper;
  48. protected ?AvatarService $service = null;
  49. public function setUp(): void {
  50. parent::setUp();
  51. $this->appData = $this->createMock(IAppData::class);
  52. $this->l = $this->createMock(IL10N::class);
  53. $this->url = $this->createMock(IURLGenerator::class);
  54. $this->random = $this->createMock(ISecureRandom::class);
  55. $this->roomService = $this->createMock(RoomService::class);
  56. $this->avatarManager = $this->createMock(IAvatarManager::class);
  57. $this->emojiHelper = Server::get(EmojiHelper::class);
  58. $this->service = new AvatarService(
  59. $this->appData,
  60. $this->l,
  61. $this->url,
  62. $this->random,
  63. $this->roomService,
  64. $this->avatarManager,
  65. $this->emojiHelper,
  66. );
  67. }
  68. public static function dataGetAvatarVersion(): array {
  69. return [
  70. ['', 'STRING WITH 8 CHARS'],
  71. ['1', '1'],
  72. ['1.png', '1'],
  73. ];
  74. }
  75. /**
  76. * @dataProvider dataGetAvatarVersion
  77. */
  78. public function testGetAvatarVersion(string $avatar, string $expected): void {
  79. /** @var Room&MockObject $room */
  80. $room = $this->createMock(Room::class);
  81. $room->method('getAvatar')
  82. ->willReturn($avatar);
  83. $actual = $this->service->getAvatarVersion($room);
  84. if ($expected === 'STRING WITH 8 CHARS') {
  85. $this->assertEquals(8, strlen($actual));
  86. } else {
  87. $this->assertEquals($expected, $actual);
  88. }
  89. }
  90. public static function dataGetFirstCombinedEmoji(): array {
  91. return [
  92. ['👋 Hello', '👋'],
  93. ['Only leading emojis 🚀', ''],
  94. ['👩🏽‍💻👩🏻‍💻👨🏿‍💻 Only one, but with all attributes', '👩🏽‍💻'],
  95. ];
  96. }
  97. /**
  98. * @dataProvider dataGetFirstCombinedEmoji
  99. */
  100. public function testGetFirstCombinedEmoji(string $roomName, string $avatarEmoji): void {
  101. $this->assertSame($avatarEmoji, self::invokePrivate($this->service, 'getFirstCombinedEmoji', [$roomName]));
  102. }
  103. }