ManagerTest.php 23 KB

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