INFRA-132 - settings/ - phpcbf
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTypeTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2
3/*
4 +--------------------------------------------------------------------+
39de6fd5 5| CiviCRM version 4.6 |
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 +--------------------------------------------------------------------+
39de6fd5 31 | CiviCRM version 4.6 |
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
00be9182 67 public function setUp() {
6a488035
TO
68
69 parent::setUp();
e4d5f1e2 70 $this->_cId_a = $this->individualCreate();
71 $this->_cId_b = $this->organizationCreate();
6a488035
TO
72 }
73
00be9182 74 public function tearDown() {
6a488035
TO
75
76 $tablesToTruncate = array(
77 'civicrm_contact',
78 'civicrm_relationship_type',
79 );
80 $this->quickCleanup($tablesToTruncate);
81 }
82
83 ///////////////// civicrm_relationship_type_add methods
84
85 /**
100fef9d 86 * Check with no name
6a488035 87 */
00be9182 88 public function testRelationshipTypeCreateWithoutName() {
6a488035
TO
89 $relTypeParams = array(
90 'contact_type_a' => 'Individual',
91 'contact_type_b' => 'Organization',
6a488035 92 );
7fbb4198 93 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
6a488035
TO
94 'Mandatory key(s) missing from params array: name_a_b, name_b_a'
95 );
96 }
97
98 /**
100fef9d 99 * Check with no contact type
6a488035 100 */
00be9182 101 public function testRelationshipTypeCreateWithoutContactType() {
6a488035
TO
102 $relTypeParams = array(
103 'name_a_b' => 'Relation 1 without contact type',
104 'name_b_a' => 'Relation 2 without contact type',
6a488035 105 );
7fbb4198 106 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
6a488035
TO
107 'Mandatory key(s) missing from params array: contact_type_a, contact_type_b'
108 );
109 }
110
111 /**
100fef9d 112 * Create relationship type
6a488035 113 */
00be9182 114 public function testRelationshipTypeCreate() {
6a488035
TO
115 $params = array(
116 'name_a_b' => 'Relation 1 for relationship type create',
117 'name_b_a' => 'Relation 2 for relationship type create',
118 'contact_type_a' => 'Individual',
119 'contact_type_b' => 'Organization',
120 'is_reserved' => 1,
121 'is_active' => 1,
6a488035
TO
122 'sequential' => 1,
123 );
7fbb4198 124 $result = $this->callAPIAndDocument('relationship_type', 'create', $params, __FUNCTION__, __FILE__);
6a488035 125 $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
6a488035
TO
126 unset($params['sequential']);
127 //assertDBState compares expected values in $result to actual values in the DB
128 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
129 }
130
131 /**
132 * Test using example code
133 */
00be9182 134 public function testRelationshipTypeCreateExample() {
3ec6e38d 135 require_once 'api/v3/examples/RelationshipType/Create.php';
6a488035
TO
136 $result = relationship_type_create_example();
137 $expectedResult = relationship_type_create_expectedresult();
791c263c 138 $this->assertAPISuccess($result);
6a488035
TO
139 }
140
6a488035 141 /**
100fef9d 142 * Check if required fields are not passed
6a488035 143 */
00be9182 144 public function testRelationshipTypeDeleteWithoutRequired() {
6a488035
TO
145 $params = array(
146 'name_b_a' => 'Relation 2 delete without required',
147 'contact_type_b' => 'Individual',
148 'is_reserved' => 0,
149 'is_active' => 0,
150 );
151
d0e1eff2
CW
152 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
153 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
6a488035
TO
154 }
155
156 /**
100fef9d 157 * Check with incorrect required fields
6a488035 158 */
00be9182 159 public function testRelationshipTypeDeleteWithIncorrectData() {
6a488035
TO
160 $params = array(
161 'id' => 'abcd',
162 'name_b_a' => 'Relation 2 delete with incorrect',
163 'description' => 'Testing relationship type',
164 'contact_type_a' => 'Individual',
165 'contact_type_b' => 'Individual',
166 'is_reserved' => 0,
167 'is_active' => 0,
6a488035 168 );
b2402735 169 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
170 'Invalid value for relationship type ID'
171 );
6a488035
TO
172 }
173
174 /**
100fef9d 175 * Check relationship type delete
6a488035 176 */
00be9182 177 public function testRelationshipTypeDelete() {
b2402735 178 $id = $this->_relationshipTypeCreate();
6a488035
TO
179 // create sample relationship type.
180 $params = array(
b2402735 181 'id' => $id,
6a488035 182 );
b2402735 183 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__, __FILE__);
184 $this->assertAPIDeleted('relationship_type', $id);
6a488035
TO
185 }
186
187 ///////////////// civicrm_relationship_type_update
188
189 /**
100fef9d 190 * Check with empty array
6a488035 191 */
00be9182 192 public function testRelationshipTypeUpdateEmpty() {
6a488035 193 $params = array();
d0e1eff2
CW
194 $result = $this->callAPIFailure('relationship_type', 'create', $params);
195 $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
196 }
197
6a488035 198 /**
100fef9d 199 * Check with no contact type
6a488035 200 */
00be9182 201 public function testRelationshipTypeUpdateWithoutContactType() {
6a488035 202 // create sample relationship type.
75638074 203 $this->_relTypeID = $this->_relationshipTypeCreate();
6a488035
TO
204
205 $relTypeParams = array(
206 'id' => $this->_relTypeID,
207 'name_a_b' => 'Test 1',
208 'name_b_a' => 'Test 2',
209 'description' => 'Testing relationship type',
210 'is_reserved' => 1,
211 'is_active' => 0,
6a488035
TO
212 );
213
b2402735 214 $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams);
6a488035 215 $this->assertNotNull($result['id']);
6a488035
TO
216 // assertDBState compares expected values in $result to actual values in the DB
217 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $relTypeParams);
218 }
219
220 /**
100fef9d 221 * Check with all parameters
6a488035 222 */
00be9182 223 public function testRelationshipTypeUpdate() {
6a488035 224 // create sample relationship type.
75638074 225 $this->_relTypeID = $this->_relationshipTypeCreate();
6a488035
TO
226
227 $params = array(
228 'id' => $this->_relTypeID,
229 'name_a_b' => 'Test 1 for update',
230 'name_b_a' => 'Test 2 for update',
231 'description' => 'SUNIL PAWAR relationship type',
232 'contact_type_a' => 'Individual',
233 'contact_type_b' => 'Individual',
234 'is_reserved' => 0,
235 'is_active' => 0,
6a488035
TO
236 );
237
7fbb4198 238 $result = $this->callAPISuccess('relationship_type', 'create', $params);
6a488035 239 $this->assertNotNull($result['id']);
7fbb4198 240
6a488035
TO
241 // assertDBState compares expected values in $result to actual values in the DB
242 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
243 }
244
245 ///////////////// civicrm_relationship_types_get methods
246
247 /**
100fef9d 248 * Check with empty array
6a488035 249 */
00be9182 250 public function testRelationshipTypesGetEmptyParams() {
6a488035
TO
251 $firstRelTypeParams = array(
252 'name_a_b' => 'Relation 27 for create',
253 'name_b_a' => 'Relation 28 for create',
254 'description' => 'Testing relationship type',
255 'contact_type_a' => 'Individual',
256 'contact_type_b' => 'Organization',
257 'is_reserved' => 1,
258 'is_active' => 1,
6a488035
TO
259 );
260
7fbb4198 261 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
6a488035
TO
262
263 $secondRelTypeParams = array(
264 'name_a_b' => 'Relation 25 for create',
265 'name_b_a' => 'Relation 26 for create',
266 'description' => 'Testing relationship type second',
267 'contact_type_a' => 'Individual',
268 'contact_type_b' => 'Organization',
269 'is_reserved' => 0,
270 'is_active' => 1,
6a488035 271 );
7fbb4198 272 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
273 $results = $this->callAPISuccess('relationship_type', 'get', array());
6a488035
TO
274
275 $this->assertEquals(2, $results['count']);
6a488035
TO
276 }
277
278 /**
100fef9d 279 * Check with params Not Array.
6a488035 280 */
00be9182 281 public function testRelationshipTypesGetParamsNotArray() {
6a488035 282
d0e1eff2 283 $results = $this->callAPIFailure('relationship_type', 'get', 'string');
6a488035
TO
284 }
285
286 /**
100fef9d 287 * Check with valid params array.
6a488035 288 */
00be9182 289 public function testRelationshipTypesGet() {
6a488035
TO
290 $firstRelTypeParams = array(
291 'name_a_b' => 'Relation 30 for create',
292 'name_b_a' => 'Relation 31 for create',
293 'description' => 'Testing relationship type',
294 'contact_type_a' => 'Individual',
295 'contact_type_b' => 'Organization',
296 'is_reserved' => 1,
297 'is_active' => 1,
6a488035
TO
298 );
299
7fbb4198 300 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
6a488035
TO
301
302 $secondRelTypeParams = array(
303 'name_a_b' => 'Relation 32 for create',
304 'name_b_a' => 'Relation 33 for create',
305 'description' => 'Testing relationship type second',
306 'contact_type_a' => 'Individual',
307 'contact_type_b' => 'Organization',
308 'is_reserved' => 0,
309 'is_active' => 1,
6a488035 310 );
7fbb4198 311 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
6a488035
TO
312
313 $params = array(
314 'name_a_b' => 'Relation 32 for create',
315 'name_b_a' => 'Relation 33 for create',
316 'description' => 'Testing relationship type second',
6a488035 317 );
7fbb4198 318 $results = $this->callAPISuccess('relationship_type', 'get', $params);
6a488035 319
6a488035
TO
320 $this->assertEquals(1, $results['count'], ' in line ' . __LINE__);
321 $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__);
322 }
323
324 /**
100fef9d 325 * Create relationship type.
6a488035 326 */
00be9182 327 public function _relationshipTypeCreate($params = NULL) {
6a488035
TO
328 if (!is_array($params) || empty($params)) {
329 $params = array(
330 'name_a_b' => 'Relation 1 for create',
331 'name_b_a' => 'Relation 2 for create',
332 'description' => 'Testing relationship type',
333 'contact_type_a' => 'Individual',
334 'contact_type_b' => 'Organization',
335 'is_reserved' => 1,
336 'is_active' => 1,
6a488035
TO
337 );
338 }
339
340 return $this->relationshipTypeCreate($params);
341 }
342}