/** * 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']); }
public function upgrade_1105() { $this->ctx->log->info('Applying update 1105'); $groups = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_group_id', array('labelColumn' => 'name')); $customFieldID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'Is_Visa_Required', 'id', 'name'); $customGroupID = array_search('Extended_Demographics', $groups); if ($customFieldID && $customGroupID) { CRM_Core_BAO_CustomField::moveField($customFieldID, $customGroupID); $result = civicrm_api3('CustomField', 'get', array('sequential' => 1, 'name' => 'Is_Visa_Required')); $weight = $result['values']['weight']; //fix the weight so that the field is next to nationality $fieldValues['custom_group_id'] = $customGroupID; CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_CustomField', $weight, 2, $fieldValues); $params = array('sequential' => 1, 'id' => $result['id'], 'is_active' => 1, 'html_type' => 'Radio', 'data_type' => 'Boolean', 'weight' => 2); $result = civicrm_api3('CustomField', 'create', $params); } return TRUE; }
/** * Process the form when submitted * * @return void * @access public */ public function postProcess() { CRM_Core_BAO_CustomField::moveField($this->_srcFID, $this->_dstGID); $dstGroup = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_dstGID, 'title'); $srcUrl = CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$this->_dstGID}"); CRM_Core_Session::setStatus(ts("%1 has been moved to the custom set <a href='%3'>%2</a>.", array(1 => $this->_srcFieldLabel, 2 => $dstGroup, 3 => $srcUrl))); }
/** * 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' => $this->customGroupCreate(array('title' => 'Test_Group A', 'name' => 'test_group_a', 'extends' => array('Individual'), 'style' => 'Inline', 'is_multiple' => 0, 'is_active' => 1, 'version' => 3)), 'B' => $this->customGroupCreate(array('title' => 'Test_Group B', 'name' => 'test_group_b', 'extends' => array('Individual'), 'style' => 'Inline', 'is_multiple' => 0, 'is_active' => 1, 'version' => 3))); $groupA = $groups['A']['values'][$groups['A']['id']]; $groupB = $groups['B']['values'][$groups['B']['id']]; $countryA = $this->customFieldCreate(array('custom_group_id' => $groups['A']['id'], 'label' => 'Country A', 'dataType' => 'Country', 'htmlType' => 'Select Country', 'default_value' => NULL)); $countryB = $this->customFieldCreate(array('custom_group_id' => $groups['A']['id'], 'label' => 'Country B', 'dataType' => 'Country', 'htmlType' => 'Select Country', 'default_value' => NULL)); $countryC = $this->customFieldCreate(array('custom_group_id' => $groups['B']['id'], 'label' => 'Country C', 'dataType' => 'Country', 'htmlType' => 'Select Country', 'default_value' => NULL)); $fields = array('countryA' => $countryA['values'][$countryA['id']], 'countryB' => $countryB['values'][$countryB['id']], 'countryC' => $countryC['values'][$countryC['id']]); $contacts = array('alice' => $this->individualCreate(array('first_name' => 'Alice', 'last_name' => 'Albertson', 'custom_' . $fields['countryA']['id'] => $countriesByName['Andorra'], 'custom_' . $fields['countryB']['id'] => $countriesByName['Barbados'])), 'bob' => $this->individualCreate(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' => $this->individualCreate(array('first_name' => 'Carol', 'last_name' => 'Carolson', 'custom_' . $fields['countryC']['id'] => $countriesByName['Cambodia']))); // Move! CRM_Core_BAO_CustomField::moveField($fields['countryB']['id'], $groupB['id']); // Group[A] no longer has fields[countryB] $errorScope = CRM_Core_TemporaryErrorScope::useException(); try { $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['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 {$groupB['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 {$groupB['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 {$groupB['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'))); $this->customGroupDelete($groups['A']['id']); $this->customGroupDelete($groupB['id']); }