2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/CiviMailUtils.php';
6 * @see also WebTest_Mailing_SpoolTest
10 * Class CRM_Mailing_BAO_SpoolTest
12 class CRM_Mailing_BAO_SpoolTest
extends CiviUnitTestCase
{
14 protected $_mut = null;
16 protected static $bodytext = 'Unit tests keep children safe.';
18 public function setUp() {
20 $this->_mut
= new CiviMailUtils( $this, true );
23 public function tearDown() {
32 public function testSend() {
33 $contact_params_1 = array(
34 'first_name' => substr(sha1(rand()), 0, 7),
35 'last_name' => 'Anderson',
36 'email' => substr(sha1(rand()), 0, 7) . '@example.org',
37 'contact_type' => 'Individual',
39 $contact_id_1 = $this->individualCreate( $contact_params_1 );
41 $contact_params_2 = array(
42 'first_name' => substr(sha1(rand()), 0, 7),
43 'last_name' => 'Xylophone',
44 'email' => substr(sha1(rand()), 0, 7) . '@example.org',
45 'contact_type' => 'Individual',
47 $contact_id_2 = $this->individualCreate( $contact_params_2 );
49 $subject = 'Test spool';
51 'from' => CRM_Utils_Mail
::formatRFC822Email( $contact_params_1['first_name'] . " " . $contact_params_1['last_name'], $contact_params_1['email'] ),
52 'toName' => $contact_params_2['first_name'] . " " . $contact_params_2['last_name'],
53 'toEmail' => $contact_params_2['email'],
54 'subject' => $subject,
55 'text' => self
::$bodytext,
56 'html' => "<p>\n" . self
::$bodytext . '</p>',
59 CRM_Utils_Mail
::send( $params );
61 $mail = $this->_mut
->getMostRecentEmail( 'raw' );
62 $this->assertContains( "Subject: $subject", $mail );
63 $this->assertContains( self
::$bodytext, $mail );
65 $mail = $this->_mut
->getMostRecentEmail( 'ezc' );
67 $this->assertEquals( $subject, $mail->subject
);
68 $this->assertContains( $contact_params_1['email'], $mail->from
->email
, 'From address incorrect.' );
69 $this->assertContains( $contact_params_2['email'], $mail->to
[0]->email
, 'Recipient incorrect.' );
71 $context = new ezcMailPartWalkContext( array( get_class($this), 'mailWalkCallback' ) );
72 $mail->walkParts( $context, $mail );
79 public static function mailWalkCallback( $context, $mailPart ) {
80 if ( $mailPart instanceof ezcMailText
) {
81 switch ( $mailPart->subType
) {
83 self
::assertContains( self
::$bodytext, $mailPart->generateBody() );
86 self
::assertContains( self
::$bodytext . '</p>', $mailPart->generateBody() );