Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | * @see also WebTest_Mailing_SpoolTest | |
5 | */ | |
6 | ||
e9479dcf EM |
7 | /** |
8 | * Class CRM_Mailing_BAO_SpoolTest | |
acb109b7 | 9 | * @group headless |
e9479dcf | 10 | */ |
6a488035 TO |
11 | class CRM_Mailing_BAO_SpoolTest extends CiviUnitTestCase { |
12 | ||
6c6e6187 | 13 | protected $_mut = NULL; |
6a488035 TO |
14 | |
15 | protected static $bodytext = 'Unit tests keep children safe.'; | |
16 | ||
00be9182 | 17 | public function setUp() { |
6a488035 | 18 | parent::setUp(); |
481a74f4 | 19 | $this->_mut = new CiviMailUtils($this, TRUE); |
6a488035 TO |
20 | } |
21 | ||
00be9182 | 22 | public function tearDown() { |
6a488035 TO |
23 | $this->_mut->stop(); |
24 | parent::tearDown(); | |
25 | } | |
26 | ||
6a488035 | 27 | /** |
eceb18cc | 28 | * Basic send. |
6a488035 | 29 | */ |
00be9182 | 30 | public function testSend() { |
6a488035 TO |
31 | $contact_params_1 = array( |
32 | 'first_name' => substr(sha1(rand()), 0, 7), | |
33 | 'last_name' => 'Anderson', | |
34 | 'email' => substr(sha1(rand()), 0, 7) . '@example.org', | |
35 | 'contact_type' => 'Individual', | |
36 | ); | |
481a74f4 | 37 | $contact_id_1 = $this->individualCreate($contact_params_1); |
6a488035 TO |
38 | |
39 | $contact_params_2 = array( | |
40 | 'first_name' => substr(sha1(rand()), 0, 7), | |
41 | 'last_name' => 'Xylophone', | |
42 | 'email' => substr(sha1(rand()), 0, 7) . '@example.org', | |
43 | 'contact_type' => 'Individual', | |
44 | ); | |
481a74f4 | 45 | $contact_id_2 = $this->individualCreate($contact_params_2); |
6a488035 TO |
46 | |
47 | $subject = 'Test spool'; | |
48 | $params = array( | |
481a74f4 | 49 | 'from' => CRM_Utils_Mail::formatRFC822Email($contact_params_1['first_name'] . " " . $contact_params_1['last_name'], $contact_params_1['email']), |
6a488035 TO |
50 | 'toName' => $contact_params_2['first_name'] . " " . $contact_params_2['last_name'], |
51 | 'toEmail' => $contact_params_2['email'], | |
52 | 'subject' => $subject, | |
53 | 'text' => self::$bodytext, | |
54 | 'html' => "<p>\n" . self::$bodytext . '</p>', | |
55 | ); | |
56 | ||
481a74f4 | 57 | CRM_Utils_Mail::send($params); |
6a488035 | 58 | |
481a74f4 TO |
59 | $mail = $this->_mut->getMostRecentEmail('raw'); |
60 | $this->assertContains("Subject: $subject", $mail); | |
61 | $this->assertContains(self::$bodytext, $mail); | |
6a488035 | 62 | |
481a74f4 | 63 | $mail = $this->_mut->getMostRecentEmail('ezc'); |
6a488035 | 64 | |
481a74f4 TO |
65 | $this->assertEquals($subject, $mail->subject); |
66 | $this->assertContains($contact_params_1['email'], $mail->from->email, 'From address incorrect.'); | |
67 | $this->assertContains($contact_params_2['email'], $mail->to[0]->email, 'Recipient incorrect.'); | |
6a488035 | 68 | |
481a74f4 TO |
69 | $context = new ezcMailPartWalkContext(array(get_class($this), 'mailWalkCallback')); |
70 | $mail->walkParts($context, $mail); | |
6a488035 TO |
71 | } |
72 | ||
4cbe18b8 EM |
73 | /** |
74 | * @param $context | |
75 | * @param $mailPart | |
76 | */ | |
6c6e6187 | 77 | public static function mailWalkCallback($context, $mailPart) { |
481a74f4 TO |
78 | if ($mailPart instanceof ezcMailText) { |
79 | switch ($mailPart->subType) { | |
6c6e6187 | 80 | case 'plain': |
481a74f4 | 81 | self::assertContains(self::$bodytext, $mailPart->generateBody()); |
6c6e6187 TO |
82 | break; |
83 | ||
84 | case 'html': | |
481a74f4 | 85 | self::assertContains(self::$bodytext . '</p>', $mailPart->generateBody()); |
6c6e6187 | 86 | break; |
6a488035 TO |
87 | } |
88 | } | |
89 | } | |
96025800 | 90 | |
6a488035 | 91 | } |