3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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_SyntaxConformanceAllEntitiesTest
extends CiviUnitTestCase
{
39 protected $_apiversion;
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;
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)
58 $this->toBeImplemented
['get'] = array('Profile', 'CustomValue', 'Constant', 'CustomSearch', 'Extension', 'ReportTemplate', 'System', 'Setting');
59 $this->toBeImplemented
['create'] = array('SurveyRespondant', 'OptionGroup', 'MailingRecipients', 'UFMatch', 'LocationType', 'CustomSearch', 'Extension', 'ReportTemplate', 'System');
60 $this->toBeImplemented
['delete'] = array('MembershipPayment', 'OptionGroup', 'SurveyRespondant', 'UFJoin', 'UFMatch', 'Extension', 'LocationType', 'System');
61 $this->onlyIDNonZeroCount
['get'] = array('ActivityType', 'Entity', 'Domain','Setting');
62 $this->deprecatedAPI
= array('Location', 'ActivityType', 'SurveyRespondant');
63 $this->deletableTestObjects
= array();
67 foreach ($this->deletableTestObjects
as $entityName => $entities) {
68 foreach ($entities as $entityID) {
69 CRM_Core_DAO
::deleteTestObjects($entityName, array('id' => $entityID));
75 public static function entities($skip = NULL) {
76 // uncomment to make a quicker run when adding a test
77 //return array(array ('Tag'), array ('Activity') );
78 $tmp = civicrm_api('Entity', 'Get', array('version' => 3));
79 if (!is_array($skip)) {
82 $tmp = array_diff($tmp['values'], $skip);
84 foreach ($tmp as $e) {
85 $entities[] = array($e);
90 public static function entities_get() {
91 // all the entities, beside the ones flagged
92 return api_v3_SyntaxConformanceAllEntitiesTest
::entities(api_v3_SyntaxConformanceAllEntitiesTest
::toBeSkipped_get(TRUE));
95 public static function entities_create() {
96 return api_v3_SyntaxConformanceAllEntitiesTest
::entities(api_v3_SyntaxConformanceAllEntitiesTest
::toBeSkipped_create(TRUE));
99 public static function entities_updatesingle() {
100 return api_v3_SyntaxConformanceAllEntitiesTest
::entities(api_v3_SyntaxConformanceAllEntitiesTest
::toBeSkipped_updatesingle(TRUE));
103 public static function entities_delete() {
104 return api_v3_SyntaxConformanceAllEntitiesTest
::entities(api_v3_SyntaxConformanceAllEntitiesTest
::toBeSkipped_delete(TRUE));
107 public static function toBeSkipped_get($sequential = FALSE) {
108 $entitiesWithoutGet = array('MailingEventSubscribe', 'MailingEventConfirm', 'MailingEventResubscribe', 'MailingEventUnsubscribe', 'MailingGroup', 'Location');
109 if ($sequential === TRUE) {
110 return $entitiesWithoutGet;
113 foreach ($entitiesWithoutGet as $e) {
114 $entities[] = array($e);
119 * Mailing Contact Just doesn't support id. We have always insisted on finding a way to
120 * support id in API but in this case the underlying tables are crying out for a restructue
121 * & it just doesn't make sense
122 * @param unknown_type $sequential
123 * @return multitype:string |multitype:multitype:string
125 public static function toBeSkipped_getByID($sequential = FALSE) {
126 return array('MailingContact');
129 public static function toBeSkipped_create($sequential = FALSE) {
130 $entitiesWithoutCreate = array('MailingGroup', 'Constant', 'Entity', 'Location', 'Profile', 'MailingRecipients');
131 if ($sequential === TRUE) {
132 return $entitiesWithoutCreate;
135 foreach ($entitiesWithoutCreate as $e) {
136 $entities[] = array($e);
141 public static function toBeSkipped_delete($sequential = FALSE) {
142 $entitiesWithout = array('Mailing', 'MailingGroup', 'Constant', 'Entity', 'Location', 'Domain', 'Profile', 'CustomValue');
143 if ($sequential === TRUE) {
144 return $entitiesWithout;
147 foreach ($entitiesWithout as $e) {
148 $entities[] = array($e);
153 * Generate list of entities to test for get by id functions
154 * @param boolean $sequential
155 * @return multitype:string |multitype:multitype:string
157 public static function toBeSkipped_automock($sequential = FALSE) {
158 $entitiesWithoutGet = array('MailingContact', 'EntityTag', 'Participant', 'ParticipantPayment', 'Setting', 'SurveyRespondant', 'MailingRecipients', 'CustomSearch', 'Extension', 'ReportTemplate', 'System');
159 if ($sequential === TRUE) {
160 return $entitiesWithoutGet;
163 foreach ($entitiesWithoutGet as $e) {
164 $entities[] = array($e);
171 * At this stage exclude the ones that don't pass & add them as we can troubleshoot them
174 public static function toBeSkipped_updatesingle($sequential = FALSE) {
175 $entitiesWithout = array(
180 'MailingEventUnsubscribe',
181 'MailingEventSubscribe',
196 'ParticipantStatusType',
213 'MailingEventConfirm',
217 'MailingEventResubscribe',
225 'ParticipantPayment',
238 if ($sequential === TRUE) {
239 return $entitiesWithout;
242 foreach ($entitiesWithout as $e) {
247 return array('pledge');
251 public function getKnownUnworkablesUpdateSingle($entity, $key){
252 // can't update values are values for which updates don't result in the value being changed
253 $knownFailures = array(
255 'cant_update' => array(
256 'state_province_id', //issues with country id - need to ensure same country
257 'master_id',//creates relationship
259 'cant_return' => array(
263 'cant_update' => array(
264 'pledge_original_installment_amount',
266 'original_installment_amount',
268 'amount' // can't be changed through API
270 'break_return' => array(// if these are passed in they are retrieved from the wrong table
273 'contribution_page_id',
274 'financial_account_id',
278 'cant_return' => array(// can't be retrieved from api
279 'honor_type_id', //due to uniquename missing
287 'initial_reminder_day',
288 'additional_reminder_day',
290 'pledge_contribution_page_id',
292 'pledge_campaign_id',
295 'PaymentProcessorType' => array(
296 'cant_update' => array(
299 'break_return' => array(
301 'cant_return' => array(
305 if(empty($knownFailures[$entity]) ||
empty($knownFailures[$entity][$key])){
308 return $knownFailures[$entity][$key];
311 /** testing the _get **/
314 * @dataProvider toBeSkipped_get
315 entities that don't need a get action
317 public function testNotImplemented_get($Entity) {
318 $result = civicrm_api($Entity, 'Get', array('version' => 3));
319 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
320 $this->assertContains("API ($Entity,Get) does not exist", $result['error_message']);
324 * @dataProvider entities
325 * @expectedException PHPUnit_Framework_Error
327 public function testWithoutParam_get($Entity) {
328 // should get php complaining that a param is missing
329 $result = civicrm_api($Entity, 'Get');
333 * @dataProvider entities
335 public function testGetFields($Entity) {
336 if (in_array($Entity, $this->deprecatedAPI
) ||
$Entity == 'Entity' ||
$Entity == 'CustomValue' ||
$Entity == 'MailingGroup') {
340 $result = civicrm_api($Entity, 'getfields', array('version' => 3));
341 $this->assertTrue(is_array($result['values']), "$Entity ::get fields doesn't return values array in line " . __LINE__
);
342 foreach ($result['values'] as $key => $value) {
343 $this->assertTrue(is_array($value), $Entity . "::" . $key . " is not an array in line " . __LINE__
);
348 * @dataProvider entities_get
350 public function testEmptyParam_get($Entity) {
352 if (in_array($Entity, $this->toBeImplemented
['get'])) {
353 // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented");
356 $result = civicrm_api($Entity, 'Get', array());
357 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
358 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
361 * @dataProvider entities_get
363 public function testEmptyParam_getString($Entity) {
365 if (in_array($Entity, $this->toBeImplemented
['get'])) {
366 // $this->markTestIncomplete("civicrm_api3_{$Entity}_get to be implemented");
369 $result = $this->callAPIFailure($Entity, 'Get', 'string');
370 $this->assertEquals(2000, $result['error_code']);
371 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
374 * @dataProvider entities_get
375 * @Xdepends testEmptyParam_get // no need to test the simple if the empty doesn't work/is skipped. doesn't seem to work
377 public function testSimple_get($Entity) {
378 // $this->markTestSkipped("test gives core error on test server (but not on our locals). Skip until we can get server to pass");
379 if (in_array($Entity, $this->toBeImplemented
['get'])) {
382 $result = civicrm_api($Entity, 'Get', array('version' => 3));
383 // @TODO: list the get that have mandatory params
384 if ($result['is_error']) {
385 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
386 // either id or contact_id or entity_id is one of the field missing
387 $this->assertContains("id", $result['error_message']);
390 $this->assertEquals(3, $result['version']);
391 $this->assertArrayHasKey('count', $result);
392 $this->assertArrayHasKey('values', $result);
397 * @dataProvider entities_get
399 public function testAcceptsOnlyID_get($Entity) {
400 // big random number. fun fact: if you multiply it by pi^e, the result is another random number, but bigger ;)
401 $nonExistantID = 30867307034;
402 if (in_array($Entity, $this->toBeImplemented
['get'])
403 ||
in_array($Entity, $this->toBeSkipped_getByID())
409 // the below function returns different values and hence an early return
410 // we'll fix this once beta1 is released
413 $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID));
415 if ($result['is_error']) {
416 // just to get a clearer message in the log
417 $this->assertEquals("only id should be enough", $result['error_message']);
419 if (!in_array($Entity, $this->onlyIDNonZeroCount
['get'])) {
420 $this->assertEquals(0, $result['count']);
425 * Create two entities and make sure we can fetch them individually by ID
427 * @dataProvider entities_get
429 * limitations include the problem with avoiding loops when creating test objects -
430 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
431 * Currency - only seems to support US
433 public function testByID_get($entityName) {
434 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
435 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
439 $baoString = _civicrm_api3_get_DAO($entityName);
440 if (empty($baoString)) {
441 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
446 $baoObj1 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
447 $this->assertTrue(is_integer($baoObj1->id
), 'check first id');
448 $this->deletableTestObjects
[$baoString][] = $baoObj1->id
;
449 $baoObj2 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
450 $this->assertTrue(is_integer($baoObj2->id
), 'check second id');
451 $this->deletableTestObjects
[$baoString][] = $baoObj2->id
;
454 $result = civicrm_api($entityName, 'get', array(
456 'id' => $baoObj1->id
,
458 $this->assertAPISuccess($result);
459 $this->assertTrue(!empty($result['values'][$baoObj1->id
]), 'Should find first object by id');
460 $this->assertEquals($baoObj1->id
, $result['values'][$baoObj1->id
]['id'], 'Should find id on first object');
461 $this->assertEquals(1, count($result['values']));
463 // fetch second by ID
464 $result = civicrm_api($entityName, 'get', array(
466 'id' => $baoObj2->id
,
468 $this->assertAPISuccess($result);
469 $this->assertTrue(!empty($result['values'][$baoObj2->id
]), 'Should find second object by id');
470 $this->assertEquals($baoObj2->id
, $result['values'][$baoObj2->id
]['id'], 'Should find id on second object');
471 $this->assertEquals(1, count($result['values']));
475 * Create two entities and make sure we can fetch them individually by ID (e.g. using "contact_id=>2"
478 * @dataProvider entities_get
480 * limitations include the problem with avoiding loops when creating test objects -
481 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
482 * Currency - only seems to support US
484 public function testByIDAlias_get($entityName) {
485 if (in_array($entityName, self
::toBeSkipped_automock(TRUE))) {
486 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
490 $baoString = _civicrm_api3_get_DAO($entityName);
491 if (empty($baoString)) {
492 $this->markTestIncomplete("Entity [$entityName] cannot be mocked - no known DAO");
496 $idFieldName = _civicrm_api_get_entity_name_from_camel($entityName) . '_id';
499 $baoObj1 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
500 $this->assertTrue(is_integer($baoObj1->id
), 'check first id');
501 $this->deletableTestObjects
[$baoString][] = $baoObj1->id
;
502 $baoObj2 = CRM_Core_DAO
::createTestObject($baoString, array('currency' => 'USD'));
503 $this->assertTrue(is_integer($baoObj2->id
), 'check second id');
504 $this->deletableTestObjects
[$baoString][] = $baoObj2->id
;
507 $result = civicrm_api($entityName, 'get', array(
509 $idFieldName => $baoObj1->id
,
511 $this->assertAPISuccess($result);
512 $this->assertTrue(!empty($result['values'][$baoObj1->id
]), 'Should find first object by id');
513 $this->assertEquals($baoObj1->id
, $result['values'][$baoObj1->id
]['id'], 'Should find id on first object');
514 $this->assertEquals(1, count($result['values']));
516 // fetch second by ID
517 $result = civicrm_api($entityName, 'get', array(
519 $idFieldName => $baoObj2->id
,
521 $this->assertAPISuccess($result);
522 $this->assertTrue(!empty($result['values'][$baoObj2->id
]), 'Should find second object by id');
523 $this->assertEquals($baoObj2->id
, $result['values'][$baoObj2->id
]['id'], 'Should find id on second object');
524 $this->assertEquals(1, count($result['values']));
528 * @dataProvider entities_get
530 public function testNonExistantID_get($Entity) {
531 // cf testAcceptsOnlyID_get
532 $nonExistantID = 30867307034;
533 if (in_array($Entity, $this->toBeImplemented
['get'])) {
537 $result = civicrm_api($Entity, 'Get', array('version' => 3, 'id' => $nonExistantID));
539 // redundant with testAcceptsOnlyID_get
540 if ($result['is_error']) {
545 $this->assertArrayHasKey('version', $result);
546 $this->assertEquals(3, $result['version']);
547 if (!in_array($Entity, $this->onlyIDNonZeroCount
['get'])) {
548 $this->assertEquals(0, $result['count']);
552 /** testing the _create **/
555 * @dataProvider toBeSkipped_create
556 entities that don't need a create action
558 public function testNotImplemented_create($Entity) {
559 $result = civicrm_api($Entity, 'Create', array('version' => 3));
560 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
561 $this->assertContains("API ($Entity,Create) does not exist", $result['error_message']);
565 * @dataProvider entities
566 * @expectedException PHPUnit_Framework_Error
568 public function testWithoutParam_create($Entity) {
569 // should create php complaining that a param is missing
570 $result = civicrm_api($Entity, 'Create');
574 * @dataProvider entities_create
576 public function testEmptyParam_create($Entity) {
577 if (in_array($Entity, $this->toBeImplemented
['create'])) {
578 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
581 $result = civicrm_api($Entity, 'Create', array());
582 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
583 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
587 * @dataProvider entities
589 public function testCreateWrongTypeParamTag_create() {
590 $result = civicrm_api("Tag", 'Create', 'this is not a string');
591 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
592 $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
596 * @dataProvider entities_updatesingle
598 * limitations include the problem with avoiding loops when creating test objects -
599 * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through
600 * Currency - only seems to support US
602 public function testCreateSingleValueAlter($entityName) {
603 if (in_array($entityName, $this->toBeImplemented
['create'])) {
604 // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented");
608 $baoString = _civicrm_api3_get_DAO($entityName);
609 $this->assertNotEmpty($baoString, $entityName);
610 $this->assertNotEmpty($entityName, $entityName);
611 $fieldsget = $fields = civicrm_api($entityName, 'getfields', array(
612 'version' => 3, 'action' => 'get'
615 if($entityName != 'Pledge'){
616 $fields = civicrm_api($entityName, 'getfields', array(
617 'version' => 3, 'action' => 'create'
621 $fields = $fields['values'];
622 $return = array_keys($fieldsget['values']);
623 $valuesNotToReturn = $this->getKnownUnworkablesUpdateSingle($entityName, 'break_return');
624 // these can't be requested as return values
625 $entityValuesThatDontWork = array_merge(
626 $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_update'),
627 $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_return'),
631 $return = array_diff($return,$valuesNotToReturn);
632 $baoObj = new CRM_Core_DAO();
633 $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0);
634 $getentities = civicrm_api($entityName, 'get', array(
643 // lets use first rather than assume only one exists
644 $entity = $getentities['values'][0];
645 $entity2 = $getentities['values'][1];
646 foreach ($fields as $field => $specs) {
648 if (!empty($specs['uniquename'])) {
649 $fieldName = $specs['uniquename'];
651 if ($field == 'currency' ||
$field == 'id' ||
$field == strtolower($entityName) . '_id'
652 ||
in_array($field,$entityValuesThatDontWork)) {
653 //@todo id & entity_id are correct but we should fix currency & frequency_day
656 switch ($specs['type']) {
657 case CRM_Utils_Type
::T_DATE
:
658 case CRM_Utils_Type
::T_TIMESTAMP
:
659 $entity[$fieldName] = '2012-05-20';
661 //case CRM_Utils_Type::T_DATETIME:
664 $entity[$fieldName] = '2012-05-20 03:05:20';
667 case CRM_Utils_Type
::T_STRING
:
668 case CRM_Utils_Type
::T_BLOB
:
669 case CRM_Utils_Type
::T_MEDIUMBLOB
:
670 case CRM_Utils_Type
::T_TEXT
:
671 case CRM_Utils_Type
::T_LONGTEXT
:
672 case CRM_Utils_Type
::T_EMAIL
:
673 $entity[$fieldName] = substr('New String',0, CRM_Utils_Array
::Value('maxlength',$specs,100));
676 case CRM_Utils_Type
::T_INT
:
677 // probably created with a 1
678 $entity[$fieldName] = '6';
679 if (CRM_Utils_Array
::value('FKClassName', $specs)) {
680 if($specs['FKClassName'] == $baoString){
681 $entity[$fieldName] = (string) $entity2['id'];
684 $entity[$fieldName] = (string) empty($entity2[$field]) ? CRM_Utils_Array
::value($specs['uniqueName'], $entity2) : $entity2[$field];
685 //todo - there isn't always something set here - & our checking on unset values is limited
686 if (empty($entity[$field])) {
687 unset($entity[$field]);
693 case CRM_Utils_Type
::T_BOOL
:
694 case CRM_Utils_Type
::T_BOOLEAN
:
695 // probably created with a 1
696 $entity[$fieldName] = '0';
699 case CRM_Utils_Type
::T_FLOAT
:
700 case CRM_Utils_Type
::T_MONEY
:
701 $entity[$field] = '222';
704 case CRM_Utils_Type
::T_URL
:
705 $entity[$field] = 'warm.beer.com';
707 $constant = CRM_Utils_Array
::value('pseudoconstant', $specs);
708 if (!empty($constant)) {
709 $constantOptions = array_reverse(array_keys(CRM_Utils_PseudoConstant
::getConstant($constant['name'])));
710 $entity[$field] = (string) $constantOptions[0];
712 $enum = CRM_Utils_Array
::value('enumValues', $specs);
714 // reverse so we 'change' value
715 $options = array_reverse(explode(',', $enum));
716 $entity[$fieldName] = $options[0];
718 $updateParams = array(
720 'id' => $entity['id'],
721 $field => $entity[$field],
724 $update = civicrm_api($entityName, 'create', $updateParams);
725 if(!empty($update['is_error'])){
728 $this->assertAPISuccess($update, print_r($updateParams, TRUE) . 'in line ' . __LINE__
);
729 $checkParams = array(
730 'id' => $entity['id'],
740 $checkEntity = civicrm_api($entityName, 'getsingle', $checkParams);
741 $this->assertEquals($entity, $checkEntity, "changing field $fieldName" . print_r($entity,TRUE) );//. print_r($checkEntity,true) .print_r($checkParams,true) . print_r($update,true) . print_r($updateParams, TRUE));
743 $baoObj->deleteTestObjects($baoString);
747 /** testing the _getFields **/
749 /** testing the _delete **/
752 * @dataProvider toBeSkipped_delete
753 entities that don't need a delete action
755 public function testNotImplemented_delete($Entity) {
756 $nonExistantID = 151416349;
757 $result = civicrm_api($Entity, 'Delete', array('version' => 3, 'id' => $nonExistantID));
758 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
759 $this->assertContains("API ($Entity,Delete) does not exist", $result['error_message']);
763 * @dataProvider entities
764 * @expectedException PHPUnit_Framework_Error
766 public function testWithoutParam_delete($Entity) {
767 // should delete php complaining that a param is missing
768 $result = civicrm_api($Entity, 'Delete');
772 * @dataProvider entities_delete
774 public function testEmptyParam_delete($Entity) {
775 if (in_array($Entity, $this->toBeImplemented
['delete'])) {
776 // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
779 $result = civicrm_api($Entity, 'Delete', array());
780 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
781 $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
785 * @dataProvider entities
787 public function testDeleteWrongTypeParamTag_delete() {
788 $result = civicrm_api("Tag", 'Delete', 'this is not a string');
789 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
790 $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
794 * Verify that HTML metacharacters provided as inputs appear consistently
797 * At time of writing, the encoding scheme requires (for example) that an
798 * event title be partially-HTML-escaped before writing to DB. To provide
799 * consistency, the API must perform extra encoding and decoding on some
802 * In this example, the event 'title' is subject to encoding, but the
803 * event 'description' is not.
805 public function testEncodeDecodeConsistency() {
807 $createResult = civicrm_api('Event', 'Create', array(
809 'title' => 'CiviCRM <> TheRest',
810 'description' => 'TheRest <> CiviCRM',
811 'event_type_id' => 1,
813 'start_date' => 20081021,
815 $this->assertAPISuccess($createResult);
816 $eventId = $createResult['id'];
817 $this->assertEquals('CiviCRM <> TheRest', $createResult['values'][$eventId]['title']);
818 $this->assertEquals('TheRest <> CiviCRM', $createResult['values'][$eventId]['description']);
820 // Verify "get" handles decoding in result value
821 $getByIdResult = civicrm_api('Event', 'Get', array(
825 $this->assertAPISuccess($getByIdResult);
826 $this->assertEquals('CiviCRM <> TheRest', $getByIdResult['values'][$eventId]['title']);
827 $this->assertEquals('TheRest <> CiviCRM', $getByIdResult['values'][$eventId]['description']);
829 // Verify "get" handles encoding in search value
830 $getByTitleResult = civicrm_api('Event', 'Get', array(
832 'title' => 'CiviCRM <> TheRest',
834 $this->assertAPISuccess($getByTitleResult);
835 $this->assertEquals('CiviCRM <> TheRest', $getByTitleResult['values'][$eventId]['title']);
836 $this->assertEquals('TheRest <> CiviCRM', $getByTitleResult['values'][$eventId]['description']);
838 // Verify that "getSingle" handles decoding
839 $getSingleResult = civicrm_api('Event', 'GetSingle', array(
844 $this->assertAPISuccess($getSingleResult);
845 $this->assertEquals('CiviCRM <> TheRest', $getSingleResult['title']);
846 $this->assertEquals('TheRest <> CiviCRM', $getSingleResult['description']);
848 // Verify that chaining handles decoding
849 $chainResult = civicrm_api('Event', 'Get', array(
852 'api.event.get' => array(
855 $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['title']);
856 $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['description']);
857 $this->assertEquals('CiviCRM <> TheRest', $chainResult['values'][$eventId]['api.event.get']['values'][0]['title']);
858 $this->assertEquals('TheRest <> CiviCRM', $chainResult['values'][$eventId]['api.event.get']['values'][0]['description']);
860 // Verify that "setvalue" handles encoding for updates
861 $setValueTitleResult = civicrm_api('Event', 'setvalue', array(
865 'value' => 'setValueTitle: CiviCRM <> TheRest',
867 $this->assertAPISuccess($setValueTitleResult);
868 $this->assertEquals('setValueTitle: CiviCRM <> TheRest', $setValueTitleResult['values']['title']);
869 $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array(
872 'field' => 'description',
873 'value' => 'setValueDescription: TheRest <> CiviCRM',
875 $this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue
876 //$this->assertAPISuccess($setValueDescriptionResult);
877 //$this->assertEquals('setValueDescription: TheRest <> CiviCRM', $setValueDescriptionResult['values']['description']);
881 * Verify that write operations (create/update) use partial HTML-encoding
883 * In this example, the event 'title' is subject to encoding, but the
884 * event 'description' is not.
886 public function testEncodeWrite() {
888 $createResult = civicrm_api('Event', 'Create', array(
890 'title' => 'createNew: CiviCRM <> TheRest',
891 'description' => 'createNew: TheRest <> CiviCRM',
892 'event_type_id' => 1,
894 'start_date' => 20081021,
896 $this->assertAPISuccess($createResult);
897 $eventId = $createResult['id'];
898 $this->assertDBQuery('createNew: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
899 1 => array($eventId, 'Integer')
901 $this->assertDBQuery('createNew: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
902 1 => array($eventId, 'Integer')
905 // Verify that "create" handles encoding for updates
906 $createWithIdResult = civicrm_api('Event', 'Create', array(
909 'title' => 'createWithId: CiviCRM <> TheRest',
910 'description' => 'createWithId: TheRest <> CiviCRM',
912 $this->assertAPISuccess($createWithIdResult);
913 $this->assertDBQuery('createWithId: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
914 1 => array($eventId, 'Integer')
916 $this->assertDBQuery('createWithId: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
917 1 => array($eventId, 'Integer')
920 // Verify that "setvalue" handles encoding for updates
921 $setValueTitleResult = civicrm_api('Event', 'setvalue', array(
925 'value' => 'setValueTitle: CiviCRM <> TheRest',
927 $this->assertAPISuccess($setValueTitleResult);
928 $this->assertDBQuery('setValueTitle: CiviCRM <> TheRest', 'SELECT title FROM civicrm_event WHERE id = %1', array(
929 1 => array($eventId, 'Integer')
931 $setValueDescriptionResult = civicrm_api('Event', 'setvalue', array(
934 'field' => 'description',
935 'value' => 'setValueDescription: TheRest <> CiviCRM',
937 $this->assertTrue((bool)$setValueDescriptionResult['is_error']); // not supported by setValue
938 //$this->assertAPISuccess($setValueDescriptionResult);
939 //$this->assertDBQuery('setValueDescription: TheRest <> CiviCRM', 'SELECT description FROM civicrm_event WHERE id = %1', array(
940 // 1 => array($eventId, 'Integer')