Commit | Line | Data |
---|---|---|
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 |
12 | require_once 'CRM/Utils/DeprecatedUtils.php'; |
13 | ||
14 | /** | |
15 | * Test class for API utils | |
16 | * | |
17 | * @package CiviCRM | |
acb109b7 | 18 | * @group headless |
6a488035 TO |
19 | */ |
20 | class 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 | ||
85 | /** | |
86 | * @param string $entity | |
87 | * @param string $action | |
88 | * @param array $params | |
e16033b4 TO |
89 | * @param bool $throws |
90 | * Whether we should pass any exceptions for authorization failures. | |
cbdcc634 EM |
91 | * |
92 | * @throws API_Exception | |
93 | * @throws Exception | |
a6c01b45 CW |
94 | * @return bool |
95 | * TRUE or FALSE depending on the outcome of the authorization check | |
d0c9daa4 | 96 | */ |
00be9182 | 97 | public function runPermissionCheck($entity, $action, $params, $throws = FALSE) { |
d0c9daa4 TO |
98 | $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); |
99 | $dispatcher->addSubscriber(new \Civi\API\Subscriber\PermissionCheck()); | |
100 | $kernel = new \Civi\API\Kernel($dispatcher); | |
5a3846f7 | 101 | $apiRequest = \Civi\API\Request::create($entity, $action, $params); |
d0c9daa4 TO |
102 | try { |
103 | $kernel->authorize(NULL, $apiRequest); | |
104 | return TRUE; | |
0db6c3e1 TO |
105 | } |
106 | catch (\API_Exception $e) { | |
d0c9daa4 TO |
107 | $extra = $e->getExtraParams(); |
108 | if (!$throws && $extra['error_code'] == API_Exception::UNAUTHORIZED) { | |
109 | return FALSE; | |
0db6c3e1 TO |
110 | } |
111 | else { | |
d0c9daa4 TO |
112 | throw $e; |
113 | } | |
114 | } | |
6a488035 TO |
115 | } |
116 | ||
c490a46a CW |
117 | /** |
118 | * Test verify mandatory - includes DAO & passed as well as empty & NULL fields | |
119 | */ | |
00be9182 | 120 | public function testVerifyMandatory() { |
6a488035 | 121 | _civicrm_api3_initialize(TRUE); |
9099cab3 | 122 | $params = [ |
6a488035 TO |
123 | 'entity_table' => 'civicrm_contact', |
124 | 'note' => '', | |
125 | 'contact_id' => $this->_contactID, | |
126 | 'modified_date' => '2011-01-31', | |
127 | 'subject' => NULL, | |
21dfd5f5 | 128 | 'version' => $this->_apiversion, |
9099cab3 | 129 | ]; |
6a488035 | 130 | try { |
9099cab3 | 131 | civicrm_api3_verify_mandatory($params, 'CRM_Core_BAO_Note', ['note', 'subject']); |
6a488035 | 132 | } |
92915c55 | 133 | catch (Exception $expected) { |
446e1c54 | 134 | $this->assertEquals('Mandatory key(s) missing from params array: note, subject', $expected->getMessage()); |
6a488035 TO |
135 | return; |
136 | } | |
137 | ||
138 | $this->fail('An expected exception has not been raised.'); | |
139 | } | |
140 | ||
c490a46a CW |
141 | /** |
142 | * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields | |
143 | */ | |
00be9182 | 144 | public function testVerifyOneMandatory() { |
6a488035 | 145 | _civicrm_api3_initialize(TRUE); |
9099cab3 | 146 | $params = [ |
6a488035 TO |
147 | 'entity_table' => 'civicrm_contact', |
148 | 'note' => '', | |
149 | 'contact_id' => $this->_contactID, | |
150 | 'modified_date' => '2011-01-31', | |
151 | 'subject' => NULL, | |
152 | 'version' => $this->_apiversion, | |
9099cab3 | 153 | ]; |
6a488035 TO |
154 | |
155 | try { | |
9099cab3 | 156 | civicrm_api3_verify_one_mandatory($params, 'CRM_Core_BAO_Note', ['note', 'subject']); |
6a488035 | 157 | } |
92915c55 | 158 | catch (Exception $expected) { |
446e1c54 | 159 | $this->assertEquals('Mandatory key(s) missing from params array: one of (note, subject)', $expected->getMessage()); |
6a488035 TO |
160 | return; |
161 | } | |
162 | ||
163 | $this->fail('An expected exception has not been raised.'); | |
164 | } | |
165 | ||
c490a46a CW |
166 | /** |
167 | * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields | |
168 | */ | |
00be9182 | 169 | public function testVerifyOneMandatoryOneSet() { |
6a488035 | 170 | _civicrm_api3_initialize(TRUE); |
9099cab3 | 171 | $params = [ |
92915c55 TO |
172 | 'version' => 3, |
173 | 'entity_table' => 'civicrm_contact', | |
174 | 'note' => 'note', | |
175 | 'contact_id' => $this->_contactID, | |
176 | 'modified_date' => '2011-01-31', | |
389bcebf | 177 | 'subject' => NULL, |
9099cab3 | 178 | ]; |
6a488035 TO |
179 | |
180 | try { | |
9099cab3 | 181 | civicrm_api3_verify_one_mandatory($params, NULL, ['note', 'subject']); |
6a488035 | 182 | } |
92915c55 | 183 | catch (Exception$expected) { |
6a488035 TO |
184 | $this->fail('Exception raised when it shouldn\'t have been in line ' . __LINE__); |
185 | } | |
186 | } | |
187 | ||
c490a46a | 188 | /** |
eceb18cc | 189 | * Test GET DAO function returns DAO. |
b6708aeb | 190 | */ |
00be9182 | 191 | public function testGetDAO() { |
9099cab3 | 192 | $params = [ |
49e101d0 CW |
193 | 'civicrm_api3_custom_group_get' => 'CRM_Core_DAO_CustomGroup', |
194 | 'custom_group' => 'CRM_Core_DAO_CustomGroup', | |
195 | 'CustomGroup' => 'CRM_Core_DAO_CustomGroup', | |
196 | 'civicrm_api3_custom_field_get' => 'CRM_Core_DAO_CustomField', | |
197 | 'civicrm_api3_survey_get' => 'CRM_Campaign_DAO_Survey', | |
198 | 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_DAO_PledgePayment', | |
199 | 'civicrm_api3_website_get' => 'CRM_Core_DAO_Website', | |
200 | 'Membership' => 'CRM_Member_DAO_Membership', | |
9099cab3 | 201 | ]; |
49e101d0 CW |
202 | foreach ($params as $input => $expected) { |
203 | $result = _civicrm_api3_get_DAO($input); | |
204 | $this->assertEquals($expected, $result); | |
205 | } | |
6a488035 | 206 | } |
c490a46a CW |
207 | |
208 | /** | |
eceb18cc | 209 | * Test GET BAO function returns BAO when it exists. |
b6708aeb | 210 | */ |
00be9182 | 211 | public function testGetBAO() { |
9099cab3 | 212 | $params = [ |
49e101d0 CW |
213 | 'civicrm_api3_website_get' => 'CRM_Core_BAO_Website', |
214 | 'civicrm_api3_survey_get' => 'CRM_Campaign_BAO_Survey', | |
215 | 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_BAO_PledgePayment', | |
216 | 'Household' => 'CRM_Contact_BAO_Contact', | |
217 | // Note this one DOES NOT have a BAO so we expect to fall back on returning the DAO | |
218 | 'mailing_group' => 'CRM_Mailing_DAO_MailingGroup', | |
5c1174d3 CW |
219 | // Make sure we get null back with nonexistant entities |
220 | 'civicrm_this_does_not_exist' => NULL, | |
9099cab3 | 221 | ]; |
49e101d0 CW |
222 | foreach ($params as $input => $expected) { |
223 | $result = _civicrm_api3_get_BAO($input); | |
224 | $this->assertEquals($expected, $result); | |
225 | } | |
6a488035 TO |
226 | } |
227 | ||
cc1b27ee | 228 | /** |
229 | * Test the validate function transforms dates. | |
230 | * | |
231 | * @throws \CiviCRM_API3_Exception | |
232 | * @throws \Exception | |
233 | */ | |
00be9182 | 234 | public function test_civicrm_api3_validate_fields() { |
cc1b27ee | 235 | $params = ['relationship_start_date' => '2010-12-20', 'relationship_end_date' => '']; |
9099cab3 | 236 | $fields = civicrm_api3('relationship', 'getfields', ['action' => 'get']); |
7f8c98cd | 237 | _civicrm_api3_validate_fields('relationship', 'get', $params, $fields['values']); |
cc1b27ee | 238 | $this->assertEquals('20101220000000', $params['relationship_start_date']); |
239 | $this->assertEquals('', $params['relationship_end_date']); | |
6a488035 TO |
240 | } |
241 | ||
00be9182 | 242 | public function test_civicrm_api3_validate_fields_membership() { |
9099cab3 | 243 | $params = [ |
92915c55 TO |
244 | 'start_date' => '2010-12-20', |
245 | 'end_date' => '', | |
246 | 'membership_end_date' => '0', | |
09ba1975 | 247 | 'membership_join_date' => '2010-12-20', |
389bcebf | 248 | 'membership_start_date' => '2010-12-20', |
9099cab3 CW |
249 | ]; |
250 | $fields = civicrm_api3('Membership', 'getfields', ['action' => 'get']); | |
7f8c98cd | 251 | _civicrm_api3_validate_fields('Membership', 'get', $params, $fields['values']); |
903d0d38 | 252 | $this->assertEquals('2010-12-20', $params['start_date']); |
ba4a1892 | 253 | $this->assertEquals('20101220000000', $params['membership_start_date']); |
6a488035 | 254 | $this->assertEquals('', $params['end_date']); |
09ba1975 | 255 | $this->assertEquals('20101220000000', $params['membership_join_date'], 'join_date not set in line ' . __LINE__); |
6a488035 TO |
256 | } |
257 | ||
00be9182 | 258 | public function test_civicrm_api3_validate_fields_event() { |
6a488035 | 259 | |
9099cab3 | 260 | $params = [ |
6a488035 | 261 | 'registration_start_date' => 20080601, |
6c6e6187 | 262 | 'registration_end_date' => '2008-10-15', |
92915c55 TO |
263 | 'start_date' => '2010-12-20', |
264 | 'end_date' => '', | |
9099cab3 CW |
265 | ]; |
266 | $fields = civicrm_api3('Event', 'getfields', ['action' => 'create']); | |
7f8c98cd | 267 | _civicrm_api3_validate_fields('event', 'create', $params, $fields['values']); |
ba4a1892 TM |
268 | $this->assertEquals('20101220000000', $params['start_date']); |
269 | $this->assertEquals('20081015000000', $params['registration_end_date']); | |
270 | $this->assertEquals('', $params['end_date']); | |
6a488035 TO |
271 | $this->assertEquals('20080601000000', $params['registration_start_date']); |
272 | } | |
273 | ||
00be9182 | 274 | public function test_civicrm_api3_validate_fields_exception() { |
9099cab3 | 275 | $params = [ |
09ba1975 | 276 | 'membership_join_date' => 'abc', |
9099cab3 | 277 | ]; |
6a488035 | 278 | try { |
9099cab3 | 279 | $fields = civicrm_api3('Membership', 'getfields', ['action' => 'get']); |
7f8c98cd | 280 | _civicrm_api3_validate_fields('Membership', 'get', $params, $fields['values']); |
6a488035 | 281 | } |
92915c55 | 282 | catch (Exception$expected) { |
09ba1975 | 283 | $this->assertEquals('membership_join_date is not a valid date: abc', $expected->getMessage()); |
6a488035 TO |
284 | } |
285 | } | |
286 | ||
00be9182 | 287 | public function testGetFields() { |
9099cab3 | 288 | $result = $this->callAPISuccess('membership', 'getfields', []); |
6a488035 | 289 | $this->assertArrayHasKey('values', $result); |
9099cab3 | 290 | $result = $this->callAPISuccess('relationship', 'getfields', []); |
6a488035 | 291 | $this->assertArrayHasKey('values', $result); |
9099cab3 | 292 | $result = $this->callAPISuccess('event', 'getfields', []); |
6a488035 TO |
293 | $this->assertArrayHasKey('values', $result); |
294 | } | |
41d89fcb | 295 | |
00be9182 | 296 | public function testGetFields_AllOptions() { |
9099cab3 CW |
297 | $result = $this->callAPISuccess('contact', 'getfields', [ |
298 | 'options' => [ | |
41d89fcb | 299 | 'get_options' => 'all', |
9099cab3 CW |
300 | ], |
301 | ]); | |
41d89fcb TO |
302 | $this->assertEquals('Household', $result['values']['contact_type']['options']['Household']); |
303 | $this->assertEquals('HTML', $result['values']['preferred_mail_format']['options']['HTML']); | |
304 | } | |
96025800 | 305 | |
5bc7c754 | 306 | public function basicArrayCases() { |
9099cab3 CW |
307 | $records = [ |
308 | ['snack_id' => 'a', 'fruit' => 'apple', 'cheese' => 'swiss'], | |
309 | ['snack_id' => 'b', 'fruit' => 'grape', 'cheese' => 'cheddar'], | |
310 | ['snack_id' => 'c', 'fruit' => 'apple', 'cheese' => 'cheddar'], | |
311 | ['snack_id' => 'd', 'fruit' => 'apple', 'cheese' => 'gouda'], | |
312 | ['snack_id' => 'e', 'fruit' => 'apple', 'cheese' => 'provolone'], | |
313 | ]; | |
314 | ||
315 | $cases[] = [ | |
5bc7c754 | 316 | $records, |
39b959db | 317 | // params |
9099cab3 | 318 | ['version' => 3], |
39b959db | 319 | // expected results |
9099cab3 CW |
320 | ['a', 'b', 'c', 'd', 'e'], |
321 | ]; | |
5bc7c754 | 322 | |
9099cab3 | 323 | $cases[] = [ |
5bc7c754 | 324 | $records, |
39b959db | 325 | // params |
9099cab3 | 326 | ['version' => 3, 'fruit' => 'apple'], |
39b959db | 327 | // expected results |
9099cab3 CW |
328 | ['a', 'c', 'd', 'e'], |
329 | ]; | |
5bc7c754 | 330 | |
9099cab3 | 331 | $cases[] = [ |
5bc7c754 | 332 | $records, |
9099cab3 CW |
333 | ['version' => 3, 'cheese' => 'cheddar'], |
334 | ['b', 'c'], | |
335 | ]; | |
5bc7c754 | 336 | |
9099cab3 | 337 | $cases[] = [ |
a066deea | 338 | $records, |
9099cab3 CW |
339 | ['version' => 3, 'id' => 'd'], |
340 | ['d'], | |
341 | ]; | |
a066deea | 342 | |
5bc7c754 TO |
343 | return $cases; |
344 | } | |
345 | ||
346 | /** | |
347 | * Make a basic API (Widget.get) which allows getting data out of a simple in-memory | |
348 | * list of records. | |
349 | * | |
350 | * @param $records | |
351 | * The list of all records. | |
352 | * @param $params | |
353 | * The filter criteria | |
354 | * @param array $resultIds | |
355 | * The records which are expected to match. | |
356 | * @dataProvider basicArrayCases | |
357 | */ | |
358 | public function testBasicArrayGet($records, $params, $resultIds) { | |
359 | $params['version'] = 3; | |
360 | ||
361 | $kernel = new \Civi\API\Kernel(new \Symfony\Component\EventDispatcher\EventDispatcher()); | |
362 | ||
363 | $provider = new \Civi\API\Provider\AdhocProvider($params['version'], 'Widget'); | |
364 | $provider->addAction('get', 'access CiviCRM', function ($apiRequest) use ($records) { | |
9099cab3 | 365 | return _civicrm_api3_basic_array_get('Widget', $apiRequest['params'], $records, 'snack_id', ['snack_id', 'fruit', 'cheese']); |
5bc7c754 TO |
366 | }); |
367 | $kernel->registerApiProvider($provider); | |
368 | ||
5a3846f7 | 369 | $r1 = $kernel->runSafe('Widget', 'get', $params); |
5bc7c754 TO |
370 | $this->assertEquals(count($resultIds), $r1['count']); |
371 | $this->assertEquals($resultIds, array_keys($r1['values'])); | |
372 | $this->assertEquals($resultIds, array_values(CRM_Utils_Array::collect('snack_id', $r1['values']))); | |
373 | $this->assertEquals($resultIds, array_values(CRM_Utils_Array::collect('id', $r1['values']))); | |
374 | ||
5a3846f7 | 375 | $r2 = $kernel->runSafe('Widget', 'get', $params + ['sequential' => 1]); |
5bc7c754 TO |
376 | $this->assertEquals(count($resultIds), $r2['count']); |
377 | $this->assertEquals($resultIds, array_values(CRM_Utils_Array::collect('snack_id', $r2['values']))); | |
378 | $this->assertEquals($resultIds, array_values(CRM_Utils_Array::collect('id', $r2['values']))); | |
379 | ||
5a3846f7 | 380 | $r3 = $kernel->runSafe('Widget', 'get', $params + ['options' => ['offset' => 1, 'limit' => 2]]); |
5bc7c754 TO |
381 | $slice = array_slice($resultIds, 1, 2); |
382 | $this->assertEquals(count($slice), $r3['count']); | |
383 | $this->assertEquals($slice, array_values(CRM_Utils_Array::collect('snack_id', $r3['values']))); | |
384 | $this->assertEquals($slice, array_values(CRM_Utils_Array::collect('id', $r3['values']))); | |
385 | } | |
386 | ||
387 | public function testBasicArrayGetReturn() { | |
9099cab3 CW |
388 | $records = [ |
389 | ['snack_id' => 'a', 'fruit' => 'apple', 'cheese' => 'swiss'], | |
390 | ['snack_id' => 'b', 'fruit' => 'grape', 'cheese' => 'cheddar'], | |
391 | ['snack_id' => 'c', 'fruit' => 'apple', 'cheese' => 'cheddar'], | |
392 | ]; | |
5bc7c754 TO |
393 | |
394 | $kernel = new \Civi\API\Kernel(new \Symfony\Component\EventDispatcher\EventDispatcher()); | |
395 | $provider = new \Civi\API\Provider\AdhocProvider(3, 'Widget'); | |
396 | $provider->addAction('get', 'access CiviCRM', function ($apiRequest) use ($records) { | |
9099cab3 | 397 | return _civicrm_api3_basic_array_get('Widget', $apiRequest['params'], $records, 'snack_id', ['snack_id', 'fruit', 'cheese']); |
5bc7c754 TO |
398 | }); |
399 | $kernel->registerApiProvider($provider); | |
400 | ||
5a3846f7 | 401 | $r1 = $kernel->runSafe('Widget', 'get', [ |
5bc7c754 TO |
402 | 'version' => 3, |
403 | 'snack_id' => 'b', | |
404 | 'return' => 'fruit', | |
9099cab3 | 405 | ]); |
5bc7c754 | 406 | $this->assertAPISuccess($r1); |
9099cab3 | 407 | $this->assertEquals(['b' => ['id' => 'b', 'fruit' => 'grape']], $r1['values']); |
5bc7c754 | 408 | |
5a3846f7 | 409 | $r2 = $kernel->runSafe('Widget', 'get', [ |
5bc7c754 TO |
410 | 'version' => 3, |
411 | 'snack_id' => 'b', | |
9099cab3 CW |
412 | 'return' => ['fruit', 'cheese'], |
413 | ]); | |
5bc7c754 | 414 | $this->assertAPISuccess($r2); |
9099cab3 | 415 | $this->assertEquals(['b' => ['id' => 'b', 'fruit' => 'grape', 'cheese' => 'cheddar']], $r2['values']); |
5bc7c754 | 416 | |
5a3846f7 | 417 | $r3 = $kernel->runSafe('Widget', 'get', [ |
5bc7c754 TO |
418 | 'version' => 3, |
419 | 'cheese' => 'cheddar', | |
9099cab3 CW |
420 | 'return' => ['fruit'], |
421 | ]); | |
5bc7c754 | 422 | $this->assertAPISuccess($r3); |
9099cab3 CW |
423 | $this->assertEquals([ |
424 | 'b' => ['id' => 'b', 'fruit' => 'grape'], | |
425 | 'c' => ['id' => 'c', 'fruit' => 'apple'], | |
426 | ], $r3['values']); | |
5bc7c754 TO |
427 | } |
428 | ||
1f87b2a5 SL |
429 | /** |
430 | * CRM-20892 Add Tests of new timestamp checking function | |
7f1a780c | 431 | * |
432 | * @throws \CRM_Core_Exception | |
1f87b2a5 SL |
433 | */ |
434 | public function testTimeStampChecking() { | |
435 | CRM_Core_DAO::executeQuery("INSERT INTO civicrm_mailing (id, modified_date) VALUES (25, '2016-06-30 12:52:52')"); | |
436 | $this->assertTrue(_civicrm_api3_compare_timestamps('2017-02-15 16:00:00', 25, 'Mailing')); | |
9099cab3 | 437 | $this->callAPISuccess('Mailing', 'create', ['id' => 25, 'subject' => 'Test Subject']); |
1f87b2a5 | 438 | $this->assertFalse(_civicrm_api3_compare_timestamps('2017-02-15 16:00:00', 25, 'Mailing')); |
9099cab3 | 439 | $this->callAPISuccess('Mailing', 'delete', ['id' => 25]); |
1f87b2a5 SL |
440 | } |
441 | ||
7f1a780c | 442 | /** |
443 | * Test that the foreign key constraint test correctly interprets pseudoconstants. | |
444 | * | |
445 | * @throws \CRM_Core_Exception | |
446 | * @throws \API_Exception | |
447 | */ | |
448 | public function testKeyConstraintCheck() { | |
449 | $fieldInfo = $this->callAPISuccess('Contribution', 'getfields', [])['values']['financial_type_id']; | |
450 | _civicrm_api3_validate_constraint(1, 'financial_type_id', $fieldInfo, 'Contribution'); | |
451 | _civicrm_api3_validate_constraint('Donation', 'financial_type_id', $fieldInfo, 'Contribution'); | |
452 | try { | |
453 | _civicrm_api3_validate_constraint('Blah', 'financial_type_id', $fieldInfo, 'Contribution'); | |
454 | } | |
455 | catch (API_Exception $e) { | |
456 | $this->assertEquals("'Blah' is not a valid option for field financial_type_id", $e->getMessage()); | |
457 | return; | |
458 | } | |
459 | $this->fail('Last function call should have thrown an exception'); | |
460 | } | |
461 | ||
6a488035 | 462 | } |