INFRA-132 - Copyright header - Drupal.WhiteSpace.ScopeIndent.IncorrectExact
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTypeTest.php
... / ...
CommitLineData
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Class api_v3_MembershipTypeTest
32 */
33class 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 /**
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 public function testUpdateWithEmptyParams() {
213 $params = array();
214 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
215 $this->assertEquals($membershiptype['error_message'],
216 'Mandatory key(s) missing from params array: domain_id, member_of_contact_id, financial_type_id, duration_unit, duration_interval, name'
217 );
218 }
219
220 public function testUpdateWithoutId() {
221 $params = array(
222 'name' => '60+ Membership',
223 'description' => 'people above 60 are given health instructions',
224 'member_of_contact_id' => $this->_contactID,
225 'financial_type_id' => 1,
226 'minimum_fee' => '1200',
227 'duration_unit' => 'month',
228 'duration_interval' => '10',
229 'period_type' => 'rolling',
230 'visibility' => 'public',
231 );
232
233 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
234 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: domain_id');
235 }
236
237 public function testUpdate() {
238 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
239 $newMembOrgParams = array(
240 'organization_name' => 'New membership organisation',
241 'contact_type' => 'Organization',
242 'visibility' => 1,
243 );
244
245 // create a new contact to update this membership type to
246 $newMembOrgID = $this->organizationCreate($newMembOrgParams);
247
248 $params = array(
249 'id' => $id,
250 'name' => 'Updated General',
251 'member_of_contact_id' => $newMembOrgID,
252 'duration_unit' => 'month',
253 'duration_interval' => '10',
254 'period_type' => 'fixed',
255 'domain_id' => 1,
256 );
257
258 $this->callAPISuccess('membership_type', 'update', $params);
259
260 $this->getAndCheck($params, $id, $this->_entity);
261 }
262
263 public function testDeleteNotExists() {
264 $params = array(
265 'id' => 'doesNotExist',
266 );
267 $membershiptype = $this->callAPIFailure('membership_type', 'delete', $params,
268 'Error while deleting membership type. id : ' . $params['id']
269 );
270 }
271
272 public function testDelete() {
273 $orgID = $this->organizationCreate(NULL);
274 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
275 $params = array(
276 'id' => $membershipTypeID,
277 );
278
279 $result = $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
280 }
281
282}