MessageTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Mailer;
  17. use Cake\Mailer\Message;
  18. use Cake\TestSuite\TestCase;
  19. use TestApp\Mailer\TestMessage;
  20. /**
  21. * MessageTest class
  22. */
  23. class MessageTest extends TestCase
  24. {
  25. /**
  26. * @var \Cake\Mailer\Message
  27. */
  28. protected $message;
  29. public function setUp(): void
  30. {
  31. parent::setUp();
  32. $this->message = new Message();
  33. }
  34. /**
  35. * testWrap method
  36. *
  37. * @return void
  38. */
  39. public function testWrap()
  40. {
  41. $renderer = new TestMessage();
  42. $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
  43. $result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
  44. $expected = [
  45. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
  46. 'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
  47. '',
  48. ];
  49. $this->assertSame($expected, $result);
  50. $text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
  51. $result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
  52. $expected = [
  53. 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
  54. 'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
  55. 'amet.',
  56. '',
  57. ];
  58. $this->assertSame($expected, $result);
  59. $text = '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula pellentesque accumsan amet.<hr></p>';
  60. $result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
  61. $expected = [
  62. '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
  63. 'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
  64. 'pellentesque accumsan amet.<hr></p>',
  65. '',
  66. ];
  67. $this->assertSame($expected, $result);
  68. $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac <a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
  69. $result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
  70. $expected = [
  71. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
  72. '<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
  73. 'nisi, vehicula pellentesque accumsan amet.',
  74. '',
  75. ];
  76. $this->assertSame($expected, $result);
  77. $text = 'Lorem ipsum <a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">ok</a>';
  78. $result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
  79. $expected = [
  80. 'Lorem ipsum',
  81. '<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
  82. 'ok</a>',
  83. '',
  84. ];
  85. $this->assertSame($expected, $result);
  86. $text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
  87. $result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
  88. $expected = [
  89. 'Lorem ipsum',
  90. 'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
  91. 'ok.',
  92. '',
  93. ];
  94. $this->assertSame($expected, $result);
  95. }
  96. /**
  97. * testHeaders method
  98. *
  99. * @return void
  100. */
  101. public function testHeaders()
  102. {
  103. $this->message->setMessageId(false);
  104. $this->message->setHeaders(['X-Something' => 'nice']);
  105. $expected = [
  106. 'X-Something' => 'nice',
  107. 'Date' => date(DATE_RFC2822),
  108. 'MIME-Version' => '1.0',
  109. 'Content-Type' => 'text/plain; charset=UTF-8',
  110. 'Content-Transfer-Encoding' => '8bit',
  111. ];
  112. $this->assertSame($expected, $this->message->getHeaders());
  113. $this->message->addHeaders(['X-Something' => 'very nice', 'X-Other' => 'cool']);
  114. $expected = [
  115. 'X-Something' => 'very nice',
  116. 'X-Other' => 'cool',
  117. 'Date' => date(DATE_RFC2822),
  118. 'MIME-Version' => '1.0',
  119. 'Content-Type' => 'text/plain; charset=UTF-8',
  120. 'Content-Transfer-Encoding' => '8bit',
  121. ];
  122. $this->assertSame($expected, $this->message->getHeaders());
  123. $this->message->setFrom('cake@cakephp.org');
  124. $this->assertSame($expected, $this->message->getHeaders());
  125. $expected = [
  126. 'From' => 'cake@cakephp.org',
  127. 'X-Something' => 'very nice',
  128. 'X-Other' => 'cool',
  129. 'Date' => date(DATE_RFC2822),
  130. 'MIME-Version' => '1.0',
  131. 'Content-Type' => 'text/plain; charset=UTF-8',
  132. 'Content-Transfer-Encoding' => '8bit',
  133. ];
  134. $this->assertSame($expected, $this->message->getHeaders(['from' => true]));
  135. $this->message->setFrom('cake@cakephp.org', 'CakePHP');
  136. $expected['From'] = 'CakePHP <cake@cakephp.org>';
  137. $this->assertSame($expected, $this->message->getHeaders(['from' => true]));
  138. $this->message->setTo(['cake@cakephp.org', 'php@cakephp.org' => 'CakePHP']);
  139. $expected = [
  140. 'From' => 'CakePHP <cake@cakephp.org>',
  141. 'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
  142. 'X-Something' => 'very nice',
  143. 'X-Other' => 'cool',
  144. 'Date' => date(DATE_RFC2822),
  145. 'MIME-Version' => '1.0',
  146. 'Content-Type' => 'text/plain; charset=UTF-8',
  147. 'Content-Transfer-Encoding' => '8bit',
  148. ];
  149. $this->assertSame($expected, $this->message->getHeaders(['from' => true, 'to' => true]));
  150. $this->message->setCharset('ISO-2022-JP');
  151. $expected = [
  152. 'From' => 'CakePHP <cake@cakephp.org>',
  153. 'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
  154. 'X-Something' => 'very nice',
  155. 'X-Other' => 'cool',
  156. 'Date' => date(DATE_RFC2822),
  157. 'MIME-Version' => '1.0',
  158. 'Content-Type' => 'text/plain; charset=ISO-2022-JP',
  159. 'Content-Transfer-Encoding' => '7bit',
  160. ];
  161. $this->assertSame($expected, $this->message->getHeaders(['from' => true, 'to' => true]));
  162. $result = $this->message->setHeaders([]);
  163. $this->assertInstanceOf(Message::class, $result);
  164. $this->message->setHeaders(['o:tag' => ['foo']]);
  165. $this->message->addHeaders(['o:tag' => ['bar']]);
  166. $result = $this->message->getHeaders();
  167. $this->assertEquals(['foo', 'bar'], $result['o:tag']);
  168. }
  169. /**
  170. * testHeadersString method
  171. *
  172. * @return void
  173. */
  174. public function testHeadersString()
  175. {
  176. $this->message->setMessageId(false);
  177. $this->message->setHeaders(['X-Something' => 'nice']);
  178. $expected = [
  179. 'X-Something: nice',
  180. 'Date: ' . date(DATE_RFC2822),
  181. 'MIME-Version: 1.0',
  182. 'Content-Type: text/plain; charset=UTF-8',
  183. 'Content-Transfer-Encoding: 8bit',
  184. ];
  185. $this->assertSame(implode("\r\n", $expected), $this->message->getHeadersString());
  186. }
  187. }