Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
30 | ||
31 | /** | |
32 | * Test class for API utils | |
33 | * | |
34 | * @package CiviCRM | |
35 | */ | |
36 | class api_v3_UtilsTest extends CiviUnitTestCase { | |
ca985406 | 37 | protected $_apiversion = 3; |
6a488035 TO |
38 | public $DBResetRequired = FALSE; |
39 | public $_eNoticeCompliant = TRUE; | |
40 | public $_contactID = 1; | |
41 | ||
42 | /** | |
43 | * Sets up the fixture, for example, opens a network connection. | |
44 | * This method is called before a test is executed. | |
45 | * | |
46 | * @access protected | |
47 | */ | |
48 | protected function setUp() { | |
49 | parent::setUp(); | |
6a488035 TO |
50 | } |
51 | ||
52 | /** | |
53 | * Tears down the fixture, for example, closes a network connection. | |
54 | * This method is called after a test is executed. | |
55 | * | |
56 | * @access protected | |
57 | */ | |
58 | protected function tearDown() {} | |
59 | ||
60 | function testAddFormattedParam() { | |
61 | $values = array('contact_type' => 'Individual'); | |
62 | $params = array('something' => 1); | |
63 | $result = _civicrm_api3_deprecated_add_formatted_param($values, $params); | |
64 | $this->assertTrue($result); | |
65 | } | |
66 | ||
67 | function testCheckPermissionReturn() { | |
68 | $check = array('check_permissions' => TRUE); | |
69 | $config = CRM_Core_Config::singleton(); | |
70 | $config->userPermissionClass->permissions = array(); | |
71 | $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'empty permissions should not be enough'); | |
72 | $config->userPermissionClass->permissions = array('access CiviCRM'); | |
73 | $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'lacking permissions should not be enough'); | |
74 | $config->userPermissionClass->permissions = array('add contacts'); | |
75 | $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'lacking permissions should not be enough'); | |
76 | ||
77 | $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts'); | |
78 | $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'exact permissions should be enough'); | |
79 | ||
80 | $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts'); | |
81 | $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $check, FALSE), 'overfluous permissions should be enough'); | |
82 | } | |
83 | ||
84 | function testCheckPermissionThrow() { | |
85 | $check = array('check_permissions' => TRUE); | |
86 | $config = CRM_Core_Config::singleton(); | |
87 | try { | |
88 | $config->userPermissionClass->permissions = array('access CiviCRM'); | |
89 | _civicrm_api3_api_check_permission('contact', 'create', $check); | |
90 | } | |
91 | catch(Exception $e) { | |
92 | $message = $e->getMessage(); | |
93 | } | |
94 | $this->assertEquals($message, 'API permission check failed for contact/create call; missing permission: add contacts.', 'lacking permissions should throw an exception'); | |
95 | ||
96 | $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts'); | |
97 | $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $check), 'overfluous permissions should return true'); | |
98 | } | |
99 | ||
100 | function testCheckPermissionSkip() { | |
101 | $config = CRM_Core_Config::singleton(); | |
102 | $config->userPermissionClass->permissions = array('access CiviCRM'); | |
103 | $params = array('check_permissions' => TRUE); | |
104 | $this->assertFalse(_civicrm_api3_api_check_permission('contact', 'create', $params, FALSE), 'lacking permissions should not be enough'); | |
105 | $params = array('check_permissions' => FALSE); | |
106 | $this->assertTrue(_civicrm_api3_api_check_permission('contact', 'create', $params, FALSE), 'permission check should be skippable'); | |
107 | } | |
108 | ||
109 | /* | |
110 | * Test verify mandatory - includes DAO & passed as well as empty & NULL fields | |
111 | */ | |
112 | function testVerifyMandatory() { | |
113 | _civicrm_api3_initialize(TRUE); | |
114 | $params = array( | |
115 | 'entity_table' => 'civicrm_contact', | |
116 | 'note' => '', | |
117 | 'contact_id' => $this->_contactID, | |
118 | 'modified_date' => '2011-01-31', | |
119 | 'subject' => NULL, | |
ca985406 | 120 | 'version' => $this->_apiversion |
6a488035 TO |
121 | ); |
122 | try { | |
123 | $result = civicrm_api3_verify_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject')); | |
124 | } | |
125 | catch(Exception $expected) { | |
126 | $this->assertEquals('Mandatory key(s) missing from params array: entity_id, note, subject', $expected->getMessage()); | |
127 | return; | |
128 | } | |
129 | ||
130 | $this->fail('An expected exception has not been raised.'); | |
131 | } | |
132 | ||
133 | /* | |
134 | * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields | |
135 | */ | |
136 | function testVerifyOneMandatory() { | |
137 | _civicrm_api3_initialize(TRUE); | |
138 | $params = array( | |
139 | 'entity_table' => 'civicrm_contact', | |
140 | 'note' => '', | |
141 | 'contact_id' => $this->_contactID, | |
142 | 'modified_date' => '2011-01-31', | |
143 | 'subject' => NULL, | |
144 | 'version' => $this->_apiversion, | |
145 | ); | |
146 | ||
147 | try { | |
148 | $result = civicrm_api3_verify_one_mandatory($params, 'CRM_Core_BAO_Note', array('note', 'subject')); | |
149 | } | |
150 | catch(Exception $expected) { | |
151 | $this->assertEquals('Mandatory key(s) missing from params array: entity_id, one of (note, subject)', $expected->getMessage()); | |
152 | return; | |
153 | } | |
154 | ||
155 | $this->fail('An expected exception has not been raised.'); | |
156 | } | |
157 | ||
158 | /* | |
159 | * Test verify one mandatory - includes DAO & passed as well as empty & NULL fields | |
160 | */ | |
161 | function testVerifyOneMandatoryOneSet() { | |
162 | _civicrm_api3_initialize(TRUE); | |
ca985406 | 163 | $params = array('version' => 3, 'entity_table' => 'civicrm_contact', 'note' => 'note', 'contact_id' => $this->_contactID, 'modified_date' => '2011-01-31', 'subject' => NULL); |
6a488035 TO |
164 | |
165 | try { | |
166 | civicrm_api3_verify_one_mandatory($params, NULL, array('note', 'subject')); | |
167 | } | |
168 | catch(Exception$expected) { | |
169 | $this->fail('Exception raised when it shouldn\'t have been in line ' . __LINE__); | |
170 | } | |
171 | } | |
172 | ||
173 | ||
174 | /* | |
b6708aeb | 175 | * Test GET DAO function returns DAO |
176 | */ | |
6a488035 TO |
177 | function testGetDAO() { |
178 | $DAO = _civicrm_api3_get_DAO('civicrm_api3_custom_group_get'); | |
179 | $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO); | |
180 | $DAO = _civicrm_api3_get_DAO('custom_group'); | |
181 | $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO); | |
182 | $DAO = _civicrm_api3_get_DAO('CustomGroup'); | |
183 | $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO); | |
184 | $DAO = _civicrm_api3_get_DAO('civicrm_api3_custom_field_get'); | |
185 | $this->assertEquals('CRM_Core_DAO_CustomField', $DAO); | |
186 | $DAO = _civicrm_api3_get_DAO('civicrm_api3_survey_get'); | |
187 | $this->assertEquals('CRM_Campaign_DAO_Survey', $DAO); | |
188 | $DAO = _civicrm_api3_get_DAO('civicrm_api3_pledge_payment_get'); | |
189 | $this->assertEquals('CRM_Pledge_DAO_PledgePayment', $DAO); | |
190 | $DAO = _civicrm_api3_get_DAO('civicrm_api3_website_get'); | |
191 | $this->assertEquals('CRM_Core_DAO_Website', $DAO); | |
192 | $DAO = _civicrm_api3_get_DAO('Membership'); | |
193 | $this->assertEquals('CRM_Member_DAO_Membership', $DAO); | |
194 | } | |
195 | /* | |
b6708aeb | 196 | * Test GET DAO function returns DAO |
197 | */ | |
6a488035 TO |
198 | function testGetBAO() { |
199 | $BAO = _civicrm_api3_get_BAO('civicrm_api3_website_get'); | |
200 | $this->assertEquals('CRM_Core_BAO_Website', $BAO); | |
201 | $BAO = _civicrm_api3_get_BAO('civicrm_api3_survey_get'); | |
202 | $this->assertEquals('CRM_Campaign_BAO_Survey', $BAO); | |
203 | $BAO = _civicrm_api3_get_BAO('civicrm_api3_pledge_payment_get'); | |
204 | $this->assertEquals('CRM_Pledge_BAO_PledgePayment', $BAO); | |
205 | } | |
206 | ||
207 | function test_civicrm_api3_validate_fields() { | |
208 | $params = array('start_date' => '2010-12-20', 'end_date' => ''); | |
209 | _civicrm_api3_validate_fields('relationship', 'get', $params); | |
210 | $this->assertEquals('20101220000000', $params['start_date']); | |
211 | $this->assertEquals('', $params['end_date']); | |
212 | } | |
213 | ||
214 | function test_civicrm_api3_validate_fields_membership() { | |
215 | $params = array('start_date' => '2010-12-20', 'end_date' => '', 'membership_end_date' => '0', 'join_date' => '2010-12-20', 'membership_start_date' => '2010-12-20'); | |
216 | _civicrm_api3_validate_fields('Membership', 'get', $params); | |
217 | $this->assertEquals('20101220000000', $params['start_date'], 'in line ' . __LINE__); | |
218 | $this->assertEquals('', $params['end_date']); | |
219 | $this->assertEquals('20101220000000', $params['join_date'], 'join_date not set in line ' . __LINE__); | |
220 | } | |
221 | ||
222 | function test_civicrm_api3_validate_fields_event() { | |
223 | ||
224 | $params = array( | |
225 | 'registration_start_date' => 20080601, | |
226 | 'registration_end_date' => '2008-10-15', 'start_date' => '2010-12-20', 'end_date' => '', | |
227 | ); | |
228 | _civicrm_api3_validate_fields('event', 'create', $params); | |
229 | $this->assertEquals('20101220000000', $params['start_date'], 'in line ' . __LINE__); | |
230 | $this->assertEquals('20081015000000', $params['registration_end_date'], 'in line ' . __LINE__); | |
231 | $this->assertEquals('', $params['end_date'], 'in line ' . __LINE__); | |
232 | $this->assertEquals('20080601000000', $params['registration_start_date']); | |
233 | } | |
234 | ||
235 | function test_civicrm_api3_validate_fields_exception() { | |
236 | $params = array( | |
237 | 'join_date' => 'abc', | |
238 | ); | |
239 | try { | |
240 | _civicrm_api3_validate_fields('Membership', 'get', $params); | |
241 | } | |
242 | catch(Exception$expected) { | |
243 | $this->assertEquals('join_date is not a valid date: abc', $expected->getMessage()); | |
244 | } | |
245 | } | |
246 | ||
247 | function testGetFields() { | |
ca985406 | 248 | $result = $this->callAPISuccess('membership', 'getfields', array()); |
6a488035 | 249 | $this->assertArrayHasKey('values', $result); |
ca985406 | 250 | $result = $this->callAPISuccess('relationship', 'getfields', array()); |
6a488035 | 251 | $this->assertArrayHasKey('values', $result); |
ca985406 | 252 | $result = $this->callAPISuccess('event', 'getfields', array()); |
6a488035 TO |
253 | $this->assertArrayHasKey('values', $result); |
254 | } | |
41d89fcb TO |
255 | |
256 | function testGetFields_AllOptions() { | |
ca985406 | 257 | $result = $this->callAPISuccess('contact', 'getfields', array( |
41d89fcb TO |
258 | 'options' => array( |
259 | 'get_options' => 'all', | |
260 | ), | |
41d89fcb | 261 | )); |
41d89fcb TO |
262 | $this->assertEquals('Household', $result['values']['contact_type']['options']['Household']); |
263 | $this->assertEquals('HTML', $result['values']['preferred_mail_format']['options']['HTML']); | |
264 | } | |
6a488035 TO |
265 | } |
266 |