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

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