ce60eef3179b17aa6f9b910032583ed9c62c44b3
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTypeTest.php
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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class api_v3_MembershipTypeTest
32 */
33 class api_v3_MembershipTypeTest extends CiviUnitTestCase {
34 protected $_contactID;
35 protected $_contributionTypeID;
36 protected $_apiversion;
37 protected $_entity = 'MembershipType';
38
39 public function setUp() {
40 parent::setUp();
41 $this->useTransaction(TRUE);
42 $this->_apiversion = 3;
43 $this->_contactID = $this->organizationCreate(NULL);
44 }
45
46 public function testGetWithoutId() {
47 $params = array(
48 'name' => '60+ Membership',
49 'description' => 'people above 60 are given health instructions',
50 'financial_type_id' => 1,
51 'minimum_fee' => '200',
52 'duration_unit' => 'month',
53 'duration_interval' => '10',
54 'visibility' => 'public',
55 );
56
57 $membershiptype = $this->callAPISuccess('membership_type', 'get', $params);
58 $this->assertEquals($membershiptype['count'], 0);
59 }
60
61 public function testGet() {
62 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
63
64 $params = array(
65 'id' => $id,
66 );
67 $membershiptype = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__, __FILE__);
68 $this->assertEquals($membershiptype['values'][$id]['name'], 'General');
69 $this->assertEquals($membershiptype['values'][$id]['member_of_contact_id'], $this->_contactID);
70 $this->assertEquals($membershiptype['values'][$id]['financial_type_id'], 1);
71 $this->assertEquals($membershiptype['values'][$id]['duration_unit'], 'year');
72 $this->assertEquals($membershiptype['values'][$id]['duration_interval'], '1');
73 $this->assertEquals($membershiptype['values'][$id]['period_type'], 'rolling');
74 $this->membershipTypeDelete($params);
75 }
76
77 /**
78 * Civicrm_membership_type_create methods.
79 */
80 public function testCreateWithEmptyParams() {
81 $membershiptype = $this->callAPIFailure('membership_type', 'create', array());
82 $this->assertEquals($membershiptype['error_message'],
83 'Mandatory key(s) missing from params array: domain_id, member_of_contact_id, financial_type_id, duration_unit, duration_interval, name'
84 );
85 }
86
87 public function testCreateWithoutMemberOfContactId() {
88 $params = array(
89 'name' => '60+ Membership',
90 'description' => 'people above 60 are given health instructions',
91 'financial_type_id' => 1,
92 'domain_id' => '1',
93 'minimum_fee' => '200',
94 'duration_unit' => 'month',
95 'duration_interval' => '10',
96 'period_type' => 'rolling',
97 'visibility' => 'public',
98 );
99
100 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params,
101 'Mandatory key(s) missing from params array: member_of_contact_id'
102 );
103 }
104
105 public function testCreateWithoutContributionTypeId() {
106 $params = array(
107 'name' => '70+ Membership',
108 'description' => 'people above 70 are given health instructions',
109 'member_of_contact_id' => $this->_contactID,
110 'domain_id' => '1',
111 'minimum_fee' => '200',
112 'duration_unit' => 'month',
113 'duration_interval' => '10',
114 'period_type' => 'rolling',
115 'visibility' => 'public',
116 );
117 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
118 $this->assertEquals($membershiptype['error_message'],
119 'Mandatory key(s) missing from params array: financial_type_id'
120 );
121 }
122
123 public function testCreateWithoutDurationUnit() {
124 $params = array(
125 'name' => '80+ Membership',
126 'description' => 'people above 80 are given health instructions',
127 'member_of_contact_id' => $this->_contactID,
128 'financial_type_id' => 1,
129 'domain_id' => '1',
130 'minimum_fee' => '200',
131 'duration_interval' => '10',
132 'visibility' => 'public',
133 );
134
135 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
136 $this->assertEquals($membershiptype['error_message'],
137 'Mandatory key(s) missing from params array: duration_unit'
138 );
139 }
140
141 public function testCreateWithoutDurationInterval() {
142 $params = array(
143 'name' => '70+ Membership',
144 'description' => 'people above 70 are given health instructions',
145 'member_of_contact_id' => $this->_contactID,
146 'domain_id' => '1',
147 'minimum_fee' => '200',
148 'duration_unit' => 'month',
149 'period_type' => 'rolling',
150 'visibility' => 'public',
151 );
152 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
153 $this->assertEquals($membershiptype['error_message'],
154 'Mandatory key(s) missing from params array: financial_type_id, duration_interval'
155 );
156 }
157
158 public function testCreateWithoutNameandDomainIDandDurationUnit() {
159 $params = array(
160 'description' => 'people above 50 are given health instructions',
161 'member_of_contact_id' => $this->_contactID,
162 'financial_type_id' => 1,
163 'minimum_fee' => '200',
164 'duration_interval' => '10',
165 'period_type' => 'rolling',
166 'visibility' => 'public',
167 );
168
169 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
170 $this->assertEquals($membershiptype['error_message'],
171 'Mandatory key(s) missing from params array: domain_id, duration_unit, name'
172 );
173 }
174
175 public function testCreateWithoutName() {
176 $params = array(
177 'description' => 'people above 50 are given health instructions',
178 'member_of_contact_id' => $this->_contactID,
179 'financial_type_id' => 1,
180 'domain_id' => '1',
181 'minimum_fee' => '200',
182 'duration_unit' => 'month',
183 'duration_interval' => '10',
184 'period_type' => 'rolling',
185 'visibility' => 'public',
186 );
187
188 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
189 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: name');
190 }
191
192 public function testCreate() {
193 $params = array(
194 'name' => '40+ Membership',
195 'description' => 'people above 40 are given health instructions',
196 'member_of_contact_id' => $this->_contactID,
197 'financial_type_id' => 1,
198 'domain_id' => '1',
199 'minimum_fee' => '200',
200 'duration_unit' => 'month',
201 'duration_interval' => '10',
202 'period_type' => 'rolling',
203 'visibility' => 'public',
204 );
205
206 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
207 $this->assertNotNull($membershipType['values']);
208 $this->membershipTypeDelete(array('id' => $membershipType['id']));
209 }
210
211
212 /**
213 * Test mandatory parameter check.
214 */
215 public function testUpdateWithEmptyParams() {
216 $this->callAPIFailure('membership_type', 'create', array());
217 }
218
219 /**
220 * Test update fails with no ID.
221 */
222 public function testUpdateWithoutId() {
223 $params = array(
224 'name' => '60+ Membership',
225 'description' => 'people above 60 are given health instructions',
226 'member_of_contact_id' => $this->_contactID,
227 'financial_type_id' => 1,
228 'minimum_fee' => '1200',
229 'duration_unit' => 'month',
230 'duration_interval' => '10',
231 'period_type' => 'rolling',
232 'visibility' => 'public',
233 );
234
235 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
236 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
237 }
238
239 public function testUpdate() {
240 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
241 $newMembOrgParams = array(
242 'organization_name' => 'New membership organisation',
243 'contact_type' => 'Organization',
244 'visibility' => 1,
245 );
246
247 // create a new contact to update this membership type to
248 $newMembOrgID = $this->organizationCreate($newMembOrgParams);
249
250 $params = array(
251 'id' => $id,
252 'name' => 'Updated General',
253 'member_of_contact_id' => $newMembOrgID,
254 'duration_unit' => 'month',
255 'duration_interval' => '10',
256 'period_type' => 'fixed',
257 'domain_id' => 1,
258 );
259
260 $this->callAPISuccess('membership_type', 'update', $params);
261
262 $this->getAndCheck($params, $id, $this->_entity);
263 }
264
265 /**
266 * Test for failure when id is not valid.
267 */
268 public function testDeleteNotExists() {
269 $params = array(
270 'id' => 'doesNotExist',
271 );
272 $this->callAPIFailure('membership_type', 'delete', $params,
273 'Error while deleting membership type. id : ' . $params['id']
274 );
275 }
276
277 public function testDelete() {
278 $orgID = $this->organizationCreate(NULL);
279 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
280 $params = array(
281 'id' => $membershipTypeID,
282 );
283
284 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
285 }
286
287 }