MessageTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Mail_Message
  24. */
  25. require_once 'Zend/Mail/Message.php';
  26. /**
  27. * Zend_Mail_Storage_Mbox
  28. */
  29. require_once 'Zend/Mail/Storage/Mbox.php';
  30. /**
  31. * Zend_Mime_Decode
  32. */
  33. require_once 'Zend/Mime/Decode.php';
  34. /**
  35. * PHPUnit test case
  36. */
  37. require_once 'PHPUnit/Framework/TestCase.php';
  38. /**
  39. * @category Zend
  40. * @package Zend_Mail
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. * @group Zend_Mail
  45. */
  46. class Zend_Mail_MessageTest extends PHPUnit_Framework_TestCase
  47. {
  48. protected $_file;
  49. public function setUp()
  50. {
  51. $this->_file = dirname(__FILE__) . '/_files/mail.txt';
  52. }
  53. public function testInvalidFile()
  54. {
  55. try {
  56. $message = new Zend_Mail_Message(array('file' => '/this/file/does/not/exists'));
  57. } catch (Exception $e) {
  58. return; // ok
  59. }
  60. $this->fail('no exception raised while loading unknown file');
  61. }
  62. public function testIsMultipart()
  63. {
  64. $message = new Zend_Mail_Message(array('file' => $this->_file));
  65. $this->assertTrue($message->isMultipart());
  66. }
  67. public function testGetHeader()
  68. {
  69. $message = new Zend_Mail_Message(array('file' => $this->_file));
  70. $this->assertEquals($message->subject, 'multipart');
  71. }
  72. public function testGetDecodedHeader()
  73. {
  74. $message = new Zend_Mail_Message(array('file' => $this->_file));
  75. $this->assertEquals($message->from, iconv('UTF-8', iconv_get_encoding('internal_encoding'),
  76. '"Peter Müller" <peter-mueller@example.com>'));
  77. }
  78. public function testGetHeaderAsArray()
  79. {
  80. $message = new Zend_Mail_Message(array('file' => $this->_file));
  81. $this->assertEquals($message->getHeader('subject', 'array'), array('multipart'));
  82. }
  83. public function testGetHeaderFromOpenFile()
  84. {
  85. $fh = fopen($this->_file, 'r');
  86. $message = new Zend_Mail_Message(array('file' => $fh));
  87. $this->assertEquals($message->subject, 'multipart');
  88. }
  89. public function testGetFirstPart()
  90. {
  91. $message = new Zend_Mail_Message(array('file' => $this->_file));
  92. $this->assertEquals(substr($message->getPart(1)->getContent(), 0, 14), 'The first part');
  93. }
  94. public function testGetFirstPartTwice()
  95. {
  96. $message = new Zend_Mail_Message(array('file' => $this->_file));
  97. $message->getPart(1);
  98. $this->assertEquals(substr($message->getPart(1)->getContent(), 0, 14), 'The first part');
  99. }
  100. public function testGetWrongPart()
  101. {
  102. $message = new Zend_Mail_Message(array('file' => $this->_file));
  103. try {
  104. $message->getPart(-1);
  105. } catch (Exception $e) {
  106. return; // ok
  107. }
  108. $this->fail('no exception raised while fetching unknown part');
  109. }
  110. public function testNoHeaderMessage()
  111. {
  112. $message = new Zend_Mail_Message(array('file' => __FILE__));
  113. $this->assertEquals(substr($message->getContent(), 0, 5), '<?php');
  114. $raw = file_get_contents(__FILE__);
  115. $raw = "\t" . $raw;
  116. $message = new Zend_Mail_Message(array('raw' => $raw));
  117. $this->assertEquals(substr($message->getContent(), 0, 6), "\t<?php");
  118. }
  119. public function testMultipleHeader()
  120. {
  121. $raw = file_get_contents($this->_file);
  122. $raw = "sUBject: test\nSubJect: test2\n" . $raw;
  123. $message = new Zend_Mail_Message(array('raw' => $raw));
  124. $this->assertEquals($message->getHeader('subject', 'string'),
  125. 'test' . Zend_Mime::LINEEND . 'test2' . Zend_Mime::LINEEND . 'multipart');
  126. $this->assertEquals($message->getHeader('subject'), array('test', 'test2', 'multipart'));
  127. }
  128. public function testContentTypeDecode()
  129. {
  130. $message = new Zend_Mail_Message(array('file' => $this->_file));
  131. $this->assertEquals(Zend_Mime_Decode::splitContentType($message->ContentType),
  132. array('type' => 'multipart/alternative', 'boundary' => 'crazy-multipart'));
  133. }
  134. public function testSplitEmptyMessage()
  135. {
  136. $this->assertEquals(Zend_Mime_Decode::splitMessageStruct('', 'xxx'), null);
  137. }
  138. public function testSplitInvalidMessage()
  139. {
  140. try {
  141. Zend_Mime_Decode::splitMessageStruct("--xxx\n", 'xxx');
  142. } catch (Zend_Exception $e) {
  143. return; // ok
  144. }
  145. $this->fail('no exception raised while decoding invalid message');
  146. }
  147. public function testInvalidMailHandler()
  148. {
  149. try {
  150. $message = new Zend_Mail_Message(array('handler' => 1));
  151. } catch (Zend_Exception $e) {
  152. return; // ok
  153. }
  154. $this->fail('no exception raised while using invalid mail handler');
  155. }
  156. public function testMissingId()
  157. {
  158. $mail = new Zend_Mail_Storage_Mbox(array('filename' => dirname(__FILE__) . '/_files/test.mbox/INBOX'));
  159. try {
  160. $message = new Zend_Mail_Message(array('handler' => $mail));
  161. } catch (Zend_Exception $e) {
  162. return; // ok
  163. }
  164. $this->fail('no exception raised while mail handler without id');
  165. }
  166. public function testIterator()
  167. {
  168. $message = new Zend_Mail_Message(array('file' => $this->_file));
  169. foreach (new RecursiveIteratorIterator($message) as $num => $part) {
  170. if ($num == 1) {
  171. // explicit call of __toString() needed for PHP < 5.2
  172. $this->assertEquals(substr($part->__toString(), 0, 14), 'The first part');
  173. }
  174. }
  175. $this->assertEquals($part->contentType, 'text/x-vertical');
  176. }
  177. public function testDecodeString()
  178. {
  179. $is = Zend_Mime_Decode::decodeQuotedPrintable('=?UTF-8?Q?"Peter M=C3=BCller"?= <peter-mueller@example.com>');
  180. $should = iconv('UTF-8', iconv_get_encoding('internal_encoding'),
  181. '"Peter Müller" <peter-mueller@example.com>');
  182. $this->assertEquals($is, $should);
  183. }
  184. public function testSplitHeader()
  185. {
  186. $header = 'foo; x=y; y="x"';
  187. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header), array('foo', 'x' => 'y', 'y' => 'x'));
  188. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'x'), 'y');
  189. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'y'), 'x');
  190. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'foo', 'foo'), 'foo');
  191. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'foo'), null);
  192. }
  193. public function testSplitInvalidHeader()
  194. {
  195. $header = '';
  196. try {
  197. Zend_Mime_Decode::splitHeaderField($header);
  198. } catch (Zend_Exception $e) {
  199. return; // ok
  200. }
  201. $this->fail('no exception raised while decoding invalid header field');
  202. }
  203. public function testSplitMessage()
  204. {
  205. $header = 'Test: test';
  206. $body = 'body';
  207. $newlines = array("\r\n", "\n\r", "\n", "\r");
  208. foreach ($newlines as $contentEOL) {
  209. foreach ($newlines as $decodeEOL) {
  210. $content = $header . $contentEOL . $contentEOL . $body;
  211. $decoded = Zend_Mime_Decode::splitMessage($content, $decoded_header, $decoded_body, $decodeEOL);
  212. $this->assertEquals(array('test' => 'test'), $decoded_header);
  213. $this->assertEquals($body, $decoded_body);
  214. }
  215. }
  216. }
  217. public function testToplines()
  218. {
  219. $message = new Zend_Mail_Message(array('headers' => file_get_contents($this->_file)));
  220. $this->assertTrue(strpos($message->getToplines(), 'multipart message') === 0);
  221. }
  222. public function testNoContent()
  223. {
  224. $message = new Zend_Mail_Message(array('raw' => 'Subject: test'));
  225. try {
  226. $message->getContent();
  227. } catch (Zend_Exception $e) {
  228. return; // ok
  229. }
  230. $this->fail('no exception raised while getting content of message without body');
  231. }
  232. public function testEmptyHeader()
  233. {
  234. $message = new Zend_Mail_Message(array());
  235. $this->assertEquals(array(), $message->getHeaders());
  236. $message = new Zend_Mail_Message(array());
  237. $subject = null;
  238. try {
  239. $subject = $message->subject;
  240. } catch (Zend_Exception $e) {
  241. // ok
  242. }
  243. if ($subject) {
  244. $this->fail('no exception raised while getting header from empty message');
  245. }
  246. }
  247. public function testEmptyBody()
  248. {
  249. $message = new Zend_Mail_Message(array());
  250. $part = null;
  251. try {
  252. $part = $message->getPart(1);
  253. } catch (Zend_Exception $e) {
  254. // ok
  255. }
  256. if ($part) {
  257. $this->fail('no exception raised while getting part from empty message');
  258. }
  259. $message = new Zend_Mail_Message(array());
  260. $this->assertTrue($message->countParts() == 0);
  261. }
  262. /**
  263. * @group ZF-5209
  264. */
  265. public function testCheckingHasHeaderFunctionality()
  266. {
  267. $message = new Zend_Mail_Message(array('headers' => array('subject' => 'foo')));
  268. $this->assertTrue( $message->headerExists('subject'));
  269. $this->assertTrue( isset($message->subject) );
  270. $this->assertTrue( $message->headerExists('SuBject'));
  271. $this->assertTrue( isset($message->suBjeCt) );
  272. $this->assertFalse($message->headerExists('From'));
  273. }
  274. public function testWrongMultipart()
  275. {
  276. $message = new Zend_Mail_Message(array('raw' => "Content-Type: multipart/mixed\r\n\r\ncontent"));
  277. try {
  278. $message->getPart(1);
  279. } catch (Zend_Exception $e) {
  280. return; // ok
  281. }
  282. $this->fail('no exception raised while getting part from message without boundary');
  283. }
  284. public function testLateFetch()
  285. {
  286. $mail = new Zend_Mail_Storage_Mbox(array('filename' => dirname(__FILE__) . '/_files/test.mbox/INBOX'));
  287. $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5));
  288. $this->assertEquals($message->countParts(), 2);
  289. $this->assertEquals($message->countParts(), 2);
  290. $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5));
  291. $this->assertEquals($message->subject, 'multipart');
  292. $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5));
  293. $this->assertTrue(strpos($message->getContent(), 'multipart message') === 0);
  294. }
  295. public function testManualIterator()
  296. {
  297. $message = new Zend_Mail_Message(array('file' => $this->_file));
  298. $this->assertTrue($message->valid());
  299. $this->assertEquals($message->getChildren(), $message->current());
  300. $this->assertEquals($message->key(), 1);
  301. $message->next();
  302. $this->assertTrue($message->valid());
  303. $this->assertEquals($message->getChildren(), $message->current());
  304. $this->assertEquals($message->key(), 2);
  305. $message->next();
  306. $this->assertFalse($message->valid());
  307. $message->rewind();
  308. $this->assertTrue($message->valid());
  309. $this->assertEquals($message->getChildren(), $message->current());
  310. $this->assertEquals($message->key(), 1);
  311. }
  312. public function testMessageFlagsAreSet()
  313. {
  314. $origFlags = array(
  315. 'foo' => 'bar',
  316. 'baz' => 'bat'
  317. );
  318. $message = new Zend_Mail_Message(array('flags' => $origFlags));
  319. $messageFlags = $message->getFlags();
  320. $this->assertTrue($message->hasFlag('bar'), var_export($messageFlags, 1));
  321. $this->assertTrue($message->hasFlag('bat'), var_export($messageFlags, 1));
  322. $this->assertEquals(array('bar' => 'bar', 'bat' => 'bat'), $messageFlags);
  323. }
  324. public function testGetHeaderFieldSingle()
  325. {
  326. $message = new Zend_Mail_Message(array('file' => $this->_file));
  327. $this->assertEquals($message->getHeaderField('subject'), 'multipart');
  328. }
  329. public function testGetHeaderFieldDefault()
  330. {
  331. $message = new Zend_Mail_Message(array('file' => $this->_file));
  332. $this->assertEquals($message->getHeaderField('content-type'), 'multipart/alternative');
  333. }
  334. public function testGetHeaderFieldNamed()
  335. {
  336. $message = new Zend_Mail_Message(array('file' => $this->_file));
  337. $this->assertEquals($message->getHeaderField('content-type', 'boundary'), 'crazy-multipart');
  338. }
  339. public function testGetHeaderFieldMissing()
  340. {
  341. $message = new Zend_Mail_Message(array('file' => $this->_file));
  342. $this->assertNull($message->getHeaderField('content-type', 'foo'));
  343. }
  344. public function testGetHeaderFieldInvalid()
  345. {
  346. $message = new Zend_Mail_Message(array('file' => $this->_file));
  347. try {
  348. $message->getHeaderField('fake-header-name', 'foo');
  349. } catch (Zend_Mail_Exception $e) {
  350. return;
  351. }
  352. $this->fail('No exception thrown while requesting invalid field name');
  353. }
  354. public function testCaseInsensitiveMultipart()
  355. {
  356. $message = new Zend_Mail_Message(array('raw' => "coNTent-TYpe: muLTIpaRT/x-empty\r\n\r\n"));
  357. $this->assertTrue($message->isMultipart());
  358. }
  359. public function testCaseInsensitiveField()
  360. {
  361. $header = 'test; fOO="this is a test"';
  362. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'Foo'), 'this is a test');
  363. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'bar'), null);
  364. }
  365. public function testSpaceInFieldName()
  366. {
  367. $header = 'test; foo =bar; baz =42';
  368. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'foo'), 'bar');
  369. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'baz'), 42);
  370. }
  371. }