ManagerTest.php 26 KB

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