MailNotificationsTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Share;
  22. use OC\Share\MailNotifications;
  23. use OCP\IL10N;
  24. use OCP\IUser;
  25. use OCP\Mail\IMailer;
  26. use OCP\ILogger;
  27. use OCP\Defaults;
  28. use OCP\IURLGenerator;
  29. /**
  30. * Class MailNotificationsTest
  31. */
  32. class MailNotificationsTest extends \Test\TestCase {
  33. /** @var IL10N */
  34. private $l10n;
  35. /** @var IMailer | \PHPUnit_Framework_MockObject_MockObject */
  36. private $mailer;
  37. /** @var ILogger */
  38. private $logger;
  39. /** @var Defaults | \PHPUnit_Framework_MockObject_MockObject */
  40. private $defaults;
  41. /** @var IUser | \PHPUnit_Framework_MockObject_MockObject */
  42. private $user;
  43. /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject */
  44. private $urlGenerator;
  45. public function setUp() {
  46. parent::setUp();
  47. $this->l10n = $this->getMockBuilder('\OCP\IL10N')
  48. ->disableOriginalConstructor()->getMock();
  49. $this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')
  50. ->disableOriginalConstructor()->getMock();
  51. $this->logger = $this->getMockBuilder('\OCP\ILogger')
  52. ->disableOriginalConstructor()->getMock();
  53. $this->defaults = $this->getMockBuilder('\OCP\Defaults')
  54. ->disableOriginalConstructor()->getMock();
  55. $this->user = $this->getMockBuilder('\OCP\IUser')
  56. ->disableOriginalConstructor()->getMock();
  57. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  58. $this->l10n->expects($this->any())
  59. ->method('t')
  60. ->will($this->returnCallback(function($text, $parameters = array()) {
  61. return vsprintf($text, $parameters);
  62. }));
  63. $this->defaults
  64. ->expects($this->once())
  65. ->method('getName')
  66. ->will($this->returnValue('UnitTestCloud'));
  67. $this->user
  68. ->expects($this->once())
  69. ->method('getEMailAddress')
  70. ->willReturn('sharer@owncloud.com');
  71. $this->user
  72. ->expects($this->once())
  73. ->method('getDisplayName')
  74. ->willReturn('TestUser');
  75. }
  76. public function testSendLinkShareMailWithoutReplyTo() {
  77. $message = $this->getMockBuilder('\OC\Mail\Message')
  78. ->disableOriginalConstructor()->getMock();
  79. $message
  80. ->expects($this->once())
  81. ->method('setSubject')
  82. ->with('TestUser shared »MyFile« with you');
  83. $message
  84. ->expects($this->once())
  85. ->method('setTo')
  86. ->with(['lukas@owncloud.com']);
  87. $message
  88. ->expects($this->once())
  89. ->method('setHtmlBody');
  90. $message
  91. ->expects($this->once())
  92. ->method('setPlainBody');
  93. $message
  94. ->expects($this->once())
  95. ->method('setFrom')
  96. ->with([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']);
  97. $this->mailer
  98. ->expects($this->once())
  99. ->method('createMessage')
  100. ->will($this->returnValue($message));
  101. $this->mailer
  102. ->expects($this->once())
  103. ->method('send')
  104. ->with($message)
  105. ->will($this->returnValue([]));
  106. $mailNotifications = new MailNotifications(
  107. $this->user,
  108. $this->l10n,
  109. $this->mailer,
  110. $this->logger,
  111. $this->defaults,
  112. $this->urlGenerator
  113. );
  114. $this->assertSame([], $mailNotifications->sendLinkShareMail('lukas@owncloud.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
  115. }
  116. public function dataSendLinkShareMailWithReplyTo() {
  117. return [
  118. ['lukas@owncloud.com', ['lukas@owncloud.com']],
  119. ['lukas@owncloud.com nickvergessen@owncloud.com', ['lukas@owncloud.com', 'nickvergessen@owncloud.com']],
  120. ['lukas@owncloud.com,nickvergessen@owncloud.com', ['lukas@owncloud.com', 'nickvergessen@owncloud.com']],
  121. ['lukas@owncloud.com, nickvergessen@owncloud.com', ['lukas@owncloud.com', 'nickvergessen@owncloud.com']],
  122. ['lukas@owncloud.com;nickvergessen@owncloud.com', ['lukas@owncloud.com', 'nickvergessen@owncloud.com']],
  123. ['lukas@owncloud.com; nickvergessen@owncloud.com', ['lukas@owncloud.com', 'nickvergessen@owncloud.com']],
  124. ];
  125. }
  126. /**
  127. * @dataProvider dataSendLinkShareMailWithReplyTo
  128. * @param string $to
  129. * @param array $expectedTo
  130. */
  131. public function testSendLinkShareMailWithReplyTo($to, array $expectedTo) {
  132. $message = $this->getMockBuilder('\OC\Mail\Message')
  133. ->disableOriginalConstructor()->getMock();
  134. $message
  135. ->expects($this->once())
  136. ->method('setSubject')
  137. ->with('TestUser shared »MyFile« with you');
  138. $message
  139. ->expects($this->once())
  140. ->method('setTo')
  141. ->with($expectedTo);
  142. $message
  143. ->expects($this->once())
  144. ->method('setHtmlBody');
  145. $message
  146. ->expects($this->once())
  147. ->method('setPlainBody');
  148. $message
  149. ->expects($this->once())
  150. ->method('setFrom')
  151. ->with([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']);
  152. $message
  153. ->expects($this->once())
  154. ->method('setReplyTo')
  155. ->with(['sharer@owncloud.com']);
  156. $this->mailer
  157. ->expects($this->once())
  158. ->method('createMessage')
  159. ->will($this->returnValue($message));
  160. $this->mailer
  161. ->expects($this->once())
  162. ->method('send')
  163. ->with($message)
  164. ->will($this->returnValue([]));
  165. $mailNotifications = new MailNotifications(
  166. $this->user,
  167. $this->l10n,
  168. $this->mailer,
  169. $this->logger,
  170. $this->defaults,
  171. $this->urlGenerator
  172. );
  173. $this->assertSame([], $mailNotifications->sendLinkShareMail($to, 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
  174. }
  175. public function testSendLinkShareMailException() {
  176. $this->setupMailerMock('TestUser shared »MyFile« with you', ['lukas@owncloud.com']);
  177. $mailNotifications = new MailNotifications(
  178. $this->user,
  179. $this->l10n,
  180. $this->mailer,
  181. $this->logger,
  182. $this->defaults,
  183. $this->urlGenerator
  184. );
  185. $this->assertSame(['lukas@owncloud.com'], $mailNotifications->sendLinkShareMail('lukas@owncloud.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
  186. }
  187. /**
  188. * @param string $subject
  189. */
  190. protected function setupMailerMock($subject, $to, $exceptionOnSend = true) {
  191. $message = $this->getMockBuilder('\OC\Mail\Message')
  192. ->disableOriginalConstructor()->getMock();
  193. $message
  194. ->expects($this->once())
  195. ->method('setSubject')
  196. ->with($subject);
  197. $message
  198. ->expects($this->once())
  199. ->method('setTo')
  200. ->with($to);
  201. $message
  202. ->expects($this->once())
  203. ->method('setHtmlBody');
  204. $message
  205. ->expects($this->once())
  206. ->method('setPlainBody');
  207. $message
  208. ->expects($this->once())
  209. ->method('setFrom')
  210. ->with([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']);
  211. $this->mailer
  212. ->expects($this->once())
  213. ->method('createMessage')
  214. ->will($this->returnValue($message));
  215. if ($exceptionOnSend) {
  216. $this->mailer
  217. ->expects($this->once())
  218. ->method('send')
  219. ->with($message)
  220. ->will($this->throwException(new \Exception('Some Exception Message')));
  221. }
  222. }
  223. }