Merge pull request #16484 from yashodha/dev_1580
[civicrm-core.git] / tests / phpunit / CRM / Utils / Mail / FilteredPearMailerTest.php
1 <?php
2
3 /**
4 * Class CRM_Utils_Mail_FilteredPearMailerTest
5 * @group headless
6 */
7 class CRM_Utils_Mail_FilteredPearMailerTest extends CiviUnitTestCase {
8
9 public function testFilter() {
10 $mock = new class() extends \Mail {
11 public $buf = [];
12
13 public function send($recipients, $headers, $body) {
14 $this->buf['recipients'] = $recipients;
15 $this->buf['headers'] = $headers;
16 $this->buf['body'] = $body;
17 return 'all the fruits in the basket';
18 }
19
20 };
21
22 $fm = new CRM_Utils_Mail_FilteredPearMailer('mock', [], $mock);
23 $fm->addFilter('1000_apple', function ($mailer, &$recipients, &$headers, &$body) {
24 $body .= ' with apples!';
25 });
26 $fm->addFilter('1000_banana', function ($mailer, &$recipients, &$headers, &$body) {
27 $headers['Banana'] = 'Cavendish';
28 });
29 $r = $fm->send(['recip'], ['Subject' => 'Fruit loops'], 'body');
30
31 $this->assertEquals('Fruit loops', $mock->buf['headers']['Subject']);
32 $this->assertEquals('Cavendish', $mock->buf['headers']['Banana']);
33 $this->assertEquals('body with apples!', $mock->buf['body']);
34 $this->assertEquals('all the fruits in the basket', $r);
35 }
36
37 public function testFilter_shortCircuit() {
38 $mock = new class() extends \Mail {
39
40 public function send($recipients, $headers, $body) {
41 return 'all the fruits in the basket';
42 }
43
44 };
45
46 $fm = new CRM_Utils_Mail_FilteredPearMailer('mock', [], $mock);
47 $fm->addFilter('1000_short_circuit', function ($mailer, &$recipients, &$headers, &$body) {
48 return 'the triumph of veggies over fruits';
49 });
50 $r = $fm->send(['recip'], ['Subject' => 'Fruit loops'], 'body');
51 $this->assertEquals('the triumph of veggies over fruits', $r);
52 }
53
54 }