Merge pull request #5010 from eileenmcnaughton/rebase2
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTypeTest.php
CommitLineData
6a488035 1<?php
6a488035 2/*
8d7a9d07
CB
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 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
e9479dcf
EM
29
30/**
31 * Class api_v3_MembershipTypeTest
32 */
6a488035
TO
33class api_v3_MembershipTypeTest extends CiviUnitTestCase {
34 protected $_contactID;
35 protected $_contributionTypeID;
36 protected $_apiversion;
37 protected $_entity = 'MembershipType';
b7c9bc4c 38
00be9182 39 public function setUp() {
6a488035 40 parent::setUp();
31c45547 41 $this->useTransaction(TRUE);
6a488035
TO
42 $this->_apiversion = 3;
43 $this->_contactID = $this->organizationCreate(NULL);
44 }
45
00be9182 46 public function testGetWithoutId() {
6a488035
TO
47 $params = array(
48 'name' => '60+ Membership',
49 'description' => 'people above 60 are given health instructions',
3d6a42e8 50 'financial_type_id' => 1,
6a488035
TO
51 'minimum_fee' => '200',
52 'duration_unit' => 'month',
53 'duration_interval' => '10',
54 'visibility' => 'public',
6a488035
TO
55 );
56
d63167fc 57 $membershiptype = $this->callAPISuccess('membership_type', 'get', $params);
6a488035
TO
58 $this->assertEquals($membershiptype['count'], 0);
59 }
60
00be9182 61 public function testGet() {
75638074 62 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
6a488035
TO
63
64 $params = array(
65 'id' => $id,
6a488035 66 );
d63167fc 67 $membershiptype = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
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__);
3d6a42e8 70 $this->assertEquals($membershiptype['values'][$id]['financial_type_id'], 1, 'In line ' . __LINE__);
6a488035
TO
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
8d7a9d07
CB
77 /**
78 * Civicrm_membership_type_create methods.
79 */
00be9182 80 public function testCreateWithEmptyParams() {
d0e1eff2 81 $membershiptype = $this->callAPIFailure('membership_type', 'create', array());
6a488035
TO
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'
8d7a9d07 84 );
6a488035
TO
85 }
86
00be9182 87 public function testCreateWithoutMemberOfContactId() {
6a488035
TO
88 $params = array(
89 'name' => '60+ Membership',
90 'description' => 'people above 60 are given health instructions',
3d6a42e8 91 'financial_type_id' => 1,
6a488035
TO
92 'domain_id' => '1',
93 'minimum_fee' => '200',
94 'duration_unit' => 'month',
95 'duration_interval' => '10',
96 'period_type' => 'rolling',
97 'visibility' => 'public',
6a488035
TO
98 );
99
d63167fc 100 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params,
8d7a9d07
CB
101 'Mandatory key(s) missing from params array: member_of_contact_id'
102 );
6a488035
TO
103 }
104
00be9182 105 public function testCreateWithoutContributionTypeId() {
6a488035
TO
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',
92915c55
TO
115 'visibility' => 'public',
116 );
d0e1eff2 117 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
118 $this->assertEquals($membershiptype['error_message'],
119 'Mandatory key(s) missing from params array: financial_type_id'
8d7a9d07 120 );
6a488035
TO
121 }
122
00be9182 123 public function testCreateWithoutDurationUnit() {
6a488035
TO
124 $params = array(
125 'name' => '80+ Membership',
126 'description' => 'people above 80 are given health instructions',
127 'member_of_contact_id' => $this->_contactID,
3d6a42e8 128 'financial_type_id' => 1,
6a488035
TO
129 'domain_id' => '1',
130 'minimum_fee' => '200',
131 'duration_interval' => '10',
92915c55
TO
132 'visibility' => 'public',
133 );
6a488035 134
d0e1eff2 135 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
136 $this->assertEquals($membershiptype['error_message'],
137 'Mandatory key(s) missing from params array: duration_unit'
8d7a9d07 138 );
6a488035
TO
139 }
140
00be9182 141 public function testCreateWithoutDurationInterval() {
6a488035
TO
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',
92915c55
TO
150 'visibility' => 'public',
151 );
d0e1eff2 152 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
153 $this->assertEquals($membershiptype['error_message'],
154 'Mandatory key(s) missing from params array: financial_type_id, duration_interval'
8d7a9d07 155 );
6a488035
TO
156 }
157
00be9182 158 public function testCreateWithoutNameandDomainIDandDurationUnit() {
6a488035
TO
159 $params = array(
160 'description' => 'people above 50 are given health instructions',
161 'member_of_contact_id' => $this->_contactID,
3d6a42e8 162 'financial_type_id' => 1,
6a488035
TO
163 'minimum_fee' => '200',
164 'duration_interval' => '10',
165 'period_type' => 'rolling',
92915c55
TO
166 'visibility' => 'public',
167 );
6a488035 168
d0e1eff2 169 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
170 $this->assertEquals($membershiptype['error_message'],
171 'Mandatory key(s) missing from params array: domain_id, duration_unit, name'
8d7a9d07 172 );
6a488035
TO
173 }
174
00be9182 175 public function testCreateWithoutName() {
6a488035
TO
176 $params = array(
177 'description' => 'people above 50 are given health instructions',
178 'member_of_contact_id' => $this->_contactID,
3d6a42e8 179 'financial_type_id' => 1,
6a488035
TO
180 'domain_id' => '1',
181 'minimum_fee' => '200',
182 'duration_unit' => 'month',
183 'duration_interval' => '10',
184 'period_type' => 'rolling',
92915c55
TO
185 'visibility' => 'public',
186 );
6a488035 187
d0e1eff2 188 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
6a488035
TO
189 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: name');
190 }
191
00be9182 192 public function testCreate() {
6a488035
TO
193 $params = array(
194 'name' => '40+ Membership',
195 'description' => 'people above 40 are given health instructions',
196 'member_of_contact_id' => $this->_contactID,
3d6a42e8 197 'financial_type_id' => 1,
6a488035
TO
198 'domain_id' => '1',
199 'minimum_fee' => '200',
200 'duration_unit' => 'month',
201 'duration_interval' => '10',
202 'period_type' => 'rolling',
203 'visibility' => 'public',
6a488035
TO
204 );
205
edc9b94e
EM
206 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
207 $this->assertNotNull($membershipType['values']);
208 $this->membershipTypeDelete(array('id' => $membershipType['id']));
6a488035
TO
209 }
210
6a488035 211
edc9b94e
EM
212 /**
213 * Test mandatory parameter check.
214 */
00be9182 215 public function testUpdateWithEmptyParams() {
edc9b94e 216 $this->callAPIFailure('membership_type', 'create', array());
6a488035
TO
217 }
218
edc9b94e
EM
219 /**
220 * Test update fails with no ID.
221 */
00be9182 222 public function testUpdateWithoutId() {
6a488035
TO
223 $params = array(
224 'name' => '60+ Membership',
225 'description' => 'people above 60 are given health instructions',
226 'member_of_contact_id' => $this->_contactID,
3d6a42e8 227 'financial_type_id' => 1,
6a488035
TO
228 'minimum_fee' => '1200',
229 'duration_unit' => 'month',
230 'duration_interval' => '10',
231 'period_type' => 'rolling',
92915c55
TO
232 'visibility' => 'public',
233 );
6a488035 234
edc9b94e
EM
235 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
236 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
6a488035
TO
237 }
238
00be9182 239 public function testUpdate() {
75638074 240 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
6a488035
TO
241 $newMembOrgParams = array(
242 'organization_name' => 'New membership organisation',
243 'contact_type' => 'Organization',
92915c55
TO
244 'visibility' => 1,
245 );
6a488035
TO
246
247 // create a new contact to update this membership type to
248 $newMembOrgID = $this->organizationCreate($newMembOrgParams);
249
250 $params = array(
251 'id' => $id,
252 'name' => 'Updated General',
253 'member_of_contact_id' => $newMembOrgID,
254 'duration_unit' => 'month',
255 'duration_interval' => '10',
256 'period_type' => 'fixed',
92915c55
TO
257 'domain_id' => 1,
258 );
3d6a42e8 259
d63167fc 260 $this->callAPISuccess('membership_type', 'update', $params);
6a488035
TO
261
262 $this->getAndCheck($params, $id, $this->_entity);
263 }
264
edc9b94e
EM
265 /**
266 * Test for failure when id is not valid.
267 */
00be9182 268 public function testDeleteNotExists() {
6a488035
TO
269 $params = array(
270 'id' => 'doesNotExist',
6a488035 271 );
edc9b94e
EM
272 $this->callAPIFailure('membership_type', 'delete', $params,
273 'Error while deleting membership type. id : ' . $params['id']
274 );
6a488035
TO
275 }
276
00be9182 277 public function testDelete() {
3d6a42e8 278 $orgID = $this->organizationCreate(NULL);
75638074 279 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
3d6a42e8 280 $params = array(
6a488035 281 'id' => $membershipTypeID,
6a488035
TO
282 );
283
edc9b94e 284 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
6a488035 285 }
96025800 286
6a488035 287}