Merge pull request #4764 from rohankatkar/CRM-15615
[civicrm-core.git] / tests / phpunit / api / v3 / MailingABTest.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_mailingab_* functions
31 *
32 * @package CiviCRM
33 */
34 class api_v3_MailingABTest extends CiviUnitTestCase {
35 protected $_mailingID_A;
36 protected $_mailingID_B;
37 protected $_mailingID_C;
38 protected $_params;
39 protected $_apiversion = 3;
40 protected $_entity = 'MailingAB';
41 protected $_groupID;
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->useTransaction(TRUE);
57 $this->_mailingID_A = $this->createMailing();
58 $this->_mailingID_B = $this->createMailing();
59 $this->_mailingID_C = $this->createMailing();
60 $this->_groupID = $this->groupCreate();
61
62 $this->_params = array(
63 'mailing_id_a' => $this->_mailingID_A,
64 'mailing_id_b' => $this->_mailingID_B,
65 'mailing_id_c' => $this->_mailingID_C,
66 'testing_criteria_id' => 1,
67 'winner_criteria_id' => 1,
68 'declare_winning_time' => '+2 days',
69 'group_percentage' => 10,
70 );
71 }
72
73 /**
74 * Test civicrm_mailing_create
75 */
76 public function testMailingABCreateSuccess() {
77 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
78 $this->assertTrue(is_numeric($result['id']), "In line " . __LINE__);
79 $this->assertEquals($this->_params['group_percentage'], $result['values'][$result['id']]['group_percentage']);
80 }
81
82 /**
83 * Test civicrm_mailing_delete
84 */
85 public function testMailerDeleteSuccess() {
86 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
87
88 $this->assertDBQuery(1, "SELECT count(*) FROM civicrm_mailing_abtest WHERE id = %1", array(
89 1 => array($result['id'], 'Integer'),
90 ));
91 $this->assertDBQuery(3, "SELECT count(*) FROM civicrm_mailing WHERE id IN (%1,%2,%3)", array(
92 1 => array($this->_mailingID_A, 'Integer'),
93 2 => array($this->_mailingID_B, 'Integer'),
94 3 => array($this->_mailingID_C, 'Integer'),
95 ));
96
97 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id']));
98
99 $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_mailing_abtest WHERE id = %1", array(
100 1 => array($result['id'], 'Integer'),
101 ));
102 $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_mailing WHERE id IN (%1,%2,%3)", array(
103 1 => array($this->_mailingID_A, 'Integer'),
104 2 => array($this->_mailingID_B, 'Integer'),
105 3 => array($this->_mailingID_C, 'Integer'),
106 ));
107 }
108
109 public function groupPctProvider() {
110 $cases = array(); // array(int $totalSize, int $groupPct, int $expectedCountA, $expectedCountB, $expectedCountC)
111 $cases[] = array(400, 7, 28, 28, 344);
112 $cases[] = array(100, 10, 10, 10, 80);
113 $cases[] = array(50, 20, 10, 10, 30);
114 $cases[] = array(50, 10, 5, 5, 40);
115 $cases[] = array(3, 10, 1, 1, 1);
116 $cases[] = array(2, 10, 1, 1, 0);
117 $cases[] = array(1, 10, 1, 0, 0);
118 return $cases;
119 }
120
121 /**
122 * Create a test and ensure that all three mailings (A/B/C) wind up with the correct
123 * number of recipients.
124 *
125 * @param $totalGroupContacts
126 * @param $groupPct
127 * @param $expectedCountA
128 * @param $expectedCountB
129 * @param $expectedCountC
130 * @dataProvider groupPctProvider
131 */
132 public function testDistribution($totalGroupContacts, $groupPct, $expectedCountA, $expectedCountB, $expectedCountC) {
133 $this->createLoggedInUser();
134
135 $result = $this->groupContactCreate($this->_groupID, $totalGroupContacts);
136 $this->assertEquals($totalGroupContacts, $result['added'], "in line " . __LINE__);
137
138 $params = $this->_params;
139 $params['group_percentage'] = $groupPct;
140 $result = $this->callAPISuccess($this->_entity, 'create', $params);
141
142 $this->callAPISuccess('Mailing', 'create', array(
143 'id' => $this->_mailingID_A,
144 'groups' => array('include' => array($this->_groupID)),
145 ));
146 $this->assertJobCounts(0, 0, 0);
147
148 $this->callAPISuccess('MailingAB', 'submit', array(
149 'id' => $result['id'],
150 'status' => 'Testing',
151 'scheduled_date' => date('YmdHis'),
152 'approval_date' => date('YmdHis'),
153 ));
154 $this->assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC);
155 $this->assertJobCounts(1, 1, 0);
156
157 $this->callAPISuccess('MailingAB', 'submit', array(
158 'id' => $result['id'],
159 'status' => 'Final',
160 'scheduled_date' => date('YmdHis'),
161 'approval_date' => date('YmdHis'),
162 ));
163 $this->assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC);
164 $this->assertJobCounts(1, 1, 1);
165 }
166
167 /**
168 * @param $expectedCountA
169 * @param $expectedCountB
170 * @param $expectedCountC
171 */
172 protected function assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC) {
173 $countA = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_A));
174 $countB = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_B));
175 $countC = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_C));
176 $this->assertEquals($expectedCountA, $countA, "check mailing recipients A in line " . __LINE__);
177 $this->assertEquals($expectedCountB, $countB, "check mailing recipients B in line " . __LINE__);
178 $this->assertEquals($expectedCountC, $countC, "check mailing recipients C in line " . __LINE__);
179 }
180
181 protected function assertJobCounts($expectedA, $expectedB, $expectedC) {
182 $this->assertDBQuery($expectedA, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
183 1 => array(
184 $this->_mailingID_A,
185 'Integer'
186 )
187 ));
188 $this->assertDBQuery($expectedB, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
189 1 => array(
190 $this->_mailingID_B,
191 'Integer'
192 )
193 ));
194 $this->assertDBQuery($expectedC, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
195 1 => array(
196 $this->_mailingID_C,
197 'Integer'
198 )
199 ));
200 }
201 }