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