3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CRM/Utils/DeprecatedUtils.php';
31 * Test class for API utils
36 class api_v3_UtilsTest
extends CiviUnitTestCase
{
37 protected $_apiversion = 3;
38 public $DBResetRequired = FALSE;
40 public $_contactID = 1;
43 * Sets up the fixture, for example, opens a network connection.
45 * This method is called before a test is executed.
47 protected function setUp() {
49 $this->useTransaction(TRUE);
52 public function testAddFormattedParam() {
53 $values = array('contact_type' => 'Individual');
54 $params = array('something' => 1);
55 $result = _civicrm_api3_deprecated_add_formatted_param($values, $params);
56 $this->assertTrue($result);
59 public function testCheckPermissionReturn() {
60 $check = array('check_permissions' => TRUE);
61 $config = CRM_Core_Config
::singleton();
62 $config->userPermissionClass
->permissions
= array();
63 $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'empty permissions should not be enough');
64 $config->userPermissionClass
->permissions
= array('access CiviCRM');
65 $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'lacking permissions should not be enough');
66 $config->userPermissionClass
->permissions
= array('add contacts');
67 $this->assertFalse($this->runPermissionCheck('contact', 'create', $check), 'lacking permissions should not be enough');
69 $config->userPermissionClass
->permissions
= array('access CiviCRM', 'add contacts');
70 $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'exact permissions should be enough');
72 $config->userPermissionClass
->permissions
= array('access CiviCRM', 'add contacts', 'import contacts');
73 $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'overfluous permissions should be enough');
76 public function testCheckPermissionThrow() {
77 $check = array('check_permissions' => TRUE);
78 $config = CRM_Core_Config
::singleton();
80 $config->userPermissionClass
->permissions
= array('access CiviCRM');
81 $this->runPermissionCheck('contact', 'create', $check, TRUE);
83 catch (Exception
$e) {
84 $message = $e->getMessage();
86 $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');
88 $config->userPermissionClass
->permissions
= array('access CiviCRM', 'add contacts', 'import contacts');
89 $this->assertTrue($this->runPermissionCheck('contact', 'create', $check), 'overfluous permissions should return true');
92 public function testCheckPermissionSkip() {
93 $config = CRM_Core_Config
::singleton();
94 $config->userPermissionClass
->permissions
= array('access CiviCRM');
95 $params = array('check_permissions' => TRUE);
96 $this->assertFalse($this->runPermissionCheck('contact', 'create', $params), 'lacking permissions should not be enough');
97 $params = array('check_permissions' => FALSE);
98 $this->assertTrue($this->runPermissionCheck('contact', 'create', $params), 'permission check should be skippable');
102 * @param string $entity
103 * @param string $action
104 * @param array $params
105 * @param bool $throws
106 * Whether we should pass any exceptions for authorization failures.
108 * @throws API_Exception
111 * TRUE or FALSE depending on the outcome of the authorization check
113 public function runPermissionCheck($entity, $action, $params, $throws = FALSE) {
114 $dispatcher = new \Symfony\Component\EventDispatcher\
EventDispatcher();
115 $dispatcher->addSubscriber(new \Civi\API\Subscriber\
PermissionCheck());
116 $kernel = new \Civi\API\
Kernel($dispatcher);
117 $apiRequest = \Civi\API\Request
::create($entity, $action, $params, NULL);
119 $kernel->authorize(NULL, $apiRequest);
122 catch (\API_Exception
$e) {
123 $extra = $e->getExtraParams();
124 if (!$throws && $extra['error_code'] == API_Exception
::UNAUTHORIZED
) {
134 * Test verify mandatory - includes DAO & passed as well as empty & NULL fields
136 public function testVerifyMandatory() {
137 _civicrm_api3_initialize(TRUE);
139 'entity_table' => 'civicrm_contact',
141 'contact_id' => $this->_contactID
,
142 'modified_date' => '2011-01-31',
144 'version' => $this->_apiversion
,
147 civicrm_api3_verify_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject'));
149 catch (Exception
$expected) {
150 $this->assertEquals('Mandatory key(s) missing from params array: note, subject', $expected->getMessage());
154 $this->fail('An expected exception has not been raised.');
158 * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields
160 public function testVerifyOneMandatory() {
161 _civicrm_api3_initialize(TRUE);
163 'entity_table' => 'civicrm_contact',
165 'contact_id' => $this->_contactID
,
166 'modified_date' => '2011-01-31',
168 'version' => $this->_apiversion
,
172 civicrm_api3_verify_one_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject'));
174 catch (Exception
$expected) {
175 $this->assertEquals('Mandatory key(s) missing from params array: one of (note, subject)', $expected->getMessage());
179 $this->fail('An expected exception has not been raised.');
183 * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields
185 public function testVerifyOneMandatoryOneSet() {
186 _civicrm_api3_initialize(TRUE);
189 'entity_table' => 'civicrm_contact',
191 'contact_id' => $this->_contactID
,
192 'modified_date' => '2011-01-31',
197 civicrm_api3_verify_one_mandatory($params, NULL, array('note', 'subject'));
199 catch (Exception
$expected) {
200 $this->fail('Exception raised when it shouldn\'t have been in line ' . __LINE__
);
205 * Test GET DAO function returns DAO.
207 public function testGetDAO() {
209 'civicrm_api3_custom_group_get' => 'CRM_Core_DAO_CustomGroup',
210 'custom_group' => 'CRM_Core_DAO_CustomGroup',
211 'CustomGroup' => 'CRM_Core_DAO_CustomGroup',
212 'civicrm_api3_custom_field_get' => 'CRM_Core_DAO_CustomField',
213 'civicrm_api3_survey_get' => 'CRM_Campaign_DAO_Survey',
214 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_DAO_PledgePayment',
215 'civicrm_api3_website_get' => 'CRM_Core_DAO_Website',
216 'Membership' => 'CRM_Member_DAO_Membership',
218 foreach ($params as $input => $expected) {
219 $result = _civicrm_api3_get_DAO($input);
220 $this->assertEquals($expected, $result);
225 * Test GET BAO function returns BAO when it exists.
227 public function testGetBAO() {
229 'civicrm_api3_website_get' => 'CRM_Core_BAO_Website',
230 'civicrm_api3_survey_get' => 'CRM_Campaign_BAO_Survey',
231 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_BAO_PledgePayment',
232 'Household' => 'CRM_Contact_BAO_Contact',
233 // Note this one DOES NOT have a BAO so we expect to fall back on returning the DAO
234 'mailing_group' => 'CRM_Mailing_DAO_MailingGroup',
235 // Make sure we get null back with nonexistant entities
236 'civicrm_this_does_not_exist' => NULL,
238 foreach ($params as $input => $expected) {
239 $result = _civicrm_api3_get_BAO($input);
240 $this->assertEquals($expected, $result);
244 public function test_civicrm_api3_validate_fields() {
245 $params = array('start_date' => '2010-12-20', 'end_date' => '');
246 $fields = civicrm_api3('relationship', 'getfields', array('action' => 'get'));
247 _civicrm_api3_validate_fields('relationship', 'get', $params, $fields['values']);
248 $this->assertEquals('20101220000000', $params['start_date']);
249 $this->assertEquals('', $params['end_date']);
252 public function test_civicrm_api3_validate_fields_membership() {
254 'start_date' => '2010-12-20',
256 'membership_end_date' => '0',
257 'join_date' => '2010-12-20',
258 'membership_start_date' => '2010-12-20',
260 $fields = civicrm_api3('Membership', 'getfields', array('action' => 'get'));
261 _civicrm_api3_validate_fields('Membership', 'get', $params, $fields['values']);
262 $this->assertEquals('2010-12-20', $params['start_date']);
263 $this->assertEquals('20101220000000', $params['membership_start_date']);
264 $this->assertEquals('', $params['end_date']);
265 $this->assertEquals('20101220000000', $params['join_date'], 'join_date not set in line ' . __LINE__
);
268 public function test_civicrm_api3_validate_fields_event() {
271 'registration_start_date' => 20080601,
272 'registration_end_date' => '2008-10-15',
273 'start_date' => '2010-12-20',
276 $fields = civicrm_api3('Event', 'getfields', array('action' => 'create'));
277 _civicrm_api3_validate_fields('event', 'create', $params, $fields['values']);
278 $this->assertEquals('20101220000000', $params['start_date']);
279 $this->assertEquals('20081015000000', $params['registration_end_date']);
280 $this->assertEquals('', $params['end_date']);
281 $this->assertEquals('20080601000000', $params['registration_start_date']);
284 public function test_civicrm_api3_validate_fields_exception() {
286 'join_date' => 'abc',
289 $fields = civicrm_api3('Membership', 'getfields', array('action' => 'get'));
290 _civicrm_api3_validate_fields('Membership', 'get', $params, $fields['values']);
292 catch (Exception
$expected) {
293 $this->assertEquals('join_date is not a valid date: abc', $expected->getMessage());
297 public function testGetFields() {
298 $result = $this->callAPISuccess('membership', 'getfields', array());
299 $this->assertArrayHasKey('values', $result);
300 $result = $this->callAPISuccess('relationship', 'getfields', array());
301 $this->assertArrayHasKey('values', $result);
302 $result = $this->callAPISuccess('event', 'getfields', array());
303 $this->assertArrayHasKey('values', $result);
306 public function testGetFields_AllOptions() {
307 $result = $this->callAPISuccess('contact', 'getfields', array(
309 'get_options' => 'all',
312 $this->assertEquals('Household', $result['values']['contact_type']['options']['Household']);
313 $this->assertEquals('HTML', $result['values']['preferred_mail_format']['options']['HTML']);
316 public function basicArrayCases() {
318 array('snack_id' => 'a', 'fruit' => 'apple', 'cheese' => 'swiss'),
319 array('snack_id' => 'b', 'fruit' => 'grape', 'cheese' => 'cheddar'),
320 array('snack_id' => 'c', 'fruit' => 'apple', 'cheese' => 'cheddar'),
321 array('snack_id' => 'd', 'fruit' => 'apple', 'cheese' => 'gouda'),
322 array('snack_id' => 'e', 'fruit' => 'apple', 'cheese' => 'provolone'),
327 array('version' => 3), // params
328 array('a', 'b', 'c', 'd', 'e'), // expected results
333 array('version' => 3, 'fruit' => 'apple'), // params
334 array('a', 'c', 'd', 'e'), // expected results
339 array('version' => 3, 'cheese' => 'cheddar'),
345 array('version' => 3, 'id' => 'd'),
353 * Make a basic API (Widget.get) which allows getting data out of a simple in-memory
357 * The list of all records.
359 * The filter criteria
360 * @param array $resultIds
361 * The records which are expected to match.
362 * @dataProvider basicArrayCases
364 public function testBasicArrayGet($records, $params, $resultIds) {
365 $params['version'] = 3;
367 $kernel = new \Civi\API\
Kernel(new \Symfony\Component\EventDispatcher\
EventDispatcher());
369 $provider = new \Civi\API\Provider\
AdhocProvider($params['version'], 'Widget');
370 $provider->addAction('get', 'access CiviCRM', function ($apiRequest) use ($records) {
371 return _civicrm_api3_basic_array_get('Widget', $apiRequest['params'], $records, 'snack_id', array('snack_id', 'fruit', 'cheese'));
373 $kernel->registerApiProvider($provider);
375 $r1 = $kernel->run('Widget', 'get', $params);
376 $this->assertEquals(count($resultIds), $r1['count']);
377 $this->assertEquals($resultIds, array_keys($r1['values']));
378 $this->assertEquals($resultIds, array_values(CRM_Utils_Array
::collect('snack_id', $r1['values'])));
379 $this->assertEquals($resultIds, array_values(CRM_Utils_Array
::collect('id', $r1['values'])));
381 $r2 = $kernel->run('Widget', 'get', $params +
array('sequential' => 1));
382 $this->assertEquals(count($resultIds), $r2['count']);
383 $this->assertEquals($resultIds, array_values(CRM_Utils_Array
::collect('snack_id', $r2['values'])));
384 $this->assertEquals($resultIds, array_values(CRM_Utils_Array
::collect('id', $r2['values'])));
386 $r3 = $kernel->run('Widget', 'get', $params +
array('options' => array('offset' => 1, 'limit' => 2)));
387 $slice = array_slice($resultIds, 1, 2);
388 $this->assertEquals(count($slice), $r3['count']);
389 $this->assertEquals($slice, array_values(CRM_Utils_Array
::collect('snack_id', $r3['values'])));
390 $this->assertEquals($slice, array_values(CRM_Utils_Array
::collect('id', $r3['values'])));
393 public function testBasicArrayGetReturn() {
395 array('snack_id' => 'a', 'fruit' => 'apple', 'cheese' => 'swiss'),
396 array('snack_id' => 'b', 'fruit' => 'grape', 'cheese' => 'cheddar'),
397 array('snack_id' => 'c', 'fruit' => 'apple', 'cheese' => 'cheddar'),
400 $kernel = new \Civi\API\
Kernel(new \Symfony\Component\EventDispatcher\
EventDispatcher());
401 $provider = new \Civi\API\Provider\
AdhocProvider(3, 'Widget');
402 $provider->addAction('get', 'access CiviCRM', function ($apiRequest) use ($records) {
403 return _civicrm_api3_basic_array_get('Widget', $apiRequest['params'], $records, 'snack_id', array('snack_id', 'fruit', 'cheese'));
405 $kernel->registerApiProvider($provider);
407 $r1 = $kernel->run('Widget', 'get', array(
412 $this->assertAPISuccess($r1);
413 $this->assertEquals(array('b' => array('id' => 'b', 'fruit' => 'grape')), $r1['values']);
415 $r2 = $kernel->run('Widget', 'get', array(
418 'return' => array('fruit', 'cheese'),
420 $this->assertAPISuccess($r2);
421 $this->assertEquals(array('b' => array('id' => 'b', 'fruit' => 'grape', 'cheese' => 'cheddar')), $r2['values']);
423 $r3 = $kernel->run('Widget', 'get', array(
425 'cheese' => 'cheddar',
426 'return' => array('fruit'),
428 $this->assertAPISuccess($r3);
429 $this->assertEquals(array(
430 'b' => array('id' => 'b', 'fruit' => 'grape'),
431 'c' => array('id' => 'c', 'fruit' => 'apple'),
436 * CRM-20892 Add Tests of new timestamp checking function
438 public function testTimeStampChecking() {
439 CRM_Core_DAO
::executeQuery("INSERT INTO civicrm_mailing (id, modified_date) VALUES (25, '2016-06-30 12:52:52')");
440 $this->assertTrue(_civicrm_api3_compare_timestamps('2017-02-15 16:00:00', 25, 'Mailing'));
441 $this->callAPISuccess('Mailing', 'create', array('id' => 25, 'subject' => 'Test Subject'));
442 $this->assertFalse(_civicrm_api3_compare_timestamps('2017-02-15 16:00:00', 25, 'Mailing'));
443 $this->callAPISuccess('Mailing', 'delete', array('id' => 25));