QpContentEncoderAcceptanceTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Mime/ContentEncoder/QpContentEncoder.php';
  4. require_once 'Swift/CharacterStream/ArrayCharacterStream.php';
  5. require_once 'Swift/CharacterReaderFactory/SimpleCharacterReaderFactory.php';
  6. require_once 'Swift/ByteStream/ArrayByteStream.php';
  7. class Swift_Mime_ContentEncoder_QpContentEncoderAcceptanceTest
  8. extends Swift_Tests_SwiftUnitTestCase
  9. {
  10. private $_samplesDir;
  11. private $_factory;
  12. public function setUp()
  13. {
  14. $this->_samplesDir = realpath(dirname(__FILE__) . '/../../../../_samples/charsets');
  15. $this->_factory = new Swift_CharacterReaderFactory_SimpleCharacterReaderFactory();
  16. }
  17. public function testEncodingAndDecodingSamples()
  18. {
  19. $sampleFp = opendir($this->_samplesDir);
  20. while (false !== $encodingDir = readdir($sampleFp))
  21. {
  22. if (substr($encodingDir, 0, 1) == '.')
  23. {
  24. continue;
  25. }
  26. $encoding = $encodingDir;
  27. $charStream = new Swift_CharacterStream_NgCharacterStream(
  28. $this->_factory, $encoding);
  29. $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
  30. $sampleDir = $this->_samplesDir . '/' . $encodingDir;
  31. if (is_dir($sampleDir))
  32. {
  33. $fileFp = opendir($sampleDir);
  34. while (false !== $sampleFile = readdir($fileFp))
  35. {
  36. if (substr($sampleFile, 0, 1) == '.')
  37. {
  38. continue;
  39. }
  40. $text = file_get_contents($sampleDir . '/' . $sampleFile);
  41. $os = new Swift_ByteStream_ArrayByteStream();
  42. $os->write($text);
  43. $is = new Swift_ByteStream_ArrayByteStream();
  44. $encoder->encodeByteStream($os, $is);
  45. $encoded = '';
  46. while (false !== $bytes = $is->read(8192))
  47. {
  48. $encoded .= $bytes;
  49. }
  50. $this->assertEqual(
  51. quoted_printable_decode($encoded), $text,
  52. '%s: Encoded string should decode back to original string for sample ' .
  53. $sampleDir . '/' . $sampleFile
  54. );
  55. }
  56. closedir($fileFp);
  57. }
  58. }
  59. closedir($sampleFp);
  60. }
  61. public function testEncodingAndDecodingSamplesFromDiConfiguredInstance()
  62. {
  63. $sampleFp = opendir($this->_samplesDir);
  64. while (false !== $encodingDir = readdir($sampleFp))
  65. {
  66. if (substr($encodingDir, 0, 1) == '.')
  67. {
  68. continue;
  69. }
  70. $encoding = $encodingDir;
  71. $encoder = $this->_createEncoderFromContainer();
  72. $sampleDir = $this->_samplesDir . '/' . $encodingDir;
  73. if (is_dir($sampleDir))
  74. {
  75. $fileFp = opendir($sampleDir);
  76. while (false !== $sampleFile = readdir($fileFp))
  77. {
  78. if (substr($sampleFile, 0, 1) == '.')
  79. {
  80. continue;
  81. }
  82. $text = file_get_contents($sampleDir . '/' . $sampleFile);
  83. $os = new Swift_ByteStream_ArrayByteStream();
  84. $os->write($text);
  85. $is = new Swift_ByteStream_ArrayByteStream();
  86. $encoder->encodeByteStream($os, $is);
  87. $encoded = '';
  88. while (false !== $bytes = $is->read(8192))
  89. {
  90. $encoded .= $bytes;
  91. }
  92. $this->assertEqual(
  93. str_replace("\r\n", "\n", quoted_printable_decode($encoded)), str_replace("\r\n", "\n", $text),
  94. '%s: Encoded string should decode back to original string for sample ' .
  95. $sampleDir . '/' . $sampleFile
  96. );
  97. }
  98. closedir($fileFp);
  99. }
  100. }
  101. closedir($sampleFp);
  102. }
  103. public function testEncodingLFTextWithDiConfiguredInstance()
  104. {
  105. $encoder = $this->_createEncoderFromContainer();
  106. $this->assertEqual("a\r\nb\r\nc", $encoder->encodeString("a\nb\nc"));
  107. }
  108. public function testEncodingCRTextWithDiConfiguredInstance()
  109. {
  110. $encoder = $this->_createEncoderFromContainer();
  111. $this->assertEqual("a\r\nb\r\nc", $encoder->encodeString("a\rb\rc"));
  112. }
  113. public function testEncodingLFCRTextWithDiConfiguredInstance()
  114. {
  115. $encoder = $this->_createEncoderFromContainer();
  116. $this->assertEqual("a\r\n\r\nb\r\n\r\nc", $encoder->encodeString("a\n\rb\n\rc"));
  117. }
  118. public function testEncodingCRLFTextWithDiConfiguredInstance()
  119. {
  120. $encoder = $this->_createEncoderFromContainer();
  121. $this->assertEqual("a\r\nb\r\nc", $encoder->encodeString("a\r\nb\r\nc"));
  122. }
  123. public function testEncodingDotStuffingWithDiConfiguredInstance()
  124. {
  125. // Enable DotEscaping
  126. Swift_Preferences::getInstance()->setQPDotEscape(true);
  127. $encoder = $this->_createEncoderFromContainer();
  128. $this->assertEqual("a=2E\r\n=2E\r\n=2Eb\r\nc", $encoder->encodeString("a.\r\n.\r\n.b\r\nc"));
  129. // Return to default
  130. Swift_Preferences::getInstance()->setQPDotEscape(false);
  131. $encoder = $this->_createEncoderFromContainer();
  132. $this->assertEqual("a.\r\n.\r\n.b\r\nc", $encoder->encodeString("a.\r\n.\r\n.b\r\nc"));
  133. }
  134. public function testDotStuffingEncodingAndDecodingSamplesFromDiConfiguredInstance()
  135. {
  136. // Enable DotEscaping
  137. Swift_Preferences::getInstance()->setQPDotEscape(true);
  138. $this->testEncodingAndDecodingSamplesFromDiConfiguredInstance();
  139. // Disable DotStuffing to continue
  140. Swift_Preferences::getInstance()->setQPDotEscape(false);
  141. }
  142. // -- Private Methods
  143. private function _createEncoderFromContainer()
  144. {
  145. return Swift_DependencyContainer::getInstance()
  146. ->lookup('mime.qpcontentencoder')
  147. ;
  148. }
  149. }