Merge pull request #7578 from seamuslee001/CRM-17802-master
[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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class api_v3_MembershipTypeTest
32 */
33 class api_v3_MembershipTypeTest extends CiviUnitTestCase {
34 protected $_contactID;
35 protected $_contributionTypeID;
36 protected $_apiversion;
37 protected $_entity = 'MembershipType';
38
39 /**
40 * Set up for tests.
41 */
42 public function setUp() {
43 parent::setUp();
44 $this->useTransaction(TRUE);
45 $this->_apiversion = 3;
46 $this->_contactID = $this->organizationCreate();
47 }
48
49 /**
50 * Get the membership without providing an ID.
51 *
52 * This should return an empty array but not an error.
53 */
54 public function testGetWithoutId() {
55 $params = array(
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',
63 );
64
65 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
66 $this->assertEquals($membershipType['count'], 0);
67 }
68
69 /**
70 * Test get works.
71 */
72 public function testGet() {
73 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
74
75 $params = array(
76 'id' => $id,
77 );
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);
86 }
87
88 /**
89 * Test create with missing mandatory field.
90 */
91 public function testCreateWithoutMemberOfContactId() {
92 $params = array(
93 'name' => '60+ Membership',
94 'description' => 'people above 60 are given health instructions',
95 'financial_type_id' => 1,
96 'domain_id' => '1',
97 'minimum_fee' => '200',
98 'duration_unit' => 'month',
99 'duration_interval' => '10',
100 'period_type' => 'rolling',
101 'visibility' => 'public',
102 );
103
104 $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id');
105 }
106
107 /**
108 * Test successful create.
109 */
110 public function testCreate() {
111 $params = array(
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,
116 'domain_id' => '1',
117 'minimum_fee' => '200',
118 'duration_unit' => 'month',
119 'duration_interval' => '10',
120 'period_type' => 'rolling',
121 'visibility' => 'public',
122 );
123
124 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
125 $this->assertNotNull($membershipType['values']);
126 $this->membershipTypeDelete(array('id' => $membershipType['id']));
127 }
128
129 /**
130 * Test update fails with no ID.
131 */
132 public function testUpdateWithoutId() {
133 $params = array(
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',
143 );
144
145 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
146 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
147 }
148
149 /**
150 * Test update.
151 */
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',
157 'visibility' => 1,
158 );
159
160 $params = array(
161 'id' => $id,
162 'name' => 'Updated General',
163 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams),
164 'duration_unit' => 'month',
165 'duration_interval' => '10',
166 'period_type' => 'fixed',
167 'domain_id' => 1,
168 );
169
170 $this->callAPISuccess('membership_type', 'update', $params);
171
172 $this->getAndCheck($params, $id, $this->_entity);
173 }
174
175 /**
176 * Test successful delete.
177 */
178 public function testDelete() {
179 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->organizationCreate()));
180 $params = array(
181 'id' => $membershipTypeID,
182 );
183
184 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
185 }
186
187 /**
188 * Delete test that could do with a decent comment block.
189 *
190 * I can't skim this & understand it so if anyone does explain it here.
191 */
192 public function testDeleteRelationshipTypesUsedByMembershipType() {
193 $rel1 = $this->relationshipTypeCreate(array(
194 'name_a_b' => 'abcde',
195 'name_b_a' => 'abcde',
196 ));
197 $rel2 = $this->relationshipTypeCreate(array(
198 'name_a_b' => 'fghij',
199 'name_b_a' => 'fghij',
200 ));
201 $rel3 = $this->relationshipTypeCreate(array(
202 'name_a_b' => 'lkmno',
203 'name_b_a' => 'lkmno',
204 ));
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'),
209 ));
210
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']);
215
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']);
220
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']));
225 }
226
227 /**
228 * Test that membership type getlist returns an array of enabled membership types.
229 */
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']);
238 }
239
240 }