Merge pull request #4944 from civicrm/batch-5.part2
[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-2014 |
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', 'In line ' . __LINE__ . " id is " . $id);
69 $this->assertEquals($membershiptype['values'][$id]['member_of_contact_id'], $this->_contactID, 'In line ' . __LINE__);
70 $this->assertEquals($membershiptype['values'][$id]['financial_type_id'], 1, 'In line ' . __LINE__);
71 $this->assertEquals($membershiptype['values'][$id]['duration_unit'], 'year', 'In line ' . __LINE__);
72 $this->assertEquals($membershiptype['values'][$id]['duration_interval'], '1', 'In line ' . __LINE__);
73 $this->assertEquals($membershiptype['values'][$id]['period_type'], 'rolling', 'In line ' . __LINE__);
74 $this->membershipTypeDelete($params);
75 }
76
77 ///////////////// civicrm_membership_type_create methods
78 public function testCreateWithEmptyParams() {
79 $membershiptype = $this->callAPIFailure('membership_type', 'create', array());
80 $this->assertEquals($membershiptype['error_message'],
81 'Mandatory key(s) missing from params array: domain_id, member_of_contact_id, financial_type_id, duration_unit, duration_interval, name'
82 );
83 }
84
85 public function testCreateWithoutMemberOfContactId() {
86 $params = array(
87 'name' => '60+ Membership',
88 'description' => 'people above 60 are given health instructions',
89 'financial_type_id' => 1,
90 'domain_id' => '1',
91 'minimum_fee' => '200',
92 'duration_unit' => 'month',
93 'duration_interval' => '10',
94 'period_type' => 'rolling',
95 'visibility' => 'public',
96 );
97
98 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params,
99 'Mandatory key(s) missing from params array: member_of_contact_id'
100 );
101 }
102
103 public function testCreateWithoutContributionTypeId() {
104 $params = array(
105 'name' => '70+ Membership',
106 'description' => 'people above 70 are given health instructions',
107 'member_of_contact_id' => $this->_contactID,
108 'domain_id' => '1',
109 'minimum_fee' => '200',
110 'duration_unit' => 'month',
111 'duration_interval' => '10',
112 'period_type' => 'rolling',
113 'visibility' => 'public',
114 );
115 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
116 $this->assertEquals($membershiptype['error_message'],
117 'Mandatory key(s) missing from params array: financial_type_id'
118 );
119 }
120
121 public function testCreateWithoutDurationUnit() {
122 $params = array(
123 'name' => '80+ Membership',
124 'description' => 'people above 80 are given health instructions',
125 'member_of_contact_id' => $this->_contactID,
126 'financial_type_id' => 1,
127 'domain_id' => '1',
128 'minimum_fee' => '200',
129 'duration_interval' => '10',
130 'visibility' => 'public',
131 );
132
133 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
134 $this->assertEquals($membershiptype['error_message'],
135 'Mandatory key(s) missing from params array: duration_unit'
136 );
137 }
138
139 public function testCreateWithoutDurationInterval() {
140 $params = array(
141 'name' => '70+ Membership',
142 'description' => 'people above 70 are given health instructions',
143 'member_of_contact_id' => $this->_contactID,
144 'domain_id' => '1',
145 'minimum_fee' => '200',
146 'duration_unit' => 'month',
147 'period_type' => 'rolling',
148 'visibility' => 'public',
149 );
150 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
151 $this->assertEquals($membershiptype['error_message'],
152 'Mandatory key(s) missing from params array: financial_type_id, duration_interval'
153 );
154 }
155
156 public function testCreateWithoutNameandDomainIDandDurationUnit() {
157 $params = array(
158 'description' => 'people above 50 are given health instructions',
159 'member_of_contact_id' => $this->_contactID,
160 'financial_type_id' => 1,
161 'minimum_fee' => '200',
162 'duration_interval' => '10',
163 'period_type' => 'rolling',
164 'visibility' => 'public',
165 );
166
167 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
168 $this->assertEquals($membershiptype['error_message'],
169 'Mandatory key(s) missing from params array: domain_id, duration_unit, name'
170 );
171 }
172
173 public function testCreateWithoutName() {
174 $params = array(
175 'description' => 'people above 50 are given health instructions',
176 'member_of_contact_id' => $this->_contactID,
177 'financial_type_id' => 1,
178 'domain_id' => '1',
179 'minimum_fee' => '200',
180 'duration_unit' => 'month',
181 'duration_interval' => '10',
182 'period_type' => 'rolling',
183 'visibility' => 'public',
184 );
185
186 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
187 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: name');
188 }
189
190 public function testCreate() {
191 $params = array(
192 'name' => '40+ Membership',
193 'description' => 'people above 40 are given health instructions',
194 'member_of_contact_id' => $this->_contactID,
195 'financial_type_id' => 1,
196 'domain_id' => '1',
197 'minimum_fee' => '200',
198 'duration_unit' => 'month',
199 'duration_interval' => '10',
200 'period_type' => 'rolling',
201 'visibility' => 'public',
202 );
203
204 $membershiptype = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
205 $this->assertNotNull($membershiptype['values']);
206 $this->membershipTypeDelete(array('id' => $membershiptype['id']));
207 }
208
209
210 public function testUpdateWithEmptyParams() {
211 $params = array();
212 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
213 $this->assertEquals($membershiptype['error_message'],
214 'Mandatory key(s) missing from params array: domain_id, member_of_contact_id, financial_type_id, duration_unit, duration_interval, name'
215 );
216 }
217
218 public function testUpdateWithoutId() {
219 $params = array(
220 'name' => '60+ Membership',
221 'description' => 'people above 60 are given health instructions',
222 'member_of_contact_id' => $this->_contactID,
223 'financial_type_id' => 1,
224 'minimum_fee' => '1200',
225 'duration_unit' => 'month',
226 'duration_interval' => '10',
227 'period_type' => 'rolling',
228 'visibility' => 'public',
229 );
230
231 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
232 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: domain_id');
233 }
234
235 public function testUpdate() {
236 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
237 $newMembOrgParams = array(
238 'organization_name' => 'New membership organisation',
239 'contact_type' => 'Organization',
240 'visibility' => 1,
241 );
242
243 // create a new contact to update this membership type to
244 $newMembOrgID = $this->organizationCreate($newMembOrgParams);
245
246 $params = array(
247 'id' => $id,
248 'name' => 'Updated General',
249 'member_of_contact_id' => $newMembOrgID,
250 'duration_unit' => 'month',
251 'duration_interval' => '10',
252 'period_type' => 'fixed',
253 'domain_id' => 1,
254 );
255
256 $this->callAPISuccess('membership_type', 'update', $params);
257
258 $this->getAndCheck($params, $id, $this->_entity);
259 }
260
261 public function testDeleteNotExists() {
262 $params = array(
263 'id' => 'doesNotExist',
264 );
265 $membershiptype = $this->callAPIFailure('membership_type', 'delete', $params,
266 'Error while deleting membership type. id : ' . $params['id']
267 );
268 }
269
270 public function testDelete() {
271 $orgID = $this->organizationCreate(NULL);
272 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
273 $params = array(
274 'id' => $membershipTypeID,
275 );
276
277 $result = $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
278 }
279 }