3 * File for the TestMailing class
9 * This file is part of CiviCRM
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.
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.
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/>.
26 require_once 'CiviTest/CiviUnitTestCase.php';
30 * Test APIv3 civicrm_mailing_* functions
34 class api_v3_MailingTest
extends CiviUnitTestCase
{
37 protected $_apiversion = 3;
38 protected $_params = array();
39 protected $_entity = 'Mailing';
48 'description' => 'Test all Mailer methods.',
49 'group' => 'CiviCRM API Tests',
55 $this->_groupID
= $this->groupCreate();
56 $this->_email
= 'test@test.test';
57 $this->_params
= array(
59 'body_text' => 'bdkfhdskfhduew',
60 'name' => 'mailing name',
66 $this->groupDelete($this->_groupID
);
70 * Test civicrm_mailing_create
72 public function testMailerCreateSuccess() {
73 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
74 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
75 $this->assertEquals(1, $jobs['count']);
76 unset($this->_params
['created_id']); // return isn't working on this in getAndCheck so lets not check it for now
77 $this->getAndCheck($this->_params
, $result['id'], 'mailing');
80 public function testMailerPreview() {
81 $contactID = $this->individualCreate();
82 $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
83 $displayName = $displayName['values'][$contactID]['display_name'];
84 $result = $this->callAPISuccess('mailing', 'create', $this->_params
);
85 $params = array('id' => $result['id'], 'contact_id' => $contactID);
86 $result = $this->callAPISuccess('mailing', 'preview', $params);
87 $text = $result['values']['text'];
90 $this->assertEquals("This is $displayName", $text); // verify the text returned is correct, with replaced token
91 $this->deleteMailing($result['id']);
93 public function testMailerSendTestMail() {
94 $contactID = $this->individualCreate();
95 $result = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
96 $email = $result['values'][$contactID]['email'];
97 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
98 $params = array('mailing_id' => $mail['id'], 'test_email' => $email, 'test_group' => NULL);
99 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
100 $this->assertEquals(1, $deliveredInfo['count'], "in line " . __LINE__
); // verify mail has been sent to user by count
101 $this->assertEquals($contactID, $deliveredInfo['values'][$deliveredInfo['id']]['contact_id'], "in line " . __LINE__
); //verify the contact_id of the recipient
102 $this->deleteMailing($mail['id']);
104 public function testMailerStats() {
105 $result = $this->groupContactCreate($this->_groupID
, 100);
106 $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group
107 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
108 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID
, 'is_test' => 0);
109 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
110 $deliveredIds = implode(',', array_keys($deliveredInfo['values']));
111 foreach (array('bounce', 'unsubscribe', 'opened') as $type) {
112 $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp
113 (event_queue_id int, time_stamp datetime, delivered_id int)
114 SELECT event_queue_id, time_stamp, id
115 FROM civicrm_mailing_event_delivered
116 WHERE id IN ($deliveredIds)
117 ORDER BY RAND() LIMIT 0,20;";
119 CRM_Core_DAO
::executeQuery($sql);
120 $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
121 CRM_Core_DAO
::executeQuery($sql);
122 if ($type == 'unsubscribe') {
123 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe)
124 SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
127 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
128 SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
130 CRM_Core_DAO
::executeQuery($sql);
132 $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
133 $expectedResult = array(
134 'Delivered' => 80, //since among 100 mails 20 has been bounced
137 'Unique Clicks' => 0,
138 'Unsubscribers' => 20
141 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
145 * Test civicrm_mailing_delete
147 public function testMailerDeleteSuccess() {
148 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
149 $jobs = $this->callAPIAndDocument($this->_entity
, 'delete', array('id' => $result['id']), __FUNCTION__
, __FILE__
);
150 $this->assertAPIDeleted($this->_entity
, $result['id']);
153 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
155 //------------ civicrm_mailing_event_bounce methods------------
158 * Test civicrm_mailing_event_bounce with wrong params.
159 * Note that tests like this are slightly better than no test but an
160 * api function cannot be considered supported / 'part of the api' without a
163 public function testMailerBounceWrongParams() {
165 'job_id' => 'Wrong ID',
166 'event_queue_id' => 'Wrong ID',
167 'hash' => 'Wrong Hash',
169 'time_stamp' => '20111109212100',
171 $result = $this->callAPIFailure('mailing_event', 'bounce', $params,
172 'Queue event could not be found'
176 //----------- civicrm_mailing_event_confirm methods -----------
179 * Test civicrm_mailing_event_confirm with wrong params.
180 * Note that tests like this are slightly better than no test but an
181 * api function cannot be considered supported / 'part of the api' without a
184 public function testMailerConfirmWrongParams() {
186 'contact_id' => 'Wrong ID',
187 'subscribe_id' => 'Wrong ID',
188 'hash' => 'Wrong Hash',
189 'event_subscribe_id' => '123',
190 'time_stamp' => '20111111010101',
192 $result = $this->callAPIFailure('mailing_event', 'confirm', $params,
193 'Confirmation failed'
197 //---------- civicrm_mailing_event_reply methods -----------
200 * Test civicrm_mailing_event_reply with wrong params.
202 * Note that tests like this are slightly better than no test but an
203 * api function cannot be considered supported / 'part of the api' without a
206 public function testMailerReplyWrongParams() {
208 'job_id' => 'Wrong ID',
209 'event_queue_id' => 'Wrong ID',
210 'hash' => 'Wrong Hash',
211 'bodyTxt' => 'Body...',
212 'replyTo' => $this->_email
,
213 'time_stamp' => '20111111010101',
215 $result = $this->callAPIFailure('mailing_event', 'reply', $params,
216 'Queue event could not be found'
221 //----------- civicrm_mailing_event_forward methods ----------
224 * Test civicrm_mailing_event_forward with wrong params.
225 * Note that tests like this are slightly better than no test but an
226 * api function cannot be considered supported / 'part of the api' without a
229 public function testMailerForwardWrongParams() {
231 'job_id' => 'Wrong ID',
232 'event_queue_id' => 'Wrong ID',
233 'hash' => 'Wrong Hash',
234 'email' => $this->_email
,
235 'time_stamp' => '20111111010101',
237 $result = $this->callAPIFailure('mailing_event', 'forward', $params,
238 'Queue event could not be found'
242 //----------- civicrm_mailing_create ----------