3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 'CiviTest/CiviUnitTestCase.php';
32 * Test APIv3 civicrm_sytanc conformance* functions
34 * @package CiviCRM_APIv3
35 * @subpackage API_Core
38 class api_v3_SyntaxConformanceTest
extends CiviUnitTestCase
{
39 protected $_apiversion = 3;
42 * @var array e.g. $this->deletes['CRM_Contact_DAO_Contact'][] = $contactID;
44 protected $deletableTestObjects;
46 /** This test case doesn't require DB reset */
47 public $DBResetRequired = FALSE;
51 /* they are two types of missing APIs:
52 - Those that are to be implemented
53 (in some future version when someone steps in -hint hint-). List the entities in toBeImplemented[ {$action} ]
54 Those that don't exist
55 and that will never exist (eg an obsoleted Entity
56 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)
60 $this->enableCiviCampaign();
61 $this->toBeImplemented
['get'] = array('Profile', 'CustomValue', 'Constant', 'CustomSearch', 'Extension', 'ReportTemplate', 'System', 'Setting');
62 $this->toBeImplemented
['create'] = array('SurveyRespondant', 'OptionGroup', 'MailingRecipients', 'UFMatch', 'LocationType', 'CustomSearch', 'Extension', 'ReportTemplate', 'System');
63 $this->toBeImplemented
['delete'] = array('MembershipPayment', 'OptionGroup', 'SurveyRespondant', 'UFJoin', 'UFMatch', 'Extension', 'LocationType', 'System');
64 $this->onlyIDNonZeroCount
['get'] = array('ActivityType', 'Entity', 'Domain','Setting');
65 $this->deprecatedAPI
= array('Location', 'ActivityType', 'SurveyRespondant');
66 $this->deletableTestObjects
= array();
70 foreach ($this->deletableTestObjects
as $entityName => $entities) {
71 foreach ($entities as $entityID) {
72 CRM_Core_DAO
::deleteTestObjects($entityName, array('id' => $entityID));
77 public static function entities($skip = NULL) {
78 // To only test specific entities, call phpunit with SYNTAX_CONFORMANCE_ENTITIES="TheEntityName"
79 // or uncomment this line:
80 //return array(array ('Tag'), array ('Activity') );
82 if (getenv('SYNTAX_CONFORMANCE_ENTITIES')) {
84 foreach (explode(' ', getenv('SYNTAX_CONFORMANCE_ENTITIES')) as $entity) {
85 $result[] = array($entity);
90 $tmp = civicrm_api('Entity', 'Get', array('version' => 3));
91 if (!is_array($skip)) {
94 $tmp = array_diff($tmp['values'], $skip);
96 foreach ($tmp as $e) {
97 $entities[] = array($e);
102 public static function entities_get() {
103 // all the entities, beside the ones flagged
104 return static::entities(static::toBeSkipped_get(TRUE));
107 public static function entities_create() {
108 return static::entities(static::toBeSkipped_create(TRUE));
111 public static function entities_updatesingle() {
112 return static::entities(static::toBeSkipped_updatesingle(TRUE));
115 public static function entities_getlimit() {
116 return static::entities(static::toBeSkipped_getlimit());
119 public static function entities_delete() {
120 return static::entities(static::toBeSkipped_delete(TRUE));
123 public static function custom_data_entities_get() {
124 return static::custom_data_entities();
127 public static function custom_data_entities() {
128 $entities = CRM_Core_BAO_CustomQuery
::$extendsMap;
129 $customDataEntities = array();
130 $invalidEntities = array('Individual', 'Organization', 'Household');
131 $entitiesToFix = array('Case', 'Relationship');
132 foreach ($entities as $entityName => $entity ) {
133 if(!in_array($entityName, $invalidEntities)
134 && !in_array($entityName, $entitiesToFix)) {
135 $customDataEntities[] = array($entityName );
138 return $customDataEntities;
141 public static function toBeSkipped_get($sequential = FALSE) {
142 $entitiesWithoutGet = array('MailingEventSubscribe', 'MailingEventConfirm', 'MailingEventResubscribe', 'MailingEventUnsubscribe', 'MailingGroup', 'Location');
143 if ($sequential === TRUE) {
144 return $entitiesWithoutGet;
147 foreach ($entitiesWithoutGet as $e) {
148 $entities[] = array($e);
154 * Mailing Contact Just doesn't support id. We have always insisted on finding a way to
155 * support id in API but in this case the underlying tables are crying out for a restructue
156 * & it just doesn't make sense
158 * @param bool|\unknown_type $sequential
160 * @return multitype:string |multitype:multitype:string
162 public static function toBeSkipped_getByID($sequential = FALSE) {
163 return array('MailingContact');
166 public static function toBeSkipped_create($sequential = FALSE) {
167 $entitiesWithoutCreate = array('MailingGroup', 'Constant', 'Entity', 'Location', 'Profile', 'MailingRecipients');
168 if ($sequential === TRUE) {
169 return $entitiesWithoutCreate;
172 foreach ($entitiesWithoutCreate as $e) {
173 $entities[] = array($e);
178 public static function toBeSkipped_delete($sequential = FALSE) {
179 $entitiesWithout = array('MailingContact', 'MailingEventConfirm', 'MailingEventResubscribe', 'MailingEventSubscribe', 'MailingEventUnsubscribe', 'MailingGroup', 'MailingRecipients', 'Constant', 'Entity', 'Location', 'Domain', 'Profile', 'CustomValue', 'Setting');
180 if ($sequential === TRUE) {
181 return $entitiesWithout;
184 foreach ($entitiesWithout as $e) {
185 $entities[] = array($e);
191 * Generate list of entities to test for get by id functions
192 * @param boolean $sequential
193 * @return multitype:string |multitype:multitype:string
195 public static function toBeSkipped_automock($sequential = FALSE) {
196 $entitiesWithoutGet = array('MailingContact', 'EntityTag', 'Participant', 'ParticipantPayment', 'Setting', 'SurveyRespondant', 'MailingRecipients', 'CustomSearch', 'Extension', 'ReportTemplate', 'System');
197 if ($sequential === TRUE) {
198 return $entitiesWithoutGet;
201 foreach ($entitiesWithoutGet as $e) {
202 $entities[] = array($e);
209 * At this stage exclude the ones that don't pass & add them as we can troubleshoot them
211 public static function toBeSkipped_updatesingle($sequential = FALSE) {
212 $entitiesWithout = array(
217 'MailingEventUnsubscribe',
218 'MailingEventSubscribe',
233 'ParticipantStatusType',
250 'MailingEventConfirm',
254 'MailingEventResubscribe',
262 'ParticipantPayment',
274 'SystemLog' //skip this because it doesn't make sense to update logs
276 if ($sequential === TRUE) {
277 return $entitiesWithout;
280 foreach ($entitiesWithout as $e) {
285 return array('pledge');
290 * At this stage exclude the ones that don't pass & add them as we can troubleshoot them
292 public static function toBeSkipped_getlimit() {
293 $entitiesWithout = array(
294 'Case',//case api has non-std mandatory fields one of (case_id, contact_id, activity_id, contact_id)
295 'EntityTag', // non-standard api - has inappropriate mandatory fields & doesn't implement limit
296 'Event', // failed 'check that a 5 limit returns 5' - probably is_template field is wrong or something, or could be limit doesn't work right
297 'Extension', // can't handle creating 25
298 'MailingGroup', // no get call on MailingGroup
299 'Note', // fails on 5 limit - probably a set up problem
300 'Setting', //a bit of a pseudoapi - keys by domain
302 return $entitiesWithout;
311 public function getKnownUnworkablesUpdateSingle($entity, $key){
312 // can't update values are values for which updates don't result in the value being changed
313 $knownFailures = array(
314 'ActionSchedule' => array(
315 'cant_update' => array(
319 'ActivityContact' => array(
320 'cant_update' => array(
321 'activity_id', //we have an FK on activity_id + contact_id + record id so if we don't leave this one distinct we get an FK constraint error
325 'cant_update' => array(
326 'state_province_id', //issues with country id - need to ensure same country
327 'master_id',//creates relationship
329 'cant_return' => array(
333 'cant_update' => array(
334 'entity_table', // believe this field is defined in error
336 'cant_return' => array(
341 'cant_update' => array(
342 'pledge_original_installment_amount',
344 'original_installment_amount',
346 'amount' // can't be changed through API
348 'break_return' => array(// if these are passed in they are retrieved from the wrong table
351 'contribution_page_id',
352 'financial_account_id',
356 'cant_return' => array(// can't be retrieved from api
357 'honor_type_id', //due to uniquename missing
365 'initial_reminder_day',
366 'additional_reminder_day',
368 'pledge_contribution_page_id',
370 'pledge_campaign_id',
373 'PaymentProcessorType' => array(
374 'cant_update' => array(
377 'break_return' => array(
379 'cant_return' => array(
383 if(empty($knownFailures[$entity]) ||
empty($knownFailures[$entity][$key])){
386 return $knownFailures[$entity][$key];
389 /** testing the _get **/
392 * @dataProvider toBeSkipped_get
393 entities that don't need a get action
395 public function testNotImplemented_get($Entity) {
396 $result = civicrm_api($Entity, 'Get', array('version' => 3));
397 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
398 // $this->assertContains("API ($Entity, Get) does not exist", $result['error_message']);
399 $this->assertRegExp('/API (.*) does not exist/', $result['error_message']);
403 * @dataProvider entities
404 * @expectedException PHPUnit_Framework_Error
406 public function testWithoutParam_get($Entity) {
407 // should get php complaining that a param is missing
408 $result = civicrm_api($Entity, 'Get');
412 * @dataProvider entities
414 public function testGetFields($Entity) {
415 if (in_array($Entity, $this->deprecatedAPI
) ||
$Entity == 'Entity' ||
$Entity == 'CustomValue' ||
$Entity == 'MailingGroup') {
419 $result = civicrm_api($Entity, 'getfields', array('version' => 3));
420 $this->assertTrue(is_array($result['values']), "$Entity ::get fields doesn't return values array in line " . __LINE__
);
421 foreach ($result['values'] as $key => $value) {
422 $this->assertTrue(is_array($value), $Entity . "::" . $key . " is not an array in line " . __LINE__
);
427 * @dataProvider entities_get
429 public function testEmptyParam_get($Entity) {
431 if (in_array($Entity, $this->toBeImplemented
['get'])) {
432 // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented");
435 $result = civicrm_api($Entity, 'Get', array());
436 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
437 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
440 * @dataProvider entities_get
442 public function testEmptyParam_getString($Entity) {
444 if (in_array($Entity, $this->toBeImplemented
['get'])) {
445 // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented");
448 $result = $this->callAPIFailure($Entity, 'Get', 'string');
449 $this->assertEquals(2000, $result['error_code']);
450 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
453 * @dataProvider entities_get
454 * @Xdepends testEmptyParam_get // no need to test the simple if the empty doesn't work/is skipped. doesn't seem to work
456 public function testSimple_get($Entity) {
457 // $this->markTestSkipped("test gives core error on test server (but not on our locals). Skip until we can get server to pass");
458 if (in_array($Entity, $this->toBeImplemented
['get'])) {
461 $result = civicrm_api($Entity, 'Get', array('version' => 3));
462 // @TODO: list the get that have mandatory params
463 if ($result['is_error']) {
464 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
465 // either id or contact_id or entity_id is one of the field missing
466 $this->assertContains("id", $result['error_message']);
469 $this->assertEquals(3, $result['version']);
470 $this->assertArrayHasKey('count', $result);
471 $this->assertArrayHasKey('values', $result);
476 * @dataProvider custom_data_entities_get
478 public function testCustomDataGet($entityName) {
479 $this->createLoggedInUser();// so subsidiary activities are created
480 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, $entityName . 'Test.php');
481 $customFieldName = 'custom_' . $ids['custom_field_id'];
482 $objects = $this->getMockableBAOObjects($entityName, 1);
483 $params = array('id' => $objects[0]->id
, 'custom_' . $ids['custom_field_id'] => "custom string");
484 $result = $this->callAPISuccess($entityName, 'create', $params);
486 $getParams = array('id' => $result['id'], 'return' => array($customFieldName));
487 $check = $this->callAPISuccess($entityName, 'get', $getParams);
488 $this->assertEquals("custom string", $check['values'][$check['id']][$customFieldName]);
490 $this->customFieldDelete($ids['custom_field_id']);
491 $this->customGroupDelete($ids['custom_group_id']);
492 $this->callAPISuccess($entityName, 'delete', array('id' => $result['id']));
496 * @dataProvider entities_get
498 public function testAcceptsOnlyID_get($Entity) {
499 // big random number. fun fact: if you multiply it by pi^e, the result is another random number, but bigger ;)
500 $nonExistantID = 30867307034;
501 if (in_array($Entity, $this->toBeImplemented
['get'])
502 ||
in_array($Entity, $this->toBeSkipped_getByID())
508 // the below function returns different values and hence an early return
509 // we'll fix this once beta1 is released
512 $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID));
514 if ($result['is_error']) {
515 // just to get a clearer message in the log
516 $this->assertEquals("only id should be enough", $result['error_message']);
518 if (!in_array($Entity, $this->onlyIDNonZeroCount
['get'])) {
519 $this->assertEquals(0, $result['count']);
524 * Create two entities and make sure we can fetch them individually by ID
526 * @dataProvider entities_get
528 * limitations include the problem with avoiding loops when creating test objects -
529 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
530 * Currency - only seems to support US
532 public function testByID_get($entityName) {
533 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
534 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
538 $baos = $this->getMockableBAOObjects($entityName);
539 list($baoObj1, $baoObj2) = $baos;
542 $result = $this->callAPISuccess($entityName, 'get', array(
543 'id' => $baoObj1->id
,
546 $this->assertTrue(!empty($result['values'][$baoObj1->id
]), 'Should find first object by id');
547 $this->assertEquals($baoObj1->id
, $result['values'][$baoObj1->id
]['id'], 'Should find id on first object');
548 $this->assertEquals(1, count($result['values']));
550 // fetch second by ID
551 $result = $this->callAPISuccess($entityName, 'get', array(
552 'id' => $baoObj2->id
,
554 $this->assertTrue(!empty($result['values'][$baoObj2->id
]), 'Should find second object by id');
555 $this->assertEquals($baoObj2->id
, $result['values'][$baoObj2->id
]['id'], 'Should find id on second object');
556 $this->assertEquals(1, count($result['values']));
560 * Ensure that the "get" operation accepts limiting the #result records.
562 * TODO Consider making a separate entity list ("entities_getlimit")
563 * For the moment, the "entities_updatesingle" list should give a good
564 * sense for which entities support createTestObject
566 * @dataProvider entities_getlimit
570 * @internal param string $entity
572 function testLimit($entityName) {
573 $cases = array(); // each case is array(0 => $inputtedApiOptions, 1 => $expectedResultCount)
575 array('options' => array('limit' => NULL)),
577 'check that a NULL limit returns unlimited',
580 array('options' => array('limit' => FALSE)),
582 'check that a FALSE limit returns unlimited',
585 array('options' => array('limit' => 0)),
587 'check that a 0 limit returns unlimited',
590 array('options' => array('limit' => 5)),
592 'check that a 5 limit returns 5',
597 'check that no limit returns 25',
600 $baoString = _civicrm_api3_get_DAO($entityName);
601 if (empty($baoString)) {
602 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
606 // make 30 test items -- 30 > 25 (the default limit)
608 for ($i = 0; $i < 30; $i++
) {
609 $baoObj = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
610 $ids[] = $baoObj->id
;
613 // each case is array(0 => $inputtedApiOptions, 1 => $expectedResultCount)
614 foreach ($cases as $case) {
615 $this->checkLimitAgainstExpected($entityName, $case[0], $case[1], $case[2]);
617 //non preferred / legacy syntax
618 if(isset($case[0]['options']['limit'])) {
619 $this->checkLimitAgainstExpected($entityName, array('rowCount' => $case[0]['options']['limit']), $case[1], $case[2]);
620 $this->checkLimitAgainstExpected($entityName, array('option_limit' => $case[0]['options']['limit']), $case[1], $case[2]);
621 $this->checkLimitAgainstExpected($entityName, array('option.limit' => $case[0]['options']['limit']), $case[1], $case[2]);
624 foreach ($ids as $id) {
625 CRM_Core_DAO
::deleteTestObjects($baoString, array('id' => $id));
631 * Check that get fetches an appropriate number of results
633 * @param string $entityName Name of entity to test
634 * @param unknown $params
635 * @param unknown $limit
636 * @param unknown $message
638 function checkLimitAgainstExpected($entityName, $params, $limit, $message) {
639 $result = $this->callAPISuccess($entityName, 'get', $params);
641 $this->assertGreaterThanOrEqual($limit, $result['count'], $message);
642 $this->assertGreaterThanOrEqual($limit, $result['count'], $message);
645 $this->assertEquals($limit, $result['count'], $message);
646 $this->assertEquals($limit, count($result['values']), $message);
650 * Create two entities and make sure we can fetch them individually by ID (e.g. using "contact_id=>2"
653 * @dataProvider entities_get
655 * limitations include the problem with avoiding loops when creating test objects -
656 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
657 * Currency - only seems to support US
659 public function testByIDAlias_get($entityName) {
660 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
661 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
665 $baoString = _civicrm_api3_get_DAO($entityName);
666 if (empty($baoString)) {
667 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
671 $idFieldName = _civicrm_api_get_entity_name_from_camel($entityName) . '_id';
674 $baoObj1 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
675 $this->assertTrue(is_integer($baoObj1->id
), 'check first id');
676 $this->deletableTestObjects
[$baoString][] = $baoObj1->id
;
677 $baoObj2 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
678 $this->assertTrue(is_integer($baoObj2->id
), 'check second id');
679 $this->deletableTestObjects
[$baoString][] = $baoObj2->id
;
682 $result = civicrm_api($entityName, 'get', array(
684 $idFieldName => $baoObj1->id
,
686 $this->assertAPISuccess($result);
687 $this->assertTrue(!empty($result['values'][$baoObj1->id
]), 'Should find first object by id');
688 $this->assertEquals($baoObj1->id
, $result['values'][$baoObj1->id
]['id'], 'Should find id on first object');
689 $this->assertEquals(1, count($result['values']));
691 // fetch second by ID
692 $result = civicrm_api($entityName, 'get', array(
694 $idFieldName => $baoObj2->id
,
696 $this->assertAPISuccess($result);
697 $this->assertTrue(!empty($result['values'][$baoObj2->id
]), 'Should find second object by id');
698 $this->assertEquals($baoObj2->id
, $result['values'][$baoObj2->id
]['id'], 'Should find id on second object');
699 $this->assertEquals(1, count($result['values']));
703 * @dataProvider entities_get
705 public function testNonExistantID_get($Entity) {
706 // cf testAcceptsOnlyID_get
707 $nonExistantID = 30867307034;
708 if (in_array($Entity, $this->toBeImplemented
['get'])) {
712 $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID));
714 // redundant with testAcceptsOnlyID_get
715 if ($result['is_error']) {
720 $this->assertArrayHasKey('version', $result);
721 $this->assertEquals(3, $result['version']);
722 if (!in_array($Entity, $this->onlyIDNonZeroCount
['get'])) {
723 $this->assertEquals(0, $result['count']);
727 /** testing the _create **/
730 * @dataProvider toBeSkipped_create
731 entities that don't need a create action
733 public function testNotImplemented_create($Entity) {
734 $result = civicrm_api($Entity, 'Create', array('version' => 3));
735 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
736 $this->assertContains(strtolower("API ($Entity, Create) does not exist"), strtolower($result['error_message']));
740 * @dataProvider entities
741 * @expectedException PHPUnit_Framework_Error
743 public function testWithoutParam_create($Entity) {
744 // should create php complaining that a param is missing
745 $result = civicrm_api($Entity, 'Create');
749 * @dataProvider entities_create
751 public function testEmptyParam_create($Entity) {
752 $this->markTestIncomplete("fixing this test to test the api functions fails on numberous tests
753 which will either create a completely blank entity (batch, participant status) or
754 have a damn good crack at it (e.g mailing job). Marking this as incomplete beats false success");
757 if (in_array($Entity, $this->toBeImplemented
['create'])) {
758 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
761 $result = $this->callAPIFailure($Entity, 'Create', array());
762 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
766 * @dataProvider entities_create
768 * Check that create doesn't work with an invalid
770 public function testInvalidID_create($Entity) {
771 // turn test off for noew
772 $this->markTestIncomplete("Entity [ $Entity ] cannot be mocked - no known DAO");
774 if (in_array($Entity, $this->toBeImplemented
['create'])) {
775 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
778 $result = $this->callAPIFailure($Entity, 'Create', array('id' => 999));
782 * @dataProvider entities
784 public function testCreateWrongTypeParamTag_create() {
785 $result = civicrm_api("Tag", 'Create', 'this is not a string');
786 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
787 $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
791 * @dataProvider entities_updatesingle
793 * limitations include the problem with avoiding loops when creating test objects -
794 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
795 * Currency - only seems to support US
797 public function testCreateSingleValueAlter($entityName) {
798 if (in_array($entityName, $this->toBeImplemented
['create'])) {
799 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
803 $baoString = _civicrm_api3_get_DAO($entityName);
804 $this->assertNotEmpty($baoString, $entityName);
805 $this->assertNotEmpty($entityName, $entityName);
806 $fieldsGet = $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'get'));
807 if($entityName != 'Pledge'){
808 $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'create'));
810 $fields = $fields['values'];
811 $return = array_keys($fieldsGet['values']);
812 $valuesNotToReturn = $this->getKnownUnworkablesUpdateSingle($entityName, 'break_return');
813 // these can't be requested as return values
814 $entityValuesThatDoNotWork = array_merge(
815 $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_update'),
816 $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_return'),
820 $return = array_diff($return,$valuesNotToReturn);
821 $baoObj = new CRM_Core_DAO();
822 $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0);
824 $getEntities = $this->callAPISuccess($entityName, 'get', array(
832 // lets use first rather than assume only one exists
833 $entity = $getEntities['values'][0];
834 $entity2 = $getEntities['values'][1];
835 $this->deletableTestObjects
[$baoString][] = $entity['id'];
836 $this->deletableTestObjects
[$baoString][] = $entity2['id'];
837 foreach ($fields as $field => $specs) {
839 if (!empty($specs['uniquename'])) {
840 $fieldName = $specs['uniquename'];
842 if ($field == 'currency' ||
$field == 'id' ||
$field == strtolower($entityName) . '_id'
843 ||
in_array($field,$entityValuesThatDoNotWork)) {
844 //@todo id & entity_id are correct but we should fix currency & frequency_day
847 $this->assertArrayHasKey('type', $specs, "the _spec function for $entityName field $field does not specify the type");
848 switch ($specs['type']) {
849 case CRM_Utils_Type
::T_DATE
:
850 case CRM_Utils_Type
::T_TIMESTAMP
:
851 $entity[$fieldName] = '2012-05-20';
853 //case CRM_Utils_Type::T_DATETIME:
856 $entity[$fieldName] = '2012-05-20 03:05:20';
859 case CRM_Utils_Type
::T_STRING
:
860 case CRM_Utils_Type
::T_BLOB
:
861 case CRM_Utils_Type
::T_MEDIUMBLOB
:
862 case CRM_Utils_Type
::T_TEXT
:
863 case CRM_Utils_Type
::T_LONGTEXT
:
864 case CRM_Utils_Type
::T_EMAIL
:
865 $entity[$fieldName] = substr('New String',0, CRM_Utils_Array
::Value('maxlength',$specs,100));
868 case CRM_Utils_Type
::T_INT
:
869 // probably created with a 1
870 $entity[$fieldName] = '6';
871 if (!empty($specs['FKClassName'])) {
872 if($specs['FKClassName'] == $baoString){
873 $entity[$fieldName] = (string) $entity2['id'];
876 $uniqueName = CRM_Utils_Array
::value('uniqueName', $specs);
877 $entity[$fieldName] = (string) empty($entity2[$field]) ? CRM_Utils_Array
::value($uniqueName, $entity2) : $entity2[$field];
878 //todo - there isn't always something set here - & our checking on unset values is limited
879 if (empty($entity[$field])) {
880 unset($entity[$field]);
886 case CRM_Utils_Type
::T_BOOLEAN
:
887 // probably created with a 1
888 $entity[$fieldName] = '0';
891 case CRM_Utils_Type
::T_FLOAT
:
892 case CRM_Utils_Type
::T_MONEY
:
893 $entity[$field] = '22.75';
896 case CRM_Utils_Type
::T_URL
:
897 $entity[$field] = 'warm.beer.com';
899 if (!empty($specs['pseudoconstant'])) {
900 $options = $this->callAPISuccess($entityName, 'getoptions', array('context' => 'create', 'field' => $field));
901 if (empty($options['values'])) {
902 //eg. pdf_format id doesn't ship with any
903 if(isset($specs['pseudoconstant']['optionGroupName'])) {
904 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array('name' => 'pdf_format', 'return' => 'id'));
905 $optionValue = $this->callAPISuccess('option_value', 'create', array('option_group_id' => $optionGroupID, 'label' => 'new option value'));
906 $options['values'][] = $optionValue['id'];
909 $entity[$field] = array_rand($options['values']);
911 $updateParams = array(
912 'id' => $entity['id'],
913 $field => isset($entity[$field]) ?
$entity[$field] : NULL,
916 $update = $this->callAPISuccess($entityName, 'create', $updateParams);
917 $checkParams = array(
918 'id' => $entity['id'],
927 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
928 $this->assertAPIArrayComparison($entity, $checkEntity, array(), "checking if $fieldName was correctly updaetd\n" . print_r(array('update-params' => $updateParams, 'update-result' => $update, 'getsingle-params' => $checkParams, 'getsingle-result' => $checkEntity, 'expected entity' => $entity), TRUE));
933 /** testing the _getFields **/
935 /** testing the _delete **/
938 * @dataProvider toBeSkipped_delete
939 entities that don't need a delete action
941 public function testNotImplemented_delete($Entity) {
942 $nonExistantID = 151416349;
943 $result = civicrm_api($Entity, 'Delete', array('version' => 3, 'id' => $nonExistantID));
944 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
945 $this->assertContains(strtolower("API ($Entity, Delete) does not exist"), strtolower($result['error_message']));
949 * @dataProvider entities
950 * @expectedException PHPUnit_Framework_Error
952 public function testWithoutParam_delete($Entity) {
953 // should delete php complaining that a param is missing
954 $result = civicrm_api($Entity, 'Delete');
958 * @dataProvider entities_delete
960 public function testEmptyParam_delete($Entity) {
961 if (in_array($Entity, $this->toBeImplemented
['delete'])) {
962 // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
965 $result = civicrm_api($Entity, 'Delete', array());
966 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
967 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
970 * @dataProvider entities_delete
972 public function testInvalidID_delete($Entity) {
973 // turn test off for now
974 $this->markTestIncomplete("Entity [ $Entity ] cannot be mocked - no known DAO");
976 if (in_array($Entity, $this->toBeImplemented
['delete'])) {
977 // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
980 $result = $this->callAPIFailure($Entity, 'Delete', array('id' => 999));
983 * @dataProvider entities
985 public function testDeleteWrongTypeParamTag_delete() {
986 $result = civicrm_api("Tag", 'Delete', 'this is not a string');
987 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
988 $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
992 * Create two entities and make sure delete action only deletes one!
994 * @dataProvider entities_delete
996 * limitations include the problem with avoiding loops when creating test objects -
997 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
998 * Currency - only seems to support US
1000 public function testByID_delete($entityName) {
1001 // turn test off for noew
1002 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
1005 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
1006 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
1009 $startCount = $this->callAPISuccess($entityName, 'getcount', array());
1011 $baos = $this->getMockableBAOObjects($entityName, $createcount);
1012 list($baoObj1, $baoObj2) = $baos;
1014 // make sure exactly 2 exist
1015 $result = $this->callAPISuccess($entityName, 'getcount', array(),
1016 $createcount +
$startCount
1019 $this->callAPISuccess($entityName, 'delete', array('id' => $baoObj2->id
));
1020 //make sure 1 less exists now
1021 $result = $this->callAPISuccess($entityName, 'getcount', array(),
1022 ($createcount +
$startCount) -1
1025 //make sure id #1 exists
1026 $result = $this->callAPISuccess($entityName, 'getcount', array('id' => $baoObj1->id
),
1029 //make sure id #2 desn't exist
1030 $result = $this->callAPISuccess($entityName, 'getcount', array('id' => $baoObj2->id
),
1036 * @param $entityName
1039 * @internal param $entityName
1043 private function getMockableBAOObjects($entityName, $count = 2) {
1044 $baoString = _civicrm_api3_get_DAO($entityName);
1045 if (empty($baoString)) {
1046 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
1051 while($i < $count) {
1053 $baoObj = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
1054 $this->assertTrue(is_integer($baoObj->id
), 'check first id');
1055 $this->deletableTestObjects
[$baoString][] = $baoObj->id
;
1064 * Verify that HTML metacharacters provided as inputs appear consistently
1067 * At time of writing, the encoding scheme requires (for example) that an
1068 * event title be partially-HTML-escaped before writing to DB. To provide
1069 * consistency, the API must perform extra encoding and decoding on some
1072 * In this example, the event 'title' is subject to encoding, but the
1073 * event 'description' is not.
1075 public function testEncodeDecodeConsistency() {
1077 $createResult = civicrm_api('Event', 'Create', array(
1079 'title' => 'CiviCRM <> TheRest',
1080 'description' => 'TheRest <> CiviCRM',
1081 'event_type_id' => 1,
1083 'start_date' => 20081021,
1085 $this->assertAPISuccess($createResult);
1086 $eventId = $createResult['id'];
1087 $this->assertEquals('CiviCRM <> TheRest', $createResult['values'][$eventId]['title']);
1088 $this->assertEquals('TheRest <> CiviCRM', $createResult['values'][$eventId]['description']);
1090 // Verify "get" handles decoding in result value
1091 $getByIdResult = civicrm_api('Event', 'Get', array(
1095 $this->assertAPISuccess($getByIdResult);
1096 $this->assertEquals('CiviCRM <> TheRest', $getByIdResult['values'][$eventId]['title']);
1097 $this->assertEquals('TheRest <> CiviCRM', $getByIdResult['values'][$eventId]['description']);
1099 // Verify "get" handles encoding in search value
1100 $getByTitleResult = civicrm_api('Event', 'Get', array(
1102 'title' => 'CiviCRM <> TheRest',
1104 $this->assertAPISuccess($getByTitleResult);
1105 $this->assertEquals('CiviCRM <> TheRest', $getByTitleResult['values'][$eventId]['title']);
1106 $this->assertEquals('TheRest <> CiviCRM', $getByTitleResult['values'][$eventId]['description']);
1108 // Verify that "getSingle" handles decoding
1109 $getSingleResult = $this->callAPISuccess('Event', 'GetSingle', array(
1113 $this->assertEquals('CiviCRM <> TheRest', $getSingleResult['title']);
1114 $this->assertEquals('TheRest <> CiviCRM', $getSingleResult['description']);
1116 // Verify that chaining handles decoding
1117 $chainResult = $this->callAPISuccess('Event', 'Get', array(
1119 'api.event.get' => array(
1122 $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['title']);
1123 $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['description']);
1124 $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['api.event.get']['values'][0]['title']);
1125 $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['api.event.get']['values'][0]['description']);
1127 // Verify that "setvalue" handles encoding for updates
1128 $setValueTitleResult = civicrm_api('Event', 'setvalue', array(
1132 'value' => 'setValueTitle: CiviCRM <> TheRest',
1134 $this->assertAPISuccess($setValueTitleResult);
1135 $this->assertEquals('setValueTitle: CiviCRM <> TheRest', $setValueTitleResult['values']['title']);
1136 $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array(
1139 'field' => 'description',
1140 'value' => 'setValueDescription: TheRest <> CiviCRM',
1142 //$this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue
1143 $this->assertEquals('setValueDescription: TheRest <> CiviCRM', $setValueDescriptionResult['values']['description']);
1147 * Verify that write operations (create/update) use partial HTML-encoding
1149 * In this example, the event 'title' is subject to encoding, but the
1150 * event 'description' is not.
1152 public function testEncodeWrite() {
1154 $createResult = civicrm_api('Event', 'Create', array(
1156 'title' => 'createNew: CiviCRM <> TheRest',
1157 'description' => 'createNew: TheRest <> CiviCRM',
1158 'event_type_id' => 1,
1160 'start_date' => 20081021,
1162 $this->assertAPISuccess($createResult);
1163 $eventId = $createResult['id'];
1164 $this->assertDBQuery('createNew: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
1165 1 => array($eventId, 'Integer')
1167 $this->assertDBQuery('createNew: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
1168 1 => array($eventId, 'Integer')
1171 // Verify that "create" handles encoding for updates
1172 $createWithIdResult = civicrm_api('Event', 'Create', array(
1175 'title' => 'createWithId: CiviCRM <> TheRest',
1176 'description' => 'createWithId: TheRest <> CiviCRM',
1178 $this->assertAPISuccess($createWithIdResult);
1179 $this->assertDBQuery('createWithId: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
1180 1 => array($eventId, 'Integer')
1182 $this->assertDBQuery('createWithId: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
1183 1 => array($eventId, 'Integer')
1186 // Verify that "setvalue" handles encoding for updates
1187 $setValueTitleResult = civicrm_api('Event', 'setvalue', array(
1191 'value' => 'setValueTitle: CiviCRM <> TheRest',
1193 $this->assertAPISuccess($setValueTitleResult);
1194 $this->assertDBQuery('setValueTitle: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
1195 1 => array($eventId, 'Integer')
1197 $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array(
1200 'field' => 'description',
1201 'value' => 'setValueDescription: TheRest <> CiviCRM',
1203 //$this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue
1204 $this->assertAPISuccess($setValueDescriptionResult);
1205 $this->assertDBQuery('setValueDescription: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
1206 1 => array($eventId, 'Integer')