LikesUndoTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. use PHPUnit\Framework\Attributes\Group;
  3. /**
  4. * Likes can always be taken back, whatever the vote object's IsUndo setting;
  5. * poll answer votes, which are likes only in form, keep that setting.
  6. */
  7. #[Group('integration')]
  8. class LikesUndoTest extends BxDolIntegrationTestCase
  9. {
  10. public function testLikeObjectsAreUndoableWhateverIsUndo(): void
  11. {
  12. $aObjects = BxDolDb::getInstance()->getColumn("SELECT `Name` FROM `sys_objects_vote` WHERE `IsUndo` = 0 AND `Name` IN ('sys_cmts', 'bx_timeline', 'bx_posts', 'bx_persons')");
  13. if (empty($aObjects))
  14. $this->markTestSkipped('No like objects with IsUndo = 0 are installed.');
  15. foreach ($aObjects as $sName) {
  16. $oVote = BxDolVote::getObjectInstance($sName, 1, false);
  17. if (!$oVote instanceof BxDolVoteLikes)
  18. continue;
  19. $this->assertTrue($oVote->isUndo(), $sName . ' likes should be undoable.');
  20. }
  21. }
  22. public function testPollAnswerVotesKeepIsUndo(): void
  23. {
  24. $aRows = BxDolDb::getInstance()->getAll("SELECT `Name`, `IsUndo` FROM `sys_objects_vote` WHERE `Name` LIKE '%\\_poll\\_answers' OR `Name` = 'bx_polls_subentries'");
  25. if (empty($aRows))
  26. $this->markTestSkipped('No poll answer vote objects are installed.');
  27. foreach ($aRows as $aRow) {
  28. $oVote = BxDolVote::getObjectInstance($aRow['Name'], 1, false);
  29. if (!$oVote)
  30. continue;
  31. $this->assertSame((int)$aRow['IsUndo'] === 1, $oVote->isUndo(), $aRow['Name'] . ' should follow IsUndo.');
  32. }
  33. }
  34. public function testMemberCanLikeAndTakeItBack(): void
  35. {
  36. if (!BxDolModuleQuery::getInstance()->isEnabledByName('bx_timeline'))
  37. $this->markTestSkipped('Timeline is not installed.');
  38. $iEventId = (int)BxDolDb::getInstance()->getOne("SELECT `id` FROM `bx_timeline_events` WHERE `active` = 1 ORDER BY `id` DESC LIMIT 1");
  39. if (!$iEventId)
  40. $this->markTestSkipped('There are no timeline events to like.');
  41. $this->bxLoginAsUser();
  42. $oVote = BxDolVote::getObjectInstance('bx_timeline', $iEventId);
  43. $this->assertInstanceOf(BxDolVoteLikes::class, $oVote);
  44. if (!$oVote->isAllowedVote())
  45. $this->markTestSkipped('The test user may not like timeline events.');
  46. $iProfileId = bx_get_logged_profile_id();
  47. $bVotedBefore = (bool)$oVote->isPerformed($iEventId, $iProfileId);
  48. try {
  49. $aFirst = $oVote->vote(['value' => 1]);
  50. $this->assertSame(0, (int)($aFirst['code'] ?? 0), 'First vote failed: ' . json_encode($aFirst));
  51. $this->assertSame(!$bVotedBefore, $aFirst['api']['is_voted']);
  52. // Before IsUndo was ignored for likes, this second vote was refused as a duplicate.
  53. $aSecond = $oVote->vote(['value' => 1]);
  54. $this->assertSame(0, (int)($aSecond['code'] ?? 0), 'Second vote failed: ' . json_encode($aSecond));
  55. $this->assertSame($bVotedBefore, $aSecond['api']['is_voted']);
  56. }
  57. finally {
  58. // Leave the member's vote as it was, even when an assertion above failed.
  59. if ((bool)$oVote->isPerformed($iEventId, $iProfileId) !== $bVotedBefore)
  60. $oVote->vote(['value' => 1]);
  61. }
  62. }
  63. }