add test for CRM_Mailing_BAO_MailingJob
[civicrm-core.git] / tests / phpunit / CRM / Mailing / BAO / MailingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 * Class CRM_Mailing_BAO_MailingTest
30 */
31 class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
32
33 public function setUp() {
34 parent::setUp();
35 }
36
37 /**
38 * Helper function to assert whether the calculated recipients of a mailing
39 * match the expected list
40 *
41 * @param $mailingID
42 * @param $expectedRecipients array
43 * Array of contact ID that should be in the recipient list.
44 */
45 private function assertRecipientsCorrect($mailingID, $expectedRecipients) {
46
47 // Reset keys to ensure match
48 $expectedRecipients = array_values($expectedRecipients);
49
50 // Load the recipients as a list of contact IDs
51 CRM_Mailing_BAO_Mailing::getRecipients($mailingID);
52 $recipients = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $mailingID));
53 $contactIDs = array();
54 foreach ($recipients['values'] as $recipient) {
55 $contactIDs[] = $recipient['contact_id'];
56 }
57
58 // Check the lists match
59 $this->assertTreeEquals($expectedRecipients, $contactIDs);
60 }
61
62 /**
63 * Helper function to create a mailing include/exclude group.
64 *
65 * @param $mailingID
66 * @param $groupID
67 * @param string $type
68 * @return array|int
69 */
70 private function createMailingGroup($mailingID, $groupID, $type = 'Include') {
71 return $this->callAPISuccess('MailingGroup', 'create', array(
72 'mailing_id' => $mailingID,
73 'group_type' => $type,
74 'entity_table' => "civicrm_group",
75 'entity_id' => $groupID,
76 ));
77 }
78
79 /**
80 * @todo Missing tests:
81 * - Ensure opt out emails are not mailed
82 * - Ensure 'stop' emails are not mailed
83 * - Ensure the deceased are not mailed
84 * - Tests for getLocationFilterAndOrderBy (selecting correct 'type')
85 * - ...
86 */
87
88 /**
89 * Test to ensure that static and smart mailing groups can be added to an
90 * email mailing as 'include' or 'exclude' groups - and the members are
91 * included or excluded appropriately.
92 *
93 * contact 0 : static 0 (inc) + smart 5 (exc)
94 * contact 1 : static 0 (inc)
95 * contact 2 : static 1 (inc)
96 * contact 3 : static 1 (inc)
97 * contact 4 : static 2 (exc) + smart 3 (inc)
98 * contact 5 : smart 3 (inc)
99 * contact 6 : smart 4 (inc)
100 * contact 7 : smart 4 (inc)
101 */
102 public function testgetRecipientsEmailGroupIncludeExclude() {
103
104 // Set up groups; 3 standard, 3 smart
105 $groupIDs = array();
106 for ($i = 0; $i < 6; $i++) {
107 $params = array(
108 'name' => 'Test static group ' . $i,
109 'title' => 'Test static group ' . $i,
110 'is_active' => 1,
111 );
112 if ($i < 3) {
113 $groupIDs[$i] = $this->groupCreate($params);
114 }
115 else {
116 $groupIDs[$i] = $this->smartGroupCreate(array(
117 'formValues' => array('last_name' => 'smart' . $i),
118 ), $params);
119 }
120 }
121
122 // Create contacts
123 $contactIDs = array(
124 0 => $this->individualCreate(array('last_name' => 'smart5'), 0),
125 1 => $this->individualCreate(array(), 1),
126 2 => $this->individualCreate(array(), 2),
127 3 => $this->individualCreate(array(), 3),
128 4 => $this->individualCreate(array('last_name' => 'smart3'), 4),
129 5 => $this->individualCreate(array('last_name' => 'smart3'), 5),
130 6 => $this->individualCreate(array('last_name' => 'smart4'), 6),
131 7 => $this->individualCreate(array('last_name' => 'smart4'), 7),
132 );
133
134 // Add contacts to static groups
135 $this->callAPISuccess('GroupContact', 'Create', array(
136 'group_id' => $groupIDs[0],
137 'contact_id' => $contactIDs[0],
138 ));
139 $this->callAPISuccess('GroupContact', 'Create', array(
140 'group_id' => $groupIDs[0],
141 'contact_id' => $contactIDs[1],
142 ));
143 $this->callAPISuccess('GroupContact', 'Create', array(
144 'group_id' => $groupIDs[1],
145 'contact_id' => $contactIDs[2],
146 ));
147 $this->callAPISuccess('GroupContact', 'Create', array(
148 'group_id' => $groupIDs[1],
149 'contact_id' => $contactIDs[3],
150 ));
151 $this->callAPISuccess('GroupContact', 'Create', array(
152 'group_id' => $groupIDs[2],
153 'contact_id' => $contactIDs[4],
154 ));
155
156 // Force rebuild the smart groups
157 for ($i = 3; $i < 6; $i++) {
158 $group = new CRM_Contact_DAO_Group();
159 $group->id = $groupIDs[$i];
160 $group->find(TRUE);
161 CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
162 }
163
164 // Check that we can include static groups in the mailing.
165 // Expected: Contacts [0-3] should be included.
166 $mailing = $this->callAPISuccess('Mailing', 'create', array());
167 $this->createMailingGroup($mailing['id'], $groupIDs[0]);
168 $this->createMailingGroup($mailing['id'], $groupIDs[1]);
169 $expected = $contactIDs;
170 unset($expected[4], $expected[5], $expected[6], $expected[7]);
171 $this->assertRecipientsCorrect($mailing['id'], $expected);
172
173 // Check that we can include smart groups in the mailing too.
174 // Expected: All contacts should be included.
175 $this->createMailingGroup($mailing['id'], $groupIDs[3]);
176 $this->createMailingGroup($mailing['id'], $groupIDs[4]);
177 $this->assertRecipientsCorrect($mailing['id'], $contactIDs);
178
179 // Check we can exclude static groups from the mailing.
180 // Expected: All contacts except [4]
181 $this->createMailingGroup($mailing['id'], $groupIDs[2], 'Exclude');
182 $expected = $contactIDs;
183 unset($expected[4]);
184 $this->assertRecipientsCorrect($mailing['id'], $expected);
185
186 // Check we can exclude smart groups from the mailing too.
187 // Expected: All contacts except [0] and [4]
188 $this->createMailingGroup($mailing['id'], $groupIDs[5], 'Exclude');
189 $expected = $contactIDs;
190 unset($expected[0], $expected[4]);
191 $this->assertRecipientsCorrect($mailing['id'], $expected);
192
193 // Tear down: delete mailing, groups, contacts
194 $this->deleteMailing($mailing['id']);
195 foreach ($groupIDs as $groupID) {
196 $this->groupDelete($groupID);
197 }
198 foreach ($contactIDs as $contactID) {
199 $this->contactDelete($contactID);
200 }
201
202 }
203
204 /**
205 * Test CRM_Mailing_BAO_Mailing::getRecipients() on sms mode
206 */
207 public function testgetRecipientsSMS() {
208 // Tests for SMS bulk mailing recipients
209 // +CRM-21320 Ensure primary mobile number is selected over non-primary
210
211 // Setup
212 $smartGroupParams = array(
213 'formValues' => array('contact_type' => array('IN' => array('Individual'))),
214 );
215 $group = $this->smartGroupCreate($smartGroupParams);
216 $sms_provider = $this->callAPISuccess('SmsProvider', 'create', array(
217 'sequential' => 1,
218 'name' => 1,
219 'title' => "Test",
220 'username' => "Test",
221 'password' => "Test",
222 'api_type' => 1,
223 'is_active' => 1,
224 ));
225
226 // Create Contact 1 and add in group
227 $contactID1 = $this->individualCreate(array(), 0);
228 $this->callAPISuccess('GroupContact', 'Create', array(
229 'group_id' => $group,
230 'contact_id' => $contactID1,
231 ));
232
233 // Create contact 2 and add in group
234 $contactID2 = $this->individualCreate(array(), 1);
235 $this->callAPISuccess('GroupContact', 'Create', array(
236 'group_id' => $group,
237 'contact_id' => $contactID2,
238 ));
239
240 $contactIDPhoneRecords = array(
241 $contactID1 => array(
242 'primary_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
243 'contact_id' => $contactID1,
244 'phone' => "01 01",
245 'location_type_id' => "Home",
246 'phone_type_id' => "Mobile",
247 'is_primary' => 1,
248 ))),
249 'other_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
250 'contact_id' => $contactID1,
251 'phone' => "01 02",
252 'location_type_id' => "Work",
253 'phone_type_id' => "Mobile",
254 'is_primary' => 0,
255 ))),
256 ),
257 // Create the non-primary with a lower ID than the primary, to test CRM-21320
258 $contactID2 => array(
259 'other_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
260 'contact_id' => $contactID2,
261 'phone' => "02 01",
262 'location_type_id' => "Home",
263 'phone_type_id' => "Mobile",
264 'is_primary' => 0,
265 ))),
266 'primary_phone_id' => CRM_Utils_Array::value('id', $this->callAPISuccess('Phone', 'create', array(
267 'contact_id' => $contactID2,
268 'phone' => "02 02",
269 'location_type_id' => "Work",
270 'phone_type_id' => "Mobile",
271 'is_primary' => 1,
272 ))),
273 ),
274 );
275
276 // Prepare expected results
277 $checkPhoneIDs = array(
278 $contactID1 => $contactIDPhoneRecords[$contactID1]['primary_phone_id'],
279 $contactID2 => $contactIDPhoneRecords[$contactID2]['primary_phone_id'],
280 );
281
282 // Create mailing
283 $mailing = $this->callAPISuccess('Mailing', 'create', array('sms_provider_id' => $sms_provider['id']));
284 $mailingInclude = $this->createMailingGroup($mailing['id'], $group);
285
286 // Get recipients
287 CRM_Mailing_BAO_Mailing::getRecipients($mailing['id']);
288 $recipients = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $mailing['id']));
289
290 // Check the count is correct
291 $this->assertEquals(2, $recipients['count'], 'Check recipient count');
292
293 // Check we got the 'primary' mobile for both contacts
294 foreach ($recipients['values'] as $value) {
295 $this->assertEquals($value['phone_id'], $checkPhoneIDs[$value['contact_id']], 'Check correct phone number for contact ' . $value['contact_id']);
296 }
297
298 // Tidy up
299 $this->deleteMailing($mailing['id']);
300 $this->callAPISuccess('SmsProvider', 'Delete', array('id' => $sms_provider['id']));
301 $this->groupDelete($group);
302 $this->contactDelete($contactID1);
303 $this->contactDelete($contactID2);
304 }
305
306 }