MailboxHeaderTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Mime/Headers/MailboxHeader.php';
  4. require_once 'Swift/Mime/HeaderEncoder.php';
  5. require_once 'Swift/Mime/Grammar.php';
  6. class Swift_Mime_Headers_MailboxHeaderTest
  7. extends Swift_Tests_SwiftUnitTestCase
  8. {
  9. /* -- RFC 2822, 3.6.2 for all tests.
  10. */
  11. private $_charset = 'utf-8';
  12. public function testTypeIsMailboxHeader()
  13. {
  14. $header = $this->_getHeader('To', $this->_getEncoder('Q', true));
  15. $this->assertEqual(Swift_Mime_Header::TYPE_MAILBOX, $header->getFieldType());
  16. }
  17. public function testMailboxIsSetForAddress()
  18. {
  19. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  20. $header->setAddresses('chris@swiftmailer.org');
  21. $this->assertEqual(array('chris@swiftmailer.org'),
  22. $header->getNameAddressStrings()
  23. );
  24. }
  25. public function testMailboxIsRenderedForNameAddress()
  26. {
  27. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  28. $header->setNameAddresses(array('chris@swiftmailer.org' => 'Chris Corbyn'));
  29. $this->assertEqual(
  30. array('Chris Corbyn <chris@swiftmailer.org>'), $header->getNameAddressStrings()
  31. );
  32. }
  33. public function testAddressCanBeReturnedForAddress()
  34. {
  35. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  36. $header->setAddresses('chris@swiftmailer.org');
  37. $this->assertEqual(array('chris@swiftmailer.org'), $header->getAddresses());
  38. }
  39. public function testAddressCanBeReturnedForNameAddress()
  40. {
  41. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  42. $header->setNameAddresses(array('chris@swiftmailer.org' => 'Chris Corbyn'));
  43. $this->assertEqual(array('chris@swiftmailer.org'), $header->getAddresses());
  44. }
  45. public function testQuotesInNameAreQuoted()
  46. {
  47. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  48. $header->setNameAddresses(array(
  49. 'chris@swiftmailer.org' => 'Chris Corbyn, "DHE"'
  50. ));
  51. $this->assertEqual(
  52. array('"Chris Corbyn, \"DHE\"" <chris@swiftmailer.org>'),
  53. $header->getNameAddressStrings()
  54. );
  55. }
  56. public function testEscapeCharsInNameAreQuoted()
  57. {
  58. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  59. $header->setNameAddresses(array(
  60. 'chris@swiftmailer.org' => 'Chris Corbyn, \\escaped\\'
  61. ));
  62. $this->assertEqual(
  63. array('"Chris Corbyn, \\\\escaped\\\\" <chris@swiftmailer.org>'),
  64. $header->getNameAddressStrings()
  65. );
  66. }
  67. public function testGetMailboxesReturnsNameValuePairs()
  68. {
  69. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  70. $header->setNameAddresses(array(
  71. 'chris@swiftmailer.org' => 'Chris Corbyn, DHE'
  72. ));
  73. $this->assertEqual(
  74. array('chris@swiftmailer.org' => 'Chris Corbyn, DHE'), $header->getNameAddresses()
  75. );
  76. }
  77. public function testMultipleAddressesCanBeSetAndFetched()
  78. {
  79. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  80. $header->setAddresses(array(
  81. 'chris@swiftmailer.org', 'mark@swiftmailer.org'
  82. ));
  83. $this->assertEqual(
  84. array('chris@swiftmailer.org', 'mark@swiftmailer.org'),
  85. $header->getAddresses()
  86. );
  87. }
  88. public function testMultipleAddressesAsMailboxes()
  89. {
  90. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  91. $header->setAddresses(array(
  92. 'chris@swiftmailer.org', 'mark@swiftmailer.org'
  93. ));
  94. $this->assertEqual(
  95. array('chris@swiftmailer.org'=>null, 'mark@swiftmailer.org'=>null),
  96. $header->getNameAddresses()
  97. );
  98. }
  99. public function testMultipleAddressesAsMailboxStrings()
  100. {
  101. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  102. $header->setAddresses(array(
  103. 'chris@swiftmailer.org', 'mark@swiftmailer.org'
  104. ));
  105. $this->assertEqual(
  106. array('chris@swiftmailer.org', 'mark@swiftmailer.org'),
  107. $header->getNameAddressStrings()
  108. );
  109. }
  110. public function testMultipleNamedMailboxesReturnsMultipleAddresses()
  111. {
  112. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  113. $header->setNameAddresses(array(
  114. 'chris@swiftmailer.org' => 'Chris Corbyn',
  115. 'mark@swiftmailer.org' => 'Mark Corbyn'
  116. ));
  117. $this->assertEqual(
  118. array('chris@swiftmailer.org', 'mark@swiftmailer.org'),
  119. $header->getAddresses()
  120. );
  121. }
  122. public function testMultipleNamedMailboxesReturnsMultipleMailboxes()
  123. {
  124. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  125. $header->setNameAddresses(array(
  126. 'chris@swiftmailer.org' => 'Chris Corbyn',
  127. 'mark@swiftmailer.org' => 'Mark Corbyn'
  128. ));
  129. $this->assertEqual(array(
  130. 'chris@swiftmailer.org' => 'Chris Corbyn',
  131. 'mark@swiftmailer.org' => 'Mark Corbyn'
  132. ),
  133. $header->getNameAddresses()
  134. );
  135. }
  136. public function testMultipleMailboxesProducesMultipleMailboxStrings()
  137. {
  138. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  139. $header->setNameAddresses(array(
  140. 'chris@swiftmailer.org' => 'Chris Corbyn',
  141. 'mark@swiftmailer.org' => 'Mark Corbyn'
  142. ));
  143. $this->assertEqual(array(
  144. 'Chris Corbyn <chris@swiftmailer.org>',
  145. 'Mark Corbyn <mark@swiftmailer.org>'
  146. ),
  147. $header->getNameAddressStrings()
  148. );
  149. }
  150. public function testSetAddressesOverwritesAnyMailboxes()
  151. {
  152. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  153. $header->setNameAddresses(array(
  154. 'chris@swiftmailer.org' => 'Chris Corbyn',
  155. 'mark@swiftmailer.org' => 'Mark Corbyn'
  156. ));
  157. $this->assertEqual(
  158. array('chris@swiftmailer.org' => 'Chris Corbyn',
  159. 'mark@swiftmailer.org' => 'Mark Corbyn'),
  160. $header->getNameAddresses()
  161. );
  162. $this->assertEqual(
  163. array('chris@swiftmailer.org', 'mark@swiftmailer.org'),
  164. $header->getAddresses()
  165. );
  166. $header->setAddresses(array('chris@swiftmailer.org', 'mark@swiftmailer.org'));
  167. $this->assertEqual(
  168. array('chris@swiftmailer.org' => null, 'mark@swiftmailer.org' => null),
  169. $header->getNameAddresses()
  170. );
  171. $this->assertEqual(
  172. array('chris@swiftmailer.org', 'mark@swiftmailer.org'),
  173. $header->getAddresses()
  174. );
  175. }
  176. public function testNameIsEncodedIfNonAscii()
  177. {
  178. $name = 'C' . pack('C', 0x8F) . 'rbyn';
  179. $encoder = $this->_getEncoder('Q');
  180. $this->_checking(Expectations::create()
  181. -> one($encoder)->encodeString($name, any(), any()) -> returns('C=8Frbyn')
  182. -> ignoring($encoder)
  183. );
  184. $header = $this->_getHeader('From', $encoder);
  185. $header->setNameAddresses(array('chris@swiftmailer.org'=>'Chris ' . $name));
  186. $addresses = $header->getNameAddressStrings();
  187. $this->assertEqual(
  188. 'Chris =?' . $this->_charset . '?Q?C=8Frbyn?= <chris@swiftmailer.org>',
  189. array_shift($addresses)
  190. );
  191. }
  192. public function testEncodingLineLengthCalculations()
  193. {
  194. /* -- RFC 2047, 2.
  195. An 'encoded-word' may not be more than 75 characters long, including
  196. 'charset', 'encoding', 'encoded-text', and delimiters.
  197. */
  198. $name = 'C' . pack('C', 0x8F) . 'rbyn';
  199. $encoder = $this->_getEncoder('Q');
  200. $this->_checking(Expectations::create()
  201. -> one($encoder)->encodeString($name, 6, 63) -> returns('C=8Frbyn')
  202. -> ignoring($encoder)
  203. );
  204. $header = $this->_getHeader('From', $encoder);
  205. $header->setNameAddresses(array('chris@swiftmailer.org'=>'Chris ' . $name));
  206. $header->getNameAddressStrings();
  207. }
  208. public function testGetValueReturnsMailboxStringValue()
  209. {
  210. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  211. $header->setNameAddresses(array(
  212. 'chris@swiftmailer.org' => 'Chris Corbyn'
  213. ));
  214. $this->assertEqual(
  215. 'Chris Corbyn <chris@swiftmailer.org>', $header->getFieldBody()
  216. );
  217. }
  218. public function testGetValueReturnsMailboxStringValueForMultipleMailboxes()
  219. {
  220. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  221. $header->setNameAddresses(array(
  222. 'chris@swiftmailer.org' => 'Chris Corbyn',
  223. 'mark@swiftmailer.org' => 'Mark Corbyn'
  224. ));
  225. $this->assertEqual(
  226. 'Chris Corbyn <chris@swiftmailer.org>, Mark Corbyn <mark@swiftmailer.org>',
  227. $header->getFieldBody()
  228. );
  229. }
  230. public function testRemoveAddressesWithSingleValue()
  231. {
  232. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  233. $header->setNameAddresses(array(
  234. 'chris@swiftmailer.org' => 'Chris Corbyn',
  235. 'mark@swiftmailer.org' => 'Mark Corbyn'
  236. ));
  237. $header->removeAddresses('chris@swiftmailer.org');
  238. $this->assertEqual(array('mark@swiftmailer.org'),
  239. $header->getAddresses()
  240. );
  241. }
  242. public function testRemoveAddressesWithList()
  243. {
  244. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  245. $header->setNameAddresses(array(
  246. 'chris@swiftmailer.org' => 'Chris Corbyn',
  247. 'mark@swiftmailer.org' => 'Mark Corbyn'
  248. ));
  249. $header->removeAddresses(
  250. array('chris@swiftmailer.org', 'mark@swiftmailer.org')
  251. );
  252. $this->assertEqual(array(), $header->getAddresses());
  253. }
  254. public function testSetBodyModel()
  255. {
  256. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  257. $header->setFieldBodyModel('chris@swiftmailer.org');
  258. $this->assertEqual(array('chris@swiftmailer.org'=>null), $header->getNameAddresses());
  259. }
  260. public function testGetBodyModel()
  261. {
  262. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  263. $header->setAddresses(array('chris@swiftmailer.org'));
  264. $this->assertEqual(array('chris@swiftmailer.org'=>null), $header->getFieldBodyModel());
  265. }
  266. public function testToString()
  267. {
  268. $header = $this->_getHeader('From', $this->_getEncoder('Q', true));
  269. $header->setNameAddresses(array(
  270. 'chris@swiftmailer.org' => 'Chris Corbyn',
  271. 'mark@swiftmailer.org' => 'Mark Corbyn'
  272. ));
  273. $this->assertEqual(
  274. 'From: Chris Corbyn <chris@swiftmailer.org>, ' .
  275. 'Mark Corbyn <mark@swiftmailer.org>' . "\r\n",
  276. $header->toString()
  277. );
  278. }
  279. // -- Private methods
  280. private function _getHeader($name, $encoder)
  281. {
  282. $header = new Swift_Mime_Headers_MailboxHeader($name, $encoder, new Swift_Mime_Grammar());
  283. $header->setCharset($this->_charset);
  284. return $header;
  285. }
  286. private function _getEncoder($type, $stub = false)
  287. {
  288. $encoder = $this->_mock('Swift_Mime_HeaderEncoder');
  289. $this->_checking(Expectations::create()
  290. -> ignoring($encoder)->getName() -> returns($type)
  291. );
  292. if ($stub)
  293. {
  294. $this->_checking(Expectations::create()
  295. -> ignoring($encoder)
  296. );
  297. }
  298. return $encoder;
  299. }
  300. }