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
31 class api_v3_MailingABTest
extends CiviUnitTestCase
{
32 protected $_mailingID_A;
33 protected $_mailingID_B;
34 protected $_mailingID_C;
36 protected $_apiversion = 3;
37 protected $_entity = 'MailingAB';
40 public function setUp() {
42 $this->useTransaction(TRUE);
43 $this->createLoggedInUser();
44 $this->_mailingID_A
= $this->createMailing();
45 $this->_mailingID_B
= $this->createMailing();
46 $this->_mailingID_C
= $this->createMailing();
47 $this->_groupID
= $this->groupCreate();
49 $this->_params
= array(
50 'mailing_id_a' => $this->_mailingID_A
,
51 'mailing_id_b' => $this->_mailingID_B
,
52 'mailing_id_c' => $this->_mailingID_C
,
53 'testing_criteria' => 'subject',
54 'winner_criteria' => 'open',
55 'declare_winning_time' => '+2 days',
56 'group_percentage' => 10,
61 * Test civicrm_mailing_create.
63 public function testMailingABCreateSuccess() {
64 $result = $this->callAPIAndDocument($this->_entity
, 'create', $this->_params
, __FUNCTION__
, __FILE__
);
65 $this->assertTrue(is_numeric($result['id']), "In line " . __LINE__
);
66 $this->assertEquals($this->_params
['group_percentage'], $result['values'][$result['id']]['group_percentage']);
70 * Test civicrm_mailing_delete.
72 public function testMailerDeleteSuccess() {
73 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
75 $this->assertDBQuery(1, "SELECT count(*) FROM civicrm_mailing_abtest WHERE id = %1", array(
76 1 => array($result['id'], 'Integer'),
78 $this->assertDBQuery(3, "SELECT count(*) FROM civicrm_mailing WHERE id IN (%1,%2,%3)", array(
79 1 => array($this->_mailingID_A
, 'Integer'),
80 2 => array($this->_mailingID_B
, 'Integer'),
81 3 => array($this->_mailingID_C
, 'Integer'),
84 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $result['id']));
86 $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_mailing_abtest WHERE id = %1", array(
87 1 => array($result['id'], 'Integer'),
89 $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_mailing WHERE id IN (%1,%2,%3)", array(
90 1 => array($this->_mailingID_A
, 'Integer'),
91 2 => array($this->_mailingID_B
, 'Integer'),
92 3 => array($this->_mailingID_C
, 'Integer'),
99 public function groupPctProvider() {
100 $cases = array(); // array(int $totalSize, int $groupPct, int $expectedCountA, $expectedCountB, $expectedCountC)
101 $cases[] = array(400, 7, 28, 28, 344);
102 $cases[] = array(100, 10, 10, 10, 80);
103 $cases[] = array(50, 20, 10, 10, 30);
104 $cases[] = array(50, 10, 5, 5, 40);
105 $cases[] = array(3, 10, 1, 1, 1);
106 $cases[] = array(2, 10, 1, 1, 0);
107 $cases[] = array(1, 10, 1, 0, 0);
112 * Create a test and ensure that all three mailings (A/B/C) wind up with the correct
113 * number of recipients.
115 * @param $totalGroupContacts
117 * @param $expectedCountA
118 * @param $expectedCountB
119 * @param $expectedCountC
120 * @dataProvider groupPctProvider
122 public function testDistribution($totalGroupContacts, $groupPct, $expectedCountA, $expectedCountB, $expectedCountC) {
124 $result = $this->groupContactCreate($this->_groupID
, $totalGroupContacts);
125 $this->assertEquals($totalGroupContacts, $result['added'], "in line " . __LINE__
);
127 $params = $this->_params
;
128 $params['group_percentage'] = $groupPct;
129 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
131 $this->callAPISuccess('Mailing', 'create', array(
132 'id' => $this->_mailingID_A
,
133 'groups' => array('include' => array($this->_groupID
)),
135 $this->assertJobCounts(0, 0, 0);
137 $this->callAPISuccess('MailingAB', 'submit', array(
138 'id' => $result['id'],
139 'status' => 'Testing',
140 'scheduled_date' => date('YmdHis'),
141 'approval_date' => date('YmdHis'),
143 $this->assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC);
144 $this->assertJobCounts(1, 1, 0);
146 $this->callAPISuccess('MailingAB', 'submit', array(
147 'id' => $result['id'],
149 'scheduled_date' => date('YmdHis'),
150 'approval_date' => date('YmdHis'),
152 $this->assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC);
153 $this->assertJobCounts(1, 1, 1);
157 * @param $expectedCountA
158 * @param $expectedCountB
159 * @param $expectedCountC
161 protected function assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC) {
162 $countA = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_A
));
163 $countB = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_B
));
164 $countC = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_C
));
165 $this->assertEquals($expectedCountA, $countA, "check mailing recipients A in line " . __LINE__
);
166 $this->assertEquals($expectedCountB, $countB, "check mailing recipients B in line " . __LINE__
);
167 $this->assertEquals($expectedCountC, $countC, "check mailing recipients C in line " . __LINE__
);
175 protected function assertJobCounts($expectedA, $expectedB, $expectedC) {
176 $this->assertDBQuery($expectedA, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
182 $this->assertDBQuery($expectedB, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
188 $this->assertDBQuery($expectedC, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(