NotificationsTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Comments\Tests\Unit\Controller;
  8. use OCA\Comments\Controller\NotificationsController;
  9. use OCP\AppFramework\Http\NotFoundResponse;
  10. use OCP\AppFramework\Http\RedirectResponse;
  11. use OCP\Comments\IComment;
  12. use OCP\Comments\ICommentsManager;
  13. use OCP\Comments\NotFoundException;
  14. use OCP\Files\Folder;
  15. use OCP\Files\IRootFolder;
  16. use OCP\Files\Node;
  17. use OCP\IRequest;
  18. use OCP\IURLGenerator;
  19. use OCP\IUser;
  20. use OCP\IUserSession;
  21. use OCP\Notification\IManager;
  22. use OCP\Notification\INotification;
  23. use Test\TestCase;
  24. class NotificationsTest extends TestCase {
  25. /** @var NotificationsController */
  26. protected $notificationsController;
  27. /** @var ICommentsManager|\PHPUnit\Framework\MockObject\MockObject */
  28. protected $commentsManager;
  29. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  30. protected $rootFolder;
  31. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  32. protected $session;
  33. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  34. protected $notificationManager;
  35. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  36. protected $urlGenerator;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->commentsManager = $this->createMock(ICommentsManager::class);
  40. $this->rootFolder = $this->createMock(IRootFolder::class);
  41. $this->session = $this->createMock(IUserSession::class);
  42. $this->notificationManager = $this->createMock(IManager::class);
  43. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  44. $this->notificationsController = new NotificationsController(
  45. 'comments',
  46. $this->createMock(IRequest::class),
  47. $this->commentsManager,
  48. $this->rootFolder,
  49. $this->urlGenerator,
  50. $this->notificationManager,
  51. $this->session
  52. );
  53. }
  54. public function testViewGuestRedirect() {
  55. $this->commentsManager->expects($this->never())
  56. ->method('get');
  57. $this->rootFolder->expects($this->never())
  58. ->method('getUserFolder');
  59. $this->session->expects($this->once())
  60. ->method('getUser')
  61. ->willReturn(null);
  62. $this->notificationManager->expects($this->never())
  63. ->method('createNotification');
  64. $this->notificationManager->expects($this->never())
  65. ->method('markProcessed');
  66. $this->urlGenerator->expects($this->exactly(2))
  67. ->method('linkToRoute')
  68. ->withConsecutive(
  69. ['comments.Notifications.view', ['id' => '42']],
  70. ['core.login.showLoginForm', ['redirect_url' => 'link-to-comment']]
  71. )
  72. ->willReturnMap([
  73. ['comments.Notifications.view', ['id' => '42'], 'link-to-comment'],
  74. ['core.login.showLoginForm', ['redirect_url' => 'link-to-comment'], 'link-to-login'],
  75. ]);
  76. /** @var RedirectResponse $response */
  77. $response = $this->notificationsController->view('42');
  78. $this->assertInstanceOf(RedirectResponse::class, $response);
  79. $this->assertSame('link-to-login', $response->getRedirectURL());
  80. }
  81. public function testViewSuccess() {
  82. $comment = $this->createMock(IComment::class);
  83. $comment->expects($this->any())
  84. ->method('getObjectType')
  85. ->willReturn('files');
  86. $comment->expects($this->any())
  87. ->method('getId')
  88. ->willReturn('1234');
  89. $this->commentsManager->expects($this->any())
  90. ->method('get')
  91. ->with('42')
  92. ->willReturn($comment);
  93. $file = $this->createMock(Node::class);
  94. $folder = $this->createMock(Folder::class);
  95. $user = $this->createMock(IUser::class);
  96. $this->rootFolder->expects($this->once())
  97. ->method('getUserFolder')
  98. ->willReturn($folder);
  99. $folder->expects($this->once())
  100. ->method('getById')
  101. ->willReturn([$file]);
  102. $this->session->expects($this->once())
  103. ->method('getUser')
  104. ->willReturn($user);
  105. $user->expects($this->any())
  106. ->method('getUID')
  107. ->willReturn('user');
  108. $notification = $this->createMock(INotification::class);
  109. $notification->expects($this->any())
  110. ->method($this->anything())
  111. ->willReturn($notification);
  112. $this->notificationManager->expects($this->once())
  113. ->method('createNotification')
  114. ->willReturn($notification);
  115. $this->notificationManager->expects($this->once())
  116. ->method('markProcessed')
  117. ->with($notification);
  118. $response = $this->notificationsController->view('42');
  119. $this->assertInstanceOf(RedirectResponse::class, $response);
  120. }
  121. public function testViewInvalidComment() {
  122. $this->commentsManager->expects($this->any())
  123. ->method('get')
  124. ->with('42')
  125. ->will($this->throwException(new NotFoundException()));
  126. $this->rootFolder->expects($this->never())
  127. ->method('getUserFolder');
  128. $user = $this->createMock(IUser::class);
  129. $this->session->expects($this->once())
  130. ->method('getUser')
  131. ->willReturn($user);
  132. $user->expects($this->any())
  133. ->method('getUID')
  134. ->willReturn('user');
  135. $this->notificationManager->expects($this->never())
  136. ->method('createNotification');
  137. $this->notificationManager->expects($this->never())
  138. ->method('markProcessed');
  139. $response = $this->notificationsController->view('42');
  140. $this->assertInstanceOf(NotFoundResponse::class, $response);
  141. }
  142. public function testViewNoFile() {
  143. $comment = $this->createMock(IComment::class);
  144. $comment->expects($this->any())
  145. ->method('getObjectType')
  146. ->willReturn('files');
  147. $comment->expects($this->any())
  148. ->method('getId')
  149. ->willReturn('1234');
  150. $this->commentsManager->expects($this->any())
  151. ->method('get')
  152. ->with('42')
  153. ->willReturn($comment);
  154. $folder = $this->createMock(Folder::class);
  155. $this->rootFolder->expects($this->once())
  156. ->method('getUserFolder')
  157. ->willReturn($folder);
  158. $folder->expects($this->once())
  159. ->method('getById')
  160. ->willReturn([]);
  161. $user = $this->createMock(IUser::class);
  162. $this->session->expects($this->once())
  163. ->method('getUser')
  164. ->willReturn($user);
  165. $user->expects($this->any())
  166. ->method('getUID')
  167. ->willReturn('user');
  168. $notification = $this->createMock(INotification::class);
  169. $notification->expects($this->any())
  170. ->method($this->anything())
  171. ->willReturn($notification);
  172. $this->notificationManager->expects($this->once())
  173. ->method('createNotification')
  174. ->willReturn($notification);
  175. $this->notificationManager->expects($this->once())
  176. ->method('markProcessed')
  177. ->with($notification);
  178. $response = $this->notificationsController->view('42');
  179. $this->assertInstanceOf(NotFoundResponse::class, $response);
  180. }
  181. }