| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- use PHPUnit\Framework\Attributes\Group;
- /**
- * Reactions settings: mode (solo / few / many), default reaction, reactions per person
- * and free-picked emoji, which are stored as e_<hex> codes.
- */
- #[Group('integration')]
- class ReactionsModesTest extends BxDolIntegrationTestCase
- {
- private const SYSTEM = 'bx_timeline_reactions';
- private const OPTIONS = ['sys_vote_reactions_mode', 'sys_vote_reactions_default', 'sys_vote_reactions_per_user', 'sys_vote_reactions_split_counters'];
- private array $_aOptionsBackup = [];
- private int $_iEventId = 0;
- /** The test member's reactions to the event before the test, given back in tearDown. */
- private array $_aReactionsBackup = [];
- protected function setUp(): void
- {
- parent::setUp();
- foreach (self::OPTIONS as $sName)
- $this->_aOptionsBackup[$sName] = getParam($sName);
- }
- protected function tearDown(): void
- {
- unset($_GET['value'], $_GET['reaction']);
- if ($this->_iEventId && ($oVote = $this->reactions($this->_iEventId))) {
- $iAuthorId = bx_get_logged_profile_id();
- foreach ($oVote->getReactionsBy($this->_iEventId, $iAuthorId) as $sReaction)
- $oVote->vote(['reaction' => $sReaction]);
- //--- Give back what the member had before the test, in a mode that takes any of it.
- $this->configure('many', 'like', 5);
- $oVote = $this->reactions($this->_iEventId);
- foreach ($this->_aReactionsBackup as $sReaction)
- $oVote->vote(['reaction' => $sReaction]);
- }
- foreach ($this->_aOptionsBackup as $sName => $sValue)
- setParam($sName, $sValue);
- parent::tearDown();
- }
- private function configure(string $sMode, string $sDefault = 'like', int $iPerUser = 1): void
- {
- setParam('sys_vote_reactions_mode', $sMode);
- setParam('sys_vote_reactions_default', $sDefault);
- setParam('sys_vote_reactions_per_user', (string)$iPerUser);
- }
- /**
- * A fresh instance: settings are read in the constructor and instances are cached.
- */
- private function reactions(int $iObjectId = 1): ?BxDolVoteReactions
- {
- if (!BxDolVote::getObjectInstance(self::SYSTEM, $iObjectId, false))
- return null;
- unset($GLOBALS['bxDolClasses']['BxDolVote!' . self::SYSTEM . $iObjectId]);
- $oVote = BxDolVote::getObjectInstance(self::SYSTEM, $iObjectId);
- return $oVote instanceof BxDolVoteReactions ? $oVote : null;
- }
- private function reactionsOrSkip(int $iObjectId = 1): BxDolVoteReactions
- {
- $oVote = $this->reactions($iObjectId);
- if (!$oVote)
- $this->markTestSkipped(self::SYSTEM . ' is not installed.');
- return $oVote;
- }
- public function testSettingsHaveTheirOwnSection(): void
- {
- $oDb = BxDolDb::getInstance();
- $aCategory = $oDb->getRow("SELECT `id`, `caption`, `hidden` FROM `sys_options_categories` WHERE `name` = 'reactions'");
- $this->assertNotEmpty($aCategory, 'Studio > Settings > Global has a Reactions section');
- $this->assertSame(0, (int)$aCategory['hidden']);
- $this->assertNotSame($aCategory['caption'], _t($aCategory['caption']), 'the section has a title');
- $aOptions = $oDb->getPairs("SELECT `name`, `category_id` FROM `sys_options` WHERE `name` IN ('" . implode("', '", self::OPTIONS) . "')", 'name', 'category_id');
- $this->assertSame(self::OPTIONS, array_keys(array_intersect_key(array_flip(self::OPTIONS), $aOptions)), 'all reaction options exist');
- foreach ($aOptions as $sName => $iCategoryId)
- $this->assertSame((int)$aCategory['id'], (int)$iCategoryId, $sName . ' is in the Reactions section');
- }
- public function testWebCounterFollowsSplitSetting(): void
- {
- $oVote = $this->memberOnEventOrSkip('few', 1);
- $sWrapper = 'counter-wrapper-' . BX_DOL_VOTE_TYPE_REACTIONS . '-';
- setParam('sys_vote_reactions_split_counters', 'on');
- $oVote = $this->reactionsOrSkip($this->_iEventId);
- $this->assertStringContainsString($sWrapper . 'divided', $oVote->getCounter(), 'Split counters: a counter per reaction.');
- $this->assertStringContainsString($sWrapper . 'divided', $oVote->getCounter(['show_counter_style' => 'compound']));
- setParam('sys_vote_reactions_split_counters', '');
- $oVote = $this->reactionsOrSkip($this->_iEventId);
- $this->assertStringContainsString($sWrapper . 'compound', $oVote->getCounter(), 'One total, as NEO shows it.');
- $this->assertStringContainsString($sWrapper . 'compound', $oVote->getCounter(['show_counter_style' => 'divided']));
- $this->assertStringNotContainsString($sWrapper . 'compound', $oVote->getCounter(['show_counter_style' => 'simple']), 'A simple counter stays simple.');
- }
- public function testEmojiValidation(): void
- {
- foreach (['😀', '❤️', '👍🏽', '🏳️🌈', '1️⃣', '🇺🇦'] as $sEmoji)
- $this->assertTrue(BxDolVoteReactions::isEmoji($sEmoji), $sEmoji . ' is an emoji.');
- foreach (['', 'a', 'like', '😀😀', '<b>', '😀 '] as $sText)
- $this->assertFalse(BxDolVoteReactions::isEmoji($sText), '"' . $sText . '" is not one emoji.');
- }
- public function testEmojiCodeRoundTrip(): void
- {
- $this->assertSame('e_1f600', BxDolVoteReactions::encodeEmoji('😀'));
- $this->assertSame('e_2764_fe0f', BxDolVoteReactions::encodeEmoji('❤️'));
- $this->assertSame('❤️', BxDolVoteReactions::decodeEmoji('e_2764_fe0f'));
- foreach (['e_', 'e_zz', 'e_61', 'like', 'e_d800', 'e_110000'] as $sCode)
- $this->assertFalse(BxDolVoteReactions::decodeEmoji($sCode), $sCode . ' is not an emoji code.');
- }
- public function testFewModeTakesOnlyListedReactions(): void
- {
- $this->configure('few');
- $oVote = $this->reactionsOrSkip();
- $this->assertSame('like', $oVote->normalizeReaction('like'));
- $this->assertFalse($oVote->normalizeReaction('😀'));
- $this->assertFalse($oVote->normalizeReaction('e_1f600'));
- $this->assertFalse($oVote->normalizeReaction('nope'));
- }
- public function testManyModeTakesAnyEmoji(): void
- {
- $this->configure('many');
- $oVote = $this->reactionsOrSkip();
- $this->assertSame('e_1f600', $oVote->normalizeReaction('😀'));
- $this->assertSame('e_1f600', $oVote->normalizeReaction('e_1f600'));
- $this->assertSame('like', $oVote->normalizeReaction('👍'), 'An emoji on the list is that reaction.');
- $this->assertFalse($oVote->normalizeReaction('text'));
- $this->assertFalse($oVote->normalizeReaction('👨👩👧👦'), 'Codes longer than the column are refused.');
- $aReaction = $oVote->getReaction('e_1f600');
- $this->assertIsArray($aReaction);
- $this->assertSame('😀', $aReaction['emoji']);
- }
- public function testSoloModeKeepsOnlyTheDefault(): void
- {
- $this->configure('solo', 'love');
- $oVote = $this->reactionsOrSkip();
- $this->assertSame('solo', $oVote->getMode());
- $this->assertSame('love', $oVote->getDefaultReaction());
- $this->assertSame(1, $oVote->getPerUser());
- $this->assertSame('love', $oVote->normalizeReaction('love'));
- $this->assertFalse($oVote->normalizeReaction('like'));
- }
- public function testUnknownDefaultMeansNone(): void
- {
- $this->configure('few', 'no_such_reaction');
- $this->assertSame('', $this->reactionsOrSkip()->getDefaultReaction());
- }
- /**
- * The test member on the latest timeline event, with their reactions to it set aside (tearDown gives them back),
- * in the given mode.
- */
- private function memberOnEventOrSkip(string $sMode, int $iPerUser): BxDolVoteReactions
- {
- if (!BxDolModuleQuery::getInstance()->isEnabledByName('bx_timeline'))
- $this->markTestSkipped('Timeline is not installed.');
- $iEventId = (int)BxDolDb::getInstance()->getOne("SELECT `id` FROM `bx_timeline_events` WHERE `active` = 1 ORDER BY `id` DESC LIMIT 1");
- if (!$iEventId)
- $this->markTestSkipped('There are no timeline events to react to.');
- $this->bxLoginAsUser();
- $this->configure($sMode, 'like', $iPerUser);
- $oVote = $this->reactionsOrSkip($iEventId);
- if (!$oVote->isAllowedVote())
- $this->markTestSkipped('The test user may not react to timeline events: ' . strip_tags((string)$oVote->msgErrAllowedVote()));
- $this->_iEventId = $iEventId;
- $this->_aReactionsBackup = $oVote->getReactionsBy($iEventId, bx_get_logged_profile_id());
- foreach ($this->_aReactionsBackup as $sReaction)
- $oVote->vote(['reaction' => $sReaction]);
- return $oVote;
- }
- /**
- * The reaction a classic web request (`actionVote`) asks for, as `_getVoteData` reads it, or false.
- */
- private function webVoteReaction(BxDolVoteReactions $oVote, string $sReaction)
- {
- $_GET['value'] = '1';
- $_GET['reaction'] = $sReaction;
- $aData = (new ReflectionMethod($oVote, '_getVoteData'))->invoke($oVote);
- return $aData === false ? false : $aData['reaction'];
- }
- public function testWebTakesBackLeftoverReaction(): void
- {
- $oVote = $this->memberOnEventOrSkip('many', 2);
- $this->assertSame(0, (int)$oVote->vote(['reaction' => '😀'])['code']);
- //--- Few mode no longer offers the emoji, but its owner can still send it back to take it back.
- $this->configure('few', 'like', 2);
- $oVote = $this->reactionsOrSkip($this->_iEventId);
- $this->assertSame('e_1f600', $this->webVoteReaction($oVote, 'e_1f600'));
- $this->assertSame('e_1f600', $this->webVoteReaction($oVote, '😀'));
- $this->assertFalse($this->webVoteReaction($oVote, 'e_1f602'), 'An emoji one does not have is refused.');
- $this->assertSame('love', $this->webVoteReaction($oVote, 'love'));
- $aResult = $oVote->vote(['reaction' => $this->webVoteReaction($oVote, 'e_1f600')]);
- $this->assertSame(0, (int)$aResult['code']);
- $this->assertSame([], $aResult['api']['reactions']);
- }
- public function testMemberReactionsWithLimitAndReplace(): void
- {
- //--- Two reactions allowed: both stay, a third is refused, giving one again takes it back.
- $oVote = $this->memberOnEventOrSkip('many', 2);
- $this->assertSame(0, (int)$oVote->vote(['reaction' => 'like'])['code']);
- $aResult = $oVote->vote(['reaction' => '😀']);
- $this->assertSame(0, (int)$aResult['code']);
- $this->assertSame(['like', 'e_1f600'], $aResult['api']['reactions']);
- $this->assertSame(1, (int)$aResult['api']['counter']['count_e_1f600']);
- $aResult = $oVote->vote(['reaction' => 'love']);
- $this->assertSame(BX_DOL_OBJECT_ERR_DUPLICATE, (int)$aResult['code']);
- $aResult = $oVote->vote(['reaction' => 'e_1f600']);
- $this->assertSame(0, (int)$aResult['code']);
- $this->assertSame(['like'], $aResult['api']['reactions']);
- //--- One reaction allowed: a new one replaces the old one.
- $this->configure('few', 'like', 1);
- $oVote = $this->reactionsOrSkip($this->_iEventId);
- $aResult = $oVote->vote(['reaction' => 'love']);
- $this->assertSame(0, (int)$aResult['code']);
- $this->assertSame(['love'], $aResult['api']['reactions']);
- $this->assertSame('love', $aResult['api']['reaction']);
- //--- Few mode refuses a free emoji.
- $this->assertSame(BX_DOL_OBJECT_ERR_WRONG_DATE, (int)$oVote->vote(['reaction' => '😀'])['code']);
- //--- Reactions the settings no longer offer can still be taken back.
- $this->configure('many', 'like', 2);
- $oVote = $this->reactionsOrSkip($this->_iEventId);
- $this->assertSame(['love', 'e_1f600'], $oVote->vote(['reaction' => '😀'])['api']['reactions']);
- $this->configure('solo', 'like');
- $oVote = $this->reactionsOrSkip($this->_iEventId);
- $this->assertSame(BX_DOL_OBJECT_ERR_WRONG_DATE, (int)$oVote->vote(['reaction' => 'e_1f602'])['code'], 'An emoji one does not have is refused.');
- $aResult = $oVote->vote(['reaction' => 'e_1f600']);
- $this->assertSame(0, (int)$aResult['code']);
- $this->assertSame(['love'], $aResult['api']['reactions']);
- $aResult = $oVote->vote(['reaction' => 'love']);
- $this->assertSame(0, (int)$aResult['code'], 'Solo mode with "like" still takes back "love".');
- $this->assertSame([], $aResult['api']['reactions']);
- }
- }
|