Permalink
Browse files

Adding EmailComponent::_getSocket() so EmailComponent + smtp is easie…

…r to test.

Fixing issue where hosts with portnames could cause smtp emails to fail.
Added tests, fixed an existing test to not depend on a local mailserver.
Fixes #1433
  • Loading branch information...
1 parent db5e18f commit 45cf1a66d36c5d7350df9d4c1fd894e8a0af6e69 @markstory markstory committed Jan 7, 2011
@@ -702,6 +702,16 @@ function __mail() {
return @mail($this->to, $this->__encode($this->subject), $message, $header, $this->additionalParams);
}
/**
+ * Helper method to get socket, overridden in tests
+ *
+ * @param array $config Config data for the socket.
+ * @return void
+ * @access protected
+ */
+ function _getSocket($config) {
+ $this->__smtpConnection =& new CakeSocket($config);
+ }
+/**
* Sends out email via SMTP
*
* @return bool Success
@@ -710,7 +720,14 @@ function __mail() {
function __smtp() {
App::import('Core', array('Socket'));
- $this->__smtpConnection =& new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->smtpOptions));
+ $defaults = array(
+ 'host' => 'localhost',
+ 'port' => 25,
+ 'protocol' => 'smtp',
+ 'timeout' => 30
+ );
+ $this->smtpOptions = array_merge($defaults, $this->smtpOptions);
+ $this->_getSocket($this->smtpOptions);
if (!$this->__smtpConnection->connect()) {
$this->smtpError = $this->__smtpConnection->lastError();
@@ -724,7 +741,7 @@ function __smtp() {
if (isset($this->smtpOptions['client'])) {
$host = $this->smtpOptions['client'];
} elseif (!empty($httpHost)) {
- $host = $httpHost;
+ list($host) = explode(':', $httpHost);
} else {
$host = 'localhost';
}
@@ -833,4 +850,4 @@ function __debug() {
}
}
-?>
+?>
@@ -24,13 +24,19 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Component', 'Email');
+App::import('Core', 'Socket');
+
+Mock::generate('CakeSocket', 'MockEmailSocket');
+
/**
* EmailTestComponent class
*
* @package cake
* @subpackage cake.tests.cases.libs.controller.components
*/
class EmailTestComponent extends EmailComponent {
+ var $smtpSend = '';
+ var $_debugSmtp = false;
/**
* smtpSend method override for testing
*
@@ -41,6 +47,18 @@ function smtpSend($data, $code = '250') {
return parent::__smtpSend($data, $code);
}
/**
+ * testing stub
+ *
+ * @return void
+ */
+ function __smtpSend($data, $code = '250') {
+ if ($this->_debugSmtp) {
+ $this->smtpSend .= $data . "\n";
+ return true;
+ }
+ return parent::__smtpSend($data, $code);
+ }
+/**
* Convenience setter method for testing.
*
* @access public
@@ -50,6 +68,18 @@ function setConnectionSocket(&$socket) {
$this->__smtpConnection = $socket;
}
/**
+ * Allows mocks to be used with tests.
+ *
+ * @param array $config
+ * @return void
+ */
+ function _getSocket($config) {
+ if (empty($this->__smtpConnection)) {
+ parent::_getSocket($config);
+ }
+ }
+
+/**
* Convenience getter method for testing.
*
* @access public
@@ -275,6 +305,34 @@ function testSmtpSend() {
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
}
/**
+ * test sending smtp from a host using a port.
+ *
+ * @return void
+ */
+ function testSmtpSendHostWithPort() {
+ $bkp = env('HTTP_HOST');
+ $_SERVER['HTTP_HOST'] = 'localhost:8080';
+
+ $this->Controller->EmailTest->reset();
+ $this->Controller->EmailTest->to = 'root@localhost';
+ $this->Controller->EmailTest->from = 'noreply@example.com';
+ $this->Controller->EmailTest->subject = 'Cake SMTP host test';
+ $this->Controller->EmailTest->replyTo = 'noreply@example.com';
+ $this->Controller->EmailTest->template = null;
+ $this->Controller->EmailTest->delivery = 'smtp';
+ $this->Controller->EmailTest->sendAs = 'text';
+ $this->Controller->EmailTest->_debugSmtp = true;
+
+ $socket = new MockEmailSocket();
+ $socket->setReturnValue('connect', true);
+
+ $this->Controller->EmailTest->setConnectionSocket($socket);
+ $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
+
+ $this->assertPattern('/HELO localhost\n/', $this->Controller->EmailTest->smtpSend);
+ $_SERVER['HTTP_HOST'] = $bkp;
+ }
+/**
* testAuthenticatedSmtpSend method
*
* @access public

0 comments on commit 45cf1a6

Please sign in to comment.