Fixed some contribution pages
[civicrm-core.git] / tests / phpunit / WebTest / Member / OnlineMembershipAddPricesetTest.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25*/
26
27
28require_once 'CiviTest/CiviSeleniumTestCase.php';
29class WebTest_Member_OnlineMembershipAddPricesetTest extends CiviSeleniumTestCase {
30
31 protected function setUp() {
32 parent::setUp();
33 }
34
35 function testAddPriceSet() {
36 // add the required permission
37 $permissions = array('edit-1-make-online-contributions');
38 $this->changePermissions($permissions);
39
40 // Log in as normal user
41 $this->webtestLogin();
42
43 $title = substr(sha1(rand()), 0, 7);
44 $setTitle = "Membership Fees - $title";
45 $usedFor = 'Membership';
46 $contributionType = 'Donation';
47 $setHelp = 'Select your membership options.';
48 $this->_testAddSet($setTitle, $usedFor, $contributionType, $setHelp);
49
50 // Get the price set id ($sid) by retrieving and parsing the URL of the New Price Field form
51 // which is where we are after adding Price Set.
52 $elements = $this->parseURL();
53 $sid = $elements['queryString']['sid'];
54 $this->assertType('numeric', $sid);
55
56 $fields = array(
57 "National Membership $title" => 'Radio',
58 "Local Chapter $title" => 'CheckBox',
59 );
60
61 list($memTypeTitle1, $memTypeTitle2) = $this->_testAddPriceFields($fields, $validateStrings, FALSE, $title, $sid, $contributionType);
62 //var_dump($validateStrings);
63
64 // load the Price Set Preview and check for expected values
65 $this->_testVerifyPriceSet($validateStrings, $sid);
66
67 $contributionPageTitle = "Contribution Page $title";
68 $paymentProcessor = "Webtest Dummy $title";
69 $pageId = $this->webtestAddContributionPage(NULL, NULL, $contributionPageTitle, array($paymentProcessor => 'Dummy'),
70 TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, $sid, FALSE, 1, NULL
71 );
72
73 // Sign up for membership
74 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
75 $lastName = 'Anderson_' . substr(sha1(rand()), 0, 7);
76 $email = "{$firstName}.{$lastName}@example.com";
77
78 $contactParams = array(
79 'first_name' => $firstName,
80 'last_name' => $lastName,
81 'email-5' => $email,
82 );
83 $this->_testSignUpOrRenewMembership($pageId, $contactParams, $memTypeTitle1, $memTypeTitle2);
84
85 // Renew this membership
86 $this->_testSignUpOrRenewMembership($pageId, $contactParams, $memTypeTitle1, $memTypeTitle2, $renew = TRUE);
87 }
88
89 function testAddPriceSetWithMultipleTerms() {
90 // add the required permission
91 $permissions = array('edit-1-make-online-contributions');
92 $this->changePermissions($permissions);
93
94 // Log in as normal user
95 $this->webtestLogin();
96
97 $title = substr(sha1(rand()), 0, 7);
98 $setTitle = "Membership Fees - $title";
99 $usedFor = 'Membership';
100 $contributionType = 'Member Dues';
101 $setHelp = 'Select your membership options.';
102 $memTypeParams1 = $this->webtestAddMembershipType();
103 $memTypeTitle1 = $memTypeParams1['membership_type'];
104 $memTypeId1 = explode('&id=', $this->getAttribute("xpath=//div[@id='membership_type']/div[2]/table/tbody//tr/td[text()='{$memTypeTitle1}']/../td[12]/span/a[3]@href"));
105 $memTypeId1 = $memTypeId1[1];
106 $this->_testAddSet($setTitle, $usedFor, $contributionType, $setHelp);
107
108 // Get the price set id ($sid) by retrieving and parsing the URL of the New Price Field form
109 // which is where we are after adding Price Set.
110 $elements = $this->parseURL();
111 $sid = $elements['queryString']['sid'];
112 $this->assertType('numeric', $sid);
113
114 $fields = array("National Membership $title", "Radio");
115 $this->openCiviPage('admin/price/field', "reset=1&action=add&sid={$sid}");
116
117 $validateStrings[] = $fields[0];
118 $this->type('label', $fields[0]);
119 $this->select('html_type', "value={$fields[1]}");
120 $options = array(
121 1 => array('label' => $memTypeTitle1."_1",
122 'membership_type_id' => $memTypeId1,
123 'amount' => 50.00,
124 'membership_num_terms' => 1,
125 ),
126 2 => array(
127 'label' => $memTypeTitle1."_2",
128 'membership_type_id' => $memTypeId1,
129 'amount' => 90.00,
130 'membership_num_terms' => 2,
131 ),
132 3 => array(
133 'label' => $memTypeTitle1."_3",
134 'membership_type_id' => $memTypeId1,
135 'amount' => 120.00,
136 'membership_num_terms' => 3,
137 ),
138
139 );
140 $i = 2;
141 foreach($options as $index => $values){
142 $this->select("membership_type_id_{$index}", "value={$values['membership_type_id']}");
143 // Because it tends to cause problems, all uses of sleep() must be justified in comments
144 // Sleep should never be used for wait for anything to load from the server
145 // Justification for this instance: FIXME
146 sleep(1);
147 $this->type("xpath=//table[@id='optionField']/tbody/tr[$i]/td[4]/input",$values['membership_num_terms']);
148 $this->type("xpath=//table[@id='optionField']/tbody/tr[$i]/td[5]/input",$values['label']);
149 $this->type("xpath=//table[@id='optionField']/tbody/tr[$i]/td[6]/input",$values['amount']);
150 if($i > 3){
151 $this->click('link=another choice');
152 }
153 $i++;
154 }
155 $this->waitForElementPresent( 'financial_type_id' );
156 $this->select("financial_type_id", "label={$contributionType}");
157 $this->waitForElementPresent('_qf_Field_next-bottom');
158 $this->click('_qf_Field_next-bottom');
159 $this->waitForPageToLoad($this->getTimeoutMsec());
160 $this->assertElementContainsText('crm-notification-container', "Price Field '{$fields[0]}' has been saved.");
161
162 // load the Price Set Preview and check for expected values
163 $this->_testVerifyPriceSet($validateStrings, $sid);
164
165 $contributionPageTitle = "Contribution Page $title";
166 $paymentProcessor = "Webtest Dummy $title";
167 $pageId = $this->webtestAddContributionPage(NULL, NULL, $contributionPageTitle, array($paymentProcessor => 'Dummy'),
168 TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, $sid, FALSE, 1, NULL
169 );
170
171 // Sign up for membership
172 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
173 $lastName = 'Anderson_' . substr(sha1(rand()), 0, 7);
174 $email = "{$firstName}.{$lastName}@example.com";
175
176 $contactParams = array(
177 'first_name' => $firstName,
178 'last_name' => $lastName,
179 'email-5' => $email,
180 );
181 //membership with number of terms as 2
182 $this->_testMultilpeTermsMembershipRegistration($pageId, $contactParams, $memTypeTitle1, 2);
183 //membership with number of terms as 3 which will renew the above membership
184 $this->_testMultilpeTermsMembershipRegistration($pageId, $contactParams, $memTypeTitle1, 3, TRUE);
185
186 }
187
188 function _testAddSet($setTitle, $usedFor, $contributionType = NULL, $setHelp) {
189 $this->openCiviPage('admin/price', 'reset=1&action=add', '_qf_Set_next-bottom');
190
191 // Enter Priceset fields (Title, Used For ...)
192 $this->type('title', $setTitle);
193 if ($usedFor == 'Event') {
194 $this->check('extends[1]');
195 }
196 elseif ($usedFor == 'Contribution') {
197 $this->check('extends[2]');
198 }
199 elseif ($usedFor == 'Membership') {
200 $this->click('extends[3]');
201 $this->waitForElementPresent( 'financial_type_id' );
202 $this->select("css=select.form-select", "label={$contributionType}");
203 }
204
205 $this->type('help_pre', $setHelp);
206
207 $this->assertChecked('is_active', 'Verify that Is Active checkbox is set.');
208 $this->click('_qf_Set_next-bottom');
209
210 $this->waitForPageToLoad($this->getTimeoutMsec());
211 $this->waitForElementPresent('_qf_Field_next-bottom');
212 $this->assertElementContainsText('crm-notification-container', "Your Set '{$setTitle}' has been added. You can add fields to this set now.");
213 }
214
215 function _testAddPriceFields(&$fields, &$validateString, $dateSpecificFields = FALSE, $title, $sid, $contributionType) {
216 $memTypeParams1 = $this->webtestAddMembershipType();
217 $memTypeTitle1 = $memTypeParams1['membership_type'];
218 $memTypeId1 = explode('&id=', $this->getAttribute("xpath=//div[@id='membership_type']/div[2]/table/tbody//tr/td[text()='{$memTypeTitle1}']/../td[12]/span/a[3]@href"));
219 $memTypeId1 = $memTypeId1[1];
220
221 $memTypeParams2 = $this->webtestAddMembershipType();
222 $memTypeTitle2 = $memTypeParams2['membership_type'];
223 $memTypeId2 = explode('&id=', $this->getAttribute("xpath=//div[@id='membership_type']/div[2]/table/tbody//tr/td[text()='{$memTypeTitle2}']/../td[12]/span/a[3]@href"));
224 $memTypeId2 = $memTypeId2[1];
225
226 $this->openCiviPage('admin/price/field', "reset=1&action=add&sid={$sid}");
227
228 foreach ($fields as $label => $type) {
229 $validateStrings[] = $label;
230
231 $this->type('label', $label);
232 $this->select('html_type', "value={$type}");
233
234 switch ($type) {
235 case 'Radio':
236 $options = array(
237 1 => array('label' => "$memTypeTitle1",
238 'membership_type_id' => $memTypeId1,
239 'amount' => 100.00,
240 ),
241 2 => array(
242 'label' => "$memTypeTitle2",
243 'membership_type_id' => $memTypeId2,
244 'amount' => 50.00,
245 ),
246 );
247 $this->addMultipleChoiceOptions($options, $validateStrings);
248 break;
249
250 case 'CheckBox':
251 $options = array(
252 1 => array('label' => "$memTypeTitle1",
253 'membership_type_id' => $memTypeId1,
254 'amount' => 100.00,
255 ),
256 2 => array(
257 'label' => "$memTypeTitle2",
258 'membership_type_id' => $memTypeId2,
259 'amount' => 50.00,
260 ),
261 );
262 $this->addMultipleChoiceOptions($options, $validateStrings);
263 break;
264
265 default:
266 break;
267 }
268 $this->select("financial_type_id", "label={$contributionType}");
269 $this->click('_qf_Field_next_new-bottom');
270 $this->waitForPageToLoad($this->getTimeoutMsec());
271 $this->waitForElementPresent('_qf_Field_next-bottom');
272 $this->assertElementContainsText('crm-notification-container', "Price Field '{$label}' has been saved.");
273 }
274 return array($memTypeTitle1, $memTypeTitle2);
275 }
276
277 function _testVerifyPriceSet($validateStrings, $sid) {
278 // verify Price Set at Preview page
279 // start at Manage Price Sets listing
280 $this->openCiviPage('admin/price', 'reset=1');
281
282 // Use the price set id ($sid) to pick the correct row
283 $this->click("css=tr#row_{$sid} a[title='Preview Price Set']");
284
285 $this->waitForPageToLoad($this->getTimeoutMsec());
286 // Look for Register button
287 $this->waitForElementPresent('_qf_Preview_cancel-bottom');
288
289 // Check for expected price set field strings
290 $this->assertStringsPresent($validateStrings);
291 }
292
293 function _testSignUpOrRenewMembership($pageId, $contactParams, $memTypeTitle1, $memTypeTitle2, $renew = FALSE) {
294 $this->webtestLogout();
295
296 $this->openCiviPage('contribute/transact', "reset=1&id=$pageId", '_qf_Main_upload-bottom');
297
298 //build the membership dates.
299 $currentYear = date('Y');
300 $currentMonth = date('m');
301 $previousDay = date('d') - 1;
302 $endYear = ($renew) ? $currentYear + 2 : $currentYear + 1;
303 $joinDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
304 $startDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
305 $endDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, $previousDay, $endYear));
306 $configVars = new CRM_Core_Config_Variables();
307 foreach (array(
308 'joinDate', 'startDate', 'endDate') as $date) {
309 $$date = CRM_Utils_Date::customFormat($$date, $configVars->dateformatFull);
310 }
311
312 $this->click("xpath=//div[@id='priceset']/div[2]/div[2]/div/span/input");
313 $this->click("xpath=//div[@id='priceset']/div[3]/div[2]/div[2]/span/input");
314
315 $this->type('email-5', $contactParams['email-5']);
316 $this->type('first_name', $contactParams['first_name']);
317 $this->type('last_name', $contactParams['last_name']);
318
319 $streetAddress = "100 Main Street";
320 $this->type("street_address-1", $streetAddress);
321 $this->type("city-1", "San Francisco");
322 $this->type("postal_code-1", "94117");
323 $this->select("country-1", "value=1228");
324 $this->select("state_province-1", "value=1001");
325
326 //Credit Card Info
327 $this->select("credit_card_type", "value=Visa");
328 $this->type("credit_card_number", "4111111111111111");
329 $this->type("cvv2", "000");
330 $this->select("credit_card_exp_date[M]", "value=1");
331 $this->select("credit_card_exp_date[Y]", "value=2020");
332
333 //Billing Info
334 $this->type("billing_first_name", $contactParams['first_name'] . "billing");
335 $this->type("billing_last_name", $contactParams['last_name'] . "billing");
336 $this->type("billing_street_address-5", "15 Main St.");
337 $this->type(" billing_city-5", "San Jose");
338 $this->select("billing_country_id-5", "value=1228");
339 $this->select("billing_state_province_id-5", "value=1004");
340 $this->type("billing_postal_code-5", "94129");
341 $this->click("_qf_Main_upload-bottom");
342
343 $this->waitForPageToLoad($this->getTimeoutMsec());
344 $this->waitForElementPresent("_qf_Confirm_next-bottom");
345
346 $this->click("_qf_Confirm_next-bottom");
347 $this->waitForPageToLoad($this->getTimeoutMsec());
348
349 //login to check membership
350
351 // Log in using webtestLogin() method
352 $this->webtestLogin();
353
354 $this->openCiviPage('member/search', 'reset=1', 'member_end_date_high');
355
356 $this->type("sort_name", "{$contactParams['first_name']} {$contactParams['last_name']}");
357 $this->click("_qf_Search_refresh");
358
359 $this->waitForPageToLoad($this->getTimeoutMsec());
360 $this->assertElementContainsText('crm-container', '2 Results');
361
362 $this->waitForElementPresent("xpath=//div[@id='memberSearch']/table/tbody/tr");
363 $this->click("xpath=//div[@id='memberSearch']/table/tbody//tr/td[4][text()='{$memTypeTitle1}']/../td[11]/span/a[text()='View']");
364 $this->waitForElementPresent("_qf_MembershipView_cancel-bottom");
365
366 //View Membership Record
367 $verifyData = array(
368 'Membership Type' => "$memTypeTitle1",
369 'Status' => 'New',
370 'Member Since' => $joinDate,
371 'Start date' => $startDate,
372 'End date' => $endDate,
373 );
374 foreach ($verifyData as $label => $value) {
375 $this->verifyText("xpath=//form[@id='MembershipView']//table/tbody/tr/td[text()='{$label}']/following-sibling::td",
376 preg_quote($value)
377 );
378 }
379
380 $this->clickLink('_qf_MembershipView_cancel-bottom', "xpath=//div[@id='memberSearch']/table/tbody/tr[2]");
381 $this->click("xpath=//div[@id='memberSearch']/table/tbody//tr/td[4][text()='{$memTypeTitle2}']/../td[11]/span/a[text()='View']");
382 $this->waitForElementPresent("_qf_MembershipView_cancel-bottom");
383
384 //View Membership Record
385 $verifyData = array(
386 'Membership Type' => "$memTypeTitle2",
387 'Status' => 'New',
388 'Member Since' => $joinDate,
389 'Start date' => $startDate,
390 'End date' => $endDate,
391 );
392 foreach ($verifyData as $label => $value) {
393 $this->verifyText("xpath=//form[@id='MembershipView']//table/tbody/tr/td[text()='{$label}']/following-sibling::td",
394 preg_quote($value)
395 );
396 }
397 }
398
399 function _testMultilpeTermsMembershipRegistration($pageId, $contactParams, $memTypeTitle1, $term, $renew = FALSE){
400 if($renew){
401 $this->openCiviPage('member/search', 'reset=1', 'member_end_date_high');
402 $this->type("sort_name", "{$contactParams['first_name']} {$contactParams['last_name']}");
403 $this->click("_qf_Search_refresh");
404 $this->waitForPageToLoad($this->getTimeoutMsec());
405 $this->waitForElementPresent("xpath=//div[@id='memberSearch']/table/tbody/tr");
406 $this->click("xpath=//div[@id='memberSearch']/table/tbody//tr/td[4][text()='{$memTypeTitle1}']/../td[11]/span/a[text()='View']");
407 $this->waitForElementPresent("_qf_MembershipView_cancel-bottom");
408 $year = CRM_Utils_Date::processDate($this->getText("xpath=//form[@id='MembershipView']//table/tbody/tr/td[text()='End date']/following-sibling::td"));
409 $prevYear = substr($year, 0, 4);
410 }
411
412 $this->webtestLogout();
413
414 $this->openCiviPage('contribute/transact', "reset=1&id=$pageId", '_qf_Main_upload-bottom');
415
416 //build the membership dates.
417 $currentYear = date('Y');
418 $currentMonth = date('m');
419 $previousDay = date('d') - 1;
420 $endYear = ($term == 3) ? $currentYear + 3 : (($term == 2) ? $currentYear + 2 : $currentYear + 1);
421 $endYear = ($renew) ? $endYear + ($prevYear - $currentYear) : $endYear;
422 $joinDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
423 $startDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
424 $endDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, $previousDay, $endYear));
425 $configVars = new CRM_Core_Config_Variables();
426 foreach (array(
427 'joinDate', 'startDate', 'endDate') as $date) {
428 $$date = CRM_Utils_Date::customFormat($$date, $configVars->dateformatFull);
429 }
430 $i = ($term == 3) ? 3 : (($term == 2) ? 2 : 1 );
431 $this->waitForElementPresent("xpath=//div[@id='priceset']/div[2]/div[2]/div[$i]/span/input");
432 $this->click("xpath=//div[@id='priceset']/div[2]/div[2]/div[$i]/span/input");
433 $amount = $this->getText("xpath=//div[@id='priceset']/div[2]/div[2]/div[$i]/span/label/span[@class='crm-price-amount-amount']");
434
435 $this->type('email-5', $contactParams['email-5']);
436 $this->type('first_name', $contactParams['first_name']);
437 $this->type('last_name', $contactParams['last_name']);
438
439 $streetAddress = "100 Main Street";
440 $this->type("street_address-1", $streetAddress);
441 $this->type("city-1", "San Francisco");
442 $this->type("postal_code-1", "94117");
443 $this->select("country-1", "value=1228");
444 $this->select("state_province-1", "value=1001");
445
446 //Credit Card Info
447 $this->select("credit_card_type", "value=Visa");
448 $this->type("credit_card_number", "4111111111111111");
449 $this->type("cvv2", "000");
450 $this->select("credit_card_exp_date[M]", "value=1");
451 $this->select("credit_card_exp_date[Y]", "value=2020");
452
453 //Billing Info
454 $this->type("billing_first_name", $contactParams['first_name'] . "billing");
455 $this->type("billing_last_name", $contactParams['last_name'] . "billing");
456 $this->type("billing_street_address-5", "15 Main St.");
457 $this->type(" billing_city-5", "San Jose");
458 $this->select("billing_country_id-5", "value=1228");
459 $this->select("billing_state_province_id-5", "value=1004");
460 $this->type("billing_postal_code-5", "94129");
461 $this->click("_qf_Main_upload-bottom");
462
463 $this->waitForPageToLoad($this->getTimeoutMsec());
464 $this->waitForElementPresent("_qf_Confirm_next-bottom");
465
466 $this->click("_qf_Confirm_next-bottom");
467 $this->waitForPageToLoad($this->getTimeoutMsec());
468
469 //login to check membership
470
471 // Log in using webtestLogin() method
472 $this->webtestLogin();
473
474 $this->openCiviPage('member/search', 'reset=1', 'member_end_date_high');
475
476 $this->type("sort_name", "{$contactParams['first_name']} {$contactParams['last_name']}");
477 $this->click("_qf_Search_refresh");
478
479 $this->waitForPageToLoad($this->getTimeoutMsec());
480 $this->assertElementContainsText('crm-container', '1 Result ');
481
482 $this->waitForElementPresent("xpath=//div[@id='memberSearch']/table/tbody/tr");
483 $this->click("xpath=//div[@id='memberSearch']/table/tbody//tr/td[4][text()='{$memTypeTitle1}']/../td[11]/span/a[text()='View']");
484 $this->waitForElementPresent("_qf_MembershipView_cancel-bottom");
485
486 //View Membership Record
487 $verifyData = array(
488 'Membership Type' => "$memTypeTitle1",
489 'Status' => 'New',
490 'Member Since' => $joinDate,
491 'Start date' => $startDate,
492 'End date' => $endDate,
493 );
494 foreach ($verifyData as $label => $value) {
495 $this->verifyText("xpath=//form[@id='MembershipView']//table/tbody/tr/td[text()='{$label}']/following-sibling::td",
496 preg_quote($value)
497 );
498 }
499 //check if the membership amount is correct
500 $this->assertTrue($this->isElementPresent("xpath=//form[@id='MembershipView']/div[2]/div/table[2]/tbody/tr/td/span[text()='{$amount}']"));
501 }
502
503}
504