MessageTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 array(
  23. array(array('lukas@owncloud.com' => 'Lukas Reschke'), array('lukas@owncloud.com' => 'Lukas Reschke')),
  24. array(array('lukas@owncloud.com' => 'Lukas Reschke', 'lukas@öwnclöüd.com', 'lukäs@owncloud.örg' => 'Lükäs Réschke'),
  25. array('lukas@owncloud.com' => 'Lukas Reschke', 'lukas@xn--wncld-iuae2c.com', 'lukäs@owncloud.xn--rg-eka' => 'Lükäs Réschke')),
  26. array(array('lukas@öwnclöüd.com'), array('lukas@xn--wncld-iuae2c.com'))
  27. );
  28. }
  29. public function setUp() {
  30. parent::setUp();
  31. $this->swiftMessage = $this->getMockBuilder('\Swift_Message')
  32. ->disableOriginalConstructor()->getMock();
  33. $this->message = new Message($this->swiftMessage, false);
  34. }
  35. /**
  36. * @requires function idn_to_ascii
  37. * @dataProvider mailAddressProvider
  38. *
  39. * @param string $unconverted
  40. * @param string $expected
  41. */
  42. public function testConvertAddresses($unconverted, $expected) {
  43. $this->assertSame($expected, self::invokePrivate($this->message, 'convertAddresses', array($unconverted)));
  44. }
  45. public function testSetFrom() {
  46. $this->swiftMessage
  47. ->expects($this->once())
  48. ->method('setFrom')
  49. ->with(array('lukas@owncloud.com'));
  50. $this->message->setFrom(array('lukas@owncloud.com'));
  51. }
  52. public function testGetFrom() {
  53. $this->swiftMessage
  54. ->expects($this->once())
  55. ->method('getFrom')
  56. ->will($this->returnValue(array('lukas@owncloud.com')));
  57. $this->assertSame(array('lukas@owncloud.com'), $this->message->getFrom());
  58. }
  59. public function testSetReplyTo() {
  60. $this->swiftMessage
  61. ->expects($this->once())
  62. ->method('setReplyTo')
  63. ->with(['lukas@owncloud.com']);
  64. $this->message->setReplyTo(['lukas@owncloud.com']);
  65. }
  66. public function testGetReplyTo() {
  67. $this->swiftMessage
  68. ->expects($this->once())
  69. ->method('getReplyTo')
  70. ->willReturn('lukas@owncloud.com');
  71. $this->assertSame('lukas@owncloud.com', $this->message->getReplyTo());
  72. }
  73. public function testSetTo() {
  74. $this->swiftMessage
  75. ->expects($this->once())
  76. ->method('setTo')
  77. ->with(array('lukas@owncloud.com'));
  78. $this->message->setTo(array('lukas@owncloud.com'));
  79. }
  80. public function testGetTo() {
  81. $this->swiftMessage
  82. ->expects($this->once())
  83. ->method('getTo')
  84. ->will($this->returnValue(array('lukas@owncloud.com')));
  85. $this->assertSame(array('lukas@owncloud.com'), $this->message->getTo());
  86. }
  87. public function testSetCc() {
  88. $this->swiftMessage
  89. ->expects($this->once())
  90. ->method('setCc')
  91. ->with(array('lukas@owncloud.com'));
  92. $this->message->setCc(array('lukas@owncloud.com'));
  93. }
  94. public function testGetCc() {
  95. $this->swiftMessage
  96. ->expects($this->once())
  97. ->method('getCc')
  98. ->will($this->returnValue(array('lukas@owncloud.com')));
  99. $this->assertSame(array('lukas@owncloud.com'), $this->message->getCc());
  100. }
  101. public function testSetBcc() {
  102. $this->swiftMessage
  103. ->expects($this->once())
  104. ->method('setBcc')
  105. ->with(array('lukas@owncloud.com'));
  106. $this->message->setBcc(array('lukas@owncloud.com'));
  107. }
  108. public function testGetBcc() {
  109. $this->swiftMessage
  110. ->expects($this->once())
  111. ->method('getBcc')
  112. ->will($this->returnValue(array('lukas@owncloud.com')));
  113. $this->assertSame(array('lukas@owncloud.com'), $this->message->getBcc());
  114. }
  115. public function testSetSubject() {
  116. $this->swiftMessage
  117. ->expects($this->once())
  118. ->method('setSubject')
  119. ->with('Fancy Subject');
  120. $this->message->setSubject('Fancy Subject');
  121. }
  122. public function testGetSubject() {
  123. $this->swiftMessage
  124. ->expects($this->once())
  125. ->method('getSubject')
  126. ->will($this->returnValue('Fancy Subject'));
  127. $this->assertSame('Fancy Subject', $this->message->getSubject());
  128. }
  129. public function testSetPlainBody() {
  130. $this->swiftMessage
  131. ->expects($this->once())
  132. ->method('setBody')
  133. ->with('Fancy Body');
  134. $this->message->setPlainBody('Fancy Body');
  135. }
  136. public function testGetPlainBody() {
  137. $this->swiftMessage
  138. ->expects($this->once())
  139. ->method('getBody')
  140. ->will($this->returnValue('Fancy Body'));
  141. $this->assertSame('Fancy Body', $this->message->getPlainBody());
  142. }
  143. public function testSetHtmlBody() {
  144. $this->swiftMessage
  145. ->expects($this->once())
  146. ->method('addPart')
  147. ->with('<blink>Fancy Body</blink>', 'text/html');
  148. $this->message->setHtmlBody('<blink>Fancy Body</blink>');
  149. }
  150. public function testPlainTextRenderOption() {
  151. /** @var \PHPUnit_Framework_MockObject_MockObject|Swift_Message $swiftMessage */
  152. $swiftMessage = $this->getMockBuilder('\Swift_Message')
  153. ->disableOriginalConstructor()->getMock();
  154. /** @var \PHPUnit_Framework_MockObject_MockObject|IEMailTemplate $template */
  155. $template = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
  156. ->disableOriginalConstructor()->getMock();
  157. $message = new Message($swiftMessage, true);
  158. $template
  159. ->expects($this->never())
  160. ->method('renderHTML');
  161. $template
  162. ->expects($this->once())
  163. ->method('renderText');
  164. $template
  165. ->expects($this->once())
  166. ->method('renderSubject');
  167. $message->useTemplate($template);
  168. }
  169. public function testBothRenderingOptions() {
  170. /** @var \PHPUnit_Framework_MockObject_MockObject|Swift_Message $swiftMessage */
  171. $swiftMessage = $this->getMockBuilder('\Swift_Message')
  172. ->disableOriginalConstructor()->getMock();
  173. /** @var \PHPUnit_Framework_MockObject_MockObject|IEMailTemplate $template */
  174. $template = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
  175. ->disableOriginalConstructor()->getMock();
  176. $message = new Message($swiftMessage, false);
  177. $template
  178. ->expects($this->once())
  179. ->method('renderHTML');
  180. $template
  181. ->expects($this->once())
  182. ->method('renderText');
  183. $template
  184. ->expects($this->once())
  185. ->method('renderSubject');
  186. $message->useTemplate($template);
  187. }
  188. }