Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTypeTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
6a488035
TO
12/**
13 * Class contains api test cases for "civicrm_relationship_type"
14 *
acb109b7 15 * @group headless
6a488035
TO
16 */
17class api_v3_RelationshipTypeTest extends CiviUnitTestCase {
18 protected $_cId_a;
19 protected $_cId_b;
20 protected $_relTypeID;
b2402735 21 protected $_apiversion = 3;
b7c9bc4c 22
00be9182 23 public function setUp() {
6a488035
TO
24
25 parent::setUp();
92915c55
TO
26 $this->_cId_a = $this->individualCreate();
27 $this->_cId_b = $this->organizationCreate();
6a488035
TO
28 }
29
00be9182 30 public function tearDown() {
6a488035 31
9099cab3 32 $tablesToTruncate = [
6a488035
TO
33 'civicrm_contact',
34 'civicrm_relationship_type',
9099cab3 35 ];
6a488035
TO
36 $this->quickCleanup($tablesToTruncate);
37 }
38
39 ///////////////// civicrm_relationship_type_add methods
40
41 /**
eceb18cc 42 * Check with no name.
2d932085
CW
43 * @param int $version
44 * @dataProvider versionThreeAndFour
6a488035 45 */
2d932085
CW
46 public function testRelationshipTypeCreateWithoutName($version) {
47 $this->_apiversion = $version;
9099cab3 48 $relTypeParams = [
6a488035
TO
49 'contact_type_a' => 'Individual',
50 'contact_type_b' => 'Organization',
9099cab3 51 ];
2d932085 52 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams);
6a488035
TO
53 }
54
6a488035 55 /**
eceb18cc 56 * Create relationship type.
2d932085
CW
57 * @param int $version
58 * @dataProvider versionThreeAndFour
6a488035 59 */
2d932085
CW
60 public function testRelationshipTypeCreate($version) {
61 $this->_apiversion = $version;
9099cab3 62 $params = [
6a488035
TO
63 'name_a_b' => 'Relation 1 for relationship type create',
64 'name_b_a' => 'Relation 2 for relationship type create',
65 'contact_type_a' => 'Individual',
66 'contact_type_b' => 'Organization',
67 'is_reserved' => 1,
68 'is_active' => 1,
6a488035 69 'sequential' => 1,
9099cab3 70 ];
7fbb4198 71 $result = $this->callAPIAndDocument('relationship_type', 'create', $params, __FUNCTION__, __FILE__);
ba4a1892 72 $this->assertNotNull($result['values'][0]['id']);
6a488035
TO
73 unset($params['sequential']);
74 //assertDBState compares expected values in $result to actual values in the DB
75 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
76 }
77
78 /**
eceb18cc 79 * Test using example code.
2d932085
CW
80 * @param int $version
81 * @dataProvider versionThreeAndFour
6a488035 82 */
2d932085
CW
83 public function testRelationshipTypeCreateExample($version) {
84 $this->_apiversion = $version;
be44cfcb 85 require_once 'api/v3/examples/RelationshipType/Create.ex.php';
6a488035
TO
86 $result = relationship_type_create_example();
87 $expectedResult = relationship_type_create_expectedresult();
791c263c 88 $this->assertAPISuccess($result);
6a488035
TO
89 }
90
6a488035 91 /**
eceb18cc 92 * Check if required fields are not passed.
2d932085
CW
93 * @param int $version
94 * @dataProvider versionThreeAndFour
6a488035 95 */
2d932085
CW
96 public function testRelationshipTypeDeleteWithoutRequired($version) {
97 $this->_apiversion = $version;
9099cab3 98 $params = [
6a488035
TO
99 'name_b_a' => 'Relation 2 delete without required',
100 'contact_type_b' => 'Individual',
101 'is_reserved' => 0,
102 'is_active' => 0,
9099cab3 103 ];
6a488035 104
d0e1eff2 105 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
2d932085
CW
106 if ($version == 3) {
107 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
108 }
109 else {
110 $this->assertEquals($result['error_message'], 'Parameter "where" is required.');
111 }
6a488035
TO
112 }
113
114 /**
eceb18cc 115 * Check with incorrect required fields.
6a488035 116 */
00be9182 117 public function testRelationshipTypeDeleteWithIncorrectData() {
9099cab3 118 $params = [
6a488035
TO
119 'id' => 'abcd',
120 'name_b_a' => 'Relation 2 delete with incorrect',
121 'description' => 'Testing relationship type',
122 'contact_type_a' => 'Individual',
123 'contact_type_b' => 'Individual',
124 'is_reserved' => 0,
125 'is_active' => 0,
9099cab3 126 ];
b2402735 127 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
4f94e3fa 128 'id is not a valid integer'
b2402735 129 );
6a488035
TO
130 }
131
132 /**
eceb18cc 133 * Check relationship type delete.
2d932085
CW
134 * @param int $version
135 * @dataProvider versionThreeAndFour
6a488035 136 */
2d932085
CW
137 public function testRelationshipTypeDelete($version) {
138 $this->_apiversion = $version;
b2402735 139 $id = $this->_relationshipTypeCreate();
6a488035 140 // create sample relationship type.
9099cab3 141 $params = [
6c6e6187 142 'id' => $id,
9099cab3 143 ];
b2402735 144 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__, __FILE__);
145 $this->assertAPIDeleted('relationship_type', $id);
6a488035
TO
146 }
147
148 ///////////////// civicrm_relationship_type_update
149
150 /**
eceb18cc 151 * Check with empty array.
2d932085
CW
152 * @param int $version
153 * @dataProvider versionThreeAndFour
6a488035 154 */
2d932085
CW
155 public function testRelationshipTypeUpdateEmpty($version) {
156 $this->_apiversion = $version;
9099cab3 157 $params = [];
d0e1eff2 158 $result = $this->callAPIFailure('relationship_type', 'create', $params);
2d932085
CW
159 $this->assertContains('name_a_b', $result['error_message']);
160 $this->assertContains('name_b_a', $result['error_message']);
6a488035
TO
161 }
162
6a488035 163 /**
eceb18cc 164 * Check with no contact type.
2d932085
CW
165 * @param int $version
166 * @dataProvider versionThreeAndFour
6a488035 167 */
2d932085
CW
168 public function testRelationshipTypeUpdateWithoutContactType($version) {
169 $this->_apiversion = $version;
6a488035 170 // create sample relationship type.
75638074 171 $this->_relTypeID = $this->_relationshipTypeCreate();
6a488035 172
9099cab3 173 $relTypeParams = [
6a488035
TO
174 'id' => $this->_relTypeID,
175 'name_a_b' => 'Test 1',
176 'name_b_a' => 'Test 2',
177 'description' => 'Testing relationship type',
178 'is_reserved' => 1,
179 'is_active' => 0,
9099cab3 180 ];
6a488035 181
b2402735 182 $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams);
6a488035 183 $this->assertNotNull($result['id']);
6a488035
TO
184 // assertDBState compares expected values in $result to actual values in the DB
185 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $relTypeParams);
186 }
187
188 /**
eceb18cc 189 * Check with all parameters.
2d932085
CW
190 * @param int $version
191 * @dataProvider versionThreeAndFour
6a488035 192 */
2d932085
CW
193 public function testRelationshipTypeUpdate($version) {
194 $this->_apiversion = $version;
6a488035 195 // create sample relationship type.
75638074 196 $this->_relTypeID = $this->_relationshipTypeCreate();
6a488035 197
9099cab3 198 $params = [
6a488035
TO
199 'id' => $this->_relTypeID,
200 'name_a_b' => 'Test 1 for update',
201 'name_b_a' => 'Test 2 for update',
202 'description' => 'SUNIL PAWAR relationship type',
203 'contact_type_a' => 'Individual',
204 'contact_type_b' => 'Individual',
205 'is_reserved' => 0,
206 'is_active' => 0,
9099cab3 207 ];
6a488035 208
7fbb4198 209 $result = $this->callAPISuccess('relationship_type', 'create', $params);
6a488035 210 $this->assertNotNull($result['id']);
7fbb4198 211
6a488035
TO
212 // assertDBState compares expected values in $result to actual values in the DB
213 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
214 }
215
216 ///////////////// civicrm_relationship_types_get methods
217
218 /**
eceb18cc 219 * Check with empty array.
2d932085
CW
220 * @param int $version
221 * @dataProvider versionThreeAndFour
6a488035 222 */
2d932085
CW
223 public function testRelationshipTypesGetEmptyParams($version) {
224 $this->_apiversion = $version;
9099cab3 225 $firstRelTypeParams = [
6a488035
TO
226 'name_a_b' => 'Relation 27 for create',
227 'name_b_a' => 'Relation 28 for create',
228 'description' => 'Testing relationship type',
229 'contact_type_a' => 'Individual',
230 'contact_type_b' => 'Organization',
231 'is_reserved' => 1,
232 'is_active' => 1,
9099cab3 233 ];
6a488035 234
7fbb4198 235 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
6a488035 236
9099cab3 237 $secondRelTypeParams = [
6a488035
TO
238 'name_a_b' => 'Relation 25 for create',
239 'name_b_a' => 'Relation 26 for create',
240 'description' => 'Testing relationship type second',
241 'contact_type_a' => 'Individual',
242 'contact_type_b' => 'Organization',
243 'is_reserved' => 0,
244 'is_active' => 1,
9099cab3 245 ];
7fbb4198 246 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
9099cab3 247 $results = $this->callAPISuccess('relationship_type', 'get', []);
6a488035
TO
248
249 $this->assertEquals(2, $results['count']);
6a488035
TO
250 }
251
6a488035 252 /**
100fef9d 253 * Check with valid params array.
2d932085
CW
254 * @param int $version
255 * @dataProvider versionThreeAndFour
6a488035 256 */
2d932085
CW
257 public function testRelationshipTypesGet($version) {
258 $this->_apiversion = $version;
9099cab3 259 $firstRelTypeParams = [
6a488035
TO
260 'name_a_b' => 'Relation 30 for create',
261 'name_b_a' => 'Relation 31 for create',
262 'description' => 'Testing relationship type',
263 'contact_type_a' => 'Individual',
264 'contact_type_b' => 'Organization',
265 'is_reserved' => 1,
266 'is_active' => 1,
9099cab3 267 ];
6a488035 268
7fbb4198 269 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
6a488035 270
9099cab3 271 $secondRelTypeParams = [
6a488035
TO
272 'name_a_b' => 'Relation 32 for create',
273 'name_b_a' => 'Relation 33 for create',
274 'description' => 'Testing relationship type second',
275 'contact_type_a' => 'Individual',
276 'contact_type_b' => 'Organization',
277 'is_reserved' => 0,
278 'is_active' => 1,
9099cab3 279 ];
7fbb4198 280 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
6a488035 281
9099cab3 282 $params = [
6a488035
TO
283 'name_a_b' => 'Relation 32 for create',
284 'name_b_a' => 'Relation 33 for create',
285 'description' => 'Testing relationship type second',
9099cab3 286 ];
7fbb4198 287 $results = $this->callAPISuccess('relationship_type', 'get', $params);
6a488035 288
6a488035
TO
289 $this->assertEquals(1, $results['count'], ' in line ' . __LINE__);
290 $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__);
291 }
292
293 /**
100fef9d 294 * Create relationship type.
1e1fdcf6
EM
295 * @param null $params
296 * @return mixed
6a488035 297 */
00be9182 298 public function _relationshipTypeCreate($params = NULL) {
6a488035 299 if (!is_array($params) || empty($params)) {
9099cab3 300 $params = [
6a488035
TO
301 'name_a_b' => 'Relation 1 for create',
302 'name_b_a' => 'Relation 2 for create',
303 'description' => 'Testing relationship type',
304 'contact_type_a' => 'Individual',
305 'contact_type_b' => 'Organization',
306 'is_reserved' => 1,
307 'is_active' => 1,
9099cab3 308 ];
6a488035
TO
309 }
310
311 return $this->relationshipTypeCreate($params);
312 }
96025800 313
6a488035 314}