PageRenderTime 29ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Mail/Storage/MessageTest.php

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