123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- <?php
- /**
- * @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de>
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- namespace OCA\Comments\Tests\Unit\Notification;
- use OCA\Comments\Notification\Notifier;
- use OCP\Comments\IComment;
- use OCP\Comments\ICommentsManager;
- use OCP\Comments\NotFoundException;
- use OCP\Files\Folder;
- use OCP\Files\IRootFolder;
- use OCP\Files\Node;
- use OCP\IL10N;
- use OCP\IURLGenerator;
- use OCP\IUserManager;
- use OCP\L10N\IFactory;
- use OCP\Notification\INotification;
- use PHPUnit\Framework\MockObject\MockObject;
- use Test\TestCase;
- class NotifierTest extends TestCase {
- /** @var Notifier */
- protected $notifier;
- /** @var IFactory|MockObject */
- protected $l10nFactory;
- /** @var IL10N|MockObject */
- protected $l;
- /** @var IRootFolder|MockObject */
- protected $folder;
- /** @var ICommentsManager|MockObject */
- protected $commentsManager;
- /** @var IURLGenerator|MockObject */
- protected $url;
- /** @var IUserManager|MockObject */
- protected $userManager;
- /** @var INotification|MockObject */
- protected $notification;
- /** @var IComment|MockObject */
- protected $comment;
- /** @var string */
- protected $lc = 'tlh_KX';
- protected function setUp(): void {
- parent::setUp();
- $this->l10nFactory = $this->createMock(IFactory::class);
- $this->folder = $this->createMock(IRootFolder::class);
- $this->commentsManager = $this->createMock(ICommentsManager::class);
- $this->url = $this->createMock(IURLGenerator::class);
- $this->userManager = $this->createMock(IUserManager::class);
- $this->notifier = new Notifier(
- $this->l10nFactory,
- $this->folder,
- $this->commentsManager,
- $this->url,
- $this->userManager
- );
- $this->l = $this->createMock(IL10N::class);
- $this->l->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($text, $parameters = []) {
- return vsprintf($text, $parameters);
- });
- $this->notification = $this->createMock(INotification::class);
- $this->comment = $this->createMock(IComment::class);
- }
- public function testPrepareSuccess() {
- $fileName = 'Gre\'thor.odp';
- $displayName = 'Huraga';
- $message = '@Huraga mentioned you in a comment on "Gre\'thor.odp"';
- /** @var Node|MockObject $node */
- $node = $this->createMock(Node::class);
- $node
- ->expects($this->atLeastOnce())
- ->method('getName')
- ->willReturn($fileName);
- $node
- ->expects($this->atLeastOnce())
- ->method('getPath')
- ->willReturn('/you/files/' . $fileName);
- $userFolder = $this->createMock(Folder::class);
- $this->folder->expects($this->once())
- ->method('getUserFolder')
- ->with('you')
- ->willReturn($userFolder);
- $userFolder->expects($this->once())
- ->method('getById')
- ->with('678')
- ->willReturn([$node]);
- $this->notification->expects($this->exactly(2))
- ->method('getUser')
- ->willReturn('you');
- $this->notification
- ->expects($this->once())
- ->method('getApp')
- ->willReturn('comments');
- $this->notification
- ->expects($this->once())
- ->method('getSubject')
- ->willReturn('mention');
- $this->notification
- ->expects($this->once())
- ->method('getSubjectParameters')
- ->willReturn(['files', '678']);
- $this->notification
- ->expects($this->never())
- ->method('setParsedSubject');
- $this->notification
- ->expects($this->once())
- ->method('setRichSubject')
- ->with('{user} mentioned you in a comment on "{file}"', $this->anything())
- ->willReturnSelf();
- $this->notification
- ->expects($this->once())
- ->method('setRichMessage')
- ->with('Hi {mention-user1}!', ['mention-user1' => ['type' => 'user', 'id' => 'you', 'name' => 'Your name']])
- ->willReturnSelf();
- $this->notification
- ->expects($this->never())
- ->method('setParsedMessage');
- $this->notification
- ->expects($this->once())
- ->method('setIcon')
- ->with('absolute-image-path')
- ->willReturnSelf();
- $this->url->expects($this->once())
- ->method('imagePath')
- ->with('core', 'actions/comment.svg')
- ->willReturn('image-path');
- $this->url->expects($this->once())
- ->method('getAbsoluteURL')
- ->with('image-path')
- ->willReturn('absolute-image-path');
- $this->l10nFactory
- ->expects($this->once())
- ->method('get')
- ->willReturn($this->l);
- $this->comment
- ->expects($this->any())
- ->method('getActorId')
- ->willReturn('huraga');
- $this->comment
- ->expects($this->any())
- ->method('getActorType')
- ->willReturn('users');
- $this->comment
- ->expects($this->any())
- ->method('getMessage')
- ->willReturn('Hi @you!');
- $this->comment
- ->expects($this->any())
- ->method('getMentions')
- ->willReturn([['type' => 'user', 'id' => 'you']]);
- $this->comment->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn('1234');
- $this->commentsManager
- ->expects($this->once())
- ->method('get')
- ->willReturn($this->comment);
- $this->commentsManager
- ->expects($this->once())
- ->method('resolveDisplayName')
- ->with('user', 'you')
- ->willReturn('Your name');
- $this->userManager
- ->expects($this->exactly(2))
- ->method('getDisplayName')
- ->willReturnMap([
- ['huraga', $displayName],
- ['you', 'You'],
- ]);
- $this->notifier->prepare($this->notification, $this->lc);
- }
- public function testPrepareSuccessDeletedUser() {
- $fileName = 'Gre\'thor.odp';
- $message = 'You were mentioned on "Gre\'thor.odp", in a comment by an account that has since been deleted';
- /** @var Node|MockObject $node */
- $node = $this->createMock(Node::class);
- $node
- ->expects($this->atLeastOnce())
- ->method('getName')
- ->willReturn($fileName);
- $node
- ->expects($this->atLeastOnce())
- ->method('getPath')
- ->willReturn('/you/files/' . $fileName);
- $userFolder = $this->createMock(Folder::class);
- $this->folder->expects($this->once())
- ->method('getUserFolder')
- ->with('you')
- ->willReturn($userFolder);
- $userFolder->expects($this->once())
- ->method('getById')
- ->with('678')
- ->willReturn([$node]);
- $this->notification->expects($this->exactly(2))
- ->method('getUser')
- ->willReturn('you');
- $this->notification
- ->expects($this->once())
- ->method('getApp')
- ->willReturn('comments');
- $this->notification
- ->expects($this->once())
- ->method('getSubject')
- ->willReturn('mention');
- $this->notification
- ->expects($this->once())
- ->method('getSubjectParameters')
- ->willReturn(['files', '678']);
- $this->notification
- ->expects($this->never())
- ->method('setParsedSubject');
- $this->notification
- ->expects($this->once())
- ->method('setRichSubject')
- ->with('You were mentioned on "{file}", in a comment by an account that has since been deleted', $this->anything())
- ->willReturnSelf();
- $this->notification
- ->expects($this->once())
- ->method('setRichMessage')
- ->with('Hi {mention-user1}!', ['mention-user1' => ['type' => 'user', 'id' => 'you', 'name' => 'Your name']])
- ->willReturnSelf();
- $this->notification
- ->expects($this->never())
- ->method('setParsedMessage');
- $this->notification
- ->expects($this->once())
- ->method('setIcon')
- ->with('absolute-image-path')
- ->willReturnSelf();
- $this->url->expects($this->once())
- ->method('imagePath')
- ->with('core', 'actions/comment.svg')
- ->willReturn('image-path');
- $this->url->expects($this->once())
- ->method('getAbsoluteURL')
- ->with('image-path')
- ->willReturn('absolute-image-path');
- $this->l10nFactory
- ->expects($this->once())
- ->method('get')
- ->willReturn($this->l);
- $this->comment
- ->expects($this->any())
- ->method('getActorId')
- ->willReturn('huraga');
- $this->comment
- ->expects($this->any())
- ->method('getActorType')
- ->willReturn(ICommentsManager::DELETED_USER);
- $this->comment
- ->expects($this->any())
- ->method('getMessage')
- ->willReturn('Hi @you!');
- $this->comment
- ->expects($this->any())
- ->method('getMentions')
- ->willReturn([['type' => 'user', 'id' => 'you']]);
- $this->commentsManager
- ->expects($this->once())
- ->method('get')
- ->willReturn($this->comment);
- $this->commentsManager
- ->expects($this->once())
- ->method('resolveDisplayName')
- ->with('user', 'you')
- ->willReturn('Your name');
- $this->userManager
- ->expects($this->once())
- ->method('getDisplayName')
- ->willReturnMap([
- ['huraga', null],
- ['you', 'You'],
- ]);
- $this->notifier->prepare($this->notification, $this->lc);
- }
- public function testPrepareDifferentApp() {
- $this->expectException(\InvalidArgumentException::class);
- $this->folder
- ->expects($this->never())
- ->method('getById');
- $this->notification
- ->expects($this->once())
- ->method('getApp')
- ->willReturn('constructions');
- $this->notification
- ->expects($this->never())
- ->method('getSubject');
- $this->notification
- ->expects($this->never())
- ->method('getSubjectParameters');
- $this->notification
- ->expects($this->never())
- ->method('setParsedSubject');
- $this->l10nFactory
- ->expects($this->never())
- ->method('get');
- $this->commentsManager
- ->expects($this->never())
- ->method('get');
- $this->userManager
- ->expects($this->never())
- ->method('getDisplayName');
- $this->notifier->prepare($this->notification, $this->lc);
- }
- public function testPrepareNotFound() {
- $this->expectException(\InvalidArgumentException::class);
- $this->folder
- ->expects($this->never())
- ->method('getById');
- $this->notification
- ->expects($this->once())
- ->method('getApp')
- ->willReturn('comments');
- $this->notification
- ->expects($this->never())
- ->method('getSubject');
- $this->notification
- ->expects($this->never())
- ->method('getSubjectParameters');
- $this->notification
- ->expects($this->never())
- ->method('setParsedSubject');
- $this->l10nFactory
- ->expects($this->never())
- ->method('get');
- $this->commentsManager
- ->expects($this->once())
- ->method('get')
- ->willThrowException(new NotFoundException());
- $this->userManager
- ->expects($this->never())
- ->method('getDisplayName');
- $this->notifier->prepare($this->notification, $this->lc);
- }
- public function testPrepareDifferentSubject() {
- $this->expectException(\InvalidArgumentException::class);
- $displayName = 'Huraga';
- $this->folder
- ->expects($this->never())
- ->method('getById');
- $this->notification
- ->expects($this->once())
- ->method('getApp')
- ->willReturn('comments');
- $this->notification
- ->expects($this->once())
- ->method('getSubject')
- ->willReturn('unlike');
- $this->notification
- ->expects($this->never())
- ->method('getSubjectParameters');
- $this->notification
- ->expects($this->never())
- ->method('setParsedSubject');
- $this->l
- ->expects($this->never())
- ->method('t');
- $this->l10nFactory
- ->expects($this->once())
- ->method('get')
- ->willReturn($this->l);
- $this->comment
- ->expects($this->any())
- ->method('getActorId')
- ->willReturn('huraga');
- $this->comment
- ->expects($this->any())
- ->method('getActorType')
- ->willReturn('users');
- $this->commentsManager
- ->expects($this->once())
- ->method('get')
- ->willReturn($this->comment);
- $this->userManager
- ->expects($this->once())
- ->method('getDisplayName')
- ->with('huraga')
- ->willReturn($displayName);
- $this->notifier->prepare($this->notification, $this->lc);
- }
- public function testPrepareNotFiles() {
- $this->expectException(\InvalidArgumentException::class);
- $displayName = 'Huraga';
- $this->folder
- ->expects($this->never())
- ->method('getById');
- $this->notification
- ->expects($this->once())
- ->method('getApp')
- ->willReturn('comments');
- $this->notification
- ->expects($this->once())
- ->method('getSubject')
- ->willReturn('mention');
- $this->notification
- ->expects($this->once())
- ->method('getSubjectParameters')
- ->willReturn(['ships', '678']);
- $this->notification
- ->expects($this->never())
- ->method('setParsedSubject');
- $this->l
- ->expects($this->never())
- ->method('t');
- $this->l10nFactory
- ->expects($this->once())
- ->method('get')
- ->willReturn($this->l);
- $this->comment
- ->expects($this->any())
- ->method('getActorId')
- ->willReturn('huraga');
- $this->comment
- ->expects($this->any())
- ->method('getActorType')
- ->willReturn('users');
- $this->commentsManager
- ->expects($this->once())
- ->method('get')
- ->willReturn($this->comment);
- $this->userManager
- ->expects($this->once())
- ->method('getDisplayName')
- ->with('huraga')
- ->willReturn($displayName);
- $this->notifier->prepare($this->notification, $this->lc);
- }
- public function testPrepareUnresolvableFileID() {
- $this->expectException(\OCP\Notification\AlreadyProcessedException::class);
- $displayName = 'Huraga';
- $userFolder = $this->createMock(Folder::class);
- $this->folder->expects($this->once())
- ->method('getUserFolder')
- ->with('you')
- ->willReturn($userFolder);
- $userFolder->expects($this->once())
- ->method('getById')
- ->with('678')
- ->willReturn([]);
- $this->notification->expects($this->once())
- ->method('getUser')
- ->willReturn('you');
- $this->notification
- ->expects($this->once())
- ->method('getApp')
- ->willReturn('comments');
- $this->notification
- ->expects($this->once())
- ->method('getSubject')
- ->willReturn('mention');
- $this->notification
- ->expects($this->once())
- ->method('getSubjectParameters')
- ->willReturn(['files', '678']);
- $this->notification
- ->expects($this->never())
- ->method('setParsedSubject');
- $this->l
- ->expects($this->never())
- ->method('t');
- $this->l10nFactory
- ->expects($this->once())
- ->method('get')
- ->willReturn($this->l);
- $this->comment
- ->expects($this->any())
- ->method('getActorId')
- ->willReturn('huraga');
- $this->comment
- ->expects($this->any())
- ->method('getActorType')
- ->willReturn('users');
- $this->commentsManager
- ->expects($this->once())
- ->method('get')
- ->willReturn($this->comment);
- $this->userManager
- ->expects($this->once())
- ->method('getDisplayName')
- ->with('huraga')
- ->willReturn($displayName);
- $this->notifier->prepare($this->notification, $this->lc);
- }
- }
|