diff options
author | Joas Schilling <213943+nickvergessen@users.noreply.github.com> | 2022-04-29 19:31:29 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-29 19:31:29 +0300 |
commit | 0d873357510a1a6d57a6b8fa54b027b2eb8e558b (patch) | |
tree | 90da8c4baebf4e2dacc76b02cfa5cd0e8a529cb6 | |
parent | 2d938ab8dcaf0455f0c0fd67cc27b8fae186326f (diff) | |
parent | a75a22f3ac3384ffd52e4642c05fbd3d35f94a3d (diff) |
Merge pull request #31703 from nextcloud/techdebt/noid/emojihelper-interface
Emojihelper interface
-rw-r--r-- | apps/user_status/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | apps/user_status/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | apps/user_status/lib/Capabilities.php | 17 | ||||
-rw-r--r-- | apps/user_status/lib/Service/StatusService.php | 19 | ||||
-rw-r--r-- | apps/user_status/tests/Unit/CapabilitiesTest.php | 12 | ||||
-rw-r--r-- | apps/user_status/tests/Unit/Service/StatusServiceTest.php | 16 | ||||
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 3 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 3 | ||||
-rw-r--r-- | lib/private/Comments/EmojiHelper.php | 101 | ||||
-rw-r--r-- | lib/private/Comments/Manager.php | 9 | ||||
-rw-r--r-- | lib/private/EmojiHelper.php (renamed from apps/user_status/lib/Service/EmojiService.php) | 28 | ||||
-rw-r--r-- | lib/private/Server.php | 2 | ||||
-rw-r--r-- | lib/public/IEmojiHelper.php | 39 | ||||
-rw-r--r-- | tests/lib/Comments/ManagerTest.php | 2 | ||||
-rw-r--r-- | tests/lib/EmojiHelperTest.php (renamed from apps/user_status/tests/Unit/Service/EmojiServiceTest.php) | 24 |
15 files changed, 91 insertions, 186 deletions
diff --git a/apps/user_status/composer/composer/autoload_classmap.php b/apps/user_status/composer/composer/autoload_classmap.php index 64da04fb8ba..9988231d76e 100644 --- a/apps/user_status/composer/composer/autoload_classmap.php +++ b/apps/user_status/composer/composer/autoload_classmap.php @@ -31,7 +31,6 @@ return array( 'OCA\\UserStatus\\Migration\\Version0002Date20200902144824' => $baseDir . '/../lib/Migration/Version0002Date20200902144824.php', 'OCA\\UserStatus\\Migration\\Version1000Date20201111130204' => $baseDir . '/../lib/Migration/Version1000Date20201111130204.php', 'OCA\\UserStatus\\Migration\\Version2301Date20210809144824' => $baseDir . '/../lib/Migration/Version2301Date20210809144824.php', - 'OCA\\UserStatus\\Service\\EmojiService' => $baseDir . '/../lib/Service/EmojiService.php', 'OCA\\UserStatus\\Service\\JSDataService' => $baseDir . '/../lib/Service/JSDataService.php', 'OCA\\UserStatus\\Service\\PredefinedStatusService' => $baseDir . '/../lib/Service/PredefinedStatusService.php', 'OCA\\UserStatus\\Service\\StatusService' => $baseDir . '/../lib/Service/StatusService.php', diff --git a/apps/user_status/composer/composer/autoload_static.php b/apps/user_status/composer/composer/autoload_static.php index c4ee7d031b5..1ad125615ea 100644 --- a/apps/user_status/composer/composer/autoload_static.php +++ b/apps/user_status/composer/composer/autoload_static.php @@ -46,7 +46,6 @@ class ComposerStaticInitUserStatus 'OCA\\UserStatus\\Migration\\Version0002Date20200902144824' => __DIR__ . '/..' . '/../lib/Migration/Version0002Date20200902144824.php', 'OCA\\UserStatus\\Migration\\Version1000Date20201111130204' => __DIR__ . '/..' . '/../lib/Migration/Version1000Date20201111130204.php', 'OCA\\UserStatus\\Migration\\Version2301Date20210809144824' => __DIR__ . '/..' . '/../lib/Migration/Version2301Date20210809144824.php', - 'OCA\\UserStatus\\Service\\EmojiService' => __DIR__ . '/..' . '/../lib/Service/EmojiService.php', 'OCA\\UserStatus\\Service\\JSDataService' => __DIR__ . '/..' . '/../lib/Service/JSDataService.php', 'OCA\\UserStatus\\Service\\PredefinedStatusService' => __DIR__ . '/..' . '/../lib/Service/PredefinedStatusService.php', 'OCA\\UserStatus\\Service\\StatusService' => __DIR__ . '/..' . '/../lib/Service/StatusService.php', diff --git a/apps/user_status/lib/Capabilities.php b/apps/user_status/lib/Capabilities.php index f5b35896a56..8f26525286c 100644 --- a/apps/user_status/lib/Capabilities.php +++ b/apps/user_status/lib/Capabilities.php @@ -25,8 +25,8 @@ declare(strict_types=1); */ namespace OCA\UserStatus; -use OCA\UserStatus\Service\EmojiService; use OCP\Capabilities\ICapability; +use OCP\IEmojiHelper; /** * Class Capabilities @@ -34,17 +34,10 @@ use OCP\Capabilities\ICapability; * @package OCA\UserStatus */ class Capabilities implements ICapability { + private IEmojiHelper $emojiHelper; - /** @var EmojiService */ - private $emojiService; - - /** - * Capabilities constructor. - * - * @param EmojiService $emojiService - */ - public function __construct(EmojiService $emojiService) { - $this->emojiService = $emojiService; + public function __construct(IEmojiHelper $emojiHelper) { + $this->emojiHelper = $emojiHelper; } /** @@ -54,7 +47,7 @@ class Capabilities implements ICapability { return [ 'user_status' => [ 'enabled' => true, - 'supports_emoji' => $this->emojiService->doesPlatformSupportEmoji(), + 'supports_emoji' => $this->emojiHelper->doesPlatformSupportEmoji(), ], ]; } diff --git a/apps/user_status/lib/Service/StatusService.php b/apps/user_status/lib/Service/StatusService.php index 5dd70e4ea5e..e038570fc00 100644 --- a/apps/user_status/lib/Service/StatusService.php +++ b/apps/user_status/lib/Service/StatusService.php @@ -37,6 +37,7 @@ use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Utility\ITimeFactory; use OCP\DB\Exception; use OCP\IConfig; +use OCP\IEmojiHelper; use OCP\IUser; use OCP\UserStatus\IUserStatus; @@ -56,8 +57,7 @@ class StatusService { /** @var PredefinedStatusService */ private $predefinedStatusService; - /** @var EmojiService */ - private $emojiService; + private IEmojiHelper $emojiHelper; /** @var bool */ private $shareeEnumeration; @@ -95,24 +95,15 @@ class StatusService { /** @var int */ public const MAXIMUM_MESSAGE_LENGTH = 80; - /** - * StatusService constructor. - * - * @param UserStatusMapper $mapper - * @param ITimeFactory $timeFactory - * @param PredefinedStatusService $defaultStatusService - * @param EmojiService $emojiService - * @param IConfig $config - */ public function __construct(UserStatusMapper $mapper, ITimeFactory $timeFactory, PredefinedStatusService $defaultStatusService, - EmojiService $emojiService, + IEmojiHelper $emojiHelper, IConfig $config) { $this->mapper = $mapper; $this->timeFactory = $timeFactory; $this->predefinedStatusService = $defaultStatusService; - $this->emojiService = $emojiService; + $this->emojiHelper = $emojiHelper; $this->shareeEnumeration = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes'; $this->shareeEnumerationPhone = $this->shareeEnumeration && $config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes'; @@ -334,7 +325,7 @@ class StatusService { } // Check if statusIcon contains only one character - if ($statusIcon !== null && !$this->emojiService->isValidEmoji($statusIcon)) { + if ($statusIcon !== null && !$this->emojiHelper->isValidSingleEmoji($statusIcon)) { throw new InvalidStatusIconException('Status-Icon is longer than one character'); } // Check for maximum length of custom message diff --git a/apps/user_status/tests/Unit/CapabilitiesTest.php b/apps/user_status/tests/Unit/CapabilitiesTest.php index af36900d3bc..02874fcbf6d 100644 --- a/apps/user_status/tests/Unit/CapabilitiesTest.php +++ b/apps/user_status/tests/Unit/CapabilitiesTest.php @@ -26,13 +26,13 @@ declare(strict_types=1); namespace OCA\UserStatus\Tests; use OCA\UserStatus\Capabilities; -use OCA\UserStatus\Service\EmojiService; +use OCP\IEmojiHelper; use Test\TestCase; class CapabilitiesTest extends TestCase { - /** @var EmojiService|\PHPUnit\Framework\MockObject\MockObject */ - private $emojiService; + /** @var IEmojiHelper|\PHPUnit\Framework\MockObject\MockObject */ + private $emojiHelper; /** @var Capabilities */ private $capabilities; @@ -40,8 +40,8 @@ class CapabilitiesTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->emojiService = $this->createMock(EmojiService::class); - $this->capabilities = new Capabilities($this->emojiService); + $this->emojiHelper = $this->createMock(IEmojiHelper::class); + $this->capabilities = new Capabilities($this->emojiHelper); } /** @@ -50,7 +50,7 @@ class CapabilitiesTest extends TestCase { * @dataProvider getCapabilitiesDataProvider */ public function testGetCapabilities(bool $supportsEmojis): void { - $this->emojiService->expects($this->once()) + $this->emojiHelper->expects($this->once()) ->method('doesPlatformSupportEmoji') ->willReturn($supportsEmojis); diff --git a/apps/user_status/tests/Unit/Service/StatusServiceTest.php b/apps/user_status/tests/Unit/Service/StatusServiceTest.php index 3dd397e1d36..df31cf0d5ad 100644 --- a/apps/user_status/tests/Unit/Service/StatusServiceTest.php +++ b/apps/user_status/tests/Unit/Service/StatusServiceTest.php @@ -35,13 +35,13 @@ use OCA\UserStatus\Exception\InvalidMessageIdException; use OCA\UserStatus\Exception\InvalidStatusIconException; use OCA\UserStatus\Exception\InvalidStatusTypeException; use OCA\UserStatus\Exception\StatusMessageTooLongException; -use OCA\UserStatus\Service\EmojiService; use OCA\UserStatus\Service\PredefinedStatusService; use OCA\UserStatus\Service\StatusService; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Utility\ITimeFactory; use OCP\DB\Exception; use OCP\IConfig; +use OCP\IEmojiHelper; use OCP\UserStatus\IUserStatus; use Test\TestCase; @@ -56,8 +56,8 @@ class StatusServiceTest extends TestCase { /** @var PredefinedStatusService|\PHPUnit\Framework\MockObject\MockObject */ private $predefinedStatusService; - /** @var EmojiService|\PHPUnit\Framework\MockObject\MockObject */ - private $emojiService; + /** @var IEmojiHelper|\PHPUnit\Framework\MockObject\MockObject */ + private $emojiHelper; /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ private $config; @@ -71,7 +71,7 @@ class StatusServiceTest extends TestCase { $this->mapper = $this->createMock(UserStatusMapper::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->predefinedStatusService = $this->createMock(PredefinedStatusService::class); - $this->emojiService = $this->createMock(EmojiService::class); + $this->emojiHelper = $this->createMock(IEmojiHelper::class); $this->config = $this->createMock(IConfig::class); @@ -84,7 +84,7 @@ class StatusServiceTest extends TestCase { $this->service = new StatusService($this->mapper, $this->timeFactory, $this->predefinedStatusService, - $this->emojiService, + $this->emojiHelper, $this->config); } @@ -138,7 +138,7 @@ class StatusServiceTest extends TestCase { $this->service = new StatusService($this->mapper, $this->timeFactory, $this->predefinedStatusService, - $this->emojiService, + $this->emojiHelper, $this->config); $this->assertEquals([], $this->service->findAllRecentStatusChanges(20, 50)); @@ -155,7 +155,7 @@ class StatusServiceTest extends TestCase { $this->service = new StatusService($this->mapper, $this->timeFactory, $this->predefinedStatusService, - $this->emojiService, + $this->emojiHelper, $this->config); $this->assertEquals([], $this->service->findAllRecentStatusChanges(20, 50)); @@ -519,7 +519,7 @@ class StatusServiceTest extends TestCase { ->willThrowException(new DoesNotExistException('')); } - $this->emojiService->method('isValidEmoji') + $this->emojiHelper->method('isValidSingleEmoji') ->with($statusIcon) ->willReturn($supportsEmoji); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index f96641b17c8..2f056457cfd 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -411,6 +411,7 @@ return array( 'OCP\\IDBConnection' => $baseDir . '/lib/public/IDBConnection.php', 'OCP\\IDateTimeFormatter' => $baseDir . '/lib/public/IDateTimeFormatter.php', 'OCP\\IDateTimeZone' => $baseDir . '/lib/public/IDateTimeZone.php', + 'OCP\\IEmojiHelper' => $baseDir . '/lib/public/IEmojiHelper.php', 'OCP\\IEventSource' => $baseDir . '/lib/public/IEventSource.php', 'OCP\\IGroup' => $baseDir . '/lib/public/IGroup.php', 'OCP\\IGroupManager' => $baseDir . '/lib/public/IGroupManager.php', @@ -821,7 +822,6 @@ return array( 'OC\\Command\\FileAccess' => $baseDir . '/lib/private/Command/FileAccess.php', 'OC\\Command\\QueueBus' => $baseDir . '/lib/private/Command/QueueBus.php', 'OC\\Comments\\Comment' => $baseDir . '/lib/private/Comments/Comment.php', - 'OC\\Comments\\EmojiHelper' => $baseDir . '/lib/private/Comments/EmojiHelper.php', 'OC\\Comments\\Manager' => $baseDir . '/lib/private/Comments/Manager.php', 'OC\\Comments\\ManagerFactory' => $baseDir . '/lib/private/Comments/ManagerFactory.php', 'OC\\Config' => $baseDir . '/lib/private/Config.php', @@ -1088,6 +1088,7 @@ return array( 'OC\\Diagnostics\\QueryLogger' => $baseDir . '/lib/private/Diagnostics/QueryLogger.php', 'OC\\DirectEditing\\Manager' => $baseDir . '/lib/private/DirectEditing/Manager.php', 'OC\\DirectEditing\\Token' => $baseDir . '/lib/private/DirectEditing/Token.php', + 'OC\\EmojiHelper' => $baseDir . '/lib/private/EmojiHelper.php', 'OC\\Encryption\\DecryptAll' => $baseDir . '/lib/private/Encryption/DecryptAll.php', 'OC\\Encryption\\EncryptionWrapper' => $baseDir . '/lib/private/Encryption/EncryptionWrapper.php', 'OC\\Encryption\\Exceptions\\DecryptionFailedException' => $baseDir . '/lib/private/Encryption/Exceptions/DecryptionFailedException.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 0661fd4f77c..95668560261 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -440,6 +440,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\IDBConnection' => __DIR__ . '/../../..' . '/lib/public/IDBConnection.php', 'OCP\\IDateTimeFormatter' => __DIR__ . '/../../..' . '/lib/public/IDateTimeFormatter.php', 'OCP\\IDateTimeZone' => __DIR__ . '/../../..' . '/lib/public/IDateTimeZone.php', + 'OCP\\IEmojiHelper' => __DIR__ . '/../../..' . '/lib/public/IEmojiHelper.php', 'OCP\\IEventSource' => __DIR__ . '/../../..' . '/lib/public/IEventSource.php', 'OCP\\IGroup' => __DIR__ . '/../../..' . '/lib/public/IGroup.php', 'OCP\\IGroupManager' => __DIR__ . '/../../..' . '/lib/public/IGroupManager.php', @@ -850,7 +851,6 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Command\\FileAccess' => __DIR__ . '/../../..' . '/lib/private/Command/FileAccess.php', 'OC\\Command\\QueueBus' => __DIR__ . '/../../..' . '/lib/private/Command/QueueBus.php', 'OC\\Comments\\Comment' => __DIR__ . '/../../..' . '/lib/private/Comments/Comment.php', - 'OC\\Comments\\EmojiHelper' => __DIR__ . '/../../..' . '/lib/private/Comments/EmojiHelper.php', 'OC\\Comments\\Manager' => __DIR__ . '/../../..' . '/lib/private/Comments/Manager.php', 'OC\\Comments\\ManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Comments/ManagerFactory.php', 'OC\\Config' => __DIR__ . '/../../..' . '/lib/private/Config.php', @@ -1117,6 +1117,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Diagnostics\\QueryLogger' => __DIR__ . '/../../..' . '/lib/private/Diagnostics/QueryLogger.php', 'OC\\DirectEditing\\Manager' => __DIR__ . '/../../..' . '/lib/private/DirectEditing/Manager.php', 'OC\\DirectEditing\\Token' => __DIR__ . '/../../..' . '/lib/private/DirectEditing/Token.php', + 'OC\\EmojiHelper' => __DIR__ . '/../../..' . '/lib/private/EmojiHelper.php', 'OC\\Encryption\\DecryptAll' => __DIR__ . '/../../..' . '/lib/private/Encryption/DecryptAll.php', 'OC\\Encryption\\EncryptionWrapper' => __DIR__ . '/../../..' . '/lib/private/Encryption/EncryptionWrapper.php', 'OC\\Encryption\\Exceptions\\DecryptionFailedException' => __DIR__ . '/../../..' . '/lib/private/Encryption/Exceptions/DecryptionFailedException.php', diff --git a/lib/private/Comments/EmojiHelper.php b/lib/private/Comments/EmojiHelper.php deleted file mode 100644 index a75919edbc9..00000000000 --- a/lib/private/Comments/EmojiHelper.php +++ /dev/null @@ -1,101 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @copyright Copyright (c) 2020, Georg Ehrke - * - * @author Georg Ehrke <oc.list@georgehrke.com> - * @author Joas Schilling <coding@schilljs.com> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ -namespace OC\Comments; - -use OCP\IDBConnection; - -/** - * Copied OCA\UserStatus\Service\EmojiService - * Needs to be unified later - */ -class EmojiHelper { - - /** @var IDBConnection */ - private $db; - - /** - * EmojiService constructor. - * - * @param IDBConnection $db - */ - public function __construct(IDBConnection $db) { - $this->db = $db; - } - - /** - * @return bool - */ - public function doesPlatformSupportEmoji(): bool { - return $this->db->supports4ByteText() && - \class_exists(\IntlBreakIterator::class); - } - - /** - * @param string $emoji - * @return bool - */ - public function isValidEmoji(string $emoji): bool { - $intlBreakIterator = \IntlBreakIterator::createCharacterInstance(); - $intlBreakIterator->setText($emoji); - - $characterCount = 0; - while ($intlBreakIterator->next() !== \IntlBreakIterator::DONE) { - $characterCount++; - } - - if ($characterCount !== 1) { - return false; - } - - $codePointIterator = \IntlBreakIterator::createCodePointInstance(); - $codePointIterator->setText($emoji); - - foreach ($codePointIterator->getPartsIterator() as $codePoint) { - $codePointType = \IntlChar::charType($codePoint); - - // If the current code-point is an emoji or a modifier (like a skin-tone) - // just continue and check the next character - if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL || - $codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER || - $codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL || - $codePointType === \IntlChar::CHAR_CATEGORY_GENERAL_OTHER_TYPES) { - continue; - } - - // If it's neither a modifier nor an emoji, we only allow - // a zero-width-joiner or a variation selector 16 - $codePointValue = \IntlChar::ord($codePoint); - if ($codePointValue === 8205 || $codePointValue === 65039) { - continue; - } - - return false; - } - - return true; - } -} diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index abbe4c66c95..3275658d555 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -39,6 +39,7 @@ use OCP\Comments\NotFoundException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IConfig; use OCP\IDBConnection; +use OCP\IEmojiHelper; use OCP\IUser; use OCP\IInitialStateService; use OCP\PreConditionNotMetException; @@ -59,7 +60,7 @@ class Manager implements ICommentsManager { /** @var ITimeFactory */ protected $timeFactory; - /** @var EmojiHelper */ + /** @var IEmojiHelper */ protected $emojiHelper; /** @var IInitialStateService */ @@ -81,7 +82,7 @@ class Manager implements ICommentsManager { LoggerInterface $logger, IConfig $config, ITimeFactory $timeFactory, - EmojiHelper $emojiHelper, + IEmojiHelper $emojiHelper, IInitialStateService $initialStateService) { $this->dbConn = $dbConn; $this->logger = $logger; @@ -153,7 +154,7 @@ class Manager implements ICommentsManager { throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving'); } - if ($comment->getVerb() === 'reaction' && !$this->emojiHelper->isValidEmoji($comment->getMessage())) { + if ($comment->getVerb() === 'reaction' && !$this->emojiHelper->isValidSingleEmoji($comment->getMessage())) { // 4 characters: laptop + person + gender + skin color => "🧑🏽💻" is a single emoji from the picker throw new \UnexpectedValueException('Reactions can only be a single emoji'); } @@ -1074,7 +1075,7 @@ class Manager implements ICommentsManager { * @since 24.0.0 */ public function supportReactions(): bool { - return $this->dbConn->supports4ByteText(); + return $this->emojiHelper->doesPlatformSupportEmoji(); } /** diff --git a/apps/user_status/lib/Service/EmojiService.php b/lib/private/EmojiHelper.php index 0f197933872..7a4580a1bdb 100644 --- a/apps/user_status/lib/Service/EmojiService.php +++ b/lib/private/EmojiHelper.php @@ -24,42 +24,24 @@ declare(strict_types=1); * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ -namespace OCA\UserStatus\Service; +namespace OC; use OCP\IDBConnection; +use OCP\IEmojiHelper; -/** - * Class EmojiService - * - * @package OCA\UserStatus\Service - */ -class EmojiService { - - /** @var IDBConnection */ - private $db; +class EmojiHelper implements IEmojiHelper { + private IDBConnection $db; - /** - * EmojiService constructor. - * - * @param IDBConnection $db - */ public function __construct(IDBConnection $db) { $this->db = $db; } - /** - * @return bool - */ public function doesPlatformSupportEmoji(): bool { return $this->db->supports4ByteText() && \class_exists(\IntlBreakIterator::class); } - /** - * @param string $emoji - * @return bool - */ - public function isValidEmoji(string $emoji): bool { + public function isValidSingleEmoji(string $emoji): bool { $intlBreakIterator = \IntlBreakIterator::createCharacterInstance(); $intlBreakIterator->setText($emoji); diff --git a/lib/private/Server.php b/lib/private/Server.php index e9d673d3746..5308de21f51 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -1431,6 +1431,8 @@ class Server extends ServerContainer implements IServerContainer { $this->registerAlias(IInitialStateService::class, InitialStateService::class); + $this->registerAlias(\OCP\IEmojiHelper::class, \OC\EmojiHelper::class); + $this->registerAlias(\OCP\UserStatus\IManager::class, \OC\UserStatus\Manager::class); $this->registerAlias(IBroker::class, Broker::class); diff --git a/lib/public/IEmojiHelper.php b/lib/public/IEmojiHelper.php new file mode 100644 index 00000000000..229b08ec68b --- /dev/null +++ b/lib/public/IEmojiHelper.php @@ -0,0 +1,39 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP; + +/** + * @since 24.0.0 + */ +interface IEmojiHelper { + /** + * @since 24.0.0 + */ + public function doesPlatformSupportEmoji(): bool; + + /** + * @since 24.0.0 + */ + public function isValidSingleEmoji(string $emoji): bool; +} diff --git a/tests/lib/Comments/ManagerTest.php b/tests/lib/Comments/ManagerTest.php index 961f8dfdd41..6bcd0dec8ed 100644 --- a/tests/lib/Comments/ManagerTest.php +++ b/tests/lib/Comments/ManagerTest.php @@ -3,8 +3,8 @@ namespace Test\Comments; use OC\Comments\Comment; -use OC\Comments\EmojiHelper; use OC\Comments\Manager; +use OC\EmojiHelper; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Comments\IComment; use OCP\Comments\ICommentsEventHandler; diff --git a/apps/user_status/tests/Unit/Service/EmojiServiceTest.php b/tests/lib/EmojiHelperTest.php index 454f1d75a0d..cdcbf358481 100644 --- a/apps/user_status/tests/Unit/Service/EmojiServiceTest.php +++ b/tests/lib/EmojiHelperTest.php @@ -1,7 +1,6 @@ <?php declare(strict_types=1); - /** * @copyright Copyright (c) 2020, Georg Ehrke * @@ -23,25 +22,24 @@ declare(strict_types=1); * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ -namespace OCA\UserStatus\Tests\Service; +namespace Test; -use OCA\UserStatus\Service\EmojiService; +use OC\EmojiHelper; use OCP\IDBConnection; -use Test\TestCase; +use OCP\IEmojiHelper; -class EmojiServiceTest extends TestCase { +class EmojiHelperTest extends TestCase { /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ private $db; - /** @var EmojiService */ - private $service; + private IEmojiHelper $helper; protected function setUp(): void { parent::setUp(); $this->db = $this->createMock(IDBConnection::class); - $this->service = new EmojiService($this->db); + $this->helper = new EmojiHelper($this->db); } /** @@ -55,7 +53,7 @@ class EmojiServiceTest extends TestCase { ->method('supports4ByteText') ->willReturn($supports4ByteText); - $this->assertEquals($expected, $this->service->doesPlatformSupportEmoji()); + $this->assertEquals($expected, $this->helper->doesPlatformSupportEmoji()); } /** @@ -72,15 +70,15 @@ class EmojiServiceTest extends TestCase { * @param string $emoji * @param bool $expected * - * @dataProvider isValidEmojiDataProvider + * @dataProvider isValidSingleEmojiDataProvider */ - public function testIsValidEmoji(string $emoji, bool $expected): void { - $actual = $this->service->isValidEmoji($emoji); + public function testIsValidSingleEmoji(string $emoji, bool $expected): void { + $actual = $this->helper->isValidSingleEmoji($emoji); $this->assertEquals($expected, $actual); } - public function isValidEmojiDataProvider(): array { + public function isValidSingleEmojiDataProvider(): array { return [ ['🏝', true], ['📱', true], |