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 +--------------------------------------------------------------------+
29 * Class api_v3_MembershipTypeTest
31 class api_v3_MembershipTypeTest
extends CiviUnitTestCase
{
32 protected $_contactID;
33 protected $_contributionTypeID;
34 protected $_apiversion;
35 protected $_entity = 'MembershipType';
40 public function setUp() {
42 $this->useTransaction(TRUE);
43 $this->_apiversion
= 3;
44 $this->_contactID
= $this->organizationCreate();
48 * Get the membership without providing an ID.
50 * This should return an empty array but not an error.
52 public function testGetWithoutId() {
54 'name' => '60+ Membership',
55 'description' => 'people above 60 are given health instructions',
56 'financial_type_id' => 1,
57 'minimum_fee' => '200',
58 'duration_unit' => 'month',
59 'duration_interval' => '10',
60 'visibility' => 'public',
63 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
64 $this->assertEquals($membershipType['count'], 0);
70 public function testGet() {
71 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
));
76 $membershipType = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__
, __FILE__
);
77 $this->assertEquals($membershipType['values'][$id]['name'], 'General');
78 $this->assertEquals($membershipType['values'][$id]['member_of_contact_id'], $this->_contactID
);
79 $this->assertEquals($membershipType['values'][$id]['financial_type_id'], 1);
80 $this->assertEquals($membershipType['values'][$id]['duration_unit'], 'year');
81 $this->assertEquals($membershipType['values'][$id]['duration_interval'], '1');
82 $this->assertEquals($membershipType['values'][$id]['period_type'], 'rolling');
83 $this->membershipTypeDelete($params);
87 * Test create with missing mandatory field.
89 public function testCreateWithoutMemberOfContactId() {
91 'name' => '60+ Membership',
92 'description' => 'people above 60 are given health instructions',
93 'financial_type_id' => 1,
95 'minimum_fee' => '200',
96 'duration_unit' => 'month',
97 'duration_interval' => '10',
98 'period_type' => 'rolling',
99 'visibility' => 'public',
102 $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id');
106 * Test successful create.
108 public function testCreate() {
110 'name' => '40+ Membership',
111 'description' => 'people above 40 are given health instructions',
112 'member_of_contact_id' => $this->_contactID
,
113 'financial_type_id' => 1,
115 'minimum_fee' => '200',
116 'duration_unit' => 'month',
117 'duration_interval' => '10',
118 'period_type' => 'rolling',
119 'visibility' => 'public',
122 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__
, __FILE__
);
123 $this->assertNotNull($membershipType['values']);
124 $this->membershipTypeDelete(array('id' => $membershipType['id']));
128 * Test update fails with no ID.
130 public function testUpdateWithoutId() {
132 'name' => '60+ Membership',
133 'description' => 'people above 60 are given health instructions',
134 'member_of_contact_id' => $this->_contactID
,
135 'financial_type_id' => 1,
136 'minimum_fee' => '1200',
137 'duration_unit' => 'month',
138 'duration_interval' => '10',
139 'period_type' => 'rolling',
140 'visibility' => 'public',
143 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
144 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
150 public function testUpdate() {
151 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
, 'financial_type_id' => 2));
152 $newMemberOrgParams = array(
153 'organization_name' => 'New membership organisation',
154 'contact_type' => 'Organization',
160 'name' => 'Updated General',
161 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams),
162 'duration_unit' => 'month',
163 'duration_interval' => '10',
164 'period_type' => 'fixed',
168 $this->callAPISuccess('membership_type', 'update', $params);
170 $this->getAndCheck($params, $id, $this->_entity
);
174 * Test successful delete.
176 public function testDelete() {
177 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->organizationCreate()));
179 'id' => $membershipTypeID,
182 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__
, __FILE__
);
186 * Delete test that could do with a decent comment block.
188 * I can't skim this & understand it so if anyone does explain it here.
190 public function testDeleteRelationshipTypesUsedByMembershipType() {
191 $rel1 = $this->relationshipTypeCreate(array(
192 'name_a_b' => 'abcde',
193 'name_b_a' => 'abcde',
195 $rel2 = $this->relationshipTypeCreate(array(
196 'name_a_b' => 'fghij',
197 'name_b_a' => 'fghij',
199 $rel3 = $this->relationshipTypeCreate(array(
200 'name_a_b' => 'lkmno',
201 'name_b_a' => 'lkmno',
203 $id = $this->membershipTypeCreate(array(
204 'member_of_contact_id' => $this->_contactID
,
205 'relationship_type_id' => array($rel1, $rel2, $rel3),
206 'relationship_direction' => array('a_b', 'a_b', 'b_a'),
209 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel2));
210 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
211 $this->assertEquals(array($rel1, $rel3), $newValues['relationship_type_id']);
212 $this->assertEquals(array('a_b', 'b_a'), $newValues['relationship_direction']);
214 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel1));
215 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
216 $this->assertEquals(array($rel3), $newValues['relationship_type_id']);
217 $this->assertEquals(array('b_a'), $newValues['relationship_direction']);
219 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel3));
220 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
221 $this->assertTrue(empty($newValues['relationship_type_id']));
222 $this->assertTrue(empty($newValues['relationship_direction']));
226 * Test that membership type getlist returns an array of enabled membership types.
228 public function testMembershipTypeGetList() {
229 $this->membershipTypeCreate();
230 $this->membershipTypeCreate(array('name' => 'cheap-skates'));
231 $this->membershipTypeCreate(array('name' => 'disabled cheap-skates', 'is_active' => 0));
232 $result = $this->callAPISuccess('MembershipType', 'getlist', array());
233 $this->assertEquals(2, $result['count']);
234 $this->assertEquals('cheap-skates', $result['values'][0]['label']);
235 $this->assertEquals('General', $result['values'][1]['label']);