You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotifierTest.php 16KB

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