MessageTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Mail;
  9. use OC\Mail\Message;
  10. use OCP\Mail\IEMailTemplate;
  11. use Swift_Message;
  12. use Test\TestCase;
  13. class MessageTest extends TestCase {
  14. /** @var Swift_Message */
  15. private $swiftMessage;
  16. /** @var Message */
  17. private $message;
  18. /**
  19. * @return array
  20. */
  21. public function mailAddressProvider() {
  22. return [
  23. [['lukas@owncloud.com' => 'Lukas Reschke'], ['lukas@owncloud.com' => 'Lukas Reschke']],
  24. [['lukas@owncloud.com' => 'Lukas Reschke', 'lukas@öwnclöüd.com', 'lukäs@owncloud.örg' => 'Lükäs Réschke'],
  25. ['lukas@owncloud.com' => 'Lukas Reschke', 'lukas@xn--wncld-iuae2c.com', 'lukäs@owncloud.xn--rg-eka' => 'Lükäs Réschke']],
  26. [['lukas@öwnclöüd.com'], ['lukas@xn--wncld-iuae2c.com']],
  27. ];
  28. }
  29. /**
  30. * @return array
  31. */
  32. public function getMailAddressProvider() {
  33. return [
  34. [null, []],
  35. [['lukas@owncloud.com' => 'Lukas Reschke'], ['lukas@owncloud.com' => 'Lukas Reschke']],
  36. ];
  37. }
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->swiftMessage = $this->getMockBuilder('\Swift_Message')
  41. ->disableOriginalConstructor()->getMock();
  42. $this->message = new Message($this->swiftMessage, false);
  43. }
  44. /**
  45. * @requires function idn_to_ascii
  46. * @dataProvider mailAddressProvider
  47. *
  48. * @param string $unconverted
  49. * @param string $expected
  50. */
  51. public function testConvertAddresses($unconverted, $expected) {
  52. $this->assertSame($expected, self::invokePrivate($this->message, 'convertAddresses', [$unconverted]));
  53. }
  54. public function testSetFrom() {
  55. $this->swiftMessage
  56. ->expects($this->once())
  57. ->method('setFrom')
  58. ->with(['lukas@owncloud.com']);
  59. $this->message->setFrom(['lukas@owncloud.com']);
  60. }
  61. /**
  62. * @dataProvider getMailAddressProvider
  63. *
  64. * @param $swiftresult
  65. * @param $return
  66. */
  67. public function testGetFrom($swiftresult, $return) {
  68. $this->swiftMessage
  69. ->expects($this->once())
  70. ->method('getFrom')
  71. ->willReturn($swiftresult);
  72. $this->assertSame($return, $this->message->getFrom());
  73. }
  74. public function testSetReplyTo() {
  75. $this->swiftMessage
  76. ->expects($this->once())
  77. ->method('setReplyTo')
  78. ->with(['lukas@owncloud.com']);
  79. $this->message->setReplyTo(['lukas@owncloud.com']);
  80. }
  81. public function testGetReplyTo() {
  82. $this->swiftMessage
  83. ->expects($this->once())
  84. ->method('getReplyTo')
  85. ->willReturn('lukas@owncloud.com');
  86. $this->assertSame('lukas@owncloud.com', $this->message->getReplyTo());
  87. }
  88. /** @dataProvider dataSetTo */
  89. public function testSetTo(array $to, array $expected) {
  90. $this->swiftMessage
  91. ->expects($this->once())
  92. ->method('setTo')
  93. ->with($expected);
  94. $this->message->setTo($to);
  95. }
  96. public function dataSetTo(): array {
  97. return [
  98. [['robot@example.com'], ['robot@example.com']],
  99. [['robot'], ['robot' => 'robot']],
  100. [['robot' => 'robot display name'], ['robot' => 'robot display name']],
  101. [['example@🤖.com'], ['example@xn--yp9h.com']],
  102. [['example@🤖.com' => 'A robot'], ['example@xn--yp9h.com' => 'A robot']],
  103. ];
  104. }
  105. /**
  106. * @dataProvider getMailAddressProvider
  107. */
  108. public function testGetTo($swiftresult, $return) {
  109. $this->swiftMessage
  110. ->expects($this->once())
  111. ->method('getTo')
  112. ->willReturn($swiftresult);
  113. $this->assertSame($return, $this->message->getTo());
  114. }
  115. public function testSetCc() {
  116. $this->swiftMessage
  117. ->expects($this->once())
  118. ->method('setCc')
  119. ->with(['lukas@owncloud.com']);
  120. $this->message->setCc(['lukas@owncloud.com']);
  121. }
  122. /**
  123. * @dataProvider getMailAddressProvider
  124. */
  125. public function testGetCc($swiftresult, $return) {
  126. $this->swiftMessage
  127. ->expects($this->once())
  128. ->method('getCc')
  129. ->willReturn($swiftresult);
  130. $this->assertSame($return, $this->message->getCc());
  131. }
  132. public function testSetBcc() {
  133. $this->swiftMessage
  134. ->expects($this->once())
  135. ->method('setBcc')
  136. ->with(['lukas@owncloud.com']);
  137. $this->message->setBcc(['lukas@owncloud.com']);
  138. }
  139. /**
  140. * @dataProvider getMailAddressProvider
  141. */
  142. public function testGetBcc($swiftresult, $return) {
  143. $this->swiftMessage
  144. ->expects($this->once())
  145. ->method('getBcc')
  146. ->willReturn($swiftresult);
  147. $this->assertSame($return, $this->message->getBcc());
  148. }
  149. public function testSetSubject() {
  150. $this->swiftMessage
  151. ->expects($this->once())
  152. ->method('setSubject')
  153. ->with('Fancy Subject');
  154. $this->message->setSubject('Fancy Subject');
  155. }
  156. public function testGetSubject() {
  157. $this->swiftMessage
  158. ->expects($this->once())
  159. ->method('getSubject')
  160. ->willReturn('Fancy Subject');
  161. $this->assertSame('Fancy Subject', $this->message->getSubject());
  162. }
  163. public function testSetPlainBody() {
  164. $this->swiftMessage
  165. ->expects($this->once())
  166. ->method('setBody')
  167. ->with('Fancy Body');
  168. $this->message->setPlainBody('Fancy Body');
  169. }
  170. public function testGetPlainBody() {
  171. $this->swiftMessage
  172. ->expects($this->once())
  173. ->method('getBody')
  174. ->willReturn('Fancy Body');
  175. $this->assertSame('Fancy Body', $this->message->getPlainBody());
  176. }
  177. public function testSetHtmlBody() {
  178. $this->swiftMessage
  179. ->expects($this->once())
  180. ->method('addPart')
  181. ->with('<blink>Fancy Body</blink>', 'text/html');
  182. $this->message->setHtmlBody('<blink>Fancy Body</blink>');
  183. }
  184. public function testPlainTextRenderOption() {
  185. /** @var \PHPUnit\Framework\MockObject\MockObject|Swift_Message $swiftMessage */
  186. $swiftMessage = $this->getMockBuilder('\Swift_Message')
  187. ->disableOriginalConstructor()->getMock();
  188. /** @var \PHPUnit\Framework\MockObject\MockObject|IEMailTemplate $template */
  189. $template = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
  190. ->disableOriginalConstructor()->getMock();
  191. $message = new Message($swiftMessage, true);
  192. $template
  193. ->expects($this->never())
  194. ->method('renderHTML');
  195. $template
  196. ->expects($this->once())
  197. ->method('renderText');
  198. $template
  199. ->expects($this->once())
  200. ->method('renderSubject');
  201. $message->useTemplate($template);
  202. }
  203. public function testBothRenderingOptions() {
  204. /** @var \PHPUnit\Framework\MockObject\MockObject|Swift_Message $swiftMessage */
  205. $swiftMessage = $this->getMockBuilder('\Swift_Message')
  206. ->disableOriginalConstructor()->getMock();
  207. /** @var \PHPUnit\Framework\MockObject\MockObject|IEMailTemplate $template */
  208. $template = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
  209. ->disableOriginalConstructor()->getMock();
  210. $message = new Message($swiftMessage, false);
  211. $template
  212. ->expects($this->once())
  213. ->method('renderHTML');
  214. $template
  215. ->expects($this->once())
  216. ->method('renderText');
  217. $template
  218. ->expects($this->once())
  219. ->method('renderSubject');
  220. $message->useTemplate($template);
  221. }
  222. }