Commit | Line | Data |
---|---|---|
4aef704e | 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'; | |
768c558c | 41 | protected $_groupID; |
4aef704e | 42 | |
00be9182 | 43 | public function setUp() { |
4aef704e | 44 | parent::setUp(); |
bf0dd3d8 | 45 | $this->useTransaction(TRUE); |
8b06123b | 46 | $this->createLoggedInUser(); |
4aef704e | 47 | $this->_mailingID_A = $this->createMailing(); |
48 | $this->_mailingID_B = $this->createMailing(); | |
49 | $this->_mailingID_C = $this->createMailing(); | |
7811a84b | 50 | $this->_groupID = $this->groupCreate(); |
4aef704e | 51 | |
52 | $this->_params = array( | |
53 | 'mailing_id_a' => $this->_mailingID_A, | |
54 | 'mailing_id_b' => $this->_mailingID_B, | |
55 | 'mailing_id_c' => $this->_mailingID_C, | |
321121a1 TO |
56 | 'testing_criteria' => 'subject', |
57 | 'winner_criteria' => 'open', | |
4aef704e | 58 | 'declare_winning_time' => '+2 days', |
59 | 'group_percentage' => 10, | |
60 | ); | |
61 | } | |
62 | ||
4aef704e | 63 | /** |
fe482240 | 64 | * Test civicrm_mailing_create. |
4aef704e | 65 | */ |
66 | public function testMailingABCreateSuccess() { | |
fd843187 | 67 | $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__); |
4aef704e | 68 | $this->assertTrue(is_numeric($result['id']), "In line " . __LINE__); |
69 | $this->assertEquals($this->_params['group_percentage'], $result['values'][$result['id']]['group_percentage']); | |
70 | } | |
71 | ||
72 | /** | |
fe482240 | 73 | * Test civicrm_mailing_delete. |
4aef704e | 74 | */ |
75 | public function testMailerDeleteSuccess() { | |
76 | $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); | |
bf0dd3d8 | 77 | |
172f99a3 | 78 | $this->assertDBQuery(1, "SELECT count(*) FROM civicrm_mailing_abtest WHERE id = %1", array( |
bf0dd3d8 TO |
79 | 1 => array($result['id'], 'Integer'), |
80 | )); | |
81 | $this->assertDBQuery(3, "SELECT count(*) FROM civicrm_mailing WHERE id IN (%1,%2,%3)", array( | |
82 | 1 => array($this->_mailingID_A, 'Integer'), | |
83 | 2 => array($this->_mailingID_B, 'Integer'), | |
84 | 3 => array($this->_mailingID_C, 'Integer'), | |
85 | )); | |
86 | ||
fd843187 | 87 | $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id'])); |
bf0dd3d8 | 88 | |
172f99a3 | 89 | $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_mailing_abtest WHERE id = %1", array( |
bf0dd3d8 TO |
90 | 1 => array($result['id'], 'Integer'), |
91 | )); | |
92 | $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_mailing WHERE id IN (%1,%2,%3)", array( | |
93 | 1 => array($this->_mailingID_A, 'Integer'), | |
94 | 2 => array($this->_mailingID_B, 'Integer'), | |
95 | 3 => array($this->_mailingID_C, 'Integer'), | |
96 | )); | |
4aef704e | 97 | } |
ef643544 | 98 | |
f0be539a EM |
99 | /** |
100 | * @return array | |
101 | */ | |
b0f9e1df TO |
102 | public function groupPctProvider() { |
103 | $cases = array(); // array(int $totalSize, int $groupPct, int $expectedCountA, $expectedCountB, $expectedCountC) | |
104 | $cases[] = array(400, 7, 28, 28, 344); | |
105 | $cases[] = array(100, 10, 10, 10, 80); | |
106 | $cases[] = array(50, 20, 10, 10, 30); | |
107 | $cases[] = array(50, 10, 5, 5, 40); | |
108 | $cases[] = array(3, 10, 1, 1, 1); | |
109 | $cases[] = array(2, 10, 1, 1, 0); | |
110 | $cases[] = array(1, 10, 1, 0, 0); | |
111 | return $cases; | |
112 | } | |
7811a84b | 113 | |
b0f9e1df | 114 | /** |
768c558c TO |
115 | * Create a test and ensure that all three mailings (A/B/C) wind up with the correct |
116 | * number of recipients. | |
117 | * | |
b0f9e1df TO |
118 | * @param $totalGroupContacts |
119 | * @param $groupPct | |
120 | * @param $expectedCountA | |
121 | * @param $expectedCountB | |
122 | * @param $expectedCountC | |
123 | * @dataProvider groupPctProvider | |
124 | */ | |
768c558c | 125 | public function testDistribution($totalGroupContacts, $groupPct, $expectedCountA, $expectedCountB, $expectedCountC) { |
768c558c | 126 | |
ef643544 | 127 | $result = $this->groupContactCreate($this->_groupID, $totalGroupContacts); |
ef643544 | 128 | $this->assertEquals($totalGroupContacts, $result['added'], "in line " . __LINE__); |
7811a84b | 129 | |
b0f9e1df TO |
130 | $params = $this->_params; |
131 | $params['group_percentage'] = $groupPct; | |
132 | $result = $this->callAPISuccess($this->_entity, 'create', $params); | |
7811a84b | 133 | |
768c558c TO |
134 | $this->callAPISuccess('Mailing', 'create', array( |
135 | 'id' => $this->_mailingID_A, | |
136 | 'groups' => array('include' => array($this->_groupID)), | |
137 | )); | |
138 | $this->assertJobCounts(0, 0, 0); | |
7811a84b | 139 | |
768c558c TO |
140 | $this->callAPISuccess('MailingAB', 'submit', array( |
141 | 'id' => $result['id'], | |
142 | 'status' => 'Testing', | |
143 | 'scheduled_date' => date('YmdHis'), | |
144 | 'approval_date' => date('YmdHis'), | |
145 | )); | |
146 | $this->assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC); | |
147 | $this->assertJobCounts(1, 1, 0); | |
148 | ||
149 | $this->callAPISuccess('MailingAB', 'submit', array( | |
150 | 'id' => $result['id'], | |
151 | 'status' => 'Final', | |
152 | 'scheduled_date' => date('YmdHis'), | |
153 | 'approval_date' => date('YmdHis'), | |
154 | )); | |
155 | $this->assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC); | |
156 | $this->assertJobCounts(1, 1, 1); | |
157 | } | |
b0f9e1df | 158 | |
768c558c TO |
159 | /** |
160 | * @param $expectedCountA | |
161 | * @param $expectedCountB | |
162 | * @param $expectedCountC | |
163 | */ | |
164 | protected function assertRecipientCounts($expectedCountA, $expectedCountB, $expectedCountC) { | |
165 | $countA = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_A)); | |
b0f9e1df | 166 | $countB = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_B)); |
b0f9e1df | 167 | $countC = $this->callAPISuccess('MailingRecipients', 'getcount', array('mailing_id' => $this->_mailingID_C)); |
768c558c TO |
168 | $this->assertEquals($expectedCountA, $countA, "check mailing recipients A in line " . __LINE__); |
169 | $this->assertEquals($expectedCountB, $countB, "check mailing recipients B in line " . __LINE__); | |
b0f9e1df | 170 | $this->assertEquals($expectedCountC, $countC, "check mailing recipients C in line " . __LINE__); |
ef643544 | 171 | } |
172 | ||
f0be539a EM |
173 | /** |
174 | * @param $expectedA | |
175 | * @param $expectedB | |
176 | * @param $expectedC | |
177 | */ | |
768c558c TO |
178 | protected function assertJobCounts($expectedA, $expectedB, $expectedC) { |
179 | $this->assertDBQuery($expectedA, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array( | |
92915c55 TO |
180 | 1 => array( |
181 | $this->_mailingID_A, | |
182 | 'Integer', | |
183 | ), | |
184 | )); | |
768c558c | 185 | $this->assertDBQuery($expectedB, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array( |
92915c55 TO |
186 | 1 => array( |
187 | $this->_mailingID_B, | |
188 | 'Integer', | |
189 | ), | |
190 | )); | |
768c558c | 191 | $this->assertDBQuery($expectedC, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array( |
92915c55 TO |
192 | 1 => array( |
193 | $this->_mailingID_C, | |
194 | 'Integer', | |
195 | ), | |
196 | )); | |
768c558c | 197 | } |
96025800 | 198 | |
4aef704e | 199 | } |