MessageTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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-2015 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. * @category Zend
  36. * @package Zend_Mail
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Mail
  41. */
  42. class Zend_Mail_MessageTest extends PHPUnit_Framework_TestCase
  43. {
  44. protected $_file;
  45. public function setUp()
  46. {
  47. $this->_file = tempnam(sys_get_temp_dir(), 'zm_');
  48. $mail = file_get_contents(dirname(__FILE__) . '/_files/mail.txt');
  49. $mail = preg_replace("/(?<!\r)\n/", "\r\n", $mail);
  50. file_put_contents($this->_file, $mail);
  51. }
  52. public function tearDown()
  53. {
  54. if (file_exists($this->_file)) {
  55. unlink($this->_file);
  56. }
  57. }
  58. public function testInvalidFile()
  59. {
  60. try {
  61. $message = new Zend_Mail_Message(array('file' => '/this/file/does/not/exists'));
  62. } catch (Exception $e) {
  63. return; // ok
  64. }
  65. $this->fail('no exception raised while loading unknown file');
  66. }
  67. public function testIsMultipart()
  68. {
  69. $message = new Zend_Mail_Message(array('file' => $this->_file));
  70. $this->assertTrue($message->isMultipart());
  71. }
  72. public function testGetHeader()
  73. {
  74. $message = new Zend_Mail_Message(array('file' => $this->_file));
  75. $this->assertEquals($message->subject, 'multipart');
  76. }
  77. public function testGetDecodedHeader()
  78. {
  79. $message = new Zend_Mail_Message(array('file' => $this->_file));
  80. $enc = PHP_VERSION_ID < 50600
  81. ? iconv_get_encoding('internal_encoding')
  82. : ini_get('default_charset');
  83. $this->assertEquals($message->from, iconv('UTF-8', $enc, '"Peter Müller" <peter-mueller@example.com>'));
  84. }
  85. public function testGetHeaderAsArray()
  86. {
  87. $message = new Zend_Mail_Message(array('file' => $this->_file));
  88. $this->assertEquals($message->getHeader('subject', 'array'), array('multipart'));
  89. }
  90. public function testGetHeaderFromOpenFile()
  91. {
  92. $fh = fopen($this->_file, 'r');
  93. $message = new Zend_Mail_Message(array('file' => $fh));
  94. $this->assertEquals($message->subject, 'multipart');
  95. }
  96. public function testGetFirstPart()
  97. {
  98. $message = new Zend_Mail_Message(array('file' => $this->_file));
  99. $this->assertEquals(substr($message->getPart(1)->getContent(), 0, 14), 'The first part');
  100. }
  101. public function testGetFirstPartTwice()
  102. {
  103. $message = new Zend_Mail_Message(array('file' => $this->_file));
  104. $message->getPart(1);
  105. $this->assertEquals(substr($message->getPart(1)->getContent(), 0, 14), 'The first part');
  106. }
  107. public function testGetWrongPart()
  108. {
  109. $message = new Zend_Mail_Message(array('file' => $this->_file));
  110. try {
  111. $message->getPart(-1);
  112. } catch (Exception $e) {
  113. return; // ok
  114. }
  115. $this->fail('no exception raised while fetching unknown part');
  116. }
  117. public function testNoHeaderMessage()
  118. {
  119. $message = new Zend_Mail_Message(array('file' => __FILE__));
  120. $this->assertEquals(substr($message->getContent(), 0, 5), '<?php');
  121. $raw = file_get_contents(__FILE__);
  122. $raw = "\t" . $raw;
  123. $message = new Zend_Mail_Message(array('raw' => $raw));
  124. $this->assertEquals(substr($message->getContent(), 0, 6), "\t<?php");
  125. }
  126. public function testMultipleHeader()
  127. {
  128. $raw = file_get_contents($this->_file);
  129. $raw = "sUBject: test\nSubJect: test2\n" . $raw;
  130. $message = new Zend_Mail_Message(array('raw' => $raw));
  131. $this->assertEquals($message->getHeader('subject', 'string'),
  132. 'test' . Zend_Mime::LINEEND . 'test2' . Zend_Mime::LINEEND . 'multipart');
  133. $this->assertEquals($message->getHeader('subject'), array('test', 'test2', 'multipart'));
  134. }
  135. public function testContentTypeDecode()
  136. {
  137. $message = new Zend_Mail_Message(array('file' => $this->_file));
  138. $this->assertEquals(Zend_Mime_Decode::splitContentType($message->ContentType),
  139. array('type' => 'multipart/alternative', 'boundary' => 'crazy-multipart'));
  140. }
  141. public function testSplitEmptyMessage()
  142. {
  143. $this->assertEquals(Zend_Mime_Decode::splitMessageStruct('', 'xxx'), null);
  144. }
  145. public function testSplitInvalidMessage()
  146. {
  147. try {
  148. Zend_Mime_Decode::splitMessageStruct("--xxx\n", 'xxx');
  149. } catch (Zend_Exception $e) {
  150. return; // ok
  151. }
  152. $this->fail('no exception raised while decoding invalid message');
  153. }
  154. public function testInvalidMailHandler()
  155. {
  156. try {
  157. $message = new Zend_Mail_Message(array('handler' => 1));
  158. } catch (Zend_Exception $e) {
  159. return; // ok
  160. }
  161. $this->fail('no exception raised while using invalid mail handler');
  162. }
  163. public function testMissingId()
  164. {
  165. $mail = new Zend_Mail_Storage_Mbox(array('filename' => dirname(__FILE__) . '/_files/test.mbox/INBOX'));
  166. try {
  167. $message = new Zend_Mail_Message(array('handler' => $mail));
  168. } catch (Zend_Exception $e) {
  169. return; // ok
  170. }
  171. $this->fail('no exception raised while mail handler without id');
  172. }
  173. public function testIterator()
  174. {
  175. $message = new Zend_Mail_Message(array('file' => $this->_file));
  176. foreach (new RecursiveIteratorIterator($message) as $num => $part) {
  177. if ($num == 1) {
  178. // explicit call of __toString() needed for PHP < 5.2
  179. $this->assertEquals(substr($part->__toString(), 0, 14), 'The first part');
  180. }
  181. }
  182. $this->assertEquals($part->contentType, 'text/x-vertical');
  183. }
  184. public function testDecodeString()
  185. {
  186. $string = '"Peter M=C3=BCller" <peter-mueller@example.com>';
  187. $is = Zend_Mime_Decode::decodeQuotedPrintable($string);
  188. $should = quoted_printable_decode($string);
  189. $this->assertEquals($is, $should);
  190. }
  191. public function testSplitHeader()
  192. {
  193. $header = 'foo; x=y; y="x"';
  194. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header), array('foo', 'x' => 'y', 'y' => 'x'));
  195. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'x'), 'y');
  196. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'y'), 'x');
  197. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'foo', 'foo'), 'foo');
  198. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'foo'), null);
  199. }
  200. public function testSplitInvalidHeader()
  201. {
  202. $header = '';
  203. try {
  204. Zend_Mime_Decode::splitHeaderField($header);
  205. } catch (Zend_Exception $e) {
  206. return; // ok
  207. }
  208. $this->fail('no exception raised while decoding invalid header field');
  209. }
  210. public function testSplitMessage()
  211. {
  212. $header = 'Test: test';
  213. $body = 'body';
  214. $newlines = array("\r\n", "\n\r", "\n", "\r");
  215. foreach ($newlines as $contentEOL) {
  216. foreach ($newlines as $decodeEOL) {
  217. $content = $header . $contentEOL . $contentEOL . $body;
  218. $decoded = Zend_Mime_Decode::splitMessage($content, $decoded_header, $decoded_body, $decodeEOL);
  219. $this->assertEquals(array('test' => 'test'), $decoded_header);
  220. $this->assertEquals($body, $decoded_body);
  221. }
  222. }
  223. }
  224. public function testToplines()
  225. {
  226. $message = new Zend_Mail_Message(array('headers' => file_get_contents($this->_file)));
  227. $this->assertTrue(strpos($message->getToplines(), 'multipart message') === 0);
  228. }
  229. public function testNoContent()
  230. {
  231. $message = new Zend_Mail_Message(array('raw' => 'Subject: test'));
  232. try {
  233. $message->getContent();
  234. } catch (Zend_Exception $e) {
  235. return; // ok
  236. }
  237. $this->fail('no exception raised while getting content of message without body');
  238. }
  239. public function testEmptyHeader()
  240. {
  241. $message = new Zend_Mail_Message(array());
  242. $this->assertEquals(array(), $message->getHeaders());
  243. $message = new Zend_Mail_Message(array());
  244. $subject = null;
  245. try {
  246. $subject = $message->subject;
  247. } catch (Zend_Exception $e) {
  248. // ok
  249. }
  250. if ($subject) {
  251. $this->fail('no exception raised while getting header from empty message');
  252. }
  253. }
  254. public function testEmptyBody()
  255. {
  256. $message = new Zend_Mail_Message(array());
  257. $part = null;
  258. try {
  259. $part = $message->getPart(1);
  260. } catch (Zend_Exception $e) {
  261. // ok
  262. }
  263. if ($part) {
  264. $this->fail('no exception raised while getting part from empty message');
  265. }
  266. $message = new Zend_Mail_Message(array());
  267. $this->assertTrue($message->countParts() == 0);
  268. }
  269. /**
  270. * @group ZF-5209
  271. */
  272. public function testCheckingHasHeaderFunctionality()
  273. {
  274. $message = new Zend_Mail_Message(array('headers' => array('subject' => 'foo')));
  275. $this->assertTrue( $message->headerExists('subject'));
  276. $this->assertTrue( isset($message->subject) );
  277. $this->assertTrue( $message->headerExists('SuBject'));
  278. $this->assertTrue( isset($message->suBjeCt) );
  279. $this->assertFalse($message->headerExists('From'));
  280. }
  281. public function testWrongMultipart()
  282. {
  283. $message = new Zend_Mail_Message(array('raw' => "Content-Type: multipart/mixed\r\n\r\ncontent"));
  284. try {
  285. $message->getPart(1);
  286. } catch (Zend_Exception $e) {
  287. return; // ok
  288. }
  289. $this->fail('no exception raised while getting part from message without boundary');
  290. }
  291. public function testLateFetch()
  292. {
  293. $mail = new Zend_Mail_Storage_Mbox(array('filename' => dirname(__FILE__) . '/_files/test.mbox/INBOX'));
  294. $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5));
  295. $this->assertEquals($message->countParts(), 2);
  296. $this->assertEquals($message->countParts(), 2);
  297. $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5));
  298. $this->assertEquals($message->subject, 'multipart');
  299. $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5));
  300. $this->assertTrue(strpos($message->getContent(), 'multipart message') === 0);
  301. }
  302. public function testManualIterator()
  303. {
  304. $message = new Zend_Mail_Message(array('file' => $this->_file));
  305. $this->assertTrue($message->valid());
  306. $this->assertEquals($message->getChildren(), $message->current());
  307. $this->assertEquals($message->key(), 1);
  308. $message->next();
  309. $this->assertTrue($message->valid());
  310. $this->assertEquals($message->getChildren(), $message->current());
  311. $this->assertEquals($message->key(), 2);
  312. $message->next();
  313. $this->assertFalse($message->valid());
  314. $message->rewind();
  315. $this->assertTrue($message->valid());
  316. $this->assertEquals($message->getChildren(), $message->current());
  317. $this->assertEquals($message->key(), 1);
  318. }
  319. public function testMessageFlagsAreSet()
  320. {
  321. $origFlags = array(
  322. 'foo' => 'bar',
  323. 'baz' => 'bat'
  324. );
  325. $message = new Zend_Mail_Message(array('flags' => $origFlags));
  326. $messageFlags = $message->getFlags();
  327. $this->assertTrue($message->hasFlag('bar'), var_export($messageFlags, 1));
  328. $this->assertTrue($message->hasFlag('bat'), var_export($messageFlags, 1));
  329. $this->assertEquals(array('bar' => 'bar', 'bat' => 'bat'), $messageFlags);
  330. }
  331. public function testGetHeaderFieldSingle()
  332. {
  333. $message = new Zend_Mail_Message(array('file' => $this->_file));
  334. $this->assertEquals($message->getHeaderField('subject'), 'multipart');
  335. }
  336. public function testGetHeaderFieldDefault()
  337. {
  338. $message = new Zend_Mail_Message(array('file' => $this->_file));
  339. $this->assertEquals($message->getHeaderField('content-type'), 'multipart/alternative');
  340. }
  341. public function testGetHeaderFieldNamed()
  342. {
  343. $message = new Zend_Mail_Message(array('file' => $this->_file));
  344. $this->assertEquals($message->getHeaderField('content-type', 'boundary'), 'crazy-multipart');
  345. }
  346. public function testGetHeaderFieldMissing()
  347. {
  348. $message = new Zend_Mail_Message(array('file' => $this->_file));
  349. $this->assertNull($message->getHeaderField('content-type', 'foo'));
  350. }
  351. public function testGetHeaderFieldInvalid()
  352. {
  353. $message = new Zend_Mail_Message(array('file' => $this->_file));
  354. try {
  355. $message->getHeaderField('fake-header-name', 'foo');
  356. } catch (Zend_Mail_Exception $e) {
  357. return;
  358. }
  359. $this->fail('No exception thrown while requesting invalid field name');
  360. }
  361. public function testCaseInsensitiveMultipart()
  362. {
  363. $message = new Zend_Mail_Message(array('raw' => "coNTent-TYpe: muLTIpaRT/x-empty\r\n\r\n"));
  364. $this->assertTrue($message->isMultipart());
  365. }
  366. public function testCaseInsensitiveField()
  367. {
  368. $header = 'test; fOO="this is a test"';
  369. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'Foo'), 'this is a test');
  370. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'bar'), null);
  371. }
  372. public function testSpaceInFieldName()
  373. {
  374. $header = 'test; foo =bar; baz =42';
  375. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'foo'), 'bar');
  376. $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'baz'), 42);
  377. }
  378. /**
  379. * @group ZF-11514
  380. */
  381. public function testConstructorMergesConstructorFlagsIntoDefaultFlags()
  382. {
  383. $message = new ZF11514_Mail_Message(array(
  384. 'file' => $this->_file,
  385. 'flags' => array('constructor')
  386. ));
  387. $flags = $message->getFlags();
  388. $this->assertArrayHasKey('default', $flags);
  389. $this->assertEquals('yes!', $flags['default']);
  390. $this->assertArrayHasKey('constructor', $flags);
  391. $this->assertEquals('constructor', $flags['constructor']);
  392. }
  393. /**
  394. * @group ZF-3745
  395. */
  396. public function testBackwardsCompatibilityMaintainedWhenPartClassNotSpecified()
  397. {
  398. $message = new Zend_Mail_Message(array('file' => $this->_file));
  399. $this->assertGreaterThan(0, count($message));
  400. foreach ( $message as $part ) {
  401. $this->assertEquals('Zend_Mail_Part', get_class($part));
  402. }
  403. }
  404. /**
  405. * @group ZF-3745
  406. */
  407. public function testMessageAcceptsPartClassOverrideViaConstructor()
  408. {
  409. $message = new Zend_Mail_Message(array(
  410. 'file' => $this->_file,
  411. 'partclass' => 'ZF3745_Mail_Part'
  412. ));
  413. $this->assertEquals('ZF3745_Mail_Part', $message->getPartClass());
  414. // Ensure message parts use the specified part class
  415. $this->assertGreaterThan(0, count($message));
  416. foreach ( $message as $part ) {
  417. $this->assertEquals('ZF3745_Mail_Part', get_class($part));
  418. }
  419. }
  420. /**
  421. * @group ZF-3745
  422. */
  423. public function testMessageAcceptsPartClassOverrideViaSetter()
  424. {
  425. $message = new Zend_Mail_Message(array('file' => $this->_file));
  426. $message->setPartClass('ZF3745_Mail_Part');
  427. $this->assertEquals('ZF3745_Mail_Part', $message->getPartClass());
  428. // Ensure message parts use the specified part class
  429. $this->assertGreaterThan(0, count($message));
  430. foreach ( $message as $part ) {
  431. $this->assertEquals('ZF3745_Mail_Part', get_class($part));
  432. }
  433. }
  434. public function invalidHeaders()
  435. {
  436. return array(
  437. 'name' => array("Fake\r\n\r\rnevilContent", 'value'),
  438. 'value' => array('Fake', "foo-bar\r\n\r\nevilContent"),
  439. 'multi-value' => array('Fake', array('okay', "foo-bar\r\n\r\nevilContent")),
  440. );
  441. }
  442. /**
  443. * @dataProvider invalidHeaders
  444. * @group ZF2015-04
  445. */
  446. public function testRaisesExceptionWhenProvidedWithHeaderContainingCRLFInjection($name, $value)
  447. {
  448. $headers = array($name => $value);
  449. $this->setExpectedException('Zend_Mail_Exception', 'valid');
  450. $message = new Zend_Mail_Message(array(
  451. 'headers' => $headers,
  452. ));
  453. }
  454. }
  455. /**
  456. * Message class which sets a pre-defined default flag set
  457. * @see ZF-11514
  458. */
  459. class ZF11514_Mail_Message extends Zend_Mail_Message
  460. {
  461. protected $_flags = array(
  462. 'default'=>'yes!'
  463. );
  464. }
  465. class ZF3745_Mail_Part extends Zend_Mail_Part
  466. {
  467. }