Migrating EmailTest to phpunit

This commit is contained in:
José Lorenzo Rodríguez Urdaneta 2010-06-25 00:02:20 -04:30
parent 7f9f76d012
commit 4689be643b
2 changed files with 65 additions and 63 deletions

View file

@ -272,33 +272,33 @@ class EmailComponent extends Object{
* Temporary store of message header lines * Temporary store of message header lines
* *
* @var array * @var array
* @access private * @access protected
*/ */
private $__header = array(); protected $_header = array();
/** /**
* If set, boundary to use for multipart mime messages * If set, boundary to use for multipart mime messages
* *
* @var string * @var string
* @access private * @access protected
*/ */
private $__boundary = null; protected $_boundary = null;
/** /**
* Temporary store of message lines * Temporary store of message lines
* *
* @var array * @var array
* @access private * @access protected
*/ */
private $__message = array(); protected $_message = array();
/** /**
* Variable that holds SMTP connection * Variable that holds SMTP connection
* *
* @var resource * @var resource
* @access private * @access protected
*/ */
private $__smtpConnection = null; protected $_smtpConnection = null;
/** /**
* Initialize component * Initialize component
@ -306,7 +306,7 @@ class EmailComponent extends Object{
* @param object $controller Instantiating controller * @param object $controller Instantiating controller
*/ */
public function initialize(&$controller, $settings = array()) { public function initialize(&$controller, $settings = array()) {
$this->Controller =& $controller; $this->Controller = $controller;
if (Configure::read('App.encoding') !== null) { if (Configure::read('App.encoding') !== null) {
$this->charset = Configure::read('App.encoding'); $this->charset = Configure::read('App.encoding');
} }
@ -364,24 +364,24 @@ class EmailComponent extends Object{
} }
$message[] = ''; $message[] = '';
$this->__message = $message; $this->_message = $message;
if (!empty($this->attachments)) { if (!empty($this->attachments)) {
$this->_attachFiles(); $this->_attachFiles();
} }
if (!is_null($this->__boundary)) { if (!is_null($this->_boundary)) {
$this->__message[] = ''; $this->_message[] = '';
$this->__message[] = '--' . $this->__boundary . '--'; $this->_message[] = '--' . $this->_boundary . '--';
$this->__message[] = ''; $this->_message[] = '';
} }
$_method = '_' . $this->delivery; $_method = '_' . $this->delivery;
$sent = $this->$_method(); $sent = $this->$_method();
$this->__header = array(); $this->_header = array();
$this->__message = array(); $this->_message = array();
return $sent; return $sent;
} }
@ -406,9 +406,9 @@ class EmailComponent extends Object{
$this->htmlMessage = null; $this->htmlMessage = null;
$this->textMessage = null; $this->textMessage = null;
$this->messageId = true; $this->messageId = true;
$this->__header = array(); $this->_header = array();
$this->__boundary = null; $this->_boundary = null;
$this->__message = array(); $this->_message = array();
} }
/** /**
@ -436,11 +436,11 @@ class EmailComponent extends Object{
if ($this->sendAs === 'both') { if ($this->sendAs === 'both') {
$htmlContent = $content; $htmlContent = $content;
if (!empty($this->attachments)) { if (!empty($this->attachments)) {
$msg[] = '--' . $this->__boundary; $msg[] = '--' . $this->_boundary;
$msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"'; $msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"';
$msg[] = ''; $msg[] = '';
} }
$msg[] = '--alt-' . $this->__boundary; $msg[] = '--alt-' . $this->_boundary;
$msg[] = 'Content-Type: text/plain; charset=' . $this->charset; $msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
$msg[] = 'Content-Transfer-Encoding: 7bit'; $msg[] = 'Content-Transfer-Encoding: 7bit';
$msg[] = ''; $msg[] = '';
@ -452,7 +452,7 @@ class EmailComponent extends Object{
$msg = array_merge($msg, $content); $msg = array_merge($msg, $content);
$msg[] = ''; $msg[] = '';
$msg[] = '--alt-' . $this->__boundary; $msg[] = '--alt-' . $this->_boundary;
$msg[] = 'Content-Type: text/html; charset=' . $this->charset; $msg[] = 'Content-Type: text/html; charset=' . $this->charset;
$msg[] = 'Content-Transfer-Encoding: 7bit'; $msg[] = 'Content-Transfer-Encoding: 7bit';
$msg[] = ''; $msg[] = '';
@ -462,7 +462,7 @@ class EmailComponent extends Object{
$htmlContent = explode("\n", $this->htmlMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent))); $htmlContent = explode("\n", $this->htmlMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
$msg = array_merge($msg, $htmlContent); $msg = array_merge($msg, $htmlContent);
$msg[] = ''; $msg[] = '';
$msg[] = '--alt-' . $this->__boundary . '--'; $msg[] = '--alt-' . $this->_boundary . '--';
$msg[] = ''; $msg[] = '';
return $msg; return $msg;
@ -471,12 +471,12 @@ class EmailComponent extends Object{
if (!empty($this->attachments)) { if (!empty($this->attachments)) {
if ($this->sendAs === 'html') { if ($this->sendAs === 'html') {
$msg[] = ''; $msg[] = '';
$msg[] = '--' . $this->__boundary; $msg[] = '--' . $this->_boundary;
$msg[] = 'Content-Type: text/html; charset=' . $this->charset; $msg[] = 'Content-Type: text/html; charset=' . $this->charset;
$msg[] = 'Content-Transfer-Encoding: 7bit'; $msg[] = 'Content-Transfer-Encoding: 7bit';
$msg[] = ''; $msg[] = '';
} else { } else {
$msg[] = '--' . $this->__boundary; $msg[] = '--' . $this->_boundary;
$msg[] = 'Content-Type: text/plain; charset=' . $this->charset; $msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
$msg[] = 'Content-Transfer-Encoding: 7bit'; $msg[] = 'Content-Transfer-Encoding: 7bit';
$msg[] = ''; $msg[] = '';
@ -504,7 +504,7 @@ class EmailComponent extends Object{
* @access private * @access private
*/ */
function _createboundary() { function _createboundary() {
$this->__boundary = md5(uniqid(time())); $this->_boundary = md5(uniqid(time()));
} }
/** /**
@ -515,7 +515,7 @@ class EmailComponent extends Object{
*/ */
function header($headers) { function header($headers) {
foreach ($headers as $header => $value) { foreach ($headers as $header => $value) {
$this->__header[] = sprintf('%s: %s', trim($header), trim($value)); $this->_header[] = sprintf('%s: %s', trim($header), trim($value));
} }
} }
/** /**
@ -576,7 +576,7 @@ class EmailComponent extends Object{
if (!empty($this->attachments)) { if (!empty($this->attachments)) {
$this->_createBoundary(); $this->_createBoundary();
$headers['MIME-Version'] = '1.0'; $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[] = 'This part of the E-mail should never be seen. If';
$headers[] = 'you are reading this, consider upgrading your e-mail'; $headers[] = 'you are reading this, consider upgrading your e-mail';
$headers[] = 'client to a MIME-compatible client.'; $headers[] = 'client to a MIME-compatible client.';
@ -585,7 +585,7 @@ class EmailComponent extends Object{
} elseif ($this->sendAs === 'html') { } elseif ($this->sendAs === 'html') {
$headers['Content-Type'] = 'text/html; charset=' . $this->charset; $headers['Content-Type'] = 'text/html; charset=' . $this->charset;
} elseif ($this->sendAs === 'both') { } 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'; $headers['Content-Transfer-Encoding'] = '7bit';
@ -601,13 +601,13 @@ class EmailComponent extends Object{
*/ */
function _formatMessage($message) { function _formatMessage($message) {
if (!empty($this->attachments)) { if (!empty($this->attachments)) {
$prefix = array('--' . $this->__boundary); $prefix = array('--' . $this->_boundary);
if ($this->sendAs === 'text') { if ($this->sendAs === 'text') {
$prefix[] = 'Content-Type: text/plain; charset=' . $this->charset; $prefix[] = 'Content-Type: text/plain; charset=' . $this->charset;
} elseif ($this->sendAs === 'html') { } elseif ($this->sendAs === 'html') {
$prefix[] = 'Content-Type: text/html; charset=' . $this->charset; $prefix[] = 'Content-Type: text/html; charset=' . $this->charset;
} elseif ($this->sendAs === 'both') { } 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[] = 'Content-Transfer-Encoding: 7bit';
$prefix[] = ''; $prefix[] = '';
@ -640,13 +640,13 @@ class EmailComponent extends Object{
$data = chunk_split(base64_encode($data)) ; $data = chunk_split(base64_encode($data)) ;
fclose($handle); fclose($handle);
$this->__message[] = '--' . $this->__boundary; $this->_message[] = '--' . $this->_boundary;
$this->__message[] = 'Content-Type: application/octet-stream'; $this->_message[] = 'Content-Type: application/octet-stream';
$this->__message[] = 'Content-Transfer-Encoding: base64'; $this->_message[] = 'Content-Transfer-Encoding: base64';
$this->__message[] = 'Content-Disposition: attachment; filename="' . basename($filename) . '"'; $this->_message[] = 'Content-Disposition: attachment; filename="' . basename($filename) . '"';
$this->__message[] = ''; $this->_message[] = '';
$this->__message[] = $data; $this->_message[] = $data;
$this->__message[] = ''; $this->_message[] = '';
} }
} }
@ -763,8 +763,8 @@ class EmailComponent extends Object{
* @access private * @access private
*/ */
function _mail() { function _mail() {
$header = implode("\n", $this->__header); $header = implode("\n", $this->_header);
$message = implode("\n", $this->__message); $message = implode("\n", $this->_message);
if (is_array($this->to)) { if (is_array($this->to)) {
$to = implode(', ', array_map(array($this, '_formatAddress'), $this->to)); $to = implode(', ', array_map(array($this, '_formatAddress'), $this->to));
} else { } else {
@ -785,10 +785,10 @@ class EmailComponent extends Object{
function _smtp() { function _smtp() {
App::import('Core', array('CakeSocket')); 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()) { if (!$this->_smtpConnection->connect()) {
$this->smtpError = $this->__smtpConnection->lastError(); $this->smtpError = $this->_smtpConnection->lastError();
return false; return false;
} elseif (!$this->_smtpSend(null, '220')) { } elseif (!$this->_smtpSend(null, '220')) {
return false; return false;
@ -852,14 +852,14 @@ class EmailComponent extends Object{
return false; return false;
} }
$header = implode("\r\n", $this->__header); $header = implode("\r\n", $this->_header);
$message = implode("\r\n", $this->__message); $message = implode("\r\n", $this->_message);
if (!$this->_smtpSend($header . "\r\n\r\n" . $message . "\r\n\r\n\r\n.")) { if (!$this->_smtpSend($header . "\r\n\r\n" . $message . "\r\n\r\n\r\n.")) {
return false; return false;
} }
$this->_smtpSend('QUIT', false); $this->_smtpSend('QUIT', false);
$this->__smtpConnection->disconnect(); $this->_smtpConnection->disconnect();
return true; return true;
} }
@ -873,10 +873,10 @@ class EmailComponent extends Object{
*/ */
function _smtpSend($data, $checkCode = '250') { function _smtpSend($data, $checkCode = '250') {
if (!is_null($data)) { if (!is_null($data)) {
$this->__smtpConnection->write($data . "\r\n"); $this->_smtpConnection->write($data . "\r\n");
} }
if ($checkCode !== false) { if ($checkCode !== false) {
$response = $this->__smtpConnection->read(); $response = $this->_smtpConnection->read();
if (preg_match('/^(' . $checkCode . ')/', $response, $code)) { if (preg_match('/^(' . $checkCode . ')/', $response, $code)) {
return $code[0]; return $code[0];
@ -895,8 +895,8 @@ class EmailComponent extends Object{
*/ */
function _debug() { function _debug() {
$nl = "\n"; $nl = "\n";
$header = implode($nl, $this->__header); $header = implode($nl, $this->_header);
$message = implode($nl, $this->__message); $message = implode($nl, $this->_message);
$fm = '<pre>'; $fm = '<pre>';
if (is_array($this->to)) { if (is_array($this->to)) {

View file

@ -20,6 +20,7 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
App::import('Component', 'Email'); App::import('Component', 'Email');
App::import('Core', 'CakeSocket');
/** /**
* EmailTestComponent class * EmailTestComponent class
@ -45,8 +46,8 @@ class EmailTestComponent extends EmailComponent {
* @access public * @access public
* @return void * @return void
*/ */
function setConnectionSocket(&$socket) { function setConnectionSocket($socket) {
$this->__smtpConnection = $socket; $this->_smtpConnection = $socket;
} }
/** /**
@ -56,7 +57,7 @@ class EmailTestComponent extends EmailComponent {
* @return mixed * @return mixed
*/ */
function getConnectionSocket() { function getConnectionSocket() {
return $this->__smtpConnection; return $this->_smtpConnection;
} }
/** /**
@ -66,7 +67,7 @@ class EmailTestComponent extends EmailComponent {
* @return void * @return void
*/ */
function setHeaders($headers) { function setHeaders($headers) {
$this->__header += $headers; $this->_header += $headers;
} }
/** /**
@ -79,7 +80,7 @@ class EmailTestComponent extends EmailComponent {
if (empty($this->_header)) { if (empty($this->_header)) {
return array(); return array();
} }
return $this->__header; return $this->_header;
} }
/** /**
@ -89,7 +90,7 @@ class EmailTestComponent extends EmailComponent {
* @return void * @return void
*/ */
function setBoundary() { function setBoundary() {
$this->__createBoundary(); $this->_createBoundary();
} }
/** /**
@ -99,10 +100,10 @@ class EmailTestComponent extends EmailComponent {
* @return string * @return string
*/ */
function getBoundary() { function getBoundary() {
if (empty($this->__boundary)) { if (empty($this->_boundary)) {
return null; return null;
} }
return $this->__boundary; return $this->_boundary;
} }
/** /**
@ -112,10 +113,10 @@ class EmailTestComponent extends EmailComponent {
* @return string * @return string
*/ */
function getMessage() { function getMessage() {
if (empty($this->__message)) { if (empty($this->_message)) {
return array(); return array();
} }
return $this->__message; return $this->_message;
} }
/** /**
@ -554,14 +555,15 @@ TEXTBLOC;
$socket = new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->Controller->EmailTest->smtpOptions)); $socket = new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->Controller->EmailTest->smtpOptions));
$this->Controller->EmailTest->setConnectionSocket($socket); $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'); $response = $this->Controller->EmailTest->smtpSend('HELO', '250');
$this->assertPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError); $this->assertPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError);
$this->Controller->EmailTest->reset(); $this->Controller->EmailTest->reset();
$response = $this->Controller->EmailTest->smtpSend('HELO somehostname', '250'); $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);
} }
/** /**