diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 42bffc9cb6d..11728ffd52e 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -183,6 +183,13 @@ class CakeEmail { */ protected $_viewVars = array(); +/** + * Theme for the View + * + * @var array + */ + protected $_theme = null; + /** * Helpers to be used in the render * @@ -776,6 +783,20 @@ public function viewVars($viewVars = null) { return $this; } +/** + * Theme to use when rendering + * + * @param string $theme + * @return mixed + */ + public function theme($theme = null) { + if ($theme === null) { + return $this->_theme; + } + $this->_theme = $theme; + return $this; + } + /** * Helpers to be used in render * @@ -1078,7 +1099,7 @@ protected function _applyConfig($config) { $simpleMethods = array( 'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc', 'messageId', 'subject', 'viewRender', 'viewVars', 'attachments', - 'transport', 'emailFormat' + 'transport', 'emailFormat', 'theme', ); foreach ($simpleMethods as $method) { if (isset($config[$method])) { @@ -1123,6 +1144,7 @@ public function reset() { $this->_template = ''; $this->_viewRender = 'View'; $this->_viewVars = array(); + $this->_theme = null; $this->_helpers = array('Html'); $this->_textMessage = ''; $this->_htmlMessage = ''; @@ -1483,6 +1505,9 @@ protected function _renderTemplates($content) { } elseif ($layoutPlugin) { $View->plugin = $layoutPlugin; } + if ($this->_theme) { + $View->theme = $this->_theme; + } foreach ($types as $type) { $View->set('content', $content); diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index 1dec09404ab..8c73411dc3b 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -81,6 +81,7 @@ class EmailConfig { 'to' => array('test@example.com' => 'Testname'), 'subject' => 'Test mail subject', 'transport' => 'Debug', + 'theme' => 'TestTheme', ); } @@ -568,6 +569,19 @@ public function testTemplate() { $this->assertSame($this->CakeEmail->template(), $expected); } +/** + * testTheme method + * + * @return void + */ + public function testTheme() { + $this->assertSame(null, $this->CakeEmail->theme()); + + $this->CakeEmail->theme('default'); + $expected = 'default'; + $this->assertSame($expected, $this->CakeEmail->theme()); + } + /** * testViewVars method * @@ -679,6 +693,9 @@ public function testConfigString() { $result = $this->CakeEmail->subject(); $this->assertEquals($configs->test['subject'], $result); + $result = $this->CakeEmail->theme(); + $this->assertEquals($configs->test['theme'], $result); + $result = $this->CakeEmail->transport(); $this->assertEquals($configs->test['transport'], $result); @@ -958,6 +975,28 @@ public function testSendRenderJapanese() { $this->assertContains('To: ', $result['headers']); } +/** + * testSendRenderThemed method + * + * @return void + */ + public function testSendRenderThemed() { + $this->CakeEmail->reset(); + $this->CakeEmail->transport('debug'); + + $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->theme('TestTheme'); + $this->CakeEmail->template('themed', 'default'); + $result = $this->CakeEmail->send(); + + $this->assertContains('In TestTheme', $result['message']); + $this->assertContains('Message-ID: ', $result['headers']); + $this->assertContains('To: ', $result['headers']); + } + /** * testSendRenderWithVars method * @@ -1058,6 +1097,12 @@ public function testSendRenderPlugin() { $this->assertContains('Into TestPlugin.', $result['message']); $this->assertContains('This email was sent using the TestPlugin.', $result['message']); + // test plugin template overridden by theme + $this->CakeEmail->theme('TestTheme'); + $result = $this->CakeEmail->send(); + + $this->assertContains('Into TestPlugin. (themed)', $result['message']); + $this->CakeEmail->viewVars(array('value' => 12345)); $result = $this->CakeEmail->template('custom', 'TestPlugin.plug_default')->send(); $this->assertContains('Here is your value: 12345', $result['message']); @@ -1215,10 +1260,12 @@ public function testMessage() { */ public function testReset() { $this->CakeEmail->to('cake@cakephp.org'); + $this->CakeEmail->theme('TestTheme'); $this->assertSame($this->CakeEmail->to(), array('cake@cakephp.org' => 'cake@cakephp.org')); $this->CakeEmail->reset(); $this->assertSame($this->CakeEmail->to(), array()); + $this->assertSame(null, $this->CakeEmail->theme()); } /** diff --git a/lib/Cake/Test/test_app/View/Themed/TestTheme/Emails/text/themed.ctp b/lib/Cake/Test/test_app/View/Themed/TestTheme/Emails/text/themed.ctp new file mode 100644 index 00000000000..5ea24fbc73e --- /dev/null +++ b/lib/Cake/Test/test_app/View/Themed/TestTheme/Emails/text/themed.ctp @@ -0,0 +1 @@ +In TestTheme diff --git a/lib/Cake/Test/test_app/View/Themed/TestTheme/Plugin/TestPlugin/Emails/text/test_plugin_tpl.ctp b/lib/Cake/Test/test_app/View/Themed/TestTheme/Plugin/TestPlugin/Emails/text/test_plugin_tpl.ctp new file mode 100644 index 00000000000..74a205d59b2 --- /dev/null +++ b/lib/Cake/Test/test_app/View/Themed/TestTheme/Plugin/TestPlugin/Emails/text/test_plugin_tpl.ctp @@ -0,0 +1 @@ +Into TestPlugin. (themed)