Browse Source

test security measure
test new configuration
fixed test cases

Stefan Dickmann 11 years ago
parent
commit
4eab04f799
2 changed files with 52 additions and 8 deletions
  1. 2 1
      Lib/EmailLib.php
  2. 50 7
      Test/Case/Lib/EmailLibTest.php

+ 2 - 1
Lib/EmailLib.php

@@ -87,6 +87,7 @@ class EmailLib extends CakeEmail {
 		} elseif ($message === null && array_key_exists('message', $config = $instance->config())) {
 			$message = $config['message'];
 		}
+		$instance->template(null);
 		return $instance->send($message);
 	}
 
@@ -448,7 +449,7 @@ class EmailLib extends CakeEmail {
 		}
 
 		// Security measure to not sent to the actual addressee in debug mode
-		if (Configure::read('debug')) {
+		if (Configure::read('debug') && Configure::read('Email.live')) {
 			$adminEmail = Configure::read('Config.adminEmail');
 			foreach ($this->_to as $k => $v) {
 				if ($k === $adminEmail) {

+ 50 - 7
Test/Case/Lib/EmailLibTest.php

@@ -16,6 +16,7 @@ class EmailLibTest extends MyCakeTestCase {
 		//$this->skipIf(!file_exists(APP . 'Config' . DS . 'email.php'), 'no email.php');
 
 		$this->Email = new TestEmailLib();
+		$this->Email->template(null);
 	}
 
 	/**
@@ -43,7 +44,7 @@ class EmailLibTest extends MyCakeTestCase {
 		if ($error = $this->Email->getError()) {
 			$this->out($error);
 		}
-		$this->assertEquals('', $this->Email->getError());
+		$this->assertNull($error);
 		$this->assertTrue($res);
 
 		$this->Email->resetAndSet();
@@ -72,7 +73,7 @@ class EmailLibTest extends MyCakeTestCase {
 		//$this->Email->resetAndSet();
 		//$this->Email->from(Configure::read('Config.adminEmail'), Configure::read('Config.adminEmailname'));
 		$res = EmailLib::systemEmail('system-mail test', 'some fast email to admin test');
-		//debug($res);
+
 		$this->assertTrue($res);
 	}
 
@@ -82,7 +83,7 @@ class EmailLibTest extends MyCakeTestCase {
 	 * @return void
 	 */
 	public function testXMailer() {
-		$this->Email = new TestEmailLib();
+		$this->Email->resetAndSet();
 		$this->Email->from('cake@cakephp.org');
 		$this->Email->to('cake@cakephp.org');
 		$this->Email->subject('My title');
@@ -95,7 +96,7 @@ class EmailLibTest extends MyCakeTestCase {
 
 		Configure::write('Config.xMailer', 'Tools Plugin');
 
-		$this->Email = new TestEmailLib();
+		$this->Email->resetAndSet();
 		$this->Email->from('cake@cakephp.org');
 		$this->Email->to('cake@cakephp.org');
 		$this->Email->subject('My title');
@@ -372,8 +373,8 @@ html-part
 	 */
 	public function testValidates() {
 		$this->skipIf(php_sapi_name() === 'cli', 'For now...');
+		Configure::write('Email.live', false);
 
-		$this->Email = new TestEmailLib();
 		$res = $this->Email->validates();
 		$this->assertFalse($res);
 		$res = $this->Email->send();
@@ -482,6 +483,43 @@ HTML;
 		$this->assertTrue(count($is) >= 16);
 	}
 
+	/**
+	 * EmailLibTest::testSendDebugMode()
+	 *
+	 * @return void
+	 */
+	public function testSendDebugMode() {
+		Configure::write('debug', 2);
+		Configure::write('Email.live', true);
+		$this->Email->to('test@test.de', 'test');
+		$this->Email->subject('Test Subject');
+		$this->Email->template(null);
+		$res = $this->Email->send('xyz xyz');
+		$this->assertTrue($res);
+		$toArray = $this->Email->getProtected('to');
+		$this->assertArrayNotHasKey('test@test.de', $toArray);
+
+		Configure::write('Email.live', false);
+		$this->Email->to('test@test.de', 'test');
+		$res = $this->Email->send('xyz xyz');
+		$this->assertTrue($res);
+		$toArray = $this->Email->getProtected('to');
+		$this->assertArrayHasKey('test@test.de', $toArray);
+	}
+
+	/**
+	 * EmailLibTest::testSmtpSettings()
+	 *
+	 * @return void
+	 */
+	public function testSmtpSettings() {
+		Configure::write('debug', 2);
+		Configure::write('Email.Smtp.username', 'test');
+		$this->Email = new TestEmailLib();
+		$settings = $this->Email->getProtected('config');
+		$this->assertEquals('test', $settings['username']);
+	}
+
 }
 
 /**
@@ -515,9 +553,14 @@ class TestEmailLib extends EmailLib {
 		return $this->_boundary;
 	}
 
+	/**
+	 * Get proteceted property of email lib
+	 *
+	 * @param string $attribute
+	 * @return string
+	 */
 	public function getProtected($attribute) {
-		$attribute = '_' . $attribute;
-		return $this->$attribute;
+		return $this->{'_' . $attribute};
 	}
 
 	/**