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 CW |
138 | |
139 | /** | |
eceb18cc | 140 | * Test that trying to delete membership status while membership still exists creates error. |
c490a46a | 141 | */ |
00be9182 | 142 | public function testDeleteWithMembershipError() { |
6a488035 TO |
143 | $membershipStatusID = $this->membershipStatusCreate(); |
144 | $this->_contactID = $this->individualCreate(); | |
145 | $this->_entity = 'membership'; | |
9099cab3 | 146 | $params = [ |
6a488035 TO |
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, | |
9099cab3 | 155 | ]; |
6a488035 | 156 | |
8cdb5f76 | 157 | $result = $this->callAPISuccess('membership', 'create', $params); |
6a488035 TO |
158 | $membershipID = $result['id']; |
159 | ||
9099cab3 | 160 | $params = [ |
6a488035 | 161 | 'id' => $membershipStatusID, |
9099cab3 | 162 | ]; |
d0e1eff2 | 163 | $result = $this->callAPIFailure('membership_status', 'delete', $params); |
6a488035 | 164 | |
9099cab3 | 165 | $this->callAPISuccess('Membership', 'Delete', [ |
6a488035 | 166 | 'id' => $membershipID, |
9099cab3 | 167 | ]); |
8cdb5f76 | 168 | $result = $this->callAPISuccess('membership_status', 'delete', $params); |
6a488035 | 169 | } |
96025800 | 170 | |
6a488035 | 171 | } |