4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 require_once 'CRM/Utils/DeprecatedUtils.php';
33 * Test class for API utils
37 class api_v3_UtilsTest
extends CiviUnitTestCase
{
38 protected $_apiversion;
39 public $DBResetRequired = FALSE;
40 public $_eNoticeCompliant = TRUE;
41 public $_contactID = 1;
44 * Sets up the fixture, for example, opens a network connection.
45 * This method is called before a test is executed.
49 protected function setUp() {
51 $this->_apiversion
= 3;
55 * Tears down the fixture, for example, closes a network connection.
56 * This method is called after a test is executed.
60 protected function tearDown() {}
62 function testAddFormattedParam() {
63 $values = array('contact_type' => 'Individual');
64 $params = array('something' => 1);
65 $result = _civicrm_api3_deprecated_add_formatted_param($values, $params);
66 $this->assertTrue($result);
69 function testCheckPermissionReturn() {
70 $check = array('check_permissions' => TRUE);
71 $config = CRM_Core_Config
::singleton();
72 $config->userPermissionClass
->permissions
= array();
73 $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'empty permissions should not be enough');
74 $config->userPermissionClass
->permissions
= array('access CiviCRM');
75 $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'lacking permissions should not be enough');
76 $config->userPermissionClass
->permissions
= array('add contacts');
77 $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'lacking permissions should not be enough');
79 $config->userPermissionClass
->permissions
= array('access CiviCRM', 'add contacts');
80 $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'exact permissions should be enough');
82 $config->userPermissionClass
->permissions
= array('access CiviCRM', 'add contacts', 'import contacts');
83 $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'overfluous permissions should be enough');
86 function testCheckPermissionThrow() {
87 $check = array('check_permissions' => TRUE);
88 $config = CRM_Core_Config
::singleton();
90 $config->userPermissionClass
->permissions
= array('access CiviCRM');
91 _civicrm_api3_api_check_permission('contact', 'create', $check);
94 $message = $e->getMessage();
96 $this->assertEquals($message, 'API permission check failed for contact/create call; missing permission: add contacts.', 'lacking permissions should throw an exception');
98 $config->userPermissionClass
->permissions
= array('access CiviCRM', 'add contacts', 'import contacts');
99 $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $check), 'overfluous permissions should return true');
102 function testCheckPermissionSkip() {
103 $config = CRM_Core_Config
::singleton();
104 $config->userPermissionClass
->permissions
= array('access CiviCRM');
105 $params = array('check_permissions' => TRUE);
106 $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $params, FALSE), 'lacking permissions should not be enough');
107 $params = array('check_permissions' => FALSE);
108 $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $params, FALSE), 'permission check should be skippable');
112 * Test verify mandatory - includes DAO & passed as well as empty & NULL fields
114 function testVerifyMandatory() {
115 _civicrm_api3_initialize(TRUE);
117 'entity_table' => 'civicrm_contact',
119 'contact_id' => $this->_contactID
,
120 'modified_date' => '2011-01-31',
122 'version' => $this->_apiversion
,
125 $result = civicrm_api3_verify_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject'));
127 catch(Exception
$expected) {
128 $this->assertEquals('Mandatory key(s) missing from params array: entity_id, note, subject', $expected->getMessage());
132 $this->fail('An expected exception has not been raised.');
136 * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields
138 function testVerifyOneMandatory() {
139 _civicrm_api3_initialize(TRUE);
141 'entity_table' => 'civicrm_contact',
143 'contact_id' => $this->_contactID
,
144 'modified_date' => '2011-01-31',
146 'version' => $this->_apiversion
,
150 $result = civicrm_api3_verify_one_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject'));
152 catch(Exception
$expected) {
153 $this->assertEquals('Mandatory key(s) missing from params array: entity_id, one of (note, subject)', $expected->getMessage());
157 $this->fail('An expected exception has not been raised.');
161 * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields
163 function testVerifyOneMandatoryOneSet() {
164 _civicrm_api3_initialize(TRUE);
165 $params = array('entity_table' => 'civicrm_contact', 'note' => 'note', 'contact_id' => $this->_contactID
, 'modified_date' => '2011-01-31', 'subject' => NULL, 'version' => $this->_apiversion
);
168 civicrm_api3_verify_one_mandatory($params, NULL, array('note', 'subject'));
170 catch(Exception
$expected) {
171 $this->fail('Exception raised when it shouldn\'t have been in line ' . __LINE__
);
177 * Test GET DAO function returns DAO
179 function testGetDAO() {
180 $DAO = _civicrm_api3_get_DAO('civicrm_api3_custom_group_get');
181 $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO);
182 $DAO = _civicrm_api3_get_DAO('custom_group');
183 $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO);
184 $DAO = _civicrm_api3_get_DAO('CustomGroup');
185 $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO);
186 $DAO = _civicrm_api3_get_DAO('civicrm_api3_custom_field_get');
187 $this->assertEquals('CRM_Core_DAO_CustomField', $DAO);
188 $DAO = _civicrm_api3_get_DAO('civicrm_api3_survey_get');
189 $this->assertEquals('CRM_Campaign_DAO_Survey', $DAO);
190 $DAO = _civicrm_api3_get_DAO('civicrm_api3_pledge_payment_get');
191 $this->assertEquals('CRM_Pledge_DAO_PledgePayment', $DAO);
192 $DAO = _civicrm_api3_get_DAO('civicrm_api3_website_get');
193 $this->assertEquals('CRM_Core_DAO_Website', $DAO);
194 $DAO = _civicrm_api3_get_DAO('Membership');
195 $this->assertEquals('CRM_Member_DAO_Membership', $DAO);
198 * Test GET DAO function returns DAO
200 function testGetBAO() {
201 $BAO = _civicrm_api3_get_BAO('civicrm_api3_website_get');
202 $this->assertEquals('CRM_Core_BAO_Website', $BAO);
203 $BAO = _civicrm_api3_get_BAO('civicrm_api3_survey_get');
204 $this->assertEquals('CRM_Campaign_BAO_Survey', $BAO);
205 $BAO = _civicrm_api3_get_BAO('civicrm_api3_pledge_payment_get');
206 $this->assertEquals('CRM_Pledge_BAO_PledgePayment', $BAO);
209 function test_civicrm_api3_validate_fields() {
210 $params = array('start_date' => '2010-12-20', 'end_date' => '');
211 _civicrm_api3_validate_fields('relationship', 'get', $params);
212 $this->assertEquals('20101220000000', $params['start_date']);
213 $this->assertEquals('', $params['end_date']);
216 function test_civicrm_api3_validate_fields_membership() {
217 $params = array('start_date' => '2010-12-20', 'end_date' => '', 'membership_end_date' => '0', 'join_date' => '2010-12-20', 'membership_start_date' => '2010-12-20');
218 _civicrm_api3_validate_fields('Membership', 'get', $params);
219 $this->assertEquals('20101220000000', $params['start_date'], 'in line ' . __LINE__
);
220 $this->assertEquals('', $params['end_date']);
221 $this->assertEquals('20101220000000', $params['join_date'], 'join_date not set in line ' . __LINE__
);
224 function test_civicrm_api3_validate_fields_event() {
227 'registration_start_date' => 20080601,
228 'registration_end_date' => '2008-10-15', 'start_date' => '2010-12-20', 'end_date' => '',
230 _civicrm_api3_validate_fields('event', 'create', $params);
231 $this->assertEquals('20101220000000', $params['start_date'], 'in line ' . __LINE__
);
232 $this->assertEquals('20081015000000', $params['registration_end_date'], 'in line ' . __LINE__
);
233 $this->assertEquals('', $params['end_date'], 'in line ' . __LINE__
);
234 $this->assertEquals('20080601000000', $params['registration_start_date']);
237 function test_civicrm_api3_validate_fields_exception() {
239 'join_date' => 'abc',
242 _civicrm_api3_validate_fields('Membership', 'get', $params);
244 catch(Exception
$expected) {
245 $this->assertEquals('join_date is not a valid date: abc', $expected->getMessage());
249 function testGetFields() {
250 $result = civicrm_api('membership', 'getfields', array('version' => 3));
251 $this->assertArrayHasKey('values', $result);
252 $result = civicrm_api('relationship', 'getfields', array('version' => 3));
253 $this->assertArrayHasKey('values', $result);
254 $result = civicrm_api('event', 'getfields', array('version' => 3));
255 $this->assertArrayHasKey('values', $result);
258 function testGetFields_AllOptions() {
259 $result = civicrm_api('contact', 'getfields', array(
261 'get_options' => 'all',
265 $this->assertAPISuccess($result);
266 $this->assertEquals('Household', $result['values']['contact_type']['options']['Household']);
267 $this->assertEquals('HTML', $result['values']['preferred_mail_format']['options']['HTML']);