From d220ee5578304b38110e45220f18dad7a478e78c Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Sun, 19 Jun 2011 18:43:11 -0400 Subject: [PATCH] Implemented the support to use helpers in CakeEmail. Fixes #1754 --- .../Controller/Component/EmailComponent.php | 1 + lib/Cake/Network/Email/CakeEmail.php | 23 ++++++++++++++ .../Component/EmailComponentTest.php | 30 +++++++++++++++++++ .../Test/Case/Network/Email/CakeEmailTest.php | 23 ++++++++++++++ .../View/Emails/text/custom_helper.ctp | 19 ++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 lib/Cake/Test/test_app/View/Emails/text/custom_helper.ctp diff --git a/lib/Cake/Controller/Component/EmailComponent.php b/lib/Cake/Controller/Component/EmailComponent.php index 1920c6466..be02a4fc7 100644 --- a/lib/Cake/Controller/Component/EmailComponent.php +++ b/lib/Cake/Controller/Component/EmailComponent.php @@ -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) { diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index c929168f2..0e8b3317d 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -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); diff --git a/lib/Cake/Test/Case/Controller/Component/EmailComponentTest.php b/lib/Cake/Test/Case/Controller/Component/EmailComponentTest.php index 163ec2dd4..d2a6196b2 100644 --- a/lib/Cake/Test/Case/Controller/Component/EmailComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/EmailComponentTest.php @@ -501,6 +501,7 @@ TEXTBLOC;

Here is your value: 22091985

+

This email was sent using the CakePHP Framework

@@ -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 * diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index 7fb4e5456..bddda5f18 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -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 * diff --git a/lib/Cake/Test/test_app/View/Emails/text/custom_helper.ctp b/lib/Cake/Test/test_app/View/Emails/text/custom_helper.ctp new file mode 100644 index 000000000..5fa5b9ad3 --- /dev/null +++ b/lib/Cake/Test/test_app/View/Emails/text/custom_helper.ctp @@ -0,0 +1,19 @@ + +Right now: Time->toAtom($time); ?>