Skip to content

Commit

Permalink
Supporting template/layout in plugins for CakeEmail. Fixes #1743.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Jun 1, 2011
1 parent 282196a commit cb88b95
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/Cake/Network/Email/CakeEmail.php
Expand Up @@ -1262,6 +1262,14 @@ protected function _render($content) {
$View->viewVars = $this->_viewVars;
$msg = array();

list($templatePlugin, $template) = pluginSplit($this->_template, true);
list($layoutPlugin, $layout) = pluginSplit($this->_layout, true);
if (!empty($templatePlugin)) {
$View->plugin = rtrim($templatePlugin, '.');
} elseif (!empty($layoutPlugin)) {
$View->plugin = rtrim($layoutPlugin, '.');
}

$content = implode("\n", $content);

if ($this->_emailFormat === 'both') {
Expand All @@ -1278,7 +1286,7 @@ protected function _render($content) {

$View->viewPath = $View->layoutPath = 'Emails' . DS . 'text';
$View->viewVars['content'] = $originalContent;
$this->_textMessage = str_replace(array("\r\n", "\r"), "\n", $View->render($this->_template, $this->_layout));
$this->_textMessage = str_replace(array("\r\n", "\r"), "\n", $View->render($template, $layout));
$content = explode("\n", $this->_textMessage);
$msg = array_merge($msg, $content);

Expand All @@ -1291,7 +1299,7 @@ protected function _render($content) {
$View->viewPath = $View->layoutPath = 'Emails' . DS . 'html';
$View->viewVars['content'] = $originalContent;
$View->hasRendered = false;
$this->_htmlMessage = str_replace(array("\r\n", "\r"), "\n", $View->render($this->_template, $this->_layout));
$this->_htmlMessage = str_replace(array("\r\n", "\r"), "\n", $View->render($template, $layout));
$content = explode("\n", $this->_htmlMessage);
$msg = array_merge($msg, $content);

Expand Down Expand Up @@ -1319,7 +1327,7 @@ protected function _render($content) {

$View->viewPath = $View->layoutPath = 'Emails' . DS . $this->_emailFormat;
$View->viewVars['content'] = $content;
$rendered = $View->render($this->_template, $this->_layout);
$rendered = $View->render($template, $layout);
$content = explode("\n", $rendered);

if ($this->_emailFormat === 'html') {
Expand Down
41 changes: 41 additions & 0 deletions lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
Expand Up @@ -616,6 +616,47 @@ public function testSendRenderWithVars() {
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Here is your value: 12345'));
}

/**
* testSendRenderPlugin method
*
* @return void
*/
public function testSendRenderPlugin() {
App::build(array(
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
));
CakePlugin::load('TestPlugin');

$this->CakeEmail->reset();
$this->CakeEmail->transport('debug');
DebugTransport::$includeAddresses = true;
$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('TestPlugin.test_plugin_tpl', 'default')->send();
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Into TestPlugin.'));
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'This email was sent using the CakePHP Framework'));

$this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'TestPlugin.plug_default')->send();
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Into TestPlugin.'));
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'This email was sent using the TestPlugin.'));

DebugTransport::$lastEmail = '';
$this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'plug_default')->send();
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Into TestPlugin.'));
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'This email was sent using the TestPlugin.'));

$this->CakeEmail->viewVars(array('value' => 12345));
$this->CakeEmail->template('custom', 'TestPlugin.plug_default')->send();
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'Here is your value: 12345'));
$this->assertTrue((bool)strpos(DebugTransport::$lastEmail, 'This email was sent using the TestPlugin.'));

$this->expectException();
$this->CakeEmail->template('test_plugin_tpl', 'plug_default')->send();
}

/**
* testSendMultipleMIME method
*
Expand Down
@@ -0,0 +1 @@
Into TestPlugin.
@@ -0,0 +1,4 @@

<?php echo $content_for_layout; ?>

This email was sent using the TestPlugin.

0 comments on commit cb88b95

Please sign in to comment.