Implemented the support to use helpers in CakeEmail. Fixes #1754

This commit is contained in:
Juan Basso 2011-06-19 18:43:11 -04:00
parent b4a780332f
commit d220ee5578
5 changed files with 96 additions and 0 deletions

View file

@ -332,6 +332,7 @@ class EmailComponent extends Component {
}
$lib->subject($this->subject)->messageID($this->messageId);
$lib->helpers($this->_controller->helpers);
$headers = array('X-Mailer' => $this->xMailer);
foreach ($this->headers as $key => $value) {

View file

@ -181,6 +181,13 @@ class CakeEmail {
*/
protected $_viewVars = array();
/**
* Helpers to be used in the render
*
* @var array
*/
protected $_helpers = array();
/**
* Text message
*
@ -715,6 +722,20 @@ class CakeEmail {
return $this;
}
/**
* Helpers to be used in render
*
* @param array $helpers
* @return mixed
*/
public function helpers($helpers = null) {
if ($helpers === null) {
return $this->_helpers;
}
$this->_helpers = (array)$helpers;
return $this;
}
/**
* Email format
*
@ -1057,6 +1078,7 @@ class CakeEmail {
$this->_template = '';
$this->_viewRender = 'View';
$this->_viewVars = array();
$this->_helpers = array();
$this->_textMessage = '';
$this->_htmlMessage = '';
$this->_message = '';
@ -1260,6 +1282,7 @@ class CakeEmail {
$View = new $viewClass(null);
$View->viewVars = $this->_viewVars;
$View->helpers = $this->_helpers;
$msg = array();
list($templatePlugin, $template) = pluginSplit($this->_template, true);

View file

@ -501,6 +501,7 @@ TEXTBLOC;
<body>
<p>Here is your value: <b>22091985</b></p>
<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
</body>
</html>
@ -522,6 +523,35 @@ HTMLBLOC;
$this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
}
/**
* testMessageRetrievalWithHelper method
*
* @access public
* @return void
*/
public function testMessageRetrievalWithHelper() {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
));
$timestamp = time();
$this->Controller->set('time', $timestamp);
$this->Controller->set('title_for_layout', 'EmailTest');
$this->Controller->helpers = array('Time');
$this->Controller->EmailTest->to = 'postmaster@example.com';
$this->Controller->EmailTest->from = 'noreply@example.com';
$this->Controller->EmailTest->subject = 'Cake Debug Test';
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
$this->Controller->EmailTest->layout = 'default';
$this->Controller->EmailTest->template = 'custom_helper';
$this->Controller->EmailTest->sendAs = 'text';
$this->Controller->EmailTest->delivery = 'DebugComp';
$this->assertTrue($this->Controller->EmailTest->send());
$this->assertTrue((bool)strpos($this->Controller->EmailTest->textMessage, 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
}
/**
* testContentArray method
*

View file

@ -616,6 +616,29 @@ class CakeEmailTest extends CakeTestCase {
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Here is your value: 12345'));
}
/**
* testSendRenderWithHelpers method
*
* @return void
*/
public function testSendRenderWithHelpers() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
DebugTransport::$includeAddresses = true;
$timestamp = time();
$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('custom_helper', 'default');
$this->CakeEmail->viewVars(array('time' => $timestamp));
$this->CakeEmail->helpers(array('Time'));
$result = $this->CakeEmail->send();
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
}
/**
* testSendRenderPlugin method
*

View file

@ -0,0 +1,19 @@
<?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.pages
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>
Right now: <?php echo $this->Time->toAtom($time); ?>