| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * File for the MembershipTest class |
| 30 | * |
| 31 | * (PHP 5) |
| 32 | * |
| 33 | * @author Walt Haas <walt@dharmatech.org> (801) 534-1262 |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Include class definitions |
| 38 | */ |
| 39 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 40 | |
| 41 | require_once 'HTML/QuickForm/Page.php'; |
| 42 | |
| 43 | /** |
| 44 | * Test CRM_Member_Form_Membership functions. |
| 45 | * |
| 46 | * @package CiviCRM |
| 47 | */ |
| 48 | class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { |
| 49 | |
| 50 | /** |
| 51 | * Assume empty database with just civicrm_data. |
| 52 | */ |
| 53 | protected $_individualId; |
| 54 | protected $_contribution; |
| 55 | protected $_financialTypeId = 1; |
| 56 | protected $_apiversion; |
| 57 | protected $_entity = 'Membership'; |
| 58 | protected $_params; |
| 59 | protected $_ids = array(); |
| 60 | protected $_paymentProcessorID; |
| 61 | |
| 62 | /** |
| 63 | * Parameters to create payment processor. |
| 64 | * |
| 65 | * @var array |
| 66 | */ |
| 67 | protected $_processorParams = array(); |
| 68 | |
| 69 | /** |
| 70 | * ID of created membership. |
| 71 | * |
| 72 | * @var int |
| 73 | */ |
| 74 | protected $_membershipID; |
| 75 | |
| 76 | /** |
| 77 | * Payment instrument mapping. |
| 78 | * |
| 79 | * @var array |
| 80 | */ |
| 81 | protected $paymentInstruments = array(); |
| 82 | |
| 83 | /** |
| 84 | * Test setup for every test. |
| 85 | * |
| 86 | * Connect to the database, truncate the tables that will be used |
| 87 | * and redirect stdin to a temporary file. |
| 88 | */ |
| 89 | public function setUp() { |
| 90 | $this->_apiversion = 3; |
| 91 | parent::setUp(); |
| 92 | |
| 93 | $this->_individualId = $this->individualCreate(); |
| 94 | $this->_paymentProcessorID = $this->processorCreate(); |
| 95 | // Insert test data. |
| 96 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); |
| 97 | $op->execute($this->_dbconn, |
| 98 | $this->createFlatXMLDataSet( |
| 99 | dirname(__FILE__) . '/dataset/data.xml' |
| 100 | ) |
| 101 | ); |
| 102 | |
| 103 | $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id')); |
| 104 | $this->paymentInstruments = $instruments['values']; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Clean up after each test. |
| 109 | */ |
| 110 | public function tearDown() { |
| 111 | $this->quickCleanUpFinancialEntities(); |
| 112 | $this->quickCleanup( |
| 113 | array( |
| 114 | 'civicrm_relationship', |
| 115 | 'civicrm_membership_type', |
| 116 | 'civicrm_membership', |
| 117 | ) |
| 118 | ); |
| 119 | $this->callAPISuccess('contact', 'delete', array('id' => 17, 'skip_undelete' => TRUE)); |
| 120 | $this->callAPISuccess('contact', 'delete', array('id' => 23, 'skip_undelete' => TRUE)); |
| 121 | $this->callAPISuccess('relationship_type', 'delete', array('id' => 20)); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Test CRM_Member_Form_Membership::buildQuickForm() |
| 126 | */ |
| 127 | //function testCRMMemberFormMembershipBuildQuickForm() |
| 128 | //{ |
| 129 | // throw new PHPUnit_Framework_IncompleteTestError( "not implemented" ); |
| 130 | //} |
| 131 | |
| 132 | /** |
| 133 | * Test CRM_Member_Form_Membership::formRule() with a parameter |
| 134 | * that has an empty contact_select_id value |
| 135 | */ |
| 136 | public function testFormRuleEmptyContact() { |
| 137 | $params = array( |
| 138 | 'contact_select_id' => 0, |
| 139 | 'membership_type_id' => array(), |
| 140 | ); |
| 141 | $files = array(); |
| 142 | $obj = new CRM_Member_Form_Membership(); |
| 143 | $rc = $obj->formRule($params, $files, $obj); |
| 144 | $this->assertType('array', $rc); |
| 145 | $this->assertTrue(array_key_exists('membership_type_id', $rc)); |
| 146 | |
| 147 | $params['membership_type_id'] = array(1 => 3); |
| 148 | $rc = $obj->formRule($params, $files, $obj); |
| 149 | $this->assertType('array', $rc); |
| 150 | $this->assertTrue(array_key_exists('join_date', $rc)); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Test that form rule fails if start date is before join date. |
| 155 | * |
| 156 | * Test CRM_Member_Form_Membership::formRule() with a parameter |
| 157 | * that has an start date before the join date and a rolling |
| 158 | * membership type. |
| 159 | */ |
| 160 | public function testFormRuleRollingEarlyStart() { |
| 161 | $unixNow = time(); |
| 162 | $ymdNow = date('m/d/Y', $unixNow); |
| 163 | $unixYesterday = $unixNow - (24 * 60 * 60); |
| 164 | $ymdYesterday = date('m/d/Y', $unixYesterday); |
| 165 | $params = array( |
| 166 | 'join_date' => $ymdNow, |
| 167 | 'start_date' => $ymdYesterday, |
| 168 | 'end_date' => '', |
| 169 | 'membership_type_id' => array('23', '15'), |
| 170 | ); |
| 171 | $files = array(); |
| 172 | $obj = new CRM_Member_Form_Membership(); |
| 173 | $rc = call_user_func(array('CRM_Member_Form_Membership', 'formRule'), |
| 174 | $params, $files, $obj |
| 175 | ); |
| 176 | $this->assertType('array', $rc); |
| 177 | $this->assertTrue(array_key_exists('start_date', $rc)); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Test CRM_Member_Form_Membership::formRule() with a parameter |
| 182 | * that has an end date before the start date and a rolling |
| 183 | * membership type |
| 184 | */ |
| 185 | public function testFormRuleRollingEarlyEnd() { |
| 186 | $unixNow = time(); |
| 187 | $ymdNow = date('m/d/Y', $unixNow); |
| 188 | $unixYesterday = $unixNow - (24 * 60 * 60); |
| 189 | $ymdYesterday = date('m/d/Y', $unixYesterday); |
| 190 | $params = array( |
| 191 | 'join_date' => $ymdNow, |
| 192 | 'start_date' => $ymdNow, |
| 193 | 'end_date' => $ymdYesterday, |
| 194 | 'membership_type_id' => array('23', '15'), |
| 195 | ); |
| 196 | $files = array(); |
| 197 | $obj = new CRM_Member_Form_Membership(); |
| 198 | $rc = $obj->formRule($params, $files, $obj); |
| 199 | $this->assertType('array', $rc, |
| 200 | 'In line ' . __LINE__ |
| 201 | ); |
| 202 | $this->assertTrue(array_key_exists('end_date', $rc), |
| 203 | 'In line ' . __LINE__ |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Test CRM_Member_Form_Membership::formRule() with end date but no start date and a rolling membership type. |
| 209 | */ |
| 210 | public function testFormRuleRollingEndNoStart() { |
| 211 | $unixNow = time(); |
| 212 | $ymdNow = date('m/d/Y', $unixNow); |
| 213 | $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60); |
| 214 | $ymdYearFromNow = date('m/d/Y', $unixYearFromNow); |
| 215 | $params = array( |
| 216 | 'join_date' => $ymdNow, |
| 217 | 'start_date' => '', |
| 218 | 'end_date' => $ymdYearFromNow, |
| 219 | 'membership_type_id' => array('23', '15'), |
| 220 | ); |
| 221 | $files = array(); |
| 222 | $obj = new CRM_Member_Form_Membership(); |
| 223 | $rc = $obj->formRule($params, $files, $obj); |
| 224 | $this->assertType('array', $rc, |
| 225 | 'In line ' . __LINE__ |
| 226 | ); |
| 227 | $this->assertTrue(array_key_exists('start_date', $rc), |
| 228 | 'In line ' . __LINE__ |
| 229 | ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Test CRM_Member_Form_Membership::formRule() with a parameter |
| 234 | * that has an end date and a lifetime membership type |
| 235 | */ |
| 236 | public function testFormRuleRollingLifetimeEnd() { |
| 237 | $unixNow = time(); |
| 238 | $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60); |
| 239 | $params = array( |
| 240 | 'join_date' => date('m/d/Y', $unixNow), |
| 241 | 'start_date' => date('m/d/Y', $unixNow), |
| 242 | 'end_date' => date('m/d/Y', |
| 243 | $unixYearFromNow |
| 244 | ), |
| 245 | 'membership_type_id' => array('23', '25'), |
| 246 | ); |
| 247 | $files = array(); |
| 248 | $obj = new CRM_Member_Form_Membership(); |
| 249 | $rc = $obj->formRule($params, $files, $obj); |
| 250 | $this->assertType('array', $rc); |
| 251 | $this->assertTrue(array_key_exists('status_id', $rc)); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Test CRM_Member_Form_Membership::formRule() with a parameter |
| 256 | * that has an override and no status |
| 257 | */ |
| 258 | public function testFormRuleOverrideNoStatus() { |
| 259 | $unixNow = time(); |
| 260 | $params = array( |
| 261 | 'join_date' => date('m/d/Y', $unixNow), |
| 262 | 'membership_type_id' => array('23', '25'), |
| 263 | 'is_override' => TRUE, |
| 264 | ); |
| 265 | $files = array(); |
| 266 | $obj = new CRM_Member_Form_Membership(); |
| 267 | $rc = $obj->formRule($params, $files, $obj); |
| 268 | $this->assertType('array', $rc); |
| 269 | $this->assertTrue(array_key_exists('status_id', $rc)); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Test CRM_Member_Form_Membership::formRule() with a join date |
| 274 | * of one month from now and a rolling membership type |
| 275 | */ |
| 276 | public function testFormRuleRollingJoin1MonthFromNow() { |
| 277 | $unixNow = time(); |
| 278 | $unix1MFmNow = $unixNow + (31 * 24 * 60 * 60); |
| 279 | $params = array( |
| 280 | 'join_date' => date('m/d/Y', $unix1MFmNow), |
| 281 | 'start_date' => '', |
| 282 | 'end_date' => '', |
| 283 | 'membership_type_id' => array('23', '15'), |
| 284 | ); |
| 285 | $files = array(); |
| 286 | $obj = new CRM_Member_Form_Membership(); |
| 287 | $rc = $obj->formRule($params, $files, $obj); |
| 288 | |
| 289 | // Should have found no valid membership status. |
| 290 | $this->assertType('array', $rc); |
| 291 | $this->assertTrue(array_key_exists('_qf_default', $rc)); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Test CRM_Member_Form_Membership::formRule() with a join date of today and a rolling membership type. |
| 296 | */ |
| 297 | public function testFormRuleRollingJoinToday() { |
| 298 | $unixNow = time(); |
| 299 | $params = array( |
| 300 | 'join_date' => date('m/d/Y', $unixNow), |
| 301 | 'start_date' => '', |
| 302 | 'end_date' => '', |
| 303 | 'membership_type_id' => array('23', '15'), |
| 304 | ); |
| 305 | $files = array(); |
| 306 | $obj = new CRM_Member_Form_Membership(); |
| 307 | $rc = $obj->formRule($params, $files, $obj); |
| 308 | |
| 309 | // Should have found New membership status |
| 310 | $this->assertTrue($rc); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Test CRM_Member_Form_Membership::formRule() with a join date |
| 315 | * of one month ago and a rolling membership type |
| 316 | */ |
| 317 | public function testFormRuleRollingJoin1MonthAgo() { |
| 318 | $unixNow = time(); |
| 319 | $unix1MAgo = $unixNow - (31 * 24 * 60 * 60); |
| 320 | $params = array( |
| 321 | 'join_date' => date('m/d/Y', $unix1MAgo), |
| 322 | 'start_date' => '', |
| 323 | 'end_date' => '', |
| 324 | 'membership_type_id' => array('23', '15'), |
| 325 | ); |
| 326 | $files = array(); |
| 327 | $obj = new CRM_Member_Form_Membership(); |
| 328 | $rc = $obj->formRule($params, $files, $obj); |
| 329 | |
| 330 | // Should have found New membership status. |
| 331 | $this->assertTrue($rc); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Test CRM_Member_Form_Membership::formRule() with a join date of six months ago and a rolling membership type. |
| 336 | */ |
| 337 | public function testFormRuleRollingJoin6MonthsAgo() { |
| 338 | $unixNow = time(); |
| 339 | $unix6MAgo = $unixNow - (180 * 24 * 60 * 60); |
| 340 | $params = array( |
| 341 | 'join_date' => date('m/d/Y', $unix6MAgo), |
| 342 | 'start_date' => '', |
| 343 | 'end_date' => '', |
| 344 | 'membership_type_id' => array('23', '15'), |
| 345 | ); |
| 346 | $files = array(); |
| 347 | $obj = new CRM_Member_Form_Membership(); |
| 348 | $rc = $obj->formRule($params, $files, $obj); |
| 349 | |
| 350 | // Should have found Current membership status. |
| 351 | $this->assertTrue($rc); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Test CRM_Member_Form_Membership::formRule() with a join date |
| 356 | * of one year+ ago and a rolling membership type |
| 357 | */ |
| 358 | public function testFormRuleRollingJoin1YearAgo() { |
| 359 | $unixNow = time(); |
| 360 | $unix1YAgo = $unixNow - (370 * 24 * 60 * 60); |
| 361 | $params = array( |
| 362 | 'join_date' => date('m/d/Y', $unix1YAgo), |
| 363 | 'start_date' => '', |
| 364 | 'end_date' => '', |
| 365 | 'membership_type_id' => array('23', '15'), |
| 366 | ); |
| 367 | $files = array(); |
| 368 | $obj = new CRM_Member_Form_Membership(); |
| 369 | $rc = $obj->formRule($params, $files, $obj); |
| 370 | |
| 371 | // Should have found Grace membership status |
| 372 | $this->assertTrue($rc); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Test CRM_Member_Form_Membership::formRule() with a join date |
| 377 | * of two years ago and a rolling membership type |
| 378 | */ |
| 379 | public function testFormRuleRollingJoin2YearsAgo() { |
| 380 | $unixNow = time(); |
| 381 | $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60); |
| 382 | $params = array( |
| 383 | 'join_date' => date('m/d/Y', $unix2YAgo), |
| 384 | 'start_date' => '', |
| 385 | 'end_date' => '', |
| 386 | 'membership_type_id' => array('23', '15'), |
| 387 | ); |
| 388 | $files = array(); |
| 389 | $obj = new CRM_Member_Form_Membership(); |
| 390 | $rc = $obj->formRule($params, $files, $obj); |
| 391 | |
| 392 | // Should have found Expired membership status |
| 393 | $this->assertTrue($rc); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Test CRM_Member_Form_Membership::formRule() with a join date |
| 398 | * of six months ago and a fixed membership type |
| 399 | */ |
| 400 | public function testFormRuleFixedJoin6MonthsAgo() { |
| 401 | $unixNow = time(); |
| 402 | $unix6MAgo = $unixNow - (180 * 24 * 60 * 60); |
| 403 | $params = array( |
| 404 | 'join_date' => date('m/d/Y', $unix6MAgo), |
| 405 | 'start_date' => '', |
| 406 | 'end_date' => '', |
| 407 | 'membership_type_id' => array('23', '7'), |
| 408 | ); |
| 409 | $files = array(); |
| 410 | $obj = new CRM_Member_Form_Membership(); |
| 411 | $rc = $obj->formRule($params, $files, $obj); |
| 412 | |
| 413 | // Should have found Current membership status |
| 414 | $this->assertTrue($rc); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Test the submit function of the membership form. |
| 419 | */ |
| 420 | public function testSubmit() { |
| 421 | $form = new CRM_Member_Form_Membership(); |
| 422 | $this->createLoggedInUser(); |
| 423 | $params = array( |
| 424 | 'cid' => $this->_individualId, |
| 425 | 'join_date' => date('m/d/Y', time()), |
| 426 | 'start_date' => '', |
| 427 | 'end_date' => '', |
| 428 | 'membership_type_id' => array('25'), |
| 429 | 'auto_renew' => '0', |
| 430 | 'max_related' => '', |
| 431 | 'num_terms' => '1', |
| 432 | 'source' => '', |
| 433 | 'total_amount' => '50.00', |
| 434 | 'financial_type_id' => '2', //Member dues, see data.xml |
| 435 | 'soft_credit_type_id' => '', |
| 436 | 'soft_credit_contact_id' => '', |
| 437 | 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>', |
| 438 | 'receipt_text_signup' => 'Thank you text', |
| 439 | 'payment_processor_id' => $this->_paymentProcessorID, |
| 440 | 'credit_card_number' => '4111111111111111', |
| 441 | 'cvv2' => '123', |
| 442 | 'credit_card_exp_date' => array( |
| 443 | 'M' => '9', |
| 444 | 'Y' => '2019', // TODO: Future proof |
| 445 | ), |
| 446 | 'credit_card_type' => 'Visa', |
| 447 | 'billing_first_name' => 'Test', |
| 448 | 'billing_middlename' => 'Last', |
| 449 | 'billing_street_address-5' => '10 Test St', |
| 450 | 'billing_city-5' => 'Test', |
| 451 | 'billing_state_province_id-5' => '1003', |
| 452 | 'billing_postal_code-5' => '90210', |
| 453 | 'billing_country_id-5' => '1228', |
| 454 | ); |
| 455 | $form->submit($params); |
| 456 | $this->callAPISuccessGetCount('Membership', array('contact_id' => $this->_individualId), 1); |
| 457 | } |
| 458 | |
| 459 | } |
| 460 | |