Merge pull request #17706 from demeritcowboy/mysql-ssl-alt
[civicrm-core.git] / tests / phpunit / api / v3 / UtilsTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
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
12require_once 'CRM/Utils/DeprecatedUtils.php';
13
14/**
15 * Test class for API utils
16 *
17 * @package CiviCRM
acb109b7 18 * @group headless
6a488035
TO
19 */
20class api_v3_UtilsTest extends CiviUnitTestCase {
ca985406 21 protected $_apiversion = 3;
6a488035 22 public $DBResetRequired = FALSE;
b7c9bc4c 23
6a488035
TO
24 public $_contactID = 1;
25
26 /**
27 * Sets up the fixture, for example, opens a network connection.
fe482240 28 *
6a488035 29 * This method is called before a test is executed.
6a488035
TO
30 */
31 protected function setUp() {
32 parent::setUp();
d1e734a5 33 $this->useTransaction(TRUE);
6a488035
TO
34 }
35
00be9182 36 public function testAddFormattedParam() {
9099cab3
CW
37 $values = ['contact_type' => 'Individual'];
38 $params = ['something' => 1];
6a488035
TO
39 $result = _civicrm_api3_deprecated_add_formatted_param($values, $params);
40 $this->assertTrue($result);
41 }
42
00be9182 43 public function testCheckPermissionReturn() {
9099cab3 44 $check = ['check_permissions' => TRUE];
6a488035 45 $config = CRM_Core_Config::singleton();
9099cab3 46 $config->userPermissionClass->permissions = [];
d0c9daa4 47 $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'empty permissions should not be enough');
9099cab3 48 $config->userPermissionClass->permissions = ['access CiviCRM'];
d0c9daa4 49 $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'lacking permissions should not be enough');
9099cab3 50 $config->userPermissionClass->permissions = ['add contacts'];
d0c9daa4 51 $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'lacking permissions should not be enough');
6a488035 52
9099cab3 53 $config->userPermissionClass->permissions = ['access CiviCRM', 'add contacts'];
d0c9daa4 54 $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'exact permissions should be enough');
6a488035 55
9099cab3 56 $config->userPermissionClass->permissions = ['access CiviCRM', 'add contacts', 'import contacts'];
d0c9daa4 57 $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'overfluous permissions should be enough');
6a488035
TO
58 }
59
00be9182 60 public function testCheckPermissionThrow() {
9099cab3 61 $check = ['check_permissions' => TRUE];
6a488035
TO
62 $config = CRM_Core_Config::singleton();
63 try {
9099cab3 64 $config->userPermissionClass->permissions = ['access CiviCRM'];
d0c9daa4 65 $this->runPermissionCheck('contact', 'create', $check, TRUE);
6a488035 66 }
92915c55 67 catch (Exception $e) {
6a488035
TO
68 $message = $e->getMessage();
69 }
1644b908 70 $this->assertEquals($message, 'API permission check failed for Contact/create call; insufficient permission: require access CiviCRM and add contacts', 'lacking permissions should throw an exception');
6a488035 71
9099cab3 72 $config->userPermissionClass->permissions = ['access CiviCRM', 'add contacts', 'import contacts'];
d0c9daa4 73 $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'overfluous permissions should return true');
6a488035
TO
74 }
75
00be9182 76 public function testCheckPermissionSkip() {
6a488035 77 $config = CRM_Core_Config::singleton();
9099cab3
CW
78 $config->userPermissionClass->permissions = ['access CiviCRM'];
79 $params = ['check_permissions' => TRUE];
d0c9daa4 80 $this->assertFalse($this->runPermissionCheck('contact', 'create', $params), 'lacking permissions should not be enough');
9099cab3 81 $params = ['check_permissions' => FALSE];
d0c9daa4
TO
82 $this->assertTrue($this->runPermissionCheck('contact', 'create', $params), 'permission check should be skippable');
83 }
84
a0f864fd
TO
85 public function getCamelCaseFuncs() {
86 // There have been two slightly different functions for normalizing names;
87 // _civicrm_api_get_camel_name() and \Civi\API\Request::normalizeEntityName().
88 return [
89 // These are the typical cases - where the two have always agreed.
90 ['Foo', 'Foo'],
91 ['foo', 'Foo'],
92 ['FooBar', 'FooBar'],
93 ['foo_bar', 'FooBar'],
94 ['fooBar', 'FooBar'],
95 ['Im', 'Im'],
96 ['ACL', 'Acl'],
97 ['HTTP', 'HTTP'],
98
99 // These are some atypical cases - where the two have always agreed.
100 ['foo__bar', 'FooBar'],
101 ['Foo_Bar', 'FooBar'],
102 ['one_two_three', 'OneTwoThree'],
103 ['oneTwo_three', 'OneTwoThree'],
104 ['Got2B', 'Got2B'],
105 ['got2_BGood', 'Got2BGood'],
106
107 // These are some atypical cases - where they have traditionally disagreed.
108 // _civicrm_api_get_camel_name() has now changed to match normalizeEntityName()
109 // because the latter is more defensive.
110 ['Foo-Bar', 'FooBar'],
111 ['Foo+Bar', 'FooBar'],
112 ['Foo.Bar', 'FooBar'],
113 ['Foo/../Bar/', 'FooBar'],
114 ['./Foo', 'Foo'],
115 ];
116 }
117
118 /**
119 * @param string $inputValue
120 * The user-supplied/untrusted entity name.
121 * @param string $expectValue
122 * The normalized/UpperCamelCase entity name.
123 * @dataProvider getCamelCaseFuncs
124 */
125 public function testCamelName($inputValue, $expectValue) {
126 $actualValue = _civicrm_api_get_camel_name($inputValue);
127 $this->assertEquals($expectValue, $actualValue);
128 }
129
d0c9daa4
TO
130 /**
131 * @param string $entity
132 * @param string $action
133 * @param array $params
e16033b4
TO
134 * @param bool $throws
135 * Whether we should pass any exceptions for authorization failures.
cbdcc634
EM
136 *
137 * @throws API_Exception
138 * @throws Exception
a6c01b45
CW
139 * @return bool
140 * TRUE or FALSE depending on the outcome of the authorization check
d0c9daa4 141 */
00be9182 142 public function runPermissionCheck($entity, $action, $params, $throws = FALSE) {
080b7aca 143 $params['version'] = 3;
d0c9daa4
TO
144 $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
145 $dispatcher->addSubscriber(new \Civi\API\Subscriber\PermissionCheck());
146 $kernel = new \Civi\API\Kernel($dispatcher);
5a3846f7 147 $apiRequest = \Civi\API\Request::create($entity, $action, $params);
d0c9daa4
TO
148 try {
149 $kernel->authorize(NULL, $apiRequest);
150 return TRUE;
0db6c3e1
TO
151 }
152 catch (\API_Exception $e) {
d0c9daa4
TO
153 $extra = $e->getExtraParams();
154 if (!$throws && $extra['error_code'] == API_Exception::UNAUTHORIZED) {
155 return FALSE;
0db6c3e1
TO
156 }
157 else {
d0c9daa4
TO
158 throw $e;
159 }
160 }
6a488035
TO
161 }
162
c490a46a
CW
163 /**
164 * Test verify mandatory - includes DAO & passed as well as empty & NULL fields
165 */
00be9182 166 public function testVerifyMandatory() {
6a488035 167 _civicrm_api3_initialize(TRUE);
9099cab3 168 $params = [
6a488035
TO
169 'entity_table' => 'civicrm_contact',
170 'note' => '',
171 'contact_id' => $this->_contactID,
172 'modified_date' => '2011-01-31',
173 'subject' => NULL,
21dfd5f5 174 'version' => $this->_apiversion,
9099cab3 175 ];
6a488035 176 try {
9099cab3 177 civicrm_api3_verify_mandatory($params, 'CRM_Core_BAO_Note', ['note', 'subject']);
6a488035 178 }
92915c55 179 catch (Exception $expected) {
446e1c54 180 $this->assertEquals('Mandatory key(s) missing from params array: note, subject', $expected->getMessage());
6a488035
TO
181 return;
182 }
183
184 $this->fail('An expected exception has not been raised.');
185 }
186
c490a46a
CW
187 /**
188 * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields
189 */
00be9182 190 public function testVerifyOneMandatory() {
6a488035 191 _civicrm_api3_initialize(TRUE);
9099cab3 192 $params = [
6a488035
TO
193 'entity_table' => 'civicrm_contact',
194 'note' => '',
195 'contact_id' => $this->_contactID,
196 'modified_date' => '2011-01-31',
197 'subject' => NULL,
198 'version' => $this->_apiversion,
9099cab3 199 ];
6a488035
TO
200
201 try {
9099cab3 202 civicrm_api3_verify_one_mandatory($params, 'CRM_Core_BAO_Note', ['note', 'subject']);
6a488035 203 }
92915c55 204 catch (Exception $expected) {
446e1c54 205 $this->assertEquals('Mandatory key(s) missing from params array: one of (note, subject)', $expected->getMessage());
6a488035
TO
206 return;
207 }
208
209 $this->fail('An expected exception has not been raised.');
210 }
211
c490a46a
CW
212 /**
213 * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields
214 */
00be9182 215 public function testVerifyOneMandatoryOneSet() {
6a488035 216 _civicrm_api3_initialize(TRUE);
9099cab3 217 $params = [
92915c55
TO
218 'version' => 3,
219 'entity_table' => 'civicrm_contact',
220 'note' => 'note',
221 'contact_id' => $this->_contactID,
222 'modified_date' => '2011-01-31',
389bcebf 223 'subject' => NULL,
9099cab3 224 ];
6a488035
TO
225
226 try {
9099cab3 227 civicrm_api3_verify_one_mandatory($params, NULL, ['note', 'subject']);
6a488035 228 }
92915c55 229 catch (Exception$expected) {
6a488035
TO
230 $this->fail('Exception raised when it shouldn\'t have been in line ' . __LINE__);
231 }
232 }
233
c490a46a 234 /**
eceb18cc 235 * Test GET DAO function returns DAO.
b6708aeb 236 */
00be9182 237 public function testGetDAO() {
9099cab3 238 $params = [
49e101d0
CW
239 'civicrm_api3_custom_group_get' => 'CRM_Core_DAO_CustomGroup',
240 'custom_group' => 'CRM_Core_DAO_CustomGroup',
241 'CustomGroup' => 'CRM_Core_DAO_CustomGroup',
242 'civicrm_api3_custom_field_get' => 'CRM_Core_DAO_CustomField',
243 'civicrm_api3_survey_get' => 'CRM_Campaign_DAO_Survey',
244 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_DAO_PledgePayment',
245 'civicrm_api3_website_get' => 'CRM_Core_DAO_Website',
246 'Membership' => 'CRM_Member_DAO_Membership',
9099cab3 247 ];
49e101d0
CW
248 foreach ($params as $input => $expected) {
249 $result = _civicrm_api3_get_DAO($input);
250 $this->assertEquals($expected, $result);
251 }
6a488035 252 }
c490a46a
CW
253
254 /**
eceb18cc 255 * Test GET BAO function returns BAO when it exists.
b6708aeb 256 */
00be9182 257 public function testGetBAO() {
9099cab3 258 $params = [
49e101d0
CW
259 'civicrm_api3_website_get' => 'CRM_Core_BAO_Website',
260 'civicrm_api3_survey_get' => 'CRM_Campaign_BAO_Survey',
261 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_BAO_PledgePayment',
262 'Household' => 'CRM_Contact_BAO_Contact',
263 // Note this one DOES NOT have a BAO so we expect to fall back on returning the DAO
264 'mailing_group' => 'CRM_Mailing_DAO_MailingGroup',
5c1174d3
CW
265 // Make sure we get null back with nonexistant entities
266 'civicrm_this_does_not_exist' => NULL,
9099cab3 267 ];
49e101d0
CW
268 foreach ($params as $input => $expected) {
269 $result = _civicrm_api3_get_BAO($input);
270 $this->assertEquals($expected, $result);
271 }
6a488035
TO
272 }
273
cc1b27ee 274 /**
275 * Test the validate function transforms dates.
276 *
277 * @throws \CiviCRM_API3_Exception
278 * @throws \Exception
279 */
00be9182 280 public function test_civicrm_api3_validate_fields() {
cc1b27ee 281 $params = ['relationship_start_date' => '2010-12-20', 'relationship_end_date' => ''];
9099cab3 282 $fields = civicrm_api3('relationship', 'getfields', ['action' => 'get']);
7f8c98cd 283 _civicrm_api3_validate_fields('relationship', 'get', $params, $fields['values']);
cc1b27ee 284 $this->assertEquals('20101220000000', $params['relationship_start_date']);
285 $this->assertEquals('', $params['relationship_end_date']);
6a488035
TO
286 }
287
00be9182 288 public function test_civicrm_api3_validate_fields_membership() {
9099cab3 289 $params = [
92915c55
TO
290 'start_date' => '2010-12-20',
291 'end_date' => '',
292 'membership_end_date' => '0',
09ba1975 293 'membership_join_date' => '2010-12-20',
389bcebf 294 'membership_start_date' => '2010-12-20',
9099cab3
CW
295 ];
296 $fields = civicrm_api3('Membership', 'getfields', ['action' => 'get']);
7f8c98cd 297 _civicrm_api3_validate_fields('Membership', 'get', $params, $fields['values']);
903d0d38 298 $this->assertEquals('2010-12-20', $params['start_date']);
ba4a1892 299 $this->assertEquals('20101220000000', $params['membership_start_date']);
6a488035 300 $this->assertEquals('', $params['end_date']);
09ba1975 301 $this->assertEquals('20101220000000', $params['membership_join_date'], 'join_date not set in line ' . __LINE__);
6a488035
TO
302 }
303
00be9182 304 public function test_civicrm_api3_validate_fields_event() {
6a488035 305
9099cab3 306 $params = [
6a488035 307 'registration_start_date' => 20080601,
6c6e6187 308 'registration_end_date' => '2008-10-15',
92915c55
TO
309 'start_date' => '2010-12-20',
310 'end_date' => '',
9099cab3
CW
311 ];
312 $fields = civicrm_api3('Event', 'getfields', ['action' => 'create']);
7f8c98cd 313 _civicrm_api3_validate_fields('event', 'create', $params, $fields['values']);
ba4a1892
TM
314 $this->assertEquals('20101220000000', $params['start_date']);
315 $this->assertEquals('20081015000000', $params['registration_end_date']);
316 $this->assertEquals('', $params['end_date']);
6a488035
TO
317 $this->assertEquals('20080601000000', $params['registration_start_date']);
318 }
319
00be9182 320 public function test_civicrm_api3_validate_fields_exception() {
9099cab3 321 $params = [
09ba1975 322 'membership_join_date' => 'abc',
9099cab3 323 ];
6a488035 324 try {
9099cab3 325 $fields = civicrm_api3('Membership', 'getfields', ['action' => 'get']);
7f8c98cd 326 _civicrm_api3_validate_fields('Membership', 'get', $params, $fields['values']);
6a488035 327 }
92915c55 328 catch (Exception$expected) {
09ba1975 329 $this->assertEquals('membership_join_date is not a valid date: abc', $expected->getMessage());
6a488035
TO
330 }
331 }
332
00be9182 333 public function testGetFields() {
9099cab3 334 $result = $this->callAPISuccess('membership', 'getfields', []);
6a488035 335 $this->assertArrayHasKey('values', $result);
9099cab3 336 $result = $this->callAPISuccess('relationship', 'getfields', []);
6a488035 337 $this->assertArrayHasKey('values', $result);
9099cab3 338 $result = $this->callAPISuccess('event', 'getfields', []);
6a488035
TO
339 $this->assertArrayHasKey('values', $result);
340 }
41d89fcb 341
00be9182 342 public function testGetFields_AllOptions() {
9099cab3
CW
343 $result = $this->callAPISuccess('contact', 'getfields', [
344 'options' => [
41d89fcb 345 'get_options' => 'all',
9099cab3
CW
346 ],
347 ]);
41d89fcb
TO
348 $this->assertEquals('Household', $result['values']['contact_type']['options']['Household']);
349 $this->assertEquals('HTML', $result['values']['preferred_mail_format']['options']['HTML']);
350 }
96025800 351
5bc7c754 352 public function basicArrayCases() {
9099cab3
CW
353 $records = [
354 ['snack_id' => 'a', 'fruit' => 'apple', 'cheese' => 'swiss'],
355 ['snack_id' => 'b', 'fruit' => 'grape', 'cheese' => 'cheddar'],
356 ['snack_id' => 'c', 'fruit' => 'apple', 'cheese' => 'cheddar'],
357 ['snack_id' => 'd', 'fruit' => 'apple', 'cheese' => 'gouda'],
358 ['snack_id' => 'e', 'fruit' => 'apple', 'cheese' => 'provolone'],
359 ];
360
361 $cases[] = [
5bc7c754 362 $records,
39b959db 363 // params
9099cab3 364 ['version' => 3],
39b959db 365 // expected results
9099cab3
CW
366 ['a', 'b', 'c', 'd', 'e'],
367 ];
5bc7c754 368
9099cab3 369 $cases[] = [
5bc7c754 370 $records,
39b959db 371 // params
9099cab3 372 ['version' => 3, 'fruit' => 'apple'],
39b959db 373 // expected results
9099cab3
CW
374 ['a', 'c', 'd', 'e'],
375 ];
5bc7c754 376
9099cab3 377 $cases[] = [
5bc7c754 378 $records,
9099cab3
CW
379 ['version' => 3, 'cheese' => 'cheddar'],
380 ['b', 'c'],
381 ];
5bc7c754 382
9099cab3 383 $cases[] = [
a066deea 384 $records,
9099cab3
CW
385 ['version' => 3, 'id' => 'd'],
386 ['d'],
387 ];
a066deea 388
5bc7c754
TO
389 return $cases;
390 }
391
392 /**
393 * Make a basic API (Widget.get) which allows getting data out of a simple in-memory
394 * list of records.
395 *
396 * @param $records
397 * The list of all records.
398 * @param $params
399 * The filter criteria
400 * @param array $resultIds
401 * The records which are expected to match.
402 * @dataProvider basicArrayCases
403 */
404 public function testBasicArrayGet($records, $params, $resultIds) {
405 $params['version'] = 3;
406
407 $kernel = new \Civi\API\Kernel(new \Symfony\Component\EventDispatcher\EventDispatcher());
408
409 $provider = new \Civi\API\Provider\AdhocProvider($params['version'], 'Widget');
410 $provider->addAction('get', 'access CiviCRM', function ($apiRequest) use ($records) {
9099cab3 411 return _civicrm_api3_basic_array_get('Widget', $apiRequest['params'], $records, 'snack_id', ['snack_id', 'fruit', 'cheese']);
5bc7c754
TO
412 });
413 $kernel->registerApiProvider($provider);
414
5a3846f7 415 $r1 = $kernel->runSafe('Widget', 'get', $params);
5bc7c754
TO
416 $this->assertEquals(count($resultIds), $r1['count']);
417 $this->assertEquals($resultIds, array_keys($r1['values']));
418 $this->assertEquals($resultIds, array_values(CRM_Utils_Array::collect('snack_id', $r1['values'])));
419 $this->assertEquals($resultIds, array_values(CRM_Utils_Array::collect('id', $r1['values'])));
420
5a3846f7 421 $r2 = $kernel->runSafe('Widget', 'get', $params + ['sequential' => 1]);
5bc7c754
TO
422 $this->assertEquals(count($resultIds), $r2['count']);
423 $this->assertEquals($resultIds, array_values(CRM_Utils_Array::collect('snack_id', $r2['values'])));
424 $this->assertEquals($resultIds, array_values(CRM_Utils_Array::collect('id', $r2['values'])));
425
5a3846f7 426 $r3 = $kernel->runSafe('Widget', 'get', $params + ['options' => ['offset' => 1, 'limit' => 2]]);
5bc7c754
TO
427 $slice = array_slice($resultIds, 1, 2);
428 $this->assertEquals(count($slice), $r3['count']);
429 $this->assertEquals($slice, array_values(CRM_Utils_Array::collect('snack_id', $r3['values'])));
430 $this->assertEquals($slice, array_values(CRM_Utils_Array::collect('id', $r3['values'])));
431 }
432
433 public function testBasicArrayGetReturn() {
9099cab3
CW
434 $records = [
435 ['snack_id' => 'a', 'fruit' => 'apple', 'cheese' => 'swiss'],
436 ['snack_id' => 'b', 'fruit' => 'grape', 'cheese' => 'cheddar'],
437 ['snack_id' => 'c', 'fruit' => 'apple', 'cheese' => 'cheddar'],
438 ];
5bc7c754
TO
439
440 $kernel = new \Civi\API\Kernel(new \Symfony\Component\EventDispatcher\EventDispatcher());
441 $provider = new \Civi\API\Provider\AdhocProvider(3, 'Widget');
442 $provider->addAction('get', 'access CiviCRM', function ($apiRequest) use ($records) {
9099cab3 443 return _civicrm_api3_basic_array_get('Widget', $apiRequest['params'], $records, 'snack_id', ['snack_id', 'fruit', 'cheese']);
5bc7c754
TO
444 });
445 $kernel->registerApiProvider($provider);
446
5a3846f7 447 $r1 = $kernel->runSafe('Widget', 'get', [
5bc7c754
TO
448 'version' => 3,
449 'snack_id' => 'b',
450 'return' => 'fruit',
9099cab3 451 ]);
5bc7c754 452 $this->assertAPISuccess($r1);
9099cab3 453 $this->assertEquals(['b' => ['id' => 'b', 'fruit' => 'grape']], $r1['values']);
5bc7c754 454
5a3846f7 455 $r2 = $kernel->runSafe('Widget', 'get', [
5bc7c754
TO
456 'version' => 3,
457 'snack_id' => 'b',
9099cab3
CW
458 'return' => ['fruit', 'cheese'],
459 ]);
5bc7c754 460 $this->assertAPISuccess($r2);
9099cab3 461 $this->assertEquals(['b' => ['id' => 'b', 'fruit' => 'grape', 'cheese' => 'cheddar']], $r2['values']);
5bc7c754 462
5a3846f7 463 $r3 = $kernel->runSafe('Widget', 'get', [
5bc7c754
TO
464 'version' => 3,
465 'cheese' => 'cheddar',
9099cab3
CW
466 'return' => ['fruit'],
467 ]);
5bc7c754 468 $this->assertAPISuccess($r3);
9099cab3
CW
469 $this->assertEquals([
470 'b' => ['id' => 'b', 'fruit' => 'grape'],
471 'c' => ['id' => 'c', 'fruit' => 'apple'],
472 ], $r3['values']);
5bc7c754
TO
473 }
474
1f87b2a5
SL
475 /**
476 * CRM-20892 Add Tests of new timestamp checking function
7f1a780c 477 *
478 * @throws \CRM_Core_Exception
1f87b2a5
SL
479 */
480 public function testTimeStampChecking() {
481 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_mailing (id, modified_date) VALUES (25, '2016-06-30 12:52:52')");
482 $this->assertTrue(_civicrm_api3_compare_timestamps('2017-02-15 16:00:00', 25, 'Mailing'));
9099cab3 483 $this->callAPISuccess('Mailing', 'create', ['id' => 25, 'subject' => 'Test Subject']);
1f87b2a5 484 $this->assertFalse(_civicrm_api3_compare_timestamps('2017-02-15 16:00:00', 25, 'Mailing'));
9099cab3 485 $this->callAPISuccess('Mailing', 'delete', ['id' => 25]);
1f87b2a5
SL
486 }
487
7f1a780c 488 /**
489 * Test that the foreign key constraint test correctly interprets pseudoconstants.
490 *
491 * @throws \CRM_Core_Exception
492 * @throws \API_Exception
493 */
494 public function testKeyConstraintCheck() {
495 $fieldInfo = $this->callAPISuccess('Contribution', 'getfields', [])['values']['financial_type_id'];
496 _civicrm_api3_validate_constraint(1, 'financial_type_id', $fieldInfo, 'Contribution');
497 _civicrm_api3_validate_constraint('Donation', 'financial_type_id', $fieldInfo, 'Contribution');
498 try {
499 _civicrm_api3_validate_constraint('Blah', 'financial_type_id', $fieldInfo, 'Contribution');
500 }
501 catch (API_Exception $e) {
502 $this->assertEquals("'Blah' is not a valid option for field financial_type_id", $e->getMessage());
503 return;
504 }
505 $this->fail('Last function call should have thrown an exception');
506 }
507
6a488035 508}