NotifierTest.php 15 KB

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