Skip to content

Commit

Permalink
Merge pull request #18386 from demeritcowboy/logging-customfield-notice
Browse files Browse the repository at this point in the history
dev/core#1989 - E_WARNING when editing custom field with logging turned on
  • Loading branch information
seamuslee001 authored Sep 6, 2020
2 parents 805feda + b3dd4cb commit 4282009
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
10 changes: 6 additions & 4 deletions CRM/Logging/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,12 @@ public function fixSchemaDifferencesFor($table, $cols = []) {
// should treat it as a modification.
$this->resetSchemaCacheForTable("log_$table");
$logTableSchema = $this->columnSpecsOf("log_$table");
foreach ($cols['ADD'] as $colKey => $col) {
if (array_key_exists($col, $logTableSchema)) {
$cols['MODIFY'][] = $col;
unset($cols['ADD'][$colKey]);
if (!empty($cols['ADD'])) {
foreach ($cols['ADD'] as $colKey => $col) {
if (array_key_exists($col, $logTableSchema)) {
$cols['MODIFY'][] = $col;
unset($cols['ADD'][$colKey]);
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/CRM/Logging/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,44 @@ public function testEnumChange() {
$this->assertEquals("'A','B','C','D'", $ci['civicrm_test_enum_change']['test_enum']['ENUM_VALUES']);
}

/**
* Test editing a custom field
*/
public function testCustomFieldEdit() {
$schema = new CRM_Logging_Schema();
$schema->enableLogging();
$customGroup = $this->entityCustomGroupWithSingleFieldCreate('Contact', 'ContactTest.php');

// get the custom group table name
$params = ['id' => $customGroup['custom_group_id']];
$custom_group = $this->callAPISuccess('custom_group', 'getsingle', $params);

// get the field db column name
$params = ['id' => $customGroup['custom_field_id']];
$custom_field = $this->callAPISuccess('custom_field', 'getsingle', $params);

// check it
$dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE `log_{$custom_group['table_name']}`");
$dao->fetch();
$this->assertStringContainsString("`{$custom_field['column_name']}` varchar(255)", $dao->Create_Table);

// Edit the field
$params = [
'id' => $customGroup['custom_field_id'],
'label' => 'Label changed',
'text_length' => 768,
];
$this->callAPISuccess('custom_field', 'create', $params);

// update logging schema
$schema->fixSchemaDifferences();

// verify
$dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE `log_{$custom_group['table_name']}`");
$dao->fetch();
$this->assertStringContainsString("`{$custom_field['column_name']}` varchar(768)", $dao->Create_Table);
}

/**
* Determine if we are running on MySQL 8 version 8.0.19 or later.
*
Expand Down

0 comments on commit 4282009

Please sign in to comment.