(NFC) (dev/core#878) Simplify '@copyright' annotation
[civicrm-core.git] / tests / phpunit / CRM / Core / Config / MailerTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CiviCRM
31 * @copyright CiviCRM LLC https://civicrm.org/licensing
32 * $Id: $
33 *
34 */
35
36 /**
37 * Class CRM_Core_Config_MailerTest
38 * @group headless
39 */
40 class CRM_Core_Config_MailerTest extends CiviUnitTestCase {
41
42 /**
43 * @var array (string=>int) Keep count of the #times different functions are called
44 */
45 public $calls;
46
47 public function setUp() {
48 $this->calls = [
49 'civicrm_alterMailer' => 0,
50 'send' => 0,
51 ];
52 parent::setUp();
53 }
54
55 public function testHookAlterMailer() {
56 $test = $this;
57 $mockMailer = new CRM_Utils_FakeObject([
58 'send' => function ($recipients, $headers, $body) use ($test) {
59 $test->calls['send']++;
60 $test->assertEquals(['to@example.org'], $recipients);
61 $test->assertEquals('Subject Example', $headers['Subject']);
62 },
63 ]);
64
65 CRM_Utils_Hook::singleton()->setHook('civicrm_alterMailer',
66 function (&$mailer, $driver, $params) use ($test, $mockMailer) {
67 $test->calls['civicrm_alterMailer']++;
68 $test->assertTrue(is_string($driver) && !empty($driver));
69 $test->assertTrue(is_array($params));
70 $test->assertTrue(is_callable([$mailer, 'send']));
71 $mailer = $mockMailer;
72 }
73 );
74
75 $params = [];
76 $params['groupName'] = 'CRM_Core_Config_MailerTest';
77 $params['from'] = 'From Example <from@example.com>';
78 $params['toName'] = 'To Example';
79 $params['toEmail'] = 'to@example.org';
80 $params['subject'] = 'Subject Example';
81 $params['text'] = 'Example text';
82 $params['html'] = '<p>Example HTML</p>';
83 CRM_Utils_Mail::send($params);
84
85 $this->assertEquals(1, $this->calls['civicrm_alterMailer']);
86 $this->assertEquals(1, $this->calls['send']);
87
88 // once more, just to make sure the hooks are called right #times
89 CRM_Utils_Mail::send($params);
90 CRM_Utils_Mail::send($params);
91 $this->assertEquals(1, $this->calls['civicrm_alterMailer']);
92 $this->assertEquals(3, $this->calls['send']);
93 }
94
95 }