MimeTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mime
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. /** Zend_Mail */
  23. require_once 'Zend/Mail.php';
  24. /** Zend_Mime */
  25. require_once 'Zend/Mime.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Mime
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Mime
  33. */
  34. class Zend_MimeTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function testBoundary()
  37. {
  38. // check boundary for uniqueness
  39. $m1 = new Zend_Mime();
  40. $m2 = new Zend_Mime();
  41. $this->assertNotEquals($m1->boundary(), $m2->boundary());
  42. // check instantiating with arbitrary boundary string
  43. $myBoundary = 'mySpecificBoundary';
  44. $m3 = new Zend_Mime($myBoundary);
  45. $this->assertEquals($m3->boundary(), $myBoundary);
  46. }
  47. public function testIsPrintable_notPrintable()
  48. {
  49. $this->assertFalse(Zend_Mime::isPrintable('Test with special chars: �����'));
  50. }
  51. public function testIsPrintable_isPrintable()
  52. {
  53. $this->assertTrue(Zend_Mime::isPrintable('Test without special chars'));
  54. }
  55. public function testQP()
  56. {
  57. $text = "This is a cool Test Text with special chars: ����\n"
  58. . "and with multiple lines���� some of the Lines are long, long"
  59. . ", long, long, long, long, long, long, long, long, long, long"
  60. . ", long, long, long, long, long, long, long, long, long, long"
  61. . ", long, long, long, long, long, long, long, long, long, long"
  62. . ", long, long, long, long and with ����";
  63. $qp = Zend_Mime::encodeQuotedPrintable($text);
  64. $this->assertEquals(quoted_printable_decode($qp), $text);
  65. }
  66. /**
  67. * @group ZF-10074
  68. */
  69. public function testEncodeQuotedPrintableWhenTextHasZeroAtTheEnd()
  70. {
  71. $raw = str_repeat('x',72) . '0';
  72. $quoted = Zend_Mime::encodeQuotedPrintable($raw, 72);
  73. $expected = quoted_printable_decode($quoted);
  74. $this->assertEquals($expected, $raw);
  75. }
  76. public function testBase64()
  77. {
  78. $content = str_repeat("\x88\xAA\xAF\xBF\x29\x88\xAA\xAF\xBF\x29\x88\xAA\xAF", 4);
  79. $encoded = Zend_Mime::encodeBase64($content);
  80. $this->assertEquals($content, base64_decode($encoded));
  81. }
  82. public function testZf1058WhitespaceAtEndOfBodyCausesInfiniteLoop()
  83. {
  84. $mail = new Zend_Mail();
  85. $mail->setSubject('my subject');
  86. $mail->setBodyText("my body\r\n\r\n...after two newlines\r\n ");
  87. $mail->setFrom('test@email.com');
  88. $mail->addTo('test@email.com');
  89. // test with generic transport
  90. require_once 'Mail/MailTest.php';
  91. $mock = new Zend_Mail_Transport_Sendmail_Mock();
  92. $mail->send($mock);
  93. $body = quoted_printable_decode($mock->body);
  94. $this->assertContains("my body\r\n\r\n...after two newlines", $body, $body);
  95. }
  96. /**
  97. * @group ZF-1688
  98. * @dataProvider dataTestEncodeMailHeaderQuotedPrintable
  99. */
  100. public function testEncodeMailHeaderQuotedPrintable($str, $charset, $result)
  101. {
  102. $this->assertEquals($result, Zend_Mime::encodeQuotedPrintableHeader($str, $charset));
  103. }
  104. public static function dataTestEncodeMailHeaderQuotedPrintable()
  105. {
  106. return array(
  107. array("äöü", "UTF-8", "=?UTF-8?Q?=C3=A4=C3=B6=C3=BC?="),
  108. array("äöü ", "UTF-8", "=?UTF-8?Q?=C3=A4=C3=B6=C3=BC?="),
  109. array("Gimme more €", "UTF-8", "=?UTF-8?Q?Gimme=20more=20=E2=82=AC?="),
  110. array("Alle meine Entchen schwimmen in dem See, schwimmen in dem See, Köpfchen in das Wasser, Schwänzchen in die Höh!", "UTF-8", "=?UTF-8?Q?Alle=20meine=20Entchen=20schwimmen=20in=20dem=20See=2C=20?=
  111. =?UTF-8?Q?schwimmen=20in=20dem=20See=2C=20K=C3=B6pfchen=20in=20das=20?=
  112. =?UTF-8?Q?Wasser=2C=20Schw=C3=A4nzchen=20in=20die=20H=C3=B6h!?="),
  113. array("ääääääääääääääääääääääääääääääääää", "UTF-8", "=?UTF-8?Q?=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4=C3=A4?="),
  114. );
  115. }
  116. /**
  117. * @group ZF-1688
  118. * @dataProvider dataTestEncodeMailHeaderBase64
  119. */
  120. public function testEncodeMailHeaderBase64($str, $charset, $result)
  121. {
  122. $this->assertEquals($result, Zend_Mime::encodeBase64Header($str, $charset));
  123. }
  124. public static function dataTestEncodeMailHeaderBase64()
  125. {
  126. return array(
  127. array("äöü", "UTF-8", "=?UTF-8?B?w6TDtsO8?="),
  128. array("Alle meine Entchen schwimmen in dem See, schwimmen in dem See, Köpfchen in das Wasser, Schwänzchen in die Höh!", "UTF-8", "=?UTF-8?B?QWxsZSBtZWluZSBFbnRjaGVuIHNjaHdpbW1lbiBpbiBkZW0gU2VlLCBzY2h3?=
  129. =?UTF-8?B?aW1tZW4gaW4gZGVtIFNlZSwgS8O2cGZjaGVuIGluIGRhcyBXYXNzZXIsIFNj?=
  130. =?UTF-8?B?aHfDpG56Y2hlbiBpbiBkaWUgSMO2aCE=?="),
  131. );
  132. }
  133. /**
  134. * @group ZF-1688
  135. */
  136. public function testLineLengthInQuotedPrintableHeaderEncoding()
  137. {
  138. $subject = "Alle meine Entchen schwimmen in dem See, schwimmen in dem See, Köpfchen in das Wasser, Schwänzchen in die Höh!";
  139. $encoded = Zend_Mime::encodeQuotedPrintableHeader($subject, "UTF-8", 100);
  140. foreach(explode(Zend_Mime::LINEEND, $encoded) AS $line ) {
  141. if(strlen($line) > 100) {
  142. $this->fail("Line '".$line."' is ".strlen($line)." chars long, only 100 allowed.");
  143. }
  144. }
  145. $encoded = Zend_Mime::encodeQuotedPrintableHeader($subject, "UTF-8", 40);
  146. foreach(explode(Zend_Mime::LINEEND, $encoded) AS $line ) {
  147. if(strlen($line) > 40) {
  148. $this->fail("Line '".$line."' is ".strlen($line)." chars long, only 40 allowed.");
  149. }
  150. }
  151. }
  152. }