Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#5140 branch 'hotfix/5140'
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks3w committed Oct 20, 2013
2 parents 7968947 + c38a124 commit 57d925a
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 18 deletions.
115 changes: 115 additions & 0 deletions src/Header/ContentTransferEncoding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Mail\Header;

use Zend\Mail\Headers;

class ContentTransferEncoding implements HeaderInterface
{
/**
* Allowed Content-Transfer-Encoding parameters specified by RFC 1521
* (reduced set)
* @var array
*/
protected static $allowedTransferEncodings = array(
'7bit',
'8bit',
'quoted-printable',
'base64',
/*
* not implemented:
* 'binary',
* x-token: 'X-'
*/
);


/**
* @var string
*/
protected $transferEncoding;

/**
* @var array
*/
protected $parameters = array();

public static function fromString($headerLine)
{
$headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'content-transfer-encoding') {
throw new Exception\InvalidArgumentException('Invalid header line for Content-Transfer-Encoding string');
}

$header = new static();
$header->setTransferEncoding($value);

return $header;
}

public function getFieldName()
{
return 'Content-Transfer-Encoding';
}

public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
{
return $this->transferEncoding;
}

public function setEncoding($encoding)
{
// Header must be always in US-ASCII
return $this;
}

public function getEncoding()
{
return 'ASCII';
}

public function toString()
{
return 'Content-Transfer-Encoding: ' . $this->getFieldValue();
}

/**
* Set the content transfer encoding
*
* @param string $transferEncoding
* @throws Exception\InvalidArgumentException
* @return self
*/
public function setTransferEncoding($transferEncoding)
{
if (!in_array($transferEncoding, self::$allowedTransferEncodings)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects one of "'. implode(', ', self::$allowedTransferEncodings) . '"; received "%s"',
__METHOD__,
(string) $transferEncoding
));
}
$this->transferEncoding = $transferEncoding;
return $this;
}

/**
* Retrieve the content transfer encoding
*
* @return string
*/
public function getTransferEncoding()
{
return $this->transferEncoding;
}
}
39 changes: 21 additions & 18 deletions src/Header/HeaderLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,26 @@ class HeaderLoader extends PluginClassLoader
* @var array Pre-aliased Header plugins
*/
protected $plugins = array(
'bcc' => 'Zend\Mail\Header\Bcc',
'cc' => 'Zend\Mail\Header\Cc',
'contenttype' => 'Zend\Mail\Header\ContentType',
'content_type' => 'Zend\Mail\Header\ContentType',
'content-type' => 'Zend\Mail\Header\ContentType',
'date' => 'Zend\Mail\Header\Date',
'from' => 'Zend\Mail\Header\From',
'message-id' => 'Zend\Mail\Header\MessageId',
'mimeversion' => 'Zend\Mail\Header\MimeVersion',
'mime_version' => 'Zend\Mail\Header\MimeVersion',
'mime-version' => 'Zend\Mail\Header\MimeVersion',
'received' => 'Zend\Mail\Header\Received',
'replyto' => 'Zend\Mail\Header\ReplyTo',
'reply_to' => 'Zend\Mail\Header\ReplyTo',
'reply-to' => 'Zend\Mail\Header\ReplyTo',
'sender' => 'Zend\Mail\Header\Sender',
'subject' => 'Zend\Mail\Header\Subject',
'to' => 'Zend\Mail\Header\To',
'bcc' => 'Zend\Mail\Header\Bcc',
'cc' => 'Zend\Mail\Header\Cc',
'contenttype' => 'Zend\Mail\Header\ContentType',
'content_type' => 'Zend\Mail\Header\ContentType',
'content-type' => 'Zend\Mail\Header\ContentType',
'contenttransferencoding' => 'Zend\Mail\Header\ContentTransferEncoding',
'content_transfer_encoding' => 'Zend\Mail\Header\ContentTransferEncoding',
'content-transfer-encoding' => 'Zend\Mail\Header\ContentTransferEncoding',
'date' => 'Zend\Mail\Header\Date',
'from' => 'Zend\Mail\Header\From',
'message-id' => 'Zend\Mail\Header\MessageId',
'mimeversion' => 'Zend\Mail\Header\MimeVersion',
'mime_version' => 'Zend\Mail\Header\MimeVersion',
'mime-version' => 'Zend\Mail\Header\MimeVersion',
'received' => 'Zend\Mail\Header\Received',
'replyto' => 'Zend\Mail\Header\ReplyTo',
'reply_to' => 'Zend\Mail\Header\ReplyTo',
'reply-to' => 'Zend\Mail\Header\ReplyTo',
'sender' => 'Zend\Mail\Header\Sender',
'subject' => 'Zend\Mail\Header\Subject',
'to' => 'Zend\Mail\Header\To',
);
}
62 changes: 62 additions & 0 deletions test/Header/ContentTransferEncodingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Mail\Header;

use Zend\Mail\Header\ContentTransferEncoding;

/**
* @group Zend_Mail
*/
class ContentTransferEncodingTest extends \PHPUnit_Framework_TestCase
{

public function testContentTransferEncodingFromStringCreatesValidContentTransferEncodingHeader()
{
$contentTransferEncodingHeader = ContentTransferEncoding::fromString('Content-Transfer-Encoding: 7bit');
$this->assertInstanceOf('Zend\Mail\Header\HeaderInterface', $contentTransferEncodingHeader);
$this->assertInstanceOf('Zend\Mail\Header\ContentTransferEncoding', $contentTransferEncodingHeader);
}

public function testContentTransferEncodingFromStringCreateExcaption()
{
$this->setExpectedException('Zend\Mail\Header\Exception\InvalidArgumentException');
$contentTransferEncodingHeader = ContentTransferEncoding::fromString('Content-Transfer-Encoding: 9bit');
}

public function testContentTransferEncodingGetFieldNameReturnsHeaderName()
{
$contentTransferEncodingHeader = new ContentTransferEncoding();
$this->assertEquals('Content-Transfer-Encoding', $contentTransferEncodingHeader->getFieldName());
}

public function testContentTransferEncodingGetFieldValueReturnsProperValue()
{
$contentTransferEncodingHeader = new ContentTransferEncoding();
$contentTransferEncodingHeader->setTransferEncoding('7bit');
$this->assertEquals('7bit', $contentTransferEncodingHeader->getFieldValue());
}

public function testContentTransferEncodingToStringReturnsHeaderFormattedString()
{
$contentTransferEncodingHeader = new ContentTransferEncoding();
$contentTransferEncodingHeader->setTransferEncoding('8bit');
$this->assertEquals("Content-Transfer-Encoding: 8bit", $contentTransferEncodingHeader->toString());
}

public function testProvidingParametersIntroducesHeaderFolding()
{
$header = new ContentTransferEncoding();
$header->setTransferEncoding('quoted-printable');
$string = $header->toString();

$this->assertContains("Content-Transfer-Encoding: quoted-printable", $string);
}

}
21 changes: 21 additions & 0 deletions test/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,27 @@ public function testSettingBodyFromSinglePartMimeMessageSetsAppropriateHeaders()
$this->assertEquals('text/html', $header->getFieldValue());
}

public function testSettingUtf8MailBodyFromSinglePartMimeUtf8MessageSetsAppropriateHeaders()
{
$mime = new Mime('foo-bar');
$part = new MimePart('UTF-8 TestString: AaÜüÄäÖöß');
$part->type = Mime::TYPE_TEXT;
$part->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
$part->charset = 'utf-8';
$body = new MimeMessage();
$body->setMime($mime);
$body->addPart($part);

$this->message->setEncoding('UTF-8');
$this->message->setBody($body);

$this->assertContains(
'Content-Type: text/plain;' . Headers::FOLDING . 'charset="utf-8"' . Headers::EOL
. 'Content-Transfer-Encoding: quoted-printable' . Headers::EOL,
$this->message->getHeaders()->toString()
);
}

public function testSettingBodyFromMultiPartMimeMessageSetsAppropriateHeaders()
{
$mime = new Mime('foo-bar');
Expand Down

0 comments on commit 57d925a

Please sign in to comment.