commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / tests / phpunit / api / v3 / MembershipTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public function setUp() {
40 parent::setUp();
41 $this->useTransaction(TRUE);
42 $this->_apiversion = 3;
43 $this->_contactID = $this->organizationCreate();
44 }
45
46 public function testGetWithoutId() {
47 $params = array(
48 'name' => '60+ Membership',
49 'description' => 'people above 60 are given health instructions',
50 'financial_type_id' => 1,
51 'minimum_fee' => '200',
52 'duration_unit' => 'month',
53 'duration_interval' => '10',
54 'visibility' => 'public',
55 );
56
57 $membershiptype = $this->callAPISuccess('membership_type', 'get', $params);
58 $this->assertEquals($membershiptype['count'], 0);
59 }
60
61 public function testGet() {
62 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
63
64 $params = array(
65 'id' => $id,
66 );
67 $membershiptype = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__, __FILE__);
68 $this->assertEquals($membershiptype['values'][$id]['name'], 'General', 'In line ' . __LINE__ . " id is " . $id);
69 $this->assertEquals($membershiptype['values'][$id]['member_of_contact_id'], $this->_contactID, 'In line ' . __LINE__);
70 $this->assertEquals($membershiptype['values'][$id]['financial_type_id'], 1, 'In line ' . __LINE__);
71 $this->assertEquals($membershiptype['values'][$id]['duration_unit'], 'year', 'In line ' . __LINE__);
72 $this->assertEquals($membershiptype['values'][$id]['duration_interval'], '1', 'In line ' . __LINE__);
73 $this->assertEquals($membershiptype['values'][$id]['period_type'], 'rolling', 'In line ' . __LINE__);
74 $this->membershipTypeDelete($params);
75 }
76
77 public function testCreateWithoutMemberOfContactId() {
78 $params = array(
79 'name' => '60+ Membership',
80 'description' => 'people above 60 are given health instructions',
81 'financial_type_id' => 1,
82 'domain_id' => '1',
83 'minimum_fee' => '200',
84 'duration_unit' => 'month',
85 'duration_interval' => '10',
86 'period_type' => 'rolling',
87 'visibility' => 'public',
88 );
89
90 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params,
91 'Mandatory key(s) missing from params array: member_of_contact_id'
92 );
93 }
94
95 public function testCreateWithoutNameandDomainIDandDurationUnit() {
96 $params = array(
97 'description' => 'people above 50 are given health instructions',
98 'member_of_contact_id' => $this->_contactID,
99 'financial_type_id' => 1,
100 'minimum_fee' => '200',
101 'duration_interval' => '10',
102 'period_type' => 'rolling',
103 'visibility' => 'public',
104 );
105
106 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
107 $this->assertEquals($membershiptype['error_message'],
108 'Mandatory key(s) missing from params array: domain_id, duration_unit, name'
109 );
110 }
111
112 public function testCreateWithoutName() {
113 $params = array(
114 'description' => 'people above 50 are given health instructions',
115 'member_of_contact_id' => $this->_contactID,
116 'financial_type_id' => 1,
117 'domain_id' => '1',
118 'minimum_fee' => '200',
119 'duration_unit' => 'month',
120 'duration_interval' => '10',
121 'period_type' => 'rolling',
122 'visibility' => 'public',
123 );
124
125 $membershiptype = $this->callAPIFailure('membership_type', 'create', $params);
126 $this->assertEquals($membershiptype['error_message'], 'Mandatory key(s) missing from params array: name');
127 }
128
129 public function testCreate() {
130 $params = array(
131 'name' => '40+ Membership',
132 'description' => 'people above 40 are given health instructions',
133 'member_of_contact_id' => $this->_contactID,
134 'financial_type_id' => 1,
135 'domain_id' => '1',
136 'minimum_fee' => '200',
137 'duration_unit' => 'month',
138 'duration_interval' => '10',
139 'period_type' => 'rolling',
140 'visibility' => 'public',
141 );
142
143 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
144 $this->assertNotNull($membershipType['values']);
145 $this->membershipTypeDelete(array('id' => $membershipType['id']));
146 }
147
148 /**
149 * Test update fails with no ID.
150 */
151 public function testUpdateWithoutId() {
152 $params = array(
153 'name' => '60+ Membership',
154 'description' => 'people above 60 are given health instructions',
155 'member_of_contact_id' => $this->_contactID,
156 'financial_type_id' => 1,
157 'minimum_fee' => '1200',
158 'duration_unit' => 'month',
159 'duration_interval' => '10',
160 'period_type' => 'rolling',
161 'visibility' => 'public',
162 );
163
164 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
165 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
166 }
167
168 public function testUpdate() {
169 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
170 $newMembOrgParams = array(
171 'organization_name' => 'New membership organisation',
172 'contact_type' => 'Organization',
173 'visibility' => 1,
174 );
175
176 // create a new contact to update this membership type to
177 $newMembOrgID = $this->organizationCreate($newMembOrgParams);
178
179 $params = array(
180 'id' => $id,
181 'name' => 'Updated General',
182 'member_of_contact_id' => $newMembOrgID,
183 'duration_unit' => 'month',
184 'duration_interval' => '10',
185 'period_type' => 'fixed',
186 'domain_id' => 1,
187 );
188
189 $this->callAPISuccess('membership_type', 'update', $params);
190
191 $this->getAndCheck($params, $id, $this->_entity);
192 }
193
194 public function testDelete() {
195 $orgID = $this->organizationCreate();
196 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $orgID));
197 $params = array(
198 'id' => $membershipTypeID,
199 );
200
201 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
202 }
203
204 public function testDeleteRelationshipTypesUsedByMembershipType() {
205 $rel1 = $this->relationshipTypeCreate(array(
206 'name_a_b' => 'abcde',
207 'name_b_a' => 'abcde',
208 ));
209 $rel2 = $this->relationshipTypeCreate(array(
210 'name_a_b' => 'fghij',
211 'name_b_a' => 'fghij',
212 ));
213 $rel3 = $this->relationshipTypeCreate(array(
214 'name_a_b' => 'lkmno',
215 'name_b_a' => 'lkmno',
216 ));
217 $id = $this->membershipTypeCreate(array(
218 'member_of_contact_id' => $this->_contactID,
219 'relationship_type_id' => array($rel1, $rel2, $rel3),
220 'relationship_direction' => array('a_b', 'a_b', 'b_a'),
221 ));
222
223 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel2));
224 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
225 $this->assertEquals(array($rel1, $rel3), $newValues['relationship_type_id']);
226 $this->assertEquals(array('a_b', 'b_a'), $newValues['relationship_direction']);
227
228 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel1));
229 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
230 $this->assertEquals(array($rel3), $newValues['relationship_type_id']);
231 $this->assertEquals(array('b_a'), $newValues['relationship_direction']);
232
233 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel3));
234 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
235 $this->assertTrue(empty($newValues['relationship_type_id']));
236 $this->assertTrue(empty($newValues['relationship_direction']));
237 }
238
239 }