Skip to content

Commit

Permalink
Added tests to send email with contents.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Apr 13, 2011
1 parent ffaee3d commit d0f1843
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/Cake/Network/CakeEmail.php
Expand Up @@ -17,6 +17,7 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Core', array('Validation', 'Multibyte'));
App::import('Lib', 'email/AbstractTransport');

/**
* Cake e-mail class.
Expand Down
70 changes: 68 additions & 2 deletions lib/Cake/tests/Case/Network/CakeEmailTest.php
Expand Up @@ -42,6 +42,51 @@ public function wrap($text) {

}

/**
* Debug transport email
*
*/
class DebugTransport extends AbstractTransport {

/**
* Last email body
*
* @var string
*/
public static $lastEmail = '';

/**
* Last email header
*
* @var string
*/
public static $lastHeader = '';

/**
* Include addresses in header
*
* @var boolean
*/
public static $includeAddresses = false;

/**
* Send
*
* @param object $email CakeEmail
* @return boolean
*/
public function send(CakeEmail $email) {
self::$lastEmail = implode("\r\n", $email->getMessage());
$options = array();
if (self::$includeAddresses) {
$options = array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true);
}
self::$lastHeader = $this->_headersToString($email->getHeaders($options));
return true;
}

}

/**
* CakeEmailTest class
*
Expand Down Expand Up @@ -308,11 +353,32 @@ public function testAttachments() {
}

/**
* testSend method
* testSendWithContent method
*
* @return void
*/
public function testSend() {
public function testSendWithContent() {
$this->CakeEmail->reset();
$this->CakeEmail->setTransport('debug');
DebugTransport::$includeAddresses = false;

$this->CakeEmail->setFrom('cake@cakephp.org');
$this->CakeEmail->setTo(array('you@cakephp.org' => 'You'));
$this->CakeEmail->setSubject('My title');
$result = $this->CakeEmail->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");

$this->assertTrue($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->assertIdentical(DebugTransport::$lastEmail, $expected);
$this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Date: '));
$this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Message-ID: '));
$this->assertFalse(strpos(DebugTransport::$lastHeader, 'To: '));

DebugTransport::$includeAddresses = true;
$this->CakeEmail->send("Other body");
$this->assertIdentical(DebugTransport::$lastEmail, "Other body\r\n\r\n");
$this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'Message-ID: '));
$this->assertTrue((bool)strpos(DebugTransport::$lastHeader, 'To: '));
}

/**
Expand Down

0 comments on commit d0f1843

Please sign in to comment.