ManagerTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. public function invalidCreateArgsProvider() {
  301. return [
  302. ['', 'aId-1', 'oType-1', 'oId-1'],
  303. ['aType-1', '', 'oType-1', 'oId-1'],
  304. ['aType-1', 'aId-1', '', 'oId-1'],
  305. ['aType-1', 'aId-1', 'oType-1', ''],
  306. [1, 'aId-1', 'oType-1', 'oId-1'],
  307. ['aType-1', 1, 'oType-1', 'oId-1'],
  308. ['aType-1', 'aId-1', 1, 'oId-1'],
  309. ['aType-1', 'aId-1', 'oType-1', 1],
  310. ];
  311. }
  312. /**
  313. * @dataProvider invalidCreateArgsProvider
  314. * @expectedException \InvalidArgumentException
  315. * @param string $aType
  316. * @param string $aId
  317. * @param string $oType
  318. * @param string $oId
  319. */
  320. public function testCreateCommentInvalidArguments($aType, $aId, $oType, $oId) {
  321. $manager = $this->getManager();
  322. $manager->create($aType, $aId, $oType, $oId);
  323. }
  324. public function testCreateComment() {
  325. $actorType = 'bot';
  326. $actorId = 'bob';
  327. $objectType = 'weather';
  328. $objectId = 'bielefeld';
  329. $comment = $this->getManager()->create($actorType, $actorId, $objectType, $objectId);
  330. $this->assertTrue($comment instanceof IComment);
  331. $this->assertSame($comment->getActorType(), $actorType);
  332. $this->assertSame($comment->getActorId(), $actorId);
  333. $this->assertSame($comment->getObjectType(), $objectType);
  334. $this->assertSame($comment->getObjectId(), $objectId);
  335. }
  336. /**
  337. * @expectedException \OCP\Comments\NotFoundException
  338. */
  339. public function testDelete() {
  340. $manager = $this->getManager();
  341. $done = $manager->delete('404');
  342. $this->assertFalse($done);
  343. $done = $manager->delete('%');
  344. $this->assertFalse($done);
  345. $done = $manager->delete('');
  346. $this->assertFalse($done);
  347. $id = strval($this->addDatabaseEntry(0, 0));
  348. $comment = $manager->get($id);
  349. $this->assertTrue($comment instanceof IComment);
  350. $done = $manager->delete($id);
  351. $this->assertTrue($done);
  352. $manager->get($id);
  353. }
  354. public function testSaveNew() {
  355. $manager = $this->getManager();
  356. $comment = new Comment();
  357. $comment
  358. ->setActor('users', 'alice')
  359. ->setObject('files', 'file64')
  360. ->setMessage('very beautiful, I am impressed!')
  361. ->setVerb('comment');
  362. $saveSuccessful = $manager->save($comment);
  363. $this->assertTrue($saveSuccessful);
  364. $this->assertTrue($comment->getId() !== '');
  365. $this->assertTrue($comment->getId() !== '0');
  366. $this->assertTrue(!is_null($comment->getCreationDateTime()));
  367. $loadedComment = $manager->get($comment->getId());
  368. $this->assertSame($comment->getMessage(), $loadedComment->getMessage());
  369. $this->assertEquals($comment->getCreationDateTime()->getTimestamp(), $loadedComment->getCreationDateTime()->getTimestamp());
  370. }
  371. public function testSaveUpdate() {
  372. $manager = $this->getManager();
  373. $comment = new Comment();
  374. $comment
  375. ->setActor('users', 'alice')
  376. ->setObject('files', 'file64')
  377. ->setMessage('very beautiful, I am impressed!')
  378. ->setVerb('comment');
  379. $manager->save($comment);
  380. $comment->setMessage('very beautiful, I am really so much impressed!');
  381. $manager->save($comment);
  382. $loadedComment = $manager->get($comment->getId());
  383. $this->assertSame($comment->getMessage(), $loadedComment->getMessage());
  384. }
  385. /**
  386. * @expectedException \OCP\Comments\NotFoundException
  387. */
  388. public function testSaveUpdateException() {
  389. $manager = $this->getManager();
  390. $comment = new Comment();
  391. $comment
  392. ->setActor('users', 'alice')
  393. ->setObject('files', 'file64')
  394. ->setMessage('very beautiful, I am impressed!')
  395. ->setVerb('comment');
  396. $manager->save($comment);
  397. $manager->delete($comment->getId());
  398. $comment->setMessage('very beautiful, I am really so much impressed!');
  399. $manager->save($comment);
  400. }
  401. /**
  402. * @expectedException \UnexpectedValueException
  403. */
  404. public function testSaveIncomplete() {
  405. $manager = $this->getManager();
  406. $comment = new Comment();
  407. $comment->setMessage('from no one to nothing');
  408. $manager->save($comment);
  409. }
  410. public function testSaveAsChild() {
  411. $id = $this->addDatabaseEntry(0, 0);
  412. $manager = $this->getManager();
  413. for ($i = 0; $i < 3; $i++) {
  414. $comment = new Comment();
  415. $comment
  416. ->setActor('users', 'alice')
  417. ->setObject('files', 'file64')
  418. ->setParentId(strval($id))
  419. ->setMessage('full ack')
  420. ->setVerb('comment')
  421. // setting the creation time avoids using sleep() while making sure to test with different timestamps
  422. ->setCreationDateTime(new \DateTime('+' . $i . ' minutes'));
  423. $manager->save($comment);
  424. $this->assertSame($comment->getTopmostParentId(), strval($id));
  425. $parentComment = $manager->get(strval($id));
  426. $this->assertSame($parentComment->getChildrenCount(), $i + 1);
  427. $this->assertEquals($parentComment->getLatestChildDateTime()->getTimestamp(), $comment->getCreationDateTime()->getTimestamp());
  428. }
  429. }
  430. public function invalidActorArgsProvider() {
  431. return
  432. [
  433. ['', ''],
  434. [1, 'alice'],
  435. ['users', 1],
  436. ];
  437. }
  438. /**
  439. * @dataProvider invalidActorArgsProvider
  440. * @expectedException \InvalidArgumentException
  441. * @param string $type
  442. * @param string $id
  443. */
  444. public function testDeleteReferencesOfActorInvalidInput($type, $id) {
  445. $manager = $this->getManager();
  446. $manager->deleteReferencesOfActor($type, $id);
  447. }
  448. public function testDeleteReferencesOfActor() {
  449. $ids = [];
  450. $ids[] = $this->addDatabaseEntry(0, 0);
  451. $ids[] = $this->addDatabaseEntry(0, 0);
  452. $ids[] = $this->addDatabaseEntry(0, 0);
  453. $manager = $this->getManager();
  454. // just to make sure they are really set, with correct actor data
  455. $comment = $manager->get(strval($ids[1]));
  456. $this->assertSame($comment->getActorType(), 'users');
  457. $this->assertSame($comment->getActorId(), 'alice');
  458. $wasSuccessful = $manager->deleteReferencesOfActor('users', 'alice');
  459. $this->assertTrue($wasSuccessful);
  460. foreach ($ids as $id) {
  461. $comment = $manager->get(strval($id));
  462. $this->assertSame($comment->getActorType(), ICommentsManager::DELETED_USER);
  463. $this->assertSame($comment->getActorId(), ICommentsManager::DELETED_USER);
  464. }
  465. // actor info is gone from DB, but when database interaction is alright,
  466. // we still expect to get true back
  467. $wasSuccessful = $manager->deleteReferencesOfActor('users', 'alice');
  468. $this->assertTrue($wasSuccessful);
  469. }
  470. public function testDeleteReferencesOfActorWithUserManagement() {
  471. $user = \OC::$server->getUserManager()->createUser('xenia', '123456');
  472. $this->assertTrue($user instanceof IUser);
  473. $manager = \OC::$server->getCommentsManager();
  474. $comment = $manager->create('users', $user->getUID(), 'files', 'file64');
  475. $comment
  476. ->setMessage('Most important comment I ever left on the Internet.')
  477. ->setVerb('comment');
  478. $status = $manager->save($comment);
  479. $this->assertTrue($status);
  480. $commentID = $comment->getId();
  481. $user->delete();
  482. $comment = $manager->get($commentID);
  483. $this->assertSame($comment->getActorType(), ICommentsManager::DELETED_USER);
  484. $this->assertSame($comment->getActorId(), ICommentsManager::DELETED_USER);
  485. }
  486. public function invalidObjectArgsProvider() {
  487. return
  488. [
  489. ['', ''],
  490. [1, 'file64'],
  491. ['files', 1],
  492. ];
  493. }
  494. /**
  495. * @dataProvider invalidObjectArgsProvider
  496. * @expectedException \InvalidArgumentException
  497. * @param string $type
  498. * @param string $id
  499. */
  500. public function testDeleteCommentsAtObjectInvalidInput($type, $id) {
  501. $manager = $this->getManager();
  502. $manager->deleteCommentsAtObject($type, $id);
  503. }
  504. public function testDeleteCommentsAtObject() {
  505. $ids = [];
  506. $ids[] = $this->addDatabaseEntry(0, 0);
  507. $ids[] = $this->addDatabaseEntry(0, 0);
  508. $ids[] = $this->addDatabaseEntry(0, 0);
  509. $manager = $this->getManager();
  510. // just to make sure they are really set, with correct actor data
  511. $comment = $manager->get(strval($ids[1]));
  512. $this->assertSame($comment->getObjectType(), 'files');
  513. $this->assertSame($comment->getObjectId(), 'file64');
  514. $wasSuccessful = $manager->deleteCommentsAtObject('files', 'file64');
  515. $this->assertTrue($wasSuccessful);
  516. $verified = 0;
  517. foreach ($ids as $id) {
  518. try {
  519. $manager->get(strval($id));
  520. } catch (NotFoundException $e) {
  521. $verified++;
  522. }
  523. }
  524. $this->assertSame($verified, 3);
  525. // actor info is gone from DB, but when database interaction is alright,
  526. // we still expect to get true back
  527. $wasSuccessful = $manager->deleteCommentsAtObject('files', 'file64');
  528. $this->assertTrue($wasSuccessful);
  529. }
  530. public function testSetMarkRead() {
  531. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
  532. $user = $this->createMock(IUser::class);
  533. $user->expects($this->any())
  534. ->method('getUID')
  535. ->will($this->returnValue('alice'));
  536. $dateTimeSet = new \DateTime();
  537. $manager = $this->getManager();
  538. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  539. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  540. $this->assertEquals($dateTimeGet->getTimestamp(), $dateTimeSet->getTimestamp());
  541. }
  542. public function testSetMarkReadUpdate() {
  543. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
  544. $user = $this->createMock(IUser::class);
  545. $user->expects($this->any())
  546. ->method('getUID')
  547. ->will($this->returnValue('alice'));
  548. $dateTimeSet = new \DateTime('yesterday');
  549. $manager = $this->getManager();
  550. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  551. $dateTimeSet = new \DateTime('today');
  552. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  553. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  554. $this->assertEquals($dateTimeGet, $dateTimeSet);
  555. }
  556. public function testReadMarkDeleteUser() {
  557. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
  558. $user = $this->createMock(IUser::class);
  559. $user->expects($this->any())
  560. ->method('getUID')
  561. ->will($this->returnValue('alice'));
  562. $dateTimeSet = new \DateTime();
  563. $manager = $this->getManager();
  564. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  565. $manager->deleteReadMarksFromUser($user);
  566. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  567. $this->assertNull($dateTimeGet);
  568. }
  569. public function testReadMarkDeleteObject() {
  570. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
  571. $user = $this->createMock(IUser::class);
  572. $user->expects($this->any())
  573. ->method('getUID')
  574. ->will($this->returnValue('alice'));
  575. $dateTimeSet = new \DateTime();
  576. $manager = $this->getManager();
  577. $manager->setReadMark('robot', '36', $dateTimeSet, $user);
  578. $manager->deleteReadMarksOnObject('robot', '36');
  579. $dateTimeGet = $manager->getReadMark('robot', '36', $user);
  580. $this->assertNull($dateTimeGet);
  581. }
  582. public function testSendEvent() {
  583. $handler1 = $this->getMockBuilder(ICommentsEventHandler::class)->getMock();
  584. $handler1->expects($this->exactly(4))
  585. ->method('handle');
  586. $handler2 = $this->getMockBuilder(ICommentsEventHandler::class)->getMock();
  587. $handler1->expects($this->exactly(4))
  588. ->method('handle');
  589. $manager = $this->getManager();
  590. $manager->registerEventHandler(function () use ($handler1) {
  591. return $handler1;
  592. });
  593. $manager->registerEventHandler(function () use ($handler2) {
  594. return $handler2;
  595. });
  596. $comment = new Comment();
  597. $comment
  598. ->setActor('users', 'alice')
  599. ->setObject('files', 'file64')
  600. ->setMessage('very beautiful, I am impressed!')
  601. ->setVerb('comment');
  602. // Add event
  603. $manager->save($comment);
  604. // Update event
  605. $comment->setMessage('Different topic');
  606. $manager->save($comment);
  607. // Delete event
  608. $manager->delete($comment->getId());
  609. }
  610. public function testResolveDisplayName() {
  611. $manager = $this->getManager();
  612. $planetClosure = function ($name) {
  613. return ucfirst($name);
  614. };
  615. $galaxyClosure = function ($name) {
  616. return strtoupper($name);
  617. };
  618. $manager->registerDisplayNameResolver('planet', $planetClosure);
  619. $manager->registerDisplayNameResolver('galaxy', $galaxyClosure);
  620. $this->assertSame('Neptune', $manager->resolveDisplayName('planet', 'neptune'));
  621. $this->assertSame('SOMBRERO', $manager->resolveDisplayName('galaxy', 'sombrero'));
  622. }
  623. /**
  624. * @expectedException \OutOfBoundsException
  625. */
  626. public function testRegisterResolverDuplicate() {
  627. $manager = $this->getManager();
  628. $planetClosure = function ($name) {
  629. return ucfirst($name);
  630. };
  631. $manager->registerDisplayNameResolver('planet', $planetClosure);
  632. $manager->registerDisplayNameResolver('planet', $planetClosure);
  633. }
  634. /**
  635. * @expectedException \InvalidArgumentException
  636. */
  637. public function testRegisterResolverInvalidType() {
  638. $manager = $this->getManager();
  639. $planetClosure = function ($name) {
  640. return ucfirst($name);
  641. };
  642. $manager->registerDisplayNameResolver(1337, $planetClosure);
  643. }
  644. /**
  645. * @expectedException \OutOfBoundsException
  646. */
  647. public function testResolveDisplayNameUnregisteredType() {
  648. $manager = $this->getManager();
  649. $planetClosure = function ($name) {
  650. return ucfirst($name);
  651. };
  652. $manager->registerDisplayNameResolver('planet', $planetClosure);
  653. $manager->resolveDisplayName('galaxy', 'sombrero');
  654. }
  655. public function testResolveDisplayNameDirtyResolver() {
  656. $manager = $this->getManager();
  657. $planetClosure = function () {
  658. return null;
  659. };
  660. $manager->registerDisplayNameResolver('planet', $planetClosure);
  661. $this->assertTrue(is_string($manager->resolveDisplayName('planet', 'neptune')));
  662. }
  663. /**
  664. * @expectedException \InvalidArgumentException
  665. */
  666. public function testResolveDisplayNameInvalidType() {
  667. $manager = $this->getManager();
  668. $planetClosure = function () {
  669. return null;
  670. };
  671. $manager->registerDisplayNameResolver('planet', $planetClosure);
  672. $this->assertTrue(is_string($manager->resolveDisplayName(1337, 'neptune')));
  673. }
  674. }