You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotificationsTest.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Comments\Tests\Unit\Controller;
  27. use OCA\Comments\Controller\Notifications;
  28. use OCP\AppFramework\Http\NotFoundResponse;
  29. use OCP\AppFramework\Http\RedirectResponse;
  30. use OCP\Comments\IComment;
  31. use OCP\Comments\ICommentsManager;
  32. use OCP\Comments\NotFoundException;
  33. use OCP\Files\Folder;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Files\Node;
  36. use OCP\IRequest;
  37. use OCP\IURLGenerator;
  38. use OCP\IUser;
  39. use OCP\IUserSession;
  40. use OCP\Notification\IManager;
  41. use OCP\Notification\INotification;
  42. use Test\TestCase;
  43. class NotificationsTest extends TestCase {
  44. /** @var Notifications */
  45. protected $notificationsController;
  46. /** @var ICommentsManager|\PHPUnit\Framework\MockObject\MockObject */
  47. protected $commentsManager;
  48. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  49. protected $rootFolder;
  50. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  51. protected $session;
  52. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  53. protected $notificationManager;
  54. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  55. protected $urlGenerator;
  56. protected function setUp(): void {
  57. parent::setUp();
  58. $this->commentsManager = $this->createMock(ICommentsManager::class);
  59. $this->rootFolder = $this->createMock(IRootFolder::class);
  60. $this->session = $this->createMock(IUserSession::class);
  61. $this->notificationManager = $this->createMock(IManager::class);
  62. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  63. $this->notificationsController = new Notifications(
  64. 'comments',
  65. $this->createMock(IRequest::class),
  66. $this->commentsManager,
  67. $this->rootFolder,
  68. $this->urlGenerator,
  69. $this->notificationManager,
  70. $this->session
  71. );
  72. }
  73. public function testViewGuestRedirect() {
  74. $this->commentsManager->expects($this->never())
  75. ->method('get');
  76. $this->rootFolder->expects($this->never())
  77. ->method('getUserFolder');
  78. $this->session->expects($this->once())
  79. ->method('getUser')
  80. ->willReturn(null);
  81. $this->notificationManager->expects($this->never())
  82. ->method('createNotification');
  83. $this->notificationManager->expects($this->never())
  84. ->method('markProcessed');
  85. $this->urlGenerator->expects($this->exactly(2))
  86. ->method('linkToRoute')
  87. ->withConsecutive(
  88. ['comments.Notifications.view', ['id' => '42']],
  89. ['core.login.showLoginForm', ['redirect_url' => 'link-to-comment']]
  90. )
  91. ->willReturnMap([
  92. ['comments.Notifications.view', ['id' => '42'], 'link-to-comment'],
  93. ['core.login.showLoginForm', ['redirect_url' => 'link-to-comment'], 'link-to-login'],
  94. ]);
  95. /** @var RedirectResponse $response */
  96. $response = $this->notificationsController->view('42');
  97. $this->assertInstanceOf(RedirectResponse::class, $response);
  98. $this->assertSame('link-to-login', $response->getRedirectURL());
  99. }
  100. public function testViewSuccess() {
  101. $comment = $this->createMock(IComment::class);
  102. $comment->expects($this->any())
  103. ->method('getObjectType')
  104. ->willReturn('files');
  105. $comment->expects($this->any())
  106. ->method('getId')
  107. ->willReturn('1234');
  108. $this->commentsManager->expects($this->any())
  109. ->method('get')
  110. ->with('42')
  111. ->willReturn($comment);
  112. $file = $this->createMock(Node::class);
  113. $folder = $this->createMock(Folder::class);
  114. $user = $this->createMock(IUser::class);
  115. $this->rootFolder->expects($this->once())
  116. ->method('getUserFolder')
  117. ->willReturn($folder);
  118. $folder->expects($this->once())
  119. ->method('getById')
  120. ->willReturn([$file]);
  121. $this->session->expects($this->once())
  122. ->method('getUser')
  123. ->willReturn($user);
  124. $user->expects($this->any())
  125. ->method('getUID')
  126. ->willReturn('user');
  127. $notification = $this->createMock(INotification::class);
  128. $notification->expects($this->any())
  129. ->method($this->anything())
  130. ->willReturn($notification);
  131. $this->notificationManager->expects($this->once())
  132. ->method('createNotification')
  133. ->willReturn($notification);
  134. $this->notificationManager->expects($this->once())
  135. ->method('markProcessed')
  136. ->with($notification);
  137. $response = $this->notificationsController->view('42');
  138. $this->assertInstanceOf(RedirectResponse::class, $response);
  139. }
  140. public function testViewInvalidComment() {
  141. $this->commentsManager->expects($this->any())
  142. ->method('get')
  143. ->with('42')
  144. ->will($this->throwException(new NotFoundException()));
  145. $this->rootFolder->expects($this->never())
  146. ->method('getUserFolder');
  147. $user = $this->createMock(IUser::class);
  148. $this->session->expects($this->once())
  149. ->method('getUser')
  150. ->willReturn($user);
  151. $user->expects($this->any())
  152. ->method('getUID')
  153. ->willReturn('user');
  154. $this->notificationManager->expects($this->never())
  155. ->method('createNotification');
  156. $this->notificationManager->expects($this->never())
  157. ->method('markProcessed');
  158. $response = $this->notificationsController->view('42');
  159. $this->assertInstanceOf(NotFoundResponse::class, $response);
  160. }
  161. public function testViewNoFile() {
  162. $comment = $this->createMock(IComment::class);
  163. $comment->expects($this->any())
  164. ->method('getObjectType')
  165. ->willReturn('files');
  166. $comment->expects($this->any())
  167. ->method('getId')
  168. ->willReturn('1234');
  169. $this->commentsManager->expects($this->any())
  170. ->method('get')
  171. ->with('42')
  172. ->willReturn($comment);
  173. $folder = $this->createMock(Folder::class);
  174. $this->rootFolder->expects($this->once())
  175. ->method('getUserFolder')
  176. ->willReturn($folder);
  177. $folder->expects($this->once())
  178. ->method('getById')
  179. ->willReturn([]);
  180. $user = $this->createMock(IUser::class);
  181. $this->session->expects($this->once())
  182. ->method('getUser')
  183. ->willReturn($user);
  184. $user->expects($this->any())
  185. ->method('getUID')
  186. ->willReturn('user');
  187. $notification = $this->createMock(INotification::class);
  188. $notification->expects($this->any())
  189. ->method($this->anything())
  190. ->willReturn($notification);
  191. $this->notificationManager->expects($this->once())
  192. ->method('createNotification')
  193. ->willReturn($notification);
  194. $this->notificationManager->expects($this->once())
  195. ->method('markProcessed')
  196. ->with($notification);
  197. $response = $this->notificationsController->view('42');
  198. $this->assertInstanceOf(NotFoundResponse::class, $response);
  199. }
  200. }