MessageTest.php 8.6 KB

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