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