3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
29 * Class api_v3_MembershipTypeTest
32 class api_v3_MembershipTypeTest
extends CiviUnitTestCase
{
33 protected $_contactID;
34 protected $_contributionTypeID;
35 protected $_apiversion;
36 protected $_entity = 'MembershipType';
41 public function setUp() {
43 $this->useTransaction(TRUE);
44 $this->_apiversion
= 3;
45 $this->_contactID
= $this->organizationCreate();
49 * Get the membership without providing an ID.
51 * This should return an empty array but not an error.
53 public function testGetWithoutId() {
55 'name' => '60+ Membership',
56 'description' => 'people above 60 are given health instructions',
57 'financial_type_id' => 1,
58 'minimum_fee' => '200',
59 'duration_unit' => 'month',
60 'duration_interval' => '10',
61 'visibility' => 'public',
64 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
65 $this->assertEquals($membershipType['count'], 0);
71 public function testGet() {
72 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
));
77 $membershipType = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__
, __FILE__
);
78 $this->assertEquals($membershipType['values'][$id]['name'], 'General');
79 $this->assertEquals($membershipType['values'][$id]['member_of_contact_id'], $this->_contactID
);
80 $this->assertEquals($membershipType['values'][$id]['financial_type_id'], 1);
81 $this->assertEquals($membershipType['values'][$id]['duration_unit'], 'year');
82 $this->assertEquals($membershipType['values'][$id]['duration_interval'], '1');
83 $this->assertEquals($membershipType['values'][$id]['period_type'], 'rolling');
84 $this->membershipTypeDelete($params);
88 * Test create with missing mandatory field.
90 public function testCreateWithoutMemberOfContactId() {
92 'name' => '60+ Membership',
93 'description' => 'people above 60 are given health instructions',
94 'financial_type_id' => 1,
96 'minimum_fee' => '200',
97 'duration_unit' => 'month',
98 'duration_interval' => '10',
99 'period_type' => 'rolling',
100 'visibility' => 'public',
103 $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id');
107 * Test successful create.
109 public function testCreate() {
111 'name' => '40+ Membership',
112 'description' => 'people above 40 are given health instructions',
113 'member_of_contact_id' => $this->_contactID
,
114 'financial_type_id' => 1,
116 'minimum_fee' => '200',
117 'duration_unit' => 'month',
118 'duration_interval' => '10',
119 'period_type' => 'rolling',
120 'visibility' => 'public',
123 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__
, __FILE__
);
124 $this->assertNotNull($membershipType['values']);
125 $this->membershipTypeDelete(array('id' => $membershipType['id']));
129 * Test update fails with no ID.
131 public function testUpdateWithoutId() {
133 'name' => '60+ Membership',
134 'description' => 'people above 60 are given health instructions',
135 'member_of_contact_id' => $this->_contactID
,
136 'financial_type_id' => 1,
137 'minimum_fee' => '1200',
138 'duration_unit' => 'month',
139 'duration_interval' => '10',
140 'period_type' => 'rolling',
141 'visibility' => 'public',
144 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
145 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
151 public function testUpdate() {
152 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
, 'financial_type_id' => 2));
153 $newMemberOrgParams = array(
154 'organization_name' => 'New membership organisation',
155 'contact_type' => 'Organization',
161 'name' => 'Updated General',
162 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams),
163 'duration_unit' => 'month',
164 'duration_interval' => '10',
165 'period_type' => 'fixed',
169 $this->callAPISuccess('membership_type', 'update', $params);
171 $this->getAndCheck($params, $id, $this->_entity
);
175 * Test successful delete.
177 public function testDelete() {
178 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->organizationCreate()));
180 'id' => $membershipTypeID,
183 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__
, __FILE__
);
187 * Delete test that could do with a decent comment block.
189 * I can't skim this & understand it so if anyone does explain it here.
191 public function testDeleteRelationshipTypesUsedByMembershipType() {
192 $rel1 = $this->relationshipTypeCreate(array(
193 'name_a_b' => 'abcde',
194 'name_b_a' => 'abcde',
196 $rel2 = $this->relationshipTypeCreate(array(
197 'name_a_b' => 'fghij',
198 'name_b_a' => 'fghij',
200 $rel3 = $this->relationshipTypeCreate(array(
201 'name_a_b' => 'lkmno',
202 'name_b_a' => 'lkmno',
204 $id = $this->membershipTypeCreate(array(
205 'member_of_contact_id' => $this->_contactID
,
206 'relationship_type_id' => array($rel1, $rel2, $rel3),
207 'relationship_direction' => array('a_b', 'a_b', 'b_a'),
210 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel2));
211 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
212 $this->assertEquals(array($rel1, $rel3), $newValues['relationship_type_id']);
213 $this->assertEquals(array('a_b', 'b_a'), $newValues['relationship_direction']);
215 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel1));
216 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
217 $this->assertEquals(array($rel3), $newValues['relationship_type_id']);
218 $this->assertEquals(array('b_a'), $newValues['relationship_direction']);
220 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel3));
221 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
222 $this->assertTrue(empty($newValues['relationship_type_id']));
223 $this->assertTrue(empty($newValues['relationship_direction']));
227 * Test that membership type getlist returns an array of enabled membership types.
229 public function testMembershipTypeGetList() {
230 $this->membershipTypeCreate();
231 $this->membershipTypeCreate(array('name' => 'cheap-skates'));
232 $this->membershipTypeCreate(array('name' => 'disabled cheap-skates', 'is_active' => 0));
233 $result = $this->callAPISuccess('MembershipType', 'getlist', array());
234 $this->assertEquals(2, $result['count']);
235 $this->assertEquals('cheap-skates', $result['values'][0]['label']);
236 $this->assertEquals('General', $result['values'][1]['label']);