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
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
06a1bc01 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06a1bc01 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
29require_once 'CiviTest/CiviUnitTestCase.php';
e9479dcf
EM
30
31/**
32 * Class api_v3_MembershipTypeTest
33 */
6a488035
TO
34class api_v3_MembershipTypeTest extends CiviUnitTestCase {
35 protected $_contactID;
36 protected $_contributionTypeID;
37 protected $_apiversion;
38 protected $_entity = 'MembershipType';
b7c9bc4c 39
6a488035 40
4cbe18b8
EM
41 /**
42 * @return array
43 */
6a488035
TO
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
6a488035
TO
63 function testGetWithoutId() {
64 $params = array(
65 'name' => '60+ Membership',
66 'description' => 'people above 60 are given health instructions',
3d6a42e8 67 'financial_type_id' => 1,
6a488035
TO
68 'minimum_fee' => '200',
69 'duration_unit' => 'month',
70 'duration_interval' => '10',
71 'visibility' => 'public',
6a488035
TO
72 );
73
d63167fc 74 $membershiptype = $this->callAPISuccess('membership_type', 'get', $params);
6a488035
TO
75 $this->assertEquals($membershiptype['count'], 0);
76 }
77
78 function testGet() {
75638074 79 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
6a488035
TO
80
81 $params = array(
82 'id' => $id,
6a488035 83 );
d63167fc 84 $membershiptype = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
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__);
3d6a42e8 87 $this->assertEquals($membershiptype['values'][$id]['financial_type_id'], 1, 'In line ' . __LINE__);
6a488035
TO
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() {
d0e1eff2 96 $membershiptype = $this->callAPIFailure('membership_type', 'create', array());
6a488035
TO
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
6a488035
TO
102 function testCreateWithoutMemberOfContactId() {
103 $params = array(
104 'name' => '60+ Membership',
105 'description' => 'people above 60 are given health instructions',
3d6a42e8 106 'financial_type_id' => 1,
6a488035
TO
107 'domain_id' => '1',
108 'minimum_fee' => '200',
109 'duration_unit' => 'month',
110 'duration_interval' => '10',
111 'period_type' => 'rolling',
112 'visibility' => 'public',
6a488035
TO
113 );
114
d63167fc 115 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params,
116 'Mandatory key(s) missing from params array: member_of_contact_id'
117 );
6a488035
TO
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',
d63167fc 130 'visibility' => 'public', );
d0e1eff2 131 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
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,
3d6a42e8 142 'financial_type_id' => 1,
6a488035
TO
143 'domain_id' => '1',
144 'minimum_fee' => '200',
145 'duration_interval' => '10',
d63167fc 146 'visibility' => 'public', );
6a488035 147
d0e1eff2 148 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
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',
d63167fc 163 'visibility' => 'public', );
d0e1eff2 164 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
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,
3d6a42e8 174 'financial_type_id' => 1,
6a488035
TO
175 'minimum_fee' => '200',
176 'duration_interval' => '10',
177 'period_type' => 'rolling',
d63167fc 178 'visibility' => 'public', );
6a488035 179
d0e1eff2 180 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
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,
3d6a42e8 190 'financial_type_id' => 1,
6a488035
TO
191 'domain_id' => '1',
192 'minimum_fee' => '200',
193 'duration_unit' => 'month',
194 'duration_interval' => '10',
195 'period_type' => 'rolling',
d63167fc 196 'visibility' => 'public', );
6a488035 197
d0e1eff2 198 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
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,
3d6a42e8 207 'financial_type_id' => 1,
6a488035
TO
208 'domain_id' => '1',
209 'minimum_fee' => '200',
210 'duration_unit' => 'month',
211 'duration_interval' => '10',
212 'period_type' => 'rolling',
213 'visibility' => 'public',
6a488035
TO
214 );
215
d63167fc 216 $membershiptype = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
217 $this->assertNotNull($membershiptype['values']);
218 $this->membershipTypeDelete(array('id' => $membershiptype['id']));
219 }
220
6a488035
TO
221
222 function testUpdateWithEmptyParams() {
223 $params = array();
d0e1eff2 224 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035 225 $this->assertEquals($membershiptype['error_message'],
d0e1eff2 226 'Mandatory key(s) missing from params array: domain_id, member_of_contact_id, financial_type_id, duration_unit, duration_interval, name'
6a488035
TO
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,
3d6a42e8 235 'financial_type_id' => 1,
6a488035
TO
236 'minimum_fee' => '1200',
237 'duration_unit' => 'month',
238 'duration_interval' => '10',
239 'period_type' => 'rolling',
d63167fc 240 'visibility' => 'public', );
6a488035 241
d0e1eff2 242 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
243 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: domain_id');
244 }
245
246 function testUpdate() {
75638074 247 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
6a488035
TO
248 $newMembOrgParams = array(
249 'organization_name' => 'New membership organisation',
250 'contact_type' => 'Organization',
d63167fc 251 'visibility' => 1, );
6a488035
TO
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',
d63167fc 263 'domain_id' => 1, );
3d6a42e8 264
d63167fc 265 $this->callAPISuccess('membership_type', 'update', $params);
6a488035
TO
266
267 $this->getAndCheck($params, $id, $this->_entity);
268 }
269
6a488035
TO
270 function testDeleteNotExists() {
271 $params = array(
272 'id' => 'doesNotExist',
6a488035 273 );
d63167fc 274 $membershiptype = $this->callAPIFailure('membership_type', 'delete', $params,
275 'Error while deleting membership type. id : ' . $params['id']
276 );
6a488035
TO
277 }
278
279 function testDelete() {
3d6a42e8 280 $orgID = $this->organizationCreate(NULL);
75638074 281 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
3d6a42e8 282 $params = array(
6a488035 283 'id' => $membershipTypeID,
6a488035
TO
284 );
285
d63167fc 286 $result = $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
287 }
288}
289