Skip to content

Commit

Permalink
Set the rich subject
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Nov 4, 2016
1 parent 683a24f commit fb570ac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
35 changes: 30 additions & 5 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@


use OCA\AnnouncementCenter\Manager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
Expand All @@ -42,15 +43,20 @@ class Notifier implements INotifier {
/** @var IUserManager */
protected $userManager;

/** @var IURLGenerator */
protected $urlGenerator;

/**
* @param Manager $manager
* @param IFactory $l10nFactory
* @param IUserManager $userManager
* @param IURLGenerator $urlGenerator
*/
public function __construct(Manager $manager, IFactory $l10nFactory, IUserManager $userManager) {
public function __construct(Manager $manager, IFactory $l10nFactory, IUserManager $userManager, IURLGenerator $urlGenerator) {
$this->manager = $manager;
$this->userManager = $userManager;
$this->l10nFactory = $l10nFactory;
$this->userManager = $userManager;
$this->urlGenerator = $urlGenerator;
}

/**
Expand All @@ -74,15 +80,34 @@ public function prepare(INotification $notification, $languageCode) {
$params = $notification->getSubjectParameters();
$user = $this->userManager->get($params[0]);
if ($user instanceof IUser) {
$params[0] = $user->getDisplayName();
$displayName = $user->getDisplayName();
} else {
$displayName = $params[0];
}

$announcement = $this->manager->getAnnouncement((int) $notification->getObjectId(), false, false, false);
$params[] = str_replace("\n", ' ', $announcement['subject']);
$subject = str_replace("\n", ' ', $announcement['subject']);
$parsedParameters = [$displayName, $subject];

$notification->setParsedMessage($announcement['message'])
->setRichSubject(
$l->t('{user} announced “{announcement}”'),
[
'user' => [
'type' => 'user',
'id' => $params[0],
'name' => $displayName,
],
'announcement' => [
'type' => 'announcement',
'id' => $notification->getObjectId(),
'name' => $subject,
'link' => $this->urlGenerator->linkToRouteAbsolute('announcementcenter.page.index') . '#' . $notification->getObjectId(),
],
]
)
->setParsedSubject(
(string) $l->t('%1$s announced “%2$s”', $params)
(string) $l->t('%1$s announced “%2$s”', $parsedParameters)
);
return $notification;

Expand Down
25 changes: 14 additions & 11 deletions tests/Notification/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\AnnouncementCenter\Manager;
use OCA\AnnouncementCenter\Tests\TestCase;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;

Expand All @@ -40,21 +41,18 @@ class NotifierTest extends TestCase {
protected $userManager;
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $factory;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
protected $urlGenerator;
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
protected $l;

protected function setUp() {
parent::setUp();

$this->manager = $this->getMockBuilder('OCA\AnnouncementCenter\Manager')
->disableOriginalConstructor()
->getMock();
$this->userManager = $this->getMockBuilder('OCP\IUserManager')
->disableOriginalConstructor()
->getMock();
$this->l = $this->getMockBuilder('OCP\IL10N')
->disableOriginalConstructor()
->getMock();
$this->manager = $this->createMock(Manager::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->l = $this->createMock(IL10N::class);
$this->l->expects($this->any())
->method('t')
->willReturnCallback(function($string, $args) {
Expand All @@ -70,7 +68,8 @@ protected function setUp() {
$this->notifier = new Notifier(
$this->manager,
$this->factory,
$this->userManager
$this->userManager,
$this->urlGenerator
);
}

Expand Down Expand Up @@ -157,7 +156,7 @@ public function testPrepare($author, $subject, $message, $objectId, $userObject,
$notification->expects($this->once())
->method('getSubjectParameters')
->willReturn([$author]);
$notification->expects($this->once())
$notification->expects($this->exactly(3))
->method('getObjectId')
->willReturn($objectId);

Expand All @@ -177,6 +176,10 @@ public function testPrepare($author, $subject, $message, $objectId, $userObject,
->method('setParsedMessage')
->with($expectedMessage)
->willReturnSelf();
$notification->expects($this->once())
->method('setRichSubject')
->with('{user} announced “{announcement}”', $this->anything())
->willReturnSelf();
$notification->expects($this->once())
->method('setParsedSubject')
->with($expectedSubject)
Expand Down

0 comments on commit fb570ac

Please sign in to comment.