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 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); }
/** * Create() method with custom data. */ public function testCreateWithCustomData() { $contactId = Contact::createIndividual(); $ids = array('contribution' => NULL); //create custom data $customGroup = Custom::createGroup(array(), 'Contribution'); $fields = array('label' => 'testFld', 'data_type' => 'String', 'html_type' => 'Text', 'is_active' => 1, 'custom_group_id' => $customGroup->id); $customField = CRM_Core_BAO_CustomField::create($fields); $params = array('contact_id' => $contactId, 'currency' => 'USD', 'financial_type_id' => 1, 'contribution_status_id' => 1, 'payment_instrument_id' => 1, 'source' => 'STUDENT', 'receive_date' => '20080522000000', 'receipt_date' => '20080522000000', 'id' => NULL, 'non_deductible_amount' => 0.0, 'total_amount' => 200.0, 'fee_amount' => 5, 'net_amount' => 195, 'trxn_id' => '22ereerwww322323', 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70', 'thankyou_date' => '20080522'); $params['custom'] = array($customField->id => array(-1 => array('value' => 'Test custom value', 'type' => 'String', 'custom_field_id' => $customField->id, 'custom_group_id' => $customGroup->id, 'table_name' => $customGroup->table_name, 'column_name' => $customField->column_name, 'file_id' => NULL))); $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); // Check that the custom field value is saved $customValueParams = array('entityID' => $contribution->id, 'custom_' . $customField->id => 1); $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams); $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value'); $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.'); $this->contributionDelete($contribution->id); Custom::deleteField($customField); Custom::deleteGroup($customGroup); Contact::delete($contactId); }
public function testFixFieldValueOfTypeMemo() { $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(); $params = array('email' => '*****@*****.**', $custom => 'note'); CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($params); $this->assertEquals($params[$custom], '%note%', 'Checking the returned value of type Memo.'); Custom::deleteField($customField); Custom::deleteGroup($customGroup); }
function testCreate() { $params = array('title' => 'Test_Group_1', 'name' => 'test_group_1', 'extends' => array('Individual'), 'weight' => 4, 'collapse_display' => 1, 'style' => 'Inline', 'help_pre' => 'This is Pre Help For Test Group 1', 'help_post' => 'This is Post Help For Test Group 1', 'is_active' => 1); require_once 'CRM/Core/BAO/CustomGroup.php'; $customGroup = CRM_Core_BAO_CustomGroup::create($params); $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id', 'Database check for custom group record.'); $this->assertEquals($params['title'], $dbCustomGroupTitle); 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); }
/** * 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); }
/** * Ensure that civicrm_contact.modified_date is updated correctly. * * Looking at it when contact-related custom data is updated. */ public function testTimestampsCustom() { $customGroup = Custom::createGroup(array(), 'Individual'); $this->assertNotNull($customGroup); $fields = array('custom_group_id' => $customGroup->id, 'data_type' => 'String', 'html_type' => 'Text'); $customField = $this->customFieldCreate($fields); $customField = $customField['values'][$customField['id']]; $test = $this; $this->_testTimestamps(array('INSERT' => function ($contactId) use($test, $customGroup, $customField) { $result = civicrm_api3('contact', 'create', array('contact_id' => $contactId, 'custom_' . $customField['id'] => 'test-1')); }, 'UPDATE' => function ($contactId) use($test, $customGroup, $customField) { CRM_Core_DAO::executeQuery("UPDATE {$customGroup->table_name} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1", array(1 => array($contactId, 'Integer'))); }, 'DELETE' => function ($contactId) use($test, $customGroup, $customField) { CRM_Core_DAO::executeQuery("DELETE FROM {$customGroup->table_name} WHERE entity_id = %1", array(1 => array($contactId, 'Integer'))); })); Custom::deleteGroup($customGroup); }
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); }