Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class contains api test cases for "civicrm_relationship_type"
14 *
15 * @group headless
16 */
17 class api_v3_RelationshipTypeTest extends CiviUnitTestCase {
18 protected $_cId_a;
19 protected $_cId_b;
20 protected $_relTypeID;
21 protected $_apiversion = 3;
22
23 public function setUp() {
24
25 parent::setUp();
26 $this->_cId_a = $this->individualCreate();
27 $this->_cId_b = $this->organizationCreate();
28 }
29
30 public function tearDown() {
31
32 $tablesToTruncate = [
33 'civicrm_contact',
34 'civicrm_relationship_type',
35 ];
36 $this->quickCleanup($tablesToTruncate);
37 }
38
39 ///////////////// civicrm_relationship_type_add methods
40
41 /**
42 * Check with no name.
43 * @param int $version
44 * @dataProvider versionThreeAndFour
45 */
46 public function testRelationshipTypeCreateWithoutName($version) {
47 $this->_apiversion = $version;
48 $relTypeParams = [
49 'contact_type_a' => 'Individual',
50 'contact_type_b' => 'Organization',
51 ];
52 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams);
53 }
54
55 /**
56 * Create relationship type.
57 * @param int $version
58 * @dataProvider versionThreeAndFour
59 */
60 public function testRelationshipTypeCreate($version) {
61 $this->_apiversion = $version;
62 $params = [
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,
69 'sequential' => 1,
70 ];
71 $result = $this->callAPIAndDocument('relationship_type', 'create', $params, __FUNCTION__, __FILE__);
72 $this->assertNotNull($result['values'][0]['id']);
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 /**
79 * Test using example code.
80 * @param int $version
81 * @dataProvider versionThreeAndFour
82 */
83 public function testRelationshipTypeCreateExample($version) {
84 $this->_apiversion = $version;
85 require_once 'api/v3/examples/RelationshipType/Create.ex.php';
86 $result = relationship_type_create_example();
87 $expectedResult = relationship_type_create_expectedresult();
88 $this->assertAPISuccess($result);
89 }
90
91 /**
92 * Check if required fields are not passed.
93 * @param int $version
94 * @dataProvider versionThreeAndFour
95 */
96 public function testRelationshipTypeDeleteWithoutRequired($version) {
97 $this->_apiversion = $version;
98 $params = [
99 'name_b_a' => 'Relation 2 delete without required',
100 'contact_type_b' => 'Individual',
101 'is_reserved' => 0,
102 'is_active' => 0,
103 ];
104
105 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
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 }
112 }
113
114 /**
115 * Check with incorrect required fields.
116 */
117 public function testRelationshipTypeDeleteWithIncorrectData() {
118 $params = [
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,
126 ];
127 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
128 'id is not a valid integer'
129 );
130 }
131
132 /**
133 * Check relationship type delete.
134 * @param int $version
135 * @dataProvider versionThreeAndFour
136 */
137 public function testRelationshipTypeDelete($version) {
138 $this->_apiversion = $version;
139 $id = $this->_relationshipTypeCreate();
140 // create sample relationship type.
141 $params = [
142 'id' => $id,
143 ];
144 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__, __FILE__);
145 $this->assertAPIDeleted('relationship_type', $id);
146 }
147
148 ///////////////// civicrm_relationship_type_update
149
150 /**
151 * Check with empty array.
152 * @param int $version
153 * @dataProvider versionThreeAndFour
154 */
155 public function testRelationshipTypeUpdateEmpty($version) {
156 $this->_apiversion = $version;
157 $params = [];
158 $result = $this->callAPIFailure('relationship_type', 'create', $params);
159 $this->assertContains('name_a_b', $result['error_message']);
160 $this->assertContains('name_b_a', $result['error_message']);
161 }
162
163 /**
164 * Check with no contact type.
165 * @param int $version
166 * @dataProvider versionThreeAndFour
167 */
168 public function testRelationshipTypeUpdateWithoutContactType($version) {
169 $this->_apiversion = $version;
170 // create sample relationship type.
171 $this->_relTypeID = $this->_relationshipTypeCreate();
172
173 $relTypeParams = [
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,
180 ];
181
182 $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams);
183 $this->assertNotNull($result['id']);
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 /**
189 * Check with all parameters.
190 * @param int $version
191 * @dataProvider versionThreeAndFour
192 */
193 public function testRelationshipTypeUpdate($version) {
194 $this->_apiversion = $version;
195 // create sample relationship type.
196 $this->_relTypeID = $this->_relationshipTypeCreate();
197
198 $params = [
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,
207 ];
208
209 $result = $this->callAPISuccess('relationship_type', 'create', $params);
210 $this->assertNotNull($result['id']);
211
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 /**
219 * Check with empty array.
220 * @param int $version
221 * @dataProvider versionThreeAndFour
222 */
223 public function testRelationshipTypesGetEmptyParams($version) {
224 $this->_apiversion = $version;
225 $firstRelTypeParams = [
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,
233 ];
234
235 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
236
237 $secondRelTypeParams = [
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,
245 ];
246 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
247 $results = $this->callAPISuccess('relationship_type', 'get', []);
248
249 $this->assertEquals(2, $results['count']);
250 }
251
252 /**
253 * Check with valid params array.
254 * @param int $version
255 * @dataProvider versionThreeAndFour
256 */
257 public function testRelationshipTypesGet($version) {
258 $this->_apiversion = $version;
259 $firstRelTypeParams = [
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,
267 ];
268
269 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
270
271 $secondRelTypeParams = [
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,
279 ];
280 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
281
282 $params = [
283 'name_a_b' => 'Relation 32 for create',
284 'name_b_a' => 'Relation 33 for create',
285 'description' => 'Testing relationship type second',
286 ];
287 $results = $this->callAPISuccess('relationship_type', 'get', $params);
288
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 /**
294 * Create relationship type.
295 * @param null $params
296 * @return mixed
297 */
298 public function _relationshipTypeCreate($params = NULL) {
299 if (!is_array($params) || empty($params)) {
300 $params = [
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,
308 ];
309 }
310
311 return $this->relationshipTypeCreate($params);
312 }
313
314 }