Merge pull request #4764 from rohankatkar/CRM-15615
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTypeTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /*
30 +--------------------------------------------------------------------+
31 | CiviCRM version 4.5 |
32 +--------------------------------------------------------------------+
33 | Copyright CiviCRM LLC (c) 2004-2014 |
34 +--------------------------------------------------------------------+
35 | This file is a part of CiviCRM. |
36 | |
37 | CiviCRM is free software; you can copy, modify, and distribute it |
38 | under the terms of the GNU Affero General Public License |
39 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
40 | |
41 | CiviCRM is distributed in the hope that it will be useful, but |
42 | WITHOUT ANY WARRANTY; without even the implied warranty of |
43 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
44 | See the GNU Affero General Public License for more details. |
45 | |
46 | You should have received a copy of the GNU Affero General Public |
47 | License and the CiviCRM Licensing Exception along |
48 | with this program; if not, contact CiviCRM LLC |
49 | at info[AT]civicrm[DOT]org. If you have questions about the |
50 | GNU Affero General Public License or the licensing of CiviCRM, |
51 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
52 +--------------------------------------------------------------------+
53 */
54
55 require_once 'CiviTest/CiviUnitTestCase.php';
56
57 /**
58 * Class contains api test cases for "civicrm_relationship_type"
59 *
60 */
61 class api_v3_RelationshipTypeTest extends CiviUnitTestCase {
62 protected $_cId_a;
63 protected $_cId_b;
64 protected $_relTypeID;
65 protected $_apiversion = 3;
66
67 /**
68 * @return array
69 */
70 function get_info() {
71 return array(
72 'name' => 'RelationshipType Create',
73 'description' => 'Test all RelationshipType Create API methods.',
74 'group' => 'CiviCRM API Tests',
75 );
76 }
77
78 function setUp() {
79
80 parent::setUp();
81 $this->_cId_a = $this->individualCreate();
82 $this->_cId_b = $this->organizationCreate();
83 }
84
85 function tearDown() {
86
87 $tablesToTruncate = array(
88 'civicrm_contact',
89 'civicrm_relationship_type',
90 );
91 $this->quickCleanup($tablesToTruncate);
92 }
93
94 ///////////////// civicrm_relationship_type_add methods
95
96 /**
97 * Check with no name
98 */
99 function testRelationshipTypeCreateWithoutName() {
100 $relTypeParams = array(
101 'contact_type_a' => 'Individual',
102 'contact_type_b' => 'Organization',
103 );
104 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
105 'Mandatory key(s) missing from params array: name_a_b, name_b_a'
106 );
107 }
108
109 /**
110 * Check with no contact type
111 */
112 function testRelationshipTypeCreateWithoutContactType() {
113 $relTypeParams = array(
114 'name_a_b' => 'Relation 1 without contact type',
115 'name_b_a' => 'Relation 2 without contact type',
116 );
117 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
118 'Mandatory key(s) missing from params array: contact_type_a, contact_type_b'
119 );
120 }
121
122 /**
123 * Create relationship type
124 */
125 function testRelationshipTypeCreate() {
126 $params = array(
127 'name_a_b' => 'Relation 1 for relationship type create',
128 'name_b_a' => 'Relation 2 for relationship type create',
129 'contact_type_a' => 'Individual',
130 'contact_type_b' => 'Organization',
131 'is_reserved' => 1,
132 'is_active' => 1,
133 'sequential' => 1,
134 );
135 $result = $this->callAPIAndDocument('relationship_type', 'create', $params, __FUNCTION__, __FILE__);
136 $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
137 unset($params['sequential']);
138 //assertDBState compares expected values in $result to actual values in the DB
139 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
140 }
141
142 /**
143 * Test using example code
144 */
145 function testRelationshipTypeCreateExample() {
146 require_once 'api/v3/examples/RelationshipType/Create.php';
147 $result = relationship_type_create_example();
148 $expectedResult = relationship_type_create_expectedresult();
149 $this->assertAPISuccess($result);
150 }
151
152 /**
153 * Check if required fields are not passed
154 */
155 function testRelationshipTypeDeleteWithoutRequired() {
156 $params = array(
157 'name_b_a' => 'Relation 2 delete without required',
158 'contact_type_b' => 'Individual',
159 'is_reserved' => 0,
160 'is_active' => 0,
161 );
162
163 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
164 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
165 }
166
167 /**
168 * Check with incorrect required fields
169 */
170 function testRelationshipTypeDeleteWithIncorrectData() {
171 $params = array(
172 'id' => 'abcd',
173 'name_b_a' => 'Relation 2 delete with incorrect',
174 'description' => 'Testing relationship type',
175 'contact_type_a' => 'Individual',
176 'contact_type_b' => 'Individual',
177 'is_reserved' => 0,
178 'is_active' => 0,
179 );
180 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
181 'Invalid value for relationship type ID'
182 );
183 }
184
185 /**
186 * Check relationship type delete
187 */
188 function testRelationshipTypeDelete() {
189 $id = $this->_relationshipTypeCreate();
190 // create sample relationship type.
191 $params = array(
192 'id' => $id,
193 );
194 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__, __FILE__);
195 $this->assertAPIDeleted('relationship_type', $id);
196 }
197
198 ///////////////// civicrm_relationship_type_update
199
200 /**
201 * Check with empty array
202 */
203 function testRelationshipTypeUpdateEmpty() {
204 $params = array();
205 $result = $this->callAPIFailure('relationship_type', 'create', $params);
206 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: name_a_b, name_b_a, contact_type_a, contact_type_b');
207 }
208
209 /**
210 * Check with no contact type
211 */
212 function testRelationshipTypeUpdateWithoutContactType() {
213 // create sample relationship type.
214 $this->_relTypeID = $this->_relationshipTypeCreate();
215
216 $relTypeParams = array(
217 'id' => $this->_relTypeID,
218 'name_a_b' => 'Test 1',
219 'name_b_a' => 'Test 2',
220 'description' => 'Testing relationship type',
221 'is_reserved' => 1,
222 'is_active' => 0,
223 );
224
225 $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams);
226 $this->assertNotNull($result['id']);
227 // assertDBState compares expected values in $result to actual values in the DB
228 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $relTypeParams);
229 }
230
231 /**
232 * Check with all parameters
233 */
234 function testRelationshipTypeUpdate() {
235 // create sample relationship type.
236 $this->_relTypeID = $this->_relationshipTypeCreate();
237
238 $params = array(
239 'id' => $this->_relTypeID,
240 'name_a_b' => 'Test 1 for update',
241 'name_b_a' => 'Test 2 for update',
242 'description' => 'SUNIL PAWAR relationship type',
243 'contact_type_a' => 'Individual',
244 'contact_type_b' => 'Individual',
245 'is_reserved' => 0,
246 'is_active' => 0,
247 );
248
249 $result = $this->callAPISuccess('relationship_type', 'create', $params);
250 $this->assertNotNull($result['id']);
251
252 // assertDBState compares expected values in $result to actual values in the DB
253 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
254 }
255
256 ///////////////// civicrm_relationship_types_get methods
257
258 /**
259 * Check with empty array
260 */
261 function testRelationshipTypesGetEmptyParams() {
262 $firstRelTypeParams = array(
263 'name_a_b' => 'Relation 27 for create',
264 'name_b_a' => 'Relation 28 for create',
265 'description' => 'Testing relationship type',
266 'contact_type_a' => 'Individual',
267 'contact_type_b' => 'Organization',
268 'is_reserved' => 1,
269 'is_active' => 1,
270 );
271
272 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
273
274 $secondRelTypeParams = array(
275 'name_a_b' => 'Relation 25 for create',
276 'name_b_a' => 'Relation 26 for create',
277 'description' => 'Testing relationship type second',
278 'contact_type_a' => 'Individual',
279 'contact_type_b' => 'Organization',
280 'is_reserved' => 0,
281 'is_active' => 1,
282 );
283 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
284 $results = $this->callAPISuccess('relationship_type', 'get', array());
285
286 $this->assertEquals(2, $results['count']);
287 }
288
289 /**
290 * Check with params Not Array.
291 */
292 function testRelationshipTypesGetParamsNotArray() {
293
294 $results = $this->callAPIFailure('relationship_type', 'get', 'string');
295 }
296
297 /**
298 * Check with valid params array.
299 */
300 function testRelationshipTypesGet() {
301 $firstRelTypeParams = array(
302 'name_a_b' => 'Relation 30 for create',
303 'name_b_a' => 'Relation 31 for create',
304 'description' => 'Testing relationship type',
305 'contact_type_a' => 'Individual',
306 'contact_type_b' => 'Organization',
307 'is_reserved' => 1,
308 'is_active' => 1,
309 );
310
311 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
312
313 $secondRelTypeParams = array(
314 'name_a_b' => 'Relation 32 for create',
315 'name_b_a' => 'Relation 33 for create',
316 'description' => 'Testing relationship type second',
317 'contact_type_a' => 'Individual',
318 'contact_type_b' => 'Organization',
319 'is_reserved' => 0,
320 'is_active' => 1,
321 );
322 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
323
324 $params = array(
325 'name_a_b' => 'Relation 32 for create',
326 'name_b_a' => 'Relation 33 for create',
327 'description' => 'Testing relationship type second',
328 );
329 $results = $this->callAPISuccess('relationship_type', 'get', $params);
330
331 $this->assertEquals(1, $results['count'], ' in line ' . __LINE__);
332 $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__);
333 }
334
335 /**
336 * Create relationship type.
337 */
338 function _relationshipTypeCreate($params = NULL) {
339 if (!is_array($params) || empty($params)) {
340 $params = array(
341 'name_a_b' => 'Relation 1 for create',
342 'name_b_a' => 'Relation 2 for create',
343 'description' => 'Testing relationship type',
344 'contact_type_a' => 'Individual',
345 'contact_type_b' => 'Organization',
346 'is_reserved' => 1,
347 'is_active' => 1,
348 );
349 }
350
351 return $this->relationshipTypeCreate($params);
352 }
353 }
354