MimeTest.php 6.3 KB

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