Skip to content

Commit

Permalink
Migrating EmailTest to phpunit
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 25, 2010
1 parent 7f9f76d commit 4689be6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 63 deletions.
102 changes: 51 additions & 51 deletions cake/libs/controller/components/email.php
Expand Up @@ -272,41 +272,41 @@ class EmailComponent extends Object{
* Temporary store of message header lines
*
* @var array
* @access private
* @access protected
*/
private $__header = array();
protected $_header = array();

/**
* If set, boundary to use for multipart mime messages
*
* @var string
* @access private
* @access protected
*/
private $__boundary = null;
protected $_boundary = null;

/**
* Temporary store of message lines
*
* @var array
* @access private
* @access protected
*/
private $__message = array();
protected $_message = array();

/**
* Variable that holds SMTP connection
*
* @var resource
* @access private
* @access protected
*/
private $__smtpConnection = null;
protected $_smtpConnection = null;

/**
* Initialize component
*
* @param object $controller Instantiating controller
*/
public function initialize(&$controller, $settings = array()) {
$this->Controller =& $controller;
$this->Controller = $controller;
if (Configure::read('App.encoding') !== null) {
$this->charset = Configure::read('App.encoding');
}
Expand Down Expand Up @@ -364,24 +364,24 @@ public function send($content = null, $template = null, $layout = null) {
}

$message[] = '';
$this->__message = $message;
$this->_message = $message;

if (!empty($this->attachments)) {
$this->_attachFiles();
}

if (!is_null($this->__boundary)) {
$this->__message[] = '';
$this->__message[] = '--' . $this->__boundary . '--';
$this->__message[] = '';
if (!is_null($this->_boundary)) {
$this->_message[] = '';
$this->_message[] = '--' . $this->_boundary . '--';
$this->_message[] = '';
}


$_method = '_' . $this->delivery;
$sent = $this->$_method();

$this->__header = array();
$this->__message = array();
$this->_header = array();
$this->_message = array();

return $sent;
}
Expand All @@ -406,9 +406,9 @@ public function reset() {
$this->htmlMessage = null;
$this->textMessage = null;
$this->messageId = true;
$this->__header = array();
$this->__boundary = null;
$this->__message = array();
$this->_header = array();
$this->_boundary = null;
$this->_message = array();
}

/**
Expand Down Expand Up @@ -436,11 +436,11 @@ function _render($content) {
if ($this->sendAs === 'both') {
$htmlContent = $content;
if (!empty($this->attachments)) {
$msg[] = '--' . $this->__boundary;
$msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
$msg[] = '--' . $this->_boundary;
$msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"';
$msg[] = '';
}
$msg[] = '--alt-' . $this->__boundary;
$msg[] = '--alt-' . $this->_boundary;
$msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
$msg[] = 'Content-Transfer-Encoding: 7bit';
$msg[] = '';
Expand All @@ -452,7 +452,7 @@ function _render($content) {
$msg = array_merge($msg, $content);

$msg[] = '';
$msg[] = '--alt-' . $this->__boundary;
$msg[] = '--alt-' . $this->_boundary;
$msg[] = 'Content-Type: text/html; charset=' . $this->charset;
$msg[] = 'Content-Transfer-Encoding: 7bit';
$msg[] = '';
Expand All @@ -462,7 +462,7 @@ function _render($content) {
$htmlContent = explode("\n", $this->htmlMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
$msg = array_merge($msg, $htmlContent);
$msg[] = '';
$msg[] = '--alt-' . $this->__boundary . '--';
$msg[] = '--alt-' . $this->_boundary . '--';
$msg[] = '';

return $msg;
Expand All @@ -471,12 +471,12 @@ function _render($content) {
if (!empty($this->attachments)) {
if ($this->sendAs === 'html') {
$msg[] = '';
$msg[] = '--' . $this->__boundary;
$msg[] = '--' . $this->_boundary;
$msg[] = 'Content-Type: text/html; charset=' . $this->charset;
$msg[] = 'Content-Transfer-Encoding: 7bit';
$msg[] = '';
} else {
$msg[] = '--' . $this->__boundary;
$msg[] = '--' . $this->_boundary;
$msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
$msg[] = 'Content-Transfer-Encoding: 7bit';
$msg[] = '';
Expand Down Expand Up @@ -504,7 +504,7 @@ function _render($content) {
* @access private
*/
function _createboundary() {
$this->__boundary = md5(uniqid(time()));
$this->_boundary = md5(uniqid(time()));
}

/**
Expand All @@ -515,7 +515,7 @@ function _createboundary() {
*/
function header($headers) {
foreach ($headers as $header => $value) {
$this->__header[] = sprintf('%s: %s', trim($header), trim($value));
$this->_header[] = sprintf('%s: %s', trim($header), trim($value));
}
}
/**
Expand Down Expand Up @@ -576,7 +576,7 @@ function _createHeader() {
if (!empty($this->attachments)) {
$this->_createBoundary();
$headers['MIME-Version'] = '1.0';
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->__boundary . '"';
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
$headers[] = 'This part of the E-mail should never be seen. If';
$headers[] = 'you are reading this, consider upgrading your e-mail';
$headers[] = 'client to a MIME-compatible client.';
Expand All @@ -585,7 +585,7 @@ function _createHeader() {
} elseif ($this->sendAs === 'html') {
$headers['Content-Type'] = 'text/html; charset=' . $this->charset;
} elseif ($this->sendAs === 'both') {
$headers['Content-Type'] = 'multipart/alternative; boundary="alt-' . $this->__boundary . '"';
$headers['Content-Type'] = 'multipart/alternative; boundary="alt-' . $this->_boundary . '"';
}

$headers['Content-Transfer-Encoding'] = '7bit';
Expand All @@ -601,13 +601,13 @@ function _createHeader() {
*/
function _formatMessage($message) {
if (!empty($this->attachments)) {
$prefix = array('--' . $this->__boundary);
$prefix = array('--' . $this->_boundary);
if ($this->sendAs === 'text') {
$prefix[] = 'Content-Type: text/plain; charset=' . $this->charset;
} elseif ($this->sendAs === 'html') {
$prefix[] = 'Content-Type: text/html; charset=' . $this->charset;
} elseif ($this->sendAs === 'both') {
$prefix[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
$prefix[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"';
}
$prefix[] = 'Content-Transfer-Encoding: 7bit';
$prefix[] = '';
Expand Down Expand Up @@ -640,13 +640,13 @@ function _attachFiles() {
$data = chunk_split(base64_encode($data)) ;
fclose($handle);

$this->__message[] = '--' . $this->__boundary;
$this->__message[] = 'Content-Type: application/octet-stream';
$this->__message[] = 'Content-Transfer-Encoding: base64';
$this->__message[] = 'Content-Disposition: attachment; filename="' . basename($filename) . '"';
$this->__message[] = '';
$this->__message[] = $data;
$this->__message[] = '';
$this->_message[] = '--' . $this->_boundary;
$this->_message[] = 'Content-Type: application/octet-stream';
$this->_message[] = 'Content-Transfer-Encoding: base64';
$this->_message[] = 'Content-Disposition: attachment; filename="' . basename($filename) . '"';
$this->_message[] = '';
$this->_message[] = $data;
$this->_message[] = '';
}
}

Expand Down Expand Up @@ -763,8 +763,8 @@ function _strip($value, $message = false) {
* @access private
*/
function _mail() {
$header = implode("\n", $this->__header);
$message = implode("\n", $this->__message);
$header = implode("\n", $this->_header);
$message = implode("\n", $this->_message);
if (is_array($this->to)) {
$to = implode(', ', array_map(array($this, '_formatAddress'), $this->to));
} else {
Expand All @@ -785,10 +785,10 @@ function _mail() {
function _smtp() {
App::import('Core', array('CakeSocket'));

$this->__smtpConnection =& new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->smtpOptions));
$this->_smtpConnection = new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->smtpOptions));

if (!$this->__smtpConnection->connect()) {
$this->smtpError = $this->__smtpConnection->lastError();
if (!$this->_smtpConnection->connect()) {
$this->smtpError = $this->_smtpConnection->lastError();
return false;
} elseif (!$this->_smtpSend(null, '220')) {
return false;
Expand Down Expand Up @@ -852,14 +852,14 @@ function _smtp() {
return false;
}

$header = implode("\r\n", $this->__header);
$message = implode("\r\n", $this->__message);
$header = implode("\r\n", $this->_header);
$message = implode("\r\n", $this->_message);
if (!$this->_smtpSend($header . "\r\n\r\n" . $message . "\r\n\r\n\r\n.")) {
return false;
}
$this->_smtpSend('QUIT', false);

$this->__smtpConnection->disconnect();
$this->_smtpConnection->disconnect();
return true;
}

Expand All @@ -873,10 +873,10 @@ function _smtp() {
*/
function _smtpSend($data, $checkCode = '250') {
if (!is_null($data)) {
$this->__smtpConnection->write($data . "\r\n");
$this->_smtpConnection->write($data . "\r\n");
}
if ($checkCode !== false) {
$response = $this->__smtpConnection->read();
$response = $this->_smtpConnection->read();

if (preg_match('/^(' . $checkCode . ')/', $response, $code)) {
return $code[0];
Expand All @@ -895,8 +895,8 @@ function _smtpSend($data, $checkCode = '250') {
*/
function _debug() {
$nl = "\n";
$header = implode($nl, $this->__header);
$message = implode($nl, $this->__message);
$header = implode($nl, $this->_header);
$message = implode($nl, $this->_message);
$fm = '<pre>';

if (is_array($this->to)) {
Expand Down
26 changes: 14 additions & 12 deletions cake/tests/cases/libs/controller/components/email.test.php
Expand Up @@ -20,6 +20,7 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Component', 'Email');
App::import('Core', 'CakeSocket');

/**
* EmailTestComponent class
Expand All @@ -45,8 +46,8 @@ function smtpSend($data, $code = '250') {
* @access public
* @return void
*/
function setConnectionSocket(&$socket) {
$this->__smtpConnection = $socket;
function setConnectionSocket($socket) {
$this->_smtpConnection = $socket;
}

/**
Expand All @@ -56,7 +57,7 @@ function setConnectionSocket(&$socket) {
* @return mixed
*/
function getConnectionSocket() {
return $this->__smtpConnection;
return $this->_smtpConnection;
}

/**
Expand All @@ -66,7 +67,7 @@ function getConnectionSocket() {
* @return void
*/
function setHeaders($headers) {
$this->__header += $headers;
$this->_header += $headers;
}

/**
Expand All @@ -79,7 +80,7 @@ function getHeaders() {
if (empty($this->_header)) {
return array();
}
return $this->__header;
return $this->_header;
}

/**
Expand All @@ -89,7 +90,7 @@ function getHeaders() {
* @return void
*/
function setBoundary() {
$this->__createBoundary();
$this->_createBoundary();
}

/**
Expand All @@ -99,10 +100,10 @@ function setBoundary() {
* @return string
*/
function getBoundary() {
if (empty($this->__boundary)) {
if (empty($this->_boundary)) {
return null;
}
return $this->__boundary;
return $this->_boundary;
}

/**
Expand All @@ -112,10 +113,10 @@ function getBoundary() {
* @return string
*/
function getMessage() {
if (empty($this->__message)) {
if (empty($this->_message)) {
return array();
}
return $this->__message;
return $this->_message;
}

/**
Expand Down Expand Up @@ -554,14 +555,15 @@ function testSmtpSendSocket() {
$socket = new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->Controller->EmailTest->smtpOptions));
$this->Controller->EmailTest->setConnectionSocket($socket);

$this->assertTrue($this->Controller->EmailTest->getConnectionSocket());
$this->assertSame($this->Controller->EmailTest->getConnectionSocket(), $socket);

$response = $this->Controller->EmailTest->smtpSend('HELO', '250');
$this->assertPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError);

$this->Controller->EmailTest->reset();
$response = $this->Controller->EmailTest->smtpSend('HELO somehostname', '250');
$this->assertNoPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError);

$this->assertNoPattern('/501 Syntax: HELO hostname/', (string)$this->Controller->EmailTest->smtpError);
}

/**
Expand Down

0 comments on commit 4689be6

Please sign in to comment.