summaryrefslogtreecommitdiffstats
path: root/tests/lib/Notification
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@gmx.de>2016-05-20 15:38:20 +0200
committerThomas Müller <DeepDiver1975@users.noreply.github.com>2016-05-20 15:38:20 +0200
commit94ad54ec9b96d41a614fbbad4a97b34c41a6901f (patch)
treef3eb7cdda2704aaf0cd59d58efe66bcbd34cb67d /tests/lib/Notification
parent2ef751b1ec28f7b5c7113af60ec8c9fa0ae1cf87 (diff)
downloadnextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.tar.gz
nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.zip
Move tests/ to PSR-4 (#24731)
* Move a-b to PSR-4 * Move c-d to PSR-4 * Move e+g to PSR-4 * Move h-l to PSR-4 * Move m-r to PSR-4 * Move s-u to PSR-4 * Move files/ to PSR-4 * Move remaining tests to PSR-4 * Remove Test\ from old autoloader
Diffstat (limited to 'tests/lib/Notification')
-rw-r--r--tests/lib/Notification/ActionTest.php233
-rw-r--r--tests/lib/Notification/ManagerTest.php418
-rw-r--r--tests/lib/Notification/NotificationTest.php629
3 files changed, 1280 insertions, 0 deletions
diff --git a/tests/lib/Notification/ActionTest.php b/tests/lib/Notification/ActionTest.php
new file mode 100644
index 00000000000..74c995280c9
--- /dev/null
+++ b/tests/lib/Notification/ActionTest.php
@@ -0,0 +1,233 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ *
+ * @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 <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Notification;
+
+
+use OC\Notification\Action;
+use OCP\Notification\IAction;
+use Test\TestCase;
+
+class ActionTest extends TestCase {
+ /** @var IAction */
+ protected $action;
+
+ public function setUp() {
+ parent::setUp();
+ $this->action = new Action();
+ }
+
+ public function dataSetLabel() {
+ return [
+ ['test1'],
+ [str_repeat('a', 1)],
+ [str_repeat('a', 32)],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetLabel
+ * @param string $label
+ */
+ public function testSetLabel($label) {
+ $this->assertSame('', $this->action->getLabel());
+ $this->assertSame($this->action, $this->action->setLabel($label));
+ $this->assertSame($label, $this->action->getLabel());
+ }
+
+ public function dataSetLabelInvalid() {
+ return [
+ [true],
+ [false],
+ [0],
+ [1],
+ [''],
+ [str_repeat('a', 33)],
+ [[]],
+ [[str_repeat('a', 33)]],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetLabelInvalid
+ * @param mixed $label
+ *
+ * @expectedException \InvalidArgumentException
+ */
+ public function testSetLabelInvalid($label) {
+ $this->action->setLabel($label);
+ }
+
+ public function dataSetParsedLabel() {
+ return [
+ ['test1'],
+ [str_repeat('a', 1)],
+ [str_repeat('a', 32)],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetParsedLabel
+ * @param string $label
+ */
+ public function testSetParsedLabel($label) {
+ $this->assertSame('', $this->action->getParsedLabel());
+ $this->assertSame($this->action, $this->action->setParsedLabel($label));
+ $this->assertSame($label, $this->action->getParsedLabel());
+ }
+
+ public function dataSetParsedLabelInvalid() {
+ return [
+ [true],
+ [false],
+ [0],
+ [1],
+ [''],
+ [[]],
+ [[str_repeat('a', 33)]],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetParsedLabelInvalid
+ * @param mixed $label
+ *
+ * @expectedException \InvalidArgumentException
+ */
+ public function testSetParsedLabelInvalid($label) {
+ $this->action->setParsedLabel($label);
+ }
+
+ public function dataSetLink() {
+ return [
+ ['test1', 'GET'],
+ ['test2', 'POST'],
+ [str_repeat('a', 1), 'PUT'],
+ [str_repeat('a', 256), 'DELETE'],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetLink
+ * @param string $link
+ * @param string $type
+ */
+ public function testSetLink($link, $type) {
+ $this->assertSame('', $this->action->getLink());
+ $this->assertSame($this->action, $this->action->setLink($link, $type));
+ $this->assertSame($link, $this->action->getLink());
+ $this->assertSame($type, $this->action->getRequestType());
+ }
+
+ public function dataSetLinkInvalid() {
+ return [
+ // Invalid link
+ [true, 'GET'],
+ [false, 'GET'],
+ [0, 'GET'],
+ [1, 'GET'],
+ ['', 'GET'],
+ [str_repeat('a', 257), 'GET'],
+ [[], 'GET'],
+ [[str_repeat('a', 257)], 'GET'],
+
+ // Invalid type
+ ['url', 'notGET'],
+ ['url', true],
+ ['url', false],
+ ['url', 0],
+ ['url', 1],
+ ['url', []],
+ ['url', ['GET']],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetLinkInvalid
+ * @param mixed $link
+ * @param mixed $type
+ *
+ * @expectedException \InvalidArgumentException
+ */
+ public function testSetLinkInvalid($link, $type) {
+ $this->action->setLink($link, $type);
+ }
+
+ public function dataSetPrimary() {
+ return [
+ [true],
+ [false],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetPrimary
+ * @param bool $primary
+ */
+ public function testSetPrimary($primary) {
+ $this->assertSame(false, $this->action->isPrimary());
+ $this->assertSame($this->action, $this->action->setPrimary($primary));
+ $this->assertSame($primary, $this->action->isPrimary());
+ }
+
+ public function dataSetPrimaryInvalid() {
+ return [
+ [0],
+ [1],
+ [''],
+ [str_repeat('a', 257)],
+ [[]],
+ [[str_repeat('a', 257)]],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetPrimaryInvalid
+ * @param mixed $primary
+ *
+ * @expectedException \InvalidArgumentException
+ */
+ public function testSetPrimaryInvalid($primary) {
+ $this->action->setPrimary($primary);
+ }
+
+ public function testIsValid() {
+ $this->assertFalse($this->action->isValid());
+ $this->assertFalse($this->action->isValidParsed());
+ $this->action->setLabel('label');
+ $this->assertFalse($this->action->isValid());
+ $this->assertFalse($this->action->isValidParsed());
+ $this->action->setLink('link', 'GET');
+ $this->assertTrue($this->action->isValid());
+ $this->assertFalse($this->action->isValidParsed());
+ }
+
+ public function testIsValidParsed() {
+ $this->assertFalse($this->action->isValid());
+ $this->assertFalse($this->action->isValidParsed());
+ $this->action->setParsedLabel('label');
+ $this->assertFalse($this->action->isValid());
+ $this->assertFalse($this->action->isValidParsed());
+ $this->action->setLink('link', 'GET');
+ $this->assertFalse($this->action->isValid());
+ $this->assertTrue($this->action->isValidParsed());
+ }
+}
diff --git a/tests/lib/Notification/ManagerTest.php b/tests/lib/Notification/ManagerTest.php
new file mode 100644
index 00000000000..4410781bc31
--- /dev/null
+++ b/tests/lib/Notification/ManagerTest.php
@@ -0,0 +1,418 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ *
+ * @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 <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Notification;
+
+use OC\Notification\Manager;
+use OCP\Notification\IManager;
+use Test\TestCase;
+
+class ManagerTest extends TestCase {
+ /** @var IManager */
+ protected $manager;
+
+ public function setUp() {
+ parent::setUp();
+ $this->manager = new Manager();
+ }
+
+ public function testRegisterApp() {
+ $app = $this->getMockBuilder('OCP\Notification\IApp')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $closure = function() use ($app) {
+ return $app;
+ };
+
+ $this->assertEquals([], $this->invokePrivate($this->manager, 'getApps'));
+
+ $this->manager->registerApp($closure);
+
+ $this->assertEquals([$app], $this->invokePrivate($this->manager, 'getApps'));
+ $this->assertEquals([$app], $this->invokePrivate($this->manager, 'getApps'));
+
+ $this->manager->registerApp($closure);
+
+ $this->assertEquals([$app, $app], $this->invokePrivate($this->manager, 'getApps'));
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testRegisterAppInvalid() {
+ $notifier = $this->getMockBuilder('OCP\Notification\INotifier')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $closure = function() use ($notifier) {
+ return $notifier;
+ };
+
+ $this->manager->registerApp($closure);
+
+ $this->invokePrivate($this->manager, 'getApps');
+ }
+
+ public function testRegisterNotifier() {
+ $notifier = $this->getMockBuilder('OCP\Notification\INotifier')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $closure = function() use ($notifier) {
+ return $notifier;
+ };
+
+ $this->assertEquals([], $this->invokePrivate($this->manager, 'getNotifiers'));
+ $this->assertEquals([], $this->invokePrivate($this->manager, 'listNotifiers'));
+
+ $this->manager->registerNotifier($closure, function() {
+ return ['id' => 'test1', 'name' => 'Test One'];
+ });
+
+ $this->assertEquals([$notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
+ $this->assertEquals(['test1' => 'Test One'], $this->invokePrivate($this->manager, 'listNotifiers'));
+ $this->assertEquals([$notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
+ $this->assertEquals(['test1' => 'Test One'], $this->invokePrivate($this->manager, 'listNotifiers'));
+
+ $this->manager->registerNotifier($closure, function() {
+ return ['id' => 'test2', 'name' => 'Test Two'];
+ });
+
+ $this->assertEquals([$notifier, $notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
+ $this->assertEquals(['test1' => 'Test One', 'test2' => 'Test Two'], $this->invokePrivate($this->manager, 'listNotifiers'));
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testRegisterNotifierInvalid() {
+ $app = $this->getMockBuilder('OCP\Notification\IApp')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $closure = function() use ($app) {
+ return $app;
+ };
+
+ $this->manager->registerNotifier($closure, function() {
+ return ['id' => 'test1', 'name' => 'Test One'];
+ });
+
+ $this->invokePrivate($this->manager, 'getNotifiers');
+ }
+
+ public function dataRegisterNotifierInfoInvalid() {
+ return [
+ [null],
+ ['No array'],
+ [['id' => 'test1', 'name' => 'Test One', 'size' => 'Invalid']],
+ [['no-id' => 'test1', 'name' => 'Test One']],
+ [['id' => 'test1', 'no-name' => 'Test One']],
+ ];
+ }
+
+ /**
+ * @dataProvider dataRegisterNotifierInfoInvalid
+ * @expectedException \InvalidArgumentException
+ * @param mixed $data
+ */
+ public function testRegisterNotifierInfoInvalid($data) {
+ $app = $this->getMockBuilder('OCP\Notification\IApp')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $closure = function() use ($app) {
+ return $app;
+ };
+
+ $this->manager->registerNotifier($closure, function() use ($data) {
+ return $data;
+ });
+
+ $this->manager->listNotifiers();
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ * @expectedExceptionMessage The given notifier ID test1 is already in use
+ */
+ public function testRegisterNotifierInfoDuplicate() {
+ $app = $this->getMockBuilder('OCP\Notification\IApp')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $closure = function() use ($app) {
+ return $app;
+ };
+
+ $this->manager->registerNotifier($closure, function() {
+ return ['id' => 'test1', 'name' => 'Test One'];
+ });
+
+ $this->manager->registerNotifier($closure, function() {
+ return ['id' => 'test1', 'name' => 'Test One'];
+ });
+
+ $this->manager->listNotifiers();
+ }
+
+ public function testCreateNotification() {
+ $action = $this->manager->createNotification();
+ $this->assertInstanceOf('OCP\Notification\INotification', $action);
+ }
+
+ public function testNotify() {
+ /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notification->expects($this->once())
+ ->method('isValid')
+ ->willReturn(true);
+
+ /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
+ $app = $this->getMockBuilder('OCP\Notification\IApp')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $app->expects($this->once())
+ ->method('notify')
+ ->with($notification);
+
+ /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
+ $app2 = $this->getMockBuilder('OCP\Notification\IApp')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $app2->expects($this->once())
+ ->method('notify')
+ ->with($notification);
+
+ $this->manager->registerApp(function() use ($app) {
+ return $app;
+ });
+ $this->manager->registerApp(function() use ($app2) {
+ return $app2;
+ });
+
+ $this->manager->notify($notification);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testNotifyInvalid() {
+ /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notification->expects($this->once())
+ ->method('isValid')
+ ->willReturn(false);
+
+ $this->manager->notify($notification);
+ }
+
+ public function testPrepare() {
+ /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notification->expects($this->once())
+ ->method('isValidParsed')
+ ->willReturn(true);
+ /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification2 */
+ $notification2 = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notification2->expects($this->exactly(2))
+ ->method('isValidParsed')
+ ->willReturn(true);
+
+ /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
+ $notifier = $this->getMockBuilder('OCP\Notification\INotifier')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notifier->expects($this->once())
+ ->method('prepare')
+ ->with($notification, 'en')
+ ->willReturnArgument(0);
+
+ /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier2 */
+ $notifier2 = $this->getMockBuilder('OCP\Notification\INotifier')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notifier2->expects($this->once())
+ ->method('prepare')
+ ->with($notification, 'en')
+ ->willReturn($notification2);
+
+ $this->manager->registerNotifier(function() use ($notifier) {
+ return $notifier;
+ }, function() {
+ return ['id' => 'test1', 'name' => 'Test One'];
+ });
+ $this->manager->registerNotifier(function() use ($notifier2) {
+ return $notifier2;
+ }, function() {
+ return ['id' => 'test2', 'name' => 'Test Two'];
+ });
+
+ $this->assertEquals($notification2, $this->manager->prepare($notification, 'en'));
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testPrepareInvalid() {
+ /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notification->expects($this->once())
+ ->method('isValidParsed')
+ ->willReturn(false);
+
+ /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
+ $notifier = $this->getMockBuilder('OCP\Notification\INotifier')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notifier->expects($this->once())
+ ->method('prepare')
+ ->with($notification, 'de')
+ ->willReturnArgument(0);
+
+ $this->manager->registerNotifier(function() use ($notifier) {
+ return $notifier;
+ }, function() {
+ return ['id' => 'test1', 'name' => 'Test One'];
+ });
+
+ $this->manager->prepare($notification, 'de');
+ }
+
+ public function testPrepareNotifierThrows() {
+ /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notification->expects($this->once())
+ ->method('isValidParsed')
+ ->willReturn(true);
+
+ /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
+ $notifier = $this->getMockBuilder('OCP\Notification\INotifier')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notifier->expects($this->once())
+ ->method('prepare')
+ ->with($notification, 'de')
+ ->willThrowException(new \InvalidArgumentException);
+
+ $this->manager->registerNotifier(function() use ($notifier) {
+ return $notifier;
+ }, function() {
+ return ['id' => 'test1', 'name' => 'Test One'];
+ });
+
+ $this->assertEquals($notification, $this->manager->prepare($notification, 'de'));
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testPrepareNoNotifier() {
+ /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $notification->expects($this->once())
+ ->method('isValidParsed')
+ ->willReturn(false);
+
+ $this->manager->prepare($notification, 'en');
+ }
+
+ public function testMarkProcessed() {
+ /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
+ $app = $this->getMockBuilder('OCP\Notification\IApp')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $app->expects($this->once())
+ ->method('markProcessed')
+ ->with($notification);
+
+ /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
+ $app2 = $this->getMockBuilder('OCP\Notification\IApp')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $app2->expects($this->once())
+ ->method('markProcessed')
+ ->with($notification);
+
+ $this->manager->registerApp(function() use ($app) {
+ return $app;
+ });
+ $this->manager->registerApp(function() use ($app2) {
+ return $app2;
+ });
+
+ $this->manager->markProcessed($notification);
+ }
+
+ public function testGetCount() {
+ /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
+ $app = $this->getMockBuilder('OCP\Notification\IApp')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $app->expects($this->once())
+ ->method('getCount')
+ ->with($notification)
+ ->willReturn(21);
+
+ /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
+ $app2 = $this->getMockBuilder('OCP\Notification\IApp')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $app2->expects($this->once())
+ ->method('getCount')
+ ->with($notification)
+ ->willReturn(42);
+
+ $this->manager->registerApp(function() use ($app) {
+ return $app;
+ });
+ $this->manager->registerApp(function() use ($app2) {
+ return $app2;
+ });
+
+ $this->assertSame(63, $this->manager->getCount($notification));
+ }
+}
diff --git a/tests/lib/Notification/NotificationTest.php b/tests/lib/Notification/NotificationTest.php
new file mode 100644
index 00000000000..c6ededf0142
--- /dev/null
+++ b/tests/lib/Notification/NotificationTest.php
@@ -0,0 +1,629 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ *
+ * @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 <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Notification;
+
+
+use OC\Notification\Notification;
+use OCP\Notification\INotification;
+use Test\TestCase;
+
+class NotificationTest extends 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 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->assertSame($this->notification, $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->assertSame($this->notification, $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 dataSetDateTime() {
+ $past = new \DateTime();
+ $past->sub(new \DateInterval('P1Y'));
+ $current = new \DateTime();
+ $future = new \DateTime();
+ $future->add(new \DateInterval('P1Y'));
+
+ return [
+ [$past],
+ [$current],
+ [$future],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetDateTime
+ * @param \DateTime $dateTime
+ */
+ public function testSetDateTime(\DateTime $dateTime) {
+ $this->assertSame(0, $this->notification->getDateTime()->getTimestamp());
+ $this->assertSame($this->notification, $this->notification->setDateTime($dateTime));
+ $this->assertSame($dateTime, $this->notification->getDateTime());
+ }
+
+ public function dataSetDateTimeZero() {
+ $nineTeenSeventy = new \DateTime();
+ $nineTeenSeventy->setTimestamp(0);
+ return [
+ [$nineTeenSeventy],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetDateTimeZero
+ * @param \DateTime $dateTime
+ *
+ * @expectedException \InvalidArgumentException
+ * @expectedMessage 'The given date time is invalid'
+ */
+ public function testSetDateTimeZero($dateTime) {
+ $this->notification->setDateTime($dateTime);
+ }
+
+ public function dataSetObject() {
+ return [
+ ['a', '21', '21'],
+ [str_repeat('a', 64), 42, '42'],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetObject
+ * @param string $type
+ * @param int|string $id
+ * @param string $exptectedId
+ */
+ public function testSetObject($type, $id, $exptectedId) {
+ $this->assertSame('', $this->notification->getObjectType());
+ $this->assertSame('', $this->notification->getObjectId());
+ $this->assertSame($this->notification, $this->notification->setObject($type, $id));
+ $this->assertSame($type, $this->notification->getObjectType());
+ $this->assertSame($exptectedId, $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 [
+ [true],
+ [false],
+ [''],
+ [str_repeat('a', 64 + 1)],
+ [[]],
+ [[str_repeat('a', 64 + 1)]],
+ ];
+ }
+
+ /**
+ * @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->assertSame($this->notification, $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->assertSame($this->notification, $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->assertSame($this->notification, $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->assertSame($this->notification, $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->assertSame($this->notification, $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 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->assertSame($this->notification, $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 testAddActionSecondPrimary() {
+ /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
+ $action = $this->getMockBuilder('OCP\Notification\IAction')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $action->expects($this->exactly(2))
+ ->method('isValid')
+ ->willReturn(true);
+ $action->expects($this->exactly(2))
+ ->method('isPrimary')
+ ->willReturn(true);
+
+ $this->assertSame($this->notification, $this->notification->addAction($action));
+
+ $this->setExpectedException('\InvalidArgumentException');
+ $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->assertSame($this->notification, $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 testAddActionSecondParsedPrimary() {
+ /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
+ $action = $this->getMockBuilder('OCP\Notification\IAction')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $action->expects($this->exactly(2))
+ ->method('isValidParsed')
+ ->willReturn(true);
+ $action->expects($this->exactly(2))
+ ->method('isPrimary')
+ ->willReturn(true);
+
+ $this->assertSame($this->notification, $this->notification->addParsedAction($action));
+
+ $this->setExpectedException('\InvalidArgumentException');
+ $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',
+ 'getParsedSubject',
+ ])
+ ->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',
+ 'getSubject',
+ ])
+ ->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, '', '', false],
+ ['app', '', 0, '', '', false],
+ ['app', 'user', 0, '', '', false],
+ ['app', 'user', time(), '', '', false],
+ ['app', 'user', time(), 'type', '', false],
+ ['app', 'user', time(), 'type', '42', true],
+ ];
+ }
+
+ /**
+ * @dataProvider dataIsValidCommon
+ *
+ * @param string $app
+ * @param string $user
+ * @param int $timestamp
+ * @param string $objectType
+ * @param string $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',
+ 'getDateTime',
+ 'getObjectType',
+ 'getObjectId',
+ ])
+ ->getMock();
+
+ $notification->expects($this->any())
+ ->method('getApp')
+ ->willReturn($app);
+
+ $notification->expects($this->any())
+ ->method('getUser')
+ ->willReturn($user);
+
+ $dateTime = new \DateTime();
+ $dateTime->setTimestamp($timestamp);
+
+ $notification->expects($this->any())
+ ->method('getDateTime')
+ ->willReturn($dateTime);
+
+ $notification->expects($this->any())
+ ->method('getObjectType')
+ ->willReturn($objectType);
+
+ $notification->expects($this->any())
+ ->method('getObjectId')
+ ->willReturn($objectId);
+
+ $this->assertEquals($expected, $this->invokePrivate($notification, 'isValidCommon'));
+ }
+}