Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-22-22-01-44
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTypeTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2
3/*
4 +--------------------------------------------------------------------+
06a1bc01 5| CiviCRM version 4.5 |
b6708aeb 6+--------------------------------------------------------------------+
06a1bc01 7| Copyright CiviCRM LLC (c) 2004-2014 |
b6708aeb 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*/
6a488035
TO
28
29/*
30 +--------------------------------------------------------------------+
06a1bc01 31 | CiviCRM version 4.5 |
6a488035 32 +--------------------------------------------------------------------+
06a1bc01 33 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
55require_once 'CiviTest/CiviUnitTestCase.php';
56
57/**
58 * Class contains api test cases for "civicrm_relationship_type"
59 *
60 */
61class api_v3_RelationshipTypeTest extends CiviUnitTestCase {
62 protected $_cId_a;
63 protected $_cId_b;
64 protected $_relTypeID;
b2402735 65 protected $_apiversion = 3;
b7c9bc4c 66
4cbe18b8
EM
67 /**
68 * @return array
69 */
6a488035
TO
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();
e4d5f1e2 81 $this->_cId_a = $this->individualCreate();
82 $this->_cId_b = $this->organizationCreate();
6a488035
TO
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 /**
100fef9d 97 * Check with no name
6a488035
TO
98 */
99 function testRelationshipTypeCreateWithoutName() {
100 $relTypeParams = array(
101 'contact_type_a' => 'Individual',
102 'contact_type_b' => 'Organization',
6a488035 103 );
7fbb4198 104 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
6a488035
TO
105 'Mandatory key(s) missing from params array: name_a_b, name_b_a'
106 );
107 }
108
109 /**
100fef9d 110 * Check with no contact type
6a488035
TO
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',
6a488035 116 );
7fbb4198 117 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
6a488035
TO
118 'Mandatory key(s) missing from params array: contact_type_a, contact_type_b'
119 );
120 }
121
122 /**
100fef9d 123 * Create relationship type
6a488035
TO
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,
6a488035
TO
133 'sequential' => 1,
134 );
7fbb4198 135 $result = $this->callAPIAndDocument('relationship_type', 'create', $params, __FUNCTION__, __FILE__);
6a488035 136 $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
6a488035
TO
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() {
3ec6e38d 146 require_once 'api/v3/examples/RelationshipType/Create.php';
6a488035
TO
147 $result = relationship_type_create_example();
148 $expectedResult = relationship_type_create_expectedresult();
791c263c 149 $this->assertAPISuccess($result);
6a488035
TO
150 }
151
6a488035 152 /**
100fef9d 153 * Check if required fields are not passed
6a488035
TO
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
d0e1eff2
CW
163 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
164 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
6a488035
TO
165 }
166
167 /**
100fef9d 168 * Check with incorrect required fields
6a488035
TO
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,
6a488035 179 );
b2402735 180 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
181 'Invalid value for relationship type ID'
182 );
6a488035
TO
183 }
184
185 /**
100fef9d 186 * Check relationship type delete
6a488035
TO
187 */
188 function testRelationshipTypeDelete() {
b2402735 189 $id = $this->_relationshipTypeCreate();
6a488035
TO
190 // create sample relationship type.
191 $params = array(
b2402735 192 'id' => $id,
6a488035 193 );
b2402735 194 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__, __FILE__);
195 $this->assertAPIDeleted('relationship_type', $id);
6a488035
TO
196 }
197
198 ///////////////// civicrm_relationship_type_update
199
200 /**
100fef9d 201 * Check with empty array
6a488035
TO
202 */
203 function testRelationshipTypeUpdateEmpty() {
204 $params = array();
d0e1eff2
CW
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');
6a488035
TO
207 }
208
6a488035 209 /**
100fef9d 210 * Check with no contact type
6a488035
TO
211 */
212 function testRelationshipTypeUpdateWithoutContactType() {
213 // create sample relationship type.
75638074 214 $this->_relTypeID = $this->_relationshipTypeCreate();
6a488035
TO
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,
6a488035
TO
223 );
224
b2402735 225 $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams);
6a488035 226 $this->assertNotNull($result['id']);
6a488035
TO
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 /**
100fef9d 232 * Check with all parameters
6a488035
TO
233 */
234 function testRelationshipTypeUpdate() {
235 // create sample relationship type.
75638074 236 $this->_relTypeID = $this->_relationshipTypeCreate();
6a488035
TO
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,
6a488035
TO
247 );
248
7fbb4198 249 $result = $this->callAPISuccess('relationship_type', 'create', $params);
6a488035 250 $this->assertNotNull($result['id']);
7fbb4198 251
6a488035
TO
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 /**
100fef9d 259 * Check with empty array
6a488035
TO
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,
6a488035
TO
270 );
271
7fbb4198 272 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
6a488035
TO
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,
6a488035 282 );
7fbb4198 283 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
284 $results = $this->callAPISuccess('relationship_type', 'get', array());
6a488035
TO
285
286 $this->assertEquals(2, $results['count']);
6a488035
TO
287 }
288
289 /**
100fef9d 290 * Check with params Not Array.
6a488035
TO
291 */
292 function testRelationshipTypesGetParamsNotArray() {
293
d0e1eff2 294 $results = $this->callAPIFailure('relationship_type', 'get', 'string');
6a488035
TO
295 }
296
297 /**
100fef9d 298 * Check with valid params array.
6a488035
TO
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,
6a488035
TO
309 );
310
7fbb4198 311 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
6a488035
TO
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,
6a488035 321 );
7fbb4198 322 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
6a488035
TO
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',
6a488035 328 );
7fbb4198 329 $results = $this->callAPISuccess('relationship_type', 'get', $params);
6a488035 330
6a488035
TO
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 /**
100fef9d 336 * Create relationship type.
6a488035
TO
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,
6a488035
TO
348 );
349 }
350
351 return $this->relationshipTypeCreate($params);
352 }
353}
354