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