ManagerTest.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. <?php
  2. namespace Test\Comments;
  3. use OC\Comments\Comment;
  4. use OC\Comments\Manager;
  5. use OC\EmojiHelper;
  6. use OCP\AppFramework\Utility\ITimeFactory;
  7. use OCP\Comments\IComment;
  8. use OCP\Comments\ICommentsEventHandler;
  9. use OCP\Comments\ICommentsManager;
  10. use OCP\Comments\NotFoundException;
  11. use OCP\IConfig;
  12. use OCP\IDBConnection;
  13. use OCP\IInitialStateService;
  14. use OCP\IUser;
  15. use OCP\Server;
  16. use Psr\Log\LoggerInterface;
  17. use Test\TestCase;
  18. /**
  19. * Class ManagerTest
  20. *
  21. * @group DB
  22. */
  23. class ManagerTest extends TestCase {
  24. /** @var IDBConnection */
  25. private $connection;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->connection = \OC::$server->getDatabaseConnection();
  29. $sql = $this->connection->getDatabasePlatform()->getTruncateTableSQL('`*PREFIX*comments`');
  30. $this->connection->prepare($sql)->execute();
  31. $sql = $this->connection->getDatabasePlatform()->getTruncateTableSQL('`*PREFIX*reactions`');
  32. $this->connection->prepare($sql)->execute();
  33. }
  34. protected function addDatabaseEntry($parentId, $topmostParentId, $creationDT = null, $latestChildDT = null, $objectId = null, $expireDate = null) {
  35. if (is_null($creationDT)) {
  36. $creationDT = new \DateTime();
  37. }
  38. if (is_null($latestChildDT)) {
  39. $latestChildDT = new \DateTime('yesterday');
  40. }
  41. if (is_null($objectId)) {
  42. $objectId = 'file64';
  43. }
  44. $qb = $this->connection->getQueryBuilder();
  45. $qb
  46. ->insert('comments')
  47. ->values([
  48. 'parent_id' => $qb->createNamedParameter($parentId),
  49. 'topmost_parent_id' => $qb->createNamedParameter($topmostParentId),
  50. 'children_count' => $qb->createNamedParameter(2),
  51. 'actor_type' => $qb->createNamedParameter('users'),
  52. 'actor_id' => $qb->createNamedParameter('alice'),
  53. 'message' => $qb->createNamedParameter('nice one'),
  54. 'verb' => $qb->createNamedParameter('comment'),
  55. 'creation_timestamp' => $qb->createNamedParameter($creationDT, 'datetime'),
  56. 'latest_child_timestamp' => $qb->createNamedParameter($latestChildDT, 'datetime'),
  57. 'object_type' => $qb->createNamedParameter('files'),
  58. 'object_id' => $qb->createNamedParameter($objectId),
  59. 'expire_date' => $qb->createNamedParameter($expireDate, 'datetime'),
  60. ])
  61. ->execute();
  62. return $qb->getLastInsertId();
  63. }
  64. protected function getManager() {
  65. return new Manager(
  66. $this->connection,
  67. $this->createMock(LoggerInterface::class),
  68. $this->createMock(IConfig::class),
  69. $this->createMock(ITimeFactory::class),
  70. new EmojiHelper($this->connection),
  71. $this->createMock(IInitialStateService::class)
  72. );
  73. }
  74. public function testGetCommentNotFound() {
  75. $this->expectException(\OCP\Comments\NotFoundException::class);
  76. $manager = $this->getManager();
  77. $manager->get('22');
  78. }
  79. public function testGetCommentNotFoundInvalidInput() {
  80. $this->expectException(\InvalidArgumentException::class);
  81. $manager = $this->getManager();
  82. $manager->get('unexisting22');
  83. }
  84. public function testGetComment() {
  85. $manager = $this->getManager();
  86. $creationDT = new \DateTime();
  87. $latestChildDT = new \DateTime('yesterday');
  88. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  89. $qb
  90. ->insert('comments')
  91. ->values([
  92. 'parent_id' => $qb->createNamedParameter('2'),
  93. 'topmost_parent_id' => $qb->createNamedParameter('1'),
  94. 'children_count' => $qb->createNamedParameter(2),
  95. 'actor_type' => $qb->createNamedParameter('users'),
  96. 'actor_id' => $qb->createNamedParameter('alice'),
  97. 'message' => $qb->createNamedParameter('nice one'),
  98. 'verb' => $qb->createNamedParameter('comment'),
  99. 'creation_timestamp' => $qb->createNamedParameter($creationDT, 'datetime'),
  100. 'latest_child_timestamp' => $qb->createNamedParameter($latestChildDT, 'datetime'),
  101. 'object_type' => $qb->createNamedParameter('files'),
  102. 'object_id' => $qb->createNamedParameter('file64'),
  103. ])
  104. ->execute();
  105. $id = strval($qb->getLastInsertId());
  106. $comment = $manager->get($id);
  107. $this->assertTrue($comment instanceof IComment);
  108. $this->assertSame($comment->getId(), $id);
  109. $this->assertSame($comment->getParentId(), '2');
  110. $this->assertSame($comment->getTopmostParentId(), '1');
  111. $this->assertSame($comment->getChildrenCount(), 2);
  112. $this->assertSame($comment->getActorType(), 'users');
  113. $this->assertSame($comment->getActorId(), 'alice');
  114. $this->assertSame($comment->getMessage(), 'nice one');
  115. $this->assertSame($comment->getVerb(), 'comment');
  116. $this->assertSame($comment->getObjectType(), 'files');
  117. $this->assertSame($comment->getObjectId(), 'file64');
  118. $this->assertEquals($comment->getCreationDateTime()->getTimestamp(), $creationDT->getTimestamp());
  119. $this->assertEquals($comment->getLatestChildDateTime(), $latestChildDT);
  120. }
  121. public function testGetTreeNotFound() {
  122. $this->expectException(\OCP\Comments\NotFoundException::class);
  123. $manager = $this->getManager();
  124. $manager->getTree('22');
  125. }
  126. public function testGetTreeNotFoundInvalidIpnut() {
  127. $this->expectException(\InvalidArgumentException::class);
  128. $manager = $this->getManager();
  129. $manager->getTree('unexisting22');
  130. }
  131. public function testGetTree() {
  132. $headId = $this->addDatabaseEntry(0, 0);
  133. $this->addDatabaseEntry($headId, $headId, new \DateTime('-3 hours'));
  134. $this->addDatabaseEntry($headId, $headId, new \DateTime('-2 hours'));
  135. $id = $this->addDatabaseEntry($headId, $headId, new \DateTime('-1 hour'));
  136. $manager = $this->getManager();
  137. $tree = $manager->getTree($headId);
  138. // Verifying the root comment
  139. $this->assertTrue(isset($tree['comment']));
  140. $this->assertTrue($tree['comment'] instanceof IComment);
  141. $this->assertSame($tree['comment']->getId(), strval($headId));
  142. $this->assertTrue(isset($tree['replies']));
  143. $this->assertSame(count($tree['replies']), 3);
  144. // one level deep
  145. foreach ($tree['replies'] as $reply) {
  146. $this->assertTrue($reply['comment'] instanceof IComment);
  147. $this->assertSame($reply['comment']->getId(), strval($id));
  148. $this->assertSame(count($reply['replies']), 0);
  149. $id--;
  150. }
  151. }
  152. public function testGetTreeNoReplies() {
  153. $id = $this->addDatabaseEntry(0, 0);
  154. $manager = $this->getManager();
  155. $tree = $manager->getTree($id);
  156. // Verifying the root comment
  157. $this->assertTrue(isset($tree['comment']));
  158. $this->assertTrue($tree['comment'] instanceof IComment);
  159. $this->assertSame($tree['comment']->getId(), strval($id));
  160. $this->assertTrue(isset($tree['replies']));
  161. $this->assertSame(count($tree['replies']), 0);
  162. // one level deep
  163. foreach ($tree['replies'] as $reply) {
  164. throw new \Exception('This ain`t happen');
  165. }
  166. }
  167. public function testGetTreeWithLimitAndOffset() {
  168. $headId = $this->addDatabaseEntry(0, 0);
  169. $this->addDatabaseEntry($headId, $headId, new \DateTime('-3 hours'));
  170. $this->addDatabaseEntry($headId, $headId, new \DateTime('-2 hours'));
  171. $this->addDatabaseEntry($headId, $headId, new \DateTime('-1 hour'));
  172. $idToVerify = $this->addDatabaseEntry($headId, $headId, new \DateTime());
  173. $manager = $this->getManager();
  174. for ($offset = 0; $offset < 3; $offset += 2) {
  175. $tree = $manager->getTree(strval($headId), 2, $offset);
  176. // Verifying the root comment
  177. $this->assertTrue(isset($tree['comment']));
  178. $this->assertTrue($tree['comment'] instanceof IComment);
  179. $this->assertSame($tree['comment']->getId(), strval($headId));
  180. $this->assertTrue(isset($tree['replies']));
  181. $this->assertSame(count($tree['replies']), 2);
  182. // one level deep
  183. foreach ($tree['replies'] as $reply) {
  184. $this->assertTrue($reply['comment'] instanceof IComment);
  185. $this->assertSame($reply['comment']->getId(), strval($idToVerify));
  186. $this->assertSame(count($reply['replies']), 0);
  187. $idToVerify--;
  188. }
  189. }
  190. }
  191. public function testGetForObject() {
  192. $this->addDatabaseEntry(0, 0);
  193. $manager = $this->getManager();
  194. $comments = $manager->getForObject('files', 'file64');
  195. $this->assertTrue(is_array($comments));
  196. $this->assertSame(count($comments), 1);
  197. $this->assertTrue($comments[0] instanceof IComment);
  198. $this->assertSame($comments[0]->getMessage(), 'nice one');
  199. }
  200. public function testGetForObjectWithLimitAndOffset() {
  201. $this->addDatabaseEntry(0, 0, new \DateTime('-6 hours'));
  202. $this->addDatabaseEntry(0, 0, new \DateTime('-5 hours'));
  203. $this->addDatabaseEntry(1, 1, new \DateTime('-4 hours'));
  204. $this->addDatabaseEntry(0, 0, new \DateTime('-3 hours'));
  205. $this->addDatabaseEntry(2, 2, new \DateTime('-2 hours'));
  206. $this->addDatabaseEntry(2, 2, new \DateTime('-1 hours'));
  207. $idToVerify = $this->addDatabaseEntry(3, 1, new \DateTime());
  208. $manager = $this->getManager();
  209. $offset = 0;
  210. do {
  211. $comments = $manager->getForObject('files', 'file64', 3, $offset);
  212. $this->assertTrue(is_array($comments));
  213. foreach ($comments as $comment) {
  214. $this->assertTrue($comment instanceof IComment);
  215. $this->assertSame($comment->getMessage(), 'nice one');
  216. $this->assertSame($comment->getId(), strval($idToVerify));
  217. $idToVerify--;
  218. }
  219. $offset += 3;
  220. } while (count($comments) > 0);
  221. }
  222. public function testGetForObjectWithDateTimeConstraint() {
  223. $this->addDatabaseEntry(0, 0, new \DateTime('-6 hours'));
  224. $this->addDatabaseEntry(0, 0, new \DateTime('-5 hours'));
  225. $id1 = $this->addDatabaseEntry(0, 0, new \DateTime('-3 hours'));
  226. $id2 = $this->addDatabaseEntry(2, 2, new \DateTime('-2 hours'));
  227. $manager = $this->getManager();
  228. $comments = $manager->getForObject('files', 'file64', 0, 0, new \DateTime('-4 hours'));
  229. $this->assertSame(count($comments), 2);
  230. $this->assertSame($comments[0]->getId(), strval($id2));
  231. $this->assertSame($comments[1]->getId(), strval($id1));
  232. }
  233. public function testGetForObjectWithLimitAndOffsetAndDateTimeConstraint() {
  234. $this->addDatabaseEntry(0, 0, new \DateTime('-7 hours'));
  235. $this->addDatabaseEntry(0, 0, new \DateTime('-6 hours'));
  236. $this->addDatabaseEntry(1, 1, new \DateTime('-5 hours'));
  237. $this->addDatabaseEntry(0, 0, new \DateTime('-3 hours'));
  238. $this->addDatabaseEntry(2, 2, new \DateTime('-2 hours'));
  239. $this->addDatabaseEntry(2, 2, new \DateTime('-1 hours'));
  240. $idToVerify = $this->addDatabaseEntry(3, 1, new \DateTime());
  241. $manager = $this->getManager();
  242. $offset = 0;
  243. do {
  244. $comments = $manager->getForObject('files', 'file64', 3, $offset, new \DateTime('-4 hours'));
  245. $this->assertTrue(is_array($comments));
  246. foreach ($comments as $comment) {
  247. $this->assertTrue($comment instanceof IComment);
  248. $this->assertSame($comment->getMessage(), 'nice one');
  249. $this->assertSame($comment->getId(), strval($idToVerify));
  250. $this->assertTrue(intval($comment->getId()) >= 4);
  251. $idToVerify--;
  252. }
  253. $offset += 3;
  254. } while (count($comments) > 0);
  255. }
  256. public function testGetNumberOfCommentsForObject() {
  257. for ($i = 1; $i < 5; $i++) {
  258. $this->addDatabaseEntry(0, 0);
  259. }
  260. $manager = $this->getManager();
  261. $amount = $manager->getNumberOfCommentsForObject('untype', '00');
  262. $this->assertSame($amount, 0);
  263. $amount = $manager->getNumberOfCommentsForObject('files', 'file64');
  264. $this->assertSame($amount, 4);
  265. }
  266. public function testGetNumberOfUnreadCommentsForFolder() {
  267. $query = $this->connection->getQueryBuilder();
  268. $query->insert('filecache')
  269. ->values([
  270. 'parent' => $query->createNamedParameter(1000),
  271. 'size' => $query->createNamedParameter(10),
  272. 'mtime' => $query->createNamedParameter(10),
  273. 'storage_mtime' => $query->createNamedParameter(10),
  274. 'path' => $query->createParameter('path'),
  275. 'path_hash' => $query->createParameter('path'),
  276. ]);
  277. $fileIds = [];
  278. for ($i = 0; $i < 4; $i++) {
  279. $query->setParameter('path', 'path_' . $i);
  280. $query->execute();
  281. $fileIds[] = $query->getLastInsertId();
  282. }
  283. // 2 comment for 1111 with 1 before read marker
  284. // 2 comments for 1112 with no read marker
  285. // 1 comment for 1113 before read marker
  286. // 1 comment for 1114 with no read marker
  287. $this->addDatabaseEntry(0, 0, null, null, $fileIds[1]);
  288. for ($i = 0; $i < 4; $i++) {
  289. $this->addDatabaseEntry(0, 0, null, null, $fileIds[$i]);
  290. }
  291. $this->addDatabaseEntry(0, 0, (new \DateTime())->modify('-2 days'), null, $fileIds[0]);
  292. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  293. $user = $this->createMock(IUser::class);
  294. $user->expects($this->any())
  295. ->method('getUID')
  296. ->willReturn('comment_test');
  297. $manager = $this->getManager();
  298. $manager->setReadMark('files', (string) $fileIds[0], (new \DateTime())->modify('-1 days'), $user);
  299. $manager->setReadMark('files', (string) $fileIds[2], (new \DateTime()), $user);
  300. $amount = $manager->getNumberOfUnreadCommentsForFolder(1000, $user);
  301. $this->assertEquals([
  302. $fileIds[0] => 1,
  303. $fileIds[1] => 2,
  304. $fileIds[3] => 1,
  305. ], $amount);
  306. }
  307. /**
  308. * @dataProvider dataGetForObjectSince
  309. * @param $lastKnown
  310. * @param $order
  311. * @param $limit
  312. * @param $resultFrom
  313. * @param $resultTo
  314. */
  315. public function testGetForObjectSince($lastKnown, $order, $limit, $resultFrom, $resultTo) {
  316. $ids = [];
  317. $ids[] = $this->addDatabaseEntry(0, 0);
  318. $ids[] = $this->addDatabaseEntry(0, 0);
  319. $ids[] = $this->addDatabaseEntry(0, 0);
  320. $ids[] = $this->addDatabaseEntry(0, 0);
  321. $ids[] = $this->addDatabaseEntry(0, 0);
  322. $manager = $this->getManager();
  323. $comments = $manager->getForObjectSince('files', 'file64', ($lastKnown === null ? 0 : $ids[$lastKnown]), $order, $limit);
  324. $expected = array_slice($ids, $resultFrom, $resultTo - $resultFrom + 1);
  325. if ($order === 'desc') {
  326. $expected = array_reverse($expected);
  327. }
  328. $this->assertSame($expected, array_map(function (IComment $c) {
  329. return (int) $c->getId();
  330. }, $comments));
  331. }
  332. public function dataGetForObjectSince() {
  333. return [
  334. [null, 'asc', 20, 0, 4],
  335. [null, 'asc', 2, 0, 1],
  336. [null, 'desc', 20, 0, 4],
  337. [null, 'desc', 2, 3, 4],
  338. [1, 'asc', 20, 2, 4],
  339. [1, 'asc', 2, 2, 3],
  340. [3, 'desc', 20, 0, 2],
  341. [3, 'desc', 2, 1, 2],
  342. ];
  343. }
  344. public function invalidCreateArgsProvider() {
  345. return [
  346. ['', 'aId-1', 'oType-1', 'oId-1'],
  347. ['aType-1', '', 'oType-1', 'oId-1'],
  348. ['aType-1', 'aId-1', '', 'oId-1'],
  349. ['aType-1', 'aId-1', 'oType-1', ''],
  350. [1, 'aId-1', 'oType-1', 'oId-1'],
  351. ['aType-1', 1, 'oType-1', 'oId-1'],
  352. ['aType-1', 'aId-1', 1, 'oId-1'],
  353. ['aType-1', 'aId-1', 'oType-1', 1],
  354. ];
  355. }
  356. /**
  357. * @dataProvider invalidCreateArgsProvider
  358. * @param string $aType
  359. * @param string $aId
  360. * @param string $oType
  361. * @param string $oId
  362. */
  363. public function testCreateCommentInvalidArguments($aType, $aId, $oType, $oId) {
  364. $this->expectException(\InvalidArgumentException::class);
  365. $manager = $this->getManager();
  366. $manager->create($aType, $aId, $oType, $oId);
  367. }
  368. public function testCreateComment() {
  369. $actorType = 'bot';
  370. $actorId = 'bob';
  371. $objectType = 'weather';
  372. $objectId = 'bielefeld';
  373. $comment = $this->getManager()->create($actorType, $actorId, $objectType, $objectId);
  374. $this->assertTrue($comment instanceof IComment);
  375. $this->assertSame($comment->getActorType(), $actorType);
  376. $this->assertSame($comment->getActorId(), $actorId);
  377. $this->assertSame($comment->getObjectType(), $objectType);
  378. $this->assertSame($comment->getObjectId(), $objectId);
  379. }
  380. public function testDelete() {
  381. $this->expectException(\OCP\Comments\NotFoundException::class);
  382. $manager = $this->getManager();
  383. $done = $manager->delete('404');
  384. $this->assertFalse($done);
  385. $done = $manager->delete('%');
  386. $this->assertFalse($done);
  387. $done = $manager->delete('');
  388. $this->assertFalse($done);
  389. $id = strval($this->addDatabaseEntry(0, 0));
  390. $comment = $manager->get($id);
  391. $this->assertTrue($comment instanceof IComment);
  392. $done = $manager->delete($id);
  393. $this->assertTrue($done);
  394. $manager->get($id);
  395. }
  396. /**
  397. * @dataProvider providerTestSave
  398. */
  399. public function testSave(string $message, string $actorId, string $verb, ?string $parentId, ?string $id = ''): IComment {
  400. $manager = $this->getManager();
  401. $comment = new Comment();
  402. $comment
  403. ->setId($id)
  404. ->setActor('users', $actorId)
  405. ->setObject('files', 'file64')
  406. ->setMessage($message)
  407. ->setVerb($verb);
  408. if ($parentId) {
  409. $comment->setParentId($parentId);
  410. }
  411. $saveSuccessful = $manager->save($comment);
  412. $this->assertTrue($saveSuccessful);
  413. $this->assertTrue($comment->getId() !== '');
  414. $this->assertTrue($comment->getId() !== '0');
  415. $this->assertTrue(!is_null($comment->getCreationDateTime()));
  416. $loadedComment = $manager->get($comment->getId());
  417. $this->assertSame($comment->getMessage(), $loadedComment->getMessage());
  418. $this->assertEquals($comment->getCreationDateTime()->getTimestamp(), $loadedComment->getCreationDateTime()->getTimestamp());
  419. return $comment;
  420. }
  421. public function providerTestSave(): array {
  422. return [
  423. ['very beautiful, I am impressed!', 'alice', 'comment', null]
  424. ];
  425. }
  426. public function testSaveUpdate() {
  427. $manager = $this->getManager();
  428. $comment = new Comment();
  429. $comment
  430. ->setActor('users', 'alice')
  431. ->setObject('files', 'file64')
  432. ->setMessage('very beautiful, I am impressed!')
  433. ->setVerb('comment')
  434. ->setExpireDate(new \DateTime('+2 hours'));
  435. $manager->save($comment);
  436. $loadedComment = $manager->get($comment->getId());
  437. // Compare current object with database values
  438. $this->assertSame($comment->getMessage(), $loadedComment->getMessage());
  439. $this->assertSame(
  440. $comment->getExpireDate()->format('Y-m-d H:i:s'),
  441. $loadedComment->getExpireDate()->format('Y-m-d H:i:s')
  442. );
  443. // Preserve the original comment to compare after update
  444. $original = clone $comment;
  445. // Update values
  446. $comment->setMessage('very beautiful, I am really so much impressed!')
  447. ->setExpireDate(new \DateTime('+1 hours'));
  448. $manager->save($comment);
  449. $loadedComment = $manager->get($comment->getId());
  450. // Compare current object with database values
  451. $this->assertSame($comment->getMessage(), $loadedComment->getMessage());
  452. $this->assertSame(
  453. $comment->getExpireDate()->format('Y-m-d H:i:s'),
  454. $loadedComment->getExpireDate()->format('Y-m-d H:i:s')
  455. );
  456. // Compare original object with database values
  457. $this->assertNotSame($original->getMessage(), $loadedComment->getMessage());
  458. $this->assertNotSame(
  459. $original->getExpireDate()->format('Y-m-d H:i:s'),
  460. $loadedComment->getExpireDate()->format('Y-m-d H:i:s')
  461. );
  462. }
  463. public function testSaveUpdateException() {
  464. $this->expectException(\OCP\Comments\NotFoundException::class);
  465. $manager = $this->getManager();
  466. $comment = new Comment();
  467. $comment
  468. ->setActor('users', 'alice')
  469. ->setObject('files', 'file64')
  470. ->setMessage('very beautiful, I am impressed!')
  471. ->setVerb('comment');
  472. $manager->save($comment);
  473. $manager->delete($comment->getId());
  474. $comment->setMessage('very beautiful, I am really so much impressed!');
  475. $manager->save($comment);
  476. }
  477. public function testSaveIncomplete() {
  478. $this->expectException(\UnexpectedValueException::class);
  479. $manager = $this->getManager();
  480. $comment = new Comment();
  481. $comment->setMessage('from no one to nothing');
  482. $manager->save($comment);
  483. }
  484. public function testSaveAsChild() {
  485. $id = $this->addDatabaseEntry(0, 0);
  486. $manager = $this->getManager();
  487. for ($i = 0; $i < 3; $i++) {
  488. $comment = new Comment();
  489. $comment
  490. ->setActor('users', 'alice')
  491. ->setObject('files', 'file64')
  492. ->setParentId(strval($id))
  493. ->setMessage('full ack')
  494. ->setVerb('comment')
  495. // setting the creation time avoids using sleep() while making sure to test with different timestamps
  496. ->setCreationDateTime(new \DateTime('+' . $i . ' minutes'));
  497. $manager->save($comment);
  498. $this->assertSame($comment->getTopmostParentId(), strval($id));
  499. $parentComment = $manager->get(strval($id));
  500. $this->assertSame($parentComment->getChildrenCount(), $i + 1);
  501. $this->assertEquals($parentComment->getLatestChildDateTime()->getTimestamp(), $comment->getCreationDateTime()->getTimestamp());
  502. }
  503. }
  504. public function invalidActorArgsProvider() {
  505. return
  506. [
  507. ['', ''],
  508. [1, 'alice'],
  509. ['users', 1],
  510. ];
  511. }
  512. /**
  513. * @dataProvider invalidActorArgsProvider
  514. * @param string $type
  515. * @param string $id
  516. */
  517. public function testDeleteReferencesOfActorInvalidInput($type, $id) {
  518. $this->expectException(\InvalidArgumentException::class);
  519. $manager = $this->getManager();
  520. $manager->deleteReferencesOfActor($type, $id);
  521. }
  522. public function testDeleteReferencesOfActor() {
  523. $ids = [];
  524. $ids[] = $this->addDatabaseEntry(0, 0);
  525. $ids[] = $this->addDatabaseEntry(0, 0);
  526. $ids[] = $this->addDatabaseEntry(0, 0);
  527. $manager = $this->getManager();
  528. // just to make sure they are really set, with correct actor data
  529. $comment = $manager->get(strval($ids[1]));
  530. $this->assertSame($comment->getActorType(), 'users');
  531. $this->assertSame($comment->getActorId(), 'alice');
  532. $wasSuccessful = $manager->deleteReferencesOfActor('users', 'alice');
  533. $this->assertTrue($wasSuccessful);
  534. foreach ($ids as $id) {
  535. $comment = $manager->get(strval($id));
  536. $this->assertSame($comment->getActorType(), ICommentsManager::DELETED_USER);
  537. $this->assertSame($comment->getActorId(), ICommentsManager::DELETED_USER);
  538. }
  539. // actor info is gone from DB, but when database interaction is alright,
  540. // we still expect to get true back
  541. $wasSuccessful = $manager->deleteReferencesOfActor('users', 'alice');
  542. $this->assertTrue($wasSuccessful);
  543. }
  544. public function testDeleteReferencesOfActorWithUserManagement() {
  545. $user = \OC::$server->getUserManager()->createUser('xenia', '123456');
  546. $this->assertTrue($user instanceof IUser);
  547. $manager = \OC::$server->getCommentsManager();
  548. $comment = $manager->create('users', $user->getUID(), 'files', 'file64');
  549. $comment
  550. ->setMessage('Most important comment I ever left on the Internet.')
  551. ->setVerb('comment');
  552. $status = $manager->save($comment);
  553. $this->assertTrue($status);
  554. $commentID = $comment->getId();
  555. $user->delete();
  556. $comment = $manager->get($commentID);
  557. $this->assertSame($comment->getActorType(), ICommentsManager::DELETED_USER);
  558. $this->assertSame($comment->getActorId(), ICommentsManager::DELETED_USER);
  559. }
  560. public function invalidObjectArgsProvider() {
  561. return
  562. [
  563. ['', ''],
  564. [1, 'file64'],
  565. ['files', 1],
  566. ];
  567. }
  568. /**
  569. * @dataProvider invalidObjectArgsProvider
  570. * @param string $type
  571. * @param string $id
  572. */
  573. public function testDeleteCommentsAtObjectInvalidInput($type, $id) {
  574. $this->expectException(\InvalidArgumentException::class);
  575. $manager = $this->getManager();
  576. $manager->deleteCommentsAtObject($type, $id);
  577. }
  578. public function testDeleteCommentsAtObject() {
  579. $ids = [];
  580. $ids[] = $this->addDatabaseEntry(0, 0);
  581. $ids[] = $this->addDatabaseEntry(0, 0);
  582. $ids[] = $this->addDatabaseEntry(0, 0);
  583. $manager = $this->getManager();
  584. // just to make sure they are really set, with correct actor data
  585. $comment = $manager->get(strval($ids[1]));
  586. $this->assertSame($comment->getObjectType(), 'files');
  587. $this->assertSame($comment->getObjectId(), 'file64');
  588. $wasSuccessful = $manager->deleteCommentsAtObject('files', 'file64');
  589. $this->assertTrue($wasSuccessful);
  590. $verified = 0;
  591. foreach ($ids as $id) {
  592. try {
  593. $manager->get(strval($id));
  594. } catch (NotFoundException $e) {
  595. $verified++;
  596. }
  597. }
  598. $this->assertSame($verified, 3);
  599. // actor info is gone from DB, but when database interaction is alright,
  600. // we still expect to get true back
  601. $wasSuccessful = $manager->deleteCommentsAtObject('files', 'file64');
  602. $this->assertTrue($wasSuccessful);
  603. }
  604. public function testDeleteCommentsExpiredAtObjectTypeAndId(): void {
  605. $ids = [];
  606. $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('+2 hours'));
  607. $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('+2 hours'));
  608. $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('+2 hours'));
  609. $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('-2 hours'));
  610. $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('-2 hours'));
  611. $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('-2 hours'));
  612. $manager = new Manager(
  613. $this->connection,
  614. $this->createMock(LoggerInterface::class),
  615. $this->createMock(IConfig::class),
  616. Server::get(ITimeFactory::class),
  617. new EmojiHelper($this->connection),
  618. $this->createMock(IInitialStateService::class)
  619. );
  620. // just to make sure they are really set, with correct actor data
  621. $comment = $manager->get((string) $ids[1]);
  622. $this->assertSame($comment->getObjectType(), 'files');
  623. $this->assertSame($comment->getObjectId(), 'file64');
  624. $deleted = $manager->deleteCommentsExpiredAtObject('files', 'file64');
  625. $this->assertTrue($deleted);
  626. $deleted = 0;
  627. $exists = 0;
  628. foreach ($ids as $id) {
  629. try {
  630. $manager->get((string) $id);
  631. $exists++;
  632. } catch (NotFoundException $e) {
  633. $deleted++;
  634. }
  635. }
  636. $this->assertSame($exists, 3);
  637. $this->assertSame($deleted, 3);
  638. // actor info is gone from DB, but when database interaction is alright,
  639. // we still expect to get true back
  640. $deleted = $manager->deleteCommentsExpiredAtObject('files', 'file64');
  641. $this->assertFalse($deleted);
  642. }
  643. public function testDeleteCommentsExpiredAtObjectType(): void {
  644. $ids = [];
  645. $ids[] = $this->addDatabaseEntry(0, 0, null, null, 'file1', new \DateTime('-2 hours'));
  646. $ids[] = $this->addDatabaseEntry(0, 0, null, null, 'file2', new \DateTime('-2 hours'));
  647. $ids[] = $this->addDatabaseEntry(0, 0, null, null, 'file3', new \DateTime('-2 hours'));
  648. $ids[] = $this->addDatabaseEntry(0, 0, null, null, 'file3', new \DateTime());
  649. $ids[] = $this->addDatabaseEntry(0, 0, null, null, 'file3', new \DateTime());
  650. $ids[] = $this->addDatabaseEntry(0, 0, null, null, 'file3', new \DateTime());
  651. $manager = new Manager(
  652. $this->connection,
  653. $this->createMock(LoggerInterface::class),
  654. $this->createMock(IConfig::class),
  655. Server::get(ITimeFactory::class),
  656. new EmojiHelper($this->connection),
  657. $this->createMock(IInitialStateService::class)
  658. );
  659. $deleted = $manager->deleteCommentsExpiredAtObject('files');
  660. $this->assertTrue($deleted);
  661. $deleted = 0;
  662. $exists = 0;
  663. foreach ($ids as $id) {
  664. try {
  665. $manager->get((string) $id);
  666. $exists++;
  667. } catch (NotFoundException $e) {
  668. $deleted++;
  669. }
  670. }
  671. $this->assertSame($exists, 0);
  672. $this->assertSame($deleted, 6);
  673. // actor info is gone from DB, but when database interaction is alright,
  674. // we still expect to get true back
  675. $deleted = $manager->deleteCommentsExpiredAtObject('files');
  676. $this->assertFalse($deleted);
  677. }
  678. public function testSetMarkRead() {
  679. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  680. $user = $this->createMock(IUser::class);
  681. $user->expects($this->any())
  682. ->method('getUID')
  683. ->willReturn('alice');
  684. $dateTimeSet = new \DateTime();
  685. $manager = $this->getManager();
  686. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  687. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  688. $this->assertEquals($dateTimeGet->getTimestamp(), $dateTimeSet->getTimestamp());
  689. }
  690. public function testSetMarkReadUpdate() {
  691. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  692. $user = $this->createMock(IUser::class);
  693. $user->expects($this->any())
  694. ->method('getUID')
  695. ->willReturn('alice');
  696. $dateTimeSet = new \DateTime('yesterday');
  697. $manager = $this->getManager();
  698. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  699. $dateTimeSet = new \DateTime('today');
  700. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  701. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  702. $this->assertEquals($dateTimeGet, $dateTimeSet);
  703. }
  704. public function testReadMarkDeleteUser() {
  705. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  706. $user = $this->createMock(IUser::class);
  707. $user->expects($this->any())
  708. ->method('getUID')
  709. ->willReturn('alice');
  710. $dateTimeSet = new \DateTime();
  711. $manager = $this->getManager();
  712. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  713. $manager->deleteReadMarksFromUser($user);
  714. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  715. $this->assertNull($dateTimeGet);
  716. }
  717. public function testReadMarkDeleteObject() {
  718. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  719. $user = $this->createMock(IUser::class);
  720. $user->expects($this->any())
  721. ->method('getUID')
  722. ->willReturn('alice');
  723. $dateTimeSet = new \DateTime();
  724. $manager = $this->getManager();
  725. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  726. $manager->deleteReadMarksOnObject('robot', '36');
  727. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  728. $this->assertNull($dateTimeGet);
  729. }
  730. public function testSendEvent() {
  731. $handler1 = $this->getMockBuilder(ICommentsEventHandler::class)->getMock();
  732. $handler1->expects($this->exactly(4))
  733. ->method('handle');
  734. $handler2 = $this->getMockBuilder(ICommentsEventHandler::class)->getMock();
  735. $handler1->expects($this->exactly(4))
  736. ->method('handle');
  737. $manager = $this->getManager();
  738. $manager->registerEventHandler(function () use ($handler1) {
  739. return $handler1;
  740. });
  741. $manager->registerEventHandler(function () use ($handler2) {
  742. return $handler2;
  743. });
  744. $comment = new Comment();
  745. $comment
  746. ->setActor('users', 'alice')
  747. ->setObject('files', 'file64')
  748. ->setMessage('very beautiful, I am impressed!')
  749. ->setVerb('comment');
  750. // Add event
  751. $manager->save($comment);
  752. // Update event
  753. $comment->setMessage('Different topic');
  754. $manager->save($comment);
  755. // Delete event
  756. $manager->delete($comment->getId());
  757. }
  758. public function testResolveDisplayName() {
  759. $manager = $this->getManager();
  760. $planetClosure = function ($name) {
  761. return ucfirst($name);
  762. };
  763. $galaxyClosure = function ($name) {
  764. return strtoupper($name);
  765. };
  766. $manager->registerDisplayNameResolver('planet', $planetClosure);
  767. $manager->registerDisplayNameResolver('galaxy', $galaxyClosure);
  768. $this->assertSame('Neptune', $manager->resolveDisplayName('planet', 'neptune'));
  769. $this->assertSame('SOMBRERO', $manager->resolveDisplayName('galaxy', 'sombrero'));
  770. }
  771. public function testRegisterResolverDuplicate() {
  772. $this->expectException(\OutOfBoundsException::class);
  773. $manager = $this->getManager();
  774. $planetClosure = function ($name) {
  775. return ucfirst($name);
  776. };
  777. $manager->registerDisplayNameResolver('planet', $planetClosure);
  778. $manager->registerDisplayNameResolver('planet', $planetClosure);
  779. }
  780. public function testRegisterResolverInvalidType() {
  781. $this->expectException(\InvalidArgumentException::class);
  782. $manager = $this->getManager();
  783. $planetClosure = function ($name) {
  784. return ucfirst($name);
  785. };
  786. $manager->registerDisplayNameResolver(1337, $planetClosure);
  787. }
  788. public function testResolveDisplayNameUnregisteredType() {
  789. $this->expectException(\OutOfBoundsException::class);
  790. $manager = $this->getManager();
  791. $planetClosure = function ($name) {
  792. return ucfirst($name);
  793. };
  794. $manager->registerDisplayNameResolver('planet', $planetClosure);
  795. $manager->resolveDisplayName('galaxy', 'sombrero');
  796. }
  797. public function testResolveDisplayNameDirtyResolver() {
  798. $manager = $this->getManager();
  799. $planetClosure = function () {
  800. return null;
  801. };
  802. $manager->registerDisplayNameResolver('planet', $planetClosure);
  803. $this->assertTrue(is_string($manager->resolveDisplayName('planet', 'neptune')));
  804. }
  805. private function skipIfNotSupport4ByteUTF() {
  806. if (!$this->getManager()->supportReactions()) {
  807. $this->markTestSkipped('MySQL doesn\'t support 4 byte UTF-8');
  808. }
  809. }
  810. /**
  811. * @dataProvider providerTestReactionAddAndDelete
  812. *
  813. * @param IComment[] $comments
  814. * @param array $reactionsExpected
  815. * @return void
  816. */
  817. public function testReactionAddAndDelete(array $comments, array $reactionsExpected) {
  818. $this->skipIfNotSupport4ByteUTF();
  819. $manager = $this->getManager();
  820. $processedComments = $this->proccessComments($comments);
  821. $comment = end($processedComments);
  822. if ($comment->getParentId()) {
  823. $parent = $manager->get($comment->getParentId());
  824. $this->assertEqualsCanonicalizing($reactionsExpected, $parent->getReactions());
  825. }
  826. }
  827. public function providerTestReactionAddAndDelete(): array {
  828. return[
  829. [
  830. [
  831. ['message', 'alice', 'comment', null],
  832. ], [],
  833. ],
  834. [
  835. [
  836. ['message', 'alice', 'comment', null],
  837. ['👍', 'alice', 'reaction', 'message#alice'],
  838. ], ['👍' => 1],
  839. ],
  840. [
  841. [
  842. ['message', 'alice', 'comment', null],
  843. ['👍', 'alice', 'reaction', 'message#alice'],
  844. ['👍', 'alice', 'reaction', 'message#alice'],
  845. ], ['👍' => 1],
  846. ],
  847. [
  848. [
  849. ['message', 'alice', 'comment', null],
  850. ['👍', 'alice', 'reaction', 'message#alice'],
  851. ['👍', 'frank', 'reaction', 'message#alice'],
  852. ], ['👍' => 2],
  853. ],
  854. [
  855. [
  856. ['message', 'alice', 'comment', null],
  857. ['👍', 'alice', 'reaction', 'message#alice'],
  858. ['👍', 'frank', 'reaction', 'message#alice'],
  859. ['👍', 'frank', 'reaction_deleted', 'message#alice'],
  860. ], ['👍' => 1],
  861. ],
  862. [
  863. [
  864. ['message', 'alice', 'comment', null],
  865. ['👍', 'alice', 'reaction', 'message#alice'],
  866. ['👍', 'frank', 'reaction', 'message#alice'],
  867. ['👍', 'alice', 'reaction_deleted', 'message#alice'],
  868. ['👍', 'frank', 'reaction_deleted', 'message#alice'],
  869. ], [],
  870. ],
  871. ];
  872. }
  873. public function testResolveDisplayNameInvalidType() {
  874. $this->expectException(\InvalidArgumentException::class);
  875. $manager = $this->getManager();
  876. $planetClosure = function () {
  877. return null;
  878. };
  879. $manager->registerDisplayNameResolver('planet', $planetClosure);
  880. $this->assertTrue(is_string($manager->resolveDisplayName(1337, 'neptune')));
  881. }
  882. /**
  883. * @param array $data
  884. * @return IComment[]
  885. */
  886. private function proccessComments(array $data): array {
  887. /** @var IComment[] */
  888. $comments = [];
  889. foreach ($data as $comment) {
  890. [$message, $actorId, $verb, $parentText] = $comment;
  891. $parentId = null;
  892. if ($parentText) {
  893. $parentId = (string) $comments[$parentText]->getId();
  894. }
  895. $id = '';
  896. if ($verb === 'reaction_deleted') {
  897. $id = $comments[$message . '#' . $actorId]->getId();
  898. }
  899. $comment = $this->testSave($message, $actorId, $verb, $parentId, $id);
  900. $comments[$comment->getMessage() . '#' . $comment->getActorId()] = $comment;
  901. }
  902. return $comments;
  903. }
  904. /**
  905. * @dataProvider providerTestRetrieveAllReactions
  906. */
  907. public function testRetrieveAllReactions(array $comments, array $expected) {
  908. $this->skipIfNotSupport4ByteUTF();
  909. $manager = $this->getManager();
  910. $processedComments = $this->proccessComments($comments);
  911. $comment = reset($processedComments);
  912. $all = $manager->retrieveAllReactions($comment->getId());
  913. $actual = array_map(function ($row) {
  914. return [
  915. 'message' => $row->getMessage(),
  916. 'actorId' => $row->getActorId(),
  917. ];
  918. }, $all);
  919. $this->assertEqualsCanonicalizing($expected, $actual);
  920. }
  921. public function providerTestRetrieveAllReactions(): array {
  922. return [
  923. [
  924. [
  925. ['message', 'alice', 'comment', null],
  926. ],
  927. [],
  928. ],
  929. [
  930. [
  931. ['message', 'alice', 'comment', null],
  932. ['👍', 'alice', 'reaction', 'message#alice'],
  933. ['👍', 'frank', 'reaction', 'message#alice'],
  934. ],
  935. [
  936. ['👍', 'alice'],
  937. ['👍', 'frank'],
  938. ],
  939. ],
  940. [
  941. [
  942. ['message', 'alice', 'comment', null],
  943. ['👍', 'alice', 'reaction', 'message#alice'],
  944. ['👍', 'alice', 'reaction', 'message#alice'],
  945. ['👍', 'frank', 'reaction', 'message#alice'],
  946. ],
  947. [
  948. ['👍', 'alice'],
  949. ['👍', 'frank'],
  950. ],
  951. ],
  952. ];
  953. }
  954. /**
  955. * @dataProvider providerTestRetrieveAllReactionsWithSpecificReaction
  956. */
  957. public function testRetrieveAllReactionsWithSpecificReaction(array $comments, string $reaction, array $expected) {
  958. $this->skipIfNotSupport4ByteUTF();
  959. $manager = $this->getManager();
  960. $processedComments = $this->proccessComments($comments);
  961. $comment = reset($processedComments);
  962. $all = $manager->retrieveAllReactionsWithSpecificReaction($comment->getId(), $reaction);
  963. $actual = array_map(function ($row) {
  964. return [
  965. 'message' => $row->getMessage(),
  966. 'actorId' => $row->getActorId(),
  967. ];
  968. }, $all);
  969. $this->assertEqualsCanonicalizing($expected, $actual);
  970. }
  971. public function providerTestRetrieveAllReactionsWithSpecificReaction(): array {
  972. return [
  973. [
  974. [
  975. ['message', 'alice', 'comment', null],
  976. ],
  977. '👎',
  978. [],
  979. ],
  980. [
  981. [
  982. ['message', 'alice', 'comment', null],
  983. ['👍', 'alice', 'reaction', 'message#alice'],
  984. ['👍', 'frank', 'reaction', 'message#alice'],
  985. ],
  986. '👍',
  987. [
  988. ['👍', 'alice'],
  989. ['👍', 'frank'],
  990. ],
  991. ],
  992. [
  993. [
  994. ['message', 'alice', 'comment', null],
  995. ['👍', 'alice', 'reaction', 'message#alice'],
  996. ['👎', 'alice', 'reaction', 'message#alice'],
  997. ['👍', 'frank', 'reaction', 'message#alice'],
  998. ],
  999. '👎',
  1000. [
  1001. ['👎', 'alice'],
  1002. ],
  1003. ],
  1004. ];
  1005. }
  1006. /**
  1007. * @dataProvider providerTestGetReactionComment
  1008. */
  1009. public function testGetReactionComment(array $comments, array $expected, bool $notFound) {
  1010. $this->skipIfNotSupport4ByteUTF();
  1011. $manager = $this->getManager();
  1012. $processedComments = $this->proccessComments($comments);
  1013. $keys = ['message', 'actorId', 'verb', 'parent'];
  1014. $expected = array_combine($keys, $expected);
  1015. if ($notFound) {
  1016. $this->expectException(\OCP\Comments\NotFoundException::class);
  1017. }
  1018. $comment = $processedComments[$expected['message'] . '#' . $expected['actorId']];
  1019. $actual = $manager->getReactionComment($comment->getParentId(), $comment->getActorType(), $comment->getActorId(), $comment->getMessage());
  1020. if (!$notFound) {
  1021. $this->assertEquals($expected['message'], $actual->getMessage());
  1022. $this->assertEquals($expected['actorId'], $actual->getActorId());
  1023. $this->assertEquals($expected['verb'], $actual->getVerb());
  1024. $this->assertEquals($processedComments[$expected['parent']]->getId(), $actual->getParentId());
  1025. }
  1026. }
  1027. public function providerTestGetReactionComment(): array {
  1028. return [
  1029. [
  1030. [
  1031. ['message', 'Matthew', 'comment', null],
  1032. ['👍', 'Matthew', 'reaction', 'message#Matthew'],
  1033. ['👍', 'Mark', 'reaction', 'message#Matthew'],
  1034. ['👍', 'Luke', 'reaction', 'message#Matthew'],
  1035. ['👍', 'John', 'reaction', 'message#Matthew'],
  1036. ],
  1037. ['👍', 'Matthew', 'reaction', 'message#Matthew'],
  1038. false,
  1039. ],
  1040. [
  1041. [
  1042. ['message', 'Matthew', 'comment', null],
  1043. ['👍', 'Matthew', 'reaction', 'message#Matthew'],
  1044. ['👍', 'Mark', 'reaction', 'message#Matthew'],
  1045. ['👍', 'Luke', 'reaction', 'message#Matthew'],
  1046. ['👍', 'John', 'reaction', 'message#Matthew'],
  1047. ],
  1048. ['👍', 'Mark', 'reaction', 'message#Matthew'],
  1049. false,
  1050. ],
  1051. [
  1052. [
  1053. ['message', 'Matthew', 'comment', null],
  1054. ['👎', 'Matthew', 'reaction', 'message#Matthew'],
  1055. ],
  1056. ['👎', 'Matthew', 'reaction', 'message#Matthew'],
  1057. false,
  1058. ],
  1059. [
  1060. [
  1061. ['message', 'Matthew', 'comment', null],
  1062. ['👎', 'Matthew', 'reaction', 'message#Matthew'],
  1063. ['👎', 'Matthew', 'reaction_deleted', 'message#Matthew'],
  1064. ],
  1065. ['👎', 'Matthew', 'reaction', 'message#Matthew'],
  1066. true,
  1067. ],
  1068. ];
  1069. }
  1070. /**
  1071. * @dataProvider providerTestReactionMessageSize
  1072. */
  1073. public function testReactionMessageSize($reactionString, $valid) {
  1074. $this->skipIfNotSupport4ByteUTF();
  1075. if (!$valid) {
  1076. $this->expectException(\UnexpectedValueException::class);
  1077. }
  1078. $manager = $this->getManager();
  1079. $comment = new Comment();
  1080. $comment->setMessage($reactionString)
  1081. ->setVerb('reaction')
  1082. ->setActor('users', 'alice')
  1083. ->setObject('files', 'file64');
  1084. $status = $manager->save($comment);
  1085. $this->assertTrue($status);
  1086. }
  1087. public function providerTestReactionMessageSize(): array {
  1088. return [
  1089. ['a', false],
  1090. ['1', false],
  1091. ['👍', true],
  1092. ['👍👍', false],
  1093. ['👍🏽', true],
  1094. ['👨🏽‍💻', true],
  1095. ['👨🏽‍💻👍', false],
  1096. ];
  1097. }
  1098. /**
  1099. * @dataProvider providerTestReactionsSummarizeOrdered
  1100. */
  1101. public function testReactionsSummarizeOrdered(array $comments, array $expected, bool $isFullMatch) {
  1102. $this->skipIfNotSupport4ByteUTF();
  1103. $manager = $this->getManager();
  1104. $processedComments = $this->proccessComments($comments);
  1105. $comment = end($processedComments);
  1106. $actual = $manager->get($comment->getParentId());
  1107. if ($isFullMatch) {
  1108. $this->assertSame($expected, $actual->getReactions());
  1109. } else {
  1110. $subResult = array_slice($actual->getReactions(), 0, count($expected));
  1111. $this->assertSame($expected, $subResult);
  1112. }
  1113. }
  1114. public function providerTestReactionsSummarizeOrdered(): array {
  1115. return [
  1116. [
  1117. [
  1118. ['message', 'alice', 'comment', null],
  1119. ['👍', 'alice', 'reaction', 'message#alice'],
  1120. ],
  1121. ['👍' => 1],
  1122. true,
  1123. ],
  1124. [
  1125. [
  1126. ['message', 'alice', 'comment', null],
  1127. ['👎', 'John', 'reaction', 'message#alice'],
  1128. ['💼', 'Luke', 'reaction', 'message#alice'],
  1129. ['📋', 'Luke', 'reaction', 'message#alice'],
  1130. ['🚀', 'Luke', 'reaction', 'message#alice'],
  1131. ['🖤', 'Luke', 'reaction', 'message#alice'],
  1132. ['😜', 'Luke', 'reaction', 'message#alice'],
  1133. ['🌖', 'Luke', 'reaction', 'message#alice'],
  1134. ['💖', 'Luke', 'reaction', 'message#alice'],
  1135. ['📥', 'Luke', 'reaction', 'message#alice'],
  1136. ['🐉', 'Luke', 'reaction', 'message#alice'],
  1137. ['☕', 'Luke', 'reaction', 'message#alice'],
  1138. ['🐄', 'Luke', 'reaction', 'message#alice'],
  1139. ['🐕', 'Luke', 'reaction', 'message#alice'],
  1140. ['🐈', 'Luke', 'reaction', 'message#alice'],
  1141. ['🛂', 'Luke', 'reaction', 'message#alice'],
  1142. ['🕸', 'Luke', 'reaction', 'message#alice'],
  1143. ['🏰', 'Luke', 'reaction', 'message#alice'],
  1144. ['⚙️', 'Luke', 'reaction', 'message#alice'],
  1145. ['🚨', 'Luke', 'reaction', 'message#alice'],
  1146. ['👥', 'Luke', 'reaction', 'message#alice'],
  1147. ['👍', 'Paul', 'reaction', 'message#alice'],
  1148. ['👍', 'Peter', 'reaction', 'message#alice'],
  1149. ['💜', 'Matthew', 'reaction', 'message#alice'],
  1150. ['💜', 'Mark', 'reaction', 'message#alice'],
  1151. ['💜', 'Luke', 'reaction', 'message#alice'],
  1152. ],
  1153. [
  1154. '💜' => 3,
  1155. '👍' => 2,
  1156. ],
  1157. false,
  1158. ],
  1159. ];
  1160. }
  1161. }