4 * Class CRM_Core_DAOTest
7 class CRM_Logging_LoggingTest
extends CiviUnitTestCase
{
9 use CRMTraits_Custom_CustomDataTrait
;
12 * Has the db been set to multilingual.
16 protected $isDBMultilingual = FALSE;
18 public function tearDown(): void
{
19 Civi
::settings()->set('logging', FALSE);
20 if ($this->isDBMultilingual
) {
21 CRM_Core_I18n_Schema
::makeSinglelingual('en_US');
23 $logging = new CRM_Logging_Schema();
24 $logging->dropAllLogTables();
25 $this->cleanupCustomGroups();
30 * Check that log tables are created even for non standard custom fields
33 * @throws \API_Exception
35 public function testLoggingNonStandardCustomTableName(): void
{
36 $this->createCustomGroupWithFieldOfType(['table_name' => 'abcd']);
37 Civi
::settings()->set('logging', TRUE);
38 $this->assertNotEmpty(CRM_Core_DAO
::singleValueQuery("SHOW tables LIKE 'log_abcd'"));
42 * Test that hooks removing tables from logging are respected during custom field add.
44 * During custom field save logging is only handled for the affected table.
45 * We need to make sure this respects hooks to remove from the logging set.
47 public function testLoggingHookIgnore(): void
{
48 $this->hookClass
->setHook('civicrm_alterLogTables', [$this, 'ignoreSillyName']);
49 Civi
::settings()->set('logging', TRUE);
50 $this->createCustomGroupWithFieldOfType(['table_name' => 'silly_name']);
51 $this->assertEmpty(CRM_Core_DAO
::singleValueQuery("SHOW tables LIKE 'log_silly_name'"));
55 * Implement hook to cause our log table to be ignored.
57 * @param array $logTableSpec
59 public function ignoreSillyName(array &$logTableSpec): void
{
60 unset($logTableSpec['silly_name']);
64 * Test creating logging schema when database is in multilingual mode.
66 public function testMultilingualLogging(): void
{
67 $this->makeMultilingual();
68 Civi
::settings()->set('logging', TRUE);
69 $value = CRM_Core_DAO
::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
70 $this->assertNotNull($value, 'Logging not enabled successfully');
74 * Test creating logging schema when database is in multilingual mode.
75 * Also test altering a multilingual table.
77 public function testMultilingualAlterSchemaLogging(): void
{
78 $this->makeMultilingual();
79 Civi
::settings()->set('logging', TRUE);
80 $logging = new CRM_Logging_Schema();
81 $value = CRM_Core_DAO
::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
82 $this->assertNotNull($value, 'Logging not enabled successfully');
83 CRM_Core_DAO
::executeQuery("ALTER TABLE `civicrm_option_value` ADD COLUMN `logging_test` INT DEFAULT '0'", [], FALSE, NULL, FALSE, FALSE);
84 CRM_Core_I18n_Schema
::rebuildMultilingualSchema(['en_US']);
85 Civi
::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
86 $logging->fixSchemaDifferencesFor('civicrm_option_value');
87 Civi
::service('sql_triggers')->rebuild('civicrm_option_value');
88 $query = CRM_Core_DAO
::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE);
90 $create = explode("\n", $query->Create_Table
);
91 // MySQL may return "DEFAULT 0" or "DEFAULT '0'" depending on version
93 in_array(" `logging_test` int(11) DEFAULT '0'", $create, TRUE)
94 ||
in_array(' `logging_test` int(11) DEFAULT 0', $create, TRUE)
95 ||
in_array(" `logging_test` int DEFAULT '0'", $create, TRUE)
96 ||
in_array(' `logging_test` int DEFAULT 0', $create, TRUE)
98 CRM_Core_DAO
::executeQuery('ALTER TABLE `civicrm_option_value` DROP COLUMN `logging_test`', [], FALSE, NULL, FALSE, FALSE);
99 $query = CRM_Core_DAO
::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE);
101 $domain = new CRM_Core_DAO_Domain();
103 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
104 Civi
::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
105 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales);
106 $logging->fixSchemaDifferencesFor('civicrm_option_value');
107 Civi
::service('sql_triggers')->rebuild('civicrm_option_value');
109 in_array(" `logging_test` int(11) DEFAULT '0'", $create, TRUE)
110 ||
in_array(' `logging_test` int(11) DEFAULT 0', $create, TRUE)
111 ||
in_array(" `logging_test` int DEFAULT '0'", $create, TRUE)
112 ||
in_array(' `logging_test` int DEFAULT 0', $create, TRUE)
117 * Convert the database to multilingual mode.
119 protected function makeMultilingual(): void
{
120 CRM_Core_I18n_Schema
::makeMultilingual('en_US');
121 $this->isDBMultilingual
= TRUE;