3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 that the core actions for APIv3 entities comply with standard syntax+behavior.
34 * By default, this tests all API entities. To only test specific entities, call phpunit with
35 * environment variable SYNTAX_CONFORMANCE_ENTITIES, e.g.
37 * env SYNTAX_CONFORMANCE_ENTITIES="Contact Event" ./scripts/phpunit api_v3_SyntaxConformanceTest
39 * @package CiviCRM_APIv3
40 * @subpackage API_Core
42 class api_v3_SyntaxConformanceTest
extends CiviUnitTestCase
{
43 protected $_apiversion = 3;
46 * @var array e.g. $this->deletes['CRM_Contact_DAO_Contact'][] = $contactID;
48 protected $deletableTestObjects;
50 /** This test case doesn't require DB reset */
51 public $DBResetRequired = FALSE;
55 /** Map custom group entities to civicrm components */
56 static $componentMap = array(
57 'Contribution' => 'CiviContribute',
58 'Membership' => 'CiviMember',
59 'Participant' => 'CiviEvent',
60 'Event' => 'CiviEvent',
62 'Pledge' => 'CiviPledge',
63 'Grant' => 'CiviGrant',
64 'Campaign' => 'CiviCampaign',
65 'Survey' => 'CiviCampaign',
68 /* they are two types of missing APIs:
69 - Those that are to be implemented
70 (in some future version when someone steps in -hint hint-). List the entities in toBeImplemented[ {$action} ]
71 Those that don't exist
72 and that will never exist (eg an obsoleted Entity
73 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)
75 public function setUp() {
77 $this->enableCiviCampaign();
78 $this->toBeImplemented
['get'] = array(
88 $this->toBeImplemented
['create'] = array(
99 $this->toBeImplemented
['delete'] = array(
109 $this->onlyIDNonZeroCount
['get'] = array('ActivityType', 'Entity', 'Domain', 'Setting');
110 $this->deprecatedAPI
= array('Location', 'ActivityType', 'SurveyRespondant');
111 $this->deletableTestObjects
= array();
114 public function tearDown() {
115 foreach ($this->deletableTestObjects
as $entityName => $entities) {
116 foreach ($entities as $entityID) {
117 CRM_Core_DAO
::deleteTestObjects($entityName, array('id' => $entityID));
127 public static function entities($skip = NULL) {
128 // The order of operations in here is screwy. In the case where SYNTAX_CONFORMANCE_ENTITIES is
129 // defined, we should be able to parse+return it immediately. However, some weird dependency
130 // crept into the system where civicrm_api('Entity','get') must be called as part of entities()
131 // (even if its return value is ignored).
133 $tmp = civicrm_api('Entity', 'Get', array('version' => 3));
134 if (getenv('SYNTAX_CONFORMANCE_ENTITIES')) {
136 'values' => explode(' ', getenv('SYNTAX_CONFORMANCE_ENTITIES')),
140 if (!is_array($skip)) {
143 $tmp = array_diff($tmp['values'], $skip);
145 foreach ($tmp as $e) {
146 $entities[] = array($e);
154 public static function entities_get() {
155 // all the entities, beside the ones flagged
156 return static::entities(static::toBeSkipped_get(TRUE));
162 public static function entities_create() {
163 return static::entities(static::toBeSkipped_create(TRUE));
169 public static function entities_updatesingle() {
170 return static::entities(static::toBeSkipped_updatesingle(TRUE));
176 public static function entities_getlimit() {
177 return static::entities(static::toBeSkipped_getlimit());
180 public static function entities_getSqlOperators() {
181 return static::entities(static::toBeSkipped_getSqlOperators());
187 public static function entities_delete() {
188 return static::entities(static::toBeSkipped_delete(TRUE));
194 public static function entities_getfields() {
195 return static::entities(static::toBeSkipped_getfields(TRUE));
201 public static function custom_data_entities_get() {
202 return static::custom_data_entities();
208 public static function custom_data_entities() {
209 $entities = CRM_Core_BAO_CustomQuery
::$extendsMap;
210 $enabledComponents = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
211 $customDataEntities = array();
212 $invalidEntities = array('Individual', 'Organization', 'Household');
213 $entitiesToFix = array('Case', 'Relationship');
214 foreach ($entities as $entityName => $entity) {
215 if (!in_array($entityName, $invalidEntities)
216 && !in_array($entityName, $entitiesToFix)
218 if (!empty(self
::$componentMap[$entityName]) && empty($enabledComponents[self
::$componentMap[$entityName]])) {
219 CRM_Core_BAO_ConfigSetting
::enableComponent(self
::$componentMap[$entityName]);
221 $customDataEntities[] = array($entityName);
224 return $customDataEntities;
228 * @param bool $sequential
232 public static function toBeSkipped_get($sequential = FALSE) {
233 $entitiesWithoutGet = array(
234 'MailingEventSubscribe',
235 'MailingEventConfirm',
236 'MailingEventResubscribe',
237 'MailingEventUnsubscribe',
240 if ($sequential === TRUE) {
241 return $entitiesWithoutGet;
244 foreach ($entitiesWithoutGet as $e) {
245 $entities[] = array($e);
251 * Mailing Contact Just doesn't support id. We have always insisted on finding a way to
252 * support id in API but in this case the underlying tables are crying out for a restructure
253 * & it just doesn't make sense, on the otherhand Event need id to be existant as pseudo property
254 * is been associated with it, so we need to bypass for get api otherwise it will through pseudo_match validation
256 * @param bool $sequential
259 * Entities that cannot be retrieved by ID
261 public static function toBeSkipped_getByID($sequential = FALSE) {
262 return array('MailingContact', 'Event');
266 * @param bool $sequential
270 public static function toBeSkipped_create($sequential = FALSE) {
271 $entitiesWithoutCreate = array('Constant', 'Entity', 'Location', 'Profile', 'MailingRecipients');
272 if ($sequential === TRUE) {
273 return $entitiesWithoutCreate;
276 foreach ($entitiesWithoutCreate as $e) {
277 $entities[] = array($e);
283 * @param bool $sequential
287 public static function toBeSkipped_delete($sequential = FALSE) {
288 $entitiesWithout = array(
290 'MailingEventConfirm',
291 'MailingEventResubscribe',
292 'MailingEventSubscribe',
293 'MailingEventUnsubscribe',
303 if ($sequential === TRUE) {
304 return $entitiesWithout;
307 foreach ($entitiesWithout as $e) {
308 $entities[] = array($e);
314 * @param bool $sequential
317 * @todo add metadata for ALL these entities
319 public static function toBeSkipped_getfields($sequential = FALSE) {
320 $entitiesWithMetadataNotYetFixed = array('ReportTemplate', 'CustomSearch');
321 if ($sequential === TRUE) {
322 return $entitiesWithMetadataNotYetFixed;
325 foreach ($entitiesWithMetadataNotYetFixed as $e) {
326 $entities[] = array($e);
332 * Generate list of entities to test for get by id functions
333 * @param bool $sequential
335 * Entities to be skipped
337 public static function toBeSkipped_automock($sequential = FALSE) {
338 $entitiesWithoutGet = array(
342 'ParticipantPayment',
351 if ($sequential === TRUE) {
352 return $entitiesWithoutGet;
355 foreach ($entitiesWithoutGet as $e) {
356 $entities[] = array($e);
363 * At this stage exclude the ones that don't pass & add them as we can troubleshoot them
365 public static function toBeSkipped_updatesingle($sequential = FALSE) {
366 $entitiesWithout = array(
368 // pseudo-entity; testUpdateSingleValueAlter doesn't introspect properly on it. Multiple magic fields
373 'MailingEventUnsubscribe',
374 'MailingEventSubscribe',
401 'MailingEventConfirm',
405 'MailingEventResubscribe',
413 'ParticipantPayment',
422 //skip this because it doesn't make sense to update logs,
424 if ($sequential === TRUE) {
425 return $entitiesWithout;
428 foreach ($entitiesWithout as $e) {
433 return array('pledge');
438 * At this stage exclude the ones that don't pass & add them as we can troubleshoot them
440 public static function toBeSkipped_getlimit() {
441 $entitiesWithout = array(
443 //case api has non-std mandatory fields one of (case_id, contact_id, activity_id, contact_id)
445 // non-standard api - has inappropriate mandatory fields & doesn't implement limit
447 // failed 'check that a 5 limit returns 5' - probably is_template field is wrong or something, or could be limit doesn't work right
449 // can't handle creating 25
451 // fails on 5 limit - probably a set up problem
453 //a bit of a pseudoapi - keys by domain
455 return $entitiesWithout;
459 * At this stage exclude the ones that don't pass & add them as we can troubleshoot them
461 public static function toBeSkipped_getSqlOperators() {
462 $entitiesWithout = array(
463 'Case', //case api has non-std mandatory fields one of (case_id, contact_id, activity_id, contact_id)
464 'Contact', // on the todo list!
465 'EntityTag', // non-standard api - has inappropriate mandatory fields & doesn't implement limit
466 'Extension', // can't handle creating 25
467 'Note', // note has a default get that isn't implemented in createTestObject -meaning you don't 'get' them
468 'Setting', //a bit of a pseudoapi - keys by domain
470 return $entitiesWithout;
479 public function getKnownUnworkablesUpdateSingle($entity, $key) {
480 // can't update values are values for which updates don't result in the value being changed
481 $knownFailures = array(
482 'ActionSchedule' => array(
483 'cant_update' => array(
487 'ActivityContact' => array(
488 'cant_update' => array(
490 //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
494 'cant_update' => array(
495 'state_province_id', //issues with country id - need to ensure same country
496 'master_id', //creates relationship
498 'cant_return' => array(),
501 'cant_update' => array(
502 'entity_table', // believe this field is defined in error
504 'cant_return' => array(
509 'cant_update' => array(
513 'MembershipBlock' => array(
514 'cant_update' => array(
515 // The fake/auto-generated values leave us unable to properly cleanup fake data
520 'ContributionSoft' => array(
521 'cant_update' => array(
522 // can't be changed through api
527 'cant_update' => array(
528 'pledge_original_installment_amount',
530 'original_installment_amount',
532 'amount' // can't be changed through API,
534 'break_return' => array(// if these are passed in they are retrieved from the wrong table
537 'contribution_page_id',
538 'financial_account_id',
542 'cant_return' => array(// can't be retrieved from api
543 'honor_type_id', //due to uniquename missing
551 'initial_reminder_day',
552 'additional_reminder_day',
554 'pledge_contribution_page_id',
556 'pledge_campaign_id',
557 'pledge_financial_type_id',
560 'PaymentProcessorType' => array(
561 'cant_update' => array(
564 'break_return' => array(),
565 'cant_return' => array(),
567 'PriceFieldValue' => array(
568 'cant_update' => array(
569 'weight', //won't update as there is no 1 in the same price set
573 if (empty($knownFailures[$entity]) ||
empty($knownFailures[$entity][$key])) {
576 return $knownFailures[$entity][$key];
579 /** testing the _get **/
582 * @dataProvider toBeSkipped_get
583 entities that don't need a get action
585 public function testNotImplemented_get($Entity) {
586 $result = civicrm_api($Entity, 'Get', array('version' => 3));
587 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
588 // $this->assertContains("API ($Entity, Get) does not exist", $result['error_message']);
589 $this->assertRegExp('/API (.*) does not exist/', $result['error_message']);
593 * @dataProvider entities
594 * @expectedException PHPUnit_Framework_Error
596 public function testWithoutParam_get($Entity) {
597 // should get php complaining that a param is missing
598 $result = civicrm_api($Entity, 'Get');
602 * @dataProvider entities
604 public function testGetFields($Entity) {
605 if (in_array($Entity, $this->deprecatedAPI
) ||
$Entity == 'Entity' ||
$Entity == 'CustomValue') {
609 $result = civicrm_api($Entity, 'getfields', array('version' => 3));
610 $this->assertTrue(is_array($result['values']), "$Entity ::get fields doesn't return values array in line " . __LINE__
);
611 foreach ($result['values'] as $key => $value) {
612 $this->assertTrue(is_array($value), $Entity . "::" . $key . " is not an array in line " . __LINE__
);
617 * @dataProvider entities_get
619 public function testEmptyParam_get($Entity) {
621 if (in_array($Entity, $this->toBeImplemented
['get'])) {
622 // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented");
625 $result = civicrm_api($Entity, 'Get', array());
626 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
627 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
631 * @dataProvider entities_get
633 public function testEmptyParam_getString($Entity) {
635 if (in_array($Entity, $this->toBeImplemented
['get'])) {
636 // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented");
639 $result = $this->callAPIFailure($Entity, 'Get', 'string');
640 $this->assertEquals(2000, $result['error_code']);
641 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
645 * @dataProvider entities_get
646 * @Xdepends testEmptyParam_get // no need to test the simple if the empty doesn't work/is skipped. doesn't seem to work
648 public function testSimple_get($Entity) {
649 // $this->markTestSkipped("test gives core error on test server (but not on our locals). Skip until we can get server to pass");
650 if (in_array($Entity, $this->toBeImplemented
['get'])) {
653 $result = civicrm_api($Entity, 'Get', array('version' => 3));
654 // @TODO: list the get that have mandatory params
655 if ($result['is_error']) {
656 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
657 // either id or contact_id or entity_id is one of the field missing
658 $this->assertContains("id", $result['error_message']);
661 $this->assertEquals(3, $result['version']);
662 $this->assertArrayHasKey('count', $result);
663 $this->assertArrayHasKey('values', $result);
668 * @dataProvider custom_data_entities_get
670 public function testCustomDataGet($entityName) {
671 $this->createLoggedInUser();// so subsidiary activities are created
672 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, $entityName . 'Test.php');
673 $customFieldName = 'custom_' . $ids['custom_field_id'];
674 $objects = $this->getMockableBAOObjects($entityName, 1);
675 $params = array('id' => $objects[0]->id
, 'custom_' . $ids['custom_field_id'] => "custom string");
676 $result = $this->callAPISuccess($entityName, 'create', $params);
678 $getParams = array('id' => $result['id'], 'return' => array($customFieldName));
679 $check = $this->callAPISuccess($entityName, 'get', $getParams);
680 $this->assertEquals("custom string", $check['values'][$check['id']][$customFieldName]);
682 $this->customFieldDelete($ids['custom_field_id']);
683 $this->customGroupDelete($ids['custom_group_id']);
684 $this->callAPISuccess($entityName, 'delete', array('id' => $result['id']));
688 * @dataProvider entities_get
690 public function testAcceptsOnlyID_get($Entity) {
691 // big random number. fun fact: if you multiply it by pi^e, the result is another random number, but bigger ;)
692 $nonExistantID = 30867307034;
693 if (in_array($Entity, $this->toBeImplemented
['get'])
694 ||
in_array($Entity, $this->toBeSkipped_getByID())
700 // the below function returns different values and hence an early return
701 // we'll fix this once beta1 is released
704 $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID));
706 if ($result['is_error']) {
707 // just to get a clearer message in the log
708 $this->assertEquals("only id should be enough", $result['error_message']);
710 if (!in_array($Entity, $this->onlyIDNonZeroCount
['get'])) {
711 $this->assertEquals(0, $result['count']);
716 * Create two entities and make sure we can fetch them individually by ID
718 * @dataProvider entities_get
720 * limitations include the problem with avoiding loops when creating test objects -
721 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
722 * Currency - only seems to support US
724 public function testByID_get($entityName) {
725 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
726 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
730 $baos = $this->getMockableBAOObjects($entityName);
731 list($baoObj1, $baoObj2) = $baos;
734 $result = $this->callAPISuccess($entityName, 'get', array(
735 'id' => $baoObj1->id
,
738 $this->assertTrue(!empty($result['values'][$baoObj1->id
]), 'Should find first object by id');
739 $this->assertEquals($baoObj1->id
, $result['values'][$baoObj1->id
]['id'], 'Should find id on first object');
740 $this->assertEquals(1, count($result['values']));
742 // fetch second by ID
743 $result = $this->callAPISuccess($entityName, 'get', array(
744 'id' => $baoObj2->id
,
746 $this->assertTrue(!empty($result['values'][$baoObj2->id
]), 'Should find second object by id');
747 $this->assertEquals($baoObj2->id
, $result['values'][$baoObj2->id
]['id'], 'Should find id on second object');
748 $this->assertEquals(1, count($result['values']));
752 * Ensure that the "get" operation accepts limiting the #result records.
754 * TODO Consider making a separate entity list ("entities_getlimit")
755 * For the moment, the "entities_updatesingle" list should give a good
756 * sense for which entities support createTestObject
758 * @dataProvider entities_getlimit
760 * @param string $entityName
762 public function testLimit($entityName) {
763 $cases = array(); // each case is array(0 => $inputtedApiOptions, 1 => $expectedResultCount)
765 array('options' => array('limit' => NULL)),
767 'check that a NULL limit returns unlimited',
770 array('options' => array('limit' => FALSE)),
772 'check that a FALSE limit returns unlimited',
775 array('options' => array('limit' => 0)),
777 'check that a 0 limit returns unlimited',
780 array('options' => array('limit' => 5)),
782 'check that a 5 limit returns 5',
787 'check that no limit returns 25',
790 $baoString = _civicrm_api3_get_BAO($entityName);
791 if (empty($baoString)) {
792 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
796 // make 30 test items -- 30 > 25 (the default limit)
798 for ($i = 0; $i < 30; $i++
) {
799 $baoObj = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
800 $ids[] = $baoObj->id
;
803 // each case is array(0 => $inputtedApiOptions, 1 => $expectedResultCount)
804 foreach ($cases as $case) {
805 $this->checkLimitAgainstExpected($entityName, $case[0], $case[1], $case[2]);
807 //non preferred / legacy syntax
808 if (isset($case[0]['options']['limit'])) {
809 $this->checkLimitAgainstExpected($entityName, array('rowCount' => $case[0]['options']['limit']), $case[1], $case[2]);
810 $this->checkLimitAgainstExpected($entityName, array('option_limit' => $case[0]['options']['limit']), $case[1], $case[2]);
811 $this->checkLimitAgainstExpected($entityName, array('option.limit' => $case[0]['options']['limit']), $case[1], $case[2]);
814 foreach ($ids as $id) {
815 CRM_Core_DAO
::deleteTestObjects($baoString, array('id' => $id));
821 * Ensure that the "get" operation accepts limiting the #result records.
823 * @dataProvider entities_getSqlOperators
825 * @param string $entityName
827 public function testSqlOperators($entityName) {
828 $baoString = _civicrm_api3_get_BAO($entityName);
829 if (empty($baoString)) {
830 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
833 $entities = $this->callAPISuccess($entityName, 'get', array('options' => array('limit' => 0), 'return' => 'id'));
834 $entities = array_keys($entities['values']);
835 $totalEntities = count($entities);
836 if ($totalEntities < 3) {
838 for ($i = 0; $i < 3 - $totalEntities; $i++
) {
839 $baoObj = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
840 $ids[] = $baoObj->id
;
844 $entities = $this->callAPISuccess($entityName, 'get', array('options' => array('limit' => 0)));
845 $entities = array_keys($entities['values']);
846 $this->assertGreaterThan(2, $totalEntities);
847 $this->callAPISuccess($entityName, 'getsingle', array('id' => array('IN' => array($entities[0]))));
848 $this->callAPISuccessGetCount($entityName, array('id' => array('NOT IN' => array($entities[0]))), $totalEntities - 1);
849 $this->callAPISuccessGetCount($entityName, array('id' => array('>' => $entities[0])), $totalEntities - 1);
853 * Check that get fetches an appropriate number of results
855 * @param string $entityName
856 * Name of entity to test.
857 * @param array $params
859 * @param string $message
861 public function checkLimitAgainstExpected($entityName, $params, $limit, $message) {
862 $result = $this->callAPISuccess($entityName, 'get', $params);
864 $this->assertGreaterThanOrEqual($limit, $result['count'], $message);
865 $this->assertGreaterThanOrEqual($limit, $result['count'], $message);
868 $this->assertEquals($limit, $result['count'], $message);
869 $this->assertEquals($limit, count($result['values']), $message);
874 * Create two entities and make sure we can fetch them individually by ID (e.g. using "contact_id=>2"
877 * @dataProvider entities_get
879 * limitations include the problem with avoiding loops when creating test objects -
880 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
881 * Currency - only seems to support US
883 public function testByIDAlias_get($entityName) {
884 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
885 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
889 $baoString = _civicrm_api3_get_BAO($entityName);
890 if (empty($baoString)) {
891 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
895 $idFieldName = _civicrm_api_get_entity_name_from_camel($entityName) . '_id';
898 $baoObj1 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
899 $this->assertTrue(is_integer($baoObj1->id
), 'check first id');
900 $this->deletableTestObjects
[$baoString][] = $baoObj1->id
;
901 $baoObj2 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
902 $this->assertTrue(is_integer($baoObj2->id
), 'check second id');
903 $this->deletableTestObjects
[$baoString][] = $baoObj2->id
;
906 $result = civicrm_api($entityName, 'get', array(
908 $idFieldName => $baoObj1->id
,
910 $this->assertAPISuccess($result);
911 $this->assertTrue(!empty($result['values'][$baoObj1->id
]), 'Should find first object by id');
912 $this->assertEquals($baoObj1->id
, $result['values'][$baoObj1->id
]['id'], 'Should find id on first object');
913 $this->assertEquals(1, count($result['values']));
915 // fetch second by ID
916 $result = civicrm_api($entityName, 'get', array(
918 $idFieldName => $baoObj2->id
,
920 $this->assertAPISuccess($result);
921 $this->assertTrue(!empty($result['values'][$baoObj2->id
]), 'Should find second object by id');
922 $this->assertEquals($baoObj2->id
, $result['values'][$baoObj2->id
]['id'], 'Should find id on second object');
923 $this->assertEquals(1, count($result['values']));
927 * @dataProvider entities_get
929 public function testNonExistantID_get($Entity) {
930 // cf testAcceptsOnlyID_get
931 $nonExistantID = 30867307034;
932 if (in_array($Entity, $this->toBeImplemented
['get'])) {
936 $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID));
938 // redundant with testAcceptsOnlyID_get
939 if ($result['is_error']) {
943 $this->assertArrayHasKey('version', $result);
944 $this->assertEquals(3, $result['version']);
945 if (!in_array($Entity, $this->onlyIDNonZeroCount
['get'])) {
946 $this->assertEquals(0, $result['count']);
950 /** testing the _create **/
953 * @dataProvider toBeSkipped_create
954 entities that don't need a create action
956 public function testNotImplemented_create($Entity) {
957 $result = civicrm_api($Entity, 'Create', array('version' => 3));
958 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
959 $this->assertContains(strtolower("API ($Entity, Create) does not exist"), strtolower($result['error_message']));
963 * @dataProvider entities
964 * @expectedException PHPUnit_Framework_Error
966 public function testWithoutParam_create($Entity) {
967 // should create php complaining that a param is missing
968 $result = civicrm_api($Entity, 'Create');
972 * @dataProvider entities_create
974 public function testEmptyParam_create($Entity) {
975 $this->markTestIncomplete("fixing this test to test the api functions fails on numberous tests
976 which will either create a completely blank entity (batch, participant status) or
977 have a damn good crack at it (e.g mailing job). Marking this as incomplete beats false success");
979 if (in_array($Entity, $this->toBeImplemented
['create'])) {
980 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
983 $result = $this->callAPIFailure($Entity, 'Create', array());
984 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
988 * @dataProvider entities_create
990 * Check that create doesn't work with an invalid
992 public function testInvalidID_create($Entity) {
993 // turn test off for noew
994 $this->markTestIncomplete("Entity [ $Entity ] cannot be mocked - no known DAO");
996 if (in_array($Entity, $this->toBeImplemented
['create'])) {
997 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
1000 $result = $this->callAPIFailure($Entity, 'Create', array('id' => 999));
1004 * @dataProvider entities
1006 public function testCreateWrongTypeParamTag_create() {
1007 $result = civicrm_api("Tag", 'Create', 'this is not a string');
1008 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
1009 $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
1013 * @dataProvider entities_updatesingle
1015 * limitations include the problem with avoiding loops when creating test objects -
1016 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
1017 * Currency - only seems to support US
1019 public function testCreateSingleValueAlter($entityName) {
1020 if (in_array($entityName, $this->toBeImplemented
['create'])) {
1021 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
1025 $baoString = _civicrm_api3_get_BAO($entityName);
1026 $this->assertNotEmpty($baoString, $entityName);
1027 $this->assertNotEmpty($entityName, $entityName);
1028 $fieldsGet = $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'get'));
1029 if ($entityName != 'Pledge') {
1030 $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'create'));
1032 $fields = $fields['values'];
1033 $return = array_keys($fieldsGet['values']);
1034 $valuesNotToReturn = $this->getKnownUnworkablesUpdateSingle($entityName, 'break_return');
1035 // these can't be requested as return values
1036 $entityValuesThatDoNotWork = array_merge(
1037 $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_update'),
1038 $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_return'),
1042 $return = array_diff($return, $valuesNotToReturn);
1043 $baoObj = new CRM_Core_DAO();
1044 $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0);
1046 $getEntities = $this->callAPISuccess($entityName, 'get', array(
1048 'return' => $return,
1050 'sort' => 'id DESC',
1055 // lets use first rather than assume only one exists
1056 $entity = $getEntities['values'][0];
1057 $entity2 = $getEntities['values'][1];
1058 $this->deletableTestObjects
[$baoString][] = $entity['id'];
1059 $this->deletableTestObjects
[$baoString][] = $entity2['id'];
1060 foreach ($fields as $field => $specs) {
1062 $fieldName = $field;
1063 if (!empty($specs['uniquename'])) {
1064 $fieldName = $specs['uniquename'];
1066 if ($field == 'currency' ||
$field == 'id' ||
$field == strtolower($entityName) . '_id'
1067 ||
in_array($field, $entityValuesThatDoNotWork)
1069 //@todo id & entity_id are correct but we should fix currency & frequency_day
1072 $this->assertArrayHasKey('type', $specs, "the _spec function for $entityName field $field does not specify the type");
1073 switch ($specs['type']) {
1074 case CRM_Utils_Type
::T_DATE
:
1075 case CRM_Utils_Type
::T_TIMESTAMP
:
1076 $entity[$fieldName] = '2012-05-20';
1079 //case CRM_Utils_Type::T_DATETIME:
1082 $entity[$fieldName] = '2012-05-20 03:05:20';
1085 case CRM_Utils_Type
::T_STRING
:
1086 case CRM_Utils_Type
::T_BLOB
:
1087 case CRM_Utils_Type
::T_MEDIUMBLOB
:
1088 case CRM_Utils_Type
::T_TEXT
:
1089 case CRM_Utils_Type
::T_LONGTEXT
:
1090 case CRM_Utils_Type
::T_EMAIL
:
1091 $entity[$fieldName] = substr('New String', 0, CRM_Utils_Array
::Value('maxlength', $specs, 100));
1094 case CRM_Utils_Type
::T_INT
:
1095 // probably created with a 1
1096 if ($fieldName == 'weight') {
1097 $entity[$fieldName] = 2;
1099 elseif (!empty($specs['FKClassName'])) {
1100 if ($specs['FKClassName'] == $baoString) {
1101 $entity[$fieldName] = (string) $entity2['id'];
1104 $uniqueName = CRM_Utils_Array
::value('uniqueName', $specs);
1105 if (!empty($entity[$fieldName])) {
1106 $resetFKTo = array($fieldName => $entity[$fieldName]);
1108 $entity[$fieldName] = (string) empty($entity2[$field]) ? CRM_Utils_Array
::value($uniqueName, $entity2) : $entity2[$field];
1109 //todo - there isn't always something set here - & our checking on unset values is limited
1110 if (empty($entity[$field])) {
1111 unset($entity[$field]);
1116 $entity[$fieldName] = '6';
1120 case CRM_Utils_Type
::T_BOOLEAN
:
1121 // probably created with a 1
1122 $entity[$fieldName] = '0';
1125 case CRM_Utils_Type
::T_FLOAT
:
1126 case CRM_Utils_Type
::T_MONEY
:
1127 $entity[$field] = '22.75';
1130 case CRM_Utils_Type
::T_URL
:
1131 $entity[$field] = 'warm.beer.com';
1133 if (!empty($specs['pseudoconstant'])) {
1134 $options = $this->callAPISuccess($entityName, 'getoptions', array('context' => 'create', 'field' => $field));
1135 if (empty($options['values'])) {
1136 //eg. pdf_format id doesn't ship with any
1137 if (isset($specs['pseudoconstant']['optionGroupName'])) {
1138 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array(
1139 'name' => 'pdf_format',
1142 $optionValue = $this->callAPISuccess('option_value', 'create', array(
1143 'option_group_id' => $optionGroupID,
1144 'label' => 'new option value'
1146 $options['values'][] = $optionValue['id'];
1149 $entity[$field] = array_rand($options['values']);
1151 $updateParams = array(
1152 'id' => $entity['id'],
1153 $field => isset($entity[$field]) ?
$entity[$field] : NULL,
1155 if (isset($updateParams['financial_type_id']) && in_array($entityName, array('Grant'))) {
1156 //api has special handling on these 2 fields for backward compatibility reasons
1157 $entity['contribution_type_id'] = $updateParams['financial_type_id'];
1160 $update = $this->callAPISuccess($entityName, 'create', $updateParams);
1161 $checkParams = array(
1162 'id' => $entity['id'],
1164 'return' => $return,
1166 'sort' => 'id DESC',
1171 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
1172 $this->assertAPIArrayComparison($entity, $checkEntity, array(), "checking if $fieldName was correctly updated\n" . print_r(array(
1173 'update-params' => $updateParams,
1174 'update-result' => $update,
1175 'getsingle-params' => $checkParams,
1176 'getsingle-result' => $checkEntity,
1177 'expected entity' => $entity
1180 //reset the foreign key fields because otherwise our cleanup routine fails & some other unexpected stuff can kick in
1181 $entity = array_merge($entity, $resetFKTo);
1182 $updateParams = array_merge($updateParams, $resetFKTo);
1183 $this->callAPISuccess($entityName, 'create', $updateParams);
1184 if (isset($updateParams['financial_type_id']) && in_array($entityName, array('Grant'))) {
1185 //api has special handling on these 2 fields for backward compatibility reasons
1186 $entity['contribution_type_id'] = $updateParams['financial_type_id'];
1193 /** testing the _getFields **/
1195 /** testing the _delete **/
1198 * @dataProvider toBeSkipped_delete
1199 entities that don't need a delete action
1201 public function testNotImplemented_delete($Entity) {
1202 $nonExistantID = 151416349;
1203 $result = civicrm_api($Entity, 'Delete', array('version' => 3, 'id' => $nonExistantID));
1204 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
1205 $this->assertContains(strtolower("API ($Entity, Delete) does not exist"), strtolower($result['error_message']));
1209 * @dataProvider entities
1210 * @expectedException PHPUnit_Framework_Error
1212 public function testWithoutParam_delete($Entity) {
1213 // should delete php complaining that a param is missing
1214 $result = civicrm_api($Entity, 'Delete');
1218 * @dataProvider entities_delete
1220 public function testEmptyParam_delete($Entity) {
1221 if (in_array($Entity, $this->toBeImplemented
['delete'])) {
1222 // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
1225 $result = civicrm_api($Entity, 'Delete', array());
1226 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
1227 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
1231 * @dataProvider entities_delete
1233 public function testInvalidID_delete($Entity) {
1234 // turn test off for now
1235 $this->markTestIncomplete("Entity [ $Entity ] cannot be mocked - no known DAO");
1237 if (in_array($Entity, $this->toBeImplemented
['delete'])) {
1238 // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
1241 $result = $this->callAPIFailure($Entity, 'Delete', array('id' => 999));
1245 * @dataProvider entities
1247 public function testDeleteWrongTypeParamTag_delete() {
1248 $result = civicrm_api("Tag", 'Delete', 'this is not a string');
1249 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
1250 $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
1254 * Create two entities and make sure delete action only deletes one!
1256 * @dataProvider entities_delete
1258 * limitations include the problem with avoiding loops when creating test objects -
1259 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
1260 * Currency - only seems to support US
1262 public function testByID_delete($entityName) {
1263 // turn test off for noew
1264 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
1267 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
1268 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
1271 $startCount = $this->callAPISuccess($entityName, 'getcount', array());
1273 $baos = $this->getMockableBAOObjects($entityName, $createcount);
1274 list($baoObj1, $baoObj2) = $baos;
1276 // make sure exactly 2 exist
1277 $result = $this->callAPISuccess($entityName, 'getcount', array(),
1278 $createcount +
$startCount
1281 $this->callAPISuccess($entityName, 'delete', array('id' => $baoObj2->id
));
1282 //make sure 1 less exists now
1283 $result = $this->callAPISuccess($entityName, 'getcount', array(),
1284 ($createcount +
$startCount) - 1
1287 //make sure id #1 exists
1288 $result = $this->callAPISuccess($entityName, 'getcount', array('id' => $baoObj1->id
),
1291 //make sure id #2 desn't exist
1292 $result = $this->callAPISuccess($entityName, 'getcount', array('id' => $baoObj2->id
),
1298 * Create two entities and make sure delete action only deletes one!
1300 * @dataProvider entities_getfields
1302 public function testGetfieldsHasTitle($entity) {
1303 $entities = $this->getEntitiesSupportingCustomFields();
1304 if (in_array($entity, $entities)) {
1305 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, $entity . 'Test.php');
1307 $actions = $this->callAPISuccess($entity, 'getactions', array());
1308 foreach ($actions['values'] as $action) {
1309 if (substr($action, -7) == '_create' ||
substr($action, -4) == '_get' ||
substr($action, -7) == '_delete') {
1310 //getactions can't distinguish between contribution_page.create & contribution_page.create
1313 $fields = $this->callAPISuccess($entity, 'getfields', array('action' => $action));
1314 if (!empty($ids) && in_array($action, array('create', 'get'))) {
1315 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $fields['values']);
1318 foreach ($fields['values'] as $fieldName => $fieldSpec) {
1319 $this->assertArrayHasKey('title', $fieldSpec, "no title for $entity - $fieldName on action $action");
1320 $this->assertNotEmpty($fieldSpec['title'], "empty title for $entity - $fieldName");
1324 $this->customFieldDelete($ids['custom_field_id']);
1325 $this->customGroupDelete($ids['custom_group_id']);
1332 public function getEntitiesSupportingCustomFields() {
1333 $entities = self
::custom_data_entities_get();
1334 $returnEntities = array();
1335 foreach ($entities as $entityArray) {
1336 $returnEntities[] = $entityArray[0];
1338 return $returnEntities;
1342 * @param string $entityName
1347 private function getMockableBAOObjects($entityName, $count = 2) {
1348 $baoString = _civicrm_api3_get_BAO($entityName);
1349 if (empty($baoString)) {
1350 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
1355 while ($i < $count) {
1357 $baoObj = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
1358 $this->assertTrue(is_integer($baoObj->id
), 'check first id');
1359 $this->deletableTestObjects
[$baoString][] = $baoObj->id
;
1368 * Verify that HTML metacharacters provided as inputs appear consistently
1371 * At time of writing, the encoding scheme requires (for example) that an
1372 * event title be partially-HTML-escaped before writing to DB. To provide
1373 * consistency, the API must perform extra encoding and decoding on some
1376 * In this example, the event 'title' is subject to encoding, but the
1377 * event 'description' is not.
1379 public function testEncodeDecodeConsistency() {
1381 $createResult = civicrm_api('Event', 'Create', array(
1383 'title' => 'CiviCRM <> TheRest',
1384 'description' => 'TheRest <> CiviCRM',
1385 'event_type_id' => 1,
1387 'start_date' => 20081021,
1389 $this->assertAPISuccess($createResult);
1390 $eventId = $createResult['id'];
1391 $this->assertEquals('CiviCRM <> TheRest', $createResult['values'][$eventId]['title']);
1392 $this->assertEquals('TheRest <> CiviCRM', $createResult['values'][$eventId]['description']);
1394 // Verify "get" handles decoding in result value
1395 $getByIdResult = civicrm_api('Event', 'Get', array(
1399 $this->assertAPISuccess($getByIdResult);
1400 $this->assertEquals('CiviCRM <> TheRest', $getByIdResult['values'][$eventId]['title']);
1401 $this->assertEquals('TheRest <> CiviCRM', $getByIdResult['values'][$eventId]['description']);
1403 // Verify "get" handles encoding in search value
1404 $getByTitleResult = civicrm_api('Event', 'Get', array(
1406 'title' => 'CiviCRM <> TheRest',
1408 $this->assertAPISuccess($getByTitleResult);
1409 $this->assertEquals('CiviCRM <> TheRest', $getByTitleResult['values'][$eventId]['title']);
1410 $this->assertEquals('TheRest <> CiviCRM', $getByTitleResult['values'][$eventId]['description']);
1412 // Verify that "getSingle" handles decoding
1413 $getSingleResult = $this->callAPISuccess('Event', 'GetSingle', array(
1417 $this->assertEquals('CiviCRM <> TheRest', $getSingleResult['title']);
1418 $this->assertEquals('TheRest <> CiviCRM', $getSingleResult['description']);
1420 // Verify that chaining handles decoding
1421 $chainResult = $this->callAPISuccess('Event', 'Get', array(
1423 'api.event.get' => array(),
1425 $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['title']);
1426 $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['description']);
1427 $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['api.event.get']['values'][0]['title']);
1428 $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['api.event.get']['values'][0]['description']);
1430 // Verify that "setvalue" handles encoding for updates
1431 $setValueTitleResult = civicrm_api('Event', 'setvalue', array(
1435 'value' => 'setValueTitle: CiviCRM <> TheRest',
1437 $this->assertAPISuccess($setValueTitleResult);
1438 $this->assertEquals('setValueTitle: CiviCRM <> TheRest', $setValueTitleResult['values']['title']);
1439 $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array(
1442 'field' => 'description',
1443 'value' => 'setValueDescription: TheRest <> CiviCRM',
1445 //$this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue
1446 $this->assertEquals('setValueDescription: TheRest <> CiviCRM', $setValueDescriptionResult['values']['description']);
1450 * Verify that write operations (create/update) use partial HTML-encoding
1452 * In this example, the event 'title' is subject to encoding, but the
1453 * event 'description' is not.
1455 public function testEncodeWrite() {
1457 $createResult = civicrm_api('Event', 'Create', array(
1459 'title' => 'createNew: CiviCRM <> TheRest',
1460 'description' => 'createNew: TheRest <> CiviCRM',
1461 'event_type_id' => 1,
1463 'start_date' => 20081021,
1465 $this->assertAPISuccess($createResult);
1466 $eventId = $createResult['id'];
1467 $this->assertDBQuery('createNew: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
1468 1 => array($eventId, 'Integer'),
1470 $this->assertDBQuery('createNew: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
1471 1 => array($eventId, 'Integer'),
1474 // Verify that "create" handles encoding for updates
1475 $createWithIdResult = civicrm_api('Event', 'Create', array(
1478 'title' => 'createWithId: CiviCRM <> TheRest',
1479 'description' => 'createWithId: TheRest <> CiviCRM',
1481 $this->assertAPISuccess($createWithIdResult);
1482 $this->assertDBQuery('createWithId: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
1483 1 => array($eventId, 'Integer'),
1485 $this->assertDBQuery('createWithId: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
1486 1 => array($eventId, 'Integer'),
1489 // Verify that "setvalue" handles encoding for updates
1490 $setValueTitleResult = civicrm_api('Event', 'setvalue', array(
1494 'value' => 'setValueTitle: CiviCRM <> TheRest',
1496 $this->assertAPISuccess($setValueTitleResult);
1497 $this->assertDBQuery('setValueTitle: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
1498 1 => array($eventId, 'Integer'),
1500 $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array(
1503 'field' => 'description',
1504 'value' => 'setValueDescription: TheRest <> CiviCRM',
1506 //$this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue
1507 $this->assertAPISuccess($setValueDescriptionResult);
1508 $this->assertDBQuery('setValueDescription: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
1509 1 => array($eventId, 'Integer'),