You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageTest.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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\Headers\AutoSubmitted;
  11. use OCP\Mail\IEMailTemplate;
  12. use Symfony\Component\Mime\Address;
  13. use Symfony\Component\Mime\Email;
  14. use Symfony\Component\Mime\Exception\RfcComplianceException;
  15. use Symfony\Component\Mime\Header\HeaderInterface;
  16. use Symfony\Component\Mime\Header\Headers;
  17. use Test\TestCase;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. class MessageTest extends TestCase {
  20. /** @var Email */
  21. private $symfonyEmail;
  22. /** @var Message */
  23. private $message;
  24. /**
  25. * @return array
  26. */
  27. public function mailAddressProvider() {
  28. return [
  29. [
  30. ['lukas@owncloud.com' => 'Lukas Reschke'],
  31. [new Address('lukas@owncloud.com', 'Lukas Reschke')]
  32. ],
  33. [
  34. [
  35. 'lukas@owncloud.com' => 'Lukas Reschke',
  36. 'lukas@öwnclöüd.com',
  37. 'lukäs@owncloud.örg' => 'Lükäs Réschke'
  38. ],
  39. [
  40. new Address('lukas@owncloud.com', 'Lukas Reschke'),
  41. new Address('lukas@öwnclöüd.com'),
  42. new Address('lukäs@owncloud.örg', 'Lükäs Réschke')
  43. ]
  44. ],
  45. [
  46. ['lukas@öwnclöüd.com'],
  47. [new Address('lukas@öwnclöüd.com')]
  48. ],
  49. ];
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function getMailAddressProvider() {
  55. return [
  56. [[], []],
  57. [['lukas@owncloud.com' => 'Lukas Reschke'], ['lukas@owncloud.com' => 'Lukas Reschke']],
  58. ];
  59. }
  60. protected function setUp(): void {
  61. parent::setUp();
  62. $this->symfonyEmail = $this->getMockBuilder(Email::class)
  63. ->disableOriginalConstructor()->getMock();
  64. $this->message = new Message($this->symfonyEmail, false);
  65. }
  66. /**
  67. * @dataProvider mailAddressProvider
  68. *
  69. * @param string $unconverted
  70. * @param string $expected
  71. */
  72. public function testConvertAddresses($unconverted, $expected) {
  73. $this->assertEquals($expected, self::invokePrivate($this->message, 'convertAddresses', [$unconverted]));
  74. }
  75. public function testSetRecipients(): void {
  76. $this->message = $this->message->setFrom(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']);
  77. $this->message = $this->message->setTo(['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"]);
  78. $this->message = $this->message->setReplyTo(['penny@stardewvalley-library.co.edu' => 'Penny']);
  79. $this->message = $this->message->setCc(['gunther@stardewvalley-library.co.edu' => 'Gunther']);
  80. $this->message = $this->message->setBcc(['pam@stardewvalley-bus.com' => 'Pam']);
  81. $this->symfonyEmail
  82. ->expects($this->once())
  83. ->method('from')
  84. ->willReturn(new Address('pierres-general-store@stardewvalley.com', 'Pierres General Store'));
  85. $this->symfonyEmail
  86. ->expects($this->once())
  87. ->method('to')
  88. ->willReturn(new Address('lewis-tent@stardewvalley.com', "Lewis' Tent Life"));
  89. $this->symfonyEmail
  90. ->expects($this->once())
  91. ->method('replyTo')
  92. ->willReturn(new Address('penny@stardewvalley-library.co.edu', 'Penny'));
  93. $this->symfonyEmail
  94. ->expects($this->once())
  95. ->method('cc')
  96. ->willReturn(new Address('gunther@stardewvalley-library.co.edu', 'Gunther'));
  97. $this->symfonyEmail
  98. ->expects($this->once())
  99. ->method('bcc')
  100. ->willReturn(new Address('pam@stardewvalley-bus.com', 'Pam'));
  101. $this->message->setRecipients();
  102. }
  103. public function testSetTo() {
  104. $expected = ['pierres-general-store@stardewvalley.com' => 'Pierres General Store'];
  105. $message = $this->message->setTo(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']);
  106. $this->assertEquals($expected, $message->getTo());
  107. }
  108. public function testSetRecipientsException(): void {
  109. $message = $this->message->setTo(['lewis-tent@~~~~.com' => "Lewis' Tent Life"]);
  110. $this->symfonyEmail
  111. ->expects($this->once())
  112. ->method('to')
  113. ->willThrowException(new RfcComplianceException());
  114. $this->expectException(RfcComplianceException::class);
  115. $message->setRecipients();
  116. }
  117. public function testSetRecipientsEmptyValues(): void {
  118. $message = $this->message->setTo([]);
  119. $this->symfonyEmail
  120. ->expects($this->once())
  121. ->method('to');
  122. $message->setRecipients();
  123. }
  124. public function testSetGetFrom() {
  125. $expected = ['pierres-general-store@stardewvalley.com' => 'Pierres General Store'];
  126. $message = $this->message->setFrom(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']);
  127. $this->assertEquals($expected, $message->getFrom());
  128. }
  129. public function testSetGetTo() {
  130. $expected = ['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"];
  131. $message = $this->message->setTo(['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"]);
  132. $this->assertEquals($expected, $message->getTo());
  133. }
  134. public function testSetGetReplyTo() {
  135. $expected = ['penny@stardewvalley-library.co.edu' => 'Penny'];
  136. $message = $this->message->setReplyTo(['penny@stardewvalley-library.co.edu' => 'Penny']);
  137. $this->assertEquals($expected, $message->getReplyTo());
  138. }
  139. public function testSetGetCC() {
  140. $expected = ['gunther@stardewvalley-library.co.edu' => 'Gunther'];
  141. $message = $this->message->setCc(['gunther@stardewvalley-library.co.edu' => 'Gunther']);
  142. $this->assertEquals($expected, $message->getCc());
  143. }
  144. public function testSetGetBCC() {
  145. $expected = ['pam@stardewvalley-bus.com' => 'Pam'];
  146. $message = $this->message->setBcc(['pam@stardewvalley-bus.com' => 'Pam']);
  147. $this->assertEquals($expected, $message->getBcc());
  148. }
  149. public function testSetPlainBody() {
  150. $this->symfonyEmail
  151. ->expects($this->once())
  152. ->method('text')
  153. ->with('Fancy Body');
  154. $this->message->setPlainBody('Fancy Body');
  155. }
  156. public function testGetPlainBody() {
  157. $this->symfonyEmail
  158. ->expects($this->once())
  159. ->method('getTextBody')
  160. ->willReturn('Fancy Body');
  161. $this->assertSame('Fancy Body', $this->message->getPlainBody());
  162. }
  163. public function testSetHtmlBody() {
  164. $this->symfonyEmail
  165. ->expects($this->once())
  166. ->method('html')
  167. ->with('<blink>Fancy Body</blink>', 'utf-8');
  168. $this->message->setHtmlBody('<blink>Fancy Body</blink>');
  169. }
  170. public function testPlainTextRenderOption() {
  171. /** @var MockObject|Email $symfonyEmail */
  172. $symfonyEmail = $this->getMockBuilder(Email::class)
  173. ->disableOriginalConstructor()->getMock();
  174. /** @var MockObject|IEMailTemplate $template */
  175. $template = $this->getMockBuilder(IEMailTemplate::class)
  176. ->disableOriginalConstructor()->getMock();
  177. $message = new Message($symfonyEmail, true);
  178. $template
  179. ->expects($this->never())
  180. ->method('renderHTML');
  181. $template
  182. ->expects($this->once())
  183. ->method('renderText');
  184. $template
  185. ->expects($this->once())
  186. ->method('renderSubject');
  187. $message->useTemplate($template);
  188. }
  189. public function testBothRenderingOptions() {
  190. /** @var MockObject|Email $symfonyEmail */
  191. $symfonyEmail = $this->getMockBuilder(Email::class)
  192. ->disableOriginalConstructor()->getMock();
  193. /** @var MockObject|IEMailTemplate $template */
  194. $template = $this->getMockBuilder(IEMailTemplate::class)
  195. ->disableOriginalConstructor()->getMock();
  196. $message = new Message($symfonyEmail, false);
  197. $template
  198. ->expects($this->once())
  199. ->method('renderHTML');
  200. $template
  201. ->expects($this->once())
  202. ->method('renderText');
  203. $template
  204. ->expects($this->once())
  205. ->method('renderSubject');
  206. $message->useTemplate($template);
  207. }
  208. public function testSetAutoSubmitted1() {
  209. $headers = new Headers($this->createMock(HeaderInterface::class));
  210. $headers->addTextHeader(AutoSubmitted::HEADER, "yes");
  211. $symfonyEmail = $this->createMock(Email::class);
  212. $symfonyEmail->method('getHeaders')
  213. ->willReturn($headers);
  214. $message = new Message($symfonyEmail, false);
  215. $message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
  216. $this->assertNotSame('no', $message->getAutoSubmitted());
  217. }
  218. public function testSetAutoSubmitted2() {
  219. $headers = new Headers($this->createMock(HeaderInterface::class));
  220. $headers->addTextHeader(AutoSubmitted::HEADER, 'no');
  221. $symfonyEmail = $this->createMock(Email::class);
  222. $symfonyEmail->method('getHeaders')
  223. ->willReturn($headers);
  224. $message = new Message($symfonyEmail, false);
  225. $message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
  226. $this->assertSame('auto-generated', $message->getAutoSubmitted());
  227. }
  228. public function testGetAutoSubmitted() {
  229. $headers = new Headers($this->createMock(HeaderInterface::class));
  230. $headers->addTextHeader(AutoSubmitted::HEADER, 'no');
  231. $symfonyEmail = $this->createMock(Email::class);
  232. $symfonyEmail->method('getHeaders')
  233. ->willReturn($headers);
  234. $message = new Message($symfonyEmail, false);
  235. $this->assertSame("no", $message->getAutoSubmitted());
  236. }
  237. }