Skip to content

Commit

Permalink
Merge pull request #20918 from eileenmcnaughton/cust2
Browse files Browse the repository at this point in the history
dev/core#2709 Enable logging for custom data tables with non-standard names
  • Loading branch information
demeritcowboy authored Jul 26, 2021
2 parents f688592 + 8a475c3 commit 31efa0d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
9 changes: 9 additions & 0 deletions CRM/Logging/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ public function __construct() {
while ($dao->fetch()) {
$this->tables[] = $dao->TABLE_NAME;
}
// Get any non standard table names used for custom groups.
// include these BEFORE the hook is called.
$customFieldDAO = CRM_Core_DAO::executeQuery("
SELECT DISTINCT table_name as TABLE_NAME FROM civicrm_custom_group
where table_name NOT LIKE 'civicrm_%';
");
while ($customFieldDAO->fetch()) {
$this->tables[] = $customFieldDAO->TABLE_NAME;
}

// do not log temp import, cache, menu and log tables
$this->tables = preg_grep('/^civicrm_import_job_/', $this->tables, PREG_GREP_INVERT);
Expand Down
38 changes: 35 additions & 3 deletions tests/phpunit/CRM/Logging/LoggingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,43 @@
*/
class CRM_Logging_LoggingTest extends CiviUnitTestCase {

use CRMTraits_Custom_CustomDataTrait;

/**
* Has the db been set to multilingual.
*
* @var bool
*/
protected $isDBMultilingual = FALSE;

public function tearDown(): void {
Civi::settings()->set('logging', FALSE);
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
if ($this->isDBMultilingual) {
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
}
$logging = new CRM_Logging_Schema();
$logging->dropAllLogTables();
$this->cleanupCustomGroups();
parent::tearDown();
}

/**
* Check that log tables are created even for non standard custom fields
* tables.
*
* @throws \API_Exception
*/
public function testLoggingNonStandardCustomTableName(): void {
$this->createCustomGroupWithFieldOfType(['table_name' => 'abcd']);
Civi::settings()->set('logging', TRUE);
$this->assertNotEmpty(CRM_Core_DAO::singleValueQuery("SHOW tables LIKE 'log_abcd'"));
}

/**
* Test creating logging schema when database is in multilingual mode.
*/
public function testMultilingualLogging(): void {
CRM_Core_I18n_Schema::makeMultilingual('en_US');
$this->makeMultilingual();
Civi::settings()->set('logging', TRUE);
$value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
$this->assertNotNull($value, 'Logging not enabled successfully');
Expand All @@ -29,7 +53,7 @@ public function testMultilingualLogging(): void {
* Also test altering a multilingual table.
*/
public function testMultilingualAlterSchemaLogging(): void {
CRM_Core_I18n_Schema::makeMultilingual('en_US');
$this->makeMultilingual();
Civi::settings()->set('logging', TRUE);
$logging = new CRM_Logging_Schema();
$value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
Expand Down Expand Up @@ -67,4 +91,12 @@ public function testMultilingualAlterSchemaLogging(): void {
);
}

/**
* Convert the database to multilingual mode.
*/
protected function makeMultilingual(): void {
CRM_Core_I18n_Schema::makeMultilingual('en_US');
$this->isDBMultilingual = TRUE;
}

}
5 changes: 2 additions & 3 deletions tests/phpunit/CRMTraits/Custom/CustomDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ protected function getCustomFieldColumnName($key) {
* @param array $fieldParams
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL, $fieldParams = []): void {
public function createCustomGroupWithFieldOfType(array $groupParams = [], string $customFieldType = 'text', ?string $identifier = NULL, array $fieldParams = []): void {
$supported = ['text', 'select', 'date', 'checkbox', 'int', 'contact_reference', 'radio', 'multi_country'];
if (!in_array($customFieldType, $supported, TRUE)) {
throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
$this->fail('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
}
$groupParams['title'] = empty($groupParams['title']) ? $identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
$groupParams['name'] = $identifier ?? 'Custom Group';
Expand Down
28 changes: 18 additions & 10 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1870,16 +1870,7 @@ public function quickCleanup($tablesToTruncate, $dropCustomValueTables = FALSE)
throw new \CRM_Core_Exception("CiviUnitTestCase: quickCleanup() is not compatible with useTransaction()");
}
if ($dropCustomValueTables) {

CustomField::get(FALSE)->setSelect(['option_group_id', 'custom_group_id'])
->addChain('delete_options', OptionGroup::delete()
->addWhere('id', '=', '$option_group_id')
)
->addChain('delete_fields', CustomField::delete()
->addWhere('id', '=', '$id')
)->execute();

CustomGroup::delete(FALSE)->addWhere('id', '>', 0)->execute();
$this->cleanupCustomGroups();
// Reset autoincrement too.
$tablesToTruncate[] = 'civicrm_custom_group';
$tablesToTruncate[] = 'civicrm_custom_field';
Expand Down Expand Up @@ -3878,4 +3869,21 @@ protected function deleteNonDefaultRelationshipTypes(): void {
])->execute();
}

/**
* Delete any existing custom data groups.
*
* @throws \API_Exception
*/
protected function cleanupCustomGroups(): void {
CustomField::get(FALSE)->setSelect(['option_group_id', 'custom_group_id'])
->addChain('delete_options', OptionGroup::delete()
->addWhere('id', '=', '$option_group_id')
)
->addChain('delete_fields', CustomField::delete()
->addWhere('id', '=', '$id')
)->execute();

CustomGroup::delete(FALSE)->addWhere('id', '>', 0)->execute();
}

}

0 comments on commit 31efa0d

Please sign in to comment.