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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.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\Notification;
  27. use OCA\Comments\Notification\Listener;
  28. use OCP\Comments\CommentsEvent;
  29. use OCP\Comments\IComment;
  30. use OCP\IURLGenerator;
  31. use OCP\IUserManager;
  32. use OCP\Notification\IManager;
  33. use OCP\Notification\INotification;
  34. use Test\TestCase;
  35. class ListenerTest extends TestCase {
  36. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  37. protected $notificationManager;
  38. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $userManager;
  40. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  41. protected $urlGenerator;
  42. /** @var Listener */
  43. protected $listener;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->notificationManager = $this->createMock(\OCP\Notification\IManager::class);
  47. $this->userManager = $this->createMock(\OCP\IUserManager::class);
  48. $this->listener = new Listener(
  49. $this->notificationManager,
  50. $this->userManager
  51. );
  52. }
  53. public function eventProvider() {
  54. return [
  55. [CommentsEvent::EVENT_ADD, 'notify'],
  56. [CommentsEvent::EVENT_UPDATE, 'notify'],
  57. [CommentsEvent::EVENT_PRE_UPDATE, 'markProcessed'],
  58. [CommentsEvent::EVENT_DELETE, 'markProcessed']
  59. ];
  60. }
  61. /**
  62. * @dataProvider eventProvider
  63. * @param string $eventType
  64. * @param string $notificationMethod
  65. */
  66. public function testEvaluate($eventType, $notificationMethod) {
  67. /** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */
  68. $comment = $this->getMockBuilder(IComment::class)->getMock();
  69. $comment->expects($this->any())
  70. ->method('getObjectType')
  71. ->willReturn('files');
  72. $comment->expects($this->any())
  73. ->method('getCreationDateTime')
  74. ->willReturn(new \DateTime());
  75. $comment->expects($this->once())
  76. ->method('getMentions')
  77. ->willReturn([
  78. [ 'type' => 'user', 'id' => 'foobar'],
  79. [ 'type' => 'user', 'id' => 'barfoo'],
  80. [ 'type' => 'user', 'id' => 'foo@bar.com'],
  81. [ 'type' => 'user', 'id' => 'bar@foo.org@foobar.io'],
  82. [ 'type' => 'user', 'id' => '23452-4333-54353-2342'],
  83. [ 'type' => 'user', 'id' => 'yolo'],
  84. ]);
  85. $comment->expects($this->atLeastOnce())
  86. ->method('getId')
  87. ->willReturn('1234');
  88. /** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */
  89. $event = $this->getMockBuilder(CommentsEvent::class)
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $event->expects($this->once())
  93. ->method('getComment')
  94. ->willReturn($comment);
  95. $event->expects(($this->any()))
  96. ->method(('getEvent'))
  97. ->willReturn($eventType);
  98. /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  99. $notification = $this->getMockBuilder(INotification::class)->getMock();
  100. $notification->expects($this->any())
  101. ->method($this->anything())
  102. ->willReturn($notification);
  103. $notification->expects($this->exactly(6))
  104. ->method('setUser');
  105. $this->notificationManager->expects($this->once())
  106. ->method('createNotification')
  107. ->willReturn($notification);
  108. $this->notificationManager->expects($this->exactly(6))
  109. ->method($notificationMethod)
  110. ->with($this->isInstanceOf('\OCP\Notification\INotification'));
  111. $this->userManager->expects($this->exactly(6))
  112. ->method('userExists')
  113. ->withConsecutive(
  114. ['foobar'],
  115. ['barfoo'],
  116. ['foo@bar.com'],
  117. ['bar@foo.org@foobar.io'],
  118. ['23452-4333-54353-2342'],
  119. ['yolo']
  120. )
  121. ->willReturn(true);
  122. $this->listener->evaluate($event);
  123. }
  124. /**
  125. * @dataProvider eventProvider
  126. * @param string $eventType
  127. */
  128. public function testEvaluateNoMentions($eventType) {
  129. /** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */
  130. $comment = $this->getMockBuilder(IComment::class)->getMock();
  131. $comment->expects($this->any())
  132. ->method('getObjectType')
  133. ->willReturn('files');
  134. $comment->expects($this->any())
  135. ->method('getCreationDateTime')
  136. ->willReturn(new \DateTime());
  137. $comment->expects($this->once())
  138. ->method('getMentions')
  139. ->willReturn([]);
  140. /** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */
  141. $event = $this->getMockBuilder(CommentsEvent::class)
  142. ->disableOriginalConstructor()
  143. ->getMock();
  144. $event->expects($this->once())
  145. ->method('getComment')
  146. ->willReturn($comment);
  147. $event->expects(($this->any()))
  148. ->method(('getEvent'))
  149. ->willReturn($eventType);
  150. $this->notificationManager->expects($this->never())
  151. ->method('createNotification');
  152. $this->notificationManager->expects($this->never())
  153. ->method('notify');
  154. $this->notificationManager->expects($this->never())
  155. ->method('markProcessed');
  156. $this->userManager->expects($this->never())
  157. ->method('userExists');
  158. $this->listener->evaluate($event);
  159. }
  160. public function testEvaluateUserDoesNotExist() {
  161. /** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */
  162. $comment = $this->getMockBuilder(IComment::class)->getMock();
  163. $comment->expects($this->any())
  164. ->method('getObjectType')
  165. ->willReturn('files');
  166. $comment->expects($this->any())
  167. ->method('getCreationDateTime')
  168. ->willReturn(new \DateTime());
  169. $comment->expects($this->once())
  170. ->method('getMentions')
  171. ->willReturn([[ 'type' => 'user', 'id' => 'foobar']]);
  172. $comment->expects($this->atLeastOnce())
  173. ->method('getId')
  174. ->willReturn('1234');
  175. /** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */
  176. $event = $this->getMockBuilder(CommentsEvent::class)
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $event->expects($this->once())
  180. ->method('getComment')
  181. ->willReturn($comment);
  182. $event->expects(($this->any()))
  183. ->method(('getEvent'))
  184. ->willReturn(CommentsEvent::EVENT_ADD);
  185. /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  186. $notification = $this->getMockBuilder(INotification::class)->getMock();
  187. $notification->expects($this->any())
  188. ->method($this->anything())
  189. ->willReturn($notification);
  190. $notification->expects($this->never())
  191. ->method('setUser');
  192. $this->notificationManager->expects($this->once())
  193. ->method('createNotification')
  194. ->willReturn($notification);
  195. $this->notificationManager->expects($this->never())
  196. ->method('notify');
  197. $this->userManager->expects($this->once())
  198. ->method('userExists')
  199. ->withConsecutive(
  200. ['foobar']
  201. )
  202. ->willReturn(false);
  203. $this->listener->evaluate($event);
  204. }
  205. }