add some test for ISO-2022-JP charset mails

This commit is contained in:
Norio Suzuki 2011-10-20 00:18:42 +09:00
parent 86a081463c
commit 712822ef83
6 changed files with 229 additions and 6 deletions

View file

@ -1108,7 +1108,6 @@ class CakeEmail {
$restore = mb_internal_encoding();
mb_internal_encoding($this->_appCharset);
}
$text = $this->_encodeString($text, $this->headerCharset);
$return = mb_encode_mimeheader($text, $this->headerCharset, 'B');
if ($internalEncoding) {
mb_internal_encoding($restore);
@ -1283,6 +1282,13 @@ class CakeEmail {
$prefix[] = '';
$message = array_merge($prefix, $message);
}
$tmp = array();
foreach ($message as $msg) {
$tmp[] = $this->_encodeString($msg, $this->charset);
}
$message = $tmp;
return $message;
}

View file

@ -55,6 +55,13 @@ class TestCakeEmail extends CakeEmail {
return $this->_boundary;
}
/**
* Encode to protected method
*
*/
public function encode($text) {
return $this->_encode($text);
}
}
/*
@ -287,10 +294,19 @@ class CakeEmailTest extends CakeTestCase {
public function testFormatAddressJapanese() {
$this->skipIf(!function_exists('mb_convert_encoding'));
$this->CakeEmail->charset = 'ISO-2022-JP';
$this->CakeEmail->headerCharset = 'ISO-2022-JP';
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
$expected = array('=?ISO-2022-JP?B?GyRCRnxLXDhsGyhCVGVzdA==?= <cake@cakephp.org>');
$this->assertIdentical($expected, $result);
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '寿限無寿限無五劫の擦り切れ海砂利水魚の水行末雲来末風来末食う寝る処に住む処やぶら小路の藪柑子パイポパイポパイポのシューリンガンシューリンガンのグーリンダイグーリンダイのポンポコピーのポンポコナーの長久命の長助'));
$expected = array("=?ISO-2022-JP?B?GyRCPHc4Qkw1PHc4Qkw1OF45ZSROOyQkakBaJGwzJDo9TXg/ZTV7GyhC?=\r\n"
." =?ISO-2022-JP?B?GyRCJE4/ZTlUS3YxQE1oS3ZJd01oS3Y/KSQmPzIkaz1oJEs9OyRgGyhC?=\r\n"
." =?ISO-2022-JP?B?GyRCPWgkZCRWJGk+Lk8pJE5pLjQ7O1IlUSUkJV0lUSUkJV0lUSUkGyhC?=\r\n"
." =?ISO-2022-JP?B?GyRCJV0kTiU3JWUhPCVqJXMlLCVzJTclZSE8JWolcyUsJXMkTiUwGyhC?=\r\n"
." =?ISO-2022-JP?B?GyRCITwlaiVzJUAlJCUwITwlaiVzJUAlJCROJV0lcyVdJTMlVCE8GyhC?=\r\n"
." =?ISO-2022-JP?B?GyRCJE4lXSVzJV0lMyVKITwkTkQ5NVdMPyRORDk9dRsoQg==?= <cake@cakephp.org>");
$this->assertIdentical($expected, $result);
}
/**
@ -383,6 +399,28 @@ class CakeEmailTest extends CakeTestCase {
$this->assertIdentical($this->CakeEmail->subject(), $expected);
}
/**
* testSubjectJapanese
*
* @return void
*/
public function testSubjectJapanese() {
$this->skipIf(!function_exists('mb_convert_encoding'));
mb_internal_encoding('UTF-8');
$this->CakeEmail->headerCharset = 'ISO-2022-JP';
$this->CakeEmail->subject('日本語のSubjectにも対応するよ');
$expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsJE4bKEJTdWJqZWN0GyRCJEskYkJQMX4kOSRrJGgbKEI=?=';
$this->assertIdentical($this->CakeEmail->subject(), $expected);
$this->CakeEmail->subject('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう');
$expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n"
." =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n"
." =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
$this->assertIdentical($this->CakeEmail->subject(), $expected);
}
/**
* testHeaders method
*
@ -446,6 +484,20 @@ class CakeEmailTest extends CakeTestCase {
);
$this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
$this->CakeEmail->charset = 'ISO-2022-JP';
$expected = array(
'From' => 'CakePHP <cake@cakephp.org>',
'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
'X-Something' => 'very nice',
'X-Other' => 'cool',
'X-Mailer' => 'CakePHP Email',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=ISO-2022-JP',
'Content-Transfer-Encoding' => '7bit'
);
$this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
$result = $this->CakeEmail->setHeaders(array());
$this->assertIsA($result, 'CakeEmail');
}
@ -745,6 +797,31 @@ class CakeEmailTest extends CakeTestCase {
$this->assertTrue((bool)strpos($result['headers'], 'To: '));
}
/**
* testSendRender method for ISO-2022-JP
*
* @return void
*/
public function testSendRenderJapanese() {
$this->skipIf(!function_exists('mb_convert_encoding'));
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
$this->CakeEmail->template('default', 'japanese');
$this->CakeEmail->charset = 'ISO-2022-JP';
$result = $this->CakeEmail->send();
$expected = mb_convert_encoding('CakePHP Framework を使って送信したメールです。 http://cakephp.org.','ISO-2022-JP');
$this->assertTrue((bool)strpos($result['message'], $expected));
$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
$this->assertTrue((bool)strpos($result['headers'], 'To: '));
}
/**
* testSendRenderWithVars method
*
@ -765,6 +842,28 @@ class CakeEmailTest extends CakeTestCase {
$this->assertTrue((bool)strpos($result['message'], 'Here is your value: 12345'));
}
/**
* testSendRenderWithVars method for ISO-2022-JP
*
* @return void
*/
public function testSendRenderWithVarsJapanese() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
$this->CakeEmail->template('japanese', 'default');
$this->CakeEmail->viewVars(array('value' => '日本語の差し込み123'));
$this->CakeEmail->charset = 'ISO-2022-JP';
$result = $this->CakeEmail->send();
$expected = mb_convert_encoding('ここにあなたの設定した値が入ります: 日本語の差し込み123', 'ISO-2022-JP');
$this->assertTrue((bool)strpos($result['message'], $expected));
}
/**
* testSendRenderWithHelpers method
*
@ -973,6 +1072,7 @@ class CakeEmailTest extends CakeTestCase {
$this->assertTrue($this->checkContentTransferEncoding($message, '7bit'));
}
/**
* testReset method
*
@ -1212,7 +1312,7 @@ class CakeEmailTest extends CakeTestCase {
$email->to('someone@example.com')->from('someone@example.com');
$result = $email->send('ってテーブルを作ってやってたらう');
$this->assertContains('Content-Type: text/plain; charset=iso-2022-jp', $result['headers']);
$this->assertContains('ってテーブルを作ってやってたらう', $result['message']);
$this->assertContains(mb_convert_encoding('ってテーブルを作ってやってたらう','ISO-2022-JP'), $result['message']);
}
private function checkContentTransferEncoding($message, $charset) {
@ -1240,4 +1340,24 @@ class CakeEmailTest extends CakeTestCase {
}
return $result['text'] && $result['html'];
}
/**
* Test CakeEmail::_encode function
*
*/
public function testEncode() {
$this->skipIf(!function_exists('mb_convert_encoding'));
$this->CakeEmail->headerCharset = 'ISO-2022-JP';
$result = $this->CakeEmail->encode('日本語');
$expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=';
$this->assertIdentical($expected, $result);
$this->CakeEmail->headerCharset = 'ISO-2022-JP';
$result = $this->CakeEmail->encode('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう');
$expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n"
. " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n"
. " =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
$this->assertIdentical($expected, $result);
}
}

View file

@ -0,0 +1,22 @@
<?php
/**
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake.libs.view.templates.elements.email.html
* @since CakePHP(tm) v 0.10.0.1076
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
?>
<p>ここにあなたの設定した値が入ります: <b><?php echo $value; ?></b></p>

View file

@ -0,0 +1,22 @@
<?php
/**
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake.libs.view.templates.elements.email.text
* @since CakePHP(tm) v 0.10.0.1076
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
?>
ここにあなたの設定した値が入ります: <?php echo $value; ?>

View file

@ -0,0 +1,31 @@
<?php
/**
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake.libs.view.templates.layouts.email.html
* @since CakePHP(tm) v 0.10.0.1076
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title><?php echo $title_for_layout;?></title>
</head>
<body>
<?php echo $content_for_layout;?>
<p>このメールは <a href="http://cakephp.org">CakePHP Framework</a> を利用して送信しました。</p>
</body>
</html>

View file

@ -0,0 +1,22 @@
<?php
/**
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake.libs.view.templates.layouts.email.text
* @since CakePHP(tm) v 0.10.0.1076
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>
<?php echo $content_for_layout;?>
CakePHP Framework を使って送信したメールです。 http://cakephp.org.