Commit | Line | Data |
---|---|---|
72ad6c1b | 1 | <?php |
c8e4bea0 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
c8e4bea0 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
c8e4bea0 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
c8e4bea0 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CiviCRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
c8e4bea0 TO |
32 | * $Id: $ |
33 | * | |
34 | */ | |
72ad6c1b TO |
35 | |
36 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
37 | ||
4cbe18b8 EM |
38 | /** |
39 | * Class CRM_Core_Config_MailerTest | |
40 | */ | |
72ad6c1b TO |
41 | class CRM_Core_Config_MailerTest extends CiviUnitTestCase { |
42 | ||
43 | /** | |
44 | * @var array (string=>int) Keep count of the #times different functions are called | |
45 | */ | |
46 | var $calls; | |
47 | ||
00be9182 | 48 | public function setUp() { |
72ad6c1b TO |
49 | $this->calls = array( |
50 | 'civicrm_alterMailer' => 0, | |
51 | 'send' => 0, | |
52 | ); | |
53 | parent::setUp(); | |
54 | } | |
55 | ||
00be9182 | 56 | public function testHookAlterMailer() { |
72ad6c1b TO |
57 | $test = $this; |
58 | $mockMailer = new CRM_Utils_FakeObject(array( | |
c301f76e | 59 | 'send' => function ($recipients, $headers, $body) use ($test) { |
60 | $test->calls['send']++; | |
61 | $test->assertEquals(array('to@example.org'), $recipients); | |
62 | $test->assertEquals('Subject Example', $headers['Subject']); | |
63 | }, | |
72ad6c1b TO |
64 | )); |
65 | ||
66 | CRM_Utils_Hook::singleton()->setHook('civicrm_alterMailer', | |
c301f76e | 67 | function (&$mailer, $driver, $params) use ($test, $mockMailer) { |
68 | $test->calls['civicrm_alterMailer']++; | |
69 | $test->assertTrue(is_string($driver) && !empty($driver)); | |
70 | $test->assertTrue(is_array($params)); | |
71 | $test->assertTrue(is_callable(array($mailer, 'send'))); | |
72 | $mailer = $mockMailer; | |
73 | } | |
72ad6c1b TO |
74 | ); |
75 | ||
76 | $params = array(); | |
77 | $params['groupName'] = 'CRM_Core_Config_MailerTest'; | |
78 | $params['from'] = 'From Example <from@example.com>'; | |
79 | $params['toName'] = 'To Example'; | |
80 | $params['toEmail'] = 'to@example.org'; | |
81 | $params['subject'] = 'Subject Example'; | |
82 | $params['text'] = 'Example text'; | |
83 | $params['html'] = '<p>Example HTML</p>'; | |
84 | CRM_Utils_Mail::send($params); | |
85 | ||
86 | $this->assertEquals(1, $this->calls['civicrm_alterMailer']); | |
87 | $this->assertEquals(1, $this->calls['send']); | |
88 | ||
89 | // once more, just to make sure the hooks are called right #times | |
90 | CRM_Utils_Mail::send($params); | |
91 | CRM_Utils_Mail::send($params); | |
92 | $this->assertEquals(1, $this->calls['civicrm_alterMailer']); | |
93 | $this->assertEquals(3, $this->calls['send']); | |
94 | } | |
95 | ||
96 | } |