Merge pull request #7722 from kcristiano/17593
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class api_v3_MembershipTypeTest
30 */
31 class api_v3_MembershipTypeTest extends CiviUnitTestCase {
32 protected $_contactID;
33 protected $_contributionTypeID;
34 protected $_apiversion;
35 protected $_entity = 'MembershipType';
36
37 /**
38 * Set up for tests.
39 */
40 public function setUp() {
41 parent::setUp();
42 $this->useTransaction(TRUE);
43 $this->_apiversion = 3;
44 $this->_contactID = $this->organizationCreate();
45 }
46
47 /**
48 * Get the membership without providing an ID.
49 *
50 * This should return an empty array but not an error.
51 */
52 public function testGetWithoutId() {
53 $params = array(
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',
61 );
62
63 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
64 $this->assertEquals($membershipType['count'], 0);
65 }
66
67 /**
68 * Test get works.
69 */
70 public function testGet() {
71 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
72
73 $params = array(
74 'id' => $id,
75 );
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);
84 }
85
86 /**
87 * Test create with missing mandatory field.
88 */
89 public function testCreateWithoutMemberOfContactId() {
90 $params = array(
91 'name' => '60+ Membership',
92 'description' => 'people above 60 are given health instructions',
93 'financial_type_id' => 1,
94 'domain_id' => '1',
95 'minimum_fee' => '200',
96 'duration_unit' => 'month',
97 'duration_interval' => '10',
98 'period_type' => 'rolling',
99 'visibility' => 'public',
100 );
101
102 $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id');
103 }
104
105 /**
106 * Test successful create.
107 */
108 public function testCreate() {
109 $params = array(
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,
114 'domain_id' => '1',
115 'minimum_fee' => '200',
116 'duration_unit' => 'month',
117 'duration_interval' => '10',
118 'period_type' => 'rolling',
119 'visibility' => 'public',
120 );
121
122 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
123 $this->assertNotNull($membershipType['values']);
124 $this->membershipTypeDelete(array('id' => $membershipType['id']));
125 }
126
127 /**
128 * Test update fails with no ID.
129 */
130 public function testUpdateWithoutId() {
131 $params = array(
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',
141 );
142
143 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
144 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
145 }
146
147 /**
148 * Test update.
149 */
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',
155 'visibility' => 1,
156 );
157
158 $params = array(
159 'id' => $id,
160 'name' => 'Updated General',
161 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams),
162 'duration_unit' => 'month',
163 'duration_interval' => '10',
164 'period_type' => 'fixed',
165 'domain_id' => 1,
166 );
167
168 $this->callAPISuccess('membership_type', 'update', $params);
169
170 $this->getAndCheck($params, $id, $this->_entity);
171 }
172
173 /**
174 * Test successful delete.
175 */
176 public function testDelete() {
177 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->organizationCreate()));
178 $params = array(
179 'id' => $membershipTypeID,
180 );
181
182 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
183 }
184
185 /**
186 * Delete test that could do with a decent comment block.
187 *
188 * I can't skim this & understand it so if anyone does explain it here.
189 */
190 public function testDeleteRelationshipTypesUsedByMembershipType() {
191 $rel1 = $this->relationshipTypeCreate(array(
192 'name_a_b' => 'abcde',
193 'name_b_a' => 'abcde',
194 ));
195 $rel2 = $this->relationshipTypeCreate(array(
196 'name_a_b' => 'fghij',
197 'name_b_a' => 'fghij',
198 ));
199 $rel3 = $this->relationshipTypeCreate(array(
200 'name_a_b' => 'lkmno',
201 'name_b_a' => 'lkmno',
202 ));
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'),
207 ));
208
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']);
213
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']);
218
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']));
223 }
224
225 /**
226 * Test that membership type getlist returns an array of enabled membership types.
227 */
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']);
236 }
237
238 }