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