| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Class api_v3_MembershipStatusTest |
| 14 | * @group headless |
| 15 | */ |
| 16 | class api_v3_MembershipStatusTest extends CiviUnitTestCase { |
| 17 | |
| 18 | protected $_contactID; |
| 19 | protected $_contributionTypeID; |
| 20 | protected $_membershipTypeID; |
| 21 | protected $_membershipStatusID; |
| 22 | |
| 23 | protected $_apiversion = 3; |
| 24 | |
| 25 | public function setUp() { |
| 26 | parent::setUp(); |
| 27 | $this->_contactID = $this->individualCreate(); |
| 28 | $this->_membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID]); |
| 29 | $this->_membershipStatusID = $this->membershipStatusCreate('test status'); |
| 30 | |
| 31 | CRM_Member_PseudoConstant::membershipType($this->_membershipTypeID, TRUE); |
| 32 | CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE); |
| 33 | } |
| 34 | |
| 35 | public function tearDown() { |
| 36 | $this->membershipStatusDelete($this->_membershipStatusID); |
| 37 | $this->membershipTypeDelete(['id' => $this->_membershipTypeID]); |
| 38 | $this->contactDelete($this->_contactID); |
| 39 | parent::tearDown(); |
| 40 | } |
| 41 | |
| 42 | ///////////////// civicrm_membership_status_get methods |
| 43 | |
| 44 | /** |
| 45 | * Test civicrm_membership_status_get with empty params. |
| 46 | */ |
| 47 | public function testGetEmptyParams() { |
| 48 | $result = $this->callAPISuccess('membership_status', 'get', []); |
| 49 | // It should be 8 statuses, 7 default from mysql_data |
| 50 | // plus one test status added in setUp |
| 51 | $this->assertEquals(8, $result['count']); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Test civicrm_membership_status_get. Success expected. |
| 56 | */ |
| 57 | public function testGet() { |
| 58 | $params = [ |
| 59 | 'name' => 'test status', |
| 60 | ]; |
| 61 | $result = $this->callAPIAndDocument('membership_status', 'get', $params, __FUNCTION__, __FILE__); |
| 62 | $this->assertEquals($result['values'][$this->_membershipStatusID]['name'], "test status", "In line " . __LINE__); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Test civicrm_membership_status_get. Success expected. |
| 67 | */ |
| 68 | public function testGetLimit() { |
| 69 | $result = $this->callAPISuccess('membership_status', 'get', []); |
| 70 | $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__); |
| 71 | $params['option.limit'] = 1; |
| 72 | $result = $this->callAPISuccess('membership_status', 'get', $params); |
| 73 | $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__); |
| 74 | } |
| 75 | |
| 76 | public function testCreateDuplicateName() { |
| 77 | $params = ['name' => 'name']; |
| 78 | $result = $this->callAPISuccess('membership_status', 'create', $params); |
| 79 | $result = $this->callAPIFailure('membership_status', 'create', $params, |
| 80 | 'A membership status with this name already exists.' |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | public function testCreateWithMissingRequired() { |
| 85 | $params = ['title' => 'Does not make sense']; |
| 86 | $this->callAPIFailure('membership_status', 'create', $params, 'Mandatory key(s) missing from params array: name'); |
| 87 | } |
| 88 | |
| 89 | public function testCreate() { |
| 90 | $params = [ |
| 91 | 'name' => 'test membership status', |
| 92 | ]; |
| 93 | $result = $this->callAPIAndDocument('membership_status', 'create', $params, __FUNCTION__, __FILE__); |
| 94 | |
| 95 | $this->assertNotNull($result['id']); |
| 96 | $this->membershipStatusDelete($result['id']); |
| 97 | } |
| 98 | |
| 99 | public function testUpdate() { |
| 100 | $params = [ |
| 101 | 'name' => 'test membership status', |
| 102 | ]; |
| 103 | $result = $this->callAPISuccess('membership_status', 'create', $params); |
| 104 | $id = $result['id']; |
| 105 | $result = $this->callAPISuccess('membership_status', 'get', $params); |
| 106 | $this->assertEquals('test membership status', $result['values'][$id]['name']); |
| 107 | $newParams = [ |
| 108 | 'id' => $id, |
| 109 | 'name' => 'renamed', |
| 110 | ]; |
| 111 | $result = $this->callAPISuccess('membership_status', 'create', $newParams); |
| 112 | $result = $this->callAPISuccess('membership_status', 'get', ['id' => $id]); |
| 113 | $this->assertEquals('renamed', $result['values'][$id]['name']); |
| 114 | $this->membershipStatusDelete($result['id']); |
| 115 | } |
| 116 | |
| 117 | ///////////////// civicrm_membership_status_delete methods |
| 118 | |
| 119 | /** |
| 120 | * Attempt (and fail) to delete membership status without an parameters. |
| 121 | */ |
| 122 | public function testDeleteEmptyParams() { |
| 123 | $result = $this->callAPIFailure('membership_status', 'delete', []); |
| 124 | } |
| 125 | |
| 126 | public function testDeleteWithMissingRequired() { |
| 127 | $params = ['title' => 'Does not make sense']; |
| 128 | $result = $this->callAPIFailure('membership_status', 'delete', $params); |
| 129 | } |
| 130 | |
| 131 | public function testDelete() { |
| 132 | $membershipID = $this->membershipStatusCreate(); |
| 133 | $params = [ |
| 134 | 'id' => $membershipID, |
| 135 | ]; |
| 136 | $result = $this->callAPISuccess('membership_status', 'delete', $params); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Test that trying to delete membership status while membership still exists creates error. |
| 141 | */ |
| 142 | public function testDeleteWithMembershipError() { |
| 143 | $membershipStatusID = $this->membershipStatusCreate(); |
| 144 | $this->_contactID = $this->individualCreate(); |
| 145 | $this->_entity = 'membership'; |
| 146 | $params = [ |
| 147 | 'contact_id' => $this->_contactID, |
| 148 | 'membership_type_id' => $this->_membershipTypeID, |
| 149 | 'join_date' => '2009-01-21', |
| 150 | 'start_date' => '2009-01-21', |
| 151 | 'end_date' => '2009-12-21', |
| 152 | 'source' => 'Payment', |
| 153 | 'is_override' => 1, |
| 154 | 'status_id' => $membershipStatusID, |
| 155 | ]; |
| 156 | |
| 157 | $result = $this->callAPISuccess('membership', 'create', $params); |
| 158 | $membershipID = $result['id']; |
| 159 | |
| 160 | $params = [ |
| 161 | 'id' => $membershipStatusID, |
| 162 | ]; |
| 163 | $result = $this->callAPIFailure('membership_status', 'delete', $params); |
| 164 | |
| 165 | $this->callAPISuccess('Membership', 'Delete', [ |
| 166 | 'id' => $membershipID, |
| 167 | ]); |
| 168 | $result = $this->callAPISuccess('membership_status', 'delete', $params); |
| 169 | } |
| 170 | |
| 171 | } |