ReactionsModesTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. use PHPUnit\Framework\Attributes\Group;
  3. /**
  4. * Reactions settings: mode (solo / few / many), default reaction, reactions per person
  5. * and free-picked emoji, which are stored as e_<hex> codes.
  6. */
  7. #[Group('integration')]
  8. class ReactionsModesTest extends BxDolIntegrationTestCase
  9. {
  10. private const SYSTEM = 'bx_timeline_reactions';
  11. private const OPTIONS = ['sys_vote_reactions_mode', 'sys_vote_reactions_default', 'sys_vote_reactions_per_user', 'sys_vote_reactions_split_counters'];
  12. private array $_aOptionsBackup = [];
  13. private int $_iEventId = 0;
  14. /** The test member's reactions to the event before the test, given back in tearDown. */
  15. private array $_aReactionsBackup = [];
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. foreach (self::OPTIONS as $sName)
  20. $this->_aOptionsBackup[$sName] = getParam($sName);
  21. }
  22. protected function tearDown(): void
  23. {
  24. unset($_GET['value'], $_GET['reaction']);
  25. if ($this->_iEventId && ($oVote = $this->reactions($this->_iEventId))) {
  26. $iAuthorId = bx_get_logged_profile_id();
  27. foreach ($oVote->getReactionsBy($this->_iEventId, $iAuthorId) as $sReaction)
  28. $oVote->vote(['reaction' => $sReaction]);
  29. //--- Give back what the member had before the test, in a mode that takes any of it.
  30. $this->configure('many', 'like', 5);
  31. $oVote = $this->reactions($this->_iEventId);
  32. foreach ($this->_aReactionsBackup as $sReaction)
  33. $oVote->vote(['reaction' => $sReaction]);
  34. }
  35. foreach ($this->_aOptionsBackup as $sName => $sValue)
  36. setParam($sName, $sValue);
  37. parent::tearDown();
  38. }
  39. private function configure(string $sMode, string $sDefault = 'like', int $iPerUser = 1): void
  40. {
  41. setParam('sys_vote_reactions_mode', $sMode);
  42. setParam('sys_vote_reactions_default', $sDefault);
  43. setParam('sys_vote_reactions_per_user', (string)$iPerUser);
  44. }
  45. /**
  46. * A fresh instance: settings are read in the constructor and instances are cached.
  47. */
  48. private function reactions(int $iObjectId = 1): ?BxDolVoteReactions
  49. {
  50. if (!BxDolVote::getObjectInstance(self::SYSTEM, $iObjectId, false))
  51. return null;
  52. unset($GLOBALS['bxDolClasses']['BxDolVote!' . self::SYSTEM . $iObjectId]);
  53. $oVote = BxDolVote::getObjectInstance(self::SYSTEM, $iObjectId);
  54. return $oVote instanceof BxDolVoteReactions ? $oVote : null;
  55. }
  56. private function reactionsOrSkip(int $iObjectId = 1): BxDolVoteReactions
  57. {
  58. $oVote = $this->reactions($iObjectId);
  59. if (!$oVote)
  60. $this->markTestSkipped(self::SYSTEM . ' is not installed.');
  61. return $oVote;
  62. }
  63. public function testSettingsHaveTheirOwnSection(): void
  64. {
  65. $oDb = BxDolDb::getInstance();
  66. $aCategory = $oDb->getRow("SELECT `id`, `caption`, `hidden` FROM `sys_options_categories` WHERE `name` = 'reactions'");
  67. $this->assertNotEmpty($aCategory, 'Studio > Settings > Global has a Reactions section');
  68. $this->assertSame(0, (int)$aCategory['hidden']);
  69. $this->assertNotSame($aCategory['caption'], _t($aCategory['caption']), 'the section has a title');
  70. $aOptions = $oDb->getPairs("SELECT `name`, `category_id` FROM `sys_options` WHERE `name` IN ('" . implode("', '", self::OPTIONS) . "')", 'name', 'category_id');
  71. $this->assertSame(self::OPTIONS, array_keys(array_intersect_key(array_flip(self::OPTIONS), $aOptions)), 'all reaction options exist');
  72. foreach ($aOptions as $sName => $iCategoryId)
  73. $this->assertSame((int)$aCategory['id'], (int)$iCategoryId, $sName . ' is in the Reactions section');
  74. }
  75. public function testWebCounterFollowsSplitSetting(): void
  76. {
  77. $oVote = $this->memberOnEventOrSkip('few', 1);
  78. $sWrapper = 'counter-wrapper-' . BX_DOL_VOTE_TYPE_REACTIONS . '-';
  79. setParam('sys_vote_reactions_split_counters', 'on');
  80. $oVote = $this->reactionsOrSkip($this->_iEventId);
  81. $this->assertStringContainsString($sWrapper . 'divided', $oVote->getCounter(), 'Split counters: a counter per reaction.');
  82. $this->assertStringContainsString($sWrapper . 'divided', $oVote->getCounter(['show_counter_style' => 'compound']));
  83. setParam('sys_vote_reactions_split_counters', '');
  84. $oVote = $this->reactionsOrSkip($this->_iEventId);
  85. $this->assertStringContainsString($sWrapper . 'compound', $oVote->getCounter(), 'One total, as NEO shows it.');
  86. $this->assertStringContainsString($sWrapper . 'compound', $oVote->getCounter(['show_counter_style' => 'divided']));
  87. $this->assertStringNotContainsString($sWrapper . 'compound', $oVote->getCounter(['show_counter_style' => 'simple']), 'A simple counter stays simple.');
  88. }
  89. public function testEmojiValidation(): void
  90. {
  91. foreach (['😀', '❤️', '👍🏽', '🏳️‍🌈', '1️⃣', '🇺🇦'] as $sEmoji)
  92. $this->assertTrue(BxDolVoteReactions::isEmoji($sEmoji), $sEmoji . ' is an emoji.');
  93. foreach (['', 'a', 'like', '😀😀', '<b>', '😀 '] as $sText)
  94. $this->assertFalse(BxDolVoteReactions::isEmoji($sText), '"' . $sText . '" is not one emoji.');
  95. }
  96. public function testEmojiCodeRoundTrip(): void
  97. {
  98. $this->assertSame('e_1f600', BxDolVoteReactions::encodeEmoji('😀'));
  99. $this->assertSame('e_2764_fe0f', BxDolVoteReactions::encodeEmoji('❤️'));
  100. $this->assertSame('❤️', BxDolVoteReactions::decodeEmoji('e_2764_fe0f'));
  101. foreach (['e_', 'e_zz', 'e_61', 'like', 'e_d800', 'e_110000'] as $sCode)
  102. $this->assertFalse(BxDolVoteReactions::decodeEmoji($sCode), $sCode . ' is not an emoji code.');
  103. }
  104. public function testFewModeTakesOnlyListedReactions(): void
  105. {
  106. $this->configure('few');
  107. $oVote = $this->reactionsOrSkip();
  108. $this->assertSame('like', $oVote->normalizeReaction('like'));
  109. $this->assertFalse($oVote->normalizeReaction('😀'));
  110. $this->assertFalse($oVote->normalizeReaction('e_1f600'));
  111. $this->assertFalse($oVote->normalizeReaction('nope'));
  112. }
  113. public function testManyModeTakesAnyEmoji(): void
  114. {
  115. $this->configure('many');
  116. $oVote = $this->reactionsOrSkip();
  117. $this->assertSame('e_1f600', $oVote->normalizeReaction('😀'));
  118. $this->assertSame('e_1f600', $oVote->normalizeReaction('e_1f600'));
  119. $this->assertSame('like', $oVote->normalizeReaction('👍'), 'An emoji on the list is that reaction.');
  120. $this->assertFalse($oVote->normalizeReaction('text'));
  121. $this->assertFalse($oVote->normalizeReaction('👨‍👩‍👧‍👦'), 'Codes longer than the column are refused.');
  122. $aReaction = $oVote->getReaction('e_1f600');
  123. $this->assertIsArray($aReaction);
  124. $this->assertSame('😀', $aReaction['emoji']);
  125. }
  126. public function testSoloModeKeepsOnlyTheDefault(): void
  127. {
  128. $this->configure('solo', 'love');
  129. $oVote = $this->reactionsOrSkip();
  130. $this->assertSame('solo', $oVote->getMode());
  131. $this->assertSame('love', $oVote->getDefaultReaction());
  132. $this->assertSame(1, $oVote->getPerUser());
  133. $this->assertSame('love', $oVote->normalizeReaction('love'));
  134. $this->assertFalse($oVote->normalizeReaction('like'));
  135. }
  136. public function testUnknownDefaultMeansNone(): void
  137. {
  138. $this->configure('few', 'no_such_reaction');
  139. $this->assertSame('', $this->reactionsOrSkip()->getDefaultReaction());
  140. }
  141. /**
  142. * The test member on the latest timeline event, with their reactions to it set aside (tearDown gives them back),
  143. * in the given mode.
  144. */
  145. private function memberOnEventOrSkip(string $sMode, int $iPerUser): BxDolVoteReactions
  146. {
  147. if (!BxDolModuleQuery::getInstance()->isEnabledByName('bx_timeline'))
  148. $this->markTestSkipped('Timeline is not installed.');
  149. $iEventId = (int)BxDolDb::getInstance()->getOne("SELECT `id` FROM `bx_timeline_events` WHERE `active` = 1 ORDER BY `id` DESC LIMIT 1");
  150. if (!$iEventId)
  151. $this->markTestSkipped('There are no timeline events to react to.');
  152. $this->bxLoginAsUser();
  153. $this->configure($sMode, 'like', $iPerUser);
  154. $oVote = $this->reactionsOrSkip($iEventId);
  155. if (!$oVote->isAllowedVote())
  156. $this->markTestSkipped('The test user may not react to timeline events: ' . strip_tags((string)$oVote->msgErrAllowedVote()));
  157. $this->_iEventId = $iEventId;
  158. $this->_aReactionsBackup = $oVote->getReactionsBy($iEventId, bx_get_logged_profile_id());
  159. foreach ($this->_aReactionsBackup as $sReaction)
  160. $oVote->vote(['reaction' => $sReaction]);
  161. return $oVote;
  162. }
  163. /**
  164. * The reaction a classic web request (`actionVote`) asks for, as `_getVoteData` reads it, or false.
  165. */
  166. private function webVoteReaction(BxDolVoteReactions $oVote, string $sReaction)
  167. {
  168. $_GET['value'] = '1';
  169. $_GET['reaction'] = $sReaction;
  170. $aData = (new ReflectionMethod($oVote, '_getVoteData'))->invoke($oVote);
  171. return $aData === false ? false : $aData['reaction'];
  172. }
  173. public function testWebTakesBackLeftoverReaction(): void
  174. {
  175. $oVote = $this->memberOnEventOrSkip('many', 2);
  176. $this->assertSame(0, (int)$oVote->vote(['reaction' => '😀'])['code']);
  177. //--- Few mode no longer offers the emoji, but its owner can still send it back to take it back.
  178. $this->configure('few', 'like', 2);
  179. $oVote = $this->reactionsOrSkip($this->_iEventId);
  180. $this->assertSame('e_1f600', $this->webVoteReaction($oVote, 'e_1f600'));
  181. $this->assertSame('e_1f600', $this->webVoteReaction($oVote, '😀'));
  182. $this->assertFalse($this->webVoteReaction($oVote, 'e_1f602'), 'An emoji one does not have is refused.');
  183. $this->assertSame('love', $this->webVoteReaction($oVote, 'love'));
  184. $aResult = $oVote->vote(['reaction' => $this->webVoteReaction($oVote, 'e_1f600')]);
  185. $this->assertSame(0, (int)$aResult['code']);
  186. $this->assertSame([], $aResult['api']['reactions']);
  187. }
  188. public function testMemberReactionsWithLimitAndReplace(): void
  189. {
  190. //--- Two reactions allowed: both stay, a third is refused, giving one again takes it back.
  191. $oVote = $this->memberOnEventOrSkip('many', 2);
  192. $this->assertSame(0, (int)$oVote->vote(['reaction' => 'like'])['code']);
  193. $aResult = $oVote->vote(['reaction' => '😀']);
  194. $this->assertSame(0, (int)$aResult['code']);
  195. $this->assertSame(['like', 'e_1f600'], $aResult['api']['reactions']);
  196. $this->assertSame(1, (int)$aResult['api']['counter']['count_e_1f600']);
  197. $aResult = $oVote->vote(['reaction' => 'love']);
  198. $this->assertSame(BX_DOL_OBJECT_ERR_DUPLICATE, (int)$aResult['code']);
  199. $aResult = $oVote->vote(['reaction' => 'e_1f600']);
  200. $this->assertSame(0, (int)$aResult['code']);
  201. $this->assertSame(['like'], $aResult['api']['reactions']);
  202. //--- One reaction allowed: a new one replaces the old one.
  203. $this->configure('few', 'like', 1);
  204. $oVote = $this->reactionsOrSkip($this->_iEventId);
  205. $aResult = $oVote->vote(['reaction' => 'love']);
  206. $this->assertSame(0, (int)$aResult['code']);
  207. $this->assertSame(['love'], $aResult['api']['reactions']);
  208. $this->assertSame('love', $aResult['api']['reaction']);
  209. //--- Few mode refuses a free emoji.
  210. $this->assertSame(BX_DOL_OBJECT_ERR_WRONG_DATE, (int)$oVote->vote(['reaction' => '😀'])['code']);
  211. //--- Reactions the settings no longer offer can still be taken back.
  212. $this->configure('many', 'like', 2);
  213. $oVote = $this->reactionsOrSkip($this->_iEventId);
  214. $this->assertSame(['love', 'e_1f600'], $oVote->vote(['reaction' => '😀'])['api']['reactions']);
  215. $this->configure('solo', 'like');
  216. $oVote = $this->reactionsOrSkip($this->_iEventId);
  217. $this->assertSame(BX_DOL_OBJECT_ERR_WRONG_DATE, (int)$oVote->vote(['reaction' => 'e_1f602'])['code'], 'An emoji one does not have is refused.');
  218. $aResult = $oVote->vote(['reaction' => 'e_1f600']);
  219. $this->assertSame(0, (int)$aResult['code']);
  220. $this->assertSame(['love'], $aResult['api']['reactions']);
  221. $aResult = $oVote->vote(['reaction' => 'love']);
  222. $this->assertSame(0, (int)$aResult['code'], 'Solo mode with "like" still takes back "love".');
  223. $this->assertSame([], $aResult['api']['reactions']);
  224. }
  225. }