MimePartTest.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. require_once 'Swift/Mime/MimeEntity.php';
  3. require_once 'Swift/Mime/MimePart.php';
  4. require_once 'Swift/Mime/AbstractMimeEntityTest.php';
  5. require_once 'Swift/Mime/Grammar.php';
  6. class Swift_Mime_MimePartTest extends Swift_Mime_AbstractMimeEntityTest
  7. {
  8. public function testNestingLevelIsSubpart()
  9. {
  10. $part = $this->_createMimePart($this->_createHeaderSet(),
  11. $this->_createEncoder(), $this->_createCache()
  12. );
  13. $this->assertEqual(
  14. Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE, $part->getNestingLevel()
  15. );
  16. }
  17. public function testCharsetIsReturnedFromHeader()
  18. {
  19. /* -- RFC 2046, 4.1.2.
  20. A critical parameter that may be specified in the Content-Type field
  21. for "text/plain" data is the character set. This is specified with a
  22. "charset" parameter, as in:
  23. Content-type: text/plain; charset=iso-8859-1
  24. Unlike some other parameter values, the values of the charset
  25. parameter are NOT case sensitive. The default character set, which
  26. must be assumed in the absence of a charset parameter, is US-ASCII.
  27. */
  28. $cType = $this->_createHeader('Content-Type', 'text/plain',
  29. array('charset' => 'iso-8859-1')
  30. );
  31. $part = $this->_createMimePart($this->_createHeaderSet(array(
  32. 'Content-Type' => $cType)),
  33. $this->_createEncoder(), $this->_createCache()
  34. );
  35. $this->assertEqual('iso-8859-1', $part->getCharset());
  36. }
  37. public function testCharsetIsSetInHeader()
  38. {
  39. $cType = $this->_createHeader('Content-Type', 'text/plain',
  40. array('charset' => 'iso-8859-1'), false
  41. );
  42. $this->_checking(Expectations::create()
  43. -> one($cType)->setParameter('charset', 'utf-8')
  44. -> ignoring($cType)
  45. );
  46. $part = $this->_createMimePart($this->_createHeaderSet(array(
  47. 'Content-Type' => $cType)),
  48. $this->_createEncoder(), $this->_createCache()
  49. );
  50. $part->setCharset('utf-8');
  51. }
  52. public function testCharsetIsSetInHeaderIfPassedToSetBody()
  53. {
  54. $cType = $this->_createHeader('Content-Type', 'text/plain',
  55. array('charset' => 'iso-8859-1'), false
  56. );
  57. $this->_checking(Expectations::create()
  58. -> one($cType)->setParameter('charset', 'utf-8')
  59. -> ignoring($cType)
  60. );
  61. $part = $this->_createMimePart($this->_createHeaderSet(array(
  62. 'Content-Type' => $cType)),
  63. $this->_createEncoder(), $this->_createCache()
  64. );
  65. $part->setBody('', 'text/plian', 'utf-8');
  66. }
  67. public function testSettingCharsetNotifiesEncoder()
  68. {
  69. $encoder = $this->_createEncoder('quoted-printable', false);
  70. $this->_checking(Expectations::create()
  71. -> one($encoder)->charsetChanged('utf-8')
  72. -> ignoring($encoder)
  73. );
  74. $part = $this->_createMimePart($this->_createHeaderSet(),
  75. $encoder, $this->_createCache()
  76. );
  77. $part->setCharset('utf-8');
  78. }
  79. public function testSettingCharsetNotifiesHeaders()
  80. {
  81. $headers = $this->_createHeaderSet(array(), false);
  82. $this->_checking(Expectations::create()
  83. -> one($headers)->charsetChanged('utf-8')
  84. -> ignoring($headers)
  85. );
  86. $part = $this->_createMimePart($headers, $this->_createEncoder(),
  87. $this->_createCache()
  88. );
  89. $part->setCharset('utf-8');
  90. }
  91. public function testSettingCharsetNotifiesChildren()
  92. {
  93. $child = $this->_createChild(0, '', false);
  94. $this->_checking(Expectations::create()
  95. -> one($child)->charsetChanged('windows-874')
  96. -> ignoring($child)
  97. );
  98. $part = $this->_createMimePart($this->_createHeaderSet(),
  99. $this->_createEncoder(), $this->_createCache()
  100. );
  101. $part->setChildren(array($child));
  102. $part->setCharset('windows-874');
  103. }
  104. public function testCharsetChangeUpdatesCharset()
  105. {
  106. $cType = $this->_createHeader('Content-Type', 'text/plain',
  107. array('charset' => 'iso-8859-1'), false
  108. );
  109. $this->_checking(Expectations::create()
  110. -> one($cType)->setParameter('charset', 'utf-8')
  111. -> ignoring($cType)
  112. );
  113. $part = $this->_createMimePart($this->_createHeaderSet(array(
  114. 'Content-Type' => $cType)),
  115. $this->_createEncoder(), $this->_createCache()
  116. );
  117. $part->charsetChanged('utf-8');
  118. }
  119. public function testSettingCharsetClearsCache()
  120. {
  121. $headers = $this->_createHeaderSet(array(), false);
  122. $this->_checking(Expectations::create()
  123. -> ignoring($headers)->toString() -> returns(
  124. "Content-Type: text/plain; charset=utf-8\r\n"
  125. )
  126. -> ignoring($headers)
  127. );
  128. $cache = $this->_createCache(false);
  129. $this->_checking(Expectations::create()
  130. -> one($cache)->clearKey(any(), 'body')
  131. -> ignoring($cache)
  132. );
  133. $entity = $this->_createEntity($headers, $this->_createEncoder(),
  134. $cache
  135. );
  136. $entity->setBody("blah\r\nblah!");
  137. $entity->toString();
  138. $entity->setCharset('iso-2022');
  139. }
  140. public function testFormatIsReturnedFromHeader()
  141. {
  142. /* -- RFC 3676.
  143. */
  144. $cType = $this->_createHeader('Content-Type', 'text/plain',
  145. array('format' => 'flowed')
  146. );
  147. $part = $this->_createMimePart($this->_createHeaderSet(array(
  148. 'Content-Type' => $cType)),
  149. $this->_createEncoder(), $this->_createCache()
  150. );
  151. $this->assertEqual('flowed', $part->getFormat());
  152. }
  153. public function testFormatIsSetInHeader()
  154. {
  155. $cType = $this->_createHeader('Content-Type', 'text/plain', array(), false);
  156. $this->_checking(Expectations::create()
  157. -> one($cType)->setParameter('format', 'fixed')
  158. -> ignoring($cType)
  159. );
  160. $part = $this->_createMimePart($this->_createHeaderSet(array(
  161. 'Content-Type' => $cType)),
  162. $this->_createEncoder(), $this->_createCache()
  163. );
  164. $part->setFormat('fixed');
  165. }
  166. public function testDelSpIsReturnedFromHeader()
  167. {
  168. /* -- RFC 3676.
  169. */
  170. $cType = $this->_createHeader('Content-Type', 'text/plain',
  171. array('delsp' => 'no')
  172. );
  173. $part = $this->_createMimePart($this->_createHeaderSet(array(
  174. 'Content-Type' => $cType)),
  175. $this->_createEncoder(), $this->_createCache()
  176. );
  177. $this->assertIdentical(false, $part->getDelSp());
  178. }
  179. public function testDelSpIsSetInHeader()
  180. {
  181. $cType = $this->_createHeader('Content-Type', 'text/plain', array(), false);
  182. $this->_checking(Expectations::create()
  183. -> one($cType)->setParameter('delsp', 'yes')
  184. -> ignoring($cType)
  185. );
  186. $part = $this->_createMimePart($this->_createHeaderSet(array(
  187. 'Content-Type' => $cType)),
  188. $this->_createEncoder(), $this->_createCache()
  189. );
  190. $part->setDelSp(true);
  191. }
  192. public function testFluidInterface()
  193. {
  194. $part = $this->_createMimePart($this->_createHeaderSet(),
  195. $this->_createEncoder(), $this->_createCache()
  196. );
  197. $this->assertSame($part,
  198. $part
  199. ->setContentType('text/plain')
  200. ->setEncoder($this->_createEncoder())
  201. ->setId('foo@bar')
  202. ->setDescription('my description')
  203. ->setMaxLineLength(998)
  204. ->setBody('xx')
  205. ->setBoundary('xyz')
  206. ->setChildren(array())
  207. ->setCharset('utf-8')
  208. ->setFormat('flowed')
  209. ->setDelSp(true)
  210. );
  211. }
  212. // -- Private helpers
  213. //abstract
  214. protected function _createEntity($headers, $encoder, $cache)
  215. {
  216. return $this->_createMimePart($headers, $encoder, $cache);
  217. }
  218. protected function _createMimePart($headers, $encoder, $cache)
  219. {
  220. return new Swift_Mime_MimePart($headers, $encoder, $cache, new Swift_Mime_Grammar());
  221. }
  222. }