Browse files

Merge pull request #567 from suzuki/2.2-cakeemail

Add charset() / headerCharset() methods to CakeEmail class
  • Loading branch information...
2 parents 079ae82 + 358b84b commit 47a2c22af4420ce384d81cd1c6608b0ef59c701e @lorenzo lorenzo committed Mar 16, 2012
Showing with 165 additions and 0 deletions.
  1. +34 −0 lib/Cake/Network/Email/CakeEmail.php
  2. +131 −0 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
View
34 lib/Cake/Network/Email/CakeEmail.php
@@ -460,6 +460,37 @@ public function addBcc($email, $name = null) {
}
/**
+ * Charset setter/getter
+ *
+ * @param string $charset
+ * @return string $this->charset
+ */
+ public function charset($charset = null) {
+ if ($charset === null) {
+ return $this->charset;
+ }
+ $this->charset = $charset;
+ if (empty($this->headerCharset)) {
+ $this->headerCharset = $charset;
+ }
+ return $this->charset;
+ }
+
+/**
+ * HeaderCharset setter/getter
+ *
+ * @param string $charset
+ * @return string $this->charset
+ */
+ public function headerCharset($charset = null) {
+ if ($charset === null) {
+ return $this->headerCharset;
+ }
+ return $this->headerCharset = $charset;
+ }
+
+
+/**
* Set email
*
* @param string $varName
@@ -1116,6 +1147,9 @@ protected function _encode($text) {
$restore = mb_internal_encoding();
mb_internal_encoding($this->_appCharset);
}
+ if (empty($this->headerCharset)) {
+ $this->headerCharset = $this->charset;
+ }
$return = mb_encode_mimeheader($text, $this->headerCharset, 'B');
if ($internalEncoding) {
mb_internal_encoding($restore);
View
131 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
@@ -1498,4 +1498,135 @@ public function testEncode() {
. " =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
$this->assertSame($expected, $result);
}
+
+
+/**
+ * Tests charset setter/getter
+ *
+ * @return void
+ */
+ public function testCharset() {
+ $this->CakeEmail->charset('UTF-8');
+ $this->assertSame($this->CakeEmail->charset(), 'UTF-8');
+
+ $this->CakeEmail->charset('ISO-2022-JP');
+ $this->assertSame($this->CakeEmail->charset(), 'ISO-2022-JP');
+
+ $charset = $this->CakeEmail->charset('Shift_JIS');
+ $this->assertSame($charset, 'Shift_JIS');
+
+ }
+
+/**
+ * Tests headerCharset setter/getter
+ *
+ * @return void
+ */
+ public function testHeaderCharset() {
+ $this->CakeEmail->headerCharset('UTF-8');
+ $this->assertSame($this->CakeEmail->headerCharset(), 'UTF-8');
+
+ $this->CakeEmail->headerCharset('ISO-2022-JP');
+ $this->assertSame($this->CakeEmail->headerCharset(), 'ISO-2022-JP');
+
+ $charset = $this->CakeEmail->headerCharset('Shift_JIS');
+ $this->assertSame($charset, 'Shift_JIS');
+ }
+
+
+/**
+ * Tests for compatible check.
+ * charset property and charset() method.
+ * headerCharset property and headerCharset() method.
+ */
+ public function testCharsetsCompatible() {
+ $this->skipIf(!function_exists('mb_convert_encoding'));
+
+ $checkHeaders = array(
+ 'from' => true,
+ 'to' => true,
+ 'cc' => true,
+ 'subject' => true,
+ );
+
+ // Header Charset : null (used by default UTF-8)
+ // Body Charset : ISO-2022-JP
+ $oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', null);
+ $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
+
+ $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', null);
+ $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
+
+ $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
+ $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
+ $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
+ $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
+
+ // Header Charset : UTF-8
+ // Boby Charset : ISO-2022-JP
+ $oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', 'utf-8');
+ $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
+
+ $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', 'utf-8');
+ $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
+
+ $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
+ $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
+ $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
+ $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
+
+ // Header Charset : ISO-2022-JP
+ // Boby Charset : UTF-8
+ $oldStyleEmail = $this->_getEmailByOldStyleCharset('utf-8', 'iso-2022-jp');
+ $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
+
+ $newStyleEmail = $this->_getEmailByNewStyleCharset('utf-8', 'iso-2022-jp');
+ $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
+
+ $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
+ $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
+ $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
+ $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
+
+ }
+
+ private function _getEmailByOldStyleCharset($charset, $headerCharset) {
+ $email = new CakeEmail(array('transport' => 'Debug'));
+
+ if (! empty($charset)) {
+ $email->charset = $charset;
+ }
+ if (! empty($headerCharset)) {
+ $email->headerCharset = $headerCharset;
+ }
+
+ $email->from('someone@example.com', 'どこかの誰か');
+ $email->to('someperson@example.jp', 'どこかのどなたか');
+ $email->cc('miku@example.net', 'ミク');
+ $email->subject('テストメール');
+ $email->send('テストメールの本文');
+
+ return $email;
+ }
+
+ private function _getEmailByNewStyleCharset($charset, $headerCharset) {
+ $email = new CakeEmail(array('transport' => 'Debug'));
+
+ if (! empty($charset)) {
+ $email->charset($charset);
+ }
+ if (! empty($headerCharset)) {
+ $email->headerCharset($headerCharset);
+ }
+
+ $email->from('someone@example.com', 'どこかの誰か');
+ $email->to('someperson@example.jp', 'どこかのどなたか');
+ $email->cc('miku@example.net', 'ミク');
+ $email->subject('テストメール');
+ $email->send('テストメールの本文');
+
+ return $email;
+ }
+
+
}

0 comments on commit 47a2c22

Please sign in to comment.