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