Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-16-12-52-48
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTypeTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 require_once 'CiviTest/CiviUnitTestCase.php';
30
31 /**
32 * Class api_v3_MembershipTypeTest
33 */
34 class api_v3_MembershipTypeTest extends CiviUnitTestCase {
35 protected $_contactID;
36 protected $_contributionTypeID;
37 protected $_apiversion;
38 protected $_entity = 'MembershipType';
39
40
41 /**
42 * @return array
43 */
44 function get_info() {
45 return array(
46 'name' => 'MembershipType Create',
47 'description' => 'Test all Membership Type Create API methods.',
48 'group' => 'CiviCRM API Tests',
49 );
50 }
51
52 function setUp() {
53 parent::setUp();
54 $this->_apiversion = 3;
55 $this->_contactID = $this->organizationCreate(NULL);
56 }
57
58 function tearDown() {
59 $tablesToTruncate = array('civicrm_contact');
60 $this->quickCleanup($tablesToTruncate);
61 }
62
63 function testGetWithoutId() {
64 $params = array(
65 'name' => '60+ Membership',
66 'description' => 'people above 60 are given health instructions',
67 'financial_type_id' => 1,
68 'minimum_fee' => '200',
69 'duration_unit' => 'month',
70 'duration_interval' => '10',
71 'visibility' => 'public',
72 );
73
74 $membershiptype = $this->callAPISuccess('membership_type', 'get', $params);
75 $this->assertEquals($membershiptype['count'], 0);
76 }
77
78 function testGet() {
79 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
80
81 $params = array(
82 'id' => $id,
83 );
84 $membershiptype = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__, __FILE__);
85 $this->assertEquals($membershiptype['values'][$id]['name'], 'General', 'In line ' . __LINE__ . " id is " . $id);
86 $this->assertEquals($membershiptype['values'][$id]['member_of_contact_id'], $this->_contactID, 'In line ' . __LINE__);
87 $this->assertEquals($membershiptype['values'][$id]['financial_type_id'], 1, 'In line ' . __LINE__);
88 $this->assertEquals($membershiptype['values'][$id]['duration_unit'], 'year', 'In line ' . __LINE__);
89 $this->assertEquals($membershiptype['values'][$id]['duration_interval'], '1', 'In line ' . __LINE__);
90 $this->assertEquals($membershiptype['values'][$id]['period_type'], 'rolling', 'In line ' . __LINE__);
91 $this->membershipTypeDelete($params);
92 }
93
94 ///////////////// civicrm_membership_type_create methods
95 function testCreateWithEmptyParams() {
96 $membershiptype = $this->callAPIFailure('membership_type', 'create', array());
97 $this->assertEquals($membershiptype['error_message'],
98 'Mandatory key(s) missing from params array: domain_id, member_of_contact_id, financial_type_id, duration_unit, duration_interval, name'
99 );
100 }
101
102 function testCreateWithoutMemberOfContactId() {
103 $params = array(
104 'name' => '60+ Membership',
105 'description' => 'people above 60 are given health instructions',
106 'financial_type_id' => 1,
107 'domain_id' => '1',
108 'minimum_fee' => '200',
109 'duration_unit' => 'month',
110 'duration_interval' => '10',
111 'period_type' => 'rolling',
112 'visibility' => 'public',
113 );
114
115 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params,
116 'Mandatory key(s) missing from params array: member_of_contact_id'
117 );
118 }
119
120 function testCreateWithoutContributionTypeId() {
121 $params = array(
122 'name' => '70+ Membership',
123 'description' => 'people above 70 are given health instructions',
124 'member_of_contact_id' => $this->_contactID,
125 'domain_id' => '1',
126 'minimum_fee' => '200',
127 'duration_unit' => 'month',
128 'duration_interval' => '10',
129 'period_type' => 'rolling',
130 'visibility' => 'public', );
131 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
132 $this->assertEquals($membershiptype['error_message'],
133 'Mandatory key(s) missing from params array: financial_type_id'
134 );
135 }
136
137 function testCreateWithoutDurationUnit() {
138 $params = array(
139 'name' => '80+ Membership',
140 'description' => 'people above 80 are given health instructions',
141 'member_of_contact_id' => $this->_contactID,
142 'financial_type_id' => 1,
143 'domain_id' => '1',
144 'minimum_fee' => '200',
145 'duration_interval' => '10',
146 'visibility' => 'public', );
147
148 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
149 $this->assertEquals($membershiptype['error_message'],
150 'Mandatory key(s) missing from params array: duration_unit'
151 );
152 }
153
154 function testCreateWithoutDurationInterval() {
155 $params = array(
156 'name' => '70+ Membership',
157 'description' => 'people above 70 are given health instructions',
158 'member_of_contact_id' => $this->_contactID,
159 'domain_id' => '1',
160 'minimum_fee' => '200',
161 'duration_unit' => 'month',
162 'period_type' => 'rolling',
163 'visibility' => 'public', );
164 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
165 $this->assertEquals($membershiptype['error_message'],
166 'Mandatory key(s) missing from params array: financial_type_id, duration_interval'
167 );
168 }
169
170 function testCreateWithoutNameandDomainIDandDurationUnit() {
171 $params = array(
172 'description' => 'people above 50 are given health instructions',
173 'member_of_contact_id' => $this->_contactID,
174 'financial_type_id' => 1,
175 'minimum_fee' => '200',
176 'duration_interval' => '10',
177 'period_type' => 'rolling',
178 'visibility' => 'public', );
179
180 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
181 $this->assertEquals($membershiptype['error_message'],
182 'Mandatory key(s) missing from params array: domain_id, duration_unit, name'
183 );
184 }
185
186 function testCreateWithoutName() {
187 $params = array(
188 'description' => 'people above 50 are given health instructions',
189 'member_of_contact_id' => $this->_contactID,
190 'financial_type_id' => 1,
191 'domain_id' => '1',
192 'minimum_fee' => '200',
193 'duration_unit' => 'month',
194 'duration_interval' => '10',
195 'period_type' => 'rolling',
196 'visibility' => 'public', );
197
198 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
199 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: name');
200 }
201
202 function testCreate() {
203 $params = array(
204 'name' => '40+ Membership',
205 'description' => 'people above 40 are given health instructions',
206 'member_of_contact_id' => $this->_contactID,
207 'financial_type_id' => 1,
208 'domain_id' => '1',
209 'minimum_fee' => '200',
210 'duration_unit' => 'month',
211 'duration_interval' => '10',
212 'period_type' => 'rolling',
213 'visibility' => 'public',
214 );
215
216 $membershiptype = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
217 $this->assertNotNull($membershiptype['values']);
218 $this->membershipTypeDelete(array('id' => $membershiptype['id']));
219 }
220
221
222 function testUpdateWithEmptyParams() {
223 $params = array();
224 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
225 $this->assertEquals($membershiptype['error_message'],
226 'Mandatory key(s) missing from params array: domain_id, member_of_contact_id, financial_type_id, duration_unit, duration_interval, name'
227 );
228 }
229
230 function testUpdateWithoutId() {
231 $params = array(
232 'name' => '60+ Membership',
233 'description' => 'people above 60 are given health instructions',
234 'member_of_contact_id' => $this->_contactID,
235 'financial_type_id' => 1,
236 'minimum_fee' => '1200',
237 'duration_unit' => 'month',
238 'duration_interval' => '10',
239 'period_type' => 'rolling',
240 'visibility' => 'public', );
241
242 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
243 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: domain_id');
244 }
245
246 function testUpdate() {
247 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
248 $newMembOrgParams = array(
249 'organization_name' => 'New membership organisation',
250 'contact_type' => 'Organization',
251 'visibility' => 1, );
252
253 // create a new contact to update this membership type to
254 $newMembOrgID = $this->organizationCreate($newMembOrgParams);
255
256 $params = array(
257 'id' => $id,
258 'name' => 'Updated General',
259 'member_of_contact_id' => $newMembOrgID,
260 'duration_unit' => 'month',
261 'duration_interval' => '10',
262 'period_type' => 'fixed',
263 'domain_id' => 1, );
264
265 $this->callAPISuccess('membership_type', 'update', $params);
266
267 $this->getAndCheck($params, $id, $this->_entity);
268 }
269
270 function testDeleteNotExists() {
271 $params = array(
272 'id' => 'doesNotExist',
273 );
274 $membershiptype = $this->callAPIFailure('membership_type', 'delete', $params,
275 'Error while deleting membership type. id : ' . $params['id']
276 );
277 }
278
279 function testDelete() {
280 $orgID = $this->organizationCreate(NULL);
281 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
282 $params = array(
283 'id' => $membershipTypeID,
284 );
285
286 $result = $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
287 }
288 }
289