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'] => 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'] => 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 * set up membership contribution page
193 * @param bool $isSeparatePayment
195 function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
196 $this->setUpMembershipBlockPriceSet();
197 $this->params
['payment_processor_id'] = $this->_ids
['payment_processor'] = $this->paymentProcessorCreate(array('payment_processor_type_id' => 'Dummy',));
198 $this->setUpContributionPage();
200 $this->callAPISuccess('membership_block', 'create', array(
201 'entity_id' => $this->_ids
['contribution_page'],
202 'entity_table' => 'civicrm_contribution_page',
203 'is_required' => TRUE,
205 'is_separate_payment' => $isSeparatePayment,
206 'membership_type_default' => $this->_ids
['membership_type'],
211 * The default data set does not include a complete default membership price set - not quite sure why
212 * This function ensures it exists & populates $this->_ids with it's data
214 function setUpMembershipBlockPriceSet() {
215 $this->_ids
['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array('name' => 'default_membership_type_amount', 'return' => 'id'));
216 if (empty($this->_ids
['membership_type'])) {
217 $this->_ids
['membership_type'] = $this->membershipTypeCreate(array('minimum_fee' => 1));
220 $this->_ids
['price_field'] = $this->callAPISuccessGetValue('price_field', array(
222 'name' => 'membership_amount',
223 'price_set_id' => reset($this->_ids
['price_set']),
224 'options' => array('limit' => 1))
226 $this->_ids
['price_field_value'] = array($this->callAPISuccessGetValue('price_field_value', array('weight' => 1, 'return' => 'id', 'name' => 'membership_amount', 'price_field_id' => $this->_ids
['price_field'])));
227 $this->callAPISuccess('price_field_value', 'create', array('id' => $this->_ids
['price_field_value'][0], 'membership_type_id' => $this->_ids
['membership_type'],));
229 catch (Exception
$e) {
230 //default price set likely not set up correctly :-(
231 $priceField = $this->callAPISuccess('price_field', 'create', array(
232 'price_set_id' => reset($this->_ids
['price_set']),
233 'name' => 'membership_amount',
234 'label' => 'Membership Amount',
235 'html_type' => 'Radio',
237 'api.price_field_value.create' => array(
238 'name' => 'membership_amount',
239 'label' => 'Membership Amount',
241 'financial_type_id' => 1,
242 'format.only_id' => TRUE,
243 'membership_type_id' => $this->_ids
['membership_type']
246 $this->_ids
['price_field'] = $priceField['id'];
247 $this->_ids
['price_field_value'] = array($priceField['values'][0]['api.price_field_value.create']);
251 * help function to set up contribution page with some defaults
253 function setUpContributionPage() {
254 $contributionPageResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
255 if (empty($this->_ids
['price_set'])) {
256 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams
);
257 $this->_ids
['price_set'][] = $priceSet['id'];
259 $priceSetID = reset($this->_ids
['price_set']);
260 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
262 if (empty($this->_ids
['price_field'])) {
263 $priceField = $this->callAPISuccess('price_field', 'create', array(
264 'price_set_id' => $priceSetID,
265 'label' => 'Goat Breed',
266 'html_type' => 'Radio',
268 $this->_ids
['price_field'] = array($priceField['id']);
270 if (empty($this->_ids
['price_field_value'])) {
271 $this->callAPISuccess('price_field_value', 'create', array(
272 'price_set_id' => $priceSetID,
273 'price_field_id' => $priceField['id'],
274 'label' => 'Long Haired Goat',
278 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
279 'price_set_id' => $priceSetID,
280 'price_field_id' => $priceField['id'],
281 'label' => 'Shoe-eating Goat',
285 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
287 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
290 public static function setUpBeforeClass() {
291 // put stuff here that should happen before all tests in this unit
294 public static function tearDownAfterClass(){
295 $tablesToTruncate = array(
297 'civicrm_financial_type',
298 'civicrm_contribution',
299 'civicrm_contribution_page',
301 $unitTest = new CiviUnitTestCase();
302 $unitTest->quickCleanup($tablesToTruncate);