PageRenderTime 75ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Mail/Storage/MessageTest.php

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