tests/phpunit/** - Remove unnecessary "require_once" statements
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTypeTest.php
CommitLineData
6a488035 1<?php
6a488035 2/*
8d7a9d07 3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
8d7a9d07 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
8d7a9d07
CB
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 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035 27
e9479dcf
EM
28/**
29 * Class api_v3_MembershipTypeTest
30 */
6a488035
TO
31class api_v3_MembershipTypeTest extends CiviUnitTestCase {
32 protected $_contactID;
33 protected $_contributionTypeID;
34 protected $_apiversion;
35 protected $_entity = 'MembershipType';
b7c9bc4c 36
80d714d2 37 /**
38 * Set up for tests.
39 */
00be9182 40 public function setUp() {
6a488035 41 parent::setUp();
31c45547 42 $this->useTransaction(TRUE);
6a488035 43 $this->_apiversion = 3;
baccd59e 44 $this->_contactID = $this->organizationCreate();
6a488035
TO
45 }
46
80d714d2 47 /**
48 * Get the membership without providing an ID.
49 *
50 * This should return an empty array but not an error.
51 */
00be9182 52 public function testGetWithoutId() {
6a488035
TO
53 $params = array(
54 'name' => '60+ Membership',
55 'description' => 'people above 60 are given health instructions',
3d6a42e8 56 'financial_type_id' => 1,
6a488035
TO
57 'minimum_fee' => '200',
58 'duration_unit' => 'month',
59 'duration_interval' => '10',
60 'visibility' => 'public',
6a488035
TO
61 );
62
80d714d2 63 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
64 $this->assertEquals($membershipType['count'], 0);
6a488035
TO
65 }
66
80d714d2 67 /**
68 * Test get works.
69 */
00be9182 70 public function testGet() {
75638074 71 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
6a488035
TO
72
73 $params = array(
74 'id' => $id,
6a488035 75 );
80d714d2 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');
6a488035
TO
83 $this->membershipTypeDelete($params);
84 }
85
5f1108c8 86 /**
87 * Test create with missing mandatory field.
88 */
00be9182 89 public function testCreateWithoutMemberOfContactId() {
6a488035
TO
90 $params = array(
91 'name' => '60+ Membership',
92 'description' => 'people above 60 are given health instructions',
3d6a42e8 93 'financial_type_id' => 1,
6a488035
TO
94 'domain_id' => '1',
95 'minimum_fee' => '200',
96 'duration_unit' => 'month',
97 'duration_interval' => '10',
98 'period_type' => 'rolling',
99 'visibility' => 'public',
6a488035
TO
100 );
101
80d714d2 102 $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id');
6a488035
TO
103 }
104
5f1108c8 105 /**
106 * Test successful create.
107 */
00be9182 108 public function testCreate() {
6a488035
TO
109 $params = array(
110 'name' => '40+ Membership',
111 'description' => 'people above 40 are given health instructions',
112 'member_of_contact_id' => $this->_contactID,
3d6a42e8 113 'financial_type_id' => 1,
6a488035
TO
114 'domain_id' => '1',
115 'minimum_fee' => '200',
116 'duration_unit' => 'month',
117 'duration_interval' => '10',
118 'period_type' => 'rolling',
119 'visibility' => 'public',
6a488035
TO
120 );
121
edc9b94e
EM
122 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
123 $this->assertNotNull($membershipType['values']);
124 $this->membershipTypeDelete(array('id' => $membershipType['id']));
6a488035
TO
125 }
126
edc9b94e
EM
127 /**
128 * Test update fails with no ID.
129 */
00be9182 130 public function testUpdateWithoutId() {
6a488035
TO
131 $params = array(
132 'name' => '60+ Membership',
133 'description' => 'people above 60 are given health instructions',
134 'member_of_contact_id' => $this->_contactID,
3d6a42e8 135 'financial_type_id' => 1,
6a488035
TO
136 'minimum_fee' => '1200',
137 'duration_unit' => 'month',
138 'duration_interval' => '10',
139 'period_type' => 'rolling',
92915c55
TO
140 'visibility' => 'public',
141 );
6a488035 142
edc9b94e
EM
143 $membershipType = $this->callAPIFailure('membership_type', 'create', $params);
144 $this->assertEquals($membershipType['error_message'], 'Mandatory key(s) missing from params array: domain_id');
6a488035
TO
145 }
146
5f1108c8 147 /**
148 * Test update.
149 */
00be9182 150 public function testUpdate() {
75638074 151 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
5f1108c8 152 $newMemberOrgParams = array(
6a488035
TO
153 'organization_name' => 'New membership organisation',
154 'contact_type' => 'Organization',
92915c55
TO
155 'visibility' => 1,
156 );
6a488035 157
6a488035
TO
158 $params = array(
159 'id' => $id,
160 'name' => 'Updated General',
5f1108c8 161 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams),
6a488035
TO
162 'duration_unit' => 'month',
163 'duration_interval' => '10',
164 'period_type' => 'fixed',
92915c55
TO
165 'domain_id' => 1,
166 );
3d6a42e8 167
d63167fc 168 $this->callAPISuccess('membership_type', 'update', $params);
6a488035
TO
169
170 $this->getAndCheck($params, $id, $this->_entity);
171 }
172
5f1108c8 173 /**
174 * Test successful delete.
175 */
00be9182 176 public function testDelete() {
5f1108c8 177 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->organizationCreate()));
3d6a42e8 178 $params = array(
6a488035 179 'id' => $membershipTypeID,
6a488035
TO
180 );
181
edc9b94e 182 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
6a488035 183 }
96025800 184
5f1108c8 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 */
baccd59e
CW
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
35dfd73b 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
6a488035 238}