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