From b3dd4cb004d8fcd18bd58b27465e5b91c271a8e3 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Wed, 2 Sep 2020 20:36:00 -0400 Subject: [PATCH] e_warning when editing custom field --- CRM/Logging/Schema.php | 10 ++++--- tests/phpunit/CRM/Logging/SchemaTest.php | 38 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index 25b63413d2..2d1d0e3037 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -441,10 +441,12 @@ AND (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString ) // 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]); + } } } diff --git a/tests/phpunit/CRM/Logging/SchemaTest.php b/tests/phpunit/CRM/Logging/SchemaTest.php index 63269ab318..19631715e2 100644 --- a/tests/phpunit/CRM/Logging/SchemaTest.php +++ b/tests/phpunit/CRM/Logging/SchemaTest.php @@ -324,6 +324,44 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase { $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. * -- 2.25.1