Merge pull request #19973 from JMAConsulting/core_365_1
[civicrm-core.git] / tests / phpunit / CRM / Logging / LoggingTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_DAOTest
5 * @group headless
6 */
7 class CRM_Logging_LoggingTest extends CiviUnitTestCase {
8
9 public function tearDown(): void {
10 Civi::settings()->set('logging', FALSE);
11 CRM_Core_I18n_Schema::makeSinglelingual('en_US');
12 $logging = new CRM_Logging_Schema();
13 $logging->dropAllLogTables();
14 parent::tearDown();
15 }
16
17 /**
18 * Test creating logging schema when database is in multilingual mode.
19 */
20 public function testMultilingualLogging(): void {
21 CRM_Core_I18n_Schema::makeMultilingual('en_US');
22 Civi::settings()->set('logging', TRUE);
23 $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
24 $this->assertNotNull($value, 'Logging not enabled successfully');
25 }
26
27 /**
28 * Test creating logging schema when database is in multilingual mode.
29 * Also test altering a multilingual table.
30 */
31 public function testMultilingualAlterSchemaLogging(): void {
32 CRM_Core_I18n_Schema::makeMultilingual('en_US');
33 Civi::settings()->set('logging', TRUE);
34 $logging = new CRM_Logging_Schema();
35 $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
36 $this->assertNotNull($value, 'Logging not enabled successfully');
37 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_option_value` ADD COLUMN `logging_test` INT DEFAULT '0'", [], FALSE, NULL, FALSE, FALSE);
38 CRM_Core_I18n_Schema::rebuildMultilingualSchema(['en_US']);
39 Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
40 $logging->fixSchemaDifferencesFor('civicrm_option_value');
41 Civi::service('sql_triggers')->rebuild('civicrm_option_value');
42 $query = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE);
43 $query->fetch();
44 $create = explode("\n", $query->Create_Table);
45 // MySQL may return "DEFAULT 0" or "DEFAULT '0'" depending on version
46 $this->assertTrue(
47 in_array(" `logging_test` int(11) DEFAULT '0'", $create, TRUE)
48 || in_array(' `logging_test` int(11) DEFAULT 0', $create, TRUE)
49 || in_array(" `logging_test` int DEFAULT '0'", $create, TRUE)
50 || in_array(' `logging_test` int DEFAULT 0', $create, TRUE)
51 );
52 CRM_Core_DAO::executeQuery('ALTER TABLE `civicrm_option_value` DROP COLUMN `logging_test`', [], FALSE, NULL, FALSE, FALSE);
53 $query = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE);
54 $query->fetch();
55 $domain = new CRM_Core_DAO_Domain();
56 $domain->find(TRUE);
57 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
58 Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
59 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales);
60 $logging->fixSchemaDifferencesFor('civicrm_option_value');
61 Civi::service('sql_triggers')->rebuild('civicrm_option_value');
62 $this->assertTrue(
63 in_array(" `logging_test` int(11) DEFAULT '0'", $create, TRUE)
64 || in_array(' `logging_test` int(11) DEFAULT 0', $create, TRUE)
65 || in_array(" `logging_test` int DEFAULT '0'", $create, TRUE)
66 || in_array(' `logging_test` int DEFAULT 0', $create, TRUE)
67 );
68 }
69
70 }