1
0

NotifierTest.php 15 KB

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