From 3a8565c6b5b6eead803fe6fc8e7659fed706efab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Wed, 10 Feb 2016 09:35:53 +0200 Subject: [PATCH] test and fix for #64 --- src/Header/HeaderWrap.php | 16 +++++++++++++++- test/Header/HeaderWrapTest.php | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Header/HeaderWrap.php b/src/Header/HeaderWrap.php index 95133906..6087205a 100644 --- a/src/Header/HeaderWrap.php +++ b/src/Header/HeaderWrap.php @@ -116,7 +116,21 @@ public static function mimeDecodeValue($value) */ public static function canBeEncoded($value) { - $encoded = iconv_mime_encode('x-test', $value, ['scheme' => 'Q']); + // avoid any wrapping by specifying line length long enough + // "test" -> 4 + // "x-test: =?ISO-8859-1?B?dGVzdA==?=" -> 33 + // 8 +2 +3 +3 -> 16 + $charset = 'UTF-8'; + $line_length = strlen($value) * 4 + strlen($charset) + 16; + + $preferences = [ + 'scheme' => 'Q', + 'input-charset' => $charset, + 'output-charset' => $charset, + 'line-length' => $line_length, + ]; + + $encoded = iconv_mime_encode('x-test', $value, $preferences); return (false !== $encoded); } diff --git a/test/Header/HeaderWrapTest.php b/test/Header/HeaderWrapTest.php index e366e010..7944d50d 100644 --- a/test/Header/HeaderWrapTest.php +++ b/test/Header/HeaderWrapTest.php @@ -9,6 +9,7 @@ namespace ZendTest\Mail\Header; +use Zend\Mail\Header\GenericHeader; use Zend\Mail\Header\HeaderWrap; /** @@ -70,4 +71,24 @@ public function testMimeDecoding() $this->assertEquals($expected, $decoded); } + + /** + * Test that fails with HeaderWrap::canBeEncoded at lowest level: + * iconv_mime_encode(): Unknown error (7) + * + * which can be triggered as: + * $header = new GenericHeader($name, $value); + */ + public function testCanBeEncoded() + { + $name = 'Subject'; + $value = "[#77675] New Issue:xxxxxxxxx xxxxxxx xxxxxxxx xxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx, tähtaeg xx.xx, xxxx"; + $encoded = "Subject: =?UTF-8?Q?[#77675]=20New=20Issue:xxxxxxxxx=20xxxxxxx=20xxxxxxxx=20?=\r\n =?UTF-8?Q?xxxxxxxxxxxxx=20xxxxxxxxxx=20xxxxxxxx,=20t=C3=A4htaeg=20xx.xx,=20xxxx?="; + $res = HeaderWrap::canBeEncoded($value); + $this->assertTrue($res); + + $header = new GenericHeader($name, $value); + $res = $header->toString(); + $this->assertEquals($encoded, $res); + } }