From 8a98ee24860bd0a358eb04565ccc3c87ccda5b2b Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 17 Jun 2011 17:51:31 -0430 Subject: [PATCH] Adding a Debug transport class to help users test their apps when sending emails --- lib/Cake/Network/Email/CakeEmail.php | 2 +- lib/Cake/Network/Email/DebugTransport.php | 50 +++++++++++++ .../Case/Network/Email/DebugTransportTest.php | 74 +++++++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 lib/Cake/Network/Email/DebugTransport.php create mode 100644 lib/Cake/Test/Case/Network/Email/DebugTransportTest.php diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 545581ba186..91dff9d3d92 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -1061,7 +1061,7 @@ public function reset() { $this->_htmlMessage = ''; $this->_message = ''; $this->_emailFormat = 'text'; - $this->_transportName = 'mail'; + $this->_transportName = 'Mail'; $this->_transportClass = null; $this->_attachments = array(); $this->_config = 'default'; diff --git a/lib/Cake/Network/Email/DebugTransport.php b/lib/Cake/Network/Email/DebugTransport.php new file mode 100644 index 00000000000..f359b8f98e6 --- /dev/null +++ b/lib/Cake/Network/Email/DebugTransport.php @@ -0,0 +1,50 @@ +getHeaders(array( + 'from' => true, + 'sender' => true, + 'replyTo' => true, + 'readReceipt' => true, + 'returnPath' => true, + 'to' => true, + 'cc' => true, + 'bcc' => true, + 'subject' => true + )); + $headers = $this->_headersToString($headers); + return $headers . "\n\n" . implode((array)$email->message(), "\n"); + } + +} diff --git a/lib/Cake/Test/Case/Network/Email/DebugTransportTest.php b/lib/Cake/Test/Case/Network/Email/DebugTransportTest.php new file mode 100644 index 00000000000..a84a862338f --- /dev/null +++ b/lib/Cake/Test/Case/Network/Email/DebugTransportTest.php @@ -0,0 +1,74 @@ + + * 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://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests + * @package cake.tests.cases.libs.email + * @since CakePHP(tm) v 2.0.0 + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + */ +App::uses('CakeEmail', 'Network/Email'); +App::uses('AbstractTransport', 'Network/Email'); +App::uses('DebugTransport', 'Network/Email'); + +/** + * Test case + * + */ +class DebugTransportTest extends CakeTestCase { + +/** + * Setup + * + * @return void + */ + public function setUp() { + $this->DebugTransport = new DebugTransport(); + $this->DebugTransport->config(); + } + +/** + * testSend method + * + * @return void + */ + public function testSend() { + $this->getMock('CakeEmail', array('message'), array(), 'DebugCakeEmail'); + $email = new DebugCakeEmail(); + $email->from('noreply@cakephp.org', 'CakePHP Test'); + $email->to('cake@cakephp.org', 'CakePHP'); + $email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso')); + $email->bcc('phpnut@cakephp.org'); + $email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>'); + $email->subject('Testing Message'); + $email->expects($this->any())->method('message')->will($this->returnValue(array('First Line', 'Second Line', ''))); + + $data = "From: CakePHP Test \r\n"; + $data .= "To: CakePHP \r\n"; + $data .= "Cc: Mark Story , Juan Basso \r\n"; + $data .= "Bcc: phpnut@cakephp.org\r\n"; + $data .= "X-Mailer: CakePHP Email\r\n"; + $data .= "Date: " . date(DATE_RFC2822) . "\r\n"; + $data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n"; + $data .= "Subject: Testing Message\r\n"; + $data .= "MIME-Version: 1.0\r\n"; + $data .= "Content-Type: text/plain; charset=UTF-8\r\n"; + $data .= "Content-Transfer-Encoding: 7bit"; + $data .= "\n\n"; + $data .= "First Line\n"; + $data .= "Second Line\n"; + + $result = $this->DebugTransport->send($email); + $this->assertEquals($data, $result); + } + +} \ No newline at end of file