chs fixed1
[civicrm-core.git] / tests / phpunit / api / v3 / MailingTest.php
CommitLineData
6a488035 1<?php
8891a36e 2/*
6a488035
TO
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
26require_once 'CiviTest/CiviUnitTestCase.php';
27
28
29/**
30 * Test APIv3 civicrm_mailing_* functions
31 *
32 * @package CiviCRM
33 */
34class api_v3_MailingTest extends CiviUnitTestCase {
35 protected $_groupID;
36 protected $_email;
c43d01f3 37 protected $_apiversion = 3;
38 protected $_params = array();
e37b4b04 39 protected $_entity = 'Mailing';
b7c9bc4c 40
430ae6dd 41
4cbe18b8
EM
42 /**
43 * @return array
44 */
430ae6dd 45 function get_info() {
6a488035
TO
46 return array(
47 'name' => 'Mailer',
48 'description' => 'Test all Mailer methods.',
49 'group' => 'CiviCRM API Tests',
50 );
51 }
52
53 function setUp() {
54 parent::setUp();
fadb804f 55 $this->_groupID = $this->groupCreate();
ef170fe0 56 $this->_email = 'test@test.test';
c43d01f3 57 $this->_params = array(
58 'subject' => 'maild',
59 'body_text' => 'bdkfhdskfhduew',
60 'name' => 'mailing name',
61 'created_id' => 1,
62 );
6a488035
TO
63 }
64
65 function tearDown() {
66 $this->groupDelete($this->_groupID);
67 }
68
69 /**
70 * Test civicrm_mailing_create
71 */
72 public function testMailerCreateSuccess() {
c43d01f3 73 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params, __FUNCTION__, __FILE__);
74 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
6a488035 75 $this->assertEquals(1, $jobs['count']);
ef170fe0 76 unset($this->_params['created_id']); // return isn't working on this in getAndCheck so lets not check it for now
e37b4b04 77 $this->getAndCheck($this->_params, $result['id'], 'mailing');
6a488035
TO
78 }
79
ef643544 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'];
88 print_r($result );
89 print_r($text );
90 $this->assertEquals("This is $displayName", $text); // verify the text returned is correct, with replaced token
91 $this->deleteMailing($result['id']);
92 }
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']);
103 }
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)
114SELECT event_queue_id, time_stamp, id
115 FROM civicrm_mailing_event_delivered
116 WHERE id IN ($deliveredIds)
117 ORDER BY RAND() LIMIT 0,20;";
118 print_r($sql );
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)
124SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
125 }
126 else {
127 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
128SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
129 }
130 CRM_Core_DAO::executeQuery($sql);
131 }
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
135 'Bounces' => 20,
136 'Opened' => 20,
137 'Unique Clicks' => 0,
138 'Unsubscribers' => 20
139 );
140
141 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
142 }
143
c43d01f3 144 /**
30d44db2 145 * Test civicrm_mailing_delete
c43d01f3 146 */
147 public function testMailerDeleteSuccess() {
e37b4b04 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']);
c43d01f3 151 }
6a488035
TO
152
153 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
154
155 //------------ civicrm_mailing_event_bounce methods------------
156
157 /**
158 * Test civicrm_mailing_event_bounce with wrong params.
e37b4b04 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
161 * success test
6a488035
TO
162 */
163 public function testMailerBounceWrongParams() {
164 $params = array(
165 'job_id' => 'Wrong ID',
166 'event_queue_id' => 'Wrong ID',
167 'hash' => 'Wrong Hash',
168 'body' => 'Body...',
6a488035
TO
169 'time_stamp' => '20111109212100',
170 );
e37b4b04 171 $result = $this->callAPIFailure('mailing_event', 'bounce', $params,
30d44db2 172 'Queue event could not be found'
173 );
6a488035
TO
174 }
175
176 //----------- civicrm_mailing_event_confirm methods -----------
177
178 /**
179 * Test civicrm_mailing_event_confirm with wrong params.
e37b4b04 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
182 * success test
6a488035
TO
183 */
184 public function testMailerConfirmWrongParams() {
185 $params = array(
186 'contact_id' => 'Wrong ID',
187 'subscribe_id' => 'Wrong ID',
188 'hash' => 'Wrong Hash',
189 'event_subscribe_id' => '123',
190 'time_stamp' => '20111111010101',
ef170fe0 191 );
e37b4b04 192 $result = $this->callAPIFailure('mailing_event', 'confirm', $params,
193 'Confirmation failed'
194 );
6a488035
TO
195 }
196
197 //---------- civicrm_mailing_event_reply methods -----------
198
199 /**
200 * Test civicrm_mailing_event_reply with wrong params.
e37b4b04 201 *
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
204 * success test
6a488035
TO
205 */
206 public function testMailerReplyWrongParams() {
207 $params = array(
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',
ef170fe0 214 );
e37b4b04 215 $result = $this->callAPIFailure('mailing_event', 'reply', $params,
216 'Queue event could not be found'
30d44db2 217 );
6a488035
TO
218 }
219
220
221 //----------- civicrm_mailing_event_forward methods ----------
222
223 /**
224 * Test civicrm_mailing_event_forward with wrong params.
e37b4b04 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
227 * success test
6a488035
TO
228 */
229 public function testMailerForwardWrongParams() {
230 $params = array(
231 'job_id' => 'Wrong ID',
232 'event_queue_id' => 'Wrong ID',
233 'hash' => 'Wrong Hash',
234 'email' => $this->_email,
235 'time_stamp' => '20111111010101',
ef170fe0 236 );
e37b4b04 237 $result = $this->callAPIFailure('mailing_event', 'forward', $params,
238 'Queue event could not be found'
239 );
6a488035
TO
240 }
241
6a488035
TO
242//----------- civicrm_mailing_create ----------
243
244}