From c6469be8bdb70a57da5212f7b1e8e4836e0bb2be Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 3 Sep 2015 14:47:49 +0200 Subject: [PATCH] Add tests for notification --- lib/private/notification/notification.php | 16 +- tests/lib/notification/notificationtest.php | 602 ++++++++++++++++++++ 2 files changed, 610 insertions(+), 8 deletions(-) create mode 100644 tests/lib/notification/notificationtest.php diff --git a/lib/private/notification/notification.php b/lib/private/notification/notification.php index 5151b5546a..2f53a34728 100644 --- a/lib/private/notification/notification.php +++ b/lib/private/notification/notification.php @@ -120,7 +120,7 @@ class Notification implements INotification { * @since 8.2.0 */ public function setUser($user) { - if (!is_string($user) || $user === '' || isset($app[64])) { + if (!is_string($user) || $user === '' || isset($user[64])) { throw new \InvalidArgumentException('The given user id is invalid'); } $this->user = $user; @@ -413,7 +413,7 @@ class Notification implements INotification { return $this->isValidCommon() && - $this->subject !== '' + $this->getSubject() !== '' ; } @@ -425,7 +425,7 @@ class Notification implements INotification { return $this->isValidCommon() && - $this->subjectParsed !== '' + $this->getParsedSubject() !== '' ; } @@ -434,15 +434,15 @@ class Notification implements INotification { */ protected function isValidCommon() { return - $this->app !== '' + $this->getApp() !== '' && - $this->user !== '' + $this->getUser() !== '' && - $this->timestamp !== 0 + $this->getTimestamp() !== 0 && - $this->objectType !== '' + $this->getObjectType() !== '' && - $this->objectId !== 0 + $this->getObjectId() !== 0 ; } } diff --git a/tests/lib/notification/notificationtest.php b/tests/lib/notification/notificationtest.php new file mode 100644 index 0000000000..8f1779dd96 --- /dev/null +++ b/tests/lib/notification/notificationtest.php @@ -0,0 +1,602 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace Test\Notification; + + +use OC\Notification\Action; +use OC\Notification\Notification; +use OCP\Notification\IAction; +use OCP\Notification\INotification; + +class NotificationTest extends \Test\TestCase { + /** @var INotification */ + protected $notification; + + public function setUp() { + parent::setUp(); + $this->notification = new Notification(); + } + + protected function dataValidString($maxLength) { + $dataSets = [ + ['test1'], + [str_repeat('a', 1)], + ]; + if ($maxLength !== false) { + $dataSets[] = [str_repeat('a', $maxLength)]; + } + return $dataSets; + } + + protected function dataInvalidString($maxLength) { + $dataSets = [ + [true], + [false], + [0], + [1], + [''], + [[]], + ]; + if ($maxLength !== false) { + $dataSets[] = [str_repeat('a', $maxLength + 1)]; + $dataSets[] = [[str_repeat('a', $maxLength + 1)]]; + } + return $dataSets; + } + + protected function dataValidInt() { + return [ + [0], + [1], + [time()], + ]; + } + + protected function dataInvalidInt() { + return [ + [true], + [false], + [''], + ['a'], + [str_repeat('a', 256)], + [[]], + [['a']], + [[str_repeat('a', 256)]], + ]; + } + + public function dataSetApp() { + return $this->dataValidString(32); + } + + /** + * @dataProvider dataSetApp + * @param string $app + */ + public function testSetApp($app) { + $this->assertSame('', $this->notification->getApp()); + $this->notification->setApp($app); + $this->assertSame($app, $this->notification->getApp()); + } + + public function dataSetAppInvalid() { + return $this->dataInvalidString(32); + } + + /** + * @dataProvider dataSetAppInvalid + * @param mixed $app + * + * @expectedException \InvalidArgumentException + */ + public function testSetAppInvalid($app) { + $this->notification->setApp($app); + } + + public function dataSetUser() { + return $this->dataValidString(64); + } + + /** + * @dataProvider dataSetUser + * @param string $user + */ + public function testSetUser($user) { + $this->assertSame('', $this->notification->getUser()); + $this->notification->setUser($user); + $this->assertSame($user, $this->notification->getUser()); + } + + public function dataSetUserInvalid() { + return $this->dataInvalidString(64); + } + + /** + * @dataProvider dataSetUserInvalid + * @param mixed $user + * + * @expectedException \InvalidArgumentException + */ + public function testSetUserInvalid($user) { + $this->notification->setUser($user); + } + + public function dataSetTimestamp() { + return $this->dataValidInt(); + } + + /** + * @dataProvider dataSetTimestamp + * @param int $timestamp + */ + public function testSetTimestamp($timestamp) { + $this->assertSame(0, $this->notification->getTimestamp()); + $this->notification->setTimestamp($timestamp); + $this->assertSame($timestamp, $this->notification->getTimestamp()); + } + + public function dataSetTimestampInvalid() { + return $this->dataInvalidInt(); + } + + /** + * @dataProvider dataSetTimestampInvalid + * @param mixed $timestamp + * + * @expectedException \InvalidArgumentException + */ + public function testSetTimestampInvalid($timestamp) { + $this->notification->setTimestamp($timestamp); + } + + public function dataSetObject() { + return [ + ['a', 1], + [str_repeat('a', 64), time()], + ]; + } + + /** + * @dataProvider dataSetObject + * @param string $type + * @param int $id + */ + public function testSetObject($type, $id) { + $this->assertSame('', $this->notification->getObjectType()); + $this->assertSame(0, $this->notification->getObjectId()); + $this->notification->setObject($type, $id); + $this->assertSame($type, $this->notification->getObjectType()); + $this->assertSame($id, $this->notification->getObjectId()); + } + + public function dataSetObjectTypeInvalid() { + return $this->dataInvalidString(64); + } + + /** + * @dataProvider dataSetObjectTypeInvalid + * @param mixed $type + * + * @expectedException \InvalidArgumentException + * @expectedMessage 'The given object type is invalid' + */ + public function testSetObjectTypeInvalid($type) { + $this->notification->setObject($type, 1337); + } + + public function dataSetObjectIdInvalid() { + return $this->dataInvalidInt(); + } + + /** + * @dataProvider dataSetObjectIdInvalid + * @param mixed $id + * + * @expectedException \InvalidArgumentException + * @expectedMessage 'The given object id is invalid' + */ + public function testSetObjectIdInvalid($id) { + $this->notification->setObject('object', $id); + } + + public function dataSetSubject() { + return [ + ['a', []], + [str_repeat('a', 64), [str_repeat('a', 160)]], + [str_repeat('a', 64), array_fill(0, 160, 'a')], + ]; + } + + /** + * @dataProvider dataSetSubject + * @param string $subject + * @param array $parameters + */ + public function testSetSubject($subject, $parameters) { + $this->assertSame('', $this->notification->getSubject()); + $this->assertSame([], $this->notification->getSubjectParameters()); + $this->notification->setSubject($subject, $parameters); + $this->assertSame($subject, $this->notification->getSubject()); + $this->assertSame($parameters, $this->notification->getSubjectParameters()); + } + + public function dataSetSubjectInvalidSubject() { + return $this->dataInvalidString(64); + } + + /** + * @dataProvider dataSetSubjectInvalidSubject + * @param mixed $subject + * + * @expectedException \InvalidArgumentException + */ + public function testSetSubjectInvalidSubject($subject) { + $this->notification->setSubject($subject, []); + } + + public function dataSetParsedSubject() { + return $this->dataValidString(false); + } + + /** + * @dataProvider dataSetParsedSubject + * @param string $subject + */ + public function testSetParsedSubject($subject) { + $this->assertSame('', $this->notification->getParsedSubject()); + $this->notification->setParsedSubject($subject); + $this->assertSame($subject, $this->notification->getParsedSubject()); + } + + public function dataSetParsedSubjectInvalid() { + return $this->dataInvalidString(false); + } + + /** + * @dataProvider dataSetParsedSubjectInvalid + * @param mixed $subject + * + * @expectedException \InvalidArgumentException + */ + public function testSetParsedSubjectInvalid($subject) { + $this->notification->setParsedSubject($subject); + } + + public function dataSetMessage() { + return [ + ['a', []], + [str_repeat('a', 64), [str_repeat('a', 160)]], + [str_repeat('a', 64), array_fill(0, 160, 'a')], + ]; + } + + /** + * @dataProvider dataSetMessage + * @param string $message + * @param array $parameters + */ + public function testSetMessage($message, $parameters) { + $this->assertSame('', $this->notification->getMessage()); + $this->assertSame([], $this->notification->getMessageParameters()); + $this->notification->setMessage($message, $parameters); + $this->assertSame($message, $this->notification->getMessage()); + $this->assertSame($parameters, $this->notification->getMessageParameters()); + } + + public function dataSetMessageInvalidMessage() { + return $this->dataInvalidString(64); + } + + /** + * @dataProvider dataSetMessageInvalidMessage + * @param mixed $message + * + * @expectedException \InvalidArgumentException + */ + public function testSetMessageInvalidMessage($message) { + $this->notification->setMessage($message, []); + } + + public function dataSetParsedMessage() { + return $this->dataValidString(false); + } + + /** + * @dataProvider dataSetParsedMessage + * @param string $message + */ + public function testSetParsedMessage($message) { + $this->assertSame('', $this->notification->getParsedMessage()); + $this->notification->setParsedMessage($message); + $this->assertSame($message, $this->notification->getParsedMessage()); + } + + public function dataSetParsedMessageInvalid() { + return $this->dataInvalidString(false); + } + + /** + * @dataProvider dataSetParsedMessageInvalid + * @param mixed $message + * + * @expectedException \InvalidArgumentException + */ + public function testSetParsedMessageInvalid($message) { + $this->notification->setParsedMessage($message); + } + + public function dataSetLink() { + return $this->dataValidString(4000); + } + + /** + * @dataProvider dataSetLink + * @param string $link + */ + public function testSetLink($link) { + $this->assertSame('', $this->notification->getLink()); + $this->notification->setLink($link); + $this->assertSame($link, $this->notification->getLink()); + } + + public function dataSetLinkInvalid() { + return $this->dataInvalidString(4000); + } + + /** + * @dataProvider dataSetLinkInvalid + * @param mixed $link + * + * @expectedException \InvalidArgumentException + */ + public function testSetLinkInvalid($link) { + $this->notification->setLink($link); + } + + public function dataSetIcon() { + return $this->dataValidString(64); + } + + /** + * @dataProvider dataSetIcon + * @param string $icon + */ + public function testSetIcon($icon) { + $this->assertSame('', $this->notification->getIcon()); + $this->notification->setIcon($icon); + $this->assertSame($icon, $this->notification->getIcon()); + } + + public function dataSetIconInvalid() { + return $this->dataInvalidString(64); + } + + /** + * @dataProvider dataSetIconInvalid + * @param mixed $icon + * + * @expectedException \InvalidArgumentException + */ + public function testSetIconInvalid($icon) { + $this->notification->setIcon($icon); + } + + public function testCreateAction() { + $action = $this->notification->createAction(); + $this->assertInstanceOf('OCP\Notification\IAction', $action); + } + + public function testAddAction() { + /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */ + $action = $this->getMockBuilder('OCP\Notification\IAction') + ->disableOriginalConstructor() + ->getMock(); + $action->expects($this->once()) + ->method('isValid') + ->willReturn(true); + $action->expects($this->never()) + ->method('isValidParsed'); + + $this->notification->addAction($action); + + $this->assertEquals([$action], $this->notification->getActions()); + $this->assertEquals([], $this->notification->getParsedActions()); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testAddActionInvalid() { + /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */ + $action = $this->getMockBuilder('OCP\Notification\IAction') + ->disableOriginalConstructor() + ->getMock(); + $action->expects($this->once()) + ->method('isValid') + ->willReturn(false); + $action->expects($this->never()) + ->method('isValidParsed'); + + $this->notification->addAction($action); + } + + public function testAddParsedAction() { + /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */ + $action = $this->getMockBuilder('OCP\Notification\IAction') + ->disableOriginalConstructor() + ->getMock(); + $action->expects($this->once()) + ->method('isValidParsed') + ->willReturn(true); + $action->expects($this->never()) + ->method('isValid'); + + $this->notification->addParsedAction($action); + + $this->assertEquals([$action], $this->notification->getParsedActions()); + $this->assertEquals([], $this->notification->getActions()); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testAddParsedActionInvalid() { + /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */ + $action = $this->getMockBuilder('OCP\Notification\IAction') + ->disableOriginalConstructor() + ->getMock(); + $action->expects($this->once()) + ->method('isValidParsed') + ->willReturn(false); + $action->expects($this->never()) + ->method('isValid'); + + $this->notification->addParsedAction($action); + } + + public function dataIsValid() { + return [ + [false, '', false], + [true, '', false], + [false, 'a', false], + [true, 'a', true], + ]; + } + + /** + * @dataProvider dataIsValid + * + * @param bool $isValidCommon + * @param string $subject + * @param bool $expected + */ + public function testIsValid($isValidCommon, $subject, $expected) { + /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ + $notification = $this->getMockBuilder('\OC\Notification\Notification') + ->setMethods([ + 'isValidCommon', + 'getSubject', + ]) + ->getMock(); + + $notification->expects($this->once()) + ->method('isValidCommon') + ->willReturn($isValidCommon); + + $notification->expects(!$isValidCommon ? $this->never() : $this->once()) + ->method('getSubject') + ->willReturn($subject); + + $notification->expects($this->never()) + ->method('getParsedSubject') + ->willReturn($subject); + + $this->assertEquals($expected, $notification->isValid()); + } + + /** + * @dataProvider dataIsValid + * + * @param bool $isValidCommon + * @param string $subject + * @param bool $expected + */ + public function testIsParsedValid($isValidCommon, $subject, $expected) { + /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ + $notification = $this->getMockBuilder('\OC\Notification\Notification') + ->setMethods([ + 'isValidCommon', + 'getParsedSubject', + ]) + ->getMock(); + + $notification->expects($this->once()) + ->method('isValidCommon') + ->willReturn($isValidCommon); + + $notification->expects(!$isValidCommon ? $this->never() : $this->once()) + ->method('getParsedSubject') + ->willReturn($subject); + + $notification->expects($this->never()) + ->method('getSubject') + ->willReturn($subject); + + $this->assertEquals($expected, $notification->isValidParsed()); + } + + public function dataIsValidCommon() { + return [ + ['', '', 0, '', 0, false], + ['app', '', 0, '', 0, false], + ['app', 'user', 0, '', 0, false], + ['app', 'user', time(), '', 0, false], + ['app', 'user', time(), 'type', 0, false], + ['app', 'user', time(), 'type', 42, true], + ]; + } + + /** + * @dataProvider dataIsValidCommon + * + * @param string $app + * @param string $user + * @param int $timestamp + * @param string $objectType + * @param int $objectId + * @param bool $expected + */ + public function testIsValidCommon($app, $user, $timestamp, $objectType, $objectId, $expected) { + /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ + $notification = $this->getMockBuilder('\OC\Notification\Notification') + ->setMethods([ + 'getApp', + 'getUser', + 'getTimestamp', + 'getObjectType', + 'getObjectId', + ]) + ->getMock(); + + $notification->expects($this->any()) + ->method('getApp') + ->willReturn($app); + + $notification->expects($this->any()) + ->method('getUser') + ->willReturn($user); + + $notification->expects($this->any()) + ->method('getTimestamp') + ->willReturn($timestamp); + + $notification->expects($this->any()) + ->method('getObjectType') + ->willReturn($objectType); + + $notification->expects($this->any()) + ->method('getObjectId') + ->willReturn($objectId); + + $this->assertEquals($expected, $this->invokePrivate($notification, 'isValidCommon')); + } +}