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