Well - it would have been one line - except then I wanted to keep testing what happens when the custom field has a default value (the breakage) & without (per before) so then I wound up having to fix up the function signature too
return civicrm_api3_create_success($values, $params);
}
else {
- return civicrm_api3_create_error($result['error_message']);
+ throw new API_Exception($result['error_message']);
}
}
else {
),
'3' => array(
'entity_id' => '1',
- 'latest' => '',
+ 'latest' => 'defaultValue',
'id' => '3',
'1' => 'warm beer',
- '2' => '',
+ '2' => 'defaultValue',
),
'4' => array(
'entity_id' => '1',
- 'latest' => '',
+ 'latest' => 'defaultValue',
'id' => '4',
- '1' => '',
- '2' => '',
+ '1' => 'defaultValue',
+ '2' => 'defaultValue',
),
'5' => array(
'entity_id' => '1',
- 'latest' => '',
+ 'latest' => 'defaultValue',
'id' => '5',
- '1' => '',
+ '1' => 'defaultValue',
),
'6' => array(
'entity_id' => '1',
),
'7' => array(
'entity_id' => '1',
- 'latest' => '',
+ 'latest' => 'defaultValue',
'id' => '7',
- '1' => '',
+ '1' => 'defaultValue',
),
),
),
'pseudoconstant' => array(
'optionGroupName' => 'individual_prefix',
),
+ 'api.aliases' => array(
+ '0' => 'individual_prefix',
+ '1' => 'individual_prefix_id',
+ ),
),
'suffix_id' => array(
'name' => 'suffix_id',
'pseudoconstant' => array(
'optionGroupName' => 'individual_suffix',
),
+ 'api.aliases' => array(
+ '0' => 'individual_suffix',
+ '1' => 'individual_suffix_id',
+ ),
),
'email_greeting_id' => array(
'name' => 'email_greeting_id',
'groupTitle' => 'select_test_g',
'data_type' => 'String',
'html_type' => 'Select',
+ 'default_value' => '',
'text_length' => '',
'options_per_line' => '',
'custom_group_id' => '1',
'1' => array(
'id' => '1',
'custom_group_id' => '1',
- 'name' => 'Name1',
+ 'name' => 'test_textfield2',
'label' => 'Name1',
'data_type' => 'String',
'html_type' => 'Text',
$expectedResult = array(
'is_error' => 0,
'version' => 3,
- 'count' => 4,
+ 'count' => 7,
'values' => array(
'mySingleField' => array(
'entity_id' => '2',
'latest' => 'value 1',
'id' => 'mySingleField',
),
- 'Cust_Field' => array(
+ 'field_12' => array(
+ 'entity_id' => '2',
+ 'latest' => 'value 3',
+ 'id' => 'field_12',
+ '1' => 'value 2',
+ '2' => 'value 3',
+ ),
+ 'field_22' => array(
+ 'entity_id' => '2',
+ 'latest' => '',
+ 'id' => 'field_22',
+ '1' => 'warm beer',
+ '2' => '',
+ ),
+ 'field_32' => array(
+ 'entity_id' => '2',
+ 'latest' => '',
+ 'id' => 'field_32',
+ '1' => 'fl* w*',
+ '2' => '',
+ ),
+ 'field_13' => array(
'entity_id' => '2',
'latest' => 'coffee',
- 'id' => 'Cust_Field',
- '1' => '',
+ 'id' => 'field_13',
+ '1' => 'defaultValue',
'2' => 'coffee',
),
- 'field_2' => array(
+ 'field_23' => array(
'entity_id' => '2',
'latest' => 'value 4',
- 'id' => 'field_2',
+ 'id' => 'field_23',
'1' => '',
'2' => 'value 4',
),
- 'field_3' => array(
+ 'field_33' => array(
'entity_id' => '2',
'latest' => '',
- 'id' => 'field_3',
+ 'id' => 'field_33',
'1' => 'vegemite',
'2' => '',
),
'entity_id' => '2',
'latest' => 'coffee',
'id' => '5',
- '1' => '',
+ '1' => 'defaultValue',
'2' => 'coffee',
),
'6' => array(
$customGroup = $this->CustomGroupMultipleCreateByParams($params);
$ids['custom_group_id'] = $customGroup['id'];
- $customField = $this->customFieldCreate($ids['custom_group_id']);
+ $customField = $this->customFieldCreate(array('custom_group_id' => $ids['custom_group_id'], 'label' => 'field_1' . $ids['custom_group_id']));
$ids['custom_field_id'][] = $customField['id'];
- $customField = $this->customFieldCreate($ids['custom_group_id'], 'field_2');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $ids['custom_group_id'], 'default_value' => '', 'label' => 'field_2' . $ids['custom_group_id']));
$ids['custom_field_id'][] = $customField['id'];
- $customField = $this->customFieldCreate($ids['custom_group_id'], 'field_3');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $ids['custom_group_id'], 'default_value' => '', 'label' => 'field_3' . $ids['custom_group_id']));
$ids['custom_field_id'][] = $customField['id'];
return $ids;
$entity = substr(basename($filename), 0, strlen(basename($filename)) - 8);
$params['extends'] = $entity ? $entity : 'Contact';
$customGroup = $this->CustomGroupCreate($params);
- $customField = $this->customFieldCreate($customGroup['id'], $function);
+ $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'], 'label' => $function));
CRM_Core_PseudoConstant::flush();
return array('custom_group_id' => $customGroup['id'], 'custom_field_id' => $customField['id']);
* @param int $customGroupID
*/
function customGroupDelete($customGroupID) {
-
$params['id'] = $customGroupID;
return $this->callAPISuccess('custom_group', 'delete', $params);
}
/**
* Function to create custom field
*
- * @param int $customGroupID
+ * @param array $params (custom_group_id) is required
* @param string $name name of custom field
* @param int $apiversion API version to use
*/
- function customFieldCreate($customGroupID, $name = "Cust Field") {
-
- $params = array(
- 'label' => $name,
- 'name' => $name,
- 'custom_group_id' => $customGroupID,
+ function customFieldCreate($params) {
+ $params = array_merge(array(
+ 'label' => 'Custom Field',
'data_type' => 'String',
'html_type' => 'Text',
'is_searchable' => 1,
'is_active' => 1,
'default_value' => 'defaultValue',
- );
+ ), $params);
$result = $this->callAPISuccess('custom_field', 'create', $params);
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
$params['custom_' . $ids['custom_field_id']] = "custom string";
$moreids = $this->CustomGroupMultipleCreateWithFields();
- $andmoreids = $this->CustomGroupMultipleCreateWithFields(array('title' => "another group"));
+ $andmoreids = $this->CustomGroupMultipleCreateWithFields(array('title' => "another group", 'name' => 'another name'));
$description = "/*this demonstrates the usage of chained api functions. A variety of techniques are used";
$subfile = "APIChainedArrayMultipleCustom";
$params = array(
*/
function testCustomFieldGetReturnOptions(){
$customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
- $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
$result = $this->callAPISuccess('custom_field', 'getsingle', array(
'id' => $customField['id'],
*/
function testCustomFieldGetReturnArray(){
$customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
- $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
$result = $this->callAPISuccess('custom_field', 'getsingle', array(
'id' => $customField['id'],
*/
function testCustomFieldGetReturnTwoOptions(){
$customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group'));
- $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
$result = $this->callAPISuccess('custom_field', 'getsingle', array(
'id' => $customField['id'],
*/
function testCustomFieldDelete() {
$customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
- $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
+ $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
$this->assertNotNull($customField['id'], 'in line ' . __LINE__);
$params = array(
$this->CustomGroupIndividual = $this->customGroupCreate($groupIndividual);
- $this->IndividualField = $this->customFieldCreate($this->CustomGroupIndividual['id'], "Custom Field" . substr(sha1(rand()), 0, 7));
+ $this->IndividualField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual['id']));
// Create Group For Individual-Student Contact Sub Type
$groupIndiStudent = array(
$this->CustomGroupIndiStudent = $this->customGroupCreate($groupIndiStudent);
- $this->IndiStudentField = $this->customFieldCreate($this->CustomGroupIndiStudent['id'], "Custom Field" . substr(sha1(rand()), 0, 7));
+ $this->IndiStudentField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent['id']));
$params = array(
'first_name' => 'Mathev',
$result = $this->callAPISuccess('Contact', 'create', $params);
$contact_id = $result['id'];
- $result = $this->callAPISuccess('Contact', 'create',
- array(
- 'contact_type' => 'Individual',
- 'id' => $contact_id,
- 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 3",
- 'custom_' . $this->ids['multi2']['custom_field_id'][0] => "coffee",
- 'custom_' . $this->ids['multi2']['custom_field_id'][1] => "value 4",
- )
+ $firstCustomField = $this->ids['multi']['custom_field_id'][0];
+ $secondCustomField = $this->ids['multi2']['custom_field_id'][0];
+ $thirdCustomField = $this->ids['multi2']['custom_field_id'][1];
+ $createParams = array(
+ 'contact_type' => 'Individual',
+ 'id' => $contact_id,
+ 'custom_' . $firstCustomField => "value 3",
+ 'custom_' . $secondCustomField => "coffee",
+ 'custom_' . $thirdCustomField => "value 4",
);
+ $result = $this->callAPISuccess('Contact', 'create', $createParams);
$params = array(
'id' => $result['id'],
$resultformatted = $this->callAPIAndDocument('CustomValue', 'Get', $params, __FUNCTION__, __FILE__, "utilises field names", 'formatFieldName');
// delete the contact
$this->callAPISuccess('contact', 'delete', array('id' => $contact_id));
-
- $this->assertEquals('coffee', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['2'], "In line " . __LINE__);
- $this->assertEquals('coffee', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['latest'], "In line " . __LINE__);
- $this->assertEquals($this->ids['multi2']['custom_field_id'][0], $result['values'][$this->ids['multi2']['custom_field_id'][0]]['id'], "In line " . __LINE__);
- $this->assertEquals('', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['1'], "In line " . __LINE__);
- $this->assertEquals($contact_id, $result['values'][$this->ids['multi2']['custom_field_id'][0]]['entity_id'], "In line " . __LINE__);
- $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['0'], "In line " . __LINE__);
- $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['latest'], "In line " . __LINE__);
- $this->assertEquals('value 1', $resultformatted['values']['mySingleField']['latest'], "In line " . __LINE__);
+ $this->assertEquals('coffee', $result['values'][$secondCustomField]['2']);
+ $this->assertEquals('coffee', $result['values'][$secondCustomField]['latest']);
+ $this->assertEquals($secondCustomField, $result['values'][$secondCustomField]['id']);
+ $this->assertEquals('defaultValue', $result['values'][$secondCustomField]['1']);
+ $this->assertEquals($contact_id, $result['values'][$secondCustomField]['entity_id']);
+ $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['0']);
+ $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['latest']);
+ $this->assertEquals('value 1', $resultformatted['values']['mySingleField']['latest']);
+ $this->assertEquals('', $result['values'][$thirdCustomField]['1']);
+ $this->assertEquals('value 4', $result['values'][$thirdCustomField]['2']);
}
}