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