EmbeddedFileAcceptanceTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. require_once 'Swift/Mime/EmbeddedFile.php';
  3. require_once 'Swift/Mime/Headers/UnstructuredHeader.php';
  4. require_once 'Swift/Mime/Headers/ParameterizedHeader.php';
  5. require_once 'Swift/Mime/Headers/IdentificationHeader.php';
  6. require_once 'Swift/Encoder/Rfc2231Encoder.php';
  7. require_once 'Swift/Mime/ContentEncoder/Base64ContentEncoder.php';
  8. require_once 'Swift/Mime/HeaderEncoder/QpHeaderEncoder.php';
  9. require_once 'Swift/CharacterStream/ArrayCharacterStream.php';
  10. require_once 'Swift/CharacterReaderFactory/SimpleCharacterReaderFactory.php';
  11. require_once 'Swift/KeyCache/ArrayKeyCache.php';
  12. require_once 'Swift/KeyCache/SimpleKeyCacheInputStream.php';
  13. require_once 'Swift/Mime/Grammar.php';
  14. class Swift_Mime_EmbeddedFileAcceptanceTest extends UnitTestCase
  15. {
  16. private $_contentEncoder;
  17. private $_cache;
  18. private $_grammar;
  19. private $_headers;
  20. public function setUp()
  21. {
  22. $this->_cache = new Swift_KeyCache_ArrayKeyCache(
  23. new Swift_KeyCache_SimpleKeyCacheInputStream()
  24. );
  25. $factory = new Swift_CharacterReaderFactory_SimpleCharacterReaderFactory();
  26. $this->_contentEncoder = new Swift_Mime_ContentEncoder_Base64ContentEncoder();
  27. $headerEncoder = new Swift_Mime_HeaderEncoder_QpHeaderEncoder(
  28. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8')
  29. );
  30. $paramEncoder = new Swift_Encoder_Rfc2231Encoder(
  31. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8')
  32. );
  33. $this->_grammar = new Swift_Mime_Grammar();
  34. $this->_headers = new Swift_Mime_SimpleHeaderSet(
  35. new Swift_Mime_SimpleHeaderFactory($headerEncoder, $paramEncoder, $this->_grammar)
  36. );
  37. }
  38. public function testContentIdIsSetInHeader()
  39. {
  40. $file = $this->_createEmbeddedFile();
  41. $file->setContentType('application/pdf');
  42. $file->setId('foo@bar');
  43. $this->assertEqual(
  44. 'Content-Type: application/pdf' . "\r\n" .
  45. 'Content-Transfer-Encoding: base64' . "\r\n" .
  46. 'Content-Disposition: inline' . "\r\n" .
  47. 'Content-ID: <foo@bar>' . "\r\n",
  48. $file->toString()
  49. );
  50. }
  51. public function testDispositionIsSetInHeader()
  52. {
  53. $file = $this->_createEmbeddedFile();
  54. $id = $file->getId();
  55. $file->setContentType('application/pdf');
  56. $file->setDisposition('attachment');
  57. $this->assertEqual(
  58. 'Content-Type: application/pdf' . "\r\n" .
  59. 'Content-Transfer-Encoding: base64' . "\r\n" .
  60. 'Content-Disposition: attachment' . "\r\n" .
  61. 'Content-ID: <'. $id . '>' . "\r\n",
  62. $file->toString()
  63. );
  64. }
  65. public function testFilenameIsSetInHeader()
  66. {
  67. $file = $this->_createEmbeddedFile();
  68. $id = $file->getId();
  69. $file->setContentType('application/pdf');
  70. $file->setFilename('foo.pdf');
  71. $this->assertEqual(
  72. 'Content-Type: application/pdf; name=foo.pdf' . "\r\n" .
  73. 'Content-Transfer-Encoding: base64' . "\r\n" .
  74. 'Content-Disposition: inline; filename=foo.pdf' . "\r\n" .
  75. 'Content-ID: <'. $id . '>' . "\r\n",
  76. $file->toString()
  77. );
  78. }
  79. public function testSizeIsSetInHeader()
  80. {
  81. $file = $this->_createEmbeddedFile();
  82. $id = $file->getId();
  83. $file->setContentType('application/pdf');
  84. $file->setSize(12340);
  85. $this->assertEqual(
  86. 'Content-Type: application/pdf' . "\r\n" .
  87. 'Content-Transfer-Encoding: base64' . "\r\n" .
  88. 'Content-Disposition: inline; size=12340' . "\r\n" .
  89. 'Content-ID: <'. $id . '>' . "\r\n",
  90. $file->toString()
  91. );
  92. }
  93. public function testMultipleParametersInHeader()
  94. {
  95. $file = $this->_createEmbeddedFile();
  96. $id = $file->getId();
  97. $file->setContentType('application/pdf');
  98. $file->setFilename('foo.pdf');
  99. $file->setSize(12340);
  100. $this->assertEqual(
  101. 'Content-Type: application/pdf; name=foo.pdf' . "\r\n" .
  102. 'Content-Transfer-Encoding: base64' . "\r\n" .
  103. 'Content-Disposition: inline; filename=foo.pdf; size=12340' . "\r\n" .
  104. 'Content-ID: <'. $id . '>' . "\r\n",
  105. $file->toString()
  106. );
  107. }
  108. public function testEndToEnd()
  109. {
  110. $file = $this->_createEmbeddedFile();
  111. $id = $file->getId();
  112. $file->setContentType('application/pdf');
  113. $file->setFilename('foo.pdf');
  114. $file->setSize(12340);
  115. $file->setBody('abcd');
  116. $this->assertEqual(
  117. 'Content-Type: application/pdf; name=foo.pdf' . "\r\n" .
  118. 'Content-Transfer-Encoding: base64' . "\r\n" .
  119. 'Content-Disposition: inline; filename=foo.pdf; size=12340' . "\r\n" .
  120. 'Content-ID: <'. $id . '>' . "\r\n" .
  121. "\r\n" .
  122. base64_encode('abcd'),
  123. $file->toString()
  124. );
  125. }
  126. // -- Private helpers
  127. protected function _createEmbeddedFile()
  128. {
  129. $entity = new Swift_Mime_EmbeddedFile(
  130. $this->_headers,
  131. $this->_contentEncoder,
  132. $this->_cache,
  133. $this->_grammar
  134. );
  135. return $entity;
  136. }
  137. }