Merge branch 'JohnFF-patch-1'
[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'] => 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'] => 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 * set up membership contribution page
193 * @param bool $isSeparatePayment
194 */
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();
199
200 $this->callAPISuccess('membership_block', 'create', array(
201 'entity_id' => $this->_ids['contribution_page'],
202 'entity_table' => 'civicrm_contribution_page',
203 'is_required' => TRUE,
204 'is_active' => TRUE,
205 'is_separate_payment' => $isSeparatePayment,
206 'membership_type_default' => $this->_ids['membership_type'],
207 ));
208 }
209
210 /**
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
213 */
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));
218 }
219 try {
220 $this->_ids['price_field'] = $this->callAPISuccessGetValue('price_field', array(
221 'return' => 'id',
222 'name' => 'membership_amount',
223 'price_set_id' => reset($this->_ids['price_set']),
224 'options' => array('limit' => 1))
225 );
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'],));
228 }
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',
236 'sequential' => 1,
237 'api.price_field_value.create' => array(
238 'name' => 'membership_amount',
239 'label' => 'Membership Amount',
240 'amount' => 1,
241 'financial_type_id' => 1,
242 'format.only_id' => TRUE,
243 'membership_type_id' => $this->_ids['membership_type']
244 )
245 ));
246 $this->_ids['price_field'] = $priceField['id'];
247 $this->_ids['price_field_value'] = array($priceField['values'][0]['api.price_field_value.create']);
248 }
249 }
250 /**
251 * help function to set up contribution page with some defaults
252 */
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'];
258 }
259 $priceSetID = reset($this->_ids['price_set']);
260 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
261
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',
267 ));
268 $this->_ids['price_field'] = array($priceField['id']);
269 }
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',
275 'amount' => 20,
276 )
277 );
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',
282 'amount' => 10,
283 )
284 );
285 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
286 }
287 $this->_ids['contribution_page'] = $contributionPageResult['id'];
288 }
289
290 public static function setUpBeforeClass() {
291 // put stuff here that should happen before all tests in this unit
292 }
293
294 public static function tearDownAfterClass(){
295 $tablesToTruncate = array(
296 'civicrm_contact',
297 'civicrm_financial_type',
298 'civicrm_contribution',
299 'civicrm_contribution_page',
300 );
301 $unitTest = new CiviUnitTestCase();
302 $unitTest->quickCleanup($tablesToTruncate);
303 }
304 }
305