MessageTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 Swift_Message;
  11. use Test\TestCase;
  12. class MessageTest extends TestCase {
  13. /** @var Swift_Message */
  14. private $swiftMessage;
  15. /** @var Message */
  16. private $message;
  17. /**
  18. * @return array
  19. */
  20. public function mailAddressProvider() {
  21. return array(
  22. array(array('lukas@owncloud.com' => 'Lukas Reschke'), array('lukas@owncloud.com' => 'Lukas Reschke')),
  23. array(array('lukas@owncloud.com' => 'Lukas Reschke', 'lukas@öwnclöüd.com', 'lukäs@owncloud.örg' => 'Lükäs Réschke'),
  24. array('lukas@owncloud.com' => 'Lukas Reschke', 'lukas@xn--wncld-iuae2c.com', 'lukäs@owncloud.xn--rg-eka' => 'Lükäs Réschke')),
  25. array(array('lukas@öwnclöüd.com'), array('lukas@xn--wncld-iuae2c.com'))
  26. );
  27. }
  28. function setUp() {
  29. parent::setUp();
  30. $this->swiftMessage = $this->getMockBuilder('\Swift_Message')
  31. ->disableOriginalConstructor()->getMock();
  32. $this->message = new Message($this->swiftMessage);
  33. }
  34. /**
  35. * @requires function idn_to_ascii
  36. * @dataProvider mailAddressProvider
  37. *
  38. * @param string $unconverted
  39. * @param string $expected
  40. */
  41. public function testConvertAddresses($unconverted, $expected) {
  42. $this->assertSame($expected, self::invokePrivate($this->message, 'convertAddresses', array($unconverted)));
  43. }
  44. public function testSetFrom() {
  45. $this->swiftMessage
  46. ->expects($this->once())
  47. ->method('setFrom')
  48. ->with(array('lukas@owncloud.com'));
  49. $this->message->setFrom(array('lukas@owncloud.com'));
  50. }
  51. public function testGetFrom() {
  52. $this->swiftMessage
  53. ->expects($this->once())
  54. ->method('getFrom')
  55. ->will($this->returnValue(array('lukas@owncloud.com')));
  56. $this->assertSame(array('lukas@owncloud.com'), $this->message->getFrom());
  57. }
  58. public function testSetReplyTo() {
  59. $this->swiftMessage
  60. ->expects($this->once())
  61. ->method('setReplyTo')
  62. ->with(['lukas@owncloud.com']);
  63. $this->message->setReplyTo(['lukas@owncloud.com']);
  64. }
  65. public function testGetReplyTo() {
  66. $this->swiftMessage
  67. ->expects($this->once())
  68. ->method('getReplyTo')
  69. ->will($this->returnValue(['lukas@owncloud.com']));
  70. $this->assertSame(['lukas@owncloud.com'], $this->message->getReplyTo());
  71. }
  72. public function testSetTo() {
  73. $this->swiftMessage
  74. ->expects($this->once())
  75. ->method('setTo')
  76. ->with(array('lukas@owncloud.com'));
  77. $this->message->setTo(array('lukas@owncloud.com'));
  78. }
  79. public function testGetTo() {
  80. $this->swiftMessage
  81. ->expects($this->once())
  82. ->method('getTo')
  83. ->will($this->returnValue(array('lukas@owncloud.com')));
  84. $this->assertSame(array('lukas@owncloud.com'), $this->message->getTo());
  85. }
  86. public function testSetCc() {
  87. $this->swiftMessage
  88. ->expects($this->once())
  89. ->method('setCc')
  90. ->with(array('lukas@owncloud.com'));
  91. $this->message->setCc(array('lukas@owncloud.com'));
  92. }
  93. public function testGetCc() {
  94. $this->swiftMessage
  95. ->expects($this->once())
  96. ->method('getCc')
  97. ->will($this->returnValue(array('lukas@owncloud.com')));
  98. $this->assertSame(array('lukas@owncloud.com'), $this->message->getCc());
  99. }
  100. public function testSetBcc() {
  101. $this->swiftMessage
  102. ->expects($this->once())
  103. ->method('setBcc')
  104. ->with(array('lukas@owncloud.com'));
  105. $this->message->setBcc(array('lukas@owncloud.com'));
  106. }
  107. public function testGetBcc() {
  108. $this->swiftMessage
  109. ->expects($this->once())
  110. ->method('getBcc')
  111. ->will($this->returnValue(array('lukas@owncloud.com')));
  112. $this->assertSame(array('lukas@owncloud.com'), $this->message->getBcc());
  113. }
  114. public function testSetSubject() {
  115. $this->swiftMessage
  116. ->expects($this->once())
  117. ->method('setSubject')
  118. ->with('Fancy Subject');
  119. $this->message->setSubject('Fancy Subject');
  120. }
  121. public function testGetSubject() {
  122. $this->swiftMessage
  123. ->expects($this->once())
  124. ->method('getSubject')
  125. ->will($this->returnValue('Fancy Subject'));
  126. $this->assertSame('Fancy Subject', $this->message->getSubject());
  127. }
  128. public function testSetPlainBody() {
  129. $this->swiftMessage
  130. ->expects($this->once())
  131. ->method('setBody')
  132. ->with('Fancy Body');
  133. $this->message->setPlainBody('Fancy Body');
  134. }
  135. public function testGetPlainBody() {
  136. $this->swiftMessage
  137. ->expects($this->once())
  138. ->method('getBody')
  139. ->will($this->returnValue('Fancy Body'));
  140. $this->assertSame('Fancy Body', $this->message->getPlainBody());
  141. }
  142. public function testSetHtmlBody() {
  143. $this->swiftMessage
  144. ->expects($this->once())
  145. ->method('addPart')
  146. ->with('<blink>Fancy Body</blink>', 'text/html');
  147. $this->message->setHtmlBody('<blink>Fancy Body</blink>');
  148. }
  149. }