From 8b3b9a2eacc3fb7f9f0a61230423a8b037681bad Mon Sep 17 00:00:00 2001 From: Adam Roses Wight Date: Tue, 23 Apr 2013 17:47:54 -0700 Subject: [PATCH] CRM-10592: custom group and field names can be specified explicitly. --- CRM/Core/BAO/CustomField.php | 11 ++++-- CRM/Core/BAO/CustomGroup.php | 19 ++++++--- .../phpunit/CRM/Core/BAO/CustomFieldTest.php | 32 +++++++++++++-- .../phpunit/CRM/Core/BAO/CustomGroupTest.php | 39 +++++++++++++++++++ 4 files changed, 88 insertions(+), 13 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 09dfa9a9fe..0827142dd8 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -132,12 +132,12 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { static function create(&$params) { if (!isset($params['id']) && !isset($params['column_name'])) { // if add mode & column_name not present, calculate it. - $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32)); + $columnName = strtolower(CRM_Utils_String::munge($params['label'], '_', 32)); $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64); } elseif (isset($params['id'])) { - $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', + $columnName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name' ); @@ -192,7 +192,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { if ($params['option_type'] == 1) { // first create an option group for this custom group $optionGroup = new CRM_Core_DAO_OptionGroup(); - $optionGroup->name = "{$params['column_name']}_" . date('YmdHis'); + $optionGroup->name = "{$columnName}_" . date('YmdHis'); $optionGroup->title = $params['label']; $optionGroup->is_active = 1; $optionGroup->save(); @@ -275,7 +275,10 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { self::createField($customField, 'modify', $indexExist); } else { - $customField->column_name .= "_{$customField->id}"; + if (!isset($params['column_name'])) { + $columnName .= "_{$customField->id}"; + } + $customField->column_name = $columnName; $customField->save(); // make sure all values are present in the object $customField->find(TRUE); diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index 11b30806f0..a3b2b26756 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -150,18 +150,25 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup { $group->name = CRM_Utils_String::munge($group->title, '_', 64); } - // lets create the table associated with the group and save it - $tableName = $group->table_name = "civicrm_value_" . strtolower($group->name); + if (isset($params['table_name'])) { + $tableName = $params['table_name']; + + if (CRM_Core_DAO_AllCoreTables::isCoreTable($tableName)) { + // Bad idea. Prevent group creation because it might lead to a broken configuration. + CRM_Core_Error::fatal(ts("Cannot create custom table because %1 is already a core table.", array('1' => $tableName))); + } + } } // enclose the below in a transaction $transaction = new CRM_Core_Transaction(); $group->save(); - if ($tableName) { - // now append group id to table name, this prevent any name conflicts - // like CRM-2742 - $tableName .= "_{$group->id}"; + if (!isset($params['id'])) { + if (!isset($params['table_name'])) { + $munged_title = strtolower(CRM_Utils_String::munge($group->title, '_', 32)); + $tableName = "civicrm_value_{$munged_title}_{$group->id}"; + } $group->table_name = $tableName; CRM_Core_DAO::setFieldValue('CRM_Core_DAO_CustomGroup', $group->id, diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index 24192a9f2a..9359e30843 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -40,6 +40,31 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.'); $this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.'); + $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.'); + $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.'); + $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName, + "Column name ends in ID"); + + Custom::deleteGroup($customGroup); + } + + function testCreateCustomfieldColumnName() { + $customGroup = Custom::createGroup(array(), 'Individual'); + $fields = array( + 'label' => 'testFld 2', + 'column_name' => 'special_colname', + 'data_type' => 'String', + 'html_type' => 'Text', + 'custom_group_id' => $customGroup->id, + ); + $customField = CRM_Core_BAO_CustomField::create($fields); + $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', + 'Database check for created CustomField.' + ); + $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.'); + $this->assertEquals($fields['column_name'], $dbColumnName, + "Column name set as specified"); + Custom::deleteGroup($customGroup); } @@ -107,9 +132,10 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { function testDeleteCustomfield() { $customGroup = Custom::createGroup(array(), 'Individual'); $fields = array( - 'groupId' => $customGroup->id, - 'dataType' => 'Memo', - 'htmlType' => 'TextArea', + 'label' => 'Throwaway Field', + 'custom_group_id' => $customGroup->id, + 'data_type' => 'Memo', + 'html_type' => 'TextArea', ); $customField = Custom::createField(array(), $fields); diff --git a/tests/phpunit/CRM/Core/BAO/CustomGroupTest.php b/tests/phpunit/CRM/Core/BAO/CustomGroupTest.php index 7dc71e76e2..ebf70eda22 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomGroupTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomGroupTest.php @@ -475,6 +475,45 @@ class CRM_Core_BAO_CustomGroupTest extends CiviUnitTestCase { 'Database check for custom group record.' ); $this->assertEquals($params['title'], $dbCustomGroupTitle); + + $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id', + 'Database check for custom group record.' + ); + $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName, + "The table name should be suffixed with '_ID' unless specified."); + + Custom::deleteGroup($customGroup); + } + + /** + * Function to test create() given a table_name + */ + function testCreateTableName() { + $params = array( + 'title' => 'Test_Group_2', + 'name' => 'test_group_2', + 'table_name' => 'test_otherTableName', + 'extends' => array(0 => 'Individual', 1 => array()), + '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, + 'version' => 3, + ); + $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); + + $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id', + 'Database check for custom group record.' + ); + $this->assertEquals($params['table_name'], $dbCustomGroupTableName); + Custom::deleteGroup($customGroup); } -- 2.25.1