From cb88b952f429a95e58963d4f6d63c6212361b82d Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Tue, 31 May 2011 22:55:58 -0400 Subject: [PATCH] Supporting template/layout in plugins for CakeEmail. Fixes #1743. --- lib/Cake/Network/Email/CakeEmail.php | 14 +++++-- .../Test/Case/Network/Email/CakeEmailTest.php | 41 +++++++++++++++++++ .../View/Emails/text/test_plugin_tpl.ctp | 1 + .../View/Layouts/Emails/text/plug_default.ctp | 4 ++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 lib/Cake/Test/test_app/Plugin/TestPlugin/View/Emails/text/test_plugin_tpl.ctp create mode 100644 lib/Cake/Test/test_app/Plugin/TestPlugin/View/Layouts/Emails/text/plug_default.ctp diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 545581ba1..3d7f6acec 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -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') { diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index 8c98b0140..7fb4e5456 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -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 * diff --git a/lib/Cake/Test/test_app/Plugin/TestPlugin/View/Emails/text/test_plugin_tpl.ctp b/lib/Cake/Test/test_app/Plugin/TestPlugin/View/Emails/text/test_plugin_tpl.ctp new file mode 100644 index 000000000..8a663d093 --- /dev/null +++ b/lib/Cake/Test/test_app/Plugin/TestPlugin/View/Emails/text/test_plugin_tpl.ctp @@ -0,0 +1 @@ +Into TestPlugin. diff --git a/lib/Cake/Test/test_app/Plugin/TestPlugin/View/Layouts/Emails/text/plug_default.ctp b/lib/Cake/Test/test_app/Plugin/TestPlugin/View/Layouts/Emails/text/plug_default.ctp new file mode 100644 index 000000000..39908fd05 --- /dev/null +++ b/lib/Cake/Test/test_app/Plugin/TestPlugin/View/Layouts/Emails/text/plug_default.ctp @@ -0,0 +1,4 @@ + + + +This email was sent using the TestPlugin.