Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove line breaks from notifications and trim the message #20

Merged
merged 1 commit into from
Nov 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions lib/notificationsnotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public function prepare(INotification $notification, $languageCode) {
case 'announced':
$params = $notification->getSubjectParameters();

$announcement = $this->manager->getAnnouncement($notification->getObjectId(), true);
$params[] = $announcement['subject'];
$announcement = $this->manager->getAnnouncement($notification->getObjectId(), false);
$params[] = $this->prepareMessage($announcement['subject']);

$notification->setParsedMessage($announcement['message'])
$notification->setParsedMessage($this->prepareMessage($announcement['message']))
->setParsedSubject(
(string) $l->t('%1$s announced “%2$s”', $params)
);
Expand All @@ -77,4 +77,27 @@ public function prepare(INotification $notification, $languageCode) {
throw new \InvalidArgumentException();
}
}

/**
* Prepare message for notification usage
*
* + Replace line breaks with spaces
* + Trim on word end after 100 chars or hard 120 chars
*
* @param string $message
* @return string
*/
protected function prepareMessage($message) {
$message = str_replace("\n", ' ', $message);

if (isset($message[120])) {
$findSpace = strpos($message, ' ', 100);
if ($findSpace !== false && $findSpace < 120) {
return substr($message, 0, $findSpace) . '…';
}
return substr($message, 0, 120) . '…';
}

return $message;
}
}
196 changes: 196 additions & 0 deletions tests/lib/NotificationsNotifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php
/**
* ownCloud - AnnouncementCenter App
*
* @author Joas Schilling
* @copyright 2015 Joas Schilling nickvergessen@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\AnnouncementCenter\Tests\Lib;

use OCA\AnnouncementCenter\NotificationsNotifier;
use OCA\AnnouncementCenter\Manager;
use OCA\AnnouncementCenter\Tests\TestCase;
use OCP\IL10N;
use OCP\L10N\IFactory;

class NotificationsNotifierTest extends TestCase {
/** @var NotificationsNotifier */
protected $notifier;

/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
protected $manager;
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $factory;
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
protected $l;

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

$this->manager = $this->getMockBuilder('OCA\AnnouncementCenter\Manager')
->disableOriginalConstructor()
->getMock();
$this->l = $this->getMockBuilder('OCP\IL10N')
->disableOriginalConstructor()
->getMock();
$this->l->expects($this->any())
->method('t')
->willReturnCallback(function($string, $args) {
return vsprintf($string, $args);
});
$this->factory = $this->getMockBuilder('OCP\L10N\IFactory')
->disableOriginalConstructor()
->getMock();
$this->factory->expects($this->any())
->method('get')
->willReturn($this->l);

$this->notifier = new NotificationsNotifier(
$this->manager,
$this->factory
);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testPrepareWrongApp() {
/** @var \OC\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
$notification = $this->getMockBuilder('OC\Notification\INotification')
->disableOriginalConstructor()
->getMock();

$notification->expects($this->once())
->method('getApp')
->willReturn('notifications');
$notification->expects($this->never())
->method('getSubject');

$this->notifier->prepare($notification, 'en');
}

/**
* @expectedException \InvalidArgumentException
*/
public function testPrepareWrongSubject() {
/** @var \OC\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
$notification = $this->getMockBuilder('OC\Notification\INotification')
->disableOriginalConstructor()
->getMock();

$notification->expects($this->once())
->method('getApp')
->willReturn('announcementcenter');
$notification->expects($this->once())
->method('getSubject')
->willReturn('wrong subject');

$this->notifier->prepare($notification, 'en');
}

public function dataPrepare() {
$subject = "subject\nsubject subject subject subject subject subject subject subject subject subject subject subject subject subject subject subject subject subject";
$subjectTrim = 'subject subject subject subject subject subject subject subject subject subject subject subject subject…';
$message = "message\nmessage message message message message message message message message message message messagemessagemessagemessagemessagemessagemessage";
$messageTrim = 'message message message message message message message message message message message message messagemessagemessagemes…';
return [
['author', 'subject', 'message', 42, 'author announced “subject”', 'message'],
['author2', $subject, $message, 21, 'author2 announced “' . $subjectTrim . '”', $messageTrim],
];
}

/**
* @dataProvider dataPrepare
*
* @param string $author
* @param string $subject
* @param string $message
* @param int $objectId
* @param string $expectedSubject
* @param string $expectedMessage
*/
public function testPrepare($author, $subject, $message, $objectId, $expectedSubject, $expectedMessage) {
/** @var \OC\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
$notification = $this->getMockBuilder('OC\Notification\INotification')
->disableOriginalConstructor()
->getMock();

$notification->expects($this->once())
->method('getApp')
->willReturn('announcementcenter');
$notification->expects($this->once())
->method('getSubject')
->willReturn('announced');
$notification->expects($this->once())
->method('getSubjectParameters')
->willReturn([$author]);
$notification->expects($this->once())
->method('getObjectId')
->willReturn($objectId);

$this->manager->expects($this->once())
->method('getAnnouncement')
->with($objectId, false)
->willReturn([
'subject' => $subject,
'message' => $message,
]);

$notification->expects($this->once())
->method('setParsedMessage')
->with($expectedMessage)
->willReturnSelf();
$notification->expects($this->once())
->method('setParsedSubject')
->with($expectedSubject)
->willReturnSelf();

$return = $this->notifier->prepare($notification, 'en');

$this->assertEquals($notification, $return);
}

public function te1stAnnouncement() {
$subject = 'subject' . "\n<html>";
$message = 'message' . "\n<html>";
$author = 'author';
$time = time() - 10;

$announcement = $this->manager->announce($subject, $message, $author, $time);
$this->assertInternalType('int', $announcement['id']);
$this->assertGreaterThan(0, $announcement['id']);
$this->assertSame('subject &lt;html&gt;', $announcement['subject']);
$this->assertSame('message<br />&lt;html&gt;', $announcement['message']);
$this->assertSame('author', $announcement['author']);
$this->assertSame($time, $announcement['time']);

$this->assertEquals($announcement, $this->manager->getAnnouncement($announcement['id']));

$this->assertEquals($announcement, $this->manager->getAnnouncement($announcement['id']));

$this->assertEquals([$announcement], $this->manager->getAnnouncements(1));

$this->manager->delete($announcement['id']);

try {
$this->manager->getAnnouncement($announcement['id']);
$this->fail('Failed to delete the announcement');
} catch (\InvalidArgumentException $e) {
$this->assertInstanceOf('InvalidArgumentException', $e);
}
}
}