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);
89 * Test create with missing mandatory field.
91 public function testCreateWithoutMemberOfContactId() {
93 'name' => '60+ Membership',
94 'description' => 'people above 60 are given health instructions',
95 'financial_type_id' => 1,
97 'minimum_fee' => '200',
98 'duration_unit' => 'month',
99 'duration_interval' => '10',
100 'period_type' => 'rolling',
101 'visibility' => 'public',
104 $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id');
108 * Test successful create.
110 public function testCreate() {
112 'name' => '40+ Membership',
113 'description' => 'people above 40 are given health instructions',
114 'member_of_contact_id' => $this->_contactID
,
115 'financial_type_id' => 1,
117 'minimum_fee' => '200',
118 'duration_unit' => 'month',
119 'duration_interval' => '10',
120 'period_type' => 'rolling',
121 'visibility' => 'public',
124 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__
, __FILE__
);
125 $this->assertNotNull($membershipType['values']);
126 $this->membershipTypeDelete(array('id' => $membershipType['id']));
130 * Test update fails with no ID.
132 public function testUpdateWithoutId() {
134 'name' => '60+ Membership',
135 'description' => 'people above 60 are given health instructions',
136 'member_of_contact_id' => $this->_contactID
,
137 'financial_type_id' => 1,
138 'minimum_fee' => '1200',
139 'duration_unit' => 'month',
140 'duration_interval' => '10',
141 'period_type' => 'rolling',
142 'visibility' => 'public',
145 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
146 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
152 public function testUpdate() {
153 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
, 'financial_type_id' => 2));
154 $newMemberOrgParams = array(
155 'organization_name' => 'New membership organisation',
156 'contact_type' => 'Organization',
162 'name' => 'Updated General',
163 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams),
164 'duration_unit' => 'month',
165 'duration_interval' => '10',
166 'period_type' => 'fixed',
170 $this->callAPISuccess('membership_type', 'update', $params);
172 $this->getAndCheck($params, $id, $this->_entity
);
176 * Test successful delete.
178 public function testDelete() {
179 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->organizationCreate()));
181 'id' => $membershipTypeID,
184 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__
, __FILE__
);
188 * Delete test that could do with a decent comment block.
190 * I can't skim this & understand it so if anyone does explain it here.
192 public function testDeleteRelationshipTypesUsedByMembershipType() {
193 $rel1 = $this->relationshipTypeCreate(array(
194 'name_a_b' => 'abcde',
195 'name_b_a' => 'abcde',
197 $rel2 = $this->relationshipTypeCreate(array(
198 'name_a_b' => 'fghij',
199 'name_b_a' => 'fghij',
201 $rel3 = $this->relationshipTypeCreate(array(
202 'name_a_b' => 'lkmno',
203 'name_b_a' => 'lkmno',
205 $id = $this->membershipTypeCreate(array(
206 'member_of_contact_id' => $this->_contactID
,
207 'relationship_type_id' => array($rel1, $rel2, $rel3),
208 'relationship_direction' => array('a_b', 'a_b', 'b_a'),
211 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel2));
212 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
213 $this->assertEquals(array($rel1, $rel3), $newValues['relationship_type_id']);
214 $this->assertEquals(array('a_b', 'b_a'), $newValues['relationship_direction']);
216 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel1));
217 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
218 $this->assertEquals(array($rel3), $newValues['relationship_type_id']);
219 $this->assertEquals(array('b_a'), $newValues['relationship_direction']);
221 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel3));
222 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
223 $this->assertTrue(empty($newValues['relationship_type_id']));
224 $this->assertTrue(empty($newValues['relationship_direction']));
228 * Test that membership type getlist returns an array of enabled membership types.
230 public function testMembershipTypeGetList() {
231 $this->membershipTypeCreate();
232 $this->membershipTypeCreate(array('name' => 'cheap-skates'));
233 $this->membershipTypeCreate(array('name' => 'disabled cheap-skates', 'is_active' => 0));
234 $result = $this->callAPISuccess('MembershipType', 'getlist', array());
235 $this->assertEquals(2, $result['count']);
236 $this->assertEquals('cheap-skates', $result['values'][0]['label']);
237 $this->assertEquals('General', $result['values'][1]['label']);