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