Merge pull request #20482 from MegaphoneJon/translation-69
[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 use CRMTraits_Custom_CustomDataTrait;
10
11 /**
12 * Has the db been set to multilingual.
13 *
14 * @var bool
15 */
16 protected $isDBMultilingual = FALSE;
17
18 public function tearDown(): void {
19 Civi::settings()->set('logging', FALSE);
20 if ($this->isDBMultilingual) {
21 CRM_Core_I18n_Schema::makeSinglelingual('en_US');
22 }
23 $logging = new CRM_Logging_Schema();
24 $logging->dropAllLogTables();
25 $this->cleanupCustomGroups();
26 parent::tearDown();
27 }
28
29 /**
30 * Check that log tables are created even for non standard custom fields
31 * tables.
32 *
33 * @throws \API_Exception
34 */
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'"));
39 }
40
41 /**
42 * Test creating logging schema when database is in multilingual mode.
43 */
44 public function testMultilingualLogging(): void {
45 $this->makeMultilingual();
46 Civi::settings()->set('logging', TRUE);
47 $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
48 $this->assertNotNull($value, 'Logging not enabled successfully');
49 }
50
51 /**
52 * Test creating logging schema when database is in multilingual mode.
53 * Also test altering a multilingual table.
54 */
55 public function testMultilingualAlterSchemaLogging(): void {
56 $this->makeMultilingual();
57 Civi::settings()->set('logging', TRUE);
58 $logging = new CRM_Logging_Schema();
59 $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
60 $this->assertNotNull($value, 'Logging not enabled successfully');
61 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_option_value` ADD COLUMN `logging_test` INT DEFAULT '0'", [], FALSE, NULL, FALSE, FALSE);
62 CRM_Core_I18n_Schema::rebuildMultilingualSchema(['en_US']);
63 Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
64 $logging->fixSchemaDifferencesFor('civicrm_option_value');
65 Civi::service('sql_triggers')->rebuild('civicrm_option_value');
66 $query = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE);
67 $query->fetch();
68 $create = explode("\n", $query->Create_Table);
69 // MySQL may return "DEFAULT 0" or "DEFAULT '0'" depending on version
70 $this->assertTrue(
71 in_array(" `logging_test` int(11) DEFAULT '0'", $create, TRUE)
72 || in_array(' `logging_test` int(11) DEFAULT 0', $create, TRUE)
73 || in_array(" `logging_test` int DEFAULT '0'", $create, TRUE)
74 || in_array(' `logging_test` int DEFAULT 0', $create, TRUE)
75 );
76 CRM_Core_DAO::executeQuery('ALTER TABLE `civicrm_option_value` DROP COLUMN `logging_test`', [], FALSE, NULL, FALSE, FALSE);
77 $query = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE);
78 $query->fetch();
79 $domain = new CRM_Core_DAO_Domain();
80 $domain->find(TRUE);
81 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
82 Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
83 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales);
84 $logging->fixSchemaDifferencesFor('civicrm_option_value');
85 Civi::service('sql_triggers')->rebuild('civicrm_option_value');
86 $this->assertTrue(
87 in_array(" `logging_test` int(11) DEFAULT '0'", $create, TRUE)
88 || in_array(' `logging_test` int(11) DEFAULT 0', $create, TRUE)
89 || in_array(" `logging_test` int DEFAULT '0'", $create, TRUE)
90 || in_array(' `logging_test` int DEFAULT 0', $create, TRUE)
91 );
92 }
93
94 /**
95 * Convert the database to multilingual mode.
96 */
97 protected function makeMultilingual(): void {
98 CRM_Core_I18n_Schema::makeMultilingual('en_US');
99 $this->isDBMultilingual = TRUE;
100 }
101
102 }