Added tests for template and layout rendering. Currently fail as component doesn't wordwrap properly. May also explain #4498

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7361 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
TommyO 2008-07-24 13:54:39 +00:00
parent e235e422ed
commit 5dc4388fa0

View file

@ -55,6 +55,8 @@ class EmailTestController extends Controller {
* @access public
*/
var $components = array('Email');
var $pageTitle = 'EmailTest';
}
/**
* EmailTest class
@ -85,6 +87,8 @@ class EmailTest extends CakeTestCase {
$this->Controller->Email->startup($this->Controller);
ClassRegistry::addObject('view', new View($this->Controller));
Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS));
}
/**
* testBadSmtpSend method
@ -117,9 +121,6 @@ class EmailTest extends CakeTestCase {
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->Controller->Email->_debug = true;
if (stristr(PHP_OS, 'win') === false) {
$this->Controller->Email->_newLine = "\n";
}
$this->Controller->Email->sendAs = 'text';
$expect = <<<TEMPDOC
<pre>Host: localhost
@ -144,9 +145,8 @@ This is the body of the message
</pre>
TEMPDOC;
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $expect);
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
}
}
/**
@ -200,12 +200,8 @@ TEMPDOC;
$this->Controller->Email->replyTo = 'noreply@example.com';
$this->Controller->Email->template = null;
$this->Controller->Email->delivery = 'debug';
if (stristr(PHP_OS, 'win') === false) {
$this->Controller->Email->_newLine = "\n";
}
$this->Controller->Email->sendAs = 'text';
$expect = <<<TEMPDOC
$message = <<<MSGBLOC
<pre>To: postmaster@localhost
From: noreply@example.com
Subject: Cake SMTP test
@ -214,7 +210,7 @@ Header:
From: noreply@example.com
Reply-To: noreply@example.com
X-Mailer: CakePHP Email Component
Content-Type: text/plain; charset=UTF-8
Content-Type: {CONTENTTYPE}
Content-Transfer-Encoding: 7bitParameters:
Message:
@ -222,22 +218,145 @@ Message:
This is the body of the message
</pre>
TEMPDOC;
MSGBLOC;
$this->Controller->Email->sendAs = 'text';
$expect = str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $message);
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $expect);
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
$this->Controller->Email->sendAs = 'html';
$expect = str_replace('Content-Type: text/plain; charset=UTF-8', 'Content-Type: text/html; charset=UTF-8', $expect);
$expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $message);
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $expect);
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
// TODO: better test for format of message sent?
$this->Controller->Email->sendAs = 'both';
$expect = str_replace('Content-Type: text/html; charset=UTF-8', 'Content-Type: multipart/alternative; boundary="alt-"' . "\n", $expect);
$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"' . "\n", $message);
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $expect);
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
}
}
/**
* testTemplates method
*
* @access public
* @return void
*/
function testTemplates() {
if (@fsockopen('localhost', 25)) {
$this->assertTrue(@fsockopen('localhost', 25), 'Local mail server is running');
$this->Controller->Email->reset();
$this->Controller->Email->to = 'postmaster@localhost';
$this->Controller->Email->from = 'noreply@example.com';
$this->Controller->Email->subject = 'Cake SMTP test';
$this->Controller->Email->replyTo = 'noreply@example.com';
$this->Controller->Email->delivery = 'debug';
$header = <<<HEADBLOC
To: postmaster@localhost
From: noreply@example.com
Subject: Cake SMTP test
Header:
From: noreply@example.com
Reply-To: noreply@example.com
X-Mailer: CakePHP Email Component
Content-Type: {CONTENTTYPE}
Content-Transfer-Encoding: 7bitParameters:
Message:
HEADBLOC;
$this->Controller->Email->layout = 'default';
$this->Controller->Email->template = 'default';
$text = <<<TEXTBLOC
This is the body of the message
This email was sent using the CakePHP Framework, http://cakephp.org.
TEXTBLOC;
$html = <<<HTMLBLOC
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>EmailTest</title>
</head>
<body>
<p> This is the body of the message</p><p> </p>
<p>This email was sent using the <a href="http://cakephp.org">CakePHP
Framework</a></p>
</body>
</html>
HTMLBLOC;
$this->Controller->Email->sendAs = 'text';
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
$this->Controller->Email->sendAs = 'html';
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
$this->Controller->Email->sendAs = 'both';
$expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"' . "\n", $header);
$expect .= '--alt-' . "\n" . 'Content-Type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $text . "\n\n";
$expect .= '--alt-' . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $html . "\n\n";
$expect = '<pre>' . $expect . '--alt---' . "\n\n" . '</pre>';
$this->assertTrue($this->Controller->Email->send('This is the body of the message'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
$text = <<<TEXTBLOC
This element has some text that is just too wide to comply with email
standards.
This is the body of the message
This email was sent using the CakePHP Framework, http://cakephp.org.
TEXTBLOC;
$this->Controller->Email->sendAs = 'text';
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
$this->assertTrue($this->Controller->Email->send('This is the body of the message', 'wide'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
$html = <<<HTMLBLOC
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>EmailTest</title>
</head>
<body>
<p> This is the body of the message</p><p> </p>
<p>This email was sent using the CakePHP Framework</p>
</body>
</html>
HTMLBLOC;
$this->Controller->Email->sendAs = 'html';
$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
$this->assertTrue($this->Controller->Email->send('This is the body of the message', 'default', 'thin'));
$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
}
}
/**
* testSendDebug method
*
@ -272,6 +391,10 @@ TEMPDOC;
$expected = "Previous content\n--alt-\n text/html; utf-8\n 7bit\n\n<p>My own html content</p>";
$this->assertEqual($result, $expected);
}
function __osFix($string) {
return str_replace(array("\r\n", "\n"), $this->Controller->Email->_newLine, $string);
}
}
?>