Merge branch '4.4' of https://github.com/civicrm/civicrm-core into 4.5
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionPageTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30
31 /**
32 * Test APIv3 civicrm_contribute_recur* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Contribution
36 */
37
38 class api_v3_ContributionPageTest extends CiviUnitTestCase {
39 protected $_apiversion = 3;
40 protected $testAmount = 34567;
41 protected $params;
42 protected $id = 0;
43 protected $contactIds = array();
44 protected $_entity = 'contribution_page';
45 protected $contribution_result = null;
46 protected $_priceSetParams = array();
47
48 /**
49 * @var array
50 * - contribution_page
51 * - price_set
52 * - price_field
53 * - price_field_value
54 */
55 protected $_ids = array();
56
57
58 public $DBResetRequired = TRUE;
59 public function setUp() {
60 parent::setUp();
61 $this->contactIds[] = $this->individualCreate();
62 $this->params = array(
63 'title' => "Test Contribution Page",
64 'financial_type_id' => 1,
65 'currency' => 'NZD',
66 'goal_amount' => $this->testAmount,
67 'is_pay_later' => 1,
68 'is_monetary' => TRUE,
69 );
70
71 $this->_priceSetParams = array(
72 'is_quick_config' => 1,
73 'extends' => 'CiviContribute',
74 'financial_type_id' => 'Donation',
75 'title' => 'my Page'
76 );
77 }
78
79 function tearDown() {
80 foreach ($this->contactIds as $id) {
81 $this->callAPISuccess('contact', 'delete', array('id' => $id));
82 }
83 $this->quickCleanUpFinancialEntities();
84 }
85
86 public function testCreateContributionPage() {
87 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
88 $this->assertEquals(1, $result['count']);
89 $this->assertNotNull($result['values'][$result['id']]['id']);
90 $this->getAndCheck($this->params, $result['id'], $this->_entity);
91 }
92
93 public function testGetBasicContributionPage() {
94 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
95 $this->id = $createResult['id'];
96 $getParams = array(
97 'currency' => 'NZD',
98 'financial_type_id' => 1,
99 );
100 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
101 $this->assertEquals(1, $getResult['count']);
102 }
103
104 public function testGetContributionPageByAmount() {
105 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
106 $this->id = $createResult['id'];
107 $getParams = array(
108 'amount' => ''. $this->testAmount, // 3456
109 'currency' => 'NZD',
110 'financial_type_id' => 1,
111 );
112 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
113 $this->assertEquals(1, $getResult['count']);
114 }
115
116 public function testDeleteContributionPage() {
117 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
118 $deleteParams = array('id' => $createResult['id']);
119 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
120 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
121 $this->assertEquals(0, $checkDeleted['count']);
122 }
123
124 public function testGetFieldsContributionPage() {
125 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
126 $this->assertEquals(12, $result['values']['start_date']['type']);
127 }
128
129
130 /**
131 * Test form submission with basic price set
132 */
133 public function testSubmit() {
134 $this->setUpContributionPage();
135 $priceFieldID = reset($this->_ids['price_field']);
136 $priceFieldValueID = reset($this->_ids['price_field_value']);
137 $submitParams = array(
138 'price_' . $priceFieldID => $priceFieldValueID,
139 'id' => (int) $this->_ids['contribution_page'],
140 'amount' => 10
141 );
142
143 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
144 $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
145 }
146 /**
147 * Test submit with a membership block in place
148 */
149 public function testSubmitMembershipBlockNotSeparatePayment() {
150 $this->setUpMembershipContributionPage();
151 $submitParams = array(
152 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
153 'id' => (int) $this->_ids['contribution_page'],
154 'amount' => 10,
155 'billing_first_name' => 'Billy',
156 'billing_middle_name' => 'Goat',
157 'billing_last_name' => 'Gruff',
158 'selectMembership' => $this->_ids['membership_type'],
159
160 );
161
162 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
163 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
164 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
165 }
166
167 /**
168 * Test submit with a membership block in place
169 */
170 public function testSubmitMembershipBlockIsSeparatePayment() {
171 $this->setUpMembershipContributionPage(TRUE);
172 $submitParams = array(
173 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
174 'id' => (int) $this->_ids['contribution_page'],
175 'amount' => 10,
176 'billing_first_name' => 'Billy',
177 'billing_middle_name' => 'Goat',
178 'billing_last_name' => 'Gruff',
179 'selectMembership' => $this->_ids['membership_type'],
180 );
181
182 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
183 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
184 $this->assertCount(2, $contributions['values']);
185 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
186 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
187 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
188 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
189 }
190
191 /**
192 * Test submit with a membership block in place
193 */
194 public function testSubmitMembershipBlockTwoTypesIsSeparatePayment() {
195 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 6)));
196 $this->_ids['membership_type'][] = $this->membershipTypeCreate(array('name' => 'Student', 'minimum_fee' => 50));
197 $this->setUpMembershipContributionPage(TRUE);
198 $submitParams = array(
199 'price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value'][1],
200 'id' => (int) $this->_ids['contribution_page'],
201 'amount' => 10,
202 'billing_first_name' => 'Billy',
203 'billing_middle_name' => 'Goat',
204 'billing_last_name' => 'Gruff',
205 'selectMembership' => $this->_ids['membership_type'][1],
206 );
207
208 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
209 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page'],));
210 $this->assertCount(2, $contributions['values']);
211 $ids = array_keys($contributions['values']);
212 $this->assertEquals('10.00', $contributions['values'][$ids[0]]['total_amount']);
213 $this->assertEquals('50.00', $contributions['values'][$ids[1]]['total_amount']);
214 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
215 $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions['values']);
216 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
217 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
218 }
219
220 /**
221 * Test submit with a membership block in place
222 */
223 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessor() {
224 $this->setUpMembershipContributionPage(TRUE);
225 $submitParams = array(
226 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
227 'id' => (int) $this->_ids['contribution_page'],
228 'amount' => 10,
229 'billing_first_name' => 'Billy',
230 'billing_middle_name' => 'Goat',
231 'billing_last_name' => 'Gruff',
232 'selectMembership' => $this->_ids['membership_type'],
233 'payment_processor' => 1,
234 'credit_card_number' => '4111111111111111',
235 'credit_card_type' => 'Visa',
236 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040 ),
237 'cvv2' => 123,
238 );
239
240 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
241 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page'], 'contribution_status_id' => 1));
242 $this->assertCount(2, $contributions['values']);
243 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
244 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
245 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
246 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
247 }
248 /**
249 * set up membership contribution page
250 * @param bool $isSeparatePayment
251 */
252 function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
253 $this->setUpMembershipBlockPriceSet();
254 $this->params['payment_processor_id'] = $this->_ids['payment_processor'] = $this->paymentProcessorCreate(array('payment_processor_type_id' => 'Dummy',));
255 $this->setUpContributionPage();
256
257 $this->callAPISuccess('membership_block', 'create', array(
258 'entity_id' => $this->_ids['contribution_page'],
259 'entity_table' => 'civicrm_contribution_page',
260 'is_required' => TRUE,
261 'is_active' => TRUE,
262 'is_separate_payment' => $isSeparatePayment,
263 'membership_type_default' => $this->_ids['membership_type'],
264 ));
265 }
266
267 /**
268 * The default data set does not include a complete default membership price set - not quite sure why
269 * This function ensures it exists & populates $this->_ids with it's data
270 */
271 function setUpMembershipBlockPriceSet() {
272 $this->_ids['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array('name' => 'default_membership_type_amount', 'return' => 'id'));
273 if (empty($this->_ids['membership_type'])) {
274 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
275 }
276 $priceField = $this->callAPISuccess('price_field', 'create', array(
277 'price_set_id' => reset($this->_ids['price_set']),
278 'name' => 'membership_amount',
279 'label' => 'Membership Amount',
280 'html_type' => 'Radio',
281 'sequential' => 1,
282 ));
283 $this->_ids['price_field'][] = $priceField['id'];
284 foreach ($this->_ids['membership_type'] as $membershipTypeID) {
285 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
286 'name' => 'membership_amount',
287 'label' => 'Membership Amount',
288 'amount' => 1,
289 'financial_type_id' => 1,
290 'format.only_id' => TRUE,
291 'membership_type_id' => $membershipTypeID,
292 'price_field_id' => $priceField['id'],
293 ));
294 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
295 }
296 }
297
298 /**
299 * help function to set up contribution page with some defaults
300 */
301 function setUpContributionPage() {
302 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
303 if (empty($this->_ids['price_set'])) {
304 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
305 $this->_ids['price_set'][] = $priceSet['id'];
306 }
307 $priceSetID = reset($this->_ids['price_set']);
308 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
309
310 if (empty($this->_ids['price_field'])) {
311 $priceField = $this->callAPISuccess('price_field', 'create', array(
312 'price_set_id' => $priceSetID,
313 'label' => 'Goat Breed',
314 'html_type' => 'Radio',
315 ));
316 $this->_ids['price_field'] = array($priceField['id']);
317 }
318 if (empty($this->_ids['price_field_value'])) {
319 $this->callAPISuccess('price_field_value', 'create', array(
320 'price_set_id' => $priceSetID,
321 'price_field_id' => $priceField['id'],
322 'label' => 'Long Haired Goat',
323 'amount' => 20,
324 )
325 );
326 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
327 'price_set_id' => $priceSetID,
328 'price_field_id' => $priceField['id'],
329 'label' => 'Shoe-eating Goat',
330 'amount' => 10,
331 )
332 );
333 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
334 }
335 $this->_ids['contribution_page'] = $contributionPageResult['id'];
336 }
337
338 public static function setUpBeforeClass() {
339 // put stuff here that should happen before all tests in this unit
340 }
341
342 public static function tearDownAfterClass(){
343 $tablesToTruncate = array(
344 'civicrm_contact',
345 'civicrm_financial_type',
346 'civicrm_contribution',
347 'civicrm_contribution_page',
348 );
349 $unitTest = new CiviUnitTestCase();
350 $unitTest->quickCleanup($tablesToTruncate);
351 }
352 }
353