Add unit tests for comment handling

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2018-10-09 13:06:41 +02:00
parent 5edb95fc6b
commit da0cfdb1dc
3 changed files with 163 additions and 7 deletions

View File

@@ -23,7 +23,6 @@
namespace OCA\Deck\Activity;
use OCA\Deck\Db\CardMapper;
use OCA\Deck\Notification\NotificationHelper;
use OCP\Comments\CommentsEvent;
use \OCP\Comments\ICommentsEventHandler;
@@ -32,12 +31,11 @@ class CommentEventHandler implements ICommentsEventHandler {
/** @var ActivityManager */
private $activityManager;
/** @var CardMapper */
private $cardMapper;
/** @var NotificationHelper */
private $notificationHelper;
public function __construct(ActivityManager $activityManager, NotificationHelper $notificationHelper, CardMapper $cardMapper) {
$this->cardMapper = $cardMapper;
public function __construct(ActivityManager $activityManager, NotificationHelper $notificationHelper) {
$this->notificationHelper = $notificationHelper;
$this->activityManager = $activityManager;
}

View File

@@ -0,0 +1,111 @@
<?php
/**
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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\Deck\Activity;
use OCA\Deck\Db\AclMapper;
use OCA\Deck\Db\AssignedUsers;
use OCA\Deck\Db\Attachment;
use OCA\Deck\Db\AttachmentMapper;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\BoardMapper;
use OCA\Deck\Db\Card;
use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\Label;
use OCA\Deck\Db\Stack;
use OCA\Deck\Db\StackMapper;
use OCA\Deck\Notification\NotificationHelper;
use OCA\Deck\Service\PermissionService;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\Comments\CommentsEvent;
use OCP\Comments\IComment;
use OCP\IL10N;
use OCP\IUser;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
class CommentEventHandlerTest extends TestCase {
/** @var CommentEventHandler */
private $commentEventHandler;
/** @var ActivityManager */
private $activityManager;
/** @var NotificationHelper */
private $notificationHelper;
public function setUp() {
$this->activityManager = $this->createMock(ActivityManager::class);
$this->notificationHelper = $this->createMock(NotificationHelper::class);
$this->commentEventHandler = new CommentEventHandler(
$this->activityManager,
$this->notificationHelper
);
}
public function testHandle() {
$comment = $this->createMock(IComment::class);
$comment->expects($this->any())->method('getId')->willReturn(1);
$comment->expects($this->any())->method('getObjectType')->willReturn('deckCard');
$commentsEvent = new CommentsEvent(CommentsEvent::EVENT_ADD, $comment);
$this->activityManager->expects($this->once())
->method('triggerEvent')
->with(ActivityManager::DECK_OBJECT_CARD, $comment, ActivityManager::SUBJECT_CARD_COMMENT_CREATE, ['comment' => 1]);
$this->notificationHelper->expects($this->once())
->method('sendMention')
->with($comment);
$this->commentEventHandler->handle($commentsEvent);
}
public function testHandleUpdate() {
$comment = $this->createMock(IComment::class);
$comment->expects($this->any())->method('getId')->willReturn(1);
$comment->expects($this->any())->method('getObjectType')->willReturn('deckCard');
$commentsEvent = new CommentsEvent(CommentsEvent::EVENT_UPDATE, $comment);
$this->activityManager->expects($this->never())
->method('triggerEvent');
$this->notificationHelper->expects($this->once())
->method('sendMention')
->with($comment);
$this->commentEventHandler->handle($commentsEvent);
}
public function testHandleInvalid() {
$comment = $this->createMock(IComment::class);
$comment->expects($this->any())->method('getId')->willReturn(1);
$comment->expects($this->any())->method('getObjectType')->willReturn('other');
$commentsEvent = new CommentsEvent(CommentsEvent::EVENT_ADD, $comment);
$this->activityManager->expects($this->never())
->method('triggerEvent');
$this->commentEventHandler->handle($commentsEvent);
}
public function invokePrivate(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
}

View File

@@ -35,7 +35,7 @@ use OCP\Notification\INotifier;
use OCP\RichObjectStrings\Definitions;
class UnknownUserTest extends \Test\TestCase {
class NotifierTest extends \Test\TestCase {
/** @var IFactory */
protected $l10nFactory;
@@ -129,6 +129,53 @@ class UnknownUserTest extends \Test\TestCase {
}
public function testPrepareCardCommentMentioned() {
/** @var INotification $notification */
$notification = $this->createMock(INotification::class);
$notification->expects($this->once())
->method('getApp')
->willReturn('deck');
$notification->expects($this->once())
->method('getSubjectParameters')
->willReturn(['Card title', 'Board title', 'admin']);
$notification->expects($this->once())
->method('getSubject')
->willReturn('card-comment-mentioned');
$notification->expects($this->once())
->method('getObjectId')
->willReturn('123');
$this->cardMapper->expects($this->once())
->method('findBoardId')
->willReturn(999);
$expectedMessage = 'admin has mentioned in a comment on "Card title".';
$notification->expects($this->once())
->method('setParsedSubject')
->with($expectedMessage);
$notification->expects($this->once())
->method('setRichSubject')
->with('{user} has mentioned in a comment on "Card title".');
$this->url->expects($this->once())
->method('imagePath')
->with('deck', 'deck-dark.svg')
->willReturn('deck-dark.svg');
$this->url->expects($this->once())
->method('getAbsoluteURL')
->with('deck-dark.svg')
->willReturn('/absolute/deck-dark.svg');
$notification->expects($this->once())
->method('setIcon')
->with('/absolute/deck-dark.svg');
$actualNotification = $this->notifier->prepare($notification, 'en_US');
$this->assertEquals($notification, $actualNotification);
}
public function dataPrepareCardAssigned() {
return [
[true], [false]
@@ -269,4 +316,4 @@ class UnknownUserTest extends \Test\TestCase {
$this->assertEquals($notification, $actualNotification);
}
}
}