Merge pull request #5926 from cakephp/issue-5764

Fix for issue #5764
This commit is contained in:
Mark Story 2015-02-24 21:51:26 -05:00
commit 077b16cb4b
2 changed files with 55 additions and 9 deletions

View file

@ -336,6 +336,13 @@ class CakeEmail {
*/ */
protected $_configClass = 'EmailConfig'; protected $_configClass = 'EmailConfig';
/**
* An instance of the EmailConfig class can be set here
*
* @var string
*/
protected $_configInstance;
/** /**
* Constructor * Constructor
* *
@ -353,6 +360,11 @@ class CakeEmail {
if ($config) { if ($config) {
$this->config($config); $this->config($config);
} elseif (class_exists($this->_configClass) && config('email')) {
$this->_configInstance = new $this->_configClass();
if (isset($this->_configInstance->default)) {
$this->config('default');
}
} }
if (empty($this->headerCharset)) { if (empty($this->headerCharset)) {
$this->headerCharset = $this->charset; $this->headerCharset = $this->charset;
@ -1223,14 +1235,16 @@ class CakeEmail {
*/ */
protected function _applyConfig($config) { protected function _applyConfig($config) {
if (is_string($config)) { if (is_string($config)) {
if (!class_exists($this->_configClass) && !config('email')) { if (!$this->_configInstance) {
throw new ConfigureException(__d('cake_dev', '%s not found.', APP . 'Config' . DS . 'email.php')); if (!class_exists($this->_configClass) && !config('email')) {
throw new ConfigureException(__d('cake_dev', '%s not found.', APP . 'Config' . DS . 'email.php'));
}
$this->_configInstance = new $this->_configClass();
} }
$configs = new $this->_configClass(); if (!isset($this->_configInstance->{$config})) {
if (!isset($configs->{$config})) {
throw new ConfigureException(__d('cake_dev', 'Unknown email configuration "%s".', $config)); throw new ConfigureException(__d('cake_dev', 'Unknown email configuration "%s".', $config));
} }
$config = $configs->{$config}; $config = $this->_configInstance->{$config};
} }
$this->_config = $config + $this->_config; $this->_config = $config + $this->_config;
if (!empty($config['charset'])) { if (!empty($config['charset'])) {

View file

@ -17,6 +17,7 @@
*/ */
App::uses('CakeEmail', 'Network/Email'); App::uses('CakeEmail', 'Network/Email');
App::uses('File', 'Utility');
/** /**
* Help to test CakeEmail * Help to test CakeEmail
@ -92,6 +93,15 @@ class TestCakeEmail extends CakeEmail {
*/ */
class TestEmailConfig { class TestEmailConfig {
/**
* default config
*
* @var array
*/
public $default = array(
'subject' => 'Default Subject',
);
/** /**
* test config * test config
* *
@ -145,6 +155,14 @@ class CakeEmailTest extends CakeTestCase {
*/ */
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->_configFileExists = true;
$emailConfig = new File(APP . 'Config' . DS . 'email.php');
if (!$emailConfig->exists()) {
$this->_configFileExists = false;
$emailConfig->create();
}
$this->CakeEmail = new TestCakeEmail(); $this->CakeEmail = new TestCakeEmail();
App::build(array( App::build(array(
@ -160,6 +178,19 @@ class CakeEmailTest extends CakeTestCase {
public function tearDown() { public function tearDown() {
parent::tearDown(); parent::tearDown();
App::build(); App::build();
if (!$this->_configFileExists) {
unlink(APP . 'Config' . DS . 'email.php');
}
}
/**
* Test if the EmailConfig::$default configuration is read when present
*
* @return void
*/
public function testDefaultConfig() {
$this->assertEquals('Default Subject', $this->CakeEmail->subject());
} }
/** /**
@ -860,16 +891,17 @@ class CakeEmailTest extends CakeTestCase {
$config = array('test' => 'ok', 'test2' => true); $config = array('test' => 'ok', 'test2' => true);
$this->CakeEmail->config($config); $this->CakeEmail->config($config);
$this->assertSame($config, $transportClass->config()); $this->assertSame($config, $transportClass->config());
$this->assertSame($config, $this->CakeEmail->config()); $expected = $config + array('subject' => 'Default Subject');
$this->assertSame($expected, $this->CakeEmail->config());
$this->CakeEmail->config(array()); $this->CakeEmail->config(array());
$this->assertSame($config, $transportClass->config()); $this->assertSame($config, $transportClass->config());
$config = array('test' => 'test@example.com'); $config = array('test' => 'test@example.com', 'subject' => 'my test subject');
$this->CakeEmail->config($config); $this->CakeEmail->config($config);
$expected = array('test' => 'test@example.com', 'test2' => true); $expected = array('test' => 'test@example.com', 'subject' => 'my test subject', 'test2' => true);
$this->assertSame($expected, $this->CakeEmail->config()); $this->assertSame($expected, $this->CakeEmail->config());
$this->assertSame($expected, $transportClass->config()); $this->assertSame(array('test' => 'test@example.com', 'test2' => true), $transportClass->config());
} }
/** /**