2016-05-09 11:02:07 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2017-11-06 17:56:42 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-09 11:02:07 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-05-09 11:02:07 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-05-09 11:02:07 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Comments\Tests\Unit\Notification;
|
|
|
|
|
|
|
|
use OCA\Comments\Notification\Listener;
|
|
|
|
use OCP\Comments\CommentsEvent;
|
|
|
|
use OCP\Comments\IComment;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\Notification\IManager;
|
|
|
|
use OCP\Notification\INotification;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class ListenerTest extends TestCase {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-05-09 11:02:07 +03:00
|
|
|
protected $notificationManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-05-09 11:02:07 +03:00
|
|
|
protected $userManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
|
2016-05-09 11:02:07 +03:00
|
|
|
protected $urlGenerator;
|
|
|
|
|
|
|
|
/** @var Listener */
|
|
|
|
protected $listener;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-05-09 11:02:07 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-01-09 14:03:34 +03:00
|
|
|
$this->notificationManager = $this->createMock(\OCP\Notification\IManager::class);
|
|
|
|
$this->userManager = $this->createMock(\OCP\IUserManager::class);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->listener = new Listener(
|
|
|
|
$this->notificationManager,
|
2017-01-09 14:03:34 +03:00
|
|
|
$this->userManager
|
2016-05-09 11:02:07 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function eventProvider() {
|
|
|
|
return [
|
|
|
|
[CommentsEvent::EVENT_ADD, 'notify'],
|
2016-10-12 19:06:22 +03:00
|
|
|
[CommentsEvent::EVENT_UPDATE, 'notify'],
|
|
|
|
[CommentsEvent::EVENT_PRE_UPDATE, 'markProcessed'],
|
2016-05-09 11:02:07 +03:00
|
|
|
[CommentsEvent::EVENT_DELETE, 'markProcessed']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider eventProvider
|
|
|
|
* @param string $eventType
|
|
|
|
* @param string $notificationMethod
|
|
|
|
*/
|
|
|
|
public function testEvaluate($eventType, $notificationMethod) {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */
|
2017-10-24 16:26:53 +03:00
|
|
|
$comment = $this->getMockBuilder(IComment::class)->getMock();
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->any())
|
|
|
|
->method('getObjectType')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('files');
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->any())
|
|
|
|
->method('getCreationDateTime')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(new \DateTime());
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->once())
|
2016-10-14 01:19:31 +03:00
|
|
|
->method('getMentions')
|
|
|
|
->willReturn([
|
|
|
|
[ 'type' => 'user', 'id' => 'foobar'],
|
|
|
|
[ 'type' => 'user', 'id' => 'barfoo'],
|
|
|
|
[ 'type' => 'user', 'id' => 'foo@bar.com'],
|
|
|
|
[ 'type' => 'user', 'id' => 'bar@foo.org@foobar.io'],
|
|
|
|
[ 'type' => 'user', 'id' => '23452-4333-54353-2342'],
|
|
|
|
[ 'type' => 'user', 'id' => 'yolo'],
|
|
|
|
]);
|
2019-07-16 14:32:44 +03:00
|
|
|
$comment->expects($this->atLeastOnce())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('1234');
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */
|
2017-10-24 16:26:53 +03:00
|
|
|
$event = $this->getMockBuilder(CommentsEvent::class)
|
2016-05-09 11:02:07 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('getComment')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($comment);
|
2016-05-09 11:02:07 +03:00
|
|
|
$event->expects(($this->any()))
|
|
|
|
->method(('getEvent'))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($eventType);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
|
2017-10-24 16:26:53 +03:00
|
|
|
$notification = $this->getMockBuilder(INotification::class)->getMock();
|
2016-05-09 11:02:07 +03:00
|
|
|
$notification->expects($this->any())
|
|
|
|
->method($this->anything())
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($notification);
|
2016-05-09 11:02:07 +03:00
|
|
|
$notification->expects($this->exactly(6))
|
|
|
|
->method('setUser');
|
|
|
|
|
|
|
|
$this->notificationManager->expects($this->once())
|
|
|
|
->method('createNotification')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($notification);
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->notificationManager->expects($this->exactly(6))
|
|
|
|
->method($notificationMethod)
|
|
|
|
->with($this->isInstanceOf('\OCP\Notification\INotification'));
|
|
|
|
|
|
|
|
$this->userManager->expects($this->exactly(6))
|
|
|
|
->method('userExists')
|
|
|
|
->withConsecutive(
|
|
|
|
['foobar'],
|
|
|
|
['barfoo'],
|
|
|
|
['foo@bar.com'],
|
|
|
|
['bar@foo.org@foobar.io'],
|
|
|
|
['23452-4333-54353-2342'],
|
|
|
|
['yolo']
|
|
|
|
)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->listener->evaluate($event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider eventProvider
|
|
|
|
* @param string $eventType
|
|
|
|
*/
|
|
|
|
public function testEvaluateNoMentions($eventType) {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */
|
2017-10-24 16:26:53 +03:00
|
|
|
$comment = $this->getMockBuilder(IComment::class)->getMock();
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->any())
|
|
|
|
->method('getObjectType')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('files');
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->any())
|
|
|
|
->method('getCreationDateTime')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(new \DateTime());
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->once())
|
2016-10-14 01:19:31 +03:00
|
|
|
->method('getMentions')
|
|
|
|
->willReturn([]);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */
|
2017-10-24 16:26:53 +03:00
|
|
|
$event = $this->getMockBuilder(CommentsEvent::class)
|
2016-05-09 11:02:07 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('getComment')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($comment);
|
2016-05-09 11:02:07 +03:00
|
|
|
$event->expects(($this->any()))
|
|
|
|
->method(('getEvent'))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($eventType);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->notificationManager->expects($this->never())
|
|
|
|
->method('createNotification');
|
|
|
|
$this->notificationManager->expects($this->never())
|
|
|
|
->method('notify');
|
|
|
|
$this->notificationManager->expects($this->never())
|
|
|
|
->method('markProcessed');
|
|
|
|
|
|
|
|
$this->userManager->expects($this->never())
|
|
|
|
->method('userExists');
|
|
|
|
|
|
|
|
$this->listener->evaluate($event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEvaluateUserDoesNotExist() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */
|
2017-10-24 16:26:53 +03:00
|
|
|
$comment = $this->getMockBuilder(IComment::class)->getMock();
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->any())
|
|
|
|
->method('getObjectType')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('files');
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->any())
|
|
|
|
->method('getCreationDateTime')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(new \DateTime());
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->once())
|
2016-10-14 01:19:31 +03:00
|
|
|
->method('getMentions')
|
|
|
|
->willReturn([[ 'type' => 'user', 'id' => 'foobar']]);
|
2019-07-16 14:32:44 +03:00
|
|
|
$comment->expects($this->atLeastOnce())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('1234');
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */
|
2017-10-24 16:26:53 +03:00
|
|
|
$event = $this->getMockBuilder(CommentsEvent::class)
|
2016-05-09 11:02:07 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('getComment')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($comment);
|
2016-05-09 11:02:07 +03:00
|
|
|
$event->expects(($this->any()))
|
|
|
|
->method(('getEvent'))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(CommentsEvent::EVENT_ADD);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
|
2017-10-24 16:26:53 +03:00
|
|
|
$notification = $this->getMockBuilder(INotification::class)->getMock();
|
2016-05-09 11:02:07 +03:00
|
|
|
$notification->expects($this->any())
|
|
|
|
->method($this->anything())
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($notification);
|
2016-05-09 11:02:07 +03:00
|
|
|
$notification->expects($this->never())
|
|
|
|
->method('setUser');
|
|
|
|
|
|
|
|
$this->notificationManager->expects($this->once())
|
|
|
|
->method('createNotification')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($notification);
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->notificationManager->expects($this->never())
|
|
|
|
->method('notify');
|
|
|
|
|
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('userExists')
|
|
|
|
->withConsecutive(
|
|
|
|
['foobar']
|
|
|
|
)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->listener->evaluate($event);
|
|
|
|
}
|
|
|
|
}
|