NotifierTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Comments\Tests\Unit\Notification;
  7. use OCA\Comments\Notification\Notifier;
  8. use OCP\Comments\IComment;
  9. use OCP\Comments\ICommentsManager;
  10. use OCP\Comments\NotFoundException;
  11. use OCP\Files\Folder;
  12. use OCP\Files\IRootFolder;
  13. use OCP\Files\Node;
  14. use OCP\IL10N;
  15. use OCP\IURLGenerator;
  16. use OCP\IUserManager;
  17. use OCP\L10N\IFactory;
  18. use OCP\Notification\AlreadyProcessedException;
  19. use OCP\Notification\INotification;
  20. use OCP\Notification\UnknownNotificationException;
  21. use PHPUnit\Framework\MockObject\MockObject;
  22. use Test\TestCase;
  23. class NotifierTest extends TestCase {
  24. /** @var Notifier */
  25. protected $notifier;
  26. /** @var IFactory|MockObject */
  27. protected $l10nFactory;
  28. /** @var IL10N|MockObject */
  29. protected $l;
  30. /** @var IRootFolder|MockObject */
  31. protected $folder;
  32. /** @var ICommentsManager|MockObject */
  33. protected $commentsManager;
  34. /** @var IURLGenerator|MockObject */
  35. protected $url;
  36. /** @var IUserManager|MockObject */
  37. protected $userManager;
  38. /** @var INotification|MockObject */
  39. protected $notification;
  40. /** @var IComment|MockObject */
  41. protected $comment;
  42. /** @var string */
  43. protected $lc = 'tlh_KX';
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->l10nFactory = $this->createMock(IFactory::class);
  47. $this->folder = $this->createMock(IRootFolder::class);
  48. $this->commentsManager = $this->createMock(ICommentsManager::class);
  49. $this->url = $this->createMock(IURLGenerator::class);
  50. $this->userManager = $this->createMock(IUserManager::class);
  51. $this->notifier = new Notifier(
  52. $this->l10nFactory,
  53. $this->folder,
  54. $this->commentsManager,
  55. $this->url,
  56. $this->userManager
  57. );
  58. $this->l = $this->createMock(IL10N::class);
  59. $this->l->expects($this->any())
  60. ->method('t')
  61. ->willReturnCallback(function ($text, $parameters = []) {
  62. return vsprintf($text, $parameters);
  63. });
  64. $this->notification = $this->createMock(INotification::class);
  65. $this->comment = $this->createMock(IComment::class);
  66. }
  67. public function testPrepareSuccess(): void {
  68. $fileName = 'Gre\'thor.odp';
  69. $displayName = 'Huraga';
  70. $message = '@Huraga mentioned you in a comment on "Gre\'thor.odp"';
  71. /** @var Node|MockObject $node */
  72. $node = $this->createMock(Node::class);
  73. $node
  74. ->expects($this->atLeastOnce())
  75. ->method('getName')
  76. ->willReturn($fileName);
  77. $node
  78. ->expects($this->atLeastOnce())
  79. ->method('getPath')
  80. ->willReturn('/you/files/' . $fileName);
  81. $userFolder = $this->createMock(Folder::class);
  82. $this->folder->expects($this->once())
  83. ->method('getUserFolder')
  84. ->with('you')
  85. ->willReturn($userFolder);
  86. $userFolder->expects($this->once())
  87. ->method('getById')
  88. ->with('678')
  89. ->willReturn([$node]);
  90. $this->notification->expects($this->exactly(2))
  91. ->method('getUser')
  92. ->willReturn('you');
  93. $this->notification
  94. ->expects($this->once())
  95. ->method('getApp')
  96. ->willReturn('comments');
  97. $this->notification
  98. ->expects($this->once())
  99. ->method('getSubject')
  100. ->willReturn('mention');
  101. $this->notification
  102. ->expects($this->once())
  103. ->method('getSubjectParameters')
  104. ->willReturn(['files', '678']);
  105. $this->notification
  106. ->expects($this->never())
  107. ->method('setParsedSubject');
  108. $this->notification
  109. ->expects($this->once())
  110. ->method('setRichSubject')
  111. ->with('{user} mentioned you in a comment on "{file}"', $this->anything())
  112. ->willReturnSelf();
  113. $this->notification
  114. ->expects($this->once())
  115. ->method('setRichMessage')
  116. ->with('Hi {mention-user1}!', ['mention-user1' => ['type' => 'user', 'id' => 'you', 'name' => 'Your name']])
  117. ->willReturnSelf();
  118. $this->notification
  119. ->expects($this->never())
  120. ->method('setParsedMessage');
  121. $this->notification
  122. ->expects($this->once())
  123. ->method('setIcon')
  124. ->with('absolute-image-path')
  125. ->willReturnSelf();
  126. $this->url->expects($this->once())
  127. ->method('imagePath')
  128. ->with('core', 'actions/comment.svg')
  129. ->willReturn('image-path');
  130. $this->url->expects($this->once())
  131. ->method('getAbsoluteURL')
  132. ->with('image-path')
  133. ->willReturn('absolute-image-path');
  134. $this->l10nFactory
  135. ->expects($this->once())
  136. ->method('get')
  137. ->willReturn($this->l);
  138. $this->comment
  139. ->expects($this->any())
  140. ->method('getActorId')
  141. ->willReturn('huraga');
  142. $this->comment
  143. ->expects($this->any())
  144. ->method('getActorType')
  145. ->willReturn('users');
  146. $this->comment
  147. ->expects($this->any())
  148. ->method('getMessage')
  149. ->willReturn('Hi @you!');
  150. $this->comment
  151. ->expects($this->any())
  152. ->method('getMentions')
  153. ->willReturn([['type' => 'user', 'id' => 'you']]);
  154. $this->comment->expects($this->atLeastOnce())
  155. ->method('getId')
  156. ->willReturn('1234');
  157. $this->commentsManager
  158. ->expects($this->once())
  159. ->method('get')
  160. ->willReturn($this->comment);
  161. $this->commentsManager
  162. ->expects($this->once())
  163. ->method('resolveDisplayName')
  164. ->with('user', 'you')
  165. ->willReturn('Your name');
  166. $this->userManager
  167. ->expects($this->exactly(2))
  168. ->method('getDisplayName')
  169. ->willReturnMap([
  170. ['huraga', $displayName],
  171. ['you', 'You'],
  172. ]);
  173. $this->notifier->prepare($this->notification, $this->lc);
  174. }
  175. public function testPrepareSuccessDeletedUser(): void {
  176. $fileName = 'Gre\'thor.odp';
  177. $message = 'You were mentioned on "Gre\'thor.odp", in a comment by an account that has since been deleted';
  178. /** @var Node|MockObject $node */
  179. $node = $this->createMock(Node::class);
  180. $node
  181. ->expects($this->atLeastOnce())
  182. ->method('getName')
  183. ->willReturn($fileName);
  184. $node
  185. ->expects($this->atLeastOnce())
  186. ->method('getPath')
  187. ->willReturn('/you/files/' . $fileName);
  188. $userFolder = $this->createMock(Folder::class);
  189. $this->folder->expects($this->once())
  190. ->method('getUserFolder')
  191. ->with('you')
  192. ->willReturn($userFolder);
  193. $userFolder->expects($this->once())
  194. ->method('getById')
  195. ->with('678')
  196. ->willReturn([$node]);
  197. $this->notification->expects($this->exactly(2))
  198. ->method('getUser')
  199. ->willReturn('you');
  200. $this->notification
  201. ->expects($this->once())
  202. ->method('getApp')
  203. ->willReturn('comments');
  204. $this->notification
  205. ->expects($this->once())
  206. ->method('getSubject')
  207. ->willReturn('mention');
  208. $this->notification
  209. ->expects($this->once())
  210. ->method('getSubjectParameters')
  211. ->willReturn(['files', '678']);
  212. $this->notification
  213. ->expects($this->never())
  214. ->method('setParsedSubject');
  215. $this->notification
  216. ->expects($this->once())
  217. ->method('setRichSubject')
  218. ->with('You were mentioned on "{file}", in a comment by an account that has since been deleted', $this->anything())
  219. ->willReturnSelf();
  220. $this->notification
  221. ->expects($this->once())
  222. ->method('setRichMessage')
  223. ->with('Hi {mention-user1}!', ['mention-user1' => ['type' => 'user', 'id' => 'you', 'name' => 'Your name']])
  224. ->willReturnSelf();
  225. $this->notification
  226. ->expects($this->never())
  227. ->method('setParsedMessage');
  228. $this->notification
  229. ->expects($this->once())
  230. ->method('setIcon')
  231. ->with('absolute-image-path')
  232. ->willReturnSelf();
  233. $this->url->expects($this->once())
  234. ->method('imagePath')
  235. ->with('core', 'actions/comment.svg')
  236. ->willReturn('image-path');
  237. $this->url->expects($this->once())
  238. ->method('getAbsoluteURL')
  239. ->with('image-path')
  240. ->willReturn('absolute-image-path');
  241. $this->l10nFactory
  242. ->expects($this->once())
  243. ->method('get')
  244. ->willReturn($this->l);
  245. $this->comment
  246. ->expects($this->any())
  247. ->method('getActorId')
  248. ->willReturn('huraga');
  249. $this->comment
  250. ->expects($this->any())
  251. ->method('getActorType')
  252. ->willReturn(ICommentsManager::DELETED_USER);
  253. $this->comment
  254. ->expects($this->any())
  255. ->method('getMessage')
  256. ->willReturn('Hi @you!');
  257. $this->comment
  258. ->expects($this->any())
  259. ->method('getMentions')
  260. ->willReturn([['type' => 'user', 'id' => 'you']]);
  261. $this->commentsManager
  262. ->expects($this->once())
  263. ->method('get')
  264. ->willReturn($this->comment);
  265. $this->commentsManager
  266. ->expects($this->once())
  267. ->method('resolveDisplayName')
  268. ->with('user', 'you')
  269. ->willReturn('Your name');
  270. $this->userManager
  271. ->expects($this->once())
  272. ->method('getDisplayName')
  273. ->willReturnMap([
  274. ['huraga', null],
  275. ['you', 'You'],
  276. ]);
  277. $this->notifier->prepare($this->notification, $this->lc);
  278. }
  279. public function testPrepareDifferentApp(): void {
  280. $this->expectException(UnknownNotificationException::class);
  281. $this->folder
  282. ->expects($this->never())
  283. ->method('getById');
  284. $this->notification
  285. ->expects($this->once())
  286. ->method('getApp')
  287. ->willReturn('constructions');
  288. $this->notification
  289. ->expects($this->never())
  290. ->method('getSubject');
  291. $this->notification
  292. ->expects($this->never())
  293. ->method('getSubjectParameters');
  294. $this->notification
  295. ->expects($this->never())
  296. ->method('setParsedSubject');
  297. $this->l10nFactory
  298. ->expects($this->never())
  299. ->method('get');
  300. $this->commentsManager
  301. ->expects($this->never())
  302. ->method('get');
  303. $this->userManager
  304. ->expects($this->never())
  305. ->method('getDisplayName');
  306. $this->notifier->prepare($this->notification, $this->lc);
  307. }
  308. public function testPrepareNotFound(): void {
  309. $this->expectException(UnknownNotificationException::class);
  310. $this->folder
  311. ->expects($this->never())
  312. ->method('getById');
  313. $this->notification
  314. ->expects($this->once())
  315. ->method('getApp')
  316. ->willReturn('comments');
  317. $this->notification
  318. ->expects($this->never())
  319. ->method('getSubject');
  320. $this->notification
  321. ->expects($this->never())
  322. ->method('getSubjectParameters');
  323. $this->notification
  324. ->expects($this->never())
  325. ->method('setParsedSubject');
  326. $this->l10nFactory
  327. ->expects($this->never())
  328. ->method('get');
  329. $this->commentsManager
  330. ->expects($this->once())
  331. ->method('get')
  332. ->willThrowException(new NotFoundException());
  333. $this->userManager
  334. ->expects($this->never())
  335. ->method('getDisplayName');
  336. $this->notifier->prepare($this->notification, $this->lc);
  337. }
  338. public function testPrepareDifferentSubject(): void {
  339. $this->expectException(UnknownNotificationException::class);
  340. $displayName = 'Huraga';
  341. $this->folder
  342. ->expects($this->never())
  343. ->method('getById');
  344. $this->notification
  345. ->expects($this->once())
  346. ->method('getApp')
  347. ->willReturn('comments');
  348. $this->notification
  349. ->expects($this->once())
  350. ->method('getSubject')
  351. ->willReturn('unlike');
  352. $this->notification
  353. ->expects($this->never())
  354. ->method('getSubjectParameters');
  355. $this->notification
  356. ->expects($this->never())
  357. ->method('setParsedSubject');
  358. $this->l
  359. ->expects($this->never())
  360. ->method('t');
  361. $this->l10nFactory
  362. ->expects($this->once())
  363. ->method('get')
  364. ->willReturn($this->l);
  365. $this->comment
  366. ->expects($this->any())
  367. ->method('getActorId')
  368. ->willReturn('huraga');
  369. $this->comment
  370. ->expects($this->any())
  371. ->method('getActorType')
  372. ->willReturn('users');
  373. $this->commentsManager
  374. ->expects($this->once())
  375. ->method('get')
  376. ->willReturn($this->comment);
  377. $this->userManager
  378. ->expects($this->once())
  379. ->method('getDisplayName')
  380. ->with('huraga')
  381. ->willReturn($displayName);
  382. $this->notifier->prepare($this->notification, $this->lc);
  383. }
  384. public function testPrepareNotFiles(): void {
  385. $this->expectException(UnknownNotificationException::class);
  386. $displayName = 'Huraga';
  387. $this->folder
  388. ->expects($this->never())
  389. ->method('getById');
  390. $this->notification
  391. ->expects($this->once())
  392. ->method('getApp')
  393. ->willReturn('comments');
  394. $this->notification
  395. ->expects($this->once())
  396. ->method('getSubject')
  397. ->willReturn('mention');
  398. $this->notification
  399. ->expects($this->once())
  400. ->method('getSubjectParameters')
  401. ->willReturn(['ships', '678']);
  402. $this->notification
  403. ->expects($this->never())
  404. ->method('setParsedSubject');
  405. $this->l
  406. ->expects($this->never())
  407. ->method('t');
  408. $this->l10nFactory
  409. ->expects($this->once())
  410. ->method('get')
  411. ->willReturn($this->l);
  412. $this->comment
  413. ->expects($this->any())
  414. ->method('getActorId')
  415. ->willReturn('huraga');
  416. $this->comment
  417. ->expects($this->any())
  418. ->method('getActorType')
  419. ->willReturn('users');
  420. $this->commentsManager
  421. ->expects($this->once())
  422. ->method('get')
  423. ->willReturn($this->comment);
  424. $this->userManager
  425. ->expects($this->once())
  426. ->method('getDisplayName')
  427. ->with('huraga')
  428. ->willReturn($displayName);
  429. $this->notifier->prepare($this->notification, $this->lc);
  430. }
  431. public function testPrepareUnresolvableFileID(): void {
  432. $this->expectException(AlreadyProcessedException::class);
  433. $displayName = 'Huraga';
  434. $userFolder = $this->createMock(Folder::class);
  435. $this->folder->expects($this->once())
  436. ->method('getUserFolder')
  437. ->with('you')
  438. ->willReturn($userFolder);
  439. $userFolder->expects($this->once())
  440. ->method('getById')
  441. ->with('678')
  442. ->willReturn([]);
  443. $this->notification->expects($this->once())
  444. ->method('getUser')
  445. ->willReturn('you');
  446. $this->notification
  447. ->expects($this->once())
  448. ->method('getApp')
  449. ->willReturn('comments');
  450. $this->notification
  451. ->expects($this->once())
  452. ->method('getSubject')
  453. ->willReturn('mention');
  454. $this->notification
  455. ->expects($this->once())
  456. ->method('getSubjectParameters')
  457. ->willReturn(['files', '678']);
  458. $this->notification
  459. ->expects($this->never())
  460. ->method('setParsedSubject');
  461. $this->l
  462. ->expects($this->never())
  463. ->method('t');
  464. $this->l10nFactory
  465. ->expects($this->once())
  466. ->method('get')
  467. ->willReturn($this->l);
  468. $this->comment
  469. ->expects($this->any())
  470. ->method('getActorId')
  471. ->willReturn('huraga');
  472. $this->comment
  473. ->expects($this->any())
  474. ->method('getActorType')
  475. ->willReturn('users');
  476. $this->commentsManager
  477. ->expects($this->once())
  478. ->method('get')
  479. ->willReturn($this->comment);
  480. $this->userManager
  481. ->expects($this->once())
  482. ->method('getDisplayName')
  483. ->with('huraga')
  484. ->willReturn($displayName);
  485. $this->notifier->prepare($this->notification, $this->lc);
  486. }
  487. }