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>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @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\Controller;
|
|
|
|
|
|
|
|
use OCA\Comments\Controller\Notifications;
|
2018-09-12 12:23:38 +03:00
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\Comments\IComment;
|
|
|
|
use OCP\Comments\ICommentsManager;
|
2016-05-09 11:02:07 +03:00
|
|
|
use OCP\Comments\NotFoundException;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\Files\Folder;
|
2018-09-12 12:23:38 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\Files\Node;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\Notification\IManager;
|
|
|
|
use OCP\Notification\INotification;
|
2016-05-09 11:02:07 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class NotificationsTest extends TestCase {
|
2018-09-12 12:23:38 +03:00
|
|
|
/** @var Notifications */
|
2016-05-09 11:02:07 +03:00
|
|
|
protected $notificationsController;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICommentsManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-05-09 11:02:07 +03:00
|
|
|
protected $commentsManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
|
2018-09-12 12:23:38 +03:00
|
|
|
protected $rootFolder;
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
2016-05-09 11:02:07 +03:00
|
|
|
protected $session;
|
|
|
|
|
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 IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
|
2018-09-12 12:23:38 +03:00
|
|
|
protected $urlGenerator;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-05-09 11:02:07 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2018-09-12 12:23:38 +03:00
|
|
|
$this->commentsManager = $this->createMock(ICommentsManager::class);
|
|
|
|
$this->rootFolder = $this->createMock(IRootFolder::class);
|
|
|
|
$this->session = $this->createMock(IUserSession::class);
|
|
|
|
$this->notificationManager = $this->createMock(IManager::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->notificationsController = new Notifications(
|
|
|
|
'comments',
|
2018-09-12 12:23:38 +03:00
|
|
|
$this->createMock(IRequest::class),
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->commentsManager,
|
2018-09-12 12:23:38 +03:00
|
|
|
$this->rootFolder,
|
|
|
|
$this->urlGenerator,
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->notificationManager,
|
|
|
|
$this->session
|
|
|
|
);
|
|
|
|
}
|
2018-09-12 12:23:38 +03:00
|
|
|
|
|
|
|
public function testViewGuestRedirect() {
|
|
|
|
$this->commentsManager->expects($this->never())
|
|
|
|
->method('get');
|
|
|
|
|
|
|
|
$this->rootFolder->expects($this->never())
|
|
|
|
->method('getUserFolder');
|
|
|
|
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn(null);
|
|
|
|
|
|
|
|
$this->notificationManager->expects($this->never())
|
|
|
|
->method('createNotification');
|
|
|
|
$this->notificationManager->expects($this->never())
|
|
|
|
->method('markProcessed');
|
|
|
|
|
|
|
|
$this->urlGenerator->expects($this->exactly(2))
|
|
|
|
->method('linkToRoute')
|
|
|
|
->withConsecutive(
|
|
|
|
['comments.Notifications.view', ['id' => '42']],
|
|
|
|
['core.login.showLoginForm', ['redirect_url' => 'link-to-comment']]
|
|
|
|
)
|
|
|
|
->willReturnMap([
|
|
|
|
['comments.Notifications.view', ['id' => '42'], 'link-to-comment'],
|
|
|
|
['core.login.showLoginForm', ['redirect_url' => 'link-to-comment'], 'link-to-login'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var RedirectResponse $response */
|
|
|
|
$response = $this->notificationsController->view('42');
|
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response);
|
|
|
|
$this->assertSame('link-to-login', $response->getRedirectURL());
|
|
|
|
}
|
|
|
|
|
2016-05-09 11:02:07 +03:00
|
|
|
public function testViewSuccess() {
|
2018-09-12 12:23:38 +03:00
|
|
|
$comment = $this->createMock(IComment::class);
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->any())
|
|
|
|
->method('getObjectType')
|
2018-09-12 12:23:38 +03:00
|
|
|
->willReturn('files');
|
2019-07-16 14:32:44 +03:00
|
|
|
$comment->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('1234');
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->commentsManager->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->with('42')
|
2018-09-12 12:23:38 +03:00
|
|
|
->willReturn($comment);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2018-09-12 12:23:38 +03:00
|
|
|
$file = $this->createMock(Node::class);
|
|
|
|
$folder = $this->createMock(Folder::class);
|
2019-06-12 15:40:42 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2018-09-12 12:23:38 +03:00
|
|
|
$this->rootFolder->expects($this->once())
|
|
|
|
->method('getUserFolder')
|
|
|
|
->willReturn($folder);
|
|
|
|
|
|
|
|
$folder->expects($this->once())
|
2016-05-09 11:02:07 +03:00
|
|
|
->method('getById')
|
2018-09-12 12:23:38 +03:00
|
|
|
->willReturn([$file]);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('getUser')
|
2019-06-12 15:40:42 +03:00
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('user');
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2018-09-12 12:23:38 +03:00
|
|
|
$notification = $this->createMock(INotification::class);
|
2016-05-09 11:02:07 +03:00
|
|
|
$notification->expects($this->any())
|
|
|
|
->method($this->anything())
|
2018-09-12 12:23:38 +03:00
|
|
|
->willReturn($notification);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->notificationManager->expects($this->once())
|
|
|
|
->method('createNotification')
|
2018-09-12 12:23:38 +03:00
|
|
|
->willReturn($notification);
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->notificationManager->expects($this->once())
|
|
|
|
->method('markProcessed')
|
|
|
|
->with($notification);
|
|
|
|
|
|
|
|
$response = $this->notificationsController->view('42');
|
2018-09-12 12:23:38 +03:00
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response);
|
2016-05-09 11:02:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testViewInvalidComment() {
|
|
|
|
$this->commentsManager->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->with('42')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
|
|
|
|
2018-09-12 12:23:38 +03:00
|
|
|
$this->rootFolder->expects($this->never())
|
|
|
|
->method('getUserFolder');
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2019-06-12 15:40:42 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
2018-09-12 12:23:38 +03:00
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('getUser')
|
2019-06-12 15:40:42 +03:00
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('user');
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->notificationManager->expects($this->never())
|
|
|
|
->method('createNotification');
|
|
|
|
$this->notificationManager->expects($this->never())
|
|
|
|
->method('markProcessed');
|
|
|
|
|
|
|
|
$response = $this->notificationsController->view('42');
|
2018-09-12 12:23:38 +03:00
|
|
|
$this->assertInstanceOf(NotFoundResponse::class, $response);
|
2016-05-09 11:02:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testViewNoFile() {
|
2018-09-12 12:23:38 +03:00
|
|
|
$comment = $this->createMock(IComment::class);
|
2016-05-09 11:02:07 +03:00
|
|
|
$comment->expects($this->any())
|
|
|
|
->method('getObjectType')
|
2018-09-12 12:23:38 +03:00
|
|
|
->willReturn('files');
|
2019-07-16 14:32:44 +03:00
|
|
|
$comment->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('1234');
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->commentsManager->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->with('42')
|
2018-09-12 12:23:38 +03:00
|
|
|
->willReturn($comment);
|
|
|
|
|
|
|
|
$folder = $this->createMock(Folder::class);
|
|
|
|
|
|
|
|
$this->rootFolder->expects($this->once())
|
|
|
|
->method('getUserFolder')
|
|
|
|
->willReturn($folder);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2018-09-12 12:23:38 +03:00
|
|
|
$folder->expects($this->once())
|
2016-05-09 11:02:07 +03:00
|
|
|
->method('getById')
|
2018-09-12 12:23:38 +03:00
|
|
|
->willReturn([]);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2019-06-12 15:40:42 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('getUser')
|
2019-06-12 15:40:42 +03:00
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('user');
|
2016-05-09 11:02:07 +03:00
|
|
|
|
2018-09-12 12:23:38 +03:00
|
|
|
$notification = $this->createMock(INotification::class);
|
2016-05-09 11:02:07 +03:00
|
|
|
$notification->expects($this->any())
|
|
|
|
->method($this->anything())
|
2018-09-12 12:23:38 +03:00
|
|
|
->willReturn($notification);
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
$this->notificationManager->expects($this->once())
|
|
|
|
->method('createNotification')
|
2018-09-12 12:23:38 +03:00
|
|
|
->willReturn($notification);
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->notificationManager->expects($this->once())
|
|
|
|
->method('markProcessed')
|
|
|
|
->with($notification);
|
|
|
|
|
|
|
|
$response = $this->notificationsController->view('42');
|
2018-09-12 12:23:38 +03:00
|
|
|
$this->assertInstanceOf(NotFoundResponse::class, $response);
|
2016-05-09 11:02:07 +03:00
|
|
|
}
|
|
|
|
}
|