phpcs - Fix error, "Expected 1 newline at end of file; XXX found".
[civicrm-core.git] / tests / phpunit / CRM / Mailing / BAO / SpoolTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/CiviMailUtils.php';
4
5 /*
6 * @see also WebTest_Mailing_SpoolTest
7 */
8
9 /**
10 * Class CRM_Mailing_BAO_SpoolTest
11 */
12 class CRM_Mailing_BAO_SpoolTest extends CiviUnitTestCase {
13
14 protected $_mut = null;
15
16 protected static $bodytext = 'Unit tests keep children safe.';
17
18 public function setUp() {
19 parent::setUp();
20 $this->_mut = new CiviMailUtils( $this, true );
21 }
22
23 public function tearDown() {
24 $this->_mut->stop();
25 parent::tearDown();
26 }
27
28
29 /**
30 * Basic send
31 */
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',
38 );
39 $contact_id_1 = $this->individualCreate( $contact_params_1 );
40
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',
46 );
47 $contact_id_2 = $this->individualCreate( $contact_params_2 );
48
49 $subject = 'Test spool';
50 $params = array(
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>',
57 );
58
59 CRM_Utils_Mail::send( $params );
60
61 $mail = $this->_mut->getMostRecentEmail( 'raw' );
62 $this->assertContains( "Subject: $subject", $mail );
63 $this->assertContains( self::$bodytext, $mail );
64
65 $mail = $this->_mut->getMostRecentEmail( 'ezc' );
66
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.' );
70
71 $context = new ezcMailPartWalkContext( array( get_class($this), 'mailWalkCallback' ) );
72 $mail->walkParts( $context, $mail );
73 }
74
75 /**
76 * @param $context
77 * @param $mailPart
78 */
79 public static function mailWalkCallback( $context, $mailPart ) {
80 if ( $mailPart instanceof ezcMailText ) {
81 switch ( $mailPart->subType ) {
82 case 'plain':
83 self::assertContains( self::$bodytext, $mailPart->generateBody() );
84 break;
85 case 'html':
86 self::assertContains( self::$bodytext . '</p>', $mailPart->generateBody() );
87 break;
88 }
89 }
90 }
91 }