Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CakeEmail can use theme when rendering template

  • Loading branch information...
commit 6fa30db5048babf40611db90bd31d257a87b46be 1 parent c700fed
@rchavik rchavik authored
View
27 lib/Cake/Network/Email/CakeEmail.php
@@ -184,6 +184,13 @@ class CakeEmail {
protected $_viewVars = array();
/**
+ * Theme for the View
+ *
+ * @var array
+ */
+ protected $_theme = null;
+
+/**
* Helpers to be used in the render
*
* @var array
@@ -777,6 +784,20 @@ public function viewVars($viewVars = null) {
}
/**
+ * 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
*
* @param array $helpers
@@ -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);
View
47 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',
);
}
@@ -569,6 +570,19 @@ public function testTemplate() {
}
/**
+ * 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
*
* @return void
@@ -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);
@@ -959,6 +976,28 @@ public function testSendRenderJapanese() {
}
/**
+ * 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
*
* @return void
@@ -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());
}
/**
View
1  lib/Cake/Test/test_app/View/Themed/TestTheme/Emails/text/themed.ctp
@@ -0,0 +1 @@
+In TestTheme
View
1  lib/Cake/Test/test_app/View/Themed/TestTheme/Plugin/TestPlugin/Emails/text/test_plugin_tpl.ctp
@@ -0,0 +1 @@
+Into TestPlugin. (themed)
Please sign in to comment.
Something went wrong with that request. Please try again.