From 9c3c84d12d479d94fe43d79b004c479ee882829d Mon Sep 17 00:00:00 2001 From: Patrick Figel Date: Fri, 22 Feb 2019 20:38:30 +0100 Subject: [PATCH] CRM/Logging - Fix log table exceptions This fixes a bug that caused columns that were excluded from being considered "log-worthy" changes to be logged anyway. That happened because columns were extracted with backticks but compared to strings without backticks. To preserve compatibility with exceptions set by alterLogTables which could contain backticks, the comparison is performed against the column name with and without backticks. --- CRM/Logging/Schema.php | 6 +++++- tests/phpunit/CRM/Logging/SchemaTest.php | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index daf6df9a05..1e259ed0e7 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -858,7 +858,11 @@ COLS; foreach ($columns as $column) { $tableExceptions = array_key_exists('exceptions', $this->logTableSpec[$table]) ? $this->logTableSpec[$table]['exceptions'] : array(); // ignore modified_date changes - if ($column != 'modified_date' && !in_array($column, $tableExceptions)) { + $tableExceptions[] = 'modified_date'; + // exceptions may be provided with or without backticks + $excludeColumn = in_array($column, $tableExceptions) || + in_array(str_replace('`', '', $column), $tableExceptions); + if (!$excludeColumn) { $cond[] = "IFNULL(OLD.$column,'') <> IFNULL(NEW.$column,'')"; } } diff --git a/tests/phpunit/CRM/Logging/SchemaTest.php b/tests/phpunit/CRM/Logging/SchemaTest.php index 2b238077e1..948c61704b 100644 --- a/tests/phpunit/CRM/Logging/SchemaTest.php +++ b/tests/phpunit/CRM/Logging/SchemaTest.php @@ -100,4 +100,27 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase { $this->assertTrue(empty($diffs['OBSOLETE'])); } + /** + * Test logging trigger definition + */ + public function testTriggerInfo() { + $info = []; + $schema = new CRM_Logging_Schema(); + $schema->enableLogging(); + $schema->triggerInfo($info, 'civicrm_group'); + // should have 3 triggers (insert/update/delete) + $this->assertCount(3, $info); + foreach ($info as $trigger) { + // table for trigger should be civicrm_group + $this->assertEquals('civicrm_group', $trigger['table'][0]); + if ($trigger['event'][0] == 'UPDATE') { + // civicrm_group.cache_date should be an exception, i.e. not logged + $this->assertNotContains( + "IFNULL(OLD.`cache_date`,'') <> IFNULL(NEW.`cache_date`,'')", + $trigger['sql'] + ); + } + } + } + } -- 2.25.1