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.

ListenerTest.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Comments\Tests\Unit\Notification;
  26. use OCA\Comments\Notification\Listener;
  27. use OCP\Comments\CommentsEvent;
  28. use OCP\Comments\IComment;
  29. use OCP\IURLGenerator;
  30. use OCP\IUserManager;
  31. use OCP\Notification\IManager;
  32. use OCP\Notification\INotification;
  33. use Test\TestCase;
  34. class ListenerTest extends TestCase {
  35. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  36. protected $notificationManager;
  37. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $userManager;
  39. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $urlGenerator;
  41. /** @var Listener */
  42. protected $listener;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->notificationManager = $this->createMock(\OCP\Notification\IManager::class);
  46. $this->userManager = $this->createMock(\OCP\IUserManager::class);
  47. $this->listener = new Listener(
  48. $this->notificationManager,
  49. $this->userManager
  50. );
  51. }
  52. public function eventProvider() {
  53. return [
  54. [CommentsEvent::EVENT_ADD, 'notify'],
  55. [CommentsEvent::EVENT_UPDATE, 'notify'],
  56. [CommentsEvent::EVENT_PRE_UPDATE, 'markProcessed'],
  57. [CommentsEvent::EVENT_DELETE, 'markProcessed']
  58. ];
  59. }
  60. /**
  61. * @dataProvider eventProvider
  62. * @param string $eventType
  63. * @param string $notificationMethod
  64. */
  65. public function testEvaluate($eventType, $notificationMethod) {
  66. /** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
  67. $comment = $this->getMockBuilder(IComment::class)->getMock();
  68. $comment->expects($this->any())
  69. ->method('getObjectType')
  70. ->will($this->returnValue('files'));
  71. $comment->expects($this->any())
  72. ->method('getCreationDateTime')
  73. ->will($this->returnValue(new \DateTime()));
  74. $comment->expects($this->once())
  75. ->method('getMentions')
  76. ->willReturn([
  77. [ 'type' => 'user', 'id' => 'foobar'],
  78. [ 'type' => 'user', 'id' => 'barfoo'],
  79. [ 'type' => 'user', 'id' => 'foo@bar.com'],
  80. [ 'type' => 'user', 'id' => 'bar@foo.org@foobar.io'],
  81. [ 'type' => 'user', 'id' => '23452-4333-54353-2342'],
  82. [ 'type' => 'user', 'id' => 'yolo'],
  83. ]);
  84. $comment->expects($this->atLeastOnce())
  85. ->method('getId')
  86. ->willReturn('1234');
  87. /** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
  88. $event = $this->getMockBuilder(CommentsEvent::class)
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $event->expects($this->once())
  92. ->method('getComment')
  93. ->will($this->returnValue($comment));
  94. $event->expects(($this->any()))
  95. ->method(('getEvent'))
  96. ->will($this->returnValue($eventType));
  97. /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  98. $notification = $this->getMockBuilder(INotification::class)->getMock();
  99. $notification->expects($this->any())
  100. ->method($this->anything())
  101. ->will($this->returnValue($notification));
  102. $notification->expects($this->exactly(6))
  103. ->method('setUser');
  104. $this->notificationManager->expects($this->once())
  105. ->method('createNotification')
  106. ->will($this->returnValue($notification));
  107. $this->notificationManager->expects($this->exactly(6))
  108. ->method($notificationMethod)
  109. ->with($this->isInstanceOf('\OCP\Notification\INotification'));
  110. $this->userManager->expects($this->exactly(6))
  111. ->method('userExists')
  112. ->withConsecutive(
  113. ['foobar'],
  114. ['barfoo'],
  115. ['foo@bar.com'],
  116. ['bar@foo.org@foobar.io'],
  117. ['23452-4333-54353-2342'],
  118. ['yolo']
  119. )
  120. ->will($this->returnValue(true));
  121. $this->listener->evaluate($event);
  122. }
  123. /**
  124. * @dataProvider eventProvider
  125. * @param string $eventType
  126. */
  127. public function testEvaluateNoMentions($eventType) {
  128. /** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
  129. $comment = $this->getMockBuilder(IComment::class)->getMock();
  130. $comment->expects($this->any())
  131. ->method('getObjectType')
  132. ->will($this->returnValue('files'));
  133. $comment->expects($this->any())
  134. ->method('getCreationDateTime')
  135. ->will($this->returnValue(new \DateTime()));
  136. $comment->expects($this->once())
  137. ->method('getMentions')
  138. ->willReturn([]);
  139. /** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
  140. $event = $this->getMockBuilder(CommentsEvent::class)
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $event->expects($this->once())
  144. ->method('getComment')
  145. ->will($this->returnValue($comment));
  146. $event->expects(($this->any()))
  147. ->method(('getEvent'))
  148. ->will($this->returnValue($eventType));
  149. $this->notificationManager->expects($this->never())
  150. ->method('createNotification');
  151. $this->notificationManager->expects($this->never())
  152. ->method('notify');
  153. $this->notificationManager->expects($this->never())
  154. ->method('markProcessed');
  155. $this->userManager->expects($this->never())
  156. ->method('userExists');
  157. $this->listener->evaluate($event);
  158. }
  159. public function testEvaluateUserDoesNotExist() {
  160. /** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
  161. $comment = $this->getMockBuilder(IComment::class)->getMock();
  162. $comment->expects($this->any())
  163. ->method('getObjectType')
  164. ->will($this->returnValue('files'));
  165. $comment->expects($this->any())
  166. ->method('getCreationDateTime')
  167. ->will($this->returnValue(new \DateTime()));
  168. $comment->expects($this->once())
  169. ->method('getMentions')
  170. ->willReturn([[ 'type' => 'user', 'id' => 'foobar']]);
  171. $comment->expects($this->atLeastOnce())
  172. ->method('getId')
  173. ->willReturn('1234');
  174. /** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
  175. $event = $this->getMockBuilder(CommentsEvent::class)
  176. ->disableOriginalConstructor()
  177. ->getMock();
  178. $event->expects($this->once())
  179. ->method('getComment')
  180. ->will($this->returnValue($comment));
  181. $event->expects(($this->any()))
  182. ->method(('getEvent'))
  183. ->will($this->returnValue(CommentsEvent::EVENT_ADD));
  184. /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  185. $notification = $this->getMockBuilder(INotification::class)->getMock();
  186. $notification->expects($this->any())
  187. ->method($this->anything())
  188. ->will($this->returnValue($notification));
  189. $notification->expects($this->never())
  190. ->method('setUser');
  191. $this->notificationManager->expects($this->once())
  192. ->method('createNotification')
  193. ->will($this->returnValue($notification));
  194. $this->notificationManager->expects($this->never())
  195. ->method('notify');
  196. $this->userManager->expects($this->once())
  197. ->method('userExists')
  198. ->withConsecutive(
  199. ['foobar']
  200. )
  201. ->will($this->returnValue(false));
  202. $this->listener->evaluate($event);
  203. }
  204. }