ObjectApiTimestampTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. use PHPUnit\Framework\Attributes\Group;
  3. /**
  4. * Object data for the app (element, action response, socket payload, comments
  5. * list) carries `ts`, the server time it was built. NEO compares it to tell
  6. * whether props or its shared object state is newer.
  7. */
  8. #[Group('integration')]
  9. class ObjectApiTimestampTest extends BxDolIntegrationTestCase
  10. {
  11. protected $_aGetBackup;
  12. protected function setUp(): void
  13. {
  14. parent::setUp();
  15. $this->_aGetBackup = $_GET;
  16. }
  17. protected function tearDown(): void
  18. {
  19. $_GET = $this->_aGetBackup ?? [];
  20. parent::tearDown();
  21. }
  22. public function testLikesApiHasTs(): void
  23. {
  24. $oVote = BxDolVote::getObjectInstance('sys_cmts', 1, false);
  25. $this->assertInstanceOf(BxDolVote::class, $oVote);
  26. $fBefore = microtime(true);
  27. $aResult = $this->bxCallProtected($oVote, '_returnVoteData', 1, 0, '', ['value' => 1], false);
  28. $this->assertIsFloat($aResult['api']['ts']);
  29. $this->assertGreaterThanOrEqual($fBefore, $aResult['api']['ts']);
  30. }
  31. public function testReactionsApiHasTs(): void
  32. {
  33. $oVote = BxDolVote::getObjectInstance('sys_cmts_reactions', 1, false);
  34. $this->assertInstanceOf(BxDolVoteReactions::class, $oVote);
  35. $fBefore = microtime(true);
  36. $aResult = $this->bxCallProtected($oVote, '_returnVoteData', 1, 0, '', ['value' => 1, 'reaction' => $oVote->getDefault()], false);
  37. $this->assertIsFloat($aResult['api']['ts']);
  38. $this->assertGreaterThanOrEqual($fBefore, $aResult['api']['ts']);
  39. }
  40. public function testVoteSocketPayloadKeepsTs(): void
  41. {
  42. $oVote = BxDolVote::getObjectInstance('sys_cmts_reactions', 1, false);
  43. $aResult = $this->bxCallProtected($oVote, '_returnVoteData', 1, 0, '', ['value' => 1, 'reaction' => $oVote->getDefault()], false);
  44. $aSocket = $this->bxCallProtected($oVote, '_returnVoteDataForSocket', $aResult);
  45. $this->assertSame($aResult['api']['ts'], $aSocket['api']['ts']);
  46. }
  47. public function testReportApiHasTs(): void
  48. {
  49. $oReport = BxDolReport::getObjectInstance('sys_cmts', 1, false);
  50. $this->assertInstanceOf(BxDolReport::class, $oReport);
  51. $fBefore = microtime(true);
  52. $aResult = $this->bxCallProtected($oReport, '_returnReportData', 1, 0, 0, [], false);
  53. $this->assertIsFloat($aResult['api']['ts']);
  54. $this->assertGreaterThanOrEqual($fBefore, $aResult['api']['ts']);
  55. }
  56. public function testElementApiHasTs(): void
  57. {
  58. $this->bxLoginAsAdmin();
  59. $_GET['api'] = 1;
  60. $oVote = BxDolVote::getObjectInstance('sys_cmts', 1);
  61. $this->assertInstanceOf(BxDolVote::class, $oVote);
  62. $fBefore = microtime(true);
  63. $aElement = $oVote->getElementAPI();
  64. if (!is_array($aElement) || empty($aElement['object_id']))
  65. $this->markTestSkipped('The like element is hidden for the admin.');
  66. $this->assertSame(1, (int)$aElement['object_id']);
  67. $this->assertIsFloat($aElement['ts']);
  68. $this->assertGreaterThanOrEqual($fBefore, $aElement['ts']);
  69. }
  70. public function testCommentsApiHasTs(): void
  71. {
  72. if (!BxDolModuleQuery::getInstance()->isEnabledByName('bx_timeline'))
  73. $this->markTestSkipped('Timeline is not installed.');
  74. $iEventId = (int)BxDolDb::getInstance()->getOne("SELECT `id` FROM `bx_timeline_events` WHERE `active` = 1 ORDER BY `id` DESC LIMIT 1");
  75. if (!$iEventId)
  76. $this->markTestSkipped('There are no timeline events.');
  77. $this->bxLoginAsAdmin();
  78. $_GET['api'] = 1;
  79. $oCmts = BxDolCmts::getObjectInstance('bx_timeline', $iEventId);
  80. if (!$oCmts || !$oCmts->isEnabled())
  81. $this->markTestSkipped('Timeline comments are not enabled.');
  82. $aData = bx_srv('system', 'get_comments_api', [$oCmts, [
  83. 'mode' => 'feed',
  84. 'order_way' => 'desc',
  85. 'start_from' => 0,
  86. 'per_view' => 1,
  87. 'is_form' => false,
  88. ]], 'TemplCmtsServices');
  89. if (!is_array($aData))
  90. $this->markTestSkipped('The admin may not view timeline comments.');
  91. $this->assertSame('comments', $aData['unit']);
  92. $this->assertSame($iEventId, (int)$aData['object_id']);
  93. $this->assertIsFloat($aData['ts']);
  94. }
  95. }