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/>.
27 * Test APIv3 civicrm_mailingab_* functions
32 class api_v3_MailingABTest
extends CiviUnitTestCase
{
33 protected $_mailingID_A;
34 protected $_mailingID_B;
35 protected $_mailingID_C;
37 protected $_apiversion = 3;
38 protected $_entity = 'MailingAB';
41 public function setUp() {
43 $this->useTransaction(TRUE);
44 $this->createLoggedInUser();
45 $this->_mailingID_A
= $this->createMailing([
46 'subject' => 'subject a ' . time(),
47 'body_text' => 'body_text a ' . time(),
49 $this->_mailingID_B
= $this->createMailing([
50 'subject' => 'subject b ' . time(),
51 'body_text' => 'body_text b ' . time(),
53 $this->_mailingID_C
= $this->createMailing([
54 'subject' => 'not yet ' . time(),
55 'body_text' => 'not yet ' . time(),
57 $this->_groupID
= $this->groupCreate();
60 'mailing_id_a' => $this->_mailingID_A
,
61 'mailing_id_b' => $this->_mailingID_B
,
62 'mailing_id_c' => $this->_mailingID_C
,
63 'testing_criteria' => 'subject',
64 'winner_criteria' => 'open',
65 'declare_winning_time' => '+2 days',
66 'group_percentage' => 10,
71 * Test civicrm_mailing_create.
73 public function testMailingABCreateSuccess() {
74 $result = $this->callAPIAndDocument($this->_entity
, 'create', $this->_params
, __FUNCTION__
, __FILE__
);
75 $this->assertTrue(is_numeric($result['id']), "In line " . __LINE__
);
76 $this->assertEquals($this->_params
['group_percentage'], $result['values'][$result['id']]['group_percentage']);
80 * Test civicrm_mailing_delete.
82 public function testMailerDeleteSuccess() {
83 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
85 $this->assertDBQuery(1, "SELECT count(*) FROM civicrm_mailing_abtest WHERE id = %1", [
86 1 => [$result['id'], 'Integer'],
88 $this->assertDBQuery(3, "SELECT count(*) FROM civicrm_mailing WHERE id IN (%1,%2,%3)", [
89 1 => [$this->_mailingID_A
, 'Integer'],
90 2 => [$this->_mailingID_B
, 'Integer'],
91 3 => [$this->_mailingID_C
, 'Integer'],
94 $this->callAPISuccess($this->_entity
, 'delete', ['id' => $result['id']]);
96 $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_mailing_abtest WHERE id = %1", [
97 1 => [$result['id'], 'Integer'],
99 $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_mailing WHERE id IN (%1,%2,%3)", [
100 1 => [$this->_mailingID_A
, 'Integer'],
101 2 => [$this->_mailingID_B
, 'Integer'],
102 3 => [$this->_mailingID_C
, 'Integer'],
109 public function groupPctProvider() {
110 // array(int $totalSize, int $groupPct, int $expectedCountA, $expectedCountB, $expectedCountC)
112 $cases[] = [400, 7, 28, 28, 344];
113 $cases[] = [100, 10, 10, 10, 80];
114 $cases[] = [50, 20, 10, 10, 30];
115 $cases[] = [50, 10, 5, 5, 40];
116 $cases[] = [3, 10, 1, 1, 1];
117 $cases[] = [2, 10, 1, 1, 0];
118 $cases[] = [1, 10, 1, 0, 0];
123 * Create a test and ensure that all three mailings (A/B/C) wind up with the correct
124 * number of recipients.
126 * @param $totalGroupContacts
128 * @param $expectedCountA
129 * @param $expectedCountB
130 * @param $expectedCountC
131 * @dataProvider groupPctProvider
133 public function testDistribution($totalGroupContacts, $groupPct, $expectedCountA, $expectedCountB, $expectedCountC) {
134 $result = $this->groupContactCreate($this->_groupID
, $totalGroupContacts, TRUE);
135 $this->assertEquals($totalGroupContacts, $result['added'], "in line " . __LINE__
);
137 $params = $this->_params
;
138 $params['group_percentage'] = $groupPct;
139 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
141 $this->callAPISuccess('Mailing', 'create', [
142 'id' => $this->_mailingID_A
,
143 'groups' => ['include' => [$this->_groupID
]],
145 $this->assertJobCounts(0, 0, 0);
147 $this->callAPISuccess('MailingAB', 'submit', [
148 'id' => $result['id'],
149 'status' => 'Testing',
150 'scheduled_date' => date('YmdHis'),
151 'approval_date' => date('YmdHis'),
153 $this->assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC);
154 $this->assertJobCounts(1, 1, 0);
156 $this->callAPISuccess('MailingAB', 'submit', [
157 'id' => $result['id'],
159 'scheduled_date' => date('YmdHis'),
160 'approval_date' => date('YmdHis'),
162 $this->assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC);
163 $this->assertJobCounts(1, 1, 1);
167 * Create a test. Declare the second mailing a winner. Ensure that key
168 * fields propagate to the final mailing.
170 public function testSubmitWinnderId() {
171 $checkSyncFields = ['subject', 'body_text'];
173 $result = $this->groupContactCreate($this->_groupID
, 20, TRUE);
174 $this->assertEquals(20, $result['added'], "in line " . __LINE__
);
176 $params = $this->_params
;
177 $params['group_percentage'] = 10;
178 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
180 $this->callAPISuccess('Mailing', 'create', [
181 'id' => $this->_mailingID_A
,
182 'groups' => ['include' => [$this->_groupID
]],
184 $this->assertJobCounts(0, 0, 0);
186 $this->callAPISuccess('MailingAB', 'submit', [
187 'id' => $result['id'],
188 'status' => 'Testing',
189 'scheduled_date' => 'now',
190 'approval_date' => 'now',
192 $this->assertJobCounts(1, 1, 0);
194 $b = $this->getApiValues('Mailing', ['id' => $this->_mailingID_B
], $checkSyncFields);
195 $c = $this->getApiValues('Mailing', ['id' => $this->_mailingID_C
], $checkSyncFields);
196 $this->assertNotEquals($b, $c);
198 $this->callAPISuccess('MailingAB', 'submit', [
199 'id' => $result['id'],
201 'winner_id' => $this->_mailingID_B
,
202 'scheduled_date' => 'now',
203 'approval_date' => 'now',
205 $this->assertJobCounts(1, 1, 1);
207 $b = $this->getApiValues('Mailing', ['id' => $this->_mailingID_B
], $checkSyncFields);
208 $c = $this->getApiValues('Mailing', ['id' => $this->_mailingID_C
], $checkSyncFields);
209 $this->assertEquals($b, $c);
213 * Lookup a record via API. Return *only* the expected values.
220 protected function getApiValues($entity, $filter, $return) {
221 $rec = $this->callAPISuccess($entity, 'getsingle', $filter +
['return' => $return]);
222 return CRM_Utils_Array
::subset($rec, $return);
226 * @param $expectedCountA
227 * @param $expectedCountB
228 * @param $expectedCountC
230 protected function assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC) {
231 $countA = $this->callAPISuccess('MailingRecipients', 'getcount', ['mailing_id' => $this->_mailingID_A
]);
232 $countB = $this->callAPISuccess('MailingRecipients', 'getcount', ['mailing_id' => $this->_mailingID_B
]);
233 $countC = $this->callAPISuccess('MailingRecipients', 'getcount', ['mailing_id' => $this->_mailingID_C
]);
234 $this->assertEquals($expectedCountA, $countA, "check mailing recipients A in line " . __LINE__
);
235 $this->assertEquals($expectedCountB, $countB, "check mailing recipients B in line " . __LINE__
);
236 $this->assertEquals($expectedCountC, $countC, "check mailing recipients C in line " . __LINE__
);
244 protected function assertJobCounts($expectedA, $expectedB, $expectedC) {
245 $this->assertDBQuery($expectedA, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', [
251 $this->assertDBQuery($expectedB, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', [
257 $this->assertDBQuery($expectedC, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', [