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