2015-09-03 15:47:49 +03:00
|
|
|
<?php
|
2020-04-09 12:50:14 +03:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
declare(strict_types = 1);
|
2015-09-03 15:47:49 +03:00
|
|
|
/**
|
|
|
|
* @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;
|
2016-10-14 17:55:20 +03:00
|
|
|
use OCP\Notification\IAction;
|
2016-01-14 16:35:24 +03:00
|
|
|
use OCP\Notification\INotification;
|
2016-10-14 17:55:20 +03:00
|
|
|
use OCP\RichObjectStrings\IValidator;
|
2015-09-03 15:48:27 +03:00
|
|
|
use Test\TestCase;
|
2015-09-03 15:47:49 +03:00
|
|
|
|
2015-09-03 15:48:27 +03:00
|
|
|
class NotificationTest extends TestCase {
|
2015-09-03 15:47:49 +03:00
|
|
|
/** @var INotification */
|
|
|
|
protected $notification;
|
2016-10-14 17:55:20 +03:00
|
|
|
/** @var IValidator|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $validator;
|
2015-09-03 15:47:49 +03:00
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-09-03 15:47:49 +03:00
|
|
|
parent::setUp();
|
2016-10-14 17:55:20 +03:00
|
|
|
$this->validator = $this->createMock(IValidator::class);
|
|
|
|
$this->notification = new Notification($this->validator);
|
2015-09-03 15:47:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function dataValidString($maxLength) {
|
|
|
|
$dataSets = [
|
|
|
|
['test1'],
|
2019-06-12 15:40:42 +03:00
|
|
|
['1564'],
|
2015-09-03 15:47:49 +03:00
|
|
|
[str_repeat('a', 1)],
|
|
|
|
];
|
|
|
|
if ($maxLength !== false) {
|
|
|
|
$dataSets[] = [str_repeat('a', $maxLength)];
|
|
|
|
}
|
|
|
|
return $dataSets;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function dataInvalidString($maxLength) {
|
|
|
|
$dataSets = [
|
2019-06-12 15:40:42 +03:00
|
|
|
['']
|
2015-09-03 15:47:49 +03:00
|
|
|
];
|
|
|
|
if ($maxLength !== false) {
|
|
|
|
$dataSets[] = [str_repeat('a', $maxLength + 1)];
|
|
|
|
}
|
|
|
|
return $dataSets;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetApp() {
|
|
|
|
return $this->dataValidString(32);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetApp
|
|
|
|
* @param string $app
|
|
|
|
*/
|
|
|
|
public function testSetApp($app) {
|
|
|
|
$this->assertSame('', $this->notification->getApp());
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->setApp($app));
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->assertSame($app, $this->notification->getApp());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetAppInvalid() {
|
|
|
|
return $this->dataInvalidString(32);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetAppInvalid
|
|
|
|
* @param mixed $app
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testSetAppInvalid($app) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->notification->setApp($app);
|
|
|
|
}
|
|
|
|
|
2019-06-12 15:40:42 +03:00
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
public function dataSetUser() {
|
|
|
|
return $this->dataValidString(64);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetUser
|
|
|
|
* @param string $user
|
|
|
|
*/
|
|
|
|
public function testSetUser($user) {
|
|
|
|
$this->assertSame('', $this->notification->getUser());
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->setUser($user));
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->assertSame($user, $this->notification->getUser());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetUserInvalid() {
|
|
|
|
return $this->dataInvalidString(64);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetUserInvalid
|
|
|
|
* @param mixed $user
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testSetUserInvalid($user) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->notification->setUser($user);
|
|
|
|
}
|
2019-06-12 15:40:42 +03:00
|
|
|
|
2015-11-18 18:27:48 +03:00
|
|
|
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],
|
|
|
|
];
|
2015-09-03 15:47:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-18 18:27:48 +03:00
|
|
|
* @dataProvider dataSetDateTime
|
|
|
|
* @param \DateTime $dateTime
|
2015-09-03 15:47:49 +03:00
|
|
|
*/
|
2015-11-18 18:27:48 +03:00
|
|
|
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());
|
2015-09-03 15:47:49 +03:00
|
|
|
}
|
|
|
|
|
2015-11-18 18:27:48 +03:00
|
|
|
public function dataSetDateTimeZero() {
|
|
|
|
$nineTeenSeventy = new \DateTime();
|
|
|
|
$nineTeenSeventy->setTimestamp(0);
|
|
|
|
return [
|
|
|
|
[$nineTeenSeventy],
|
|
|
|
];
|
2015-09-03 15:47:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-18 18:27:48 +03:00
|
|
|
* @dataProvider dataSetDateTimeZero
|
|
|
|
* @param \DateTime $dateTime
|
2015-09-03 15:47:49 +03:00
|
|
|
*
|
2015-11-18 18:27:48 +03:00
|
|
|
* @expectedMessage 'The given date time is invalid'
|
2015-09-03 15:47:49 +03:00
|
|
|
*/
|
2015-11-18 18:27:48 +03:00
|
|
|
public function testSetDateTimeZero($dateTime) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-11-18 18:27:48 +03:00
|
|
|
$this->notification->setDateTime($dateTime);
|
2015-09-03 15:47:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetObject() {
|
|
|
|
return [
|
2019-07-16 12:36:32 +03:00
|
|
|
['a', '21'],
|
|
|
|
[str_repeat('a', 64), '42'],
|
2015-09-03 15:47:49 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetObject
|
|
|
|
* @param string $type
|
2019-07-16 12:36:32 +03:00
|
|
|
* @param string $id
|
2015-09-03 15:47:49 +03:00
|
|
|
*/
|
2019-07-16 12:36:32 +03:00
|
|
|
public function testSetObject($type, $id) {
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->assertSame('', $this->notification->getObjectType());
|
2015-11-23 16:06:26 +03:00
|
|
|
$this->assertSame('', $this->notification->getObjectId());
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->setObject($type, $id));
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->assertSame($type, $this->notification->getObjectType());
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->assertSame($id, $this->notification->getObjectId());
|
2015-09-03 15:47:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetObjectTypeInvalid() {
|
|
|
|
return $this->dataInvalidString(64);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetObjectIdInvalid() {
|
2015-11-23 16:06:26 +03:00
|
|
|
return [
|
|
|
|
[''],
|
|
|
|
[str_repeat('a', 64 + 1)],
|
|
|
|
];
|
2015-09-03 15:47:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetObjectIdInvalid
|
|
|
|
* @param mixed $id
|
|
|
|
*
|
|
|
|
* @expectedMessage 'The given object id is invalid'
|
|
|
|
*/
|
|
|
|
public function testSetObjectIdInvalid($id) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
$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());
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->setSubject($subject, $parameters));
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->assertSame($subject, $this->notification->getSubject());
|
|
|
|
$this->assertSame($parameters, $this->notification->getSubjectParameters());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetSubjectInvalidSubject() {
|
|
|
|
return $this->dataInvalidString(64);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetSubjectInvalidSubject
|
|
|
|
* @param mixed $subject
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testSetSubjectInvalidSubject($subject) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
$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());
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->setParsedSubject($subject));
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->assertSame($subject, $this->notification->getParsedSubject());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetParsedSubjectInvalid() {
|
|
|
|
return $this->dataInvalidString(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetParsedSubjectInvalid
|
|
|
|
* @param mixed $subject
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testSetParsedSubjectInvalid($subject) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
$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());
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->setMessage($message, $parameters));
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->assertSame($message, $this->notification->getMessage());
|
|
|
|
$this->assertSame($parameters, $this->notification->getMessageParameters());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetMessageInvalidMessage() {
|
|
|
|
return $this->dataInvalidString(64);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetMessageInvalidMessage
|
|
|
|
* @param mixed $message
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testSetMessageInvalidMessage($message) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
$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());
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->setParsedMessage($message));
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->assertSame($message, $this->notification->getParsedMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetParsedMessageInvalid() {
|
|
|
|
return $this->dataInvalidString(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetParsedMessageInvalid
|
|
|
|
* @param mixed $message
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testSetParsedMessageInvalid($message) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
$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());
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->setLink($link));
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->assertSame($link, $this->notification->getLink());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetLinkInvalid() {
|
|
|
|
return $this->dataInvalidString(4000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetLinkInvalid
|
|
|
|
* @param mixed $link
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testSetLinkInvalid($link) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
$this->notification->setLink($link);
|
|
|
|
}
|
|
|
|
|
2016-10-07 18:00:24 +03:00
|
|
|
public function dataSetIcon() {
|
|
|
|
return $this->dataValidString(4000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetIcon
|
|
|
|
* @param string $icon
|
|
|
|
*/
|
|
|
|
public function testSetIcon($icon) {
|
|
|
|
$this->assertSame('', $this->notification->getIcon());
|
|
|
|
$this->assertSame($this->notification, $this->notification->setIcon($icon));
|
|
|
|
$this->assertSame($icon, $this->notification->getIcon());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSetIconInvalid() {
|
|
|
|
return $this->dataInvalidString(4000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSetIconInvalid
|
|
|
|
* @param mixed $icon
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testSetIconInvalid($icon) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2016-10-07 18:00:24 +03:00
|
|
|
$this->notification->setIcon($icon);
|
|
|
|
}
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
public function testCreateAction() {
|
|
|
|
$action = $this->notification->createAction();
|
2016-10-14 17:55:20 +03:00
|
|
|
$this->assertInstanceOf(IAction::class, $action);
|
2015-09-03 15:47:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddAction() {
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
|
2016-10-14 17:55:20 +03:00
|
|
|
$action = $this->createMock(IAction::class);
|
2015-09-03 15:47:49 +03:00
|
|
|
$action->expects($this->once())
|
|
|
|
->method('isValid')
|
|
|
|
->willReturn(true);
|
|
|
|
$action->expects($this->never())
|
|
|
|
->method('isValidParsed');
|
|
|
|
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->addAction($action));
|
2015-09-03 15:47:49 +03:00
|
|
|
|
|
|
|
$this->assertEquals([$action], $this->notification->getActions());
|
|
|
|
$this->assertEquals([], $this->notification->getParsedActions());
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
public function testAddActionInvalid() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
|
2016-10-14 17:55:20 +03:00
|
|
|
$action = $this->createMock(IAction::class);
|
2015-09-03 15:47:49 +03:00
|
|
|
$action->expects($this->once())
|
|
|
|
->method('isValid')
|
|
|
|
->willReturn(false);
|
|
|
|
$action->expects($this->never())
|
|
|
|
->method('isValidParsed');
|
|
|
|
|
|
|
|
$this->notification->addAction($action);
|
|
|
|
}
|
|
|
|
|
2015-11-16 18:14:52 +03:00
|
|
|
public function testAddActionSecondPrimary() {
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
|
2016-10-14 17:55:20 +03:00
|
|
|
$action = $this->createMock(IAction::class);
|
2015-11-16 18:14:52 +03:00
|
|
|
$action->expects($this->exactly(2))
|
|
|
|
->method('isValid')
|
|
|
|
->willReturn(true);
|
|
|
|
$action->expects($this->exactly(2))
|
|
|
|
->method('isPrimary')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->addAction($action));
|
2015-11-16 18:14:52 +03:00
|
|
|
|
2018-01-24 20:10:16 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2015-11-16 18:14:52 +03:00
|
|
|
$this->notification->addAction($action);
|
|
|
|
}
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
public function testAddParsedAction() {
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
|
2016-10-14 17:55:20 +03:00
|
|
|
$action = $this->createMock(IAction::class);
|
2015-09-03 15:47:49 +03:00
|
|
|
$action->expects($this->once())
|
|
|
|
->method('isValidParsed')
|
|
|
|
->willReturn(true);
|
|
|
|
$action->expects($this->never())
|
|
|
|
->method('isValid');
|
|
|
|
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->addParsedAction($action));
|
2015-09-03 15:47:49 +03:00
|
|
|
|
|
|
|
$this->assertEquals([$action], $this->notification->getParsedActions());
|
|
|
|
$this->assertEquals([], $this->notification->getActions());
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
public function testAddParsedActionInvalid() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
|
2016-10-14 17:55:20 +03:00
|
|
|
$action = $this->createMock(IAction::class);
|
2015-09-03 15:47:49 +03:00
|
|
|
$action->expects($this->once())
|
|
|
|
->method('isValidParsed')
|
|
|
|
->willReturn(false);
|
|
|
|
$action->expects($this->never())
|
|
|
|
->method('isValid');
|
|
|
|
|
|
|
|
$this->notification->addParsedAction($action);
|
|
|
|
}
|
|
|
|
|
2015-11-16 18:14:52 +03:00
|
|
|
public function testAddActionSecondParsedPrimary() {
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
|
2016-10-14 17:55:20 +03:00
|
|
|
$action = $this->createMock(IAction::class);
|
2015-11-16 18:14:52 +03:00
|
|
|
$action->expects($this->exactly(2))
|
|
|
|
->method('isValidParsed')
|
|
|
|
->willReturn(true);
|
|
|
|
$action->expects($this->exactly(2))
|
|
|
|
->method('isPrimary')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2015-11-17 11:26:13 +03:00
|
|
|
$this->assertSame($this->notification, $this->notification->addParsedAction($action));
|
2015-11-16 18:14:52 +03:00
|
|
|
|
2018-01-24 20:10:16 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2015-11-16 18:14:52 +03:00
|
|
|
$this->notification->addParsedAction($action);
|
|
|
|
}
|
|
|
|
|
2016-08-15 12:12:25 +03:00
|
|
|
public function testAddActionParsedPrimaryEnd() {
|
|
|
|
/** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
|
2016-10-14 17:55:20 +03:00
|
|
|
$action1 = $this->createMock(IAction::class);
|
2016-08-15 12:12:25 +03:00
|
|
|
$action1->expects($this->exactly(2))
|
|
|
|
->method('isValidParsed')
|
|
|
|
->willReturn(true);
|
|
|
|
$action1->expects($this->exactly(2))
|
|
|
|
->method('isPrimary')
|
|
|
|
->willReturn(false);
|
|
|
|
/** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
|
2016-10-14 17:55:20 +03:00
|
|
|
$action2 = $this->createMock(IAction::class);
|
2016-08-15 12:12:25 +03:00
|
|
|
$action2->expects($this->once())
|
|
|
|
->method('isValidParsed')
|
|
|
|
->willReturn(true);
|
|
|
|
$action2->expects($this->once())
|
|
|
|
->method('isPrimary')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertSame($this->notification, $this->notification->addParsedAction($action1));
|
|
|
|
$this->assertSame($this->notification, $this->notification->addParsedAction($action2));
|
|
|
|
$this->assertSame($this->notification, $this->notification->addParsedAction($action1));
|
|
|
|
|
|
|
|
$this->assertEquals([$action2, $action1, $action1], $this->notification->getParsedActions());
|
|
|
|
}
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
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) {
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
2016-10-14 17:55:20 +03:00
|
|
|
$notification = $this->getMockBuilder(Notification::class)
|
2015-09-03 15:47:49 +03:00
|
|
|
->setMethods([
|
|
|
|
'isValidCommon',
|
|
|
|
'getSubject',
|
2016-04-08 15:41:30 +03:00
|
|
|
'getParsedSubject',
|
2015-09-03 15:47:49 +03:00
|
|
|
])
|
2016-10-14 17:55:20 +03:00
|
|
|
->setConstructorArgs([$this->validator])
|
2015-09-03 15:47:49 +03:00
|
|
|
->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) {
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
2016-10-14 17:55:20 +03:00
|
|
|
$notification = $this->getMockBuilder(Notification::class)
|
2015-09-03 15:47:49 +03:00
|
|
|
->setMethods([
|
|
|
|
'isValidCommon',
|
|
|
|
'getParsedSubject',
|
2016-04-08 15:41:30 +03:00
|
|
|
'getSubject',
|
2015-09-03 15:47:49 +03:00
|
|
|
])
|
2016-10-14 17:55:20 +03:00
|
|
|
->setConstructorArgs([$this->validator])
|
2015-09-03 15:47:49 +03:00
|
|
|
->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 [
|
2015-11-23 16:06:26 +03:00
|
|
|
['', '', 0, '', '', false],
|
|
|
|
['app', '', 0, '', '', false],
|
|
|
|
['app', 'user', 0, '', '', false],
|
|
|
|
['app', 'user', time(), '', '', false],
|
|
|
|
['app', 'user', time(), 'type', '', false],
|
|
|
|
['app', 'user', time(), 'type', '42', true],
|
2015-09-03 15:47:49 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataIsValidCommon
|
|
|
|
*
|
|
|
|
* @param string $app
|
|
|
|
* @param string $user
|
|
|
|
* @param int $timestamp
|
|
|
|
* @param string $objectType
|
2015-11-23 16:06:26 +03:00
|
|
|
* @param string $objectId
|
2015-09-03 15:47:49 +03:00
|
|
|
* @param bool $expected
|
|
|
|
*/
|
|
|
|
public function testIsValidCommon($app, $user, $timestamp, $objectType, $objectId, $expected) {
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
2016-10-14 17:55:20 +03:00
|
|
|
$notification = $this->getMockBuilder(Notification::class)
|
2015-09-03 15:47:49 +03:00
|
|
|
->setMethods([
|
|
|
|
'getApp',
|
|
|
|
'getUser',
|
2015-11-18 18:27:48 +03:00
|
|
|
'getDateTime',
|
2015-09-03 15:47:49 +03:00
|
|
|
'getObjectType',
|
|
|
|
'getObjectId',
|
|
|
|
])
|
2016-10-14 17:55:20 +03:00
|
|
|
->setConstructorArgs([$this->validator])
|
2015-09-03 15:47:49 +03:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$notification->expects($this->any())
|
|
|
|
->method('getApp')
|
|
|
|
->willReturn($app);
|
|
|
|
|
|
|
|
$notification->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
|
|
|
|
2015-11-18 18:27:48 +03:00
|
|
|
$dateTime = new \DateTime();
|
|
|
|
$dateTime->setTimestamp($timestamp);
|
|
|
|
|
2015-09-03 15:47:49 +03:00
|
|
|
$notification->expects($this->any())
|
2015-11-18 18:27:48 +03:00
|
|
|
->method('getDateTime')
|
|
|
|
->willReturn($dateTime);
|
2015-09-03 15:47:49 +03:00
|
|
|
|
|
|
|
$notification->expects($this->any())
|
|
|
|
->method('getObjectType')
|
|
|
|
->willReturn($objectType);
|
|
|
|
|
|
|
|
$notification->expects($this->any())
|
|
|
|
->method('getObjectId')
|
|
|
|
->willReturn($objectId);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->invokePrivate($notification, 'isValidCommon'));
|
|
|
|
}
|
|
|
|
}
|