Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
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 | ||
30 | ||
31 | /** | |
32 | * Test APIv3 civicrm_sytanc conformance* functions | |
33 | * | |
34 | * @package CiviCRM_APIv3 | |
35 | * @subpackage API_Core | |
36 | */ | |
37 | ||
38 | class api_v3_SyntaxConformanceAllEntitiesTest extends CiviUnitTestCase { | |
39 | protected $_apiversion; | |
40 | ||
4a97890c TO |
41 | /** |
42 | * @var array e.g. $this->deletes['CRM_Contact_DAO_Contact'][] = $contactID; | |
43 | */ | |
44 | protected $deletableTestObjects; | |
45 | ||
afb0ff51 | 46 | /** This test case doesn't require DB reset */ |
6a488035 TO |
47 | public $DBResetRequired = FALSE; |
48 | ||
49 | /* they are two types of missing APIs: | |
50 | - Those that are to be implemented | |
51 | (in some future version when someone steps in -hint hint-). List the entities in toBeImplemented[ {$action} ] | |
52 | Those that don't exist | |
53 | and that will never exist (eg an obsoleted Entity | |
54 | they need to be returned by the function toBeSkipped_{$action} (because it has to be a static method and therefore couldn't access a this->toBeSkipped) | |
55 | */ function setUp() { | |
56 | parent::setUp(); | |
57 | ||
0121533e | 58 | $this->toBeImplemented['get'] = array('Profile', 'CustomValue', 'Constant', 'CustomSearch', 'Extension', 'ReportTemplate', 'System', 'Setting'); |
6a488035 TO |
59 | $this->toBeImplemented['create'] = array('SurveyRespondant', 'OptionGroup', 'MailingRecipients', 'UFMatch', 'LocationType', 'CustomSearch', 'Extension', 'ReportTemplate', 'System'); |
60 | $this->toBeImplemented['delete'] = array('MembershipPayment', 'OptionGroup', 'SurveyRespondant', 'UFJoin', 'UFMatch', 'Extension', 'LocationType', 'System'); | |
61 | $this->onlyIDNonZeroCount['get'] = array('ActivityType', 'Entity', 'Domain','Setting'); | |
62 | $this->deprecatedAPI = array('Location', 'ActivityType', 'SurveyRespondant'); | |
4a97890c | 63 | $this->deletableTestObjects = array(); |
6a488035 TO |
64 | } |
65 | ||
4a97890c TO |
66 | function tearDown() { |
67 | foreach ($this->deletableTestObjects as $entityName => $entities) { | |
68 | foreach ($entities as $entityID) { | |
69 | CRM_Core_DAO::deleteTestObjects($entityName, array('id' => $entityID)); | |
70 | } | |
71 | } | |
72 | } | |
6a488035 TO |
73 | |
74 | ||
75 | public static function entities($skip = NULL) { | |
76 | // uncomment to make a quicker run when adding a test | |
77 | //return array(array ('Tag'), array ('Activity') ); | |
78 | $tmp = civicrm_api('Entity', 'Get', array('version' => 3)); | |
79 | if (!is_array($skip)) { | |
80 | $skip = array(); | |
81 | } | |
82 | $tmp = array_diff($tmp['values'], $skip); | |
83 | $entities = array(); | |
84 | foreach ($tmp as $e) { | |
85 | $entities[] = array($e); | |
86 | } | |
87 | return $entities; | |
88 | } | |
89 | ||
90 | public static function entities_get() { | |
91 | // all the entities, beside the ones flagged | |
92 | return api_v3_SyntaxConformanceAllEntitiesTest::entities(api_v3_SyntaxConformanceAllEntitiesTest::toBeSkipped_get(TRUE)); | |
93 | } | |
94 | ||
95 | public static function entities_create() { | |
96 | return api_v3_SyntaxConformanceAllEntitiesTest::entities(api_v3_SyntaxConformanceAllEntitiesTest::toBeSkipped_create(TRUE)); | |
97 | } | |
98 | ||
99 | public static function entities_updatesingle() { | |
100 | return api_v3_SyntaxConformanceAllEntitiesTest::entities(api_v3_SyntaxConformanceAllEntitiesTest::toBeSkipped_updatesingle(TRUE)); | |
101 | } | |
102 | ||
103 | public static function entities_delete() { | |
104 | return api_v3_SyntaxConformanceAllEntitiesTest::entities(api_v3_SyntaxConformanceAllEntitiesTest::toBeSkipped_delete(TRUE)); | |
105 | } | |
106 | ||
107 | public static function toBeSkipped_get($sequential = FALSE) { | |
108 | $entitiesWithoutGet = array('MailingEventSubscribe', 'MailingEventConfirm', 'MailingEventResubscribe', 'MailingEventUnsubscribe', 'MailingGroup', 'Location'); | |
109 | if ($sequential === TRUE) { | |
110 | return $entitiesWithoutGet; | |
111 | } | |
112 | $entities = array(); | |
113 | foreach ($entitiesWithoutGet as $e) { | |
114 | $entities[] = array($e); | |
115 | } | |
116 | return $entities; | |
117 | } | |
118 | ||
119 | ||
120 | public static function toBeSkipped_create($sequential = FALSE) { | |
121 | $entitiesWithoutCreate = array('MailingGroup', 'Constant', 'Entity', 'Location', 'Profile', 'MailingRecipients'); | |
122 | if ($sequential === TRUE) { | |
123 | return $entitiesWithoutCreate; | |
124 | } | |
125 | $entities = array(); | |
126 | foreach ($entitiesWithoutCreate as $e) { | |
127 | $entities[] = array($e); | |
128 | } | |
129 | return $entities; | |
130 | } | |
131 | ||
132 | public static function toBeSkipped_delete($sequential = FALSE) { | |
133 | $entitiesWithout = array('Mailing', 'MailingGroup', 'Constant', 'Entity', 'Location', 'Domain', 'Profile', 'CustomValue'); | |
134 | if ($sequential === TRUE) { | |
135 | return $entitiesWithout; | |
136 | } | |
137 | $entities = array(); | |
138 | foreach ($entitiesWithout as $e) { | |
139 | $entities[] = array($e); | |
140 | } | |
141 | return $entities; | |
142 | } | |
faacb3e4 | 143 | /** |
144 | * Generate list of entities to test for get by id functions | |
145 | * @param boolean $sequential | |
146 | * @return multitype:string |multitype:multitype:string | |
147 | */ | |
b07a3bf9 | 148 | public static function toBeSkipped_automock($sequential = FALSE) { |
faacb3e4 | 149 | $entitiesWithoutGet = array('EntityTag', 'Participant', 'ParticipantPayment', 'Setting', 'SurveyRespondant', 'MailingRecipients', 'CustomSearch', 'Extension', 'ReportTemplate', 'System'); |
b07a3bf9 TO |
150 | if ($sequential === TRUE) { |
151 | return $entitiesWithoutGet; | |
152 | } | |
153 | $entities = array(); | |
154 | foreach ($entitiesWithoutGet as $e) { | |
155 | $entities[] = array($e); | |
156 | } | |
157 | return $entities; | |
158 | } | |
159 | ||
160 | ||
6a488035 TO |
161 | /* |
162 | * At this stage exclude the ones that don't pass & add them as we can troubleshoot them | |
163 | */ | |
164 | ||
165 | public static function toBeSkipped_updatesingle($sequential = FALSE) { | |
166 | $entitiesWithout = array( | |
167 | 'Mailing', | |
168 | 'MailingGroup', | |
169 | 'MailingJob', | |
170 | 'Address', | |
171 | 'MailingEventUnsubscribe', | |
172 | 'MailingEventSubscribe', | |
173 | 'Constant', | |
174 | 'Entity', | |
175 | 'Location', | |
176 | 'Domain', | |
177 | 'Profile', | |
178 | 'CustomValue', | |
179 | 'SurveyRespondant', | |
180 | 'Tag', | |
181 | 'UFMatch', | |
182 | 'UFJoin', | |
183 | 'UFField', | |
184 | 'OptionValue', | |
185 | 'Relationship', | |
186 | 'RelationshipType', | |
187 | 'ParticipantStatusType', | |
188 | 'Note', | |
189 | 'OptionGroup', | |
190 | 'Membership', | |
191 | 'MembershipType', | |
192 | 'MembershipStatus', | |
193 | 'Group', | |
194 | 'GroupOrganization', | |
195 | 'GroupNesting', | |
196 | 'Job', | |
197 | 'File', | |
198 | 'EntityTag', | |
199 | 'CustomField', | |
200 | 'CustomGroup', | |
201 | 'Contribution', | |
202 | 'ContributionRecur', | |
203 | 'ActivityType', | |
204 | 'MailingEventConfirm', | |
205 | 'Case', | |
206 | 'Contact', | |
207 | 'ContactType', | |
208 | 'MailingEventResubscribe', | |
209 | 'UFGroup', | |
210 | 'Activity', | |
211 | 'Email', | |
212 | 'Event', | |
213 | 'GroupContact', | |
214 | 'MembershipPayment', | |
215 | 'Participant', | |
216 | 'ParticipantPayment', | |
217 | 'LineItem', | |
218 | 'PriceSet', | |
219 | 'PriceField', | |
220 | 'PriceFieldValue', | |
221 | 'PledgePayment', | |
222 | 'ContributionPage', | |
223 | 'Phone', | |
faacb3e4 | 224 | 'PaymentProcessor', |
6a488035 TO |
225 | 'MailSettings', |
226 | 'Setting', | |
227 | ); | |
228 | if ($sequential === TRUE) { | |
229 | return $entitiesWithout; | |
230 | } | |
231 | $entities = array(); | |
232 | foreach ($entitiesWithout as $e) { | |
233 | $entities[] = array( | |
234 | $e, | |
235 | ); | |
236 | } | |
237 | return array('pledge'); | |
238 | return $entities; | |
239 | } | |
240 | ||
241 | public function getKnownUnworkablesUpdateSingle($entity, $key){ | |
242 | // can't update values are values for which updates don't result in the value being changed | |
243 | $knownFailures = array( | |
244 | 'Address' => array( | |
245 | 'cant_update' => array( | |
246 | 'state_province_id', //issues with country id - need to ensure same country | |
247 | 'master_id',//creates relationship | |
248 | ), | |
249 | 'cant_return' => array( | |
250 | ) | |
251 | ), | |
252 | 'Pledge' => array( | |
253 | 'cant_update' => array( | |
254 | 'pledge_original_installment_amount', | |
255 | 'installments', | |
256 | 'original_installment_amount', | |
257 | 'next_pay_date', | |
258 | 'amount' // can't be changed through API | |
259 | ), | |
260 | 'break_return' => array(// if these are passed in they are retrieved from the wrong table | |
261 | 'honor_contact_id', | |
262 | 'cancel_date', | |
263 | 'contribution_page_id', | |
264 | 'financial_account_id', | |
265 | 'financial_type_id', | |
266 | 'currency' | |
267 | ), | |
268 | 'cant_return' => array(// can't be retrieved from api | |
269 | 'honor_type_id', //due to uniquename missing | |
270 | 'end_date', | |
271 | 'modified_date', | |
272 | 'acknowledge_date', | |
273 | 'start_date', | |
274 | 'frequency_day', | |
275 | 'currency', | |
276 | 'max_reminders', | |
277 | 'initial_reminder_day', | |
278 | 'additional_reminder_day', | |
279 | 'frequency_unit', | |
280 | 'pledge_contribution_page_id', | |
281 | 'pledge_status_id', | |
282 | 'pledge_campaign_id', | |
283 | ) | |
284 | ), | |
285 | 'PaymentProcessorType' => array( | |
286 | 'cant_update' => array( | |
287 | 'billing_mode', | |
288 | ), | |
289 | 'break_return' => array( | |
290 | ), | |
291 | 'cant_return' => array( | |
292 | ), | |
293 | ), | |
294 | ); | |
295 | if(empty($knownFailures[$entity]) || empty($knownFailures[$entity][$key])){ | |
296 | return array(); | |
297 | } | |
298 | return $knownFailures[$entity][$key]; | |
299 | } | |
300 | ||
301 | /** testing the _get **/ | |
302 | ||
303 | /** | |
304 | * @dataProvider toBeSkipped_get | |
305 | entities that don't need a get action | |
306 | */ | |
307 | public function testNotImplemented_get($Entity) { | |
308 | $result = civicrm_api($Entity, 'Get', array('version' => 3)); | |
309 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
310 | $this->assertContains("API ($Entity,Get) does not exist", $result['error_message']); | |
311 | } | |
312 | ||
313 | /** | |
314 | * @dataProvider entities | |
315 | * @expectedException PHPUnit_Framework_Error | |
316 | */ | |
317 | public function testWithoutParam_get($Entity) { | |
318 | // should get php complaining that a param is missing | |
319 | $result = civicrm_api($Entity, 'Get'); | |
320 | } | |
321 | ||
322 | /** | |
323 | * @dataProvider entities | |
324 | */ | |
325 | public function testGetFields($Entity) { | |
326 | if (in_array($Entity, $this->deprecatedAPI) || $Entity == 'Entity' || $Entity == 'CustomValue' || $Entity == 'MailingGroup') { | |
327 | return; | |
328 | } | |
329 | ||
330 | $result = civicrm_api($Entity, 'getfields', array('version' => 3)); | |
331 | $this->assertTrue(is_array($result['values']), "$Entity ::get fields doesn't return values array in line " . __LINE__); | |
332 | foreach ($result['values'] as $key => $value) { | |
333 | $this->assertTrue(is_array($value), $Entity . "::" . $key . " is not an array in line " . __LINE__); | |
334 | } | |
335 | } | |
336 | ||
337 | /** | |
338 | * @dataProvider entities_get | |
339 | */ | |
340 | public function testEmptyParam_get($Entity) { | |
341 | ||
342 | if (in_array($Entity, $this->toBeImplemented['get'])) { | |
343 | // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented"); | |
344 | return; | |
345 | } | |
346 | $result = civicrm_api($Entity, 'Get', array()); | |
347 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
348 | $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']); | |
349 | } | |
350 | /** | |
351 | * @dataProvider entities_get | |
352 | */ | |
353 | public function testEmptyParam_getString($Entity) { | |
354 | ||
355 | if (in_array($Entity, $this->toBeImplemented['get'])) { | |
356 | // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented"); | |
357 | return; | |
358 | } | |
359 | $result = civicrm_api($Entity, 'Get', 'string'); | |
360 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
361 | $this->assertEquals(2000, $result['error_code']); | |
362 | $this->assertEquals('Input variable `params` is not an array', $result['error_message']); | |
363 | } | |
364 | /** | |
365 | * @dataProvider entities_get | |
366 | * @Xdepends testEmptyParam_get // no need to test the simple if the empty doesn't work/is skipped. doesn't seem to work | |
367 | */ | |
368 | public function testSimple_get($Entity) { | |
369 | // $this->markTestSkipped("test gives core error on test server (but not on our locals). Skip until we can get server to pass"); | |
6a488035 TO |
370 | if (in_array($Entity, $this->toBeImplemented['get'])) { |
371 | return; | |
372 | } | |
373 | $result = civicrm_api($Entity, 'Get', array('version' => 3)); | |
374 | // @TODO: list the get that have mandatory params | |
375 | if ($result['is_error']) { | |
376 | $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']); | |
377 | // either id or contact_id or entity_id is one of the field missing | |
378 | $this->assertContains("id", $result['error_message']); | |
379 | } | |
380 | else { | |
381 | $this->assertEquals(3, $result['version']); | |
382 | $this->assertArrayHasKey('count', $result); | |
383 | $this->assertArrayHasKey('values', $result); | |
384 | } | |
385 | } | |
386 | ||
387 | /** | |
388 | * @dataProvider entities_get | |
389 | */ | |
390 | public function testAcceptsOnlyID_get($Entity) { | |
391 | // big random number. fun fact: if you multiply it by pi^e, the result is another random number, but bigger ;) | |
392 | $nonExistantID = 30867307034; | |
393 | if (in_array($Entity, $this->toBeImplemented['get'])) { | |
394 | return; | |
395 | } | |
396 | ||
397 | // FIXME | |
398 | // the below function returns different values and hence an early return | |
399 | // we'll fix this once beta1 is released | |
400 | // return; | |
401 | ||
0121533e | 402 | $result = civicrm_api($Entity, 'Get', array( 'debug' => 1, 'version' => 3, 'id' => $nonExistantID)); |
6a488035 TO |
403 | |
404 | if ($result['is_error']) { | |
405 | // just to get a clearer message in the log | |
406 | $this->assertEquals("only id should be enough", $result['error_message']); | |
407 | } | |
408 | if (!in_array($Entity, $this->onlyIDNonZeroCount['get'])) { | |
409 | $this->assertEquals(0, $result['count']); | |
410 | } | |
411 | } | |
412 | ||
413 | /** | |
4a97890c TO |
414 | * Create two entities and make sure we can fetch them individually by ID |
415 | * | |
416 | * @dataProvider entities_get | |
417 | * | |
418 | * limitations include the problem with avoiding loops when creating test objects - | |
419 | * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through | |
420 | * Currency - only seems to support US | |
421 | */ | |
422 | public function testByID_get($entityName) { | |
b07a3bf9 | 423 | if (in_array($entityName, self::toBeSkipped_automock(TRUE))) { |
4a97890c TO |
424 | // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented"); |
425 | return; | |
426 | } | |
427 | ||
428 | $baoString = _civicrm_api3_get_DAO($entityName); | |
429 | if (empty($baoString)) { | |
430 | $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO"); | |
431 | return; | |
432 | } | |
433 | ||
434 | // create entities | |
435 | $baoObj1 = CRM_Core_DAO::createTestObject($baoString, array('currency' => 'USD')); | |
436 | $this->assertTrue(is_integer($baoObj1->id), 'check first id'); | |
437 | $this->deletableTestObjects[$baoString][] = $baoObj1->id; | |
438 | $baoObj2 = CRM_Core_DAO::createTestObject($baoString, array('currency' => 'USD')); | |
439 | $this->assertTrue(is_integer($baoObj2->id), 'check second id'); | |
440 | $this->deletableTestObjects[$baoString][] = $baoObj2->id; | |
441 | ||
442 | // fetch first by ID | |
443 | $result = civicrm_api($entityName, 'get', array( | |
444 | 'version' => 3, | |
445 | 'id' => $baoObj1->id, | |
446 | )); | |
447 | $this->assertAPISuccess($result); | |
448 | $this->assertTrue(!empty($result['values'][$baoObj1->id]), 'Should find first object by id'); | |
449 | $this->assertEquals($baoObj1->id, $result['values'][$baoObj1->id]['id'], 'Should find id on first object'); | |
450 | $this->assertEquals(1, count($result['values'])); | |
451 | ||
452 | // fetch second by ID | |
453 | $result = civicrm_api($entityName, 'get', array( | |
454 | 'version' => 3, | |
455 | 'id' => $baoObj2->id, | |
456 | )); | |
457 | $this->assertAPISuccess($result); | |
458 | $this->assertTrue(!empty($result['values'][$baoObj2->id]), 'Should find second object by id'); | |
459 | $this->assertEquals($baoObj2->id, $result['values'][$baoObj2->id]['id'], 'Should find id on second object'); | |
460 | $this->assertEquals(1, count($result['values'])); | |
461 | } | |
462 | ||
afb0ff51 TO |
463 | /** |
464 | * Create two entities and make sure we can fetch them individually by ID (e.g. using "contact_id=>2" | |
465 | * or "group_id=>4") | |
466 | * | |
467 | * @dataProvider entities_get | |
468 | * | |
469 | * limitations include the problem with avoiding loops when creating test objects - | |
470 | * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through | |
471 | * Currency - only seems to support US | |
472 | */ | |
473 | public function testByIDAlias_get($entityName) { | |
c4de8b59 | 474 | if (in_array($entityName, self::toBeSkipped_automock(TRUE))) { |
afb0ff51 TO |
475 | // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented"); |
476 | return; | |
477 | } | |
478 | ||
479 | $baoString = _civicrm_api3_get_DAO($entityName); | |
480 | if (empty($baoString)) { | |
481 | $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO"); | |
482 | return; | |
483 | } | |
484 | ||
c4de8b59 TO |
485 | $idFieldName = _civicrm_api_get_entity_name_from_camel($entityName) . '_id'; |
486 | ||
afb0ff51 TO |
487 | // create entities |
488 | $baoObj1 = CRM_Core_DAO::createTestObject($baoString, array('currency' => 'USD')); | |
489 | $this->assertTrue(is_integer($baoObj1->id), 'check first id'); | |
490 | $this->deletableTestObjects[$baoString][] = $baoObj1->id; | |
491 | $baoObj2 = CRM_Core_DAO::createTestObject($baoString, array('currency' => 'USD')); | |
492 | $this->assertTrue(is_integer($baoObj2->id), 'check second id'); | |
493 | $this->deletableTestObjects[$baoString][] = $baoObj2->id; | |
494 | ||
495 | // fetch first by ID | |
496 | $result = civicrm_api($entityName, 'get', array( | |
497 | 'version' => 3, | |
c4de8b59 | 498 | $idFieldName => $baoObj1->id, |
afb0ff51 TO |
499 | )); |
500 | $this->assertAPISuccess($result); | |
501 | $this->assertTrue(!empty($result['values'][$baoObj1->id]), 'Should find first object by id'); | |
502 | $this->assertEquals($baoObj1->id, $result['values'][$baoObj1->id]['id'], 'Should find id on first object'); | |
503 | $this->assertEquals(1, count($result['values'])); | |
504 | ||
505 | // fetch second by ID | |
506 | $result = civicrm_api($entityName, 'get', array( | |
507 | 'version' => 3, | |
c4de8b59 | 508 | $idFieldName => $baoObj2->id, |
afb0ff51 TO |
509 | )); |
510 | $this->assertAPISuccess($result); | |
511 | $this->assertTrue(!empty($result['values'][$baoObj2->id]), 'Should find second object by id'); | |
512 | $this->assertEquals($baoObj2->id, $result['values'][$baoObj2->id]['id'], 'Should find id on second object'); | |
513 | $this->assertEquals(1, count($result['values'])); | |
514 | } | |
515 | ||
516 | /** | |
6a488035 TO |
517 | * @dataProvider entities_get |
518 | */ | |
519 | public function testNonExistantID_get($Entity) { | |
520 | // cf testAcceptsOnlyID_get | |
521 | $nonExistantID = 30867307034; | |
522 | if (in_array($Entity, $this->toBeImplemented['get'])) { | |
523 | return; | |
524 | } | |
525 | ||
526 | $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID)); | |
527 | ||
528 | // redundant with testAcceptsOnlyID_get | |
529 | if ($result['is_error']) { | |
530 | return; | |
531 | } | |
532 | ||
533 | ||
534 | $this->assertArrayHasKey('version', $result); | |
535 | $this->assertEquals(3, $result['version']); | |
536 | if (!in_array($Entity, $this->onlyIDNonZeroCount['get'])) { | |
537 | $this->assertEquals(0, $result['count']); | |
538 | } | |
539 | } | |
540 | ||
541 | /** testing the _create **/ | |
542 | ||
543 | /** | |
544 | * @dataProvider toBeSkipped_create | |
545 | entities that don't need a create action | |
546 | */ | |
547 | public function testNotImplemented_create($Entity) { | |
548 | $result = civicrm_api($Entity, 'Create', array('version' => 3)); | |
549 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
550 | $this->assertContains("API ($Entity,Create) does not exist", $result['error_message']); | |
551 | } | |
552 | ||
553 | /** | |
554 | * @dataProvider entities | |
555 | * @expectedException PHPUnit_Framework_Error | |
556 | */ | |
557 | public function testWithoutParam_create($Entity) { | |
558 | // should create php complaining that a param is missing | |
559 | $result = civicrm_api($Entity, 'Create'); | |
560 | } | |
561 | ||
562 | /** | |
563 | * @dataProvider entities_create | |
564 | */ | |
565 | public function testEmptyParam_create($Entity) { | |
566 | if (in_array($Entity, $this->toBeImplemented['create'])) { | |
567 | // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented"); | |
568 | return; | |
569 | } | |
570 | $result = civicrm_api($Entity, 'Create', array()); | |
571 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
572 | $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']); | |
573 | } | |
574 | ||
575 | /** | |
576 | * @dataProvider entities | |
577 | */ | |
578 | public function testCreateWrongTypeParamTag_create() { | |
579 | $result = civicrm_api("Tag", 'Create', 'this is not a string'); | |
580 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
581 | $this->assertEquals("Input variable `params` is not an array", $result['error_message']); | |
582 | } | |
583 | ||
584 | /** | |
585 | * @dataProvider entities_updatesingle | |
586 | * | |
587 | * limitations include the problem with avoiding loops when creating test objects - | |
588 | * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through | |
589 | * Currency - only seems to support US | |
590 | */ | |
591 | public function testCreateSingleValueAlter($entityName) { | |
592 | if (in_array($entityName, $this->toBeImplemented['create'])) { | |
593 | // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented"); | |
594 | return; | |
595 | } | |
596 | ||
597 | $baoString = _civicrm_api3_get_DAO($entityName); | |
598 | $this->assertNotEmpty($baoString, $entityName); | |
599 | $this->assertNotEmpty($entityName, $entityName); | |
600 | $fieldsget = $fields = civicrm_api($entityName, 'getfields', array( | |
601 | 'version' => 3, 'action' => 'get' | |
602 | ) | |
603 | ); | |
604 | if($entityName != 'Pledge'){ | |
605 | $fields = civicrm_api($entityName, 'getfields', array( | |
606 | 'version' => 3, 'action' => 'create' | |
607 | ) | |
608 | ); | |
609 | } | |
610 | $fields = $fields['values']; | |
611 | $return = array_keys($fieldsget['values']); | |
612 | $valuesNotToReturn = $this->getKnownUnworkablesUpdateSingle($entityName, 'break_return'); | |
613 | // these can't be requested as return values | |
614 | $entityValuesThatDontWork = array_merge( | |
615 | $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_update'), | |
616 | $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_return'), | |
617 | $valuesNotToReturn | |
618 | ); | |
619 | ||
620 | $return = array_diff($return,$valuesNotToReturn); | |
621 | $baoObj = new CRM_Core_DAO(); | |
622 | $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0); | |
623 | $getentities = civicrm_api($entityName, 'get', array( | |
624 | 'version' => 3, | |
625 | 'sequential' => 1, | |
626 | 'return' => $return, | |
627 | 'options' => array( | |
628 | 'sort' => 'id DESC', | |
629 | 'limit' => 2, | |
630 | ), | |
631 | )); | |
632 | // lets use first rather than assume only one exists | |
633 | $entity = $getentities['values'][0]; | |
634 | $entity2 = $getentities['values'][1]; | |
635 | foreach ($fields as $field => $specs) { | |
636 | $fieldName = $field; | |
637 | if (!empty($specs['uniquename'])) { | |
638 | $fieldName = $specs['uniquename']; | |
639 | } | |
640 | if ($field == 'currency' || $field == 'id' || $field == strtolower($entityName) . '_id' | |
641 | || in_array($field,$entityValuesThatDontWork)) { | |
642 | //@todo id & entity_id are correct but we should fix currency & frequency_day | |
643 | continue; | |
644 | } | |
645 | switch ($specs['type']) { | |
646 | case CRM_Utils_Type::T_DATE: | |
647 | case CRM_Utils_Type::T_TIMESTAMP: | |
648 | $entity[$fieldName] = '2012-05-20'; | |
649 | break; | |
650 | //case CRM_Utils_Type::T_DATETIME: | |
651 | ||
652 | case 12: | |
653 | $entity[$fieldName] = '2012-05-20 03:05:20'; | |
654 | break; | |
655 | ||
656 | case CRM_Utils_Type::T_STRING: | |
657 | case CRM_Utils_Type::T_BLOB: | |
658 | case CRM_Utils_Type::T_MEDIUMBLOB: | |
659 | case CRM_Utils_Type::T_TEXT: | |
660 | case CRM_Utils_Type::T_LONGTEXT: | |
661 | case CRM_Utils_Type::T_EMAIL: | |
662 | $entity[$fieldName] = substr('New String',0, CRM_Utils_Array::Value('maxlength',$specs,100)); | |
663 | break; | |
664 | ||
665 | case CRM_Utils_Type::T_INT: | |
666 | // probably created with a 1 | |
667 | $entity[$fieldName] = '6'; | |
668 | if (CRM_Utils_Array::value('FKClassName', $specs)) { | |
669 | if($specs['FKClassName'] == $baoString){ | |
670 | $entity[$fieldName] = (string) $entity2['id']; | |
671 | } | |
672 | else{ | |
673 | $entity[$fieldName] = (string) empty($entity2[$field]) ? CRM_Utils_Array::value($specs['uniqueName'], $entity2) : $entity2[$field]; | |
674 | //todo - there isn't always something set here - & our checking on unset values is limited | |
675 | if (empty($entity[$field])) { | |
676 | unset($entity[$field]); | |
677 | } | |
678 | } | |
679 | } | |
680 | break; | |
681 | ||
682 | case CRM_Utils_Type::T_BOOL: | |
683 | case CRM_Utils_Type::T_BOOLEAN: | |
684 | // probably created with a 1 | |
685 | $entity[$fieldName] = '0'; | |
686 | break; | |
687 | ||
688 | case CRM_Utils_Type::T_FLOAT: | |
689 | case CRM_Utils_Type::T_MONEY: | |
690 | $entity[$field] = '222'; | |
691 | break; | |
692 | ||
693 | case CRM_Utils_Type::T_URL: | |
694 | $entity[$field] = 'warm.beer.com'; | |
695 | } | |
696 | $constant = CRM_Utils_Array::value('pseudoconstant', $specs); | |
697 | if (!empty($constant)) { | |
698 | $constantOptions = array_reverse(array_keys(CRM_Utils_PseudoConstant::getConstant($constant['name']))); | |
699 | $entity[$field] = (string) $constantOptions[0]; | |
700 | } | |
701 | $enum = CRM_Utils_Array::value('enumValues', $specs); | |
702 | if (!empty($enum)) { | |
703 | // reverse so we 'change' value | |
704 | $options = array_reverse(explode(',', $enum)); | |
705 | $entity[$fieldName] = $options[0]; | |
706 | } | |
707 | $updateParams = array( | |
708 | 'version' => 3, | |
709 | 'id' => $entity['id'], | |
710 | $field => $entity[$field], | |
711 | 'debug' => 1, | |
712 | ); | |
713 | ||
714 | $update = civicrm_api($entityName, 'create', $updateParams); | |
715 | if(!empty($update['is_error'])){ | |
716 | print_r($update); | |
717 | } | |
718 | $this->assertAPISuccess($update, print_r($updateParams, TRUE) . 'in line ' . __LINE__); | |
719 | $checkParams = array( | |
720 | 'id' => $entity['id'], | |
721 | 'version' => 3, | |
722 | 'sequential' => 1, | |
723 | 'return' => $return, | |
724 | 'options' => array( | |
725 | 'sort' => 'id DESC', | |
726 | 'limit' => 2, | |
727 | ), | |
728 | ); | |
729 | ||
730 | $checkEntity = civicrm_api($entityName, 'getsingle', $checkParams); | |
4a97890c | 731 | $this->assertEquals($entity, $checkEntity, "changing field $fieldName" . print_r($entity,TRUE) );//. print_r($checkEntity,true) .print_r($checkParams,true) . print_r($update,true) . print_r($updateParams, TRUE)); |
6a488035 TO |
732 | } |
733 | $baoObj->deleteTestObjects($baoString); | |
734 | $baoObj->free(); | |
735 | } | |
736 | ||
737 | /** testing the _getFields **/ | |
738 | ||
739 | /** testing the _delete **/ | |
740 | ||
741 | /** | |
742 | * @dataProvider toBeSkipped_delete | |
743 | entities that don't need a delete action | |
744 | */ | |
745 | public function testNotImplemented_delete($Entity) { | |
746 | $nonExistantID = 151416349; | |
747 | $result = civicrm_api($Entity, 'Delete', array('version' => 3, 'id' => $nonExistantID)); | |
748 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
749 | $this->assertContains("API ($Entity,Delete) does not exist", $result['error_message']); | |
750 | } | |
751 | ||
752 | /** | |
753 | * @dataProvider entities | |
754 | * @expectedException PHPUnit_Framework_Error | |
755 | */ | |
756 | public function testWithoutParam_delete($Entity) { | |
757 | // should delete php complaining that a param is missing | |
758 | $result = civicrm_api($Entity, 'Delete'); | |
759 | } | |
760 | ||
761 | /** | |
762 | * @dataProvider entities_delete | |
763 | */ | |
764 | public function testEmptyParam_delete($Entity) { | |
765 | if (in_array($Entity, $this->toBeImplemented['delete'])) { | |
766 | // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented"); | |
767 | return; | |
768 | } | |
769 | $result = civicrm_api($Entity, 'Delete', array()); | |
770 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
771 | $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']); | |
772 | } | |
773 | ||
774 | /** | |
775 | * @dataProvider entities | |
776 | */ | |
777 | public function testDeleteWrongTypeParamTag_delete() { | |
778 | $result = civicrm_api("Tag", 'Delete', 'this is not a string'); | |
779 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); | |
780 | $this->assertEquals("Input variable `params` is not an array", $result['error_message']); | |
781 | } | |
782 | ||
783 | /** | |
784 | * Verify that HTML metacharacters provided as inputs appear consistently | |
785 | * as outputs. | |
786 | * | |
787 | * At time of writing, the encoding scheme requires (for example) that an | |
788 | * event title be partially-HTML-escaped before writing to DB. To provide | |
789 | * consistency, the API must perform extra encoding and decoding on some | |
790 | * fields. | |
791 | * | |
792 | * In this example, the event 'title' is subject to encoding, but the | |
793 | * event 'description' is not. | |
794 | */ | |
795 | public function testEncodeDecodeConsistency() { | |
796 | // Create example | |
797 | $createResult = civicrm_api('Event', 'Create', array( | |
798 | 'version' => 3, | |
799 | 'title' => 'CiviCRM <> TheRest', | |
800 | 'description' => 'TheRest <> CiviCRM', | |
801 | 'event_type_id' => 1, | |
802 | 'is_public' => 1, | |
803 | 'start_date' => 20081021, | |
804 | )); | |
805 | $this->assertAPISuccess($createResult); | |
806 | $eventId = $createResult['id']; | |
807 | $this->assertEquals('CiviCRM <> TheRest', $createResult['values'][$eventId]['title']); | |
808 | $this->assertEquals('TheRest <> CiviCRM', $createResult['values'][$eventId]['description']); | |
809 | ||
810 | // Verify "get" handles decoding in result value | |
811 | $getByIdResult = civicrm_api('Event', 'Get', array( | |
812 | 'version' => 3, | |
813 | 'id' => $eventId, | |
814 | )); | |
815 | $this->assertAPISuccess($getByIdResult); | |
816 | $this->assertEquals('CiviCRM <> TheRest', $getByIdResult['values'][$eventId]['title']); | |
817 | $this->assertEquals('TheRest <> CiviCRM', $getByIdResult['values'][$eventId]['description']); | |
818 | ||
819 | // Verify "get" handles encoding in search value | |
820 | $getByTitleResult = civicrm_api('Event', 'Get', array( | |
821 | 'version' => 3, | |
822 | 'title' => 'CiviCRM <> TheRest', | |
823 | )); | |
824 | $this->assertAPISuccess($getByTitleResult); | |
825 | $this->assertEquals('CiviCRM <> TheRest', $getByTitleResult['values'][$eventId]['title']); | |
826 | $this->assertEquals('TheRest <> CiviCRM', $getByTitleResult['values'][$eventId]['description']); | |
827 | ||
828 | // Verify that "getSingle" handles decoding | |
829 | $getSingleResult = civicrm_api('Event', 'GetSingle', array( | |
830 | 'version' => 3, | |
831 | 'id' => $eventId, | |
832 | )); | |
833 | ||
834 | $this->assertAPISuccess($getSingleResult); | |
835 | $this->assertEquals('CiviCRM <> TheRest', $getSingleResult['title']); | |
836 | $this->assertEquals('TheRest <> CiviCRM', $getSingleResult['description']); | |
837 | ||
838 | // Verify that chaining handles decoding | |
839 | $chainResult = civicrm_api('Event', 'Get', array( | |
840 | 'version' => 3, | |
841 | 'id' => $eventId, | |
842 | 'api.event.get' => array( | |
843 | ), | |
844 | )); | |
845 | $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['title']); | |
846 | $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['description']); | |
847 | $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['api.event.get']['values'][0]['title']); | |
848 | $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['api.event.get']['values'][0]['description']); | |
849 | ||
850 | // Verify that "setvalue" handles encoding for updates | |
851 | $setValueTitleResult = civicrm_api('Event', 'setvalue', array( | |
852 | 'version' => 3, | |
853 | 'id' => $eventId, | |
854 | 'field' => 'title', | |
855 | 'value' => 'setValueTitle: CiviCRM <> TheRest', | |
856 | )); | |
857 | $this->assertAPISuccess($setValueTitleResult); | |
858 | $this->assertEquals('setValueTitle: CiviCRM <> TheRest', $setValueTitleResult['values']['title']); | |
859 | $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array( | |
860 | 'version' => 3, | |
861 | 'id' => $eventId, | |
862 | 'field' => 'description', | |
863 | 'value' => 'setValueDescription: TheRest <> CiviCRM', | |
864 | )); | |
865 | $this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue | |
866 | //$this->assertAPISuccess($setValueDescriptionResult); | |
867 | //$this->assertEquals('setValueDescription: TheRest <> CiviCRM', $setValueDescriptionResult['values']['description']); | |
868 | } | |
869 | ||
870 | /** | |
871 | * Verify that write operations (create/update) use partial HTML-encoding | |
872 | * | |
873 | * In this example, the event 'title' is subject to encoding, but the | |
874 | * event 'description' is not. | |
875 | */ | |
876 | public function testEncodeWrite() { | |
877 | // Create example | |
878 | $createResult = civicrm_api('Event', 'Create', array( | |
879 | 'version' => 3, | |
880 | 'title' => 'createNew: CiviCRM <> TheRest', | |
881 | 'description' => 'createNew: TheRest <> CiviCRM', | |
882 | 'event_type_id' => 1, | |
883 | 'is_public' => 1, | |
884 | 'start_date' => 20081021, | |
885 | )); | |
886 | $this->assertAPISuccess($createResult); | |
887 | $eventId = $createResult['id']; | |
888 | $this->assertDBQuery('createNew: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array( | |
889 | 1 => array($eventId, 'Integer') | |
890 | )); | |
891 | $this->assertDBQuery('createNew: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array( | |
892 | 1 => array($eventId, 'Integer') | |
893 | )); | |
894 | ||
895 | // Verify that "create" handles encoding for updates | |
896 | $createWithIdResult = civicrm_api('Event', 'Create', array( | |
897 | 'version' => 3, | |
898 | 'id' => $eventId, | |
899 | 'title' => 'createWithId: CiviCRM <> TheRest', | |
900 | 'description' => 'createWithId: TheRest <> CiviCRM', | |
901 | )); | |
902 | $this->assertAPISuccess($createWithIdResult); | |
903 | $this->assertDBQuery('createWithId: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array( | |
904 | 1 => array($eventId, 'Integer') | |
905 | )); | |
906 | $this->assertDBQuery('createWithId: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array( | |
907 | 1 => array($eventId, 'Integer') | |
908 | )); | |
909 | ||
910 | // Verify that "setvalue" handles encoding for updates | |
911 | $setValueTitleResult = civicrm_api('Event', 'setvalue', array( | |
912 | 'version' => 3, | |
913 | 'id' => $eventId, | |
914 | 'field' => 'title', | |
915 | 'value' => 'setValueTitle: CiviCRM <> TheRest', | |
916 | )); | |
917 | $this->assertAPISuccess($setValueTitleResult); | |
918 | $this->assertDBQuery('setValueTitle: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array( | |
919 | 1 => array($eventId, 'Integer') | |
920 | )); | |
921 | $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array( | |
922 | 'version' => 3, | |
923 | 'id' => $eventId, | |
924 | 'field' => 'description', | |
925 | 'value' => 'setValueDescription: TheRest <> CiviCRM', | |
926 | )); | |
927 | $this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue | |
928 | //$this->assertAPISuccess($setValueDescriptionResult); | |
929 | //$this->assertDBQuery('setValueDescription: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array( | |
930 | // 1 => array($eventId, 'Integer') | |
931 | //)); | |
932 | } | |
933 | ||
934 | } |