Bug38Test.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. class Swift_Bug38Test extends Swift_Tests_SwiftUnitTestCase
  4. {
  5. private $_attFile;
  6. private $_attFileName;
  7. private $_attFileType;
  8. public function setUp()
  9. {
  10. $this->_attFileName = 'data.txt';
  11. $this->_attFileType = 'text/plain';
  12. $this->_attFile = dirname(__FILE__) . '/../../_samples/files/data.txt';
  13. Swift_Preferences::getInstance()->setCharset('utf-8');
  14. }
  15. public function testWritingMessageToByteStreamProducesCorrectStructure()
  16. {
  17. $message = new Swift_Message();
  18. $message->setSubject('test subject');
  19. $message->setTo('user@domain.tld');
  20. $message->setCc('other@domain.tld');
  21. $message->setFrom('user@domain.tld');
  22. $image = new Swift_Image('<data>', 'image.gif', 'image/gif');
  23. $cid = $message->embed($image);
  24. $message->setBody('HTML part', 'text/html');
  25. $id = $message->getId();
  26. $date = preg_quote(date('r', $message->getDate()), '~');
  27. $boundary = $message->getBoundary();
  28. $imgId = $image->getId();
  29. $stream = new Swift_ByteStream_ArrayByteStream();
  30. $message->toByteStream($stream);
  31. $this->assertPatternInStream(
  32. '~^' .
  33. 'Message-ID: <' . $id . '>' . "\r\n" .
  34. 'Date: ' . $date . "\r\n" .
  35. 'Subject: test subject' . "\r\n" .
  36. 'From: user@domain.tld' . "\r\n" .
  37. 'To: user@domain.tld' . "\r\n" .
  38. 'Cc: other@domain.tld' . "\r\n" .
  39. 'MIME-Version: 1.0' . "\r\n" .
  40. 'Content-Type: multipart/related;' . "\r\n" .
  41. ' boundary="' . $boundary . '"' . "\r\n" .
  42. "\r\n\r\n" .
  43. '--' . $boundary . "\r\n" .
  44. 'Content-Type: text/html; charset=utf-8' . "\r\n" .
  45. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  46. "\r\n" .
  47. 'HTML part' .
  48. "\r\n\r\n" .
  49. '--' . $boundary . "\r\n" .
  50. 'Content-Type: image/gif; name=image.gif' . "\r\n" .
  51. 'Content-Transfer-Encoding: base64' . "\r\n" .
  52. 'Content-Disposition: inline; filename=image.gif' . "\r\n" .
  53. 'Content-ID: <' . preg_quote($imgId, '~') . '>' . "\r\n" .
  54. "\r\n" .
  55. preg_quote(base64_encode('<data>'), '~') .
  56. "\r\n\r\n" .
  57. '--' . $boundary . '--' . "\r\n" .
  58. '$~D',
  59. $stream
  60. );
  61. }
  62. public function testWritingMessageToByteStreamTwiceProducesCorrectStructure()
  63. {
  64. $message = new Swift_Message();
  65. $message->setSubject('test subject');
  66. $message->setTo('user@domain.tld');
  67. $message->setCc('other@domain.tld');
  68. $message->setFrom('user@domain.tld');
  69. $image = new Swift_Image('<data>', 'image.gif', 'image/gif');
  70. $cid = $message->embed($image);
  71. $message->setBody('HTML part', 'text/html');
  72. $id = $message->getId();
  73. $date = preg_quote(date('r', $message->getDate()), '~');
  74. $boundary = $message->getBoundary();
  75. $imgId = $image->getId();
  76. $pattern = '~^' .
  77. 'Message-ID: <' . $id . '>' . "\r\n" .
  78. 'Date: ' . $date . "\r\n" .
  79. 'Subject: test subject' . "\r\n" .
  80. 'From: user@domain.tld' . "\r\n" .
  81. 'To: user@domain.tld' . "\r\n" .
  82. 'Cc: other@domain.tld' . "\r\n" .
  83. 'MIME-Version: 1.0' . "\r\n" .
  84. 'Content-Type: multipart/related;' . "\r\n" .
  85. ' boundary="' . $boundary . '"' . "\r\n" .
  86. "\r\n\r\n" .
  87. '--' . $boundary . "\r\n" .
  88. 'Content-Type: text/html; charset=utf-8' . "\r\n" .
  89. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  90. "\r\n" .
  91. 'HTML part' .
  92. "\r\n\r\n" .
  93. '--' . $boundary . "\r\n" .
  94. 'Content-Type: image/gif; name=image.gif' . "\r\n" .
  95. 'Content-Transfer-Encoding: base64' . "\r\n" .
  96. 'Content-Disposition: inline; filename=image.gif' . "\r\n" .
  97. 'Content-ID: <' . preg_quote($imgId, '~') . '>' . "\r\n" .
  98. "\r\n" .
  99. preg_quote(base64_encode('<data>'), '~') .
  100. "\r\n\r\n" .
  101. '--' . $boundary . '--' . "\r\n" .
  102. '$~D'
  103. ;
  104. $streamA = new Swift_ByteStream_ArrayByteStream();
  105. $streamB = new Swift_ByteStream_ArrayByteStream();
  106. $message->toByteStream($streamA);
  107. $message->toByteStream($streamB);
  108. $this->assertPatternInStream($pattern, $streamA);
  109. $this->assertPatternInStream($pattern, $streamB);
  110. }
  111. public function testWritingMessageToByteStreamTwiceUsingAFileAttachment()
  112. {
  113. $message = new Swift_Message();
  114. $message->setSubject('test subject');
  115. $message->setTo('user@domain.tld');
  116. $message->setCc('other@domain.tld');
  117. $message->setFrom('user@domain.tld');
  118. $attachment = Swift_Attachment::fromPath($this->_attFile);
  119. $message->attach($attachment);
  120. $message->setBody('HTML part', 'text/html');
  121. $id = $message->getId();
  122. $date = preg_quote(date('r', $message->getDate()), '~');
  123. $boundary = $message->getBoundary();
  124. $streamA = new Swift_ByteStream_ArrayByteStream();
  125. $streamB = new Swift_ByteStream_ArrayByteStream();
  126. $pattern = '~^' .
  127. 'Message-ID: <' . $id . '>' . "\r\n" .
  128. 'Date: ' . $date . "\r\n" .
  129. 'Subject: test subject' . "\r\n" .
  130. 'From: user@domain.tld' . "\r\n" .
  131. 'To: user@domain.tld' . "\r\n" .
  132. 'Cc: other@domain.tld' . "\r\n" .
  133. 'MIME-Version: 1.0' . "\r\n" .
  134. 'Content-Type: multipart/mixed;' . "\r\n" .
  135. ' boundary="' . $boundary . '"' . "\r\n" .
  136. "\r\n\r\n" .
  137. '--' . $boundary . "\r\n" .
  138. 'Content-Type: text/html; charset=utf-8' . "\r\n" .
  139. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  140. "\r\n" .
  141. 'HTML part' .
  142. "\r\n\r\n" .
  143. '--' . $boundary . "\r\n" .
  144. 'Content-Type: ' . $this->_attFileType . '; name=' . $this->_attFileName . "\r\n" .
  145. 'Content-Transfer-Encoding: base64' . "\r\n" .
  146. 'Content-Disposition: attachment; filename=' . $this->_attFileName . "\r\n" .
  147. "\r\n" .
  148. preg_quote(base64_encode(file_get_contents($this->_attFile)), '~') .
  149. "\r\n\r\n" .
  150. '--' . $boundary . '--' . "\r\n" .
  151. '$~D'
  152. ;
  153. $message->toByteStream($streamA);
  154. $message->toByteStream($streamB);
  155. $this->assertPatternInStream($pattern, $streamA);
  156. $this->assertPatternInStream($pattern, $streamB);
  157. }
  158. // -- Helpers
  159. public function assertPatternInStream($pattern, $stream, $message = '%s')
  160. {
  161. $string = '';
  162. while (false !== $bytes = $stream->read(8192))
  163. {
  164. $string .= $bytes;
  165. }
  166. $this->assertPattern($pattern, $string, $message);
  167. }
  168. }