Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
e9479dcf EM |
12 | /** |
13 | * Class api_v3_MembershipStatusTest | |
acb109b7 | 14 | * @group headless |
e9479dcf | 15 | */ |
6a488035 TO |
16 | class api_v3_MembershipStatusTest extends CiviUnitTestCase { |
17 | ||
18 | protected $_contactID; | |
19 | protected $_contributionTypeID; | |
20 | protected $_membershipTypeID; | |
21 | protected $_membershipStatusID; | |
b7c9bc4c | 22 | |
6c6e6187 | 23 | protected $_apiversion = 3; |
6a488035 | 24 | |
00be9182 | 25 | public function setUp() { |
6a488035 | 26 | parent::setUp(); |
6a488035 | 27 | $this->_contactID = $this->individualCreate(); |
9099cab3 | 28 | $this->_membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID]); |
6a488035 TO |
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 | ||
00be9182 | 35 | public function tearDown() { |
6a488035 | 36 | $this->membershipStatusDelete($this->_membershipStatusID); |
fda18dc3 | 37 | $this->membershipTypeDelete(['id' => $this->_membershipTypeID]); |
6a488035 | 38 | $this->contactDelete($this->_contactID); |
fda18dc3 | 39 | parent::tearDown(); |
6a488035 TO |
40 | } |
41 | ||
42 | ///////////////// civicrm_membership_status_get methods | |
43 | ||
6a488035 | 44 | /** |
fe482240 | 45 | * Test civicrm_membership_status_get with empty params. |
6a488035 | 46 | */ |
00be9182 | 47 | public function testGetEmptyParams() { |
9099cab3 | 48 | $result = $this->callAPISuccess('membership_status', 'get', []); |
6a488035 TO |
49 | // It should be 8 statuses, 7 default from mysql_data |
50 | // plus one test status added in setUp | |
6901eb18 | 51 | $this->assertEquals(8, $result['count']); |
6a488035 TO |
52 | } |
53 | ||
54 | /** | |
d177a2a6 | 55 | * Test civicrm_membership_status_get. Success expected. |
6a488035 | 56 | */ |
00be9182 | 57 | public function testGet() { |
9099cab3 | 58 | $params = [ |
6a488035 | 59 | 'name' => 'test status', |
9099cab3 | 60 | ]; |
6901eb18 | 61 | $result = $this->callAPIAndDocument('membership_status', 'get', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
62 | $this->assertEquals($result['values'][$this->_membershipStatusID]['name'], "test status", "In line " . __LINE__); |
63 | } | |
64 | ||
65 | /** | |
d177a2a6 | 66 | * Test civicrm_membership_status_get. Success expected. |
6a488035 | 67 | */ |
00be9182 | 68 | public function testGetLimit() { |
9099cab3 | 69 | $result = $this->callAPISuccess('membership_status', 'get', []); |
8cdb5f76 | 70 | $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__); |
6a488035 | 71 | $params['option.limit'] = 1; |
6901eb18 | 72 | $result = $this->callAPISuccess('membership_status', 'get', $params); |
8cdb5f76 | 73 | $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__); |
6a488035 TO |
74 | } |
75 | ||
00be9182 | 76 | public function testCreateDuplicateName() { |
9099cab3 | 77 | $params = ['name' => 'name']; |
6901eb18 | 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 | ); | |
6a488035 TO |
82 | } |
83 | ||
00be9182 | 84 | public function testCreateWithMissingRequired() { |
9099cab3 | 85 | $params = ['title' => 'Does not make sense']; |
d235daf6 | 86 | $this->callAPIFailure('membership_status', 'create', $params, 'Mandatory key(s) missing from params array: name'); |
6a488035 TO |
87 | } |
88 | ||
00be9182 | 89 | public function testCreate() { |
9099cab3 | 90 | $params = [ |
6a488035 | 91 | 'name' => 'test membership status', |
9099cab3 | 92 | ]; |
6901eb18 | 93 | $result = $this->callAPIAndDocument('membership_status', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 | 94 | |
6a488035 TO |
95 | $this->assertNotNull($result['id']); |
96 | $this->membershipStatusDelete($result['id']); | |
97 | } | |
98 | ||
00be9182 | 99 | public function testUpdate() { |
9099cab3 | 100 | $params = [ |
92915c55 | 101 | 'name' => 'test membership status', |
9099cab3 | 102 | ]; |
6901eb18 | 103 | $result = $this->callAPISuccess('membership_status', 'create', $params); |
6a488035 | 104 | $id = $result['id']; |
6901eb18 | 105 | $result = $this->callAPISuccess('membership_status', 'get', $params); |
6a488035 | 106 | $this->assertEquals('test membership status', $result['values'][$id]['name']); |
9099cab3 | 107 | $newParams = [ |
6a488035 | 108 | 'id' => $id, |
92915c55 | 109 | 'name' => 'renamed', |
9099cab3 | 110 | ]; |
6901eb18 | 111 | $result = $this->callAPISuccess('membership_status', 'create', $newParams); |
9099cab3 | 112 | $result = $this->callAPISuccess('membership_status', 'get', ['id' => $id]); |
6a488035 TO |
113 | $this->assertEquals('renamed', $result['values'][$id]['name']); |
114 | $this->membershipStatusDelete($result['id']); | |
115 | } | |
116 | ||
6a488035 | 117 | ///////////////// civicrm_membership_status_delete methods |
a1a2a83d TO |
118 | |
119 | /** | |
120 | * Attempt (and fail) to delete membership status without an parameters. | |
121 | */ | |
00be9182 | 122 | public function testDeleteEmptyParams() { |
9099cab3 | 123 | $result = $this->callAPIFailure('membership_status', 'delete', []); |
6a488035 TO |
124 | } |
125 | ||
00be9182 | 126 | public function testDeleteWithMissingRequired() { |
9099cab3 | 127 | $params = ['title' => 'Does not make sense']; |
d0e1eff2 | 128 | $result = $this->callAPIFailure('membership_status', 'delete', $params); |
6a488035 TO |
129 | } |
130 | ||
00be9182 | 131 | public function testDelete() { |
6a488035 | 132 | $membershipID = $this->membershipStatusCreate(); |
9099cab3 | 133 | $params = [ |
6a488035 | 134 | 'id' => $membershipID, |
9099cab3 | 135 | ]; |
8cdb5f76 | 136 | $result = $this->callAPISuccess('membership_status', 'delete', $params); |
6a488035 | 137 | } |
c490a46a | 138 | |
f9e6db84 KK |
139 | /** |
140 | * Test that after checking the person as 'Deceased', the Membership is also 'Deceased' both through inline and normal edit. | |
141 | */ | |
142 | public function testDeceasedMembershipInline() { | |
143 | $contactID = $this->individualCreate(); | |
144 | $params = [ | |
145 | 'contact_id' => $contactID, | |
146 | 'membership_type_id' => $this->_membershipTypeID, | |
147 | 'join_date' => '2006-01-21', | |
148 | 'start_date' => '2006-01-21', | |
149 | 'end_date' => '2006-12-21', | |
150 | 'status_id' => $this->_membershipStatusID, | |
151 | ]; | |
152 | $this->callApiSuccess('membership', 'create', $params); | |
153 | $this->callApiSuccess('contact', 'create', ['id' => $contactID, 'is_deceased' => 1]); | |
154 | $membership = $this->callApiSuccessGetSingle('membership', ['contact_id' => $contactID]); | |
155 | $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased'), $membership['status_id']); | |
156 | } | |
157 | ||
c490a46a | 158 | /** |
eceb18cc | 159 | * Test that trying to delete membership status while membership still exists creates error. |
c490a46a | 160 | */ |
00be9182 | 161 | public function testDeleteWithMembershipError() { |
6a488035 TO |
162 | $membershipStatusID = $this->membershipStatusCreate(); |
163 | $this->_contactID = $this->individualCreate(); | |
164 | $this->_entity = 'membership'; | |
9099cab3 | 165 | $params = [ |
6a488035 TO |
166 | 'contact_id' => $this->_contactID, |
167 | 'membership_type_id' => $this->_membershipTypeID, | |
168 | 'join_date' => '2009-01-21', | |
169 | 'start_date' => '2009-01-21', | |
170 | 'end_date' => '2009-12-21', | |
171 | 'source' => 'Payment', | |
172 | 'is_override' => 1, | |
173 | 'status_id' => $membershipStatusID, | |
9099cab3 | 174 | ]; |
6a488035 | 175 | |
8cdb5f76 | 176 | $result = $this->callAPISuccess('membership', 'create', $params); |
6a488035 TO |
177 | $membershipID = $result['id']; |
178 | ||
9099cab3 | 179 | $params = [ |
6a488035 | 180 | 'id' => $membershipStatusID, |
9099cab3 | 181 | ]; |
d0e1eff2 | 182 | $result = $this->callAPIFailure('membership_status', 'delete', $params); |
6a488035 | 183 | |
9099cab3 | 184 | $this->callAPISuccess('Membership', 'Delete', [ |
6a488035 | 185 | 'id' => $membershipID, |
9099cab3 | 186 | ]); |
8cdb5f76 | 187 | $result = $this->callAPISuccess('membership_status', 'delete', $params); |
6a488035 | 188 | } |
96025800 | 189 | |
6a488035 | 190 | } |