Merge pull request #6739 from jitendrapurohit/CRM-17205
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 /**
40 * Set up for tests.
41 */
42 public function setUp() {
43 parent::setUp();
44 $this->useTransaction(TRUE);
45 $this->_apiversion = 3;
46 $this->_contactID = $this->organizationCreate();
47 }
48
49 /**
50 * Get the membership without providing an ID.
51 *
52 * This should return an empty array but not an error.
53 */
54 public function testGetWithoutId() {
55 $params = array(
56 'name' => '60+ Membership',
57 'description' => 'people above 60 are given health instructions',
58 'financial_type_id' => 1,
59 'minimum_fee' => '200',
60 'duration_unit' => 'month',
61 'duration_interval' => '10',
62 'visibility' => 'public',
63 );
64
65 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
66 $this->assertEquals($membershipType['count'], 0);
67 }
68
69 /**
70 * Test get works.
71 */
72 public function testGet() {
73 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
74
75 $params = array(
76 'id' => $id,
77 );
78 $membershipType = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__, __FILE__);
79 $this->assertEquals($membershipType['values'][$id]['name'], 'General');
80 $this->assertEquals($membershipType['values'][$id]['member_of_contact_id'], $this->_contactID);
81 $this->assertEquals($membershipType['values'][$id]['financial_type_id'], 1);
82 $this->assertEquals($membershipType['values'][$id]['duration_unit'], 'year');
83 $this->assertEquals($membershipType['values'][$id]['duration_interval'], '1');
84 $this->assertEquals($membershipType['values'][$id]['period_type'], 'rolling');
85 $this->membershipTypeDelete($params);
86 }
87
88 public function testCreateWithoutMemberOfContactId() {
89 $params = array(
90 'name' => '60+ Membership',
91 'description' => 'people above 60 are given health instructions',
92 'financial_type_id' => 1,
93 'domain_id' => '1',
94 'minimum_fee' => '200',
95 'duration_unit' => 'month',
96 'duration_interval' => '10',
97 'period_type' => 'rolling',
98 'visibility' => 'public',
99 );
100
101 $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id');
102 }
103
104 public function testCreateWithoutNameandDomainIDandDurationUnit() {
105 $params = array(
106 'description' => 'people above 50 are given health instructions',
107 'member_of_contact_id' => $this->_contactID,
108 'financial_type_id' => 1,
109 'minimum_fee' => '200',
110 'duration_interval' => '10',
111 'period_type' => 'rolling',
112 'visibility' => 'public',
113 );
114
115 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
116 $this->assertEquals($membershiptype['error_message'],
117 'Mandatory key(s) missing from params array: domain_id, duration_unit, name'
118 );
119 }
120
121 public function testCreateWithoutName() {
122 $params = array(
123 'description' => 'people above 50 are given health instructions',
124 'member_of_contact_id' => $this->_contactID,
125 'financial_type_id' => 1,
126 'domain_id' => '1',
127 'minimum_fee' => '200',
128 'duration_unit' => 'month',
129 'duration_interval' => '10',
130 'period_type' => 'rolling',
131 'visibility' => 'public',
132 );
133
134 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
135 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: name');
136 }
137
138 public function testCreate() {
139 $params = array(
140 'name' => '40+ Membership',
141 'description' => 'people above 40 are given health instructions',
142 'member_of_contact_id' => $this->_contactID,
143 'financial_type_id' => 1,
144 'domain_id' => '1',
145 'minimum_fee' => '200',
146 'duration_unit' => 'month',
147 'duration_interval' => '10',
148 'period_type' => 'rolling',
149 'visibility' => 'public',
150 );
151
152 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
153 $this->assertNotNull($membershipType['values']);
154 $this->membershipTypeDelete(array('id' => $membershipType['id']));
155 }
156
157 /**
158 * Test update fails with no ID.
159 */
160 public function testUpdateWithoutId() {
161 $params = array(
162 'name' => '60+ Membership',
163 'description' => 'people above 60 are given health instructions',
164 'member_of_contact_id' => $this->_contactID,
165 'financial_type_id' => 1,
166 'minimum_fee' => '1200',
167 'duration_unit' => 'month',
168 'duration_interval' => '10',
169 'period_type' => 'rolling',
170 'visibility' => 'public',
171 );
172
173 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
174 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
175 }
176
177 public function testUpdate() {
178 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
179 $newMembOrgParams = array(
180 'organization_name' => 'New membership organisation',
181 'contact_type' => 'Organization',
182 'visibility' => 1,
183 );
184
185 // create a new contact to update this membership type to
186 $newMembOrgID = $this->organizationCreate($newMembOrgParams);
187
188 $params = array(
189 'id' => $id,
190 'name' => 'Updated General',
191 'member_of_contact_id' => $newMembOrgID,
192 'duration_unit' => 'month',
193 'duration_interval' => '10',
194 'period_type' => 'fixed',
195 'domain_id' => 1,
196 );
197
198 $this->callAPISuccess('membership_type', 'update', $params);
199
200 $this->getAndCheck($params, $id, $this->_entity);
201 }
202
203 public function testDelete() {
204 $orgID = $this->organizationCreate();
205 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
206 $params = array(
207 'id' => $membershipTypeID,
208 );
209
210 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
211 }
212
213 public function testDeleteRelationshipTypesUsedByMembershipType() {
214 $rel1 = $this->relationshipTypeCreate(array(
215 'name_a_b' => 'abcde',
216 'name_b_a' => 'abcde',
217 ));
218 $rel2 = $this->relationshipTypeCreate(array(
219 'name_a_b' => 'fghij',
220 'name_b_a' => 'fghij',
221 ));
222 $rel3 = $this->relationshipTypeCreate(array(
223 'name_a_b' => 'lkmno',
224 'name_b_a' => 'lkmno',
225 ));
226 $id = $this->membershipTypeCreate(array(
227 'member_of_contact_id' => $this->_contactID,
228 'relationship_type_id' => array($rel1, $rel2, $rel3),
229 'relationship_direction' => array('a_b', 'a_b', 'b_a'),
230 ));
231
232 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel2));
233 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
234 $this->assertEquals(array($rel1, $rel3), $newValues['relationship_type_id']);
235 $this->assertEquals(array('a_b', 'b_a'), $newValues['relationship_direction']);
236
237 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel1));
238 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
239 $this->assertEquals(array($rel3), $newValues['relationship_type_id']);
240 $this->assertEquals(array('b_a'), $newValues['relationship_direction']);
241
242 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel3));
243 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
244 $this->assertTrue(empty($newValues['relationship_type_id']));
245 $this->assertTrue(empty($newValues['relationship_direction']));
246 }
247
248 }