123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Joas Schilling <coding@schilljs.com>
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @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 OCA\Comments\Tests\Unit\Controller;
- use OCA\Comments\Controller\NotificationsController;
- use OCP\AppFramework\Http\NotFoundResponse;
- use OCP\AppFramework\Http\RedirectResponse;
- use OCP\Comments\IComment;
- use OCP\Comments\ICommentsManager;
- use OCP\Comments\NotFoundException;
- use OCP\Files\Folder;
- use OCP\Files\IRootFolder;
- use OCP\Files\Node;
- use OCP\IRequest;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\IUserSession;
- use OCP\Notification\IManager;
- use OCP\Notification\INotification;
- use Test\TestCase;
- class NotificationsTest extends TestCase {
- /** @var NotificationsController */
- protected $notificationsController;
- /** @var ICommentsManager|\PHPUnit\Framework\MockObject\MockObject */
- protected $commentsManager;
- /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
- protected $rootFolder;
- /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
- protected $session;
- /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
- protected $notificationManager;
- /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
- protected $urlGenerator;
- protected function setUp(): void {
- parent::setUp();
- $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);
- $this->notificationsController = new NotificationsController(
- 'comments',
- $this->createMock(IRequest::class),
- $this->commentsManager,
- $this->rootFolder,
- $this->urlGenerator,
- $this->notificationManager,
- $this->session
- );
- }
- 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());
- }
- public function testViewSuccess() {
- $comment = $this->createMock(IComment::class);
- $comment->expects($this->any())
- ->method('getObjectType')
- ->willReturn('files');
- $comment->expects($this->any())
- ->method('getId')
- ->willReturn('1234');
- $this->commentsManager->expects($this->any())
- ->method('get')
- ->with('42')
- ->willReturn($comment);
- $file = $this->createMock(Node::class);
- $folder = $this->createMock(Folder::class);
- $user = $this->createMock(IUser::class);
- $this->rootFolder->expects($this->once())
- ->method('getUserFolder')
- ->willReturn($folder);
- $folder->expects($this->once())
- ->method('getById')
- ->willReturn([$file]);
- $this->session->expects($this->once())
- ->method('getUser')
- ->willReturn($user);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user');
- $notification = $this->createMock(INotification::class);
- $notification->expects($this->any())
- ->method($this->anything())
- ->willReturn($notification);
- $this->notificationManager->expects($this->once())
- ->method('createNotification')
- ->willReturn($notification);
- $this->notificationManager->expects($this->once())
- ->method('markProcessed')
- ->with($notification);
- $response = $this->notificationsController->view('42');
- $this->assertInstanceOf(RedirectResponse::class, $response);
- }
- public function testViewInvalidComment() {
- $this->commentsManager->expects($this->any())
- ->method('get')
- ->with('42')
- ->will($this->throwException(new NotFoundException()));
- $this->rootFolder->expects($this->never())
- ->method('getUserFolder');
- $user = $this->createMock(IUser::class);
- $this->session->expects($this->once())
- ->method('getUser')
- ->willReturn($user);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user');
- $this->notificationManager->expects($this->never())
- ->method('createNotification');
- $this->notificationManager->expects($this->never())
- ->method('markProcessed');
- $response = $this->notificationsController->view('42');
- $this->assertInstanceOf(NotFoundResponse::class, $response);
- }
- public function testViewNoFile() {
- $comment = $this->createMock(IComment::class);
- $comment->expects($this->any())
- ->method('getObjectType')
- ->willReturn('files');
- $comment->expects($this->any())
- ->method('getId')
- ->willReturn('1234');
- $this->commentsManager->expects($this->any())
- ->method('get')
- ->with('42')
- ->willReturn($comment);
- $folder = $this->createMock(Folder::class);
- $this->rootFolder->expects($this->once())
- ->method('getUserFolder')
- ->willReturn($folder);
- $folder->expects($this->once())
- ->method('getById')
- ->willReturn([]);
- $user = $this->createMock(IUser::class);
- $this->session->expects($this->once())
- ->method('getUser')
- ->willReturn($user);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user');
- $notification = $this->createMock(INotification::class);
- $notification->expects($this->any())
- ->method($this->anything())
- ->willReturn($notification);
- $this->notificationManager->expects($this->once())
- ->method('createNotification')
- ->willReturn($notification);
- $this->notificationManager->expects($this->once())
- ->method('markProcessed')
- ->with($notification);
- $response = $this->notificationsController->view('42');
- $this->assertInstanceOf(NotFoundResponse::class, $response);
- }
- }
|