PartTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /**
  23. * Zend_Mime_Part
  24. */
  25. require_once 'Zend/Mime/Part.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_Mime_PartTest extends PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * MIME part test object
  38. *
  39. * @var Zend_Mime_Part
  40. */
  41. protected $part = null;
  42. protected $testText;
  43. protected function setUp()
  44. {
  45. $this->testText = 'safdsafsa�lg ��gd�� sd�jg�sdjg�ld�gksd�gj�sdfg�dsj�gjsd�gj�dfsjg�dsfj�djs�g kjhdkj '
  46. . 'fgaskjfdh gksjhgjkdh gjhfsdghdhgksdjhg';
  47. $this->part = new Zend_Mime_Part($this->testText);
  48. $this->part->encoding = Zend_Mime::ENCODING_BASE64;
  49. $this->part->type = "text/plain";
  50. $this->part->filename = 'test.txt';
  51. $this->part->disposition = 'attachment';
  52. $this->part->charset = 'iso8859-1';
  53. $this->part->id = '4711';
  54. }
  55. public function testHeaders()
  56. {
  57. $expectedHeaders = array('Content-Type: text/plain',
  58. 'Content-Transfer-Encoding: ' . Zend_Mime::ENCODING_BASE64,
  59. 'Content-Disposition: attachment',
  60. 'filename="test.txt"',
  61. 'charset=iso8859-1',
  62. 'Content-ID: <4711>');
  63. $actual = $this->part->getHeaders();
  64. foreach ($expectedHeaders as $expected) {
  65. $this->assertContains($expected, $actual);
  66. }
  67. }
  68. public function testContentEncoding()
  69. {
  70. // Test with base64 encoding
  71. $content = $this->part->getContent();
  72. $this->assertEquals($this->testText, base64_decode($content));
  73. // Test with quotedPrintable Encoding:
  74. $this->part->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE;
  75. $content = $this->part->getContent();
  76. $this->assertEquals($this->testText, quoted_printable_decode($content));
  77. // Test with 8Bit encoding
  78. $this->part->encoding = Zend_Mime::ENCODING_8BIT;
  79. $content = $this->part->getContent();
  80. $this->assertEquals($this->testText, $content);
  81. }
  82. public function testStreamEncoding()
  83. {
  84. $testfile = realpath(__FILE__);
  85. $original = file_get_contents($testfile);
  86. // Test Base64
  87. $fp = fopen($testfile,'rb');
  88. $this->assertTrue(is_resource($fp));
  89. $part = new Zend_Mime_Part($fp);
  90. $part->encoding = Zend_Mime::ENCODING_BASE64;
  91. $fp2 = $part->getEncodedStream();
  92. $this->assertTrue(is_resource($fp2));
  93. $encoded = stream_get_contents($fp2);
  94. fclose($fp);
  95. $this->assertEquals(base64_decode($encoded),$original);
  96. // test QuotedPrintable
  97. $fp = fopen($testfile,'rb');
  98. $this->assertTrue(is_resource($fp));
  99. $part = new Zend_Mime_Part($fp);
  100. $part->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE;
  101. $fp2 = $part->getEncodedStream();
  102. $this->assertTrue(is_resource($fp2));
  103. $encoded = stream_get_contents($fp2);
  104. fclose($fp);
  105. $this->assertEquals(quoted_printable_decode($encoded),$original);
  106. }
  107. /**
  108. * @group ZF-1491
  109. */
  110. public function testGetRawContentFromPart()
  111. {
  112. $this->assertEquals($this->testText, $this->part->getRawContent());
  113. }
  114. }