Supporting template/layout in plugins for CakeEmail. Fixes #1743.

This commit is contained in:
Juan Basso 2011-05-31 22:55:58 -04:00
parent 282196a7cd
commit cb88b952f4
4 changed files with 57 additions and 3 deletions

View file

@ -1262,6 +1262,14 @@ class CakeEmail {
$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') {
@ -1278,7 +1286,7 @@ class CakeEmail {
$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);
@ -1291,7 +1299,7 @@ class CakeEmail {
$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);
@ -1319,7 +1327,7 @@ class CakeEmail {
$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') {

View file

@ -616,6 +616,47 @@ class CakeEmailTest extends CakeTestCase {
$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
*

View file

@ -0,0 +1 @@
Into TestPlugin.

View file

@ -0,0 +1,4 @@
<?php echo $content_for_layout; ?>
This email was sent using the TestPlugin.