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));
123 * Generate list of all entities.
130 public static function entities($skip = array()) {
131 // The order of operations in here is screwy. In the case where SYNTAX_CONFORMANCE_ENTITIES is
132 // defined, we should be able to parse+return it immediately. However, some weird dependency
133 // crept into the system where civicrm_api('Entity','get') must be called as part of entities()
134 // (even if its return value is ignored).
136 $tmp = civicrm_api('Entity', 'Get', array('version' => 3));
137 if (getenv('SYNTAX_CONFORMANCE_ENTITIES')) {
139 'values' => explode(' ', getenv('SYNTAX_CONFORMANCE_ENTITIES')),
143 if (!is_array($skip)) {
146 $tmp = array_diff($tmp['values'], $skip);
148 foreach ($tmp as $e) {
149 $entities[] = array($e);
155 * Get list of entities for get test.
159 public static function entities_get() {
160 // all the entities, beside the ones flagged
161 return static::entities(static::toBeSkipped_get(TRUE));
167 public static function entities_create() {
168 return static::entities(static::toBeSkipped_create(TRUE));
174 public static function entities_updatesingle() {
175 return static::entities(static::toBeSkipped_updatesingle(TRUE));
181 public static function entities_getlimit() {
182 return static::entities(static::toBeSkipped_getlimit());
185 public static function entities_getSqlOperators() {
186 return static::entities(static::toBeSkipped_getSqlOperators());
192 public static function entities_delete() {
193 return static::entities(static::toBeSkipped_delete(TRUE));
199 public static function entities_getfields() {
200 return static::entities(static::toBeSkipped_getfields(TRUE));
206 public static function custom_data_entities_get() {
207 return static::custom_data_entities();
213 public static function custom_data_entities() {
214 $entities = CRM_Core_BAO_CustomQuery
::$extendsMap;
215 $enabledComponents = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
216 $customDataEntities = array();
217 $invalidEntities = array('Individual', 'Organization', 'Household');
218 $entitiesToFix = array('Case', 'Relationship');
219 foreach ($entities as $entityName => $entity) {
220 if (!in_array($entityName, $invalidEntities)
221 && !in_array($entityName, $entitiesToFix)
223 if (!empty(self
::$componentMap[$entityName]) && empty($enabledComponents[self
::$componentMap[$entityName]])) {
224 CRM_Core_BAO_ConfigSetting
::enableComponent(self
::$componentMap[$entityName]);
226 $customDataEntities[] = array($entityName);
229 return $customDataEntities;
233 * @param bool $sequential
237 public static function toBeSkipped_get($sequential = FALSE) {
238 $entitiesWithoutGet = array(
239 'MailingEventSubscribe',
240 'MailingEventConfirm',
241 'MailingEventResubscribe',
242 'MailingEventUnsubscribe',
245 if ($sequential === TRUE) {
246 return $entitiesWithoutGet;
249 foreach ($entitiesWithoutGet as $e) {
250 $entities[] = array($e);
256 * Mailing Contact Just doesn't support id. We have always insisted on finding a way to
257 * support id in API but in this case the underlying tables are crying out for a restructure
258 * & it just doesn't make sense, on the otherhand Event need id to be existant as pseudo property
259 * is been associated with it, so we need to bypass for get api otherwise it will through pseudo_match validation
261 * @param bool $sequential
264 * Entities that cannot be retrieved by ID
266 public static function toBeSkipped_getByID($sequential = FALSE) {
267 return array('MailingContact', 'Event');
271 * @param bool $sequential
275 public static function toBeSkipped_create($sequential = FALSE) {
276 $entitiesWithoutCreate = array('Constant', 'Entity', 'Location', 'Profile', 'MailingRecipients');
277 if ($sequential === TRUE) {
278 return $entitiesWithoutCreate;
281 foreach ($entitiesWithoutCreate as $e) {
282 $entities[] = array($e);
288 * @param bool $sequential
292 public static function toBeSkipped_delete($sequential = FALSE) {
293 $entitiesWithout = array(
295 'MailingEventConfirm',
296 'MailingEventResubscribe',
297 'MailingEventSubscribe',
298 'MailingEventUnsubscribe',
308 if ($sequential === TRUE) {
309 return $entitiesWithout;
312 foreach ($entitiesWithout as $e) {
313 $entities[] = array($e);
319 * @param bool $sequential
322 * @todo add metadata for ALL these entities
324 public static function toBeSkipped_getfields($sequential = FALSE) {
325 $entitiesWithMetadataNotYetFixed = array('ReportTemplate', 'CustomSearch');
326 if ($sequential === TRUE) {
327 return $entitiesWithMetadataNotYetFixed;
330 foreach ($entitiesWithMetadataNotYetFixed as $e) {
331 $entities[] = array($e);
337 * Generate list of entities to test for get by id functions
338 * @param bool $sequential
340 * Entities to be skipped
342 public static function toBeSkipped_automock($sequential = FALSE) {
343 $entitiesWithoutGet = array(
347 'ParticipantPayment',
356 if ($sequential === TRUE) {
357 return $entitiesWithoutGet;
360 foreach ($entitiesWithoutGet as $e) {
361 $entities[] = array($e);
368 * At this stage exclude the ones that don't pass & add them as we can troubleshoot them
369 * @param bool $sequential
372 public static function toBeSkipped_updatesingle($sequential = FALSE) {
373 $entitiesWithout = array(
375 // pseudo-entity; testUpdateSingleValueAlter doesn't introspect properly on it. Multiple magic fields
380 'MailingEventUnsubscribe',
381 'MailingEventSubscribe',
408 'MailingEventConfirm',
412 'MailingEventResubscribe',
420 'ParticipantPayment',
429 //skip this because it doesn't make sense to update logs,
431 if ($sequential === TRUE) {
432 return $entitiesWithout;
435 foreach ($entitiesWithout as $e) {
440 return array('pledge');
445 * At this stage exclude the ones that don't pass & add them as we can troubleshoot them
447 public static function toBeSkipped_getlimit() {
448 $entitiesWithout = array(
450 //case api has non-std mandatory fields one of (case_id, contact_id, activity_id, contact_id)
452 // non-standard api - has inappropriate mandatory fields & doesn't implement limit
454 // failed 'check that a 5 limit returns 5' - probably is_template field is wrong or something, or could be limit doesn't work right
456 // can't handle creating 25
458 // fails on 5 limit - probably a set up problem
460 //a bit of a pseudoapi - keys by domain
462 return $entitiesWithout;
466 * At this stage exclude the ones that don't pass & add them as we can troubleshoot them
468 public static function toBeSkipped_getSqlOperators() {
469 $entitiesWithout = array(
470 'Case', //case api has non-std mandatory fields one of (case_id, contact_id, activity_id, contact_id)
471 'Contact', // on the todo list!
472 'EntityTag', // non-standard api - has inappropriate mandatory fields & doesn't implement limit
473 'Extension', // can't handle creating 25
474 'Note', // note has a default get that isn't implemented in createTestObject -meaning you don't 'get' them
475 'Setting', //a bit of a pseudoapi - keys by domain
477 return $entitiesWithout;
486 public function getKnownUnworkablesUpdateSingle($entity, $key) {
487 // can't update values are values for which updates don't result in the value being changed
488 $knownFailures = array(
489 'ActionSchedule' => array(
490 'cant_update' => array(
494 'ActivityContact' => array(
495 'cant_update' => array(
497 //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
501 'cant_update' => array(
502 'state_province_id', //issues with country id - need to ensure same country
503 'master_id', //creates relationship
505 'cant_return' => array(),
508 'cant_update' => array(
509 'entity_table', // believe this field is defined in error
511 'cant_return' => array(
516 'cant_update' => array(
520 'MembershipBlock' => array(
521 'cant_update' => array(
522 // The fake/auto-generated values leave us unable to properly cleanup fake data
527 'ContributionSoft' => array(
528 'cant_update' => array(
529 // can't be changed through api
534 'cant_update' => array(
535 'pledge_original_installment_amount',
537 'original_installment_amount',
539 'amount' // can't be changed through API,
541 'break_return' => array(// if these are passed in they are retrieved from the wrong table
544 'contribution_page_id',
545 'financial_account_id',
549 'cant_return' => array(// can't be retrieved from api
550 'honor_type_id', //due to uniquename missing
558 'initial_reminder_day',
559 'additional_reminder_day',
561 'pledge_contribution_page_id',
563 'pledge_campaign_id',
564 'pledge_financial_type_id',
567 'PaymentProcessorType' => array(
568 'cant_update' => array(
571 'break_return' => array(),
572 'cant_return' => array(),
574 'PriceFieldValue' => array(
575 'cant_update' => array(
576 'weight', //won't update as there is no 1 in the same price set
580 if (empty($knownFailures[$entity]) ||
empty($knownFailures[$entity][$key])) {
583 return $knownFailures[$entity][$key];
586 /** testing the _get **/
589 * @dataProvider toBeSkipped_get
590 entities that don't need a get action
593 public function testNotImplemented_get($Entity) {
594 $result = civicrm_api($Entity, 'Get', array('version' => 3));
595 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
596 // $this->assertContains("API ($Entity, Get) does not exist", $result['error_message']);
597 $this->assertRegExp('/API (.*) does not exist/', $result['error_message']);
601 * @dataProvider entities
602 * @expectedException PHPUnit_Framework_Error
605 public function testWithoutParam_get($Entity) {
606 // should get php complaining that a param is missing
607 $result = civicrm_api($Entity, 'Get');
611 * @dataProvider entities
614 public function testGetFields($Entity) {
615 if (in_array($Entity, $this->deprecatedAPI
) ||
$Entity == 'Entity' ||
$Entity == 'CustomValue') {
619 $result = civicrm_api($Entity, 'getfields', array('version' => 3));
620 $this->assertTrue(is_array($result['values']), "$Entity ::get fields doesn't return values array in line " . __LINE__
);
621 foreach ($result['values'] as $key => $value) {
622 $this->assertTrue(is_array($value), $Entity . "::" . $key . " is not an array in line " . __LINE__
);
627 * @dataProvider entities_get
630 public function testEmptyParam_get($Entity) {
632 if (in_array($Entity, $this->toBeImplemented
['get'])) {
633 // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented");
636 $result = civicrm_api($Entity, 'Get', array());
637 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
638 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
642 * @dataProvider entities_get
645 public function testEmptyParam_getString($Entity) {
647 if (in_array($Entity, $this->toBeImplemented
['get'])) {
648 // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented");
651 $result = $this->callAPIFailure($Entity, 'Get', 'string');
652 $this->assertEquals(2000, $result['error_code']);
653 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
657 * @dataProvider entities_get
658 * @Xdepends testEmptyParam_get // no need to test the simple if the empty doesn't work/is skipped. doesn't seem to work
661 public function testSimple_get($Entity) {
662 // $this->markTestSkipped("test gives core error on test server (but not on our locals). Skip until we can get server to pass");
663 if (in_array($Entity, $this->toBeImplemented
['get'])) {
666 $result = civicrm_api($Entity, 'Get', array('version' => 3));
667 // @TODO: list the get that have mandatory params
668 if ($result['is_error']) {
669 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
670 // either id or contact_id or entity_id is one of the field missing
671 $this->assertContains("id", $result['error_message']);
674 $this->assertEquals(3, $result['version']);
675 $this->assertArrayHasKey('count', $result);
676 $this->assertArrayHasKey('values', $result);
681 * @dataProvider custom_data_entities_get
684 public function testCustomDataGet($entityName) {
685 $this->createLoggedInUser();// so subsidiary activities are created
686 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, $entityName . 'Test.php');
687 $customFieldName = 'custom_' . $ids['custom_field_id'];
688 $objects = $this->getMockableBAOObjects($entityName, 1);
689 $params = array('id' => $objects[0]->id
, 'custom_' . $ids['custom_field_id'] => "custom string");
690 $result = $this->callAPISuccess($entityName, 'create', $params);
692 $getParams = array('id' => $result['id'], 'return' => array($customFieldName));
693 $check = $this->callAPISuccess($entityName, 'get', $getParams);
694 $this->assertEquals("custom string", $check['values'][$check['id']][$customFieldName]);
696 $this->customFieldDelete($ids['custom_field_id']);
697 $this->customGroupDelete($ids['custom_group_id']);
698 $this->callAPISuccess($entityName, 'delete', array('id' => $result['id']));
702 * @dataProvider entities_get
705 public function testAcceptsOnlyID_get($Entity) {
706 // big random number. fun fact: if you multiply it by pi^e, the result is another random number, but bigger ;)
707 $nonExistantID = 30867307034;
708 if (in_array($Entity, $this->toBeImplemented
['get'])
709 ||
in_array($Entity, $this->toBeSkipped_getByID())
715 // the below function returns different values and hence an early return
716 // we'll fix this once beta1 is released
719 $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID));
721 if ($result['is_error']) {
722 // just to get a clearer message in the log
723 $this->assertEquals("only id should be enough", $result['error_message']);
725 if (!in_array($Entity, $this->onlyIDNonZeroCount
['get'])) {
726 $this->assertEquals(0, $result['count']);
731 * Create two entities and make sure we can fetch them individually by ID
733 * @dataProvider entities_get
735 * limitations include the problem with avoiding loops when creating test objects -
736 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
737 * Currency - only seems to support US
740 public function testByID_get($entityName) {
741 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
742 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
746 $baos = $this->getMockableBAOObjects($entityName);
747 list($baoObj1, $baoObj2) = $baos;
750 $result = $this->callAPISuccess($entityName, 'get', array(
751 'id' => $baoObj1->id
,
754 $this->assertTrue(!empty($result['values'][$baoObj1->id
]), 'Should find first object by id');
755 $this->assertEquals($baoObj1->id
, $result['values'][$baoObj1->id
]['id'], 'Should find id on first object');
756 $this->assertEquals(1, count($result['values']));
758 // fetch second by ID
759 $result = $this->callAPISuccess($entityName, 'get', array(
760 'id' => $baoObj2->id
,
762 $this->assertTrue(!empty($result['values'][$baoObj2->id
]), 'Should find second object by id');
763 $this->assertEquals($baoObj2->id
, $result['values'][$baoObj2->id
]['id'], 'Should find id on second object');
764 $this->assertEquals(1, count($result['values']));
768 * Ensure that the "get" operation accepts limiting the #result records.
770 * TODO Consider making a separate entity list ("entities_getlimit")
771 * For the moment, the "entities_updatesingle" list should give a good
772 * sense for which entities support createTestObject
774 * @dataProvider entities_getlimit
776 * @param string $entityName
778 public function testLimit($entityName) {
779 $cases = array(); // each case is array(0 => $inputtedApiOptions, 1 => $expectedResultCount)
781 array('options' => array('limit' => NULL)),
783 'check that a NULL limit returns unlimited',
786 array('options' => array('limit' => FALSE)),
788 'check that a FALSE limit returns unlimited',
791 array('options' => array('limit' => 0)),
793 'check that a 0 limit returns unlimited',
796 array('options' => array('limit' => 5)),
798 'check that a 5 limit returns 5',
803 'check that no limit returns 25',
806 $baoString = _civicrm_api3_get_BAO($entityName);
807 if (empty($baoString)) {
808 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
812 // make 30 test items -- 30 > 25 (the default limit)
814 for ($i = 0; $i < 30; $i++
) {
815 $baoObj = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
816 $ids[] = $baoObj->id
;
819 // each case is array(0 => $inputtedApiOptions, 1 => $expectedResultCount)
820 foreach ($cases as $case) {
821 $this->checkLimitAgainstExpected($entityName, $case[0], $case[1], $case[2]);
823 //non preferred / legacy syntax
824 if (isset($case[0]['options']['limit'])) {
825 $this->checkLimitAgainstExpected($entityName, array('rowCount' => $case[0]['options']['limit']), $case[1], $case[2]);
826 $this->checkLimitAgainstExpected($entityName, array('option_limit' => $case[0]['options']['limit']), $case[1], $case[2]);
827 $this->checkLimitAgainstExpected($entityName, array('option.limit' => $case[0]['options']['limit']), $case[1], $case[2]);
830 foreach ($ids as $id) {
831 CRM_Core_DAO
::deleteTestObjects($baoString, array('id' => $id));
837 * Ensure that the "get" operation accepts limiting the #result records.
839 * @dataProvider entities_getSqlOperators
841 * @param string $entityName
843 public function testSqlOperators($entityName) {
844 $baoString = _civicrm_api3_get_BAO($entityName);
845 if (empty($baoString)) {
846 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
849 $entities = $this->callAPISuccess($entityName, 'get', array('options' => array('limit' => 0), 'return' => 'id'));
850 $entities = array_keys($entities['values']);
851 $totalEntities = count($entities);
852 if ($totalEntities < 3) {
854 for ($i = 0; $i < 3 - $totalEntities; $i++
) {
855 $baoObj = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
856 $ids[] = $baoObj->id
;
860 $entities = $this->callAPISuccess($entityName, 'get', array('options' => array('limit' => 0)));
861 $entities = array_keys($entities['values']);
862 $this->assertGreaterThan(2, $totalEntities);
863 $this->callAPISuccess($entityName, 'getsingle', array('id' => array('IN' => array($entities[0]))));
864 $this->callAPISuccessGetCount($entityName, array('id' => array('NOT IN' => array($entities[0]))), $totalEntities - 1);
865 $this->callAPISuccessGetCount($entityName, array('id' => array('>' => $entities[0])), $totalEntities - 1);
869 * Check that get fetches an appropriate number of results
871 * @param string $entityName
872 * Name of entity to test.
873 * @param array $params
875 * @param string $message
877 public function checkLimitAgainstExpected($entityName, $params, $limit, $message) {
878 $result = $this->callAPISuccess($entityName, 'get', $params);
880 $this->assertGreaterThanOrEqual($limit, $result['count'], $message);
881 $this->assertGreaterThanOrEqual($limit, $result['count'], $message);
884 $this->assertEquals($limit, $result['count'], $message);
885 $this->assertEquals($limit, count($result['values']), $message);
890 * Create two entities and make sure we can fetch them individually by ID (e.g. using "contact_id=>2"
893 * @dataProvider entities_get
895 * limitations include the problem with avoiding loops when creating test objects -
896 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
897 * Currency - only seems to support US
899 * @throws \PHPUnit_Framework_IncompleteTestError
901 public function testByIDAlias_get($entityName) {
902 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
903 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
907 $baoString = _civicrm_api3_get_BAO($entityName);
908 if (empty($baoString)) {
909 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
913 $idFieldName = _civicrm_api_get_entity_name_from_camel($entityName) . '_id';
916 $baoObj1 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
917 $this->assertTrue(is_integer($baoObj1->id
), 'check first id');
918 $this->deletableTestObjects
[$baoString][] = $baoObj1->id
;
919 $baoObj2 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
920 $this->assertTrue(is_integer($baoObj2->id
), 'check second id');
921 $this->deletableTestObjects
[$baoString][] = $baoObj2->id
;
924 $result = civicrm_api($entityName, 'get', array(
926 $idFieldName => $baoObj1->id
,
928 $this->assertAPISuccess($result);
929 $this->assertTrue(!empty($result['values'][$baoObj1->id
]), 'Should find first object by id');
930 $this->assertEquals($baoObj1->id
, $result['values'][$baoObj1->id
]['id'], 'Should find id on first object');
931 $this->assertEquals(1, count($result['values']));
933 // fetch second by ID
934 $result = civicrm_api($entityName, 'get', array(
936 $idFieldName => $baoObj2->id
,
938 $this->assertAPISuccess($result);
939 $this->assertTrue(!empty($result['values'][$baoObj2->id
]), 'Should find second object by id');
940 $this->assertEquals($baoObj2->id
, $result['values'][$baoObj2->id
]['id'], 'Should find id on second object');
941 $this->assertEquals(1, count($result['values']));
945 * @dataProvider entities_get
948 public function testNonExistantID_get($Entity) {
949 // cf testAcceptsOnlyID_get
950 $nonExistantID = 30867307034;
951 if (in_array($Entity, $this->toBeImplemented
['get'])) {
955 $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID));
957 // redundant with testAcceptsOnlyID_get
958 if ($result['is_error']) {
962 $this->assertArrayHasKey('version', $result);
963 $this->assertEquals(3, $result['version']);
964 if (!in_array($Entity, $this->onlyIDNonZeroCount
['get'])) {
965 $this->assertEquals(0, $result['count']);
969 /** testing the _create **/
972 * @dataProvider toBeSkipped_create
973 entities that don't need a create action
976 public function testNotImplemented_create($Entity) {
977 $result = civicrm_api($Entity, 'Create', array('version' => 3));
978 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
979 $this->assertContains(strtolower("API ($Entity, Create) does not exist"), strtolower($result['error_message']));
983 * @dataProvider entities
984 * @expectedException PHPUnit_Framework_Error
987 public function testWithoutParam_create($Entity) {
988 // should create php complaining that a param is missing
989 $result = civicrm_api($Entity, 'Create');
993 * @dataProvider entities_create
995 * @throws \PHPUnit_Framework_IncompleteTestError
997 public function testEmptyParam_create($Entity) {
998 $this->markTestIncomplete("fixing this test to test the api functions fails on numberous tests
999 which will either create a completely blank entity (batch, participant status) or
1000 have a damn good crack at it (e.g mailing job). Marking this as incomplete beats false success");
1002 if (in_array($Entity, $this->toBeImplemented
['create'])) {
1003 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
1006 $result = $this->callAPIFailure($Entity, 'Create', array());
1007 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
1011 * @dataProvider entities_create
1013 * Check that create doesn't work with an invalid
1015 * @throws \PHPUnit_Framework_IncompleteTestError
1017 public function testInvalidID_create($Entity) {
1018 // turn test off for noew
1019 $this->markTestIncomplete("Entity [ $Entity ] cannot be mocked - no known DAO");
1021 if (in_array($Entity, $this->toBeImplemented
['create'])) {
1022 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
1025 $result = $this->callAPIFailure($Entity, 'Create', array('id' => 999));
1029 * @dataProvider entities
1031 public function testCreateWrongTypeParamTag_create() {
1032 $result = civicrm_api("Tag", 'Create', 'this is not a string');
1033 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
1034 $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
1038 * @dataProvider entities_updatesingle
1040 * limitations include the problem with avoiding loops when creating test objects -
1041 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
1042 * Currency - only seems to support US
1043 * @param $entityName
1045 public function testCreateSingleValueAlter($entityName) {
1046 if (in_array($entityName, $this->toBeImplemented
['create'])) {
1047 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
1051 $baoString = _civicrm_api3_get_BAO($entityName);
1052 $this->assertNotEmpty($baoString, $entityName);
1053 $this->assertNotEmpty($entityName, $entityName);
1054 $fieldsGet = $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'get'));
1055 if ($entityName != 'Pledge') {
1056 $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'create'));
1058 $fields = $fields['values'];
1059 $return = array_keys($fieldsGet['values']);
1060 $valuesNotToReturn = $this->getKnownUnworkablesUpdateSingle($entityName, 'break_return');
1061 // these can't be requested as return values
1062 $entityValuesThatDoNotWork = array_merge(
1063 $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_update'),
1064 $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_return'),
1068 $return = array_diff($return, $valuesNotToReturn);
1069 $baoObj = new CRM_Core_DAO();
1070 $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0);
1072 $getEntities = $this->callAPISuccess($entityName, 'get', array(
1074 'return' => $return,
1076 'sort' => 'id DESC',
1081 // lets use first rather than assume only one exists
1082 $entity = $getEntities['values'][0];
1083 $entity2 = $getEntities['values'][1];
1084 $this->deletableTestObjects
[$baoString][] = $entity['id'];
1085 $this->deletableTestObjects
[$baoString][] = $entity2['id'];
1086 foreach ($fields as $field => $specs) {
1088 $fieldName = $field;
1089 if (!empty($specs['uniquename'])) {
1090 $fieldName = $specs['uniquename'];
1092 if ($field == 'currency' ||
$field == 'id' ||
$field == strtolower($entityName) . '_id'
1093 ||
in_array($field, $entityValuesThatDoNotWork)
1095 //@todo id & entity_id are correct but we should fix currency & frequency_day
1098 $this->assertArrayHasKey('type', $specs, "the _spec function for $entityName field $field does not specify the type");
1099 switch ($specs['type']) {
1100 case CRM_Utils_Type
::T_DATE
:
1101 case CRM_Utils_Type
::T_TIMESTAMP
:
1102 $entity[$fieldName] = '2012-05-20';
1105 //case CRM_Utils_Type::T_DATETIME:
1108 $entity[$fieldName] = '2012-05-20 03:05:20';
1111 case CRM_Utils_Type
::T_STRING
:
1112 case CRM_Utils_Type
::T_BLOB
:
1113 case CRM_Utils_Type
::T_MEDIUMBLOB
:
1114 case CRM_Utils_Type
::T_TEXT
:
1115 case CRM_Utils_Type
::T_LONGTEXT
:
1116 case CRM_Utils_Type
::T_EMAIL
:
1117 $entity[$fieldName] = substr('New String', 0, CRM_Utils_Array
::Value('maxlength', $specs, 100));
1120 case CRM_Utils_Type
::T_INT
:
1121 // probably created with a 1
1122 if ($fieldName == 'weight') {
1123 $entity[$fieldName] = 2;
1125 elseif (!empty($specs['FKClassName'])) {
1126 if ($specs['FKClassName'] == $baoString) {
1127 $entity[$fieldName] = (string) $entity2['id'];
1130 $uniqueName = CRM_Utils_Array
::value('uniqueName', $specs);
1131 if (!empty($entity[$fieldName])) {
1132 $resetFKTo = array($fieldName => $entity[$fieldName]);
1134 $entity[$fieldName] = (string) empty($entity2[$field]) ? CRM_Utils_Array
::value($uniqueName, $entity2) : $entity2[$field];
1135 //todo - there isn't always something set here - & our checking on unset values is limited
1136 if (empty($entity[$field])) {
1137 unset($entity[$field]);
1142 $entity[$fieldName] = '6';
1146 case CRM_Utils_Type
::T_BOOLEAN
:
1147 // probably created with a 1
1148 $entity[$fieldName] = '0';
1151 case CRM_Utils_Type
::T_FLOAT
:
1152 case CRM_Utils_Type
::T_MONEY
:
1153 $entity[$field] = '22.75';
1156 case CRM_Utils_Type
::T_URL
:
1157 $entity[$field] = 'warm.beer.com';
1159 if (!empty($specs['pseudoconstant'])) {
1160 $options = $this->callAPISuccess($entityName, 'getoptions', array('context' => 'create', 'field' => $field));
1161 if (empty($options['values'])) {
1162 //eg. pdf_format id doesn't ship with any
1163 if (isset($specs['pseudoconstant']['optionGroupName'])) {
1164 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array(
1165 'name' => 'pdf_format',
1168 $optionValue = $this->callAPISuccess('option_value', 'create', array(
1169 'option_group_id' => $optionGroupID,
1170 'label' => 'new option value'
1172 $options['values'][] = $optionValue['id'];
1175 $entity[$field] = array_rand($options['values']);
1177 $updateParams = array(
1178 'id' => $entity['id'],
1179 $field => isset($entity[$field]) ?
$entity[$field] : NULL,
1181 if (isset($updateParams['financial_type_id']) && in_array($entityName, array('Grant'))) {
1182 //api has special handling on these 2 fields for backward compatibility reasons
1183 $entity['contribution_type_id'] = $updateParams['financial_type_id'];
1186 $update = $this->callAPISuccess($entityName, 'create', $updateParams);
1187 $checkParams = array(
1188 'id' => $entity['id'],
1190 'return' => $return,
1192 'sort' => 'id DESC',
1197 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
1198 $this->assertAPIArrayComparison($entity, $checkEntity, array(), "checking if $fieldName was correctly updated\n" . print_r(array(
1199 'update-params' => $updateParams,
1200 'update-result' => $update,
1201 'getsingle-params' => $checkParams,
1202 'getsingle-result' => $checkEntity,
1203 'expected entity' => $entity
1206 //reset the foreign key fields because otherwise our cleanup routine fails & some other unexpected stuff can kick in
1207 $entity = array_merge($entity, $resetFKTo);
1208 $updateParams = array_merge($updateParams, $resetFKTo);
1209 $this->callAPISuccess($entityName, 'create', $updateParams);
1210 if (isset($updateParams['financial_type_id']) && in_array($entityName, array('Grant'))) {
1211 //api has special handling on these 2 fields for backward compatibility reasons
1212 $entity['contribution_type_id'] = $updateParams['financial_type_id'];
1219 /** testing the _getFields **/
1221 /** testing the _delete **/
1224 * @dataProvider toBeSkipped_delete
1225 entities that don't need a delete action
1228 public function testNotImplemented_delete($Entity) {
1229 $nonExistantID = 151416349;
1230 $result = civicrm_api($Entity, 'Delete', array('version' => 3, 'id' => $nonExistantID));
1231 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
1232 $this->assertContains(strtolower("API ($Entity, Delete) does not exist"), strtolower($result['error_message']));
1236 * @dataProvider entities
1237 * @expectedException PHPUnit_Framework_Error
1240 public function testWithoutParam_delete($Entity) {
1241 // should delete php complaining that a param is missing
1242 $result = civicrm_api($Entity, 'Delete');
1246 * @dataProvider entities_delete
1249 public function testEmptyParam_delete($Entity) {
1250 if (in_array($Entity, $this->toBeImplemented
['delete'])) {
1251 // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
1254 $result = civicrm_api($Entity, 'Delete', array());
1255 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
1256 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
1260 * @dataProvider entities_delete
1262 * @throws \PHPUnit_Framework_IncompleteTestError
1264 public function testInvalidID_delete($Entity) {
1265 // turn test off for now
1266 $this->markTestIncomplete("Entity [ $Entity ] cannot be mocked - no known DAO");
1268 if (in_array($Entity, $this->toBeImplemented
['delete'])) {
1269 // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
1272 $result = $this->callAPIFailure($Entity, 'Delete', array('id' => 999));
1276 * @dataProvider entities
1278 public function testDeleteWrongTypeParamTag_delete() {
1279 $result = civicrm_api("Tag", 'Delete', 'this is not a string');
1280 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
1281 $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
1285 * Create two entities and make sure delete action only deletes one!
1287 * @dataProvider entities_delete
1289 * limitations include the problem with avoiding loops when creating test objects -
1290 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
1291 * Currency - only seems to support US
1292 * @param $entityName
1293 * @throws \PHPUnit_Framework_IncompleteTestError
1295 public function testByID_delete($entityName) {
1296 // turn test off for noew
1297 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
1300 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
1301 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
1304 $startCount = $this->callAPISuccess($entityName, 'getcount', array());
1306 $baos = $this->getMockableBAOObjects($entityName, $createcount);
1307 list($baoObj1, $baoObj2) = $baos;
1309 // make sure exactly 2 exist
1310 $result = $this->callAPISuccess($entityName, 'getcount', array(),
1311 $createcount +
$startCount
1314 $this->callAPISuccess($entityName, 'delete', array('id' => $baoObj2->id
));
1315 //make sure 1 less exists now
1316 $result = $this->callAPISuccess($entityName, 'getcount', array(),
1317 ($createcount +
$startCount) - 1
1320 //make sure id #1 exists
1321 $result = $this->callAPISuccess($entityName, 'getcount', array('id' => $baoObj1->id
),
1324 //make sure id #2 desn't exist
1325 $result = $this->callAPISuccess($entityName, 'getcount', array('id' => $baoObj2->id
),
1331 * Create two entities and make sure delete action only deletes one!
1333 * @dataProvider entities_getfields
1336 public function testGetfieldsHasTitle($entity) {
1337 $entities = $this->getEntitiesSupportingCustomFields();
1338 if (in_array($entity, $entities)) {
1339 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, $entity . 'Test.php');
1341 $actions = $this->callAPISuccess($entity, 'getactions', array());
1342 foreach ($actions['values'] as $action) {
1343 if (substr($action, -7) == '_create' ||
substr($action, -4) == '_get' ||
substr($action, -7) == '_delete') {
1344 //getactions can't distinguish between contribution_page.create & contribution_page.create
1347 $fields = $this->callAPISuccess($entity, 'getfields', array('action' => $action));
1348 if (!empty($ids) && in_array($action, array('create', 'get'))) {
1349 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $fields['values']);
1352 foreach ($fields['values'] as $fieldName => $fieldSpec) {
1353 $this->assertArrayHasKey('title', $fieldSpec, "no title for $entity - $fieldName on action $action");
1354 $this->assertNotEmpty($fieldSpec['title'], "empty title for $entity - $fieldName");
1358 $this->customFieldDelete($ids['custom_field_id']);
1359 $this->customGroupDelete($ids['custom_group_id']);
1366 public function getEntitiesSupportingCustomFields() {
1367 $entities = self
::custom_data_entities_get();
1368 $returnEntities = array();
1369 foreach ($entities as $entityArray) {
1370 $returnEntities[] = $entityArray[0];
1372 return $returnEntities;
1376 * @param string $entityName
1381 private function getMockableBAOObjects($entityName, $count = 2) {
1382 $baoString = _civicrm_api3_get_BAO($entityName);
1383 if (empty($baoString)) {
1384 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
1389 while ($i < $count) {
1391 $baoObj = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
1392 $this->assertTrue(is_integer($baoObj->id
), 'check first id');
1393 $this->deletableTestObjects
[$baoString][] = $baoObj->id
;
1402 * Verify that HTML metacharacters provided as inputs appear consistently
1405 * At time of writing, the encoding scheme requires (for example) that an
1406 * event title be partially-HTML-escaped before writing to DB. To provide
1407 * consistency, the API must perform extra encoding and decoding on some
1410 * In this example, the event 'title' is subject to encoding, but the
1411 * event 'description' is not.
1413 public function testEncodeDecodeConsistency() {
1415 $createResult = civicrm_api('Event', 'Create', array(
1417 'title' => 'CiviCRM <> TheRest',
1418 'description' => 'TheRest <> CiviCRM',
1419 'event_type_id' => 1,
1421 'start_date' => 20081021,
1423 $this->assertAPISuccess($createResult);
1424 $eventId = $createResult['id'];
1425 $this->assertEquals('CiviCRM <> TheRest', $createResult['values'][$eventId]['title']);
1426 $this->assertEquals('TheRest <> CiviCRM', $createResult['values'][$eventId]['description']);
1428 // Verify "get" handles decoding in result value
1429 $getByIdResult = civicrm_api('Event', 'Get', array(
1433 $this->assertAPISuccess($getByIdResult);
1434 $this->assertEquals('CiviCRM <> TheRest', $getByIdResult['values'][$eventId]['title']);
1435 $this->assertEquals('TheRest <> CiviCRM', $getByIdResult['values'][$eventId]['description']);
1437 // Verify "get" handles encoding in search value
1438 $getByTitleResult = civicrm_api('Event', 'Get', array(
1440 'title' => 'CiviCRM <> TheRest',
1442 $this->assertAPISuccess($getByTitleResult);
1443 $this->assertEquals('CiviCRM <> TheRest', $getByTitleResult['values'][$eventId]['title']);
1444 $this->assertEquals('TheRest <> CiviCRM', $getByTitleResult['values'][$eventId]['description']);
1446 // Verify that "getSingle" handles decoding
1447 $getSingleResult = $this->callAPISuccess('Event', 'GetSingle', array(
1451 $this->assertEquals('CiviCRM <> TheRest', $getSingleResult['title']);
1452 $this->assertEquals('TheRest <> CiviCRM', $getSingleResult['description']);
1454 // Verify that chaining handles decoding
1455 $chainResult = $this->callAPISuccess('Event', 'Get', array(
1457 'api.event.get' => array(),
1459 $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['title']);
1460 $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['description']);
1461 $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['api.event.get']['values'][0]['title']);
1462 $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['api.event.get']['values'][0]['description']);
1464 // Verify that "setvalue" handles encoding for updates
1465 $setValueTitleResult = civicrm_api('Event', 'setvalue', array(
1469 'value' => 'setValueTitle: CiviCRM <> TheRest',
1471 $this->assertAPISuccess($setValueTitleResult);
1472 $this->assertEquals('setValueTitle: CiviCRM <> TheRest', $setValueTitleResult['values']['title']);
1473 $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array(
1476 'field' => 'description',
1477 'value' => 'setValueDescription: TheRest <> CiviCRM',
1479 //$this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue
1480 $this->assertEquals('setValueDescription: TheRest <> CiviCRM', $setValueDescriptionResult['values']['description']);
1484 * Verify that write operations (create/update) use partial HTML-encoding
1486 * In this example, the event 'title' is subject to encoding, but the
1487 * event 'description' is not.
1489 public function testEncodeWrite() {
1491 $createResult = civicrm_api('Event', 'Create', array(
1493 'title' => 'createNew: CiviCRM <> TheRest',
1494 'description' => 'createNew: TheRest <> CiviCRM',
1495 'event_type_id' => 1,
1497 'start_date' => 20081021,
1499 $this->assertAPISuccess($createResult);
1500 $eventId = $createResult['id'];
1501 $this->assertDBQuery('createNew: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
1502 1 => array($eventId, 'Integer'),
1504 $this->assertDBQuery('createNew: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
1505 1 => array($eventId, 'Integer'),
1508 // Verify that "create" handles encoding for updates
1509 $createWithIdResult = civicrm_api('Event', 'Create', array(
1512 'title' => 'createWithId: CiviCRM <> TheRest',
1513 'description' => 'createWithId: TheRest <> CiviCRM',
1515 $this->assertAPISuccess($createWithIdResult);
1516 $this->assertDBQuery('createWithId: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
1517 1 => array($eventId, 'Integer'),
1519 $this->assertDBQuery('createWithId: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
1520 1 => array($eventId, 'Integer'),
1523 // Verify that "setvalue" handles encoding for updates
1524 $setValueTitleResult = civicrm_api('Event', 'setvalue', array(
1528 'value' => 'setValueTitle: CiviCRM <> TheRest',
1530 $this->assertAPISuccess($setValueTitleResult);
1531 $this->assertDBQuery('setValueTitle: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
1532 1 => array($eventId, 'Integer'),
1534 $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array(
1537 'field' => 'description',
1538 'value' => 'setValueDescription: TheRest <> CiviCRM',
1540 //$this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue
1541 $this->assertAPISuccess($setValueDescriptionResult);
1542 $this->assertDBQuery('setValueDescription: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
1543 1 => array($eventId, 'Integer'),