/home/desid573/public_html/family/pdd/lib/Cake/Test/Case/Network/Email
Edit: /home/desid573/public_html/family/pdd/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php (82671B)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Network.Email
* @since CakePHP(tm) v 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeEmail', 'Network/Email');
App::uses('File', 'Utility');
/**
* Help to test CakeEmail
*/
class TestCakeEmail extends CakeEmail {
/**
* Config class name.
*
* Use a the testing config class in this file.
*
* @var string
*/
protected $_configClass = 'TestEmailConfig';
/**
* Config
*/
protected $_config = array();
/**
* Wrap to protected method
*
* @return array
*/
public function formatAddress($address) {
return parent::_formatAddress($address);
}
/**
* Wrap to protected method
*
* @return array
*/
public function wrap($text, $length = CakeEmail::LINE_LENGTH_MUST) {
return parent::_wrap($text, $length);
}
/**
* Get the boundary attribute
*
* @return string
*/
public function getBoundary() {
return $this->_boundary;
}
/**
* Encode to protected method
*
* @return string
*/
public function encode($text) {
return $this->_encode($text);
}
/**
* Render to protected method
*
* @return array
*/
public function render($content) {
return $this->_render($content);
}
}
/**
* EmailConfig class
*/
class TestEmailConfig {
/**
* default config
*
* @var array
*/
public $default = array(
'subject' => 'Default Subject',
);
/**
* test config
*
* @var array
*/
public $test = array(
'from' => array('some@example.com' => 'My website'),
'to' => array('test@example.com' => 'Testname'),
'subject' => 'Test mail subject',
'transport' => 'Debug',
'theme' => 'TestTheme',
'helpers' => array('Html', 'Form'),
);
/**
* test config 2
*
* @var array
*/
public $test2 = array(
'from' => array('some@example.com' => 'My website'),
'to' => array('test@example.com' => 'Testname'),
'subject' => 'Test mail subject',
'transport' => 'Smtp',
'host' => 'cakephp.org',
'timeout' => 60
);
}
/**
* ExtendTransport class
* test class to ensure the class has send() method
*/
class ExtendTransport {
}
/**
* CakeEmailTest class
*
* @package Cake.Test.Case.Network.Email
*/
class CakeEmailTest extends CakeTestCase {
/**
* setUp
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->_configFileExists = true;
$emailConfig = new File(APP . 'Config' . DS . 'email.php');
if (!$emailConfig->exists()) {
$this->_configFileExists = false;
$emailConfig->create();
}
$this->CakeEmail = new TestCakeEmail();
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
App::build();
if (!$this->_configFileExists) {
unlink(APP . 'Config' . DS . 'email.php');
}
}
/**
* Test if the EmailConfig::$default configuration is read when present
*
* @return void
*/
public function testDefaultConfig() {
$this->assertEquals('Default Subject', $this->CakeEmail->subject());
}
/**
* testFrom method
*
* @return void
*/
public function testFrom() {
$this->assertSame(array(), $this->CakeEmail->from());
$this->CakeEmail->from('cake@cakephp.org');
$expected = array('cake@cakephp.org' => 'cake@cakephp.org');
$this->assertSame($expected, $this->CakeEmail->from());
$this->CakeEmail->from(array('cake@cakephp.org'));
$this->assertSame($expected, $this->CakeEmail->from());
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
$expected = array('cake@cakephp.org' => 'CakePHP');
$this->assertSame($expected, $this->CakeEmail->from());
$result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP'));
$this->assertSame($expected, $this->CakeEmail->from());
$this->assertSame($this->CakeEmail, $result);
$this->setExpectedException('SocketException');
$this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address'));
}
/**
* Test that from addresses using colons work.
*
* @return void
*/
public function testFromWithColonsAndQuotes() {
$address = array(
'info@example.com' => '70:20:00 " Forum'
);
$this->CakeEmail->from($address);
$this->assertEquals($address, $this->CakeEmail->from());
$this->CakeEmail->to('info@example.com')
->subject('Test email')
->transport('Debug');
$result = $this->CakeEmail->send();
$this->assertContains('From: "70:20:00 \" Forum"
', $result['headers']);
}
/**
* testSender method
*
* @return void
*/
public function testSender() {
$this->CakeEmail->reset();
$this->assertSame(array(), $this->CakeEmail->sender());
$this->CakeEmail->sender('cake@cakephp.org', 'Name');
$expected = array('cake@cakephp.org' => 'Name');
$this->assertSame($expected, $this->CakeEmail->sender());
$headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
$this->assertFalse($headers['From']);
$this->assertSame('Name ', $headers['Sender']);
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
$headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
$this->assertSame('CakePHP ', $headers['From']);
$this->assertSame('', $headers['Sender']);
}
/**
* testTo method
*
* @return void
*/
public function testTo() {
$this->assertSame(array(), $this->CakeEmail->to());
$result = $this->CakeEmail->to('cake@cakephp.org');
$expected = array('cake@cakephp.org' => 'cake@cakephp.org');
$this->assertSame($expected, $this->CakeEmail->to());
$this->assertSame($this->CakeEmail, $result);
$this->CakeEmail->to('cake@cakephp.org', 'CakePHP');
$expected = array('cake@cakephp.org' => 'CakePHP');
$this->assertSame($expected, $this->CakeEmail->to());
$list = array(
'root@localhost' => 'root',
'bjørn@hammeröath.com' => 'Bjorn',
'cake.php@cakephp.org' => 'Cake PHP',
'cake-php@googlegroups.com' => 'Cake Groups',
'root@cakephp.org'
);
$this->CakeEmail->to($list);
$expected = array(
'root@localhost' => 'root',
'bjørn@hammeröath.com' => 'Bjorn',
'cake.php@cakephp.org' => 'Cake PHP',
'cake-php@googlegroups.com' => 'Cake Groups',
'root@cakephp.org' => 'root@cakephp.org'
);
$this->assertSame($expected, $this->CakeEmail->to());
$this->CakeEmail->addTo('jrbasso@cakephp.org');
$this->CakeEmail->addTo('mark_story@cakephp.org', 'Mark Story');
$this->CakeEmail->addTo('foobar@ætdcadsl.dk');
$result = $this->CakeEmail->addTo(array('phpnut@cakephp.org' => 'PhpNut', 'jose_zap@cakephp.org'));
$expected = array(
'root@localhost' => 'root',
'bjørn@hammeröath.com' => 'Bjorn',
'cake.php@cakephp.org' => 'Cake PHP',
'cake-php@googlegroups.com' => 'Cake Groups',
'root@cakephp.org' => 'root@cakephp.org',
'jrbasso@cakephp.org' => 'jrbasso@cakephp.org',
'mark_story@cakephp.org' => 'Mark Story',
'foobar@ætdcadsl.dk' => 'foobar@ætdcadsl.dk',
'phpnut@cakephp.org' => 'PhpNut',
'jose_zap@cakephp.org' => 'jose_zap@cakephp.org'
);
$this->assertSame($expected, $this->CakeEmail->to());
$this->assertSame($this->CakeEmail, $result);
}
/**
* Data provider function for testBuildInvalidData
*
* @return array
*/
public static function invalidEmails() {
return array(
array(1.0),
array(''),
array('string'),
array(''),
array(array('ok@cakephp.org', 1.0, '', 'string'))
);
}
/**
* testBuildInvalidData
*
* @dataProvider invalidEmails
* @expectedException SocketException
* @return void
*/
public function testInvalidEmail($value) {
$this->CakeEmail->to($value);
}
/**
* testBuildInvalidData
*
* @dataProvider invalidEmails
* @expectedException SocketException
* @return void
*/
public function testInvalidEmailAdd($value) {
$this->CakeEmail->addTo($value);
}
/**
* test emailPattern method
*
* @return void
*/
public function testEmailPattern() {
$regex = '/.+@.+\..+/i';
$this->assertSame($regex, $this->CakeEmail->emailPattern($regex)->emailPattern());
}
/**
* Tests that it is possible to set email regex configuration to a CakeEmail object
*
* @return void
*/
public function testConfigEmailPattern() {
$regex = '/.+@.+\..+/i';
$email = new CakeEmail(array('emailPattern' => $regex));
$this->assertSame($regex, $email->emailPattern());
}
/**
* Tests that it is possible set custom email validation
*
* @return void
*/
public function testCustomEmailValidation() {
$regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}$/i';
$this->CakeEmail->emailPattern($regex)->to('pass.@example.com');
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
), $this->CakeEmail->to());
$this->CakeEmail->addTo('pass..old.docomo@example.com');
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
), $this->CakeEmail->to());
$this->CakeEmail->reset();
$emails = array(
'pass.@example.com',
'pass..old.docomo@example.com'
);
$additionalEmails = array(
'.extend.@example.com',
'.docomo@example.com'
);
$this->CakeEmail->emailPattern($regex)->to($emails);
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
), $this->CakeEmail->to());
$this->CakeEmail->addTo($additionalEmails);
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
'.extend.@example.com' => '.extend.@example.com',
'.docomo@example.com' => '.docomo@example.com',
), $this->CakeEmail->to());
}
/**
* Tests that it is possible to unset the email pattern and make use of filter_var() instead.
*
* @return void
*
* @expectedException SocketException
* @expectedExceptionMessage Invalid email: "fail.@example.com"
*/
public function testUnsetEmailPattern() {
$email = new CakeEmail();
$this->assertSame(CakeEmail::EMAIL_PATTERN, $email->emailPattern());
$email->emailPattern(null);
$this->assertNull($email->emailPattern());
$email->to('pass@example.com');
$email->to('fail.@example.com');
}
/**
* testFormatAddress method
*
* @return void
*/
public function testFormatAddress() {
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org'));
$expected = array('cake@cakephp.org');
$this->assertSame($expected, $result);
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org', 'php@cakephp.org' => 'php@cakephp.org'));
$expected = array('cake@cakephp.org', 'php@cakephp.org');
$this->assertSame($expected, $result);
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'CakePHP', 'php@cakephp.org' => 'Cake'));
$expected = array('CakePHP ', 'Cake ');
$this->assertSame($expected, $result);
$result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last, First'));
$expected = array('"Last, First" ');
$this->assertSame($expected, $result);
$result = $this->CakeEmail->formatAddress(array('me@example.com' => '"Last" First'));
$expected = array('"\"Last\" First" ');
$this->assertSame($expected, $result);
$result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last First'));
$expected = array('Last First ');
$this->assertSame($expected, $result);
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'ÄÖÜTest'));
$expected = array('=?UTF-8?B?w4TDlsOcVGVzdA==?= ');
$this->assertSame($expected, $result);
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
$expected = array('=?UTF-8?B?5pel5pys6KqeVGVzdA==?= ');
$this->assertSame($expected, $result);
}
/**
* testFormatAddressJapanese
*
* @return void
*/
public function testFormatAddressJapanese() {
$this->skipIf(!function_exists('mb_convert_encoding'));
$this->CakeEmail->headerCharset = 'ISO-2022-JP';
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
$expected = array('=?ISO-2022-JP?B?GyRCRnxLXDhsGyhCVGVzdA==?= ');
$this->assertSame($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==?= ");
$this->assertSame($expected, $result);
}
/**
* testAddresses method
*
* @return void
*/
public function testAddresses() {
$this->CakeEmail->reset();
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
$this->CakeEmail->replyTo('replyto@cakephp.org', 'ReplyTo CakePHP');
$this->CakeEmail->readReceipt('readreceipt@cakephp.org', 'ReadReceipt CakePHP');
$this->CakeEmail->returnPath('returnpath@cakephp.org', 'ReturnPath CakePHP');
$this->CakeEmail->to('to@cakephp.org', 'To, CakePHP');
$this->CakeEmail->cc('cc@cakephp.org', 'Cc CakePHP');
$this->CakeEmail->bcc('bcc@cakephp.org', 'Bcc CakePHP');
$this->CakeEmail->addTo('to2@cakephp.org', 'To2 CakePHP');
$this->CakeEmail->addCc('cc2@cakephp.org', 'Cc2 CakePHP');
$this->CakeEmail->addBcc('bcc2@cakephp.org', 'Bcc2 CakePHP');
$this->assertSame($this->CakeEmail->from(), array('cake@cakephp.org' => 'CakePHP'));
$this->assertSame($this->CakeEmail->replyTo(), array('replyto@cakephp.org' => 'ReplyTo CakePHP'));
$this->assertSame($this->CakeEmail->readReceipt(), array('readreceipt@cakephp.org' => 'ReadReceipt CakePHP'));
$this->assertSame($this->CakeEmail->returnPath(), array('returnpath@cakephp.org' => 'ReturnPath CakePHP'));
$this->assertSame($this->CakeEmail->to(), array('to@cakephp.org' => 'To, CakePHP', 'to2@cakephp.org' => 'To2 CakePHP'));
$this->assertSame($this->CakeEmail->cc(), array('cc@cakephp.org' => 'Cc CakePHP', 'cc2@cakephp.org' => 'Cc2 CakePHP'));
$this->assertSame($this->CakeEmail->bcc(), array('bcc@cakephp.org' => 'Bcc CakePHP', 'bcc2@cakephp.org' => 'Bcc2 CakePHP'));
$headers = $this->CakeEmail->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true));
$this->assertSame($headers['From'], 'CakePHP ');
$this->assertSame($headers['Reply-To'], 'ReplyTo CakePHP ');
$this->assertSame($headers['Disposition-Notification-To'], 'ReadReceipt CakePHP ');
$this->assertSame($headers['Return-Path'], 'ReturnPath CakePHP ');
$this->assertSame($headers['To'], '"To, CakePHP" , To2 CakePHP ');
$this->assertSame($headers['Cc'], 'Cc CakePHP , Cc2 CakePHP ');
$this->assertSame($headers['Bcc'], 'Bcc CakePHP , Bcc2 CakePHP ');
}
/**
* testMessageId method
*
* @return void
*/
public function testMessageId() {
$this->CakeEmail->messageId(true);
$result = $this->CakeEmail->getHeaders();
$this->assertTrue(isset($result['Message-ID']));
$this->CakeEmail->messageId(false);
$result = $this->CakeEmail->getHeaders();
$this->assertFalse(isset($result['Message-ID']));
$result = $this->CakeEmail->messageId('');
$this->assertSame($this->CakeEmail, $result);
$result = $this->CakeEmail->getHeaders();
$this->assertSame('', $result['Message-ID']);
$result = $this->CakeEmail->messageId();
$this->assertSame('', $result);
}
/**
* testMessageIdInvalid method
*
* @return void
* @expectedException SocketException
*/
public function testMessageIdInvalid() {
$this->CakeEmail->messageId('my-email@localhost');
}
/**
* testDomain method
*
* @return void
*/
public function testDomain() {
$result = $this->CakeEmail->domain();
$expected = env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n');
$this->assertSame($expected, $result);
$this->CakeEmail->domain('example.org');
$result = $this->CakeEmail->domain();
$expected = 'example.org';
$this->assertSame($expected, $result);
}
/**
* testMessageIdWithDomain method
*
* @return void
*/
public function testMessageIdWithDomain() {
$this->CakeEmail->domain('example.org');
$result = $this->CakeEmail->getHeaders();
$expected = '@example.org>';
$this->assertTextContains($expected, $result['Message-ID']);
$_SERVER['HTTP_HOST'] = 'example.org';
$result = $this->CakeEmail->getHeaders();
$this->assertTextContains('example.org', $result['Message-ID']);
$_SERVER['HTTP_HOST'] = 'example.org:81';
$result = $this->CakeEmail->getHeaders();
$this->assertTextNotContains(':81', $result['Message-ID']);
}
/**
* testSubject method
*
* @return void
*/
public function testSubject() {
$this->CakeEmail->subject('You have a new message.');
$this->assertSame('You have a new message.', $this->CakeEmail->subject());
$this->CakeEmail->subject('You have a new message, I think.');
$this->assertSame($this->CakeEmail->subject(), 'You have a new message, I think.');
$this->CakeEmail->subject(1);
$this->assertSame('1', $this->CakeEmail->subject());
$this->CakeEmail->subject('هذه رسالة بعنوان طويل مرسل للمستلم');
$expected = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
$this->assertSame($expected, $this->CakeEmail->subject());
}
/**
* 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->assertSame($expected, $this->CakeEmail->subject());
$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->assertSame($expected, $this->CakeEmail->subject());
}
/**
* testHeaders method
*
* @return void
*/
public function testHeaders() {
$this->CakeEmail->messageId(false);
$this->CakeEmail->setHeaders(array('X-Something' => 'nice'));
$expected = array(
'X-Something' => 'nice',
'X-Mailer' => 'CakePHP Email',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '8bit'
);
$this->assertSame($expected, $this->CakeEmail->getHeaders());
$this->CakeEmail->addHeaders(array('X-Something' => 'very nice', 'X-Other' => 'cool'));
$expected = array(
'X-Something' => 'very nice',
'X-Other' => 'cool',
'X-Mailer' => 'CakePHP Email',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '8bit'
);
$this->assertSame($expected, $this->CakeEmail->getHeaders());
$this->CakeEmail->from('cake@cakephp.org');
$this->assertSame($expected, $this->CakeEmail->getHeaders());
$expected = array(
'From' => 'cake@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=UTF-8',
'Content-Transfer-Encoding' => '8bit'
);
$this->assertSame($expected, $this->CakeEmail->getHeaders(array('from' => true)));
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
$expected['From'] = 'CakePHP ';
$this->assertSame($expected, $this->CakeEmail->getHeaders(array('from' => true)));
$this->CakeEmail->to(array('cake@cakephp.org', 'php@cakephp.org' => 'CakePHP'));
$expected = array(
'From' => 'CakePHP ',
'To' => 'cake@cakephp.org, CakePHP ',
'X-Something' => 'very nice',
'X-Other' => 'cool',
'X-Mailer' => 'CakePHP Email',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '8bit'
);
$this->assertSame($expected, $this->CakeEmail->getHeaders(array('from' => true, 'to' => true)));
$this->CakeEmail->charset = 'ISO-2022-JP';
$expected = array(
'From' => 'CakePHP ',
'To' => 'cake@cakephp.org, CakePHP ',
'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->assertSame($expected, $this->CakeEmail->getHeaders(array('from' => true, 'to' => true)));
$result = $this->CakeEmail->setHeaders(array());
$this->assertInstanceOf('CakeEmail', $result);
}
/**
* Data provider function for testInvalidHeaders
*
* @return array
*/
public static function invalidHeaders() {
return array(
array(10),
array(''),
array('string'),
array(false),
array(null)
);
}
/**
* testInvalidHeaders
*
* @dataProvider invalidHeaders
* @expectedException SocketException
* @return void
*/
public function testInvalidHeaders($value) {
$this->CakeEmail->setHeaders($value);
}
/**
* testInvalidAddHeaders
*
* @dataProvider invalidHeaders
* @expectedException SocketException
* @return void
*/
public function testInvalidAddHeaders($value) {
$this->CakeEmail->addHeaders($value);
}
/**
* testTemplate method
*
* @return void
*/
public function testTemplate() {
$this->CakeEmail->template('template', 'layout');
$expected = array('template' => 'template', 'layout' => 'layout');
$this->assertSame($expected, $this->CakeEmail->template());
$this->CakeEmail->template('new_template');
$expected = array('template' => 'new_template', 'layout' => 'layout');
$this->assertSame($expected, $this->CakeEmail->template());
$this->CakeEmail->template('template', null);
$expected = array('template' => 'template', 'layout' => null);
$this->assertSame($expected, $this->CakeEmail->template());
$this->CakeEmail->template(null, null);
$expected = array('template' => null, 'layout' => null);
$this->assertSame($expected, $this->CakeEmail->template());
}
/**
* testTheme method
*
* @return void
*/
public function testTheme() {
$this->assertNull($this->CakeEmail->theme());
$this->CakeEmail->theme('default');
$expected = 'default';
$this->assertSame($expected, $this->CakeEmail->theme());
}
/**
* testViewVars method
*
* @return void
*/
public function testViewVars() {
$this->assertSame(array(), $this->CakeEmail->viewVars());
$this->CakeEmail->viewVars(array('value' => 12345));
$this->assertSame(array('value' => 12345), $this->CakeEmail->viewVars());
$this->CakeEmail->viewVars(array('name' => 'CakePHP'));
$this->assertSame(array('value' => 12345, 'name' => 'CakePHP'), $this->CakeEmail->viewVars());
$this->CakeEmail->viewVars(array('value' => 4567));
$this->assertSame(array('value' => 4567, 'name' => 'CakePHP'), $this->CakeEmail->viewVars());
}
/**
* testAttachments method
*
* @return void
*/
public function testAttachments() {
$this->CakeEmail->attachments(CAKE . 'basics.php');
$expected = array(
'basics.php' => array(
'file' => CAKE . 'basics.php',
'mimetype' => 'application/octet-stream'
)
);
$this->assertSame($expected, $this->CakeEmail->attachments());
$this->CakeEmail->attachments(array());
$this->assertSame(array(), $this->CakeEmail->attachments());
$this->CakeEmail->attachments(array(
array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')
));
$this->CakeEmail->addAttachments(CAKE . 'bootstrap.php');
$this->CakeEmail->addAttachments(array(CAKE . 'bootstrap.php'));
$this->CakeEmail->addAttachments(array('other.txt' => CAKE . 'bootstrap.php', 'license' => CAKE . 'LICENSE.txt'));
$expected = array(
'basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'),
'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
'license' => array('file' => CAKE . 'LICENSE.txt', 'mimetype' => 'application/octet-stream')
);
$this->assertSame($expected, $this->CakeEmail->attachments());
$this->setExpectedException('SocketException');
$this->CakeEmail->attachments(array(array('nofile' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
}
/**
* testTransport method
*
* @return void
*/
public function testTransport() {
$result = $this->CakeEmail->transport('Debug');
$this->assertSame($this->CakeEmail, $result);
$this->assertSame('Debug', $this->CakeEmail->transport());
$result = $this->CakeEmail->transportClass();
$this->assertInstanceOf('DebugTransport', $result);
$this->setExpectedException('SocketException');
$this->CakeEmail->transport('Invalid');
$this->CakeEmail->transportClass();
}
/**
* testExtendTransport method
*
* @return void
*/
public function testExtendTransport() {
$this->setExpectedException('SocketException');
$this->CakeEmail->transport('Extend');
$this->CakeEmail->transportClass();
}
/**
* testConfig method
*
* @return void
*/
public function testConfig() {
$transportClass = $this->CakeEmail->transport('debug')->transportClass();
$config = array('test' => 'ok', 'test2' => true);
$this->CakeEmail->config($config);
$this->assertSame($config, $transportClass->config());
$expected = $config + array('subject' => 'Default Subject');
$this->assertSame($expected, $this->CakeEmail->config());
$this->CakeEmail->config(array());
$this->assertSame($config, $transportClass->config());
$config = array('test' => 'test@example.com', 'subject' => 'my test subject');
$this->CakeEmail->config($config);
$expected = array('test' => 'test@example.com', 'subject' => 'my test subject', 'test2' => true);
$this->assertSame($expected, $this->CakeEmail->config());
$this->assertSame(array('test' => 'test@example.com', 'test2' => true), $transportClass->config());
}
/**
* testConfigString method
*
* @return void
*/
public function testConfigString() {
$configs = new TestEmailConfig();
$this->CakeEmail->config('test');
$result = $this->CakeEmail->to();
$this->assertEquals($configs->test['to'], $result);
$result = $this->CakeEmail->from();
$this->assertEquals($configs->test['from'], $result);
$result = $this->CakeEmail->subject();
$this->assertEquals($configs->test['subject'], $result);
$result = $this->CakeEmail->theme();
$this->assertEquals($configs->test['theme'], $result);
$result = $this->CakeEmail->transport();
$this->assertEquals($configs->test['transport'], $result);
$result = $this->CakeEmail->transportClass();
$this->assertInstanceOf('DebugTransport', $result);
$result = $this->CakeEmail->helpers();
$this->assertEquals($configs->test['helpers'], $result);
}
/**
* Test updating config doesn't reset transport's config.
*
* @return void
*/
public function testConfigMerge() {
$this->CakeEmail->config('test2');
$expected = array(
'host' => 'cakephp.org',
'port' => 25,
'timeout' => 60,
'username' => null,
'password' => null,
'client' => null,
'tls' => false,
'ssl_allow_self_signed' => false
);
$this->assertEquals($expected, $this->CakeEmail->transportClass()->config());
$this->CakeEmail->config(array('log' => true));
$this->CakeEmail->transportClass()->config();
$expected += array('log' => true);
$this->assertEquals($expected, $this->CakeEmail->transportClass()->config());
$this->CakeEmail->config(array('timeout' => 45));
$result = $this->CakeEmail->transportClass()->config();
$this->assertEquals(45, $result['timeout']);
}
/**
* Calling send() with no parameters should not overwrite the view variables.
*
* @return void
*/
public function testSendWithNoContentDoesNotOverwriteViewVar() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('Debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to('you@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->emailFormat('text');
$this->CakeEmail->template('default');
$this->CakeEmail->viewVars(array(
'content' => 'A message to you',
));
$result = $this->CakeEmail->send();
$this->assertContains('A message to you', $result['message']);
}
/**
* testSendWithContent method
*
* @return void
*/
public function testSendWithContent() {
$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'));
$result = $this->CakeEmail->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");
$expected = array('headers', 'message');
$this->assertEquals($expected, array_keys($result));
$expected = "Here is my body, with multi lines.\r\nThis is the second line.\r\n\r\nAnd the last.\r\n\r\n";
$this->assertEquals($expected, $result['message']);
$this->assertTrue((bool)strpos($result['headers'], 'Date: '));
$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
$this->assertTrue((bool)strpos($result['headers'], 'To: '));
$result = $this->CakeEmail->send("Other body");
$expected = "Other body\r\n\r\n";
$this->assertSame($expected, $result['message']);
$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
$this->assertTrue((bool)strpos($result['headers'], 'To: '));
$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'));
$result = $this->CakeEmail->send(array('Sending content', 'As array'));
$expected = "Sending content\r\nAs array\r\n\r\n\r\n";
$this->assertSame($expected, $result['message']);
}
/**
* testSendWithoutFrom method
*
* @return void
*/
public function testSendWithoutFrom() {
$this->CakeEmail->transport('Debug');
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
$this->setExpectedException('SocketException');
$this->CakeEmail->send("Forgot to set From");
}
/**
* testSendWithoutTo method
*
* @return void
*/
public function testSendWithoutTo() {
$this->CakeEmail->transport('Debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
$this->setExpectedException('SocketException');
$this->CakeEmail->send("Forgot to set To");
}
/**
* Test send() with no template.
*
* @return void
*/
public function testSendNoTemplateWithAttachments() {
$this->CakeEmail->transport('debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->emailFormat('text');
$this->CakeEmail->attachments(array(CAKE . 'basics.php'));
$result = $this->CakeEmail->send('Hello');
$boundary = $this->CakeEmail->getBoundary();
$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
$expected = "--$boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"Hello" .
"\r\n" .
"\r\n" .
"\r\n" .
"--$boundary\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment; filename=\"basics.php\"\r\n\r\n";
$this->assertContains($expected, $result['message']);
}
/**
* Test send() with no template and data string attachment
*
* @return void
*/
public function testSendNoTemplateWithDataStringAttachment() {
$this->CakeEmail->transport('debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->emailFormat('text');
$data = file_get_contents(CAKE . 'Console/Templates/skel/webroot/img/cake.icon.png');
$this->CakeEmail->attachments(array('cake.icon.png' => array(
'data' => $data,
'mimetype' => 'image/png'
)));
$result = $this->CakeEmail->send('Hello');
$boundary = $this->CakeEmail->getBoundary();
$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
$expected = "--$boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"Hello" .
"\r\n" .
"\r\n" .
"\r\n" .
"--$boundary\r\n" .
"Content-Type: image/png\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment; filename=\"cake.icon.png\"\r\n\r\n";
$expected .= chunk_split(base64_encode($data), 76, "\r\n");
$this->assertContains($expected, $result['message']);
}
/**
* Test send() with no template as both
*
* @return void
*/
public function testSendNoTemplateWithAttachmentsAsBoth() {
$this->CakeEmail->transport('debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->emailFormat('both');
$this->CakeEmail->attachments(array(CAKE . 'VERSION.txt'));
$result = $this->CakeEmail->send('Hello');
$boundary = $this->CakeEmail->getBoundary();
$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
$expected = "--$boundary\r\n" .
"Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
"\r\n" .
"--alt-$boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"Hello" .
"\r\n" .
"\r\n" .
"\r\n" .
"--alt-$boundary\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"Hello" .
"\r\n" .
"\r\n" .
"\r\n" .
"--alt-{$boundary}--\r\n" .
"\r\n" .
"--$boundary\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n\r\n";
$this->assertContains($expected, $result['message']);
}
/**
* Test setting inline attachments and messages.
*
* @return void
*/
public function testSendWithInlineAttachments() {
$this->CakeEmail->transport('debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->emailFormat('both');
$this->CakeEmail->attachments(array(
'cake.png' => array(
'file' => CAKE . 'VERSION.txt',
'contentId' => 'abc123'
)
));
$result = $this->CakeEmail->send('Hello');
$boundary = $this->CakeEmail->getBoundary();
$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
$expected = "--$boundary\r\n" .
"Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
"\r\n" .
"--rel-$boundary\r\n" .
"Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
"\r\n" .
"--alt-$boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"Hello" .
"\r\n" .
"\r\n" .
"\r\n" .
"--alt-$boundary\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"Hello" .
"\r\n" .
"\r\n" .
"\r\n" .
"--alt-{$boundary}--\r\n" .
"\r\n" .
"--rel-$boundary\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-ID: \r\n" .
"Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
$this->assertContains($expected, $result['message']);
$this->assertContains('--rel-' . $boundary . '--', $result['message']);
$this->assertContains('--' . $boundary . '--', $result['message']);
}
/**
* Test setting inline attachments and HTML only messages.
*
* @return void
*/
public function testSendWithInlineAttachmentsHtmlOnly() {
$this->CakeEmail->transport('debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->emailFormat('html');
$this->CakeEmail->attachments(array(
'cake.png' => array(
'file' => CAKE . 'VERSION.txt',
'contentId' => 'abc123'
)
));
$result = $this->CakeEmail->send('Hello');
$boundary = $this->CakeEmail->getBoundary();
$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
$expected = "--$boundary\r\n" .
"Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
"\r\n" .
"--rel-$boundary\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"Hello" .
"\r\n" .
"\r\n" .
"\r\n" .
"--rel-$boundary\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-ID: \r\n" .
"Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
$this->assertContains($expected, $result['message']);
$this->assertContains('--rel-' . $boundary . '--', $result['message']);
$this->assertContains('--' . $boundary . '--', $result['message']);
}
/**
* Test disabling content-disposition.
*
* @return void
*/
public function testSendWithNoContentDispositionAttachments() {
$this->CakeEmail->transport('debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->emailFormat('text');
$this->CakeEmail->attachments(array(
'cake.png' => array(
'file' => CAKE . 'VERSION.txt',
'contentDisposition' => false
)
));
$result = $this->CakeEmail->send('Hello');
$boundary = $this->CakeEmail->getBoundary();
$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
$expected = "--$boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"Hello" .
"\r\n" .
"\r\n" .
"\r\n" .
"--{$boundary}\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"\r\n";
$this->assertContains($expected, $result['message']);
$this->assertContains('--' . $boundary . '--', $result['message']);
}
/**
* testSendWithLog method
*
* @return void
*/
public function testSendWithLog() {
CakeLog::config('email', array(
'engine' => 'File',
'path' => TMP
));
CakeLog::drop('default');
$this->CakeEmail->transport('Debug');
$this->CakeEmail->to('me@cakephp.org');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('log' => 'cake_test_emails'));
$result = $this->CakeEmail->send("Logging This");
App::uses('File', 'Utility');
$File = new File(TMP . 'cake_test_emails.log');
$log = $File->read();
$this->assertTrue(strpos($log, $result['headers']) !== false);
$this->assertTrue(strpos($log, $result['message']) !== false);
$File->delete();
CakeLog::drop('email');
}
/**
* testSendWithLogAndScope method
*
* @return void
*/
public function testSendWithLogAndScope() {
CakeLog::config('email', array(
'engine' => 'File',
'path' => TMP,
'types' => array('cake_test_emails'),
'scopes' => array('email')
));
CakeLog::drop('default');
$this->CakeEmail->transport('Debug');
$this->CakeEmail->to('me@cakephp.org');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('log' => array('level' => 'cake_test_emails', 'scope' => 'email')));
$result = $this->CakeEmail->send("Logging This");
App::uses('File', 'Utility');
$File = new File(TMP . 'cake_test_emails.log');
$log = $File->read();
$this->assertTrue(strpos($log, $result['headers']) !== false);
$this->assertTrue(strpos($log, $result['message']) !== false);
$File->delete();
CakeLog::drop('email');
}
/**
* testSendRender method
*
* @return void
*/
public function testSendRender() {
$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', 'default');
$result = $this->CakeEmail->send();
$this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
$this->assertContains('Message-ID: ', $result['headers']);
$this->assertContains('To: ', $result['headers']);
}
/**
* test sending and rendering with no layout
*
* @return void
*/
public function testSendRenderNoLayout() {
$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', null);
$result = $this->CakeEmail->send('message body.');
$this->assertContains('message body.', $result['message']);
$this->assertNotContains('This email was sent using the CakePHP Framework', $result['message']);
}
/**
* testSendRender both method
*
* @return void
*/
public function testSendRenderBoth() {
$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', 'default');
$this->CakeEmail->emailFormat('both');
$result = $this->CakeEmail->send();
$this->assertContains('Message-ID: ', $result['headers']);
$this->assertContains('To: ', $result['headers']);
$boundary = $this->CakeEmail->getBoundary();
$this->assertContains('Content-Type: multipart/alternative; boundary="' . $boundary . '"', $result['headers']);
$expected = "--$boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"\r\n" .
"\r\n" .
"This email was sent using the CakePHP Framework, http://cakephp.org." .
"\r\n" .
"\r\n" .
"--$boundary\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"assertStringStartsWith($expected, $result['message']);
$expected = "