| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Mail
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * Zend_Mail_Message
- */
- require_once 'Zend/Mail/Message.php';
- /**
- * Zend_Mail_Storage_Mbox
- */
- require_once 'Zend/Mail/Storage/Mbox.php';
- /**
- * Zend_Mime_Decode
- */
- require_once 'Zend/Mime/Decode.php';
- /**
- * @category Zend
- * @package Zend_Mail
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Mail
- */
- class Zend_Mail_MessageTest extends PHPUnit_Framework_TestCase
- {
- protected $_file;
- public function setUp()
- {
- $this->_file = tempnam(sys_get_temp_dir(), 'zm_');
- $mail = file_get_contents(dirname(__FILE__) . '/_files/mail.txt');
- $mail = preg_replace("/(?<!\r)\n/", "\r\n", $mail);
- file_put_contents($this->_file, $mail);
- }
- public function tearDown()
- {
- if (file_exists($this->_file)) {
- unlink($this->_file);
- }
- }
- public function testInvalidFile()
- {
- try {
- $message = new Zend_Mail_Message(array('file' => '/this/file/does/not/exists'));
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('no exception raised while loading unknown file');
- }
- public function testIsMultipart()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertTrue($message->isMultipart());
- }
- public function testGetHeader()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertEquals($message->subject, 'multipart');
- }
- public function testGetDecodedHeader()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $enc = PHP_VERSION_ID < 50600
- ? iconv_get_encoding('internal_encoding')
- : ini_get('default_charset');
- $this->assertEquals($message->from, iconv('UTF-8', $enc, '"Peter Müller" <peter-mueller@example.com>'));
- }
- public function testGetHeaderAsArray()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertEquals($message->getHeader('subject', 'array'), array('multipart'));
- }
- public function testGetHeaderFromOpenFile()
- {
- $fh = fopen($this->_file, 'r');
- $message = new Zend_Mail_Message(array('file' => $fh));
- $this->assertEquals($message->subject, 'multipart');
- }
- public function testGetFirstPart()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertEquals(substr($message->getPart(1)->getContent(), 0, 14), 'The first part');
- }
- public function testGetFirstPartTwice()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $message->getPart(1);
- $this->assertEquals(substr($message->getPart(1)->getContent(), 0, 14), 'The first part');
- }
- public function testGetWrongPart()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- try {
- $message->getPart(-1);
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('no exception raised while fetching unknown part');
- }
- public function testNoHeaderMessage()
- {
- $message = new Zend_Mail_Message(array('file' => __FILE__));
- $this->assertEquals(substr($message->getContent(), 0, 5), '<?php');
- $raw = file_get_contents(__FILE__);
- $raw = "\t" . $raw;
- $message = new Zend_Mail_Message(array('raw' => $raw));
- $this->assertEquals(substr($message->getContent(), 0, 6), "\t<?php");
- }
- public function testMultipleHeader()
- {
- $raw = file_get_contents($this->_file);
- $raw = "sUBject: test\nSubJect: test2\n" . $raw;
- $message = new Zend_Mail_Message(array('raw' => $raw));
- $this->assertEquals($message->getHeader('subject', 'string'),
- 'test' . Zend_Mime::LINEEND . 'test2' . Zend_Mime::LINEEND . 'multipart');
- $this->assertEquals($message->getHeader('subject'), array('test', 'test2', 'multipart'));
- }
- public function testContentTypeDecode()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertEquals(Zend_Mime_Decode::splitContentType($message->ContentType),
- array('type' => 'multipart/alternative', 'boundary' => 'crazy-multipart'));
- }
- public function testSplitEmptyMessage()
- {
- $this->assertEquals(Zend_Mime_Decode::splitMessageStruct('', 'xxx'), null);
- }
- public function testSplitInvalidMessage()
- {
- try {
- Zend_Mime_Decode::splitMessageStruct("--xxx\n", 'xxx');
- } catch (Zend_Exception $e) {
- return; // ok
- }
- $this->fail('no exception raised while decoding invalid message');
- }
- public function testInvalidMailHandler()
- {
- try {
- $message = new Zend_Mail_Message(array('handler' => 1));
- } catch (Zend_Exception $e) {
- return; // ok
- }
- $this->fail('no exception raised while using invalid mail handler');
- }
- public function testMissingId()
- {
- $mail = new Zend_Mail_Storage_Mbox(array('filename' => dirname(__FILE__) . '/_files/test.mbox/INBOX'));
- try {
- $message = new Zend_Mail_Message(array('handler' => $mail));
- } catch (Zend_Exception $e) {
- return; // ok
- }
- $this->fail('no exception raised while mail handler without id');
- }
- public function testIterator()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- foreach (new RecursiveIteratorIterator($message) as $num => $part) {
- if ($num == 1) {
- // explicit call of __toString() needed for PHP < 5.2
- $this->assertEquals(substr($part->__toString(), 0, 14), 'The first part');
- }
- }
- $this->assertEquals($part->contentType, 'text/x-vertical');
- }
- public function testDecodeString()
- {
- $string = '"Peter M=C3=BCller" <peter-mueller@example.com>';
- $is = Zend_Mime_Decode::decodeQuotedPrintable($string);
- $should = quoted_printable_decode($string);
- $this->assertEquals($is, $should);
- }
- public function testSplitHeader()
- {
- $header = 'foo; x=y; y="x"';
- $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header), array('foo', 'x' => 'y', 'y' => 'x'));
- $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'x'), 'y');
- $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'y'), 'x');
- $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'foo', 'foo'), 'foo');
- $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'foo'), null);
- }
- public function testSplitInvalidHeader()
- {
- $header = '';
- try {
- Zend_Mime_Decode::splitHeaderField($header);
- } catch (Zend_Exception $e) {
- return; // ok
- }
- $this->fail('no exception raised while decoding invalid header field');
- }
- public function testSplitMessage()
- {
- $header = 'Test: test';
- $body = 'body';
- $newlines = array("\r\n", "\n\r", "\n", "\r");
- foreach ($newlines as $contentEOL) {
- foreach ($newlines as $decodeEOL) {
- $content = $header . $contentEOL . $contentEOL . $body;
- $decoded = Zend_Mime_Decode::splitMessage($content, $decoded_header, $decoded_body, $decodeEOL);
- $this->assertEquals(array('test' => 'test'), $decoded_header);
- $this->assertEquals($body, $decoded_body);
- }
- }
- }
- public function testToplines()
- {
- $message = new Zend_Mail_Message(array('headers' => file_get_contents($this->_file)));
- $this->assertTrue(strpos($message->getToplines(), 'multipart message') === 0);
- }
- public function testNoContent()
- {
- $message = new Zend_Mail_Message(array('raw' => 'Subject: test'));
- try {
- $message->getContent();
- } catch (Zend_Exception $e) {
- return; // ok
- }
- $this->fail('no exception raised while getting content of message without body');
- }
- public function testEmptyHeader()
- {
- $message = new Zend_Mail_Message(array());
- $this->assertEquals(array(), $message->getHeaders());
- $message = new Zend_Mail_Message(array());
- $subject = null;
- try {
- $subject = $message->subject;
- } catch (Zend_Exception $e) {
- // ok
- }
- if ($subject) {
- $this->fail('no exception raised while getting header from empty message');
- }
- }
- public function testEmptyBody()
- {
- $message = new Zend_Mail_Message(array());
- $part = null;
- try {
- $part = $message->getPart(1);
- } catch (Zend_Exception $e) {
- // ok
- }
- if ($part) {
- $this->fail('no exception raised while getting part from empty message');
- }
- $message = new Zend_Mail_Message(array());
- $this->assertTrue($message->countParts() == 0);
- }
- /**
- * @group ZF-5209
- */
- public function testCheckingHasHeaderFunctionality()
- {
- $message = new Zend_Mail_Message(array('headers' => array('subject' => 'foo')));
- $this->assertTrue( $message->headerExists('subject'));
- $this->assertTrue( isset($message->subject) );
- $this->assertTrue( $message->headerExists('SuBject'));
- $this->assertTrue( isset($message->suBjeCt) );
- $this->assertFalse($message->headerExists('From'));
- }
- public function testWrongMultipart()
- {
- $message = new Zend_Mail_Message(array('raw' => "Content-Type: multipart/mixed\r\n\r\ncontent"));
- try {
- $message->getPart(1);
- } catch (Zend_Exception $e) {
- return; // ok
- }
- $this->fail('no exception raised while getting part from message without boundary');
- }
- public function testLateFetch()
- {
- $mail = new Zend_Mail_Storage_Mbox(array('filename' => dirname(__FILE__) . '/_files/test.mbox/INBOX'));
- $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5));
- $this->assertEquals($message->countParts(), 2);
- $this->assertEquals($message->countParts(), 2);
- $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5));
- $this->assertEquals($message->subject, 'multipart');
- $message = new Zend_Mail_Message(array('handler' => $mail, 'id' => 5));
- $this->assertTrue(strpos($message->getContent(), 'multipart message') === 0);
- }
- public function testManualIterator()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertTrue($message->valid());
- $this->assertEquals($message->getChildren(), $message->current());
- $this->assertEquals($message->key(), 1);
- $message->next();
- $this->assertTrue($message->valid());
- $this->assertEquals($message->getChildren(), $message->current());
- $this->assertEquals($message->key(), 2);
- $message->next();
- $this->assertFalse($message->valid());
- $message->rewind();
- $this->assertTrue($message->valid());
- $this->assertEquals($message->getChildren(), $message->current());
- $this->assertEquals($message->key(), 1);
- }
- public function testMessageFlagsAreSet()
- {
- $origFlags = array(
- 'foo' => 'bar',
- 'baz' => 'bat'
- );
- $message = new Zend_Mail_Message(array('flags' => $origFlags));
- $messageFlags = $message->getFlags();
- $this->assertTrue($message->hasFlag('bar'), var_export($messageFlags, 1));
- $this->assertTrue($message->hasFlag('bat'), var_export($messageFlags, 1));
- $this->assertEquals(array('bar' => 'bar', 'bat' => 'bat'), $messageFlags);
- }
- public function testGetHeaderFieldSingle()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertEquals($message->getHeaderField('subject'), 'multipart');
- }
- public function testGetHeaderFieldDefault()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertEquals($message->getHeaderField('content-type'), 'multipart/alternative');
- }
- public function testGetHeaderFieldNamed()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertEquals($message->getHeaderField('content-type', 'boundary'), 'crazy-multipart');
- }
- public function testGetHeaderFieldMissing()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertNull($message->getHeaderField('content-type', 'foo'));
- }
- public function testGetHeaderFieldInvalid()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- try {
- $message->getHeaderField('fake-header-name', 'foo');
- } catch (Zend_Mail_Exception $e) {
- return;
- }
- $this->fail('No exception thrown while requesting invalid field name');
- }
- public function testCaseInsensitiveMultipart()
- {
- $message = new Zend_Mail_Message(array('raw' => "coNTent-TYpe: muLTIpaRT/x-empty\r\n\r\n"));
- $this->assertTrue($message->isMultipart());
- }
- public function testCaseInsensitiveField()
- {
- $header = 'test; fOO="this is a test"';
- $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'Foo'), 'this is a test');
- $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'bar'), null);
- }
- public function testSpaceInFieldName()
- {
- $header = 'test; foo =bar; baz =42';
- $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'foo'), 'bar');
- $this->assertEquals(Zend_Mime_Decode::splitHeaderField($header, 'baz'), 42);
- }
-
- /**
- * @group ZF-11514
- */
- public function testConstructorMergesConstructorFlagsIntoDefaultFlags()
- {
- $message = new ZF11514_Mail_Message(array(
- 'file' => $this->_file,
- 'flags' => array('constructor')
- ));
- $flags = $message->getFlags();
- $this->assertArrayHasKey('default', $flags);
- $this->assertEquals('yes!', $flags['default']);
- $this->assertArrayHasKey('constructor', $flags);
- $this->assertEquals('constructor', $flags['constructor']);
- }
-
- /**
- * @group ZF-3745
- */
- public function testBackwardsCompatibilityMaintainedWhenPartClassNotSpecified()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $this->assertGreaterThan(0, count($message));
- foreach ( $message as $part ) {
- $this->assertEquals('Zend_Mail_Part', get_class($part));
- }
- }
-
- /**
- * @group ZF-3745
- */
- public function testMessageAcceptsPartClassOverrideViaConstructor()
- {
- $message = new Zend_Mail_Message(array(
- 'file' => $this->_file,
- 'partclass' => 'ZF3745_Mail_Part'
- ));
- $this->assertEquals('ZF3745_Mail_Part', $message->getPartClass());
-
- // Ensure message parts use the specified part class
- $this->assertGreaterThan(0, count($message));
- foreach ( $message as $part ) {
- $this->assertEquals('ZF3745_Mail_Part', get_class($part));
- }
- }
-
- /**
- * @group ZF-3745
- */
- public function testMessageAcceptsPartClassOverrideViaSetter()
- {
- $message = new Zend_Mail_Message(array('file' => $this->_file));
- $message->setPartClass('ZF3745_Mail_Part');
- $this->assertEquals('ZF3745_Mail_Part', $message->getPartClass());
-
- // Ensure message parts use the specified part class
- $this->assertGreaterThan(0, count($message));
- foreach ( $message as $part ) {
- $this->assertEquals('ZF3745_Mail_Part', get_class($part));
- }
- }
- public function invalidHeaders()
- {
- return array(
- 'name' => array("Fake\r\n\r\rnevilContent", 'value'),
- 'value' => array('Fake', "foo-bar\r\n\r\nevilContent"),
- 'multi-value' => array('Fake', array('okay', "foo-bar\r\n\r\nevilContent")),
- );
- }
-
- /**
- * @dataProvider invalidHeaders
- * @group ZF2015-04
- */
- public function testRaisesExceptionWhenProvidedWithHeaderContainingCRLFInjection($name, $value)
- {
- $headers = array($name => $value);
- $this->setExpectedException('Zend_Mail_Exception', 'valid');
- $message = new Zend_Mail_Message(array(
- 'headers' => $headers,
- ));
- }
- }
- /**
- * Message class which sets a pre-defined default flag set
- * @see ZF-11514
- */
- class ZF11514_Mail_Message extends Zend_Mail_Message
- {
- protected $_flags = array(
- 'default'=>'yes!'
- );
- }
- class ZF3745_Mail_Part extends Zend_Mail_Part
- {
- }
|