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