ManagerTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. <?php
  2. namespace Test\Comments;
  3. use OC\Comments\Comment;
  4. use OC\Comments\ManagerFactory;
  5. use OCP\Comments\IComment;
  6. use OCP\Comments\ICommentsEventHandler;
  7. use OCP\Comments\ICommentsManager;
  8. use OCP\Comments\NotFoundException;
  9. use OCP\IDBConnection;
  10. use OCP\IUser;
  11. use Test\TestCase;
  12. /**
  13. * Class ManagerTest
  14. *
  15. * @group DB
  16. */
  17. class ManagerTest extends TestCase {
  18. /** @var IDBConnection */
  19. private $connection;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->connection = \OC::$server->getDatabaseConnection();
  23. $sql = $this->connection->getDatabasePlatform()->getTruncateTableSQL('`*PREFIX*comments`');
  24. $this->connection->prepare($sql)->execute();
  25. }
  26. protected function addDatabaseEntry($parentId, $topmostParentId, $creationDT = null, $latestChildDT = null, $objectId = null) {
  27. if (is_null($creationDT)) {
  28. $creationDT = new \DateTime();
  29. }
  30. if (is_null($latestChildDT)) {
  31. $latestChildDT = new \DateTime('yesterday');
  32. }
  33. if (is_null($objectId)) {
  34. $objectId = 'file64';
  35. }
  36. $qb = $this->connection->getQueryBuilder();
  37. $qb
  38. ->insert('comments')
  39. ->values([
  40. 'parent_id' => $qb->createNamedParameter($parentId),
  41. 'topmost_parent_id' => $qb->createNamedParameter($topmostParentId),
  42. 'children_count' => $qb->createNamedParameter(2),
  43. 'actor_type' => $qb->createNamedParameter('users'),
  44. 'actor_id' => $qb->createNamedParameter('alice'),
  45. 'message' => $qb->createNamedParameter('nice one'),
  46. 'verb' => $qb->createNamedParameter('comment'),
  47. 'creation_timestamp' => $qb->createNamedParameter($creationDT, 'datetime'),
  48. 'latest_child_timestamp' => $qb->createNamedParameter($latestChildDT, 'datetime'),
  49. 'object_type' => $qb->createNamedParameter('files'),
  50. 'object_id' => $qb->createNamedParameter($objectId),
  51. ])
  52. ->execute();
  53. return $qb->getLastInsertId();
  54. }
  55. protected function getManager() {
  56. $factory = new ManagerFactory(\OC::$server);
  57. return $factory->getManager();
  58. }
  59. public function testGetCommentNotFound() {
  60. $this->expectException(\OCP\Comments\NotFoundException::class);
  61. $manager = $this->getManager();
  62. $manager->get('22');
  63. }
  64. public function testGetCommentNotFoundInvalidInput() {
  65. $this->expectException(\InvalidArgumentException::class);
  66. $manager = $this->getManager();
  67. $manager->get('unexisting22');
  68. }
  69. public function testGetComment() {
  70. $manager = $this->getManager();
  71. $creationDT = new \DateTime();
  72. $latestChildDT = new \DateTime('yesterday');
  73. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  74. $qb
  75. ->insert('comments')
  76. ->values([
  77. 'parent_id' => $qb->createNamedParameter('2'),
  78. 'topmost_parent_id' => $qb->createNamedParameter('1'),
  79. 'children_count' => $qb->createNamedParameter(2),
  80. 'actor_type' => $qb->createNamedParameter('users'),
  81. 'actor_id' => $qb->createNamedParameter('alice'),
  82. 'message' => $qb->createNamedParameter('nice one'),
  83. 'verb' => $qb->createNamedParameter('comment'),
  84. 'creation_timestamp' => $qb->createNamedParameter($creationDT, 'datetime'),
  85. 'latest_child_timestamp' => $qb->createNamedParameter($latestChildDT, 'datetime'),
  86. 'object_type' => $qb->createNamedParameter('files'),
  87. 'object_id' => $qb->createNamedParameter('file64'),
  88. ])
  89. ->execute();
  90. $id = strval($qb->getLastInsertId());
  91. $comment = $manager->get($id);
  92. $this->assertTrue($comment instanceof IComment);
  93. $this->assertSame($comment->getId(), $id);
  94. $this->assertSame($comment->getParentId(), '2');
  95. $this->assertSame($comment->getTopmostParentId(), '1');
  96. $this->assertSame($comment->getChildrenCount(), 2);
  97. $this->assertSame($comment->getActorType(), 'users');
  98. $this->assertSame($comment->getActorId(), 'alice');
  99. $this->assertSame($comment->getMessage(), 'nice one');
  100. $this->assertSame($comment->getVerb(), 'comment');
  101. $this->assertSame($comment->getObjectType(), 'files');
  102. $this->assertSame($comment->getObjectId(), 'file64');
  103. $this->assertEquals($comment->getCreationDateTime()->getTimestamp(), $creationDT->getTimestamp());
  104. $this->assertEquals($comment->getLatestChildDateTime(), $latestChildDT);
  105. }
  106. public function testGetTreeNotFound() {
  107. $this->expectException(\OCP\Comments\NotFoundException::class);
  108. $manager = $this->getManager();
  109. $manager->getTree('22');
  110. }
  111. public function testGetTreeNotFoundInvalidIpnut() {
  112. $this->expectException(\InvalidArgumentException::class);
  113. $manager = $this->getManager();
  114. $manager->getTree('unexisting22');
  115. }
  116. public function testGetTree() {
  117. $headId = $this->addDatabaseEntry(0, 0);
  118. $this->addDatabaseEntry($headId, $headId, new \DateTime('-3 hours'));
  119. $this->addDatabaseEntry($headId, $headId, new \DateTime('-2 hours'));
  120. $id = $this->addDatabaseEntry($headId, $headId, new \DateTime('-1 hour'));
  121. $manager = $this->getManager();
  122. $tree = $manager->getTree($headId);
  123. // Verifying the root comment
  124. $this->assertTrue(isset($tree['comment']));
  125. $this->assertTrue($tree['comment'] instanceof IComment);
  126. $this->assertSame($tree['comment']->getId(), strval($headId));
  127. $this->assertTrue(isset($tree['replies']));
  128. $this->assertSame(count($tree['replies']), 3);
  129. // one level deep
  130. foreach ($tree['replies'] as $reply) {
  131. $this->assertTrue($reply['comment'] instanceof IComment);
  132. $this->assertSame($reply['comment']->getId(), strval($id));
  133. $this->assertSame(count($reply['replies']), 0);
  134. $id--;
  135. }
  136. }
  137. public function testGetTreeNoReplies() {
  138. $id = $this->addDatabaseEntry(0, 0);
  139. $manager = $this->getManager();
  140. $tree = $manager->getTree($id);
  141. // Verifying the root comment
  142. $this->assertTrue(isset($tree['comment']));
  143. $this->assertTrue($tree['comment'] instanceof IComment);
  144. $this->assertSame($tree['comment']->getId(), strval($id));
  145. $this->assertTrue(isset($tree['replies']));
  146. $this->assertSame(count($tree['replies']), 0);
  147. // one level deep
  148. foreach ($tree['replies'] as $reply) {
  149. throw new \Exception('This ain`t happen');
  150. }
  151. }
  152. public function testGetTreeWithLimitAndOffset() {
  153. $headId = $this->addDatabaseEntry(0, 0);
  154. $this->addDatabaseEntry($headId, $headId, new \DateTime('-3 hours'));
  155. $this->addDatabaseEntry($headId, $headId, new \DateTime('-2 hours'));
  156. $this->addDatabaseEntry($headId, $headId, new \DateTime('-1 hour'));
  157. $idToVerify = $this->addDatabaseEntry($headId, $headId, new \DateTime());
  158. $manager = $this->getManager();
  159. for ($offset = 0; $offset < 3; $offset += 2) {
  160. $tree = $manager->getTree(strval($headId), 2, $offset);
  161. // Verifying the root comment
  162. $this->assertTrue(isset($tree['comment']));
  163. $this->assertTrue($tree['comment'] instanceof IComment);
  164. $this->assertSame($tree['comment']->getId(), strval($headId));
  165. $this->assertTrue(isset($tree['replies']));
  166. $this->assertSame(count($tree['replies']), 2);
  167. // one level deep
  168. foreach ($tree['replies'] as $reply) {
  169. $this->assertTrue($reply['comment'] instanceof IComment);
  170. $this->assertSame($reply['comment']->getId(), strval($idToVerify));
  171. $this->assertSame(count($reply['replies']), 0);
  172. $idToVerify--;
  173. }
  174. }
  175. }
  176. public function testGetForObject() {
  177. $this->addDatabaseEntry(0, 0);
  178. $manager = $this->getManager();
  179. $comments = $manager->getForObject('files', 'file64');
  180. $this->assertTrue(is_array($comments));
  181. $this->assertSame(count($comments), 1);
  182. $this->assertTrue($comments[0] instanceof IComment);
  183. $this->assertSame($comments[0]->getMessage(), 'nice one');
  184. }
  185. public function testGetForObjectWithLimitAndOffset() {
  186. $this->addDatabaseEntry(0, 0, new \DateTime('-6 hours'));
  187. $this->addDatabaseEntry(0, 0, new \DateTime('-5 hours'));
  188. $this->addDatabaseEntry(1, 1, new \DateTime('-4 hours'));
  189. $this->addDatabaseEntry(0, 0, new \DateTime('-3 hours'));
  190. $this->addDatabaseEntry(2, 2, new \DateTime('-2 hours'));
  191. $this->addDatabaseEntry(2, 2, new \DateTime('-1 hours'));
  192. $idToVerify = $this->addDatabaseEntry(3, 1, new \DateTime());
  193. $manager = $this->getManager();
  194. $offset = 0;
  195. do {
  196. $comments = $manager->getForObject('files', 'file64', 3, $offset);
  197. $this->assertTrue(is_array($comments));
  198. foreach ($comments as $comment) {
  199. $this->assertTrue($comment instanceof IComment);
  200. $this->assertSame($comment->getMessage(), 'nice one');
  201. $this->assertSame($comment->getId(), strval($idToVerify));
  202. $idToVerify--;
  203. }
  204. $offset += 3;
  205. } while (count($comments) > 0);
  206. }
  207. public function testGetForObjectWithDateTimeConstraint() {
  208. $this->addDatabaseEntry(0, 0, new \DateTime('-6 hours'));
  209. $this->addDatabaseEntry(0, 0, new \DateTime('-5 hours'));
  210. $id1 = $this->addDatabaseEntry(0, 0, new \DateTime('-3 hours'));
  211. $id2 = $this->addDatabaseEntry(2, 2, new \DateTime('-2 hours'));
  212. $manager = $this->getManager();
  213. $comments = $manager->getForObject('files', 'file64', 0, 0, new \DateTime('-4 hours'));
  214. $this->assertSame(count($comments), 2);
  215. $this->assertSame($comments[0]->getId(), strval($id2));
  216. $this->assertSame($comments[1]->getId(), strval($id1));
  217. }
  218. public function testGetForObjectWithLimitAndOffsetAndDateTimeConstraint() {
  219. $this->addDatabaseEntry(0, 0, new \DateTime('-7 hours'));
  220. $this->addDatabaseEntry(0, 0, new \DateTime('-6 hours'));
  221. $this->addDatabaseEntry(1, 1, new \DateTime('-5 hours'));
  222. $this->addDatabaseEntry(0, 0, new \DateTime('-3 hours'));
  223. $this->addDatabaseEntry(2, 2, new \DateTime('-2 hours'));
  224. $this->addDatabaseEntry(2, 2, new \DateTime('-1 hours'));
  225. $idToVerify = $this->addDatabaseEntry(3, 1, new \DateTime());
  226. $manager = $this->getManager();
  227. $offset = 0;
  228. do {
  229. $comments = $manager->getForObject('files', 'file64', 3, $offset, new \DateTime('-4 hours'));
  230. $this->assertTrue(is_array($comments));
  231. foreach ($comments as $comment) {
  232. $this->assertTrue($comment instanceof IComment);
  233. $this->assertSame($comment->getMessage(), 'nice one');
  234. $this->assertSame($comment->getId(), strval($idToVerify));
  235. $this->assertTrue(intval($comment->getId()) >= 4);
  236. $idToVerify--;
  237. }
  238. $offset += 3;
  239. } while (count($comments) > 0);
  240. }
  241. public function testGetNumberOfCommentsForObject() {
  242. for ($i = 1; $i < 5; $i++) {
  243. $this->addDatabaseEntry(0, 0);
  244. }
  245. $manager = $this->getManager();
  246. $amount = $manager->getNumberOfCommentsForObject('untype', '00');
  247. $this->assertSame($amount, 0);
  248. $amount = $manager->getNumberOfCommentsForObject('files', 'file64');
  249. $this->assertSame($amount, 4);
  250. }
  251. public function testGetNumberOfUnreadCommentsForFolder() {
  252. $query = $this->connection->getQueryBuilder();
  253. $query->insert('filecache')
  254. ->values([
  255. 'parent' => $query->createNamedParameter(1000),
  256. 'size' => $query->createNamedParameter(10),
  257. 'mtime' => $query->createNamedParameter(10),
  258. 'storage_mtime' => $query->createNamedParameter(10),
  259. 'path' => $query->createParameter('path'),
  260. 'path_hash' => $query->createParameter('path'),
  261. ]);
  262. $fileIds = [];
  263. for ($i = 0; $i < 4; $i++) {
  264. $query->setParameter('path', 'path_' . $i);
  265. $query->execute();
  266. $fileIds[] = $query->getLastInsertId();
  267. }
  268. // 2 comment for 1111 with 1 before read marker
  269. // 2 comments for 1112 with no read marker
  270. // 1 comment for 1113 before read marker
  271. // 1 comment for 1114 with no read marker
  272. $this->addDatabaseEntry(0, 0, null, null, $fileIds[1]);
  273. for ($i = 0; $i < 4; $i++) {
  274. $this->addDatabaseEntry(0, 0, null, null, $fileIds[$i]);
  275. }
  276. $this->addDatabaseEntry(0, 0, (new \DateTime())->modify('-2 days'), null, $fileIds[0]);
  277. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  278. $user = $this->createMock(IUser::class);
  279. $user->expects($this->any())
  280. ->method('getUID')
  281. ->willReturn('comment_test');
  282. $manager = $this->getManager();
  283. $manager->setReadMark('files', (string) $fileIds[0], (new \DateTime())->modify('-1 days'), $user);
  284. $manager->setReadMark('files', (string) $fileIds[2], (new \DateTime()), $user);
  285. $amount = $manager->getNumberOfUnreadCommentsForFolder(1000, $user);
  286. $this->assertEquals([
  287. $fileIds[0] => 1,
  288. $fileIds[1] => 2,
  289. $fileIds[3] => 1,
  290. ], $amount);
  291. }
  292. /**
  293. * @dataProvider dataGetForObjectSince
  294. * @param $lastKnown
  295. * @param $order
  296. * @param $limit
  297. * @param $resultFrom
  298. * @param $resultTo
  299. */
  300. public function testGetForObjectSince($lastKnown, $order, $limit, $resultFrom, $resultTo) {
  301. $ids = [];
  302. $ids[] = $this->addDatabaseEntry(0, 0);
  303. $ids[] = $this->addDatabaseEntry(0, 0);
  304. $ids[] = $this->addDatabaseEntry(0, 0);
  305. $ids[] = $this->addDatabaseEntry(0, 0);
  306. $ids[] = $this->addDatabaseEntry(0, 0);
  307. $manager = $this->getManager();
  308. $comments = $manager->getForObjectSince('files', 'file64', ($lastKnown === null ? 0 : $ids[$lastKnown]), $order, $limit);
  309. $expected = array_slice($ids, $resultFrom, $resultTo - $resultFrom + 1);
  310. if ($order === 'desc') {
  311. $expected = array_reverse($expected);
  312. }
  313. $this->assertSame($expected, array_map(function (IComment $c) {
  314. return (int) $c->getId();
  315. }, $comments));
  316. }
  317. public function dataGetForObjectSince() {
  318. return [
  319. [null, 'asc', 20, 0, 4],
  320. [null, 'asc', 2, 0, 1],
  321. [null, 'desc', 20, 0, 4],
  322. [null, 'desc', 2, 3, 4],
  323. [1, 'asc', 20, 2, 4],
  324. [1, 'asc', 2, 2, 3],
  325. [3, 'desc', 20, 0, 2],
  326. [3, 'desc', 2, 1, 2],
  327. ];
  328. }
  329. public function invalidCreateArgsProvider() {
  330. return [
  331. ['', 'aId-1', 'oType-1', 'oId-1'],
  332. ['aType-1', '', 'oType-1', 'oId-1'],
  333. ['aType-1', 'aId-1', '', 'oId-1'],
  334. ['aType-1', 'aId-1', 'oType-1', ''],
  335. [1, 'aId-1', 'oType-1', 'oId-1'],
  336. ['aType-1', 1, 'oType-1', 'oId-1'],
  337. ['aType-1', 'aId-1', 1, 'oId-1'],
  338. ['aType-1', 'aId-1', 'oType-1', 1],
  339. ];
  340. }
  341. /**
  342. * @dataProvider invalidCreateArgsProvider
  343. * @param string $aType
  344. * @param string $aId
  345. * @param string $oType
  346. * @param string $oId
  347. */
  348. public function testCreateCommentInvalidArguments($aType, $aId, $oType, $oId) {
  349. $this->expectException(\InvalidArgumentException::class);
  350. $manager = $this->getManager();
  351. $manager->create($aType, $aId, $oType, $oId);
  352. }
  353. public function testCreateComment() {
  354. $actorType = 'bot';
  355. $actorId = 'bob';
  356. $objectType = 'weather';
  357. $objectId = 'bielefeld';
  358. $comment = $this->getManager()->create($actorType, $actorId, $objectType, $objectId);
  359. $this->assertTrue($comment instanceof IComment);
  360. $this->assertSame($comment->getActorType(), $actorType);
  361. $this->assertSame($comment->getActorId(), $actorId);
  362. $this->assertSame($comment->getObjectType(), $objectType);
  363. $this->assertSame($comment->getObjectId(), $objectId);
  364. }
  365. public function testDelete() {
  366. $this->expectException(\OCP\Comments\NotFoundException::class);
  367. $manager = $this->getManager();
  368. $done = $manager->delete('404');
  369. $this->assertFalse($done);
  370. $done = $manager->delete('%');
  371. $this->assertFalse($done);
  372. $done = $manager->delete('');
  373. $this->assertFalse($done);
  374. $id = strval($this->addDatabaseEntry(0, 0));
  375. $comment = $manager->get($id);
  376. $this->assertTrue($comment instanceof IComment);
  377. $done = $manager->delete($id);
  378. $this->assertTrue($done);
  379. $manager->get($id);
  380. }
  381. public function testSaveNew() {
  382. $manager = $this->getManager();
  383. $comment = new Comment();
  384. $comment
  385. ->setActor('users', 'alice')
  386. ->setObject('files', 'file64')
  387. ->setMessage('very beautiful, I am impressed!')
  388. ->setVerb('comment');
  389. $saveSuccessful = $manager->save($comment);
  390. $this->assertTrue($saveSuccessful);
  391. $this->assertTrue($comment->getId() !== '');
  392. $this->assertTrue($comment->getId() !== '0');
  393. $this->assertTrue(!is_null($comment->getCreationDateTime()));
  394. $loadedComment = $manager->get($comment->getId());
  395. $this->assertSame($comment->getMessage(), $loadedComment->getMessage());
  396. $this->assertEquals($comment->getCreationDateTime()->getTimestamp(), $loadedComment->getCreationDateTime()->getTimestamp());
  397. }
  398. public function testSaveUpdate() {
  399. $manager = $this->getManager();
  400. $comment = new Comment();
  401. $comment
  402. ->setActor('users', 'alice')
  403. ->setObject('files', 'file64')
  404. ->setMessage('very beautiful, I am impressed!')
  405. ->setVerb('comment');
  406. $manager->save($comment);
  407. $comment->setMessage('very beautiful, I am really so much impressed!');
  408. $manager->save($comment);
  409. $loadedComment = $manager->get($comment->getId());
  410. $this->assertSame($comment->getMessage(), $loadedComment->getMessage());
  411. }
  412. public function testSaveUpdateException() {
  413. $this->expectException(\OCP\Comments\NotFoundException::class);
  414. $manager = $this->getManager();
  415. $comment = new Comment();
  416. $comment
  417. ->setActor('users', 'alice')
  418. ->setObject('files', 'file64')
  419. ->setMessage('very beautiful, I am impressed!')
  420. ->setVerb('comment');
  421. $manager->save($comment);
  422. $manager->delete($comment->getId());
  423. $comment->setMessage('very beautiful, I am really so much impressed!');
  424. $manager->save($comment);
  425. }
  426. public function testSaveIncomplete() {
  427. $this->expectException(\UnexpectedValueException::class);
  428. $manager = $this->getManager();
  429. $comment = new Comment();
  430. $comment->setMessage('from no one to nothing');
  431. $manager->save($comment);
  432. }
  433. public function testSaveAsChild() {
  434. $id = $this->addDatabaseEntry(0, 0);
  435. $manager = $this->getManager();
  436. for ($i = 0; $i < 3; $i++) {
  437. $comment = new Comment();
  438. $comment
  439. ->setActor('users', 'alice')
  440. ->setObject('files', 'file64')
  441. ->setParentId(strval($id))
  442. ->setMessage('full ack')
  443. ->setVerb('comment')
  444. // setting the creation time avoids using sleep() while making sure to test with different timestamps
  445. ->setCreationDateTime(new \DateTime('+' . $i . ' minutes'));
  446. $manager->save($comment);
  447. $this->assertSame($comment->getTopmostParentId(), strval($id));
  448. $parentComment = $manager->get(strval($id));
  449. $this->assertSame($parentComment->getChildrenCount(), $i + 1);
  450. $this->assertEquals($parentComment->getLatestChildDateTime()->getTimestamp(), $comment->getCreationDateTime()->getTimestamp());
  451. }
  452. }
  453. public function invalidActorArgsProvider() {
  454. return
  455. [
  456. ['', ''],
  457. [1, 'alice'],
  458. ['users', 1],
  459. ];
  460. }
  461. /**
  462. * @dataProvider invalidActorArgsProvider
  463. * @param string $type
  464. * @param string $id
  465. */
  466. public function testDeleteReferencesOfActorInvalidInput($type, $id) {
  467. $this->expectException(\InvalidArgumentException::class);
  468. $manager = $this->getManager();
  469. $manager->deleteReferencesOfActor($type, $id);
  470. }
  471. public function testDeleteReferencesOfActor() {
  472. $ids = [];
  473. $ids[] = $this->addDatabaseEntry(0, 0);
  474. $ids[] = $this->addDatabaseEntry(0, 0);
  475. $ids[] = $this->addDatabaseEntry(0, 0);
  476. $manager = $this->getManager();
  477. // just to make sure they are really set, with correct actor data
  478. $comment = $manager->get(strval($ids[1]));
  479. $this->assertSame($comment->getActorType(), 'users');
  480. $this->assertSame($comment->getActorId(), 'alice');
  481. $wasSuccessful = $manager->deleteReferencesOfActor('users', 'alice');
  482. $this->assertTrue($wasSuccessful);
  483. foreach ($ids as $id) {
  484. $comment = $manager->get(strval($id));
  485. $this->assertSame($comment->getActorType(), ICommentsManager::DELETED_USER);
  486. $this->assertSame($comment->getActorId(), ICommentsManager::DELETED_USER);
  487. }
  488. // actor info is gone from DB, but when database interaction is alright,
  489. // we still expect to get true back
  490. $wasSuccessful = $manager->deleteReferencesOfActor('users', 'alice');
  491. $this->assertTrue($wasSuccessful);
  492. }
  493. public function testDeleteReferencesOfActorWithUserManagement() {
  494. $user = \OC::$server->getUserManager()->createUser('xenia', '123456');
  495. $this->assertTrue($user instanceof IUser);
  496. $manager = \OC::$server->getCommentsManager();
  497. $comment = $manager->create('users', $user->getUID(), 'files', 'file64');
  498. $comment
  499. ->setMessage('Most important comment I ever left on the Internet.')
  500. ->setVerb('comment');
  501. $status = $manager->save($comment);
  502. $this->assertTrue($status);
  503. $commentID = $comment->getId();
  504. $user->delete();
  505. $comment = $manager->get($commentID);
  506. $this->assertSame($comment->getActorType(), ICommentsManager::DELETED_USER);
  507. $this->assertSame($comment->getActorId(), ICommentsManager::DELETED_USER);
  508. }
  509. public function invalidObjectArgsProvider() {
  510. return
  511. [
  512. ['', ''],
  513. [1, 'file64'],
  514. ['files', 1],
  515. ];
  516. }
  517. /**
  518. * @dataProvider invalidObjectArgsProvider
  519. * @param string $type
  520. * @param string $id
  521. */
  522. public function testDeleteCommentsAtObjectInvalidInput($type, $id) {
  523. $this->expectException(\InvalidArgumentException::class);
  524. $manager = $this->getManager();
  525. $manager->deleteCommentsAtObject($type, $id);
  526. }
  527. public function testDeleteCommentsAtObject() {
  528. $ids = [];
  529. $ids[] = $this->addDatabaseEntry(0, 0);
  530. $ids[] = $this->addDatabaseEntry(0, 0);
  531. $ids[] = $this->addDatabaseEntry(0, 0);
  532. $manager = $this->getManager();
  533. // just to make sure they are really set, with correct actor data
  534. $comment = $manager->get(strval($ids[1]));
  535. $this->assertSame($comment->getObjectType(), 'files');
  536. $this->assertSame($comment->getObjectId(), 'file64');
  537. $wasSuccessful = $manager->deleteCommentsAtObject('files', 'file64');
  538. $this->assertTrue($wasSuccessful);
  539. $verified = 0;
  540. foreach ($ids as $id) {
  541. try {
  542. $manager->get(strval($id));
  543. } catch (NotFoundException $e) {
  544. $verified++;
  545. }
  546. }
  547. $this->assertSame($verified, 3);
  548. // actor info is gone from DB, but when database interaction is alright,
  549. // we still expect to get true back
  550. $wasSuccessful = $manager->deleteCommentsAtObject('files', 'file64');
  551. $this->assertTrue($wasSuccessful);
  552. }
  553. public function testSetMarkRead() {
  554. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  555. $user = $this->createMock(IUser::class);
  556. $user->expects($this->any())
  557. ->method('getUID')
  558. ->willReturn('alice');
  559. $dateTimeSet = new \DateTime();
  560. $manager = $this->getManager();
  561. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  562. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  563. $this->assertEquals($dateTimeGet->getTimestamp(), $dateTimeSet->getTimestamp());
  564. }
  565. public function testSetMarkReadUpdate() {
  566. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  567. $user = $this->createMock(IUser::class);
  568. $user->expects($this->any())
  569. ->method('getUID')
  570. ->willReturn('alice');
  571. $dateTimeSet = new \DateTime('yesterday');
  572. $manager = $this->getManager();
  573. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  574. $dateTimeSet = new \DateTime('today');
  575. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  576. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  577. $this->assertEquals($dateTimeGet, $dateTimeSet);
  578. }
  579. public function testReadMarkDeleteUser() {
  580. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  581. $user = $this->createMock(IUser::class);
  582. $user->expects($this->any())
  583. ->method('getUID')
  584. ->willReturn('alice');
  585. $dateTimeSet = new \DateTime();
  586. $manager = $this->getManager();
  587. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  588. $manager->deleteReadMarksFromUser($user);
  589. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  590. $this->assertNull($dateTimeGet);
  591. }
  592. public function testReadMarkDeleteObject() {
  593. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  594. $user = $this->createMock(IUser::class);
  595. $user->expects($this->any())
  596. ->method('getUID')
  597. ->willReturn('alice');
  598. $dateTimeSet = new \DateTime();
  599. $manager = $this->getManager();
  600. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  601. $manager->deleteReadMarksOnObject('robot', '36');
  602. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  603. $this->assertNull($dateTimeGet);
  604. }
  605. public function testSendEvent() {
  606. $handler1 = $this->getMockBuilder(ICommentsEventHandler::class)->getMock();
  607. $handler1->expects($this->exactly(4))
  608. ->method('handle');
  609. $handler2 = $this->getMockBuilder(ICommentsEventHandler::class)->getMock();
  610. $handler1->expects($this->exactly(4))
  611. ->method('handle');
  612. $manager = $this->getManager();
  613. $manager->registerEventHandler(function () use ($handler1) {
  614. return $handler1;
  615. });
  616. $manager->registerEventHandler(function () use ($handler2) {
  617. return $handler2;
  618. });
  619. $comment = new Comment();
  620. $comment
  621. ->setActor('users', 'alice')
  622. ->setObject('files', 'file64')
  623. ->setMessage('very beautiful, I am impressed!')
  624. ->setVerb('comment');
  625. // Add event
  626. $manager->save($comment);
  627. // Update event
  628. $comment->setMessage('Different topic');
  629. $manager->save($comment);
  630. // Delete event
  631. $manager->delete($comment->getId());
  632. }
  633. public function testResolveDisplayName() {
  634. $manager = $this->getManager();
  635. $planetClosure = function ($name) {
  636. return ucfirst($name);
  637. };
  638. $galaxyClosure = function ($name) {
  639. return strtoupper($name);
  640. };
  641. $manager->registerDisplayNameResolver('planet', $planetClosure);
  642. $manager->registerDisplayNameResolver('galaxy', $galaxyClosure);
  643. $this->assertSame('Neptune', $manager->resolveDisplayName('planet', 'neptune'));
  644. $this->assertSame('SOMBRERO', $manager->resolveDisplayName('galaxy', 'sombrero'));
  645. }
  646. public function testRegisterResolverDuplicate() {
  647. $this->expectException(\OutOfBoundsException::class);
  648. $manager = $this->getManager();
  649. $planetClosure = function ($name) {
  650. return ucfirst($name);
  651. };
  652. $manager->registerDisplayNameResolver('planet', $planetClosure);
  653. $manager->registerDisplayNameResolver('planet', $planetClosure);
  654. }
  655. public function testRegisterResolverInvalidType() {
  656. $this->expectException(\InvalidArgumentException::class);
  657. $manager = $this->getManager();
  658. $planetClosure = function ($name) {
  659. return ucfirst($name);
  660. };
  661. $manager->registerDisplayNameResolver(1337, $planetClosure);
  662. }
  663. public function testResolveDisplayNameUnregisteredType() {
  664. $this->expectException(\OutOfBoundsException::class);
  665. $manager = $this->getManager();
  666. $planetClosure = function ($name) {
  667. return ucfirst($name);
  668. };
  669. $manager->registerDisplayNameResolver('planet', $planetClosure);
  670. $manager->resolveDisplayName('galaxy', 'sombrero');
  671. }
  672. public function testResolveDisplayNameDirtyResolver() {
  673. $manager = $this->getManager();
  674. $planetClosure = function () {
  675. return null;
  676. };
  677. $manager->registerDisplayNameResolver('planet', $planetClosure);
  678. $this->assertTrue(is_string($manager->resolveDisplayName('planet', 'neptune')));
  679. }
  680. public function testResolveDisplayNameInvalidType() {
  681. $this->expectException(\InvalidArgumentException::class);
  682. $manager = $this->getManager();
  683. $planetClosure = function () {
  684. return null;
  685. };
  686. $manager->registerDisplayNameResolver('planet', $planetClosure);
  687. $this->assertTrue(is_string($manager->resolveDisplayName(1337, 'neptune')));
  688. }
  689. }