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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Comments\Tests\Unit\Controller;
  24. use OCA\Comments\Controller\Notifications;
  25. use OCP\Comments\IComment;
  26. use OCP\Comments\ICommentsManager;
  27. use OCP\Comments\NotFoundException;
  28. use OCP\Files\Folder;
  29. use OCP\Files\Node;
  30. use OCP\IRequest;
  31. use OCP\IURLGenerator;
  32. use OCP\IUser;
  33. use OCP\IUserSession;
  34. use OCP\Notification\IManager;
  35. use OCP\Notification\INotification;
  36. use Test\TestCase;
  37. class NotificationsTest extends TestCase {
  38. /** @var \OCA\Comments\Controller\Notifications */
  39. protected $notificationsController;
  40. /** @var \OCP\Comments\ICommentsManager|\PHPUnit_Framework_MockObject_MockObject */
  41. protected $commentsManager;
  42. /** @var \OCP\Files\Folder|\PHPUnit_Framework_MockObject_MockObject */
  43. protected $folder;
  44. /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  45. protected $session;
  46. /** @var \OCP\Notification\IManager|\PHPUnit_Framework_MockObject_MockObject */
  47. protected $notificationManager;
  48. protected function setUp() {
  49. parent::setUp();
  50. $this->commentsManager = $this->getMockBuilder(ICommentsManager::class)->getMock();
  51. $this->folder = $this->getMockBuilder(Folder::class)->getMock();
  52. $this->session = $this->getMockBuilder(IUserSession::class)->getMock();
  53. $this->notificationManager = $this->getMockBuilder(IManager::class)->getMock();
  54. $this->notificationsController = new Notifications(
  55. 'comments',
  56. $this->getMockBuilder(IRequest::class)->getMock(),
  57. $this->commentsManager,
  58. $this->folder,
  59. $this->getMockBuilder(IURLGenerator::class)->getMock(),
  60. $this->notificationManager,
  61. $this->session
  62. );
  63. }
  64. public function testViewSuccess() {
  65. $comment = $this->getMockBuilder(IComment::class)->getMock();
  66. $comment->expects($this->any())
  67. ->method('getObjectType')
  68. ->will($this->returnValue('files'));
  69. $this->commentsManager->expects($this->any())
  70. ->method('get')
  71. ->with('42')
  72. ->will($this->returnValue($comment));
  73. $file = $this->getMockBuilder(Node::class)->getMock();
  74. $this->folder->expects($this->once())
  75. ->method('getById')
  76. ->will($this->returnValue([$file]));
  77. $this->session->expects($this->once())
  78. ->method('getUser')
  79. ->will($this->returnValue($this->getMockBuilder(IUser::class)->getMock()));
  80. $notification = $this->getMockBuilder(INotification::class)->getMock();
  81. $notification->expects($this->any())
  82. ->method($this->anything())
  83. ->will($this->returnValue($notification));
  84. $this->notificationManager->expects($this->once())
  85. ->method('createNotification')
  86. ->will($this->returnValue($notification));
  87. $this->notificationManager->expects($this->once())
  88. ->method('markProcessed')
  89. ->with($notification);
  90. $response = $this->notificationsController->view('42');
  91. $this->assertInstanceOf('\OCP\AppFramework\Http\RedirectResponse', $response);
  92. }
  93. public function testViewInvalidComment() {
  94. $this->commentsManager->expects($this->any())
  95. ->method('get')
  96. ->with('42')
  97. ->will($this->throwException(new NotFoundException()));
  98. $this->folder->expects($this->never())
  99. ->method('getById');
  100. $this->session->expects($this->never())
  101. ->method('getUser');
  102. $this->notificationManager->expects($this->never())
  103. ->method('createNotification');
  104. $this->notificationManager->expects($this->never())
  105. ->method('markProcessed');
  106. $response = $this->notificationsController->view('42');
  107. $this->assertInstanceOf('\OCP\AppFramework\Http\NotFoundResponse', $response);
  108. }
  109. public function testViewNoFile() {
  110. $comment = $this->getMockBuilder(IComment::class)->getMock();
  111. $comment->expects($this->any())
  112. ->method('getObjectType')
  113. ->will($this->returnValue('files'));
  114. $this->commentsManager->expects($this->any())
  115. ->method('get')
  116. ->with('42')
  117. ->will($this->returnValue($comment));
  118. $this->folder->expects($this->once())
  119. ->method('getById')
  120. ->will($this->returnValue([]));
  121. $this->session->expects($this->once())
  122. ->method('getUser')
  123. ->will($this->returnValue($this->getMockBuilder(IUser::class)->getMock()));
  124. $notification = $this->getMockBuilder(INotification::class)->getMock();
  125. $notification->expects($this->any())
  126. ->method($this->anything())
  127. ->will($this->returnValue($notification));
  128. $this->notificationManager->expects($this->once())
  129. ->method('createNotification')
  130. ->will($this->returnValue($notification));
  131. $this->notificationManager->expects($this->once())
  132. ->method('markProcessed')
  133. ->with($notification);
  134. $response = $this->notificationsController->view('42');
  135. $this->assertInstanceOf('\OCP\AppFramework\Http\NotFoundResponse', $response);
  136. }
  137. }