IdentificationHeaderTest.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Mime/Headers/IdentificationHeader.php';
  4. require_once 'Swift/Mime/Grammar.php';
  5. class Swift_Mime_Headers_IdentificationHeaderTest
  6. extends Swift_Tests_SwiftUnitTestCase
  7. {
  8. public function testTypeIsIdHeader()
  9. {
  10. $header = $this->_getHeader('Message-ID');
  11. $this->assertEqual(Swift_Mime_Header::TYPE_ID, $header->getFieldType());
  12. }
  13. public function testValueMatchesMsgIdSpec()
  14. {
  15. /* -- RFC 2822, 3.6.4.
  16. message-id = "Message-ID:" msg-id CRLF
  17. in-reply-to = "In-Reply-To:" 1*msg-id CRLF
  18. references = "References:" 1*msg-id CRLF
  19. msg-id = [CFWS] "<" id-left "@" id-right ">" [CFWS]
  20. id-left = dot-atom-text / no-fold-quote / obs-id-left
  21. id-right = dot-atom-text / no-fold-literal / obs-id-right
  22. no-fold-quote = DQUOTE *(qtext / quoted-pair) DQUOTE
  23. no-fold-literal = "[" *(dtext / quoted-pair) "]"
  24. */
  25. $header = $this->_getHeader('Message-ID');
  26. $header->setId('id-left@id-right');
  27. $this->assertEqual('<id-left@id-right>', $header->getFieldBody());
  28. }
  29. public function testIdCanBeRetrievedVerbatim()
  30. {
  31. $header = $this->_getHeader('Message-ID');
  32. $header->setId('id-left@id-right');
  33. $this->assertEqual('id-left@id-right', $header->getId());
  34. }
  35. public function testMultipleIdsCanBeSet()
  36. {
  37. $header = $this->_getHeader('References');
  38. $header->setIds(array('a@b', 'x@y'));
  39. $this->assertEqual(array('a@b', 'x@y'), $header->getIds());
  40. }
  41. public function testSettingMultipleIdsProducesAListValue()
  42. {
  43. /* -- RFC 2822, 3.6.4.
  44. The "References:" and "In-Reply-To:" field each contain one or more
  45. unique message identifiers, optionally separated by CFWS.
  46. .. SNIP ..
  47. in-reply-to = "In-Reply-To:" 1*msg-id CRLF
  48. references = "References:" 1*msg-id CRLF
  49. */
  50. $header = $this->_getHeader('References');
  51. $header->setIds(array('a@b', 'x@y'));
  52. $this->assertEqual('<a@b> <x@y>', $header->getFieldBody());
  53. }
  54. public function testIdLeftCanBeQuoted()
  55. {
  56. /* -- RFC 2822, 3.6.4.
  57. id-left = dot-atom-text / no-fold-quote / obs-id-left
  58. */
  59. $header = $this->_getHeader('References');
  60. $header->setId('"ab"@c');
  61. $this->assertEqual('"ab"@c', $header->getId());
  62. $this->assertEqual('<"ab"@c>', $header->getFieldBody());
  63. }
  64. public function testIdLeftCanContainAnglesAsQuotedPairs()
  65. {
  66. /* -- RFC 2822, 3.6.4.
  67. no-fold-quote = DQUOTE *(qtext / quoted-pair) DQUOTE
  68. */
  69. $header = $this->_getHeader('References');
  70. $header->setId('"a\\<\\>b"@c');
  71. $this->assertEqual('"a\\<\\>b"@c', $header->getId());
  72. $this->assertEqual('<"a\\<\\>b"@c>', $header->getFieldBody());
  73. }
  74. public function testIdLeftCanBeDotAtom()
  75. {
  76. $header = $this->_getHeader('References');
  77. $header->setId('a.b+&%$.c@d');
  78. $this->assertEqual('a.b+&%$.c@d', $header->getId());
  79. $this->assertEqual('<a.b+&%$.c@d>', $header->getFieldBody());
  80. }
  81. public function testInvalidIdLeftThrowsException()
  82. {
  83. try
  84. {
  85. $header = $this->_getHeader('References');
  86. $header->setId('a b c@d');
  87. $this->fail(
  88. 'Exception should be thrown since "a b c" is not valid id-left.'
  89. );
  90. }
  91. catch (Exception $e)
  92. {
  93. $this->pass();
  94. }
  95. }
  96. public function testIdRightCanBeDotAtom()
  97. {
  98. /* -- RFC 2822, 3.6.4.
  99. id-right = dot-atom-text / no-fold-literal / obs-id-right
  100. */
  101. $header = $this->_getHeader('References');
  102. $header->setId('a@b.c+&%$.d');
  103. $this->assertEqual('a@b.c+&%$.d', $header->getId());
  104. $this->assertEqual('<a@b.c+&%$.d>', $header->getFieldBody());
  105. }
  106. public function testIdRightCanBeLiteral()
  107. {
  108. /* -- RFC 2822, 3.6.4.
  109. no-fold-literal = "[" *(dtext / quoted-pair) "]"
  110. */
  111. $header = $this->_getHeader('References');
  112. $header->setId('a@[1.2.3.4]');
  113. $this->assertEqual('a@[1.2.3.4]', $header->getId());
  114. $this->assertEqual('<a@[1.2.3.4]>', $header->getFieldBody());
  115. }
  116. public function testInvalidIdRightThrowsException()
  117. {
  118. try
  119. {
  120. $header = $this->_getHeader('References');
  121. $header->setId('a@b c d');
  122. $this->fail(
  123. 'Exception should be thrown since "b c d" is not valid id-right.'
  124. );
  125. }
  126. catch (Exception $e)
  127. {
  128. $this->pass();
  129. }
  130. }
  131. public function testMissingAtSignThrowsException()
  132. {
  133. /* -- RFC 2822, 3.6.4.
  134. msg-id = [CFWS] "<" id-left "@" id-right ">" [CFWS]
  135. */
  136. try
  137. {
  138. $header = $this->_getHeader('References');
  139. $header->setId('abc');
  140. $this->fail(
  141. 'Exception should be thrown since "abc" is does not contain @.'
  142. );
  143. }
  144. catch (Exception $e)
  145. {
  146. $this->pass();
  147. }
  148. }
  149. public function testSetBodyModel()
  150. {
  151. $header = $this->_getHeader('Message-ID');
  152. $header->setFieldBodyModel('a@b');
  153. $this->assertEqual(array('a@b'), $header->getIds());
  154. }
  155. public function testGetBodyModel()
  156. {
  157. $header = $this->_getHeader('Message-ID');
  158. $header->setId('a@b');
  159. $this->assertEqual(array('a@b'), $header->getFieldBodyModel());
  160. }
  161. public function testStringValue()
  162. {
  163. $header = $this->_getHeader('References');
  164. $header->setIds(array('a@b', 'x@y'));
  165. $this->assertEqual('References: <a@b> <x@y>' . "\r\n", $header->toString());
  166. }
  167. // -- Private methods
  168. private function _getHeader($name)
  169. {
  170. return new Swift_Mime_Headers_IdentificationHeader($name, new Swift_Mime_Grammar());
  171. }
  172. }