Skip to content

Commit

Permalink
dev/core#2709 Enable logging for custom data tables with non-standard…
Browse files Browse the repository at this point in the history
… names
  • Loading branch information
eileenmcnaughton committed Jul 21, 2021
1 parent 930539a commit 3c792af
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CRM/Logging/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public function __construct() {
WHERE TABLE_SCHEMA = '{$civiDBName}'
AND TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME LIKE 'civicrm_%'
-- Get any non standard table names used for custom groups.
-- include these BEFORE the hook is called.
UNION SELECT DISTINCT table_name FROM civicrm_custom_group
");
while ($dao->fetch()) {
$this->tables[] = $dao->TABLE_NAME;
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 @@ -3880,4 +3871,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 3c792af

Please sign in to comment.