Merge pull request #4820 from kurund/CRM-15705
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTypeTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
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 public function setUp() {
41 parent::setUp();
42 $this->useTransaction(TRUE);
43 $this->_apiversion = 3;
44 $this->_contactID = $this->organizationCreate(NULL);
45 }
46
47 public function testGetWithoutId() {
48 $params = array(
49 'name' => '60+ Membership',
50 'description' => 'people above 60 are given health instructions',
51 'financial_type_id' => 1,
52 'minimum_fee' => '200',
53 'duration_unit' => 'month',
54 'duration_interval' => '10',
55 'visibility' => 'public',
56 );
57
58 $membershiptype = $this->callAPISuccess('membership_type', 'get', $params);
59 $this->assertEquals($membershiptype['count'], 0);
60 }
61
62 public function testGet() {
63 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
64
65 $params = array(
66 'id' => $id,
67 );
68 $membershiptype = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__, __FILE__);
69 $this->assertEquals($membershiptype['values'][$id]['name'], 'General', 'In line ' . __LINE__ . " id is " . $id);
70 $this->assertEquals($membershiptype['values'][$id]['member_of_contact_id'], $this->_contactID, 'In line ' . __LINE__);
71 $this->assertEquals($membershiptype['values'][$id]['financial_type_id'], 1, 'In line ' . __LINE__);
72 $this->assertEquals($membershiptype['values'][$id]['duration_unit'], 'year', 'In line ' . __LINE__);
73 $this->assertEquals($membershiptype['values'][$id]['duration_interval'], '1', 'In line ' . __LINE__);
74 $this->assertEquals($membershiptype['values'][$id]['period_type'], 'rolling', 'In line ' . __LINE__);
75 $this->membershipTypeDelete($params);
76 }
77
78 ///////////////// civicrm_membership_type_create methods
79 public function testCreateWithEmptyParams() {
80 $membershiptype = $this->callAPIFailure('membership_type', 'create', array());
81 $this->assertEquals($membershiptype['error_message'],
82 'Mandatory key(s) missing from params array: domain_id, member_of_contact_id, financial_type_id, duration_unit, duration_interval, name'
83 );
84 }
85
86 public function testCreateWithoutMemberOfContactId() {
87 $params = array(
88 'name' => '60+ Membership',
89 'description' => 'people above 60 are given health instructions',
90 'financial_type_id' => 1,
91 'domain_id' => '1',
92 'minimum_fee' => '200',
93 'duration_unit' => 'month',
94 'duration_interval' => '10',
95 'period_type' => 'rolling',
96 'visibility' => 'public',
97 );
98
99 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params,
100 'Mandatory key(s) missing from params array: member_of_contact_id'
101 );
102 }
103
104 public function testCreateWithoutContributionTypeId() {
105 $params = array(
106 'name' => '70+ Membership',
107 'description' => 'people above 70 are given health instructions',
108 'member_of_contact_id' => $this->_contactID,
109 'domain_id' => '1',
110 'minimum_fee' => '200',
111 'duration_unit' => 'month',
112 'duration_interval' => '10',
113 'period_type' => 'rolling',
114 'visibility' => 'public', );
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 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
133 $this->assertEquals($membershiptype['error_message'],
134 'Mandatory key(s) missing from params array: duration_unit'
135 );
136 }
137
138 public function testCreateWithoutDurationInterval() {
139 $params = array(
140 'name' => '70+ Membership',
141 'description' => 'people above 70 are given health instructions',
142 'member_of_contact_id' => $this->_contactID,
143 'domain_id' => '1',
144 'minimum_fee' => '200',
145 'duration_unit' => 'month',
146 'period_type' => 'rolling',
147 'visibility' => 'public', );
148 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
149 $this->assertEquals($membershiptype['error_message'],
150 'Mandatory key(s) missing from params array: financial_type_id, duration_interval'
151 );
152 }
153
154 public function testCreateWithoutNameandDomainIDandDurationUnit() {
155 $params = array(
156 'description' => 'people above 50 are given health instructions',
157 'member_of_contact_id' => $this->_contactID,
158 'financial_type_id' => 1,
159 'minimum_fee' => '200',
160 'duration_interval' => '10',
161 'period_type' => 'rolling',
162 'visibility' => 'public', );
163
164 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
165 $this->assertEquals($membershiptype['error_message'],
166 'Mandatory key(s) missing from params array: domain_id, duration_unit, name'
167 );
168 }
169
170 public function testCreateWithoutName() {
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 'domain_id' => '1',
176 'minimum_fee' => '200',
177 'duration_unit' => 'month',
178 'duration_interval' => '10',
179 'period_type' => 'rolling',
180 'visibility' => 'public', );
181
182 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
183 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: name');
184 }
185
186 public function testCreate() {
187 $params = array(
188 'name' => '40+ Membership',
189 'description' => 'people above 40 are given health instructions',
190 'member_of_contact_id' => $this->_contactID,
191 'financial_type_id' => 1,
192 'domain_id' => '1',
193 'minimum_fee' => '200',
194 'duration_unit' => 'month',
195 'duration_interval' => '10',
196 'period_type' => 'rolling',
197 'visibility' => 'public',
198 );
199
200 $membershiptype = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
201 $this->assertNotNull($membershiptype['values']);
202 $this->membershipTypeDelete(array('id' => $membershiptype['id']));
203 }
204
205
206 public function testUpdateWithEmptyParams() {
207 $params = array();
208 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
209 $this->assertEquals($membershiptype['error_message'],
210 'Mandatory key(s) missing from params array: domain_id, member_of_contact_id, financial_type_id, duration_unit, duration_interval, name'
211 );
212 }
213
214 public function testUpdateWithoutId() {
215 $params = array(
216 'name' => '60+ Membership',
217 'description' => 'people above 60 are given health instructions',
218 'member_of_contact_id' => $this->_contactID,
219 'financial_type_id' => 1,
220 'minimum_fee' => '1200',
221 'duration_unit' => 'month',
222 'duration_interval' => '10',
223 'period_type' => 'rolling',
224 'visibility' => 'public', );
225
226 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
227 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: domain_id');
228 }
229
230 public function testUpdate() {
231 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
232 $newMembOrgParams = array(
233 'organization_name' => 'New membership organisation',
234 'contact_type' => 'Organization',
235 'visibility' => 1, );
236
237 // create a new contact to update this membership type to
238 $newMembOrgID = $this->organizationCreate($newMembOrgParams);
239
240 $params = array(
241 'id' => $id,
242 'name' => 'Updated General',
243 'member_of_contact_id' => $newMembOrgID,
244 'duration_unit' => 'month',
245 'duration_interval' => '10',
246 'period_type' => 'fixed',
247 'domain_id' => 1, );
248
249 $this->callAPISuccess('membership_type', 'update', $params);
250
251 $this->getAndCheck($params, $id, $this->_entity);
252 }
253
254 public function testDeleteNotExists() {
255 $params = array(
256 'id' => 'doesNotExist',
257 );
258 $membershiptype = $this->callAPIFailure('membership_type', 'delete', $params,
259 'Error while deleting membership type. id : ' . $params['id']
260 );
261 }
262
263 public function testDelete() {
264 $orgID = $this->organizationCreate(NULL);
265 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
266 $params = array(
267 'id' => $membershipTypeID,
268 );
269
270 $result = $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
271 }
272 }