2
0

MimeTest.php 6.4 KB

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