Skip to content

Commit

Permalink
CakeEmail can use theme when rendering template
Browse files Browse the repository at this point in the history
  • Loading branch information
rchavik committed Mar 28, 2012
1 parent c700fed commit 6fa30db
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/Cake/Network/Email/CakeEmail.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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])) {
Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
Expand Up @@ -81,6 +81,7 @@ class EmailConfig {
'to' => array('test@example.com' => 'Testname'),
'subject' => 'Test mail subject',
'transport' => 'Debug',
'theme' => 'TestTheme',
);

}
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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());
}

/**
Expand Down
@@ -0,0 +1 @@
In TestTheme
@@ -0,0 +1 @@
Into TestPlugin. (themed)

0 comments on commit 6fa30db

Please sign in to comment.