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';
42 public function setUp() {
44 $this->useTransaction(TRUE);
45 $this->_apiversion
= 3;
46 $this->_contactID
= $this->organizationCreate();
50 * Get the membership without providing an ID.
52 * This should return an empty array but not an error.
54 public function testGetWithoutId() {
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',
65 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
66 $this->assertEquals($membershipType['count'], 0);
72 public function testGet() {
73 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
));
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);
88 public function testCreateWithoutMemberOfContactId() {
90 'name' => '60+ Membership',
91 'description' => 'people above 60 are given health instructions',
92 'financial_type_id' => 1,
94 'minimum_fee' => '200',
95 'duration_unit' => 'month',
96 'duration_interval' => '10',
97 'period_type' => 'rolling',
98 'visibility' => 'public',
101 $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id');
104 public function testCreateWithoutNameandDomainIDandDurationUnit() {
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',
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'
121 public function testCreateWithoutName() {
123 'description' => 'people above 50 are given health instructions',
124 'member_of_contact_id' => $this->_contactID
,
125 'financial_type_id' => 1,
127 'minimum_fee' => '200',
128 'duration_unit' => 'month',
129 'duration_interval' => '10',
130 'period_type' => 'rolling',
131 'visibility' => 'public',
134 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
135 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: name');
138 public function testCreate() {
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,
145 'minimum_fee' => '200',
146 'duration_unit' => 'month',
147 'duration_interval' => '10',
148 'period_type' => 'rolling',
149 'visibility' => 'public',
152 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__
, __FILE__
);
153 $this->assertNotNull($membershipType['values']);
154 $this->membershipTypeDelete(array('id' => $membershipType['id']));
158 * Test update fails with no ID.
160 public function testUpdateWithoutId() {
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',
173 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
174 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
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',
185 // create a new contact to update this membership type to
186 $newMembOrgID = $this->organizationCreate($newMembOrgParams);
190 'name' => 'Updated General',
191 'member_of_contact_id' => $newMembOrgID,
192 'duration_unit' => 'month',
193 'duration_interval' => '10',
194 'period_type' => 'fixed',
198 $this->callAPISuccess('membership_type', 'update', $params);
200 $this->getAndCheck($params, $id, $this->_entity
);
203 public function testDelete() {
204 $orgID = $this->organizationCreate();
205 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
207 'id' => $membershipTypeID,
210 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__
, __FILE__
);
213 public function testDeleteRelationshipTypesUsedByMembershipType() {
214 $rel1 = $this->relationshipTypeCreate(array(
215 'name_a_b' => 'abcde',
216 'name_b_a' => 'abcde',
218 $rel2 = $this->relationshipTypeCreate(array(
219 'name_a_b' => 'fghij',
220 'name_b_a' => 'fghij',
222 $rel3 = $this->relationshipTypeCreate(array(
223 'name_a_b' => 'lkmno',
224 'name_b_a' => 'lkmno',
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'),
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']);
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']);
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']));