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