NotifierTest.php 14 KB

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