function testDeleteCustomfield() { $customGroup = Custom::createGroup(array(), 'Individual'); $fields = array('groupId' => $customGroup->id, 'dataType' => 'Memo', 'htmlType' => 'TextArea'); $customField = Custom::createField(array(), $fields); CRM_Core_BAO_CustomField::deleteField($customField); $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', 'Database check for deleted Custom Field.'); Custom::deleteGroup($customGroup); }
public function fixCustomFieldValue() { $customGroup = Custom::createGroup(array(), 'Individual'); $fields = array('groupId' => $customGroup->id, 'dataType' => 'Memo', 'htmlType' => 'TextArea'); $customField = Custom::createField(array(), $fields); $custom = 'custom_' . $customField->id; $params = array('email' => '*****@*****.**', $custom => 'note'); CRM_Core_BAO_CustomValue::fixCustomFieldValue($params); $this->assertEquals($params[$custom], '%note%', 'Checking the returned value of type Memo.'); Custom::deleteField($customField); Custom::deleteGroup($customGroup); }
/** * Test setValues() and getValues() methods with custom field YesNo(Boolean) Radio */ public function testSetGetValuesYesNoRadio() { $params = array(); $contactID = Contact::createIndividual(); //create Custom Group $customGroup = Custom::createGroup($params, 'Individual', TRUE); //create Custom Field of type YesNo(Boolean) Radio $fields = array('groupId' => $customGroup->id, 'dataType' => 'Boolean', 'htmlType' => 'Radio'); $customField = Custom::createField($params, $fields); // Retrieve the field ID for sample custom field 'test_Boolean' $params = array('label' => 'test_Boolean'); $field = array(); //get field Id CRM_Core_BAO_CustomField::retrieve($params, $field); $fieldID = $field['id']; // valid boolean value '1' for Boolean Radio $yesNo = '1'; $params = array('entityID' => $contactID, 'custom_' . $fieldID => $yesNo); $result = CRM_Core_BAO_CustomValueTable::setValues($params); $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).'); // Check that the YesNo radio value is stored $values = array(); $params = array('entityID' => $contactID, 'custom_' . $fieldID => 1); $values = CRM_Core_BAO_CustomValueTable::getValues($params); $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).'); $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo, 'Verify that the boolean value is stored for contact ' . $contactID); // Now set YesNo radio to an invalid boolean value and try to reset $badYesNo = '20'; $params = array('entityID' => $contactID, 'custom_' . $fieldID => $badYesNo); $errorScope = CRM_Core_TemporaryErrorScope::useException(); $message = NULL; try { $result = CRM_Core_BAO_CustomValueTable::setValues($params); } catch (Exception $e) { $message = $e->getMessage(); } $errorScope = NULL; // Check that an exception has been thrown $this->assertNotNull($message, 'Verify than an exception is thrown when bad boolean is passed'); $params = array('entityID' => $contactID, 'custom_' . $fieldID => 1); $values = CRM_Core_BAO_CustomValueTable::getValues($params); $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo, 'Verify that the date value has NOT been updated for contact ' . $contactID); // Cleanup Custom::deleteField($customField); Custom::deleteGroup($customGroup); Contact::delete($contactID); }
public function testCustomGroupMultipleOldFormate() { $params = array(); $contactID = Contact::createIndividual(); $customGroup = Custom::createGroup($params, 'Individual', TRUE); $fields = array('groupId' => $customGroup->id, 'dataType' => 'String', 'htmlType' => 'Text'); $customField = Custom::createField($params, $fields); $params = array('entityID' => $contactID, "custom_{$customField->id}" => 'First String'); $error = CRM_Core_BAO_CustomValueTable::setValues($params); $newParams = array('entityID' => $contactID, "custom_{$customField->id}" => 1); $result = CRM_Core_BAO_CustomValueTable::getValues($newParams); $this->assertEquals($params["custom_{$customField->id}"], $result["custom_{$customField->id}_1"]); $this->assertEquals($params['entityID'], $result['entityID']); Custom::deleteField($customField); Custom::deleteGroup($customGroup); Contact::delete($contactID); }
function testSetGetValuesYesNoRadio() { $params = array(); $contactID = Contact::createIndividual(); //create Custom Group $customGroup = Custom::createGroup($params, 'Individual', true); //create Custom Field of type YesNo(Boolean) Radio $fields = array('groupId' => $customGroup->id, 'dataType' => 'Boolean', 'htmlType' => 'Radio'); $customField = Custom::createField($params, $fields); // Retrieve the field ID for sample custom field 'test_Boolean' $params = array('label' => 'test_Boolean'); $field = array(); //get field Id require_once 'CRM/Core/BAO/CustomField.php'; CRM_Core_BAO_CustomField::retrieve($params, $field); $fieldID = $field['id']; // valid boolean value '1' for Boolean Radio $yesNo = '1'; $params = array('entityID' => $contactID, 'custom_' . $fieldID => $yesNo); require_once 'CRM/Core/BAO/CustomValueTable.php'; $result = CRM_Core_BAO_CustomValueTable::setValues($params); $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).'); // Check that the YesNo radio value is stored $values = array(); $params = array('entityID' => $contactID, 'custom_' . $fieldID => 1); $values = CRM_Core_BAO_CustomValueTable::getValues($params); $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).'); $this->assertEquals($values['custom_1_1'], $yesNo, 'Verify that the date value is stored for contact ' . $contactID); // Now set YesNo radio to an invalid boolean value and try to reset $badYesNo = '20'; $params = array('entityID' => $contactID, 'custom_' . $fieldID => $badYesNo); require_once 'CRM/Core/BAO/CustomValueTable.php'; $result = CRM_Core_BAO_CustomValueTable::setValues($params); // Check that the error flag is set AND that custom date value has not been modified $this->assertEquals($result['is_error'], $yesNo, 'Verify that is_error = 1 when bad boolen value is passed.'); $params = array('entityID' => $contactID, 'custom_' . $fieldID => 1); $values = CRM_Core_BAO_CustomValueTable::getValues($params); $this->assertEquals($values['custom_1_1'], $yesNo, 'Verify that the date value has NOT been updated for contact ' . $contactID); // Cleanup Custom::deleteField($customField); Custom::deleteGroup($customGroup); Contact::delete($contactID); }
/** * Function to test checkCustomField() */ function testCheckCustomField() { $customGroupTitle = 'My Custom Group'; $groupParams = array('title' => $customGroupTitle, 'name' => 'my_custom_group', 'extends' => 'Individual', 'help_pre' => 'Custom Group Help Pre', 'help_post' => 'Custom Group Help Post', 'is_active' => 1, 'collapse_display' => 1); $customGroup = Custom::createGroup($groupParams); $customGroupId = $customGroup->id; $customFieldLabel = 'Test Custom Field'; $fieldParams = array('custom_group_id' => $customGroupId, 'label' => $customFieldLabel, 'html_type' => 'Text', 'data_type' => 'String', 'is_required' => 1, 'is_searchable' => 0, 'is_active' => 1); $customField = Custom::createField($fieldParams); $customFieldId = $customField->id; //check db for custom group $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id', 'Database check for custom group record.'); $this->assertEquals($customGroupTitle, $dbCustomGroupTitle); //check db for custom field $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id', 'Database check for custom field record.'); $this->assertEquals($customFieldLabel, $dbCustomFieldLabel); //check the custom field type. $params = array('Individual'); require_once 'CRM/Core/BAO/CustomGroup.php'; $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params); $this->assertEquals(false, $usedFor); $params = array('Contribution', 'Membership', 'Participant'); $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params); $this->assertEquals(true, $usedFor); //cleanup DB by deleting customGroup Custom::deleteField($customField); Custom::deleteGroup($customGroup); }
/** * Move a custom field from $groupA to $groupB. Make sure that data records are * correctly matched and created. */ public function testMoveField() { $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE)); $this->assertTrue($countriesByName['ANDORRA'] > 0); $groups = array('A' => Custom::createGroup(array('title' => 'Test_Group A', 'name' => 'test_group_a', 'extends' => array('Individual'), 'style' => 'Inline', 'is_multiple' => 0, 'is_active' => 1, 'version' => 3)), 'B' => Custom::createGroup(array('title' => 'Test_Group B', 'name' => 'test_group_b', 'extends' => array('Individual'), 'style' => 'Inline', 'is_multiple' => 0, 'is_active' => 1, 'version' => 3))); $fields = array('countryA' => Custom::createField(array(), array('groupId' => $groups['A']->id, 'label' => 'Country A', 'dataType' => 'Country', 'htmlType' => 'Select Country')), 'countryB' => Custom::createField(array(), array('groupId' => $groups['A']->id, 'label' => 'Country B', 'dataType' => 'Country', 'htmlType' => 'Select Country')), 'countryC' => Custom::createField(array(), array('groupId' => $groups['B']->id, 'label' => 'Country C', 'dataType' => 'Country', 'htmlType' => 'Select Country'))); $contacts = array('alice' => Contact::createIndividual(array('first_name' => 'Alice', 'last_name' => 'Albertson', 'custom_' . $fields['countryA']->id => $countriesByName['ANDORRA'], 'custom_' . $fields['countryB']->id => $countriesByName['BARBADOS'])), 'bob' => Contact::createIndividual(array('first_name' => 'Bob', 'last_name' => 'Roberts', 'custom_' . $fields['countryA']->id => $countriesByName['AUSTRIA'], 'custom_' . $fields['countryB']->id => $countriesByName['BERMUDA'], 'custom_' . $fields['countryC']->id => $countriesByName['CHAD'])), 'carol' => Contact::createIndividual(array('first_name' => 'Carol', 'last_name' => 'Carolson', 'custom_' . $fields['countryC']->id => $countriesByName['CAMBODIA']))); // Move! CRM_Core_BAO_CustomField::moveField($fields['countryB']->id, $groups['B']->id); // Group[A] no longer has fields[countryB] $errorScope = CRM_Core_TemporaryErrorScope::useException(); try { $this->assertDBQuery(1, "SELECT {$fields['countryB']->column_name} FROM {$groups['A']->table_name}"); $this->fail('Expected exception when querying column on wrong table'); } catch (PEAR_Exception $e) { } $errorScope = NULL; // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before $this->assertDBQuery(1, "SELECT count(*) FROM {$groups['B']->table_name}\n WHERE entity_id = %1\n AND {$fields['countryB']->column_name} = %3\n AND {$fields['countryC']->column_name} is null", array(1 => array($contacts['alice'], 'Integer'), 3 => array($countriesByName['BARBADOS'], 'Integer'))); // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record $this->assertDBQuery(1, "SELECT count(*) FROM {$groups['B']->table_name}\n WHERE entity_id = %1\n AND {$fields['countryB']->column_name} = %3\n AND {$fields['countryC']->column_name} = %4", array(1 => array($contacts['bob'], 'Integer'), 3 => array($countriesByName['BERMUDA'], 'Integer'), 4 => array($countriesByName['CHAD'], 'Integer'))); // Carol: Group[B] still has fields[countryC] but did not get fields[countryB] $this->assertDBQuery(1, "SELECT count(*) FROM {$groups['B']->table_name}\n WHERE entity_id = %1\n AND {$fields['countryB']->column_name} is null\n AND {$fields['countryC']->column_name} = %4", array(1 => array($contacts['carol'], 'Integer'), 4 => array($countriesByName['CAMBODIA'], 'Integer'))); Custom::deleteGroup($groups['A']); Custom::deleteGroup($groups['B']); }
/** * Test getGroupTitles() */ public function testgetGroupTitles() { $customGrouptitle = 'Test Custom Group'; $groupParams = array('title' => $customGrouptitle, 'name' => 'test_custom_group', 'style' => 'Tab', 'extends' => 'Individual', 'weight' => 10, 'is_active' => 1, 'version' => 3); $customGroup = Custom::createGroup($groupParams); $customGroupId = $customGroup->id; $customFieldLabel = 'Test Custom Field'; $fieldParams = array('custom_group_id' => $customGroupId, 'label' => $customFieldLabel, 'html_type' => 'Text', 'data_type' => 'String', 'is_required' => 1, 'is_searchable' => 0, 'is_active' => 1, 'version' => 3); $customField = Custom::createField($fieldParams); $customFieldId = $customField->id; $params = array($customFieldId); $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params); $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Custom Group', 'Check Group Title.'); Custom::deleteGroup($customGroup); }