3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Test APIv3 civicrm_contribute_recur* functions
34 * @package CiviCRM_APIv3
35 * @subpackage API_Contribution
38 class api_v3_ContributionPageTest
extends CiviUnitTestCase
{
39 protected $_apiversion = 3;
40 protected $testAmount = 34567;
43 protected $contactIds = array();
44 protected $_entity = 'contribution_page';
45 protected $contribution_result = null;
46 protected $_priceSetParams = array();
55 protected $_ids = array();
58 public $DBResetRequired = TRUE;
59 public function setUp() {
61 $this->contactIds
[] = $this->individualCreate();
62 $this->params
= array(
63 'title' => "Test Contribution Page",
64 'financial_type_id' => 1,
66 'goal_amount' => $this->testAmount
,
68 'is_monetary' => TRUE,
71 $this->_priceSetParams
= array(
72 'is_quick_config' => 1,
73 'extends' => 'CiviContribute',
74 'financial_type_id' => 'Donation',
80 foreach ($this->contactIds
as $id) {
81 $this->callAPISuccess('contact', 'delete', array('id' => $id));
83 $this->quickCleanUpFinancialEntities();
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
);
93 public function testGetBasicContributionPage() {
94 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
95 $this->id
= $createResult['id'];
98 'financial_type_id' => 1,
100 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
101 $this->assertEquals(1, $getResult['count']);
104 public function testGetContributionPageByAmount() {
105 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
106 $this->id
= $createResult['id'];
108 'amount' => ''. $this->testAmount
, // 3456
110 'financial_type_id' => 1,
112 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
113 $this->assertEquals(1, $getResult['count']);
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']);
124 public function testGetFieldsContributionPage() {
125 $result = $this->callAPISuccess($this->_entity
, 'getfields', array('action' => 'create'));
126 $this->assertEquals(12, $result['values']['start_date']['type']);
131 * Test form submission with basic price set
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'],
143 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
144 $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page']));
147 * Test submit with a membership block in place
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'],
155 'billing_first_name' => 'Billy',
156 'billing_middle_name' => 'Goat',
157 'billing_last_name' => 'Gruff',
158 'selectMembership' => $this->_ids
['membership_type'],
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']));
168 * Test submit with a membership block in place
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'],
176 'billing_first_name' => 'Billy',
177 'billing_middle_name' => 'Goat',
178 'billing_last_name' => 'Gruff',
179 'selectMembership' => $this->_ids
['membership_type'],
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']);
192 * Test submit with a membership block in place
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'],
202 'billing_first_name' => 'Billy',
203 'billing_middle_name' => 'Goat',
204 'billing_last_name' => 'Gruff',
205 'selectMembership' => $this->_ids
['membership_type'][1],
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']);
221 * Test submit with a membership block in place
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'],
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 ),
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']);
249 * set up membership contribution page
250 * @param bool $isSeparatePayment
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();
257 $this->callAPISuccess('membership_block', 'create', array(
258 'entity_id' => $this->_ids
['contribution_page'],
259 'entity_table' => 'civicrm_contribution_page',
260 'is_required' => TRUE,
262 'is_separate_payment' => $isSeparatePayment,
263 'membership_type_default' => $this->_ids
['membership_type'],
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
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)));
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',
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',
289 'financial_type_id' => 1,
290 'format.only_id' => TRUE,
291 'membership_type_id' => $membershipTypeID,
292 'price_field_id' => $priceField['id'],
294 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
299 * help function to set up contribution page with some defaults
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'];
307 $priceSetID = reset($this->_ids
['price_set']);
308 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
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',
316 $this->_ids
['price_field'] = array($priceField['id']);
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',
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',
333 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
335 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
338 public static function setUpBeforeClass() {
339 // put stuff here that should happen before all tests in this unit
342 public static function tearDownAfterClass(){
343 $tablesToTruncate = array(
345 'civicrm_financial_type',
346 'civicrm_contribution',
347 'civicrm_contribution_page',
349 $unitTest = new CiviUnitTestCase();
350 $unitTest->quickCleanup($tablesToTruncate);