| 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 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 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 29 | class api_v3_MembershipStatusTest extends CiviUnitTestCase { |
| 30 | |
| 31 | protected $_contactID; |
| 32 | protected $_contributionTypeID; |
| 33 | protected $_membershipTypeID; |
| 34 | protected $_membershipStatusID; |
| 35 | |
| 36 | protected $_apiversion =3; |
| 37 | |
| 38 | function get_info() { |
| 39 | return array( |
| 40 | 'name' => 'MembershipStatus Calc', |
| 41 | 'description' => 'Test all MembershipStatus Calc API methods.', |
| 42 | 'group' => 'CiviCRM API Tests', |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | function setUp() { |
| 47 | parent::setUp(); |
| 48 | $this->_contactID = $this->individualCreate(); |
| 49 | $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID)); |
| 50 | $this->_membershipStatusID = $this->membershipStatusCreate('test status'); |
| 51 | |
| 52 | CRM_Member_PseudoConstant::membershipType($this->_membershipTypeID, TRUE); |
| 53 | CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE); |
| 54 | } |
| 55 | |
| 56 | function tearDown() { |
| 57 | $this->membershipStatusDelete($this->_membershipStatusID); |
| 58 | $this->membershipTypeDelete(array('id' => $this->_membershipTypeID)); |
| 59 | $this->contactDelete($this->_contactID); |
| 60 | } |
| 61 | |
| 62 | ///////////////// civicrm_membership_status_get methods |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Test civicrm_membership_status_get with empty params |
| 67 | */ |
| 68 | function testGetEmptyParams() { |
| 69 | $result = $this->callAPISuccess('membership_status', 'get', array()); |
| 70 | // It should be 8 statuses, 7 default from mysql_data |
| 71 | // plus one test status added in setUp |
| 72 | $this->assertEquals(8, $result['count']); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Test civicrm_membership_status_get. Success expected. |
| 77 | */ |
| 78 | function testGet() { |
| 79 | $params = array( |
| 80 | 'name' => 'test status', |
| 81 | ); |
| 82 | $result = $this->callAPIAndDocument('membership_status', 'get', $params, __FUNCTION__, __FILE__); |
| 83 | $this->assertEquals($result['values'][$this->_membershipStatusID]['name'], "test status", "In line " . __LINE__); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Test civicrm_membership_status_get. Success expected. |
| 88 | */ |
| 89 | function testGetLimit() { |
| 90 | $result = $this->callAPISuccess('membership_status', 'get', array()); |
| 91 | $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__); |
| 92 | $params['option.limit'] = 1; |
| 93 | $result = $this->callAPISuccess('membership_status', 'get', $params); |
| 94 | $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__); |
| 95 | } |
| 96 | |
| 97 | function testCreateDuplicateName() { |
| 98 | $params = array('name' => 'name'); |
| 99 | $result = $this->callAPISuccess('membership_status', 'create', $params); |
| 100 | $result = $this->callAPIFailure('membership_status', 'create', $params, |
| 101 | 'A membership status with this name already exists.' |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | function testCreateWithMissingRequired() { |
| 106 | $params = array('title' => 'Does not make sense'); |
| 107 | $result = $this->callAPIFailure('membership_status', 'create', $params); |
| 108 | } |
| 109 | |
| 110 | function testCreate() { |
| 111 | $params = array( |
| 112 | 'name' => 'test membership status', |
| 113 | ); |
| 114 | $result = $this->callAPIAndDocument('membership_status', 'create', $params, __FUNCTION__, __FILE__); |
| 115 | |
| 116 | $this->assertNotNull($result['id']); |
| 117 | $this->membershipStatusDelete($result['id']); |
| 118 | } |
| 119 | |
| 120 | function testUpdate() { |
| 121 | $params = array( |
| 122 | 'name' => 'test membership status', ); |
| 123 | $result = $this->callAPISuccess('membership_status', 'create', $params); |
| 124 | $id = $result['id']; |
| 125 | $result = $this->callAPISuccess('membership_status', 'get', $params); |
| 126 | $this->assertEquals('test membership status', $result['values'][$id]['name']); |
| 127 | $newParams = array( |
| 128 | 'id' => $id, |
| 129 | 'name' => 'renamed', ); |
| 130 | $result = $this->callAPISuccess('membership_status', 'create', $newParams); |
| 131 | $result = $this->callAPISuccess('membership_status', 'get', array('id' => $id)); |
| 132 | $this->assertEquals('renamed', $result['values'][$id]['name']); |
| 133 | $this->membershipStatusDelete($result['id']); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | ///////////////// civicrm_membership_status_delete methods |
| 138 | function testDeleteEmptyParams() { |
| 139 | $result = $this->callAPIFailure('membership_status', 'delete', array()); |
| 140 | } |
| 141 | |
| 142 | function testDeleteWithMissingRequired() { |
| 143 | $params = array('title' => 'Does not make sense'); |
| 144 | $result = $this->callAPIFailure('membership_status', 'delete', $params); |
| 145 | } |
| 146 | |
| 147 | function testDelete() { |
| 148 | $membershipID = $this->membershipStatusCreate(); |
| 149 | $params = array( |
| 150 | 'id' => $membershipID, |
| 151 | ); |
| 152 | $result = $this->callAPISuccess('membership_status', 'delete', $params); |
| 153 | } |
| 154 | /* |
| 155 | * Test that trying to delete membership status while membership still exists creates error |
| 156 | */ |
| 157 | function testDeleteWithMembershipError() { |
| 158 | $membershipStatusID = $this->membershipStatusCreate(); |
| 159 | $this->_contactID = $this->individualCreate(); |
| 160 | $this->_entity = 'membership'; |
| 161 | $params = array( |
| 162 | 'contact_id' => $this->_contactID, |
| 163 | 'membership_type_id' => $this->_membershipTypeID, |
| 164 | 'join_date' => '2009-01-21', |
| 165 | 'start_date' => '2009-01-21', |
| 166 | 'end_date' => '2009-12-21', |
| 167 | 'source' => 'Payment', |
| 168 | 'is_override' => 1, |
| 169 | 'status_id' => $membershipStatusID, |
| 170 | ); |
| 171 | |
| 172 | $result = $this->callAPISuccess('membership', 'create', $params); |
| 173 | $membershipID = $result['id']; |
| 174 | |
| 175 | $params = array( |
| 176 | 'id' => $membershipStatusID, |
| 177 | ); |
| 178 | $result = $this->callAPIFailure('membership_status', 'delete', $params); |
| 179 | |
| 180 | $this->callAPISuccess('Membership', 'Delete', array( |
| 181 | 'id' => $membershipID, |
| 182 | )); |
| 183 | $result = $this->callAPISuccess('membership_status', 'delete', $params); |
| 184 | } |
| 185 | } |
| 186 | |