CRM-15578 - Mailing API - Add "preview_recipients" action
[civicrm-core.git] / tests / phpunit / api / v3 / MailingTest.php
1 <?php
2 /*
3 * File for the TestMailing class
4 *
5 * (PHP 5)
6 *
7 * @package CiviCRM
8 *
9 * This file is part of CiviCRM
10 *
11 * CiviCRM is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Affero General Public License
13 * as published by the Free Software Foundation; either version 3 of
14 * the License, or (at your option) any later version.
15 *
16 * CiviCRM is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public
22 * License along with this program. If not, see
23 * <http://www.gnu.org/licenses/>.
24 */
25
26 require_once 'CiviTest/CiviUnitTestCase.php';
27
28
29 /**
30 * Test APIv3 civicrm_mailing_* functions
31 *
32 * @package CiviCRM
33 */
34 class api_v3_MailingTest extends CiviUnitTestCase {
35 protected $_groupID;
36 protected $_email;
37 protected $_apiversion = 3;
38 protected $_params = array();
39 protected $_entity = 'Mailing';
40 protected $_groupIDs; // array(string $pseudonym => int $id)
41 protected $_contactIDs; // array(string $pseudonym => int $id)
42
43 /**
44 * @return array
45 */
46 function get_info() {
47 return array(
48 'name' => 'Mailer',
49 'description' => 'Test all Mailer methods.',
50 'group' => 'CiviCRM API Tests',
51 );
52 }
53
54 function setUp() {
55 parent::setUp();
56 $this->_contactIDs = array();
57 $this->_groupID = $this->groupCreate();
58 $this->_groupIDs = array();
59 $this->_email = 'test@test.test';
60 $this->_params = array(
61 'subject' => 'maild',
62 'body_text' => "This is {contact.display_name}",
63 'name' => 'mailing name',
64 'created_id' => 1,
65 );
66 }
67
68 function tearDown() {
69 foreach ($this->_contactIDs as $contactID) {
70 $this->contactDelete($contactID);
71 }
72 $this->groupDelete($this->_groupID);
73 foreach ($this->_groupIDs as $groupID) {
74 $this->groupDelete($groupID);
75 }
76 }
77
78 /**
79 * Test civicrm_mailing_create
80 */
81 public function testMailerCreateSuccess() {
82 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params, __FUNCTION__, __FILE__);
83 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
84 $this->assertEquals(1, $jobs['count']);
85 unset($this->_params['created_id']); // return isn't working on this in getAndCheck so lets not check it for now
86 $this->getAndCheck($this->_params, $result['id'], 'mailing');
87 }
88
89 public function testMailerPreview() {
90 $contactID = $this->individualCreate();
91 $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
92 $displayName = $displayName['values'][$contactID]['display_name'];
93
94 $result = $this->callAPISuccess('mailing', 'create', $this->_params);
95
96 $params = array('id' => $result['id'], 'contact_id' => $contactID);
97 $result = $this->callAPISuccess('mailing', 'preview', $params);
98 $text = $result['values']['text'];
99 $this->assertEquals("This is $displayName", $text); // verify the text returned is correct, with replaced token
100 $this->deleteMailing($result['id']);
101 }
102
103 public function testMailerPreviewRecipients() {
104 // BEGIN SAMPLE DATA
105 $this->groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
106 $this->groupIDs['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
107 $this->contactIDs['includeme'] = $this->individualCreate(array('include.me@example.org'));
108 $this->contactIDs['excludeme'] = $this->individualCreate(array('exclude.me@example.org'));
109 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs['inc'], 'contact_id' => $this->contactIDs['includeme']));
110 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs['inc'], 'contact_id' => $this->contactIDs['excludeme']));
111 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs['exc'], 'contact_id' => $this->contactIDs['excludeme']));
112
113 $params = $this->_params;
114 $params['groups']['include'] = array($this->groupIDs['inc']);
115 $params['groups']['exclude'] = array($this->groupIDs['exc']);
116 $params['mailings']['include'] = array();
117 $params['mailings']['exclude'] = array();
118 // END SAMPLE DATA
119
120 $maxIDs = array(
121 'mailing' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
122 'job' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
123 'group' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
124 );
125 $preview = $this->callAPIAndDocument('Mailing', 'preview_recipients', $params, __FUNCTION__, __FILE__);
126 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
127 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
128 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
129
130 $previewIds = array_values(CRM_Utils_Array::collect('contact_id', $preview['values']));
131 $this->assertEquals(array((string)$this->contactIDs['includeme']), $previewIds);
132 }
133
134 public function testMailerSendTestMail() {
135 $contactID = $this->individualCreate();
136 $result = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
137 $email = $result['values'][$contactID]['email'];
138
139 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
140
141 $params = array('mailing_id' => $mail['id'], 'test_email' => $email, 'test_group' => NULL);
142 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
143 $this->assertEquals(1, $deliveredInfo['count'], "in line " . __LINE__); // verify mail has been sent to user by count
144 $this->assertEquals($contactID, $deliveredInfo['values'][$deliveredInfo['id']]['contact_id'], "in line " . __LINE__); //verify the contact_id of the recipient
145 $this->deleteMailing($mail['id']);
146 }
147
148 public function testMailerStats() {
149 $result = $this->groupContactCreate($this->_groupID, 100);
150 $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group
151
152 //Create and send test mail first and change the mail job to live,
153 //because stats api only works on live mail
154 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
155 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID);
156 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
157 $deliveredIds = implode(',', array_keys($deliveredInfo['values']));
158
159 //Change the test mail into live
160 $sql = "UPDATE civicrm_mailing_job SET is_test = 0 WHERE mailing_id = {$mail['id']}";
161 CRM_Core_DAO::executeQuery($sql);
162
163 foreach (array('bounce', 'unsubscribe', 'opened') as $type) {
164 $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp
165 (event_queue_id int, time_stamp datetime, delivered_id int)
166 SELECT event_queue_id, time_stamp, id
167 FROM civicrm_mailing_event_delivered
168 WHERE id IN ($deliveredIds)
169 ORDER BY RAND() LIMIT 0,20;";
170 CRM_Core_DAO::executeQuery($sql);
171
172 $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
173 CRM_Core_DAO::executeQuery($sql);
174
175 if ($type == 'unsubscribe') {
176 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe)
177 SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
178 }
179 else {
180 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
181 SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
182 }
183 CRM_Core_DAO::executeQuery($sql);
184 }
185
186 $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
187 $expectedResult = array(
188 'Delivered' => 80, //since among 100 mails 20 has been bounced
189 'Bounces' => 20,
190 'Opened' => 20,
191 'Unique Clicks' => 0,
192 'Unsubscribers' => 20
193 );
194 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
195 $this->deleteMailing($mail['id']);
196 }
197 /**
198 * Test civicrm_mailing_delete
199 */
200 public function testMailerDeleteSuccess() {
201 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
202 $jobs = $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
203 $this->assertAPIDeleted($this->_entity, $result['id']);
204 }
205
206 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
207
208 //------------ civicrm_mailing_event_bounce methods------------
209
210 /**
211 * Test civicrm_mailing_event_bounce with wrong params.
212 * Note that tests like this are slightly better than no test but an
213 * api function cannot be considered supported / 'part of the api' without a
214 * success test
215 */
216 public function testMailerBounceWrongParams() {
217 $params = array(
218 'job_id' => 'Wrong ID',
219 'event_queue_id' => 'Wrong ID',
220 'hash' => 'Wrong Hash',
221 'body' => 'Body...',
222 'time_stamp' => '20111109212100',
223 );
224 $result = $this->callAPIFailure('mailing_event', 'bounce', $params,
225 'Queue event could not be found'
226 );
227 }
228
229 //----------- civicrm_mailing_event_confirm methods -----------
230
231 /**
232 * Test civicrm_mailing_event_confirm with wrong params.
233 * Note that tests like this are slightly better than no test but an
234 * api function cannot be considered supported / 'part of the api' without a
235 * success test
236 */
237 public function testMailerConfirmWrongParams() {
238 $params = array(
239 'contact_id' => 'Wrong ID',
240 'subscribe_id' => 'Wrong ID',
241 'hash' => 'Wrong Hash',
242 'event_subscribe_id' => '123',
243 'time_stamp' => '20111111010101',
244 );
245 $result = $this->callAPIFailure('mailing_event', 'confirm', $params,
246 'Confirmation failed'
247 );
248 }
249
250 //---------- civicrm_mailing_event_reply methods -----------
251
252 /**
253 * Test civicrm_mailing_event_reply with wrong params.
254 *
255 * Note that tests like this are slightly better than no test but an
256 * api function cannot be considered supported / 'part of the api' without a
257 * success test
258 */
259 public function testMailerReplyWrongParams() {
260 $params = array(
261 'job_id' => 'Wrong ID',
262 'event_queue_id' => 'Wrong ID',
263 'hash' => 'Wrong Hash',
264 'bodyTxt' => 'Body...',
265 'replyTo' => $this->_email,
266 'time_stamp' => '20111111010101',
267 );
268 $result = $this->callAPIFailure('mailing_event', 'reply', $params,
269 'Queue event could not be found'
270 );
271 }
272
273
274 //----------- civicrm_mailing_event_forward methods ----------
275
276 /**
277 * Test civicrm_mailing_event_forward with wrong params.
278 * Note that tests like this are slightly better than no test but an
279 * api function cannot be considered supported / 'part of the api' without a
280 * success test
281 */
282 public function testMailerForwardWrongParams() {
283 $params = array(
284 'job_id' => 'Wrong ID',
285 'event_queue_id' => 'Wrong ID',
286 'hash' => 'Wrong Hash',
287 'email' => $this->_email,
288 'time_stamp' => '20111111010101',
289 );
290 $result = $this->callAPIFailure('mailing_event', 'forward', $params,
291 'Queue event could not be found'
292 );
293 }
294
295 //----------- civicrm_mailing_create ----------
296
297 }