3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Class api_v3_MembershipTypeTest
33 class api_v3_MembershipTypeTest
extends CiviUnitTestCase
{
34 protected $_contactID;
35 protected $_contributionTypeID;
36 protected $_apiversion;
37 protected $_entity = 'MembershipType';
39 public function setUp() {
41 $this->useTransaction(TRUE);
42 $this->_apiversion
= 3;
43 $this->_contactID
= $this->organizationCreate();
46 public function testGetWithoutId() {
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',
57 $membershiptype = $this->callAPISuccess('membership_type', 'get', $params);
58 $this->assertEquals($membershiptype['count'], 0);
61 public function testGet() {
62 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
));
67 $membershiptype = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__
, __FILE__
);
68 $this->assertEquals($membershiptype['values'][$id]['name'], 'General');
69 $this->assertEquals($membershiptype['values'][$id]['member_of_contact_id'], $this->_contactID
);
70 $this->assertEquals($membershiptype['values'][$id]['financial_type_id'], 1);
71 $this->assertEquals($membershiptype['values'][$id]['duration_unit'], 'year');
72 $this->assertEquals($membershiptype['values'][$id]['duration_interval'], '1');
73 $this->assertEquals($membershiptype['values'][$id]['period_type'], 'rolling');
74 $this->membershipTypeDelete($params);
77 public function testCreateWithoutMemberOfContactId() {
79 'name' => '60+ Membership',
80 'description' => 'people above 60 are given health instructions',
81 'financial_type_id' => 1,
83 'minimum_fee' => '200',
84 'duration_unit' => 'month',
85 'duration_interval' => '10',
86 'period_type' => 'rolling',
87 'visibility' => 'public',
90 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params,
91 'Mandatory key(s) missing from params array: member_of_contact_id'
95 public function testCreateWithoutNameandDomainIDandDurationUnit() {
97 'description' => 'people above 50 are given health instructions',
98 'member_of_contact_id' => $this->_contactID
,
99 'financial_type_id' => 1,
100 'minimum_fee' => '200',
101 'duration_interval' => '10',
102 'period_type' => 'rolling',
103 'visibility' => 'public',
106 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
107 $this->assertEquals($membershiptype['error_message'],
108 'Mandatory key(s) missing from params array: domain_id, duration_unit, name'
112 public function testCreateWithoutName() {
114 'description' => 'people above 50 are given health instructions',
115 'member_of_contact_id' => $this->_contactID
,
116 'financial_type_id' => 1,
118 'minimum_fee' => '200',
119 'duration_unit' => 'month',
120 'duration_interval' => '10',
121 'period_type' => 'rolling',
122 'visibility' => 'public',
125 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
126 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: name');
129 public function testCreate() {
131 'name' => '40+ Membership',
132 'description' => 'people above 40 are given health instructions',
133 'member_of_contact_id' => $this->_contactID
,
134 'financial_type_id' => 1,
136 'minimum_fee' => '200',
137 'duration_unit' => 'month',
138 'duration_interval' => '10',
139 'period_type' => 'rolling',
140 'visibility' => 'public',
143 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__
, __FILE__
);
144 $this->assertNotNull($membershipType['values']);
145 $this->membershipTypeDelete(array('id' => $membershipType['id']));
149 * Test update fails with no ID.
151 public function testUpdateWithoutId() {
153 'name' => '60+ Membership',
154 'description' => 'people above 60 are given health instructions',
155 'member_of_contact_id' => $this->_contactID
,
156 'financial_type_id' => 1,
157 'minimum_fee' => '1200',
158 'duration_unit' => 'month',
159 'duration_interval' => '10',
160 'period_type' => 'rolling',
161 'visibility' => 'public',
164 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
165 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
168 public function testUpdate() {
169 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
, 'financial_type_id' => 2));
170 $newMembOrgParams = array(
171 'organization_name' => 'New membership organisation',
172 'contact_type' => 'Organization',
176 // create a new contact to update this membership type to
177 $newMembOrgID = $this->organizationCreate($newMembOrgParams);
181 'name' => 'Updated General',
182 'member_of_contact_id' => $newMembOrgID,
183 'duration_unit' => 'month',
184 'duration_interval' => '10',
185 'period_type' => 'fixed',
189 $this->callAPISuccess('membership_type', 'update', $params);
191 $this->getAndCheck($params, $id, $this->_entity
);
194 public function testDelete() {
195 $orgID = $this->organizationCreate();
196 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
198 'id' => $membershipTypeID,
201 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__
, __FILE__
);
204 public function testDeleteRelationshipTypesUsedByMembershipType() {
205 $rel1 = $this->relationshipTypeCreate(array(
206 'name_a_b' => 'abcde',
207 'name_b_a' => 'abcde',
209 $rel2 = $this->relationshipTypeCreate(array(
210 'name_a_b' => 'fghij',
211 'name_b_a' => 'fghij',
213 $rel3 = $this->relationshipTypeCreate(array(
214 'name_a_b' => 'lkmno',
215 'name_b_a' => 'lkmno',
217 $id = $this->membershipTypeCreate(array(
218 'member_of_contact_id' => $this->_contactID
,
219 'relationship_type_id' => array($rel1, $rel2, $rel3),
220 'relationship_direction' => array('a_b', 'a_b', 'b_a'),
223 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel2));
224 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
225 $this->assertEquals(array($rel1, $rel3), $newValues['relationship_type_id']);
226 $this->assertEquals(array('a_b', 'b_a'), $newValues['relationship_direction']);
228 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel1));
229 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
230 $this->assertEquals(array($rel3), $newValues['relationship_type_id']);
231 $this->assertEquals(array('b_a'), $newValues['relationship_direction']);
233 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel3));
234 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
235 $this->assertTrue(empty($newValues['relationship_type_id']));
236 $this->assertTrue(empty($newValues['relationship_direction']));