MessageTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. public function testSetTo() {
  89. $this->swiftMessage
  90. ->expects($this->once())
  91. ->method('setTo')
  92. ->with(['lukas@owncloud.com']);
  93. $this->message->setTo(['lukas@owncloud.com']);
  94. }
  95. /**
  96. * @dataProvider getMailAddressProvider
  97. */
  98. public function testGetTo($swiftresult,$return) {
  99. $this->swiftMessage
  100. ->expects($this->once())
  101. ->method('getTo')
  102. ->willReturn($swiftresult);
  103. $this->assertSame($return, $this->message->getTo());
  104. }
  105. public function testSetCc() {
  106. $this->swiftMessage
  107. ->expects($this->once())
  108. ->method('setCc')
  109. ->with(['lukas@owncloud.com']);
  110. $this->message->setCc(['lukas@owncloud.com']);
  111. }
  112. /**
  113. * @dataProvider getMailAddressProvider
  114. */
  115. public function testGetCc($swiftresult,$return) {
  116. $this->swiftMessage
  117. ->expects($this->once())
  118. ->method('getCc')
  119. ->willReturn($swiftresult);
  120. $this->assertSame($return, $this->message->getCc());
  121. }
  122. public function testSetBcc() {
  123. $this->swiftMessage
  124. ->expects($this->once())
  125. ->method('setBcc')
  126. ->with(['lukas@owncloud.com']);
  127. $this->message->setBcc(['lukas@owncloud.com']);
  128. }
  129. /**
  130. * @dataProvider getMailAddressProvider
  131. */
  132. public function testGetBcc($swiftresult,$return) {
  133. $this->swiftMessage
  134. ->expects($this->once())
  135. ->method('getBcc')
  136. ->willReturn($swiftresult);
  137. $this->assertSame($return, $this->message->getBcc());
  138. }
  139. public function testSetSubject() {
  140. $this->swiftMessage
  141. ->expects($this->once())
  142. ->method('setSubject')
  143. ->with('Fancy Subject');
  144. $this->message->setSubject('Fancy Subject');
  145. }
  146. public function testGetSubject() {
  147. $this->swiftMessage
  148. ->expects($this->once())
  149. ->method('getSubject')
  150. ->willReturn('Fancy Subject');
  151. $this->assertSame('Fancy Subject', $this->message->getSubject());
  152. }
  153. public function testSetPlainBody() {
  154. $this->swiftMessage
  155. ->expects($this->once())
  156. ->method('setBody')
  157. ->with('Fancy Body');
  158. $this->message->setPlainBody('Fancy Body');
  159. }
  160. public function testGetPlainBody() {
  161. $this->swiftMessage
  162. ->expects($this->once())
  163. ->method('getBody')
  164. ->willReturn('Fancy Body');
  165. $this->assertSame('Fancy Body', $this->message->getPlainBody());
  166. }
  167. public function testSetHtmlBody() {
  168. $this->swiftMessage
  169. ->expects($this->once())
  170. ->method('addPart')
  171. ->with('<blink>Fancy Body</blink>', 'text/html');
  172. $this->message->setHtmlBody('<blink>Fancy Body</blink>');
  173. }
  174. public function testPlainTextRenderOption() {
  175. /** @var \PHPUnit\Framework\MockObject\MockObject|Swift_Message $swiftMessage */
  176. $swiftMessage = $this->getMockBuilder('\Swift_Message')
  177. ->disableOriginalConstructor()->getMock();
  178. /** @var \PHPUnit\Framework\MockObject\MockObject|IEMailTemplate $template */
  179. $template = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
  180. ->disableOriginalConstructor()->getMock();
  181. $message = new Message($swiftMessage, true);
  182. $template
  183. ->expects($this->never())
  184. ->method('renderHTML');
  185. $template
  186. ->expects($this->once())
  187. ->method('renderText');
  188. $template
  189. ->expects($this->once())
  190. ->method('renderSubject');
  191. $message->useTemplate($template);
  192. }
  193. public function testBothRenderingOptions() {
  194. /** @var \PHPUnit\Framework\MockObject\MockObject|Swift_Message $swiftMessage */
  195. $swiftMessage = $this->getMockBuilder('\Swift_Message')
  196. ->disableOriginalConstructor()->getMock();
  197. /** @var \PHPUnit\Framework\MockObject\MockObject|IEMailTemplate $template */
  198. $template = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
  199. ->disableOriginalConstructor()->getMock();
  200. $message = new Message($swiftMessage, false);
  201. $template
  202. ->expects($this->once())
  203. ->method('renderHTML');
  204. $template
  205. ->expects($this->once())
  206. ->method('renderText');
  207. $template
  208. ->expects($this->once())
  209. ->method('renderSubject');
  210. $message->useTemplate($template);
  211. }
  212. }