3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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;
48 public $_eNoticeCompliant = FALSE;
49 /* they are two types of missing APIs:
50 - Those that are to be implemented
51 (in some future version when someone steps in -hint hint-). List the entities in toBeImplemented[ {$action} ]
52 Those that don't exist
53 and that will never exist (eg an obsoleted Entity
54 they need to be returned by the function toBeSkipped_{$action} (because it has to be a static method and therefore couldn't access a this->toBeSkipped)
59 $this->toBeImplemented
['get'] = array('Profile', 'CustomValue', 'Constant', 'CustomSearch', 'Extension', 'ReportTemplate', 'System', 'Setting');
60 $this->toBeImplemented
['create'] = array('SurveyRespondant', 'OptionGroup', 'MailingRecipients', 'UFMatch', 'LocationType', 'CustomSearch', 'Extension', 'ReportTemplate', 'System');
61 $this->toBeImplemented
['delete'] = array('MembershipPayment', 'OptionGroup', 'SurveyRespondant', 'UFJoin', 'UFMatch', 'Extension', 'LocationType', 'System');
62 $this->onlyIDNonZeroCount
['get'] = array('ActivityType', 'Entity', 'Domain','Setting');
63 $this->deprecatedAPI
= array('Location', 'ActivityType', 'SurveyRespondant');
64 $this->deletableTestObjects
= array();
68 foreach ($this->deletableTestObjects
as $entityName => $entities) {
69 foreach ($entities as $entityID) {
70 CRM_Core_DAO
::deleteTestObjects($entityName, array('id' => $entityID));
76 public static function entities($skip = NULL) {
77 // uncomment to make a quicker run when adding a test
78 //return array(array ('Tag'), array ('Activity') );
79 $tmp = civicrm_api('Entity', 'Get', array('version' => 3));
80 if (!is_array($skip)) {
83 $tmp = array_diff($tmp['values'], $skip);
85 foreach ($tmp as $e) {
86 $entities[] = array($e);
91 public static function entities_get() {
92 // all the entities, beside the ones flagged
93 return static::entities(static::toBeSkipped_get(TRUE));
96 public static function entities_create() {
97 return static::entities(static::toBeSkipped_create(TRUE));
100 public static function entities_updatesingle() {
101 return static::entities(static::toBeSkipped_updatesingle(TRUE));
104 public static function entities_delete() {
105 return static::entities(static::toBeSkipped_delete(TRUE));
108 public static function toBeSkipped_get($sequential = FALSE) {
109 $entitiesWithoutGet = array('MailingEventSubscribe', 'MailingEventConfirm', 'MailingEventResubscribe', 'MailingEventUnsubscribe', 'MailingGroup', 'Location');
110 if ($sequential === TRUE) {
111 return $entitiesWithoutGet;
114 foreach ($entitiesWithoutGet as $e) {
115 $entities[] = array($e);
120 * Mailing Contact Just doesn't support id. We have always insisted on finding a way to
121 * support id in API but in this case the underlying tables are crying out for a restructue
122 * & it just doesn't make sense
123 * @param unknown_type $sequential
124 * @return multitype:string |multitype:multitype:string
126 public static function toBeSkipped_getByID($sequential = FALSE) {
127 return array('MailingContact');
130 public static function toBeSkipped_create($sequential = FALSE) {
131 $entitiesWithoutCreate = array('MailingGroup', 'Constant', 'Entity', 'Location', 'Profile', 'MailingRecipients');
132 if ($sequential === TRUE) {
133 return $entitiesWithoutCreate;
136 foreach ($entitiesWithoutCreate as $e) {
137 $entities[] = array($e);
142 public static function toBeSkipped_delete($sequential = FALSE) {
143 $entitiesWithout = array('MailingGroup', 'Constant', 'Entity', 'Location', 'Domain', 'Profile', 'CustomValue');
144 if ($sequential === TRUE) {
145 return $entitiesWithout;
148 foreach ($entitiesWithout as $e) {
149 $entities[] = array($e);
154 * Generate list of entities to test for get by id functions
155 * @param boolean $sequential
156 * @return multitype:string |multitype:multitype:string
158 public static function toBeSkipped_automock($sequential = FALSE) {
159 $entitiesWithoutGet = array('MailingContact', 'EntityTag', 'Participant', 'ParticipantPayment', 'Setting', 'SurveyRespondant', 'MailingRecipients', 'CustomSearch', 'Extension', 'ReportTemplate', 'System');
160 if ($sequential === TRUE) {
161 return $entitiesWithoutGet;
164 foreach ($entitiesWithoutGet as $e) {
165 $entities[] = array($e);
172 * At this stage exclude the ones that don't pass & add them as we can troubleshoot them
175 public static function toBeSkipped_updatesingle($sequential = FALSE) {
176 $entitiesWithout = array(
181 'MailingEventUnsubscribe',
182 'MailingEventSubscribe',
197 'ParticipantStatusType',
214 'MailingEventConfirm',
218 'MailingEventResubscribe',
226 'ParticipantPayment',
239 if ($sequential === TRUE) {
240 return $entitiesWithout;
243 foreach ($entitiesWithout as $e) {
248 return array('pledge');
252 public function getKnownUnworkablesUpdateSingle($entity, $key){
253 // can't update values are values for which updates don't result in the value being changed
254 $knownFailures = array(
255 'ActionSchedule' => array(
256 'cant_update' => array(
261 'cant_update' => array(
262 'state_province_id', //issues with country id - need to ensure same country
263 'master_id',//creates relationship
265 'cant_return' => array(
269 'cant_update' => array(
270 'pledge_original_installment_amount',
272 'original_installment_amount',
274 'amount' // can't be changed through API
276 'break_return' => array(// if these are passed in they are retrieved from the wrong table
279 'contribution_page_id',
280 'financial_account_id',
284 'cant_return' => array(// can't be retrieved from api
285 'honor_type_id', //due to uniquename missing
293 'initial_reminder_day',
294 'additional_reminder_day',
296 'pledge_contribution_page_id',
298 'pledge_campaign_id',
301 'PaymentProcessorType' => array(
302 'cant_update' => array(
305 'break_return' => array(
307 'cant_return' => array(
311 if(empty($knownFailures[$entity]) ||
empty($knownFailures[$entity][$key])){
314 return $knownFailures[$entity][$key];
317 /** testing the _get **/
320 * @dataProvider toBeSkipped_get
321 entities that don't need a get action
323 public function testNotImplemented_get($Entity) {
324 $result = civicrm_api($Entity, 'Get', array('version' => 3));
325 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
326 $this->assertContains("API ($Entity,Get) does not exist", $result['error_message']);
330 * @dataProvider entities
331 * @expectedException PHPUnit_Framework_Error
333 public function testWithoutParam_get($Entity) {
334 // should get php complaining that a param is missing
335 $result = civicrm_api($Entity, 'Get');
339 * @dataProvider entities
341 public function testGetFields($Entity) {
342 if (in_array($Entity, $this->deprecatedAPI
) ||
$Entity == 'Entity' ||
$Entity == 'CustomValue' ||
$Entity == 'MailingGroup') {
346 $result = civicrm_api($Entity, 'getfields', array('version' => 3));
347 $this->assertTrue(is_array($result['values']), "$Entity ::get fields doesn't return values array in line " . __LINE__
);
348 foreach ($result['values'] as $key => $value) {
349 $this->assertTrue(is_array($value), $Entity . "::" . $key . " is not an array in line " . __LINE__
);
354 * @dataProvider entities_get
356 public function testEmptyParam_get($Entity) {
358 if (in_array($Entity, $this->toBeImplemented
['get'])) {
359 // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented");
362 $result = civicrm_api($Entity, 'Get', array());
363 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
364 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
367 * @dataProvider entities_get
369 public function testEmptyParam_getString($Entity) {
371 if (in_array($Entity, $this->toBeImplemented
['get'])) {
372 // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented");
375 $result = $this->callAPIFailure($Entity, 'Get', 'string');
376 $this->assertEquals(2000, $result['error_code']);
377 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
380 * @dataProvider entities_get
381 * @Xdepends testEmptyParam_get // no need to test the simple if the empty doesn't work/is skipped. doesn't seem to work
383 public function testSimple_get($Entity) {
384 // $this->markTestSkipped("test gives core error on test server (but not on our locals). Skip until we can get server to pass");
385 if (in_array($Entity, $this->toBeImplemented
['get'])) {
388 $result = civicrm_api($Entity, 'Get', array('version' => 3));
389 // @TODO: list the get that have mandatory params
390 if ($result['is_error']) {
391 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
392 // either id or contact_id or entity_id is one of the field missing
393 $this->assertContains("id", $result['error_message']);
396 $this->assertEquals(3, $result['version']);
397 $this->assertArrayHasKey('count', $result);
398 $this->assertArrayHasKey('values', $result);
403 * @dataProvider entities_get
405 public function testAcceptsOnlyID_get($Entity) {
406 // big random number. fun fact: if you multiply it by pi^e, the result is another random number, but bigger ;)
407 $nonExistantID = 30867307034;
408 if (in_array($Entity, $this->toBeImplemented
['get'])
409 ||
in_array($Entity, $this->toBeSkipped_getByID())
415 // the below function returns different values and hence an early return
416 // we'll fix this once beta1 is released
419 $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID));
421 if ($result['is_error']) {
422 // just to get a clearer message in the log
423 $this->assertEquals("only id should be enough", $result['error_message']);
425 if (!in_array($Entity, $this->onlyIDNonZeroCount
['get'])) {
426 $this->assertEquals(0, $result['count']);
431 * Create two entities and make sure we can fetch them individually by ID
433 * @dataProvider entities_get
435 * limitations include the problem with avoiding loops when creating test objects -
436 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
437 * Currency - only seems to support US
439 public function testByID_get($entityName) {
440 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
441 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
445 $baos = $this->getMockableBAOObjects($entityName);
446 list($baoObj1, $baoObj2) = $baos;
449 $result = civicrm_api($entityName, 'get', array(
451 'id' => $baoObj1->id
,
453 $this->assertAPISuccess($result);
454 $this->assertTrue(!empty($result['values'][$baoObj1->id
]), 'Should find first object by id');
455 $this->assertEquals($baoObj1->id
, $result['values'][$baoObj1->id
]['id'], 'Should find id on first object');
456 $this->assertEquals(1, count($result['values']));
458 // fetch second by ID
459 $result = civicrm_api($entityName, 'get', array(
461 'id' => $baoObj2->id
,
463 $this->assertAPISuccess($result);
464 $this->assertTrue(!empty($result['values'][$baoObj2->id
]), 'Should find second object by id');
465 $this->assertEquals($baoObj2->id
, $result['values'][$baoObj2->id
]['id'], 'Should find id on second object');
466 $this->assertEquals(1, count($result['values']));
470 * Create two entities and make sure we can fetch them individually by ID (e.g. using "contact_id=>2"
473 * @dataProvider entities_get
475 * limitations include the problem with avoiding loops when creating test objects -
476 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
477 * Currency - only seems to support US
479 public function testByIDAlias_get($entityName) {
480 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
481 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
485 $baoString = _civicrm_api3_get_DAO($entityName);
486 if (empty($baoString)) {
487 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
491 $idFieldName = _civicrm_api_get_entity_name_from_camel($entityName) . '_id';
494 $baoObj1 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
495 $this->assertTrue(is_integer($baoObj1->id
), 'check first id');
496 $this->deletableTestObjects
[$baoString][] = $baoObj1->id
;
497 $baoObj2 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
498 $this->assertTrue(is_integer($baoObj2->id
), 'check second id');
499 $this->deletableTestObjects
[$baoString][] = $baoObj2->id
;
502 $result = civicrm_api($entityName, 'get', array(
504 $idFieldName => $baoObj1->id
,
506 $this->assertAPISuccess($result);
507 $this->assertTrue(!empty($result['values'][$baoObj1->id
]), 'Should find first object by id');
508 $this->assertEquals($baoObj1->id
, $result['values'][$baoObj1->id
]['id'], 'Should find id on first object');
509 $this->assertEquals(1, count($result['values']));
511 // fetch second by ID
512 $result = civicrm_api($entityName, 'get', array(
514 $idFieldName => $baoObj2->id
,
516 $this->assertAPISuccess($result);
517 $this->assertTrue(!empty($result['values'][$baoObj2->id
]), 'Should find second object by id');
518 $this->assertEquals($baoObj2->id
, $result['values'][$baoObj2->id
]['id'], 'Should find id on second object');
519 $this->assertEquals(1, count($result['values']));
523 * @dataProvider entities_get
525 public function testNonExistantID_get($Entity) {
526 // cf testAcceptsOnlyID_get
527 $nonExistantID = 30867307034;
528 if (in_array($Entity, $this->toBeImplemented
['get'])) {
532 $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID));
534 // redundant with testAcceptsOnlyID_get
535 if ($result['is_error']) {
540 $this->assertArrayHasKey('version', $result);
541 $this->assertEquals(3, $result['version']);
542 if (!in_array($Entity, $this->onlyIDNonZeroCount
['get'])) {
543 $this->assertEquals(0, $result['count']);
547 /** testing the _create **/
550 * @dataProvider toBeSkipped_create
551 entities that don't need a create action
553 public function testNotImplemented_create($Entity) {
554 $result = civicrm_api($Entity, 'Create', array('version' => 3));
555 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
556 $this->assertContains("API ($Entity,Create) does not exist", $result['error_message']);
560 * @dataProvider entities
561 * @expectedException PHPUnit_Framework_Error
563 public function testWithoutParam_create($Entity) {
564 // should create php complaining that a param is missing
565 $result = civicrm_api($Entity, 'Create');
569 * @dataProvider entities_create
571 public function testEmptyParam_create($Entity) {
572 $this->markTestIncomplete("fixing this test to test the api functions fails on numberous tests
573 which will either create a completely blank entity (batch, participant status) or
574 have a damn good crack at it (e.g mailing job). Marking this as incomplete beats false success");
577 if (in_array($Entity, $this->toBeImplemented
['create'])) {
578 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
581 $result = $this->callAPIFailure($Entity, 'Create', array());
582 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
586 * @dataProvider entities_create
588 * Check that create doesn't work with an invalid
590 public function testInvalidID_create($Entity) {
591 // turn test off for noew
592 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
594 if (in_array($Entity, $this->toBeImplemented
['create'])) {
595 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
598 $result = $this->callAPIFailure($Entity, 'Create', array('id' => 999));
602 * @dataProvider entities
604 public function testCreateWrongTypeParamTag_create() {
605 $result = civicrm_api("Tag", 'Create', 'this is not a string');
606 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
607 $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
611 * @dataProvider entities_updatesingle
613 * limitations include the problem with avoiding loops when creating test objects -
614 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
615 * Currency - only seems to support US
617 public function testCreateSingleValueAlter($entityName) {
618 if (in_array($entityName, $this->toBeImplemented
['create'])) {
619 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
623 $baoString = _civicrm_api3_get_DAO($entityName);
624 $this->assertNotEmpty($baoString, $entityName);
625 $this->assertNotEmpty($entityName, $entityName);
626 $fieldsget = $fields = civicrm_api($entityName, 'getfields', array(
627 'version' => 3, 'action' => 'get'
630 if($entityName != 'Pledge'){
631 $fields = civicrm_api($entityName, 'getfields', array(
632 'version' => 3, 'action' => 'create'
636 $fields = $fields['values'];
637 $return = array_keys($fieldsget['values']);
638 $valuesNotToReturn = $this->getKnownUnworkablesUpdateSingle($entityName, 'break_return');
639 // these can't be requested as return values
640 $entityValuesThatDontWork = array_merge(
641 $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_update'),
642 $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_return'),
646 $return = array_diff($return,$valuesNotToReturn);
647 $baoObj = new CRM_Core_DAO();
648 $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0);
649 $getentities = civicrm_api($entityName, 'get', array(
658 // lets use first rather than assume only one exists
659 $entity = $getentities['values'][0];
660 $entity2 = $getentities['values'][1];
661 foreach ($fields as $field => $specs) {
663 if (!empty($specs['uniquename'])) {
664 $fieldName = $specs['uniquename'];
666 if ($field == 'currency' ||
$field == 'id' ||
$field == strtolower($entityName) . '_id'
667 ||
in_array($field,$entityValuesThatDontWork)) {
668 //@todo id & entity_id are correct but we should fix currency & frequency_day
671 switch ($specs['type']) {
672 case CRM_Utils_Type
::T_DATE
:
673 case CRM_Utils_Type
::T_TIMESTAMP
:
674 $entity[$fieldName] = '2012-05-20';
676 //case CRM_Utils_Type::T_DATETIME:
679 $entity[$fieldName] = '2012-05-20 03:05:20';
682 case CRM_Utils_Type
::T_STRING
:
683 case CRM_Utils_Type
::T_BLOB
:
684 case CRM_Utils_Type
::T_MEDIUMBLOB
:
685 case CRM_Utils_Type
::T_TEXT
:
686 case CRM_Utils_Type
::T_LONGTEXT
:
687 case CRM_Utils_Type
::T_EMAIL
:
688 $entity[$fieldName] = substr('New String',0, CRM_Utils_Array
::Value('maxlength',$specs,100));
691 case CRM_Utils_Type
::T_INT
:
692 // probably created with a 1
693 $entity[$fieldName] = '6';
694 if (CRM_Utils_Array
::value('FKClassName', $specs)) {
695 if($specs['FKClassName'] == $baoString){
696 $entity[$fieldName] = (string) $entity2['id'];
699 $entity[$fieldName] = (string) empty($entity2[$field]) ? CRM_Utils_Array
::value($specs['uniqueName'], $entity2) : $entity2[$field];
700 //todo - there isn't always something set here - & our checking on unset values is limited
701 if (empty($entity[$field])) {
702 unset($entity[$field]);
708 case CRM_Utils_Type
::T_BOOLEAN
:
709 // probably created with a 1
710 $entity[$fieldName] = '0';
713 case CRM_Utils_Type
::T_FLOAT
:
714 case CRM_Utils_Type
::T_MONEY
:
715 $entity[$field] = '222';
718 case CRM_Utils_Type
::T_URL
:
719 $entity[$field] = 'warm.beer.com';
721 if (!empty($specs['pseudoconstant']) ||
!empty($specs['enumValues'])) {
722 $options = civicrm_api($entityName, 'getoptions', array('context' => 'create', 'field' => $field, 'version' => 3));
723 if (empty($options['values'])) {
725 $entity[$field] = array_rand($options['values']);
727 $updateParams = array(
729 'id' => $entity['id'],
730 $field => $entity[$field],
733 $update = $this->callAPISuccess($entityName, 'create', $updateParams);
734 $checkParams = array(
735 'id' => $entity['id'],
745 $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams);
746 $this->assertAPIArrayComparison($entity, $checkEntity, array(), "changing field $fieldName\n" . print_r(array('update-params' => $updateParams, 'update-result' => $update, 'getsingle-params' => $checkParams, 'getsingle-result' => $checkEntity, 'expected entity' => $entity), TRUE));
748 $baoObj->deleteTestObjects($baoString);
752 /** testing the _getFields **/
754 /** testing the _delete **/
757 * @dataProvider toBeSkipped_delete
758 entities that don't need a delete action
760 public function testNotImplemented_delete($Entity) {
761 $nonExistantID = 151416349;
762 $result = civicrm_api($Entity, 'Delete', array('version' => 3, 'id' => $nonExistantID));
763 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
764 $this->assertContains("API ($Entity,Delete) does not exist", $result['error_message']);
768 * @dataProvider entities
769 * @expectedException PHPUnit_Framework_Error
771 public function testWithoutParam_delete($Entity) {
772 // should delete php complaining that a param is missing
773 $result = civicrm_api($Entity, 'Delete');
777 * @dataProvider entities_delete
779 public function testEmptyParam_delete($Entity) {
780 if (in_array($Entity, $this->toBeImplemented
['delete'])) {
781 // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
784 $result = civicrm_api($Entity, 'Delete', array());
785 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
786 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
789 * @dataProvider entities_delete
791 public function testInvalidID_delete($Entity) {
792 // turn test off for noew
793 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
795 if (in_array($Entity, $this->toBeImplemented
['delete'])) {
796 // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
799 $result = $this->callAPIFailure($Entity, 'Delete', array('id' => 999));
802 * @dataProvider entities
804 public function testDeleteWrongTypeParamTag_delete() {
805 $result = civicrm_api("Tag", 'Delete', 'this is not a string');
806 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
807 $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
811 * Create two entities and make sure delete action only deletes one!
813 * @dataProvider entities_delete
815 * limitations include the problem with avoiding loops when creating test objects -
816 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
817 * Currency - only seems to support US
819 public function testByID_delete($entityName) {
820 // turn test off for noew
821 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
824 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
825 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
828 $startCount = $this->callAPISuccess($entityName, 'getcount', array());
830 $baos = $this->getMockableBAOObjects($entityName, $createcount);
831 list($baoObj1, $baoObj2) = $baos;
833 // make sure exactly 2 exist
834 $result = $this->callAPISuccess($entityName, 'getcount', array(),
835 $createcount +
$startCount
838 $this->callAPISuccess($entityName, 'delete', array('id' => $baoObj2->id
));
839 //make sure 1 less exists now
840 $result = $this->callAPISuccess($entityName, 'getcount', array(),
841 ($createcount +
$startCount) -1
844 //make sure id #1 exists
845 $result = $this->callAPISuccess($entityName, 'getcount', array('id' => $baoObj1->id
),
848 //make sure id #2 desn't exist
849 $result = $this->callAPISuccess($entityName, 'getcount', array('id' => $baoObj2->id
),
856 */private function getMockableBAOObjects($entityName, $count = 2) {
857 $baoString = _civicrm_api3_get_DAO($entityName);
858 if (empty($baoString)) {
859 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
865 $baoObj = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
866 $this->assertTrue(is_integer($baoObj->id
), 'check first id');
867 $this->deletableTestObjects
[$baoString][] = $baoObj->id
;
876 * Verify that HTML metacharacters provided as inputs appear consistently
879 * At time of writing, the encoding scheme requires (for example) that an
880 * event title be partially-HTML-escaped before writing to DB. To provide
881 * consistency, the API must perform extra encoding and decoding on some
884 * In this example, the event 'title' is subject to encoding, but the
885 * event 'description' is not.
887 public function testEncodeDecodeConsistency() {
889 $createResult = civicrm_api('Event', 'Create', array(
891 'title' => 'CiviCRM <> TheRest',
892 'description' => 'TheRest <> CiviCRM',
893 'event_type_id' => 1,
895 'start_date' => 20081021,
897 $this->assertAPISuccess($createResult);
898 $eventId = $createResult['id'];
899 $this->assertEquals('CiviCRM <> TheRest', $createResult['values'][$eventId]['title']);
900 $this->assertEquals('TheRest <> CiviCRM', $createResult['values'][$eventId]['description']);
902 // Verify "get" handles decoding in result value
903 $getByIdResult = civicrm_api('Event', 'Get', array(
907 $this->assertAPISuccess($getByIdResult);
908 $this->assertEquals('CiviCRM <> TheRest', $getByIdResult['values'][$eventId]['title']);
909 $this->assertEquals('TheRest <> CiviCRM', $getByIdResult['values'][$eventId]['description']);
911 // Verify "get" handles encoding in search value
912 $getByTitleResult = civicrm_api('Event', 'Get', array(
914 'title' => 'CiviCRM <> TheRest',
916 $this->assertAPISuccess($getByTitleResult);
917 $this->assertEquals('CiviCRM <> TheRest', $getByTitleResult['values'][$eventId]['title']);
918 $this->assertEquals('TheRest <> CiviCRM', $getByTitleResult['values'][$eventId]['description']);
920 // Verify that "getSingle" handles decoding
921 $getSingleResult = civicrm_api('Event', 'GetSingle', array(
926 $this->assertAPISuccess($getSingleResult);
927 $this->assertEquals('CiviCRM <> TheRest', $getSingleResult['title']);
928 $this->assertEquals('TheRest <> CiviCRM', $getSingleResult['description']);
930 // Verify that chaining handles decoding
931 $chainResult = civicrm_api('Event', 'Get', array(
934 'api.event.get' => array(
937 $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['title']);
938 $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['description']);
939 $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['api.event.get']['values'][0]['title']);
940 $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['api.event.get']['values'][0]['description']);
942 // Verify that "setvalue" handles encoding for updates
943 $setValueTitleResult = civicrm_api('Event', 'setvalue', array(
947 'value' => 'setValueTitle: CiviCRM <> TheRest',
949 $this->assertAPISuccess($setValueTitleResult);
950 $this->assertEquals('setValueTitle: CiviCRM <> TheRest', $setValueTitleResult['values']['title']);
951 $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array(
954 'field' => 'description',
955 'value' => 'setValueDescription: TheRest <> CiviCRM',
957 $this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue
958 //$this->assertAPISuccess($setValueDescriptionResult);
959 //$this->assertEquals('setValueDescription: TheRest <> CiviCRM', $setValueDescriptionResult['values']['description']);
963 * Verify that write operations (create/update) use partial HTML-encoding
965 * In this example, the event 'title' is subject to encoding, but the
966 * event 'description' is not.
968 public function testEncodeWrite() {
970 $createResult = civicrm_api('Event', 'Create', array(
972 'title' => 'createNew: CiviCRM <> TheRest',
973 'description' => 'createNew: TheRest <> CiviCRM',
974 'event_type_id' => 1,
976 'start_date' => 20081021,
978 $this->assertAPISuccess($createResult);
979 $eventId = $createResult['id'];
980 $this->assertDBQuery('createNew: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
981 1 => array($eventId, 'Integer')
983 $this->assertDBQuery('createNew: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
984 1 => array($eventId, 'Integer')
987 // Verify that "create" handles encoding for updates
988 $createWithIdResult = civicrm_api('Event', 'Create', array(
991 'title' => 'createWithId: CiviCRM <> TheRest',
992 'description' => 'createWithId: TheRest <> CiviCRM',
994 $this->assertAPISuccess($createWithIdResult);
995 $this->assertDBQuery('createWithId: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
996 1 => array($eventId, 'Integer')
998 $this->assertDBQuery('createWithId: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
999 1 => array($eventId, 'Integer')
1002 // Verify that "setvalue" handles encoding for updates
1003 $setValueTitleResult = civicrm_api('Event', 'setvalue', array(
1007 'value' => 'setValueTitle: CiviCRM <> TheRest',
1009 $this->assertAPISuccess($setValueTitleResult);
1010 $this->assertDBQuery('setValueTitle: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
1011 1 => array($eventId, 'Integer')
1013 $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array(
1016 'field' => 'description',
1017 'value' => 'setValueDescription: TheRest <> CiviCRM',
1019 $this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue
1020 //$this->assertAPISuccess($setValueDescriptionResult);
1021 //$this->assertDBQuery('setValueDescription: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
1022 // 1 => array($eventId, 'Integer')