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