Merge pull request #24 from deepak-srivastava/24571eeacd20595493c8077eb0e0c01ea814b9df
[civicrm-core.git] / tests / phpunit / WebTest / Member / OfflineMembershipAddPricesetTest.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_OfflineMembershipAddPricesetTest extends CiviSeleniumTestCase {
30
31 protected function setUp() {
32 parent::setUp();
33 }
34
35 function testAddPriceSet() {
36 // This is the path where our testing install resides.
37 // The rest of URL is defined in CiviSeleniumTestCase base class, in
38 // class attributes.
39 $this->open($this->sboxPath);
40
41 // Log in using webtestLogin() method
42 $this->webtestLogin();
43
44 $title = substr(sha1(rand()), 0, 7);
45 $setTitle = "Membership Fees - $title";
46 $usedFor = 'Membership';
47 $contributionType = 'Donation';
48 $setHelp = 'Select your membership options.';
49 $this->_testAddSet($setTitle, $usedFor, $contributionType, $setHelp);
50
51 // Get the price set id ($sid) by retrieving and parsing the URL of the New Price Field form
52 // which is where we are after adding Price Set.
53 $elements = $this->parseURL();
54 $sid = $elements['queryString']['sid'];
55 $this->assertType('numeric', $sid);
56
57 $fields = array(
58 "National Membership $title" => 'Radio',
59 "Local Chapter $title" => 'CheckBox',
60 );
61
62 list($memTypeTitle1, $memTypeTitle2) = $this->_testAddPriceFields($fields, $validateStrings, FALSE, $title, $sid, $contributionType);
63
64 // load the Price Set Preview and check for expected values
65 $this->_testVerifyPriceSet($validateStrings, $sid);
66
67 // Sign up for membership
68 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
69 $lastName = 'Anderson_' . substr(sha1(rand()), 0, 7);
70 $email = "{$firstName}.{$lastName}@example.com";
71 $contactParams = array(
72 'first_name' => $firstName,
73 'last_name' => $lastName,
74 'email-5' => $email,
75 );
76
77 // Add a contact from the quick add block
78 $this->webtestAddContact($firstName, $lastName, $email);
79
80 $this->_testSignUpOrRenewMembership($sid, $contactParams, $memTypeTitle1, $memTypeTitle2);
81
82 // Renew this membership
83 $this->_testSignUpOrRenewMembership($sid, $contactParams, $memTypeTitle1, $memTypeTitle2, $renew = TRUE);
84 }
85
86 function testAddPriceSetWithMultipleTerms() {
87 // This is the path where our testing install resides.
88 // The rest of URL is defined in CiviSeleniumTestCase base class, in
89 // class attributes.
90 $this->open($this->sboxPath);
91
92 // Log in using webtestLogin() method
93 $this->webtestLogin();
94
95 $title = substr(sha1(rand()), 0, 7);
96 $setTitle = "Membership Fees - $title";
97 $usedFor = 'Membership';
98 $contributionType = 'Member Dues';
99 $setHelp = 'Select your membership options.';
100 $memTypeParams1 = $this->webtestAddMembershipType();
101 $memTypeTitle1 = $memTypeParams1['membership_type'];
102 $memTypeId1 = explode('&id=', $this->getAttribute("xpath=//div[@id='membership_type']/div[2]/table/tbody//tr/td[text()='{$memTypeTitle1}']/../td[12]/span/a[3]@href"));
103 $memTypeId1 = $memTypeId1[1];
104 $this->_testAddSet($setTitle, $usedFor, $contributionType, $setHelp);
105
106 // Get the price set id ($sid) by retrieving and parsing the URL of the New Price Field form
107 // which is where we are after adding Price Set.
108 $elements = $this->parseURL();
109 $sid = $elements['queryString']['sid'];
110 $this->assertType('numeric', $sid);
111
112 $fields = array("National Membership $title", "Radio");
113 $this->openCiviPage("admin/price/field", "reset=1&action=add&sid={$sid}");
114
115 $validateStrings[] = $fields[0];
116 $this->type('label', $fields[0]);
117 $this->select('html_type', "value={$fields[1]}");
118 $options = array(
119 1 => array('label' => $memTypeTitle1."_1",
120 'membership_type_id' => $memTypeId1,
121 'amount' => 50.00,
122 'membership_num_terms' => 1,
123 ),
124 2 => array(
125 'label' => $memTypeTitle1."_2",
126 'membership_type_id' => $memTypeId1,
127 'amount' => 90.00,
128 'membership_num_terms' => 2,
129 ),
130 3 => array(
131 'label' => $memTypeTitle1."_3",
132 'membership_type_id' => $memTypeId1,
133 'amount' => 120.00,
134 'membership_num_terms' => 3,
135 ),
136 );
137 $i = 2;
138 foreach($options as $index => $values){
139 $this->select("membership_type_id_{$index}", "value={$values['membership_type_id']}");
140 sleep(1);
141 $this->type("xpath=//table[@id='optionField']/tbody/tr[$i]/td[4]/input",$values['membership_num_terms']);
142 $this->type("xpath=//table[@id='optionField']/tbody/tr[$i]/td[5]/input",$values['label']);
143 $this->type("xpath=//table[@id='optionField']/tbody/tr[$i]/td[6]/input",$values['amount']);
144 if($i > 3){
145 $this->click('link=another choice');
146 }
147 $i++;
148 }
149 $this->waitForElementPresent( 'financial_type_id' );
150 $this->select("financial_type_id", "label={$contributionType}");
151 $this->waitForElementPresent('_qf_Field_next-bottom');
152 $this->click('_qf_Field_next-bottom');
153 $this->waitForPageToLoad($this->getTimeoutMsec());
154 $this->assertTrue($this->isTextPresent("Price Field '{$fields[0]}' has been saved."));
155
156 // load the Price Set Preview and check for expected values
157 $this->_testVerifyPriceSet($validateStrings, $sid);
158
159 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
160 $lastName = 'Anderson_' . substr(sha1(rand()), 0, 7);
161 $email = "{$firstName}.{$lastName}@example.com";
162
163 $contactParams = array(
164 'first_name' => $firstName,
165 'last_name' => $lastName,
166 'email-5' => $email,
167 );
168 $this->webtestAddContact($firstName, $lastName, $email);
169 //membership with number of terms as 3
170 $this->_testMultilpeTermsMembershipRegistration($sid, $contactParams, $memTypeTitle1, 3);
171 //membership with number of terms as 2
172 $this->_testMultilpeTermsMembershipRegistration($sid, $contactParams, $memTypeTitle1, 2);
173
174 }
175
176
177 function _testAddSet($setTitle, $usedFor, $contributionType = NULL, $setHelp) {
178 $this->openCiviPage('admin/price', 'reset=1&action=add', '_qf_Set_next-bottom');
179
180 // Enter Priceset fields (Title, Used For ...)
181 $this->type('title', $setTitle);
182 if ($usedFor == 'Event') {
183 $this->check('extends_1');
184 }
185 elseif ($usedFor == 'Contribution') {
186 $this->check('extends_2');
187 }
188 elseif ($usedFor == 'Membership') {
189 $this->click('extends_3');
190 $this->waitForElementPresent('financial_type_id');
191 $this->select("css=select.form-select", "label={$contributionType}");
192 }
193 $this->type('help_pre', $setHelp);
194 $this->assertChecked('is_active', 'Verify that Is Active checkbox is set.');
195
196 $this->click('_qf_Set_next-bottom');
197 $this->waitForElementPresent('_qf_Field_next-bottom');
198 $this->assertElementContainsText('crm-notification-container', "Your Set '{$setTitle}' has been added. You can add fields to this set now.");
199 }
200
201 function _testAddPriceFields(&$fields, &$validateString, $dateSpecificFields = FALSE, $title, $sid, $contributionType) {
202 $memTypeParams1 = $this->webtestAddMembershipType();
203 $memTypeTitle1 = $memTypeParams1['membership_type'];
204
205 $memTypeId1 = explode('&id=', $this->getAttribute("xpath=//div[@id='membership_type']/div[2]/table/tbody//tr/td[text()='{$memTypeTitle1}']/../td[12]/span/a[3]@href"));
206 $memTypeId1 = $memTypeId1[1];
207
208 $memTypeParams2 = $this->webtestAddMembershipType();
209 $memTypeTitle2 = $memTypeParams2['membership_type'];
210 $memTypeId2 = explode('&id=', $this->getAttribute("xpath=//div[@id='membership_type']/div[2]/table/tbody//tr/td[text()='{$memTypeTitle2}']/../td[12]/span/a[3]@href"));
211 $memTypeId2 = $memTypeId2[1];
212
213 $this->openCiviPage("admin/price/field", "reset=1&action=add&sid={$sid}");
214
215 foreach ($fields as $label => $type) {
216 $validateStrings[] = $label;
217
218 $this->type('label', $label);
219 $this->select('html_type', "value={$type}");
220
221 switch ($type) {
222 case 'Radio':
223 $options = array(
224 1 => array('label' => "$memTypeTitle1",
225 'membership_type_id' => $memTypeId1,
226 'amount' => 100.00,
227 ),
228 2 => array(
229 'label' => "$memTypeTitle2",
230 'membership_type_id' => $memTypeId2,
231 'amount' => 50.00,
232 ),
233 );
234 $this->addMultipleChoiceOptions($options, $validateStrings);
235 break;
236
237 case 'CheckBox':
238 $options = array(
239 1 => array('label' => "$memTypeTitle1",
240 'membership_type_id' => $memTypeId1,
241 'amount' => 100.00,
242 ),
243 2 => array(
244 'label' => "$memTypeTitle2",
245 'membership_type_id' => $memTypeId2,
246 'amount' => 50.00,
247 ),
248 );
249 $this->addMultipleChoiceOptions($options, $validateStrings);
250 break;
251
252 default:
253 break;
254 }
255 $this->select("financial_type_id", "label={$contributionType}");
256 $this->click('_qf_Field_next_new-bottom');
257 $this->waitForPageToLoad($this->getTimeoutMsec());
258 $this->assertElementContainsText('crm-notification-container', "Price Field '{$label}' has been saved.");
259 }
260 return array($memTypeTitle1, $memTypeTitle2);
261 }
262
263 function _testVerifyPriceSet($validateStrings, $sid) {
264 // verify Price Set at Preview page
265 // start at Manage Price Sets listing
266 $this->openCiviPage('admin/price', 'reset=1');
267
268 // Use the price set id ($sid) to pick the correct row
269 $this->click("css=tr#row_{$sid} a[title='Preview Price Set']");
270
271 // Look for Register button
272 $this->waitForElementPresent('_qf_Preview_cancel-bottom');
273
274 // Check for expected price set field strings
275 $this->assertStringsPresent($validateStrings);
276 }
277
278 function _testSignUpOrRenewMembership($sid, $contactParams, $memTypeTitle1, $memTypeTitle2, $renew = FALSE) {
279 //build the membership dates.
280 require_once 'CRM/Core/Config.php';
281 require_once 'CRM/Utils/Array.php';
282 require_once 'CRM/Utils/Date.php';
283 $currentYear = date('Y');
284 $currentMonth = date('m');
285 $previousDay = date('d') - 1;
286 $endYear = ($renew) ? $currentYear + 2 : $currentYear + 1;
287 $joinDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
288 $startDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
289 $endDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, $previousDay, $endYear));
290 $configVars = new CRM_Core_Config_Variables();
291 foreach (array(
292 'joinDate', 'startDate', 'endDate') as $date) {
293 $$date = CRM_Utils_Date::customFormat($$date, $configVars->dateformatFull);
294 }
295
296 if (!$renew) {
297 // Go directly to the URL of the screen that you will be testing (Activity Tab).
298 $this->click('css=li#tab_member a');
299 $this->waitForElementPresent('link=Add Membership');
300
301 $this->click('link=Add Membership');
302 $this->waitForElementPresent('_qf_Membership_cancel-bottom');
303
304 $this->select('price_set_id', "value={$sid}");
305 $this->waitForElementPresent('pricesetTotal');
306
307 $this->click("xpath=//div[@id='priceset']/div[2]/div[2]/div/span/input");
308 $this->click("xpath=//div[@id='priceset']/div[3]/div[2]/div[2]/span/input");
309
310 $this->type('source', 'Offline membership Sign Up Test Text');
311 $this->click('_qf_Membership_upload-bottom');
312 }
313 else {
314 $this->click("xpath=//div[@id='memberships']//table/tbody//tr/td[text()='{$memTypeTitle1}']/../td[9]/span[2][text()='more']/ul/li/a[text()='Renew']");
315 $this->waitForElementPresent('_qf_MembershipRenewal_cancel-bottom');
316 $this->click('_qf_MembershipRenewal_upload-bottom');
317
318 $this->waitForElementPresent("xpath=//div[@id='memberships']//table/tbody/tr");
319 $this->click("xpath=//div[@id='memberships']//table/tbody//tr/td[text()='{$memTypeTitle2}']/../td[9]/span[2][text()='more']/ul/li/a[text()='Renew']");
320 $this->waitForElementPresent('_qf_MembershipRenewal_cancel-bottom');
321 $this->click('_qf_MembershipRenewal_upload-bottom');
322 }
323 $this->waitForElementPresent("xpath=//div[@id='memberships']//table/tbody/tr");
324 $this->click("xpath=//div[@id='memberships']//table/tbody//tr/td[text()='{$memTypeTitle1}']/../td[9]/span/a[text()='View']");
325 $this->waitForElementPresent("_qf_MembershipView_cancel-bottom");
326
327 //View Membership Record
328 $verifyData = array(
329 'Membership Type' => "{$memTypeTitle1}",
330 'Status' => 'New',
331 'Member Since' => $joinDate,
332 'Start date' => $startDate,
333 'End date' => $endDate,
334 );
335 $this->webtestVerifyTabularData($verifyData);
336
337 $this->click('_qf_MembershipView_cancel-bottom');
338 $this->waitForElementPresent("xpath=//div[@id='memberships']//table/tbody/tr");
339 $this->click("xpath=//div[@id='memberships']//table/tbody//tr/td[text()='{$memTypeTitle2}']/../td[9]/span/a[text()='View']");
340 $this->waitForElementPresent("_qf_MembershipView_cancel-bottom");
341
342 //View Membership Record
343 $verifyData = array(
344 'Membership Type' => "{$memTypeTitle2}",
345 'Status' => 'New',
346 'Member Since' => $joinDate,
347 'Start date' => $startDate,
348 'End date' => $endDate,
349 );
350 $this->webtestVerifyTabularData($verifyData);
351
352 $this->click("_qf_MembershipView_cancel-bottom");
353 $this->waitForElementPresent("xpath=//div[@id='memberships']//table/tbody/tr");
354 }
355
356 function _testMultilpeTermsMembershipRegistration($sid, $contactParams, $memTypeTitle1, $term){
357 //build the membership dates.
358 require_once 'CRM/Core/Config.php';
359 require_once 'CRM/Utils/Array.php';
360 require_once 'CRM/Utils/Date.php';
361 $currentYear = date('Y');
362 $currentMonth = date('m');
363 $previousDay = date('d') - 1;
364 $endYear = ($term == 3) ? $currentYear + 3 : (($term == 2) ? $currentYear + 2 : $currentYear + 1);
365 $joinDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
366 $startDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, date('d'), $currentYear));
367 $endDate = date('Y-m-d', mktime(0, 0, 0, $currentMonth, $previousDay, $endYear));
368 $configVars = new CRM_Core_Config_Variables();
369 foreach (array(
370 'joinDate', 'startDate', 'endDate') as $date) {
371 $$date = CRM_Utils_Date::customFormat($$date, $configVars->dateformatFull);
372 }
373
374 // Go directly to the URL of the screen that you will be testing (Activity Tab).
375 $this->click('css=li#tab_member a');
376 $this->waitForElementPresent('link=Add Membership');
377
378 $this->click('link=Add Membership');
379 $this->waitForElementPresent('_qf_Membership_cancel-bottom');
380
381 $this->select('price_set_id', "value={$sid}");
382 $this->waitForElementPresent('pricesetTotal');
383
384 $i = ($term == 3) ? 3 : (($term == 2) ? 2 : 1 );
385 $this->waitForElementPresent("xpath=//div[@id='priceset']/div[2]/div[2]/div[$i]/span/input");
386 $this->click("xpath=//div[@id='priceset']/div[2]/div[2]/div[$i]/span/input");
387 $amount = $this->getText("xpath=//div[@id='priceset']/div[2]/div[2]/div[$i]/span/label/span[@class='crm-price-amount-amount']");
388
389 $this->type('source', 'Offline membership Sign Up Test Text');
390 $this->click('_qf_Membership_upload-bottom');
391
392 $this->waitForElementPresent("xpath=//div[@id='memberships']//table/tbody/tr");
393 $this->click("xpath=//div[@id='memberships']//table/tbody//tr/td[text()='{$endDate}']/../td[9]/span/a[text()='View']");
394 $this->waitForElementPresent("_qf_MembershipView_cancel-bottom");
395 //View Membership Record
396 $verifyData = array(
397 'Membership Type' => "$memTypeTitle1",
398 'Status' => 'New',
399 'Member Since' => $joinDate,
400 'Start date' => $startDate,
401 'End date' => $endDate,
402 );
403 $this->webtestVerifyTabularData($verifyData);
404
405 //check if the membership amount is correct
406 $this->assertTrue($this->isElementPresent("xpath=//form[@id='MembershipView']/div[2]/div/table[2]/tbody/tr/td/span[text()='{$amount}']"));
407 $this->click("_qf_MembershipView_cancel-bottom");
408 $this->waitForPageToLoad($this->getTimeoutMsec());
409 }
410 }