Commit | Line | Data |
---|---|---|
15a68d41 SL |
1 | <?php |
2 | ||
3 | /** | |
4 | * Class CRM_Core_DAOTest | |
5 | * @group headless | |
6 | */ | |
7 | class CRM_Logging_LoggingTest extends CiviUnitTestCase { | |
8 | ||
8a475c3d EM |
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 | ||
dd09ee0c | 18 | public function tearDown(): void { |
4b208509 | 19 | Civi::settings()->set('logging', FALSE); |
8a475c3d EM |
20 | if ($this->isDBMultilingual) { |
21 | CRM_Core_I18n_Schema::makeSinglelingual('en_US'); | |
22 | } | |
15a68d41 SL |
23 | $logging = new CRM_Logging_Schema(); |
24 | $logging->dropAllLogTables(); | |
8a475c3d | 25 | $this->cleanupCustomGroups(); |
15a68d41 SL |
26 | parent::tearDown(); |
27 | } | |
28 | ||
8a475c3d EM |
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 | ||
15a68d41 SL |
41 | /** |
42 | * Test creating logging schema when database is in multilingual mode. | |
43 | */ | |
4b208509 | 44 | public function testMultilingualLogging(): void { |
8a475c3d | 45 | $this->makeMultilingual(); |
4b208509 EM |
46 | Civi::settings()->set('logging', TRUE); |
47 | $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE); | |
15a68d41 | 48 | $this->assertNotNull($value, 'Logging not enabled successfully'); |
15a68d41 SL |
49 | } |
50 | ||
15a68d41 SL |
51 | /** |
52 | * Test creating logging schema when database is in multilingual mode. | |
53 | * Also test altering a multilingual table. | |
54 | */ | |
4b208509 | 55 | public function testMultilingualAlterSchemaLogging(): void { |
8a475c3d | 56 | $this->makeMultilingual(); |
4b208509 | 57 | Civi::settings()->set('logging', TRUE); |
15a68d41 | 58 | $logging = new CRM_Logging_Schema(); |
4b208509 | 59 | $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE); |
15a68d41 | 60 | $this->assertNotNull($value, 'Logging not enabled successfully'); |
9099cab3 CW |
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']); | |
4b208509 | 63 | Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = []; |
fcc20cec | 64 | $logging->fixSchemaDifferencesFor('civicrm_option_value'); |
65 | Civi::service('sql_triggers')->rebuild('civicrm_option_value'); | |
4b208509 | 66 | $query = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE); |
15a68d41 | 67 | $query->fetch(); |
14e47fc1 | 68 | $create = explode("\n", $query->Create_Table); |
204aa6fb PF |
69 | // MySQL may return "DEFAULT 0" or "DEFAULT '0'" depending on version |
70 | $this->assertTrue( | |
4b208509 EM |
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) | |
204aa6fb | 75 | ); |
4b208509 EM |
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); | |
14e47fc1 SL |
78 | $query->fetch(); |
79 | $domain = new CRM_Core_DAO_Domain(); | |
80 | $domain->find(TRUE); | |
81 | $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); | |
4b208509 | 82 | Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = []; |
14e47fc1 | 83 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales); |
fcc20cec | 84 | $logging->fixSchemaDifferencesFor('civicrm_option_value'); |
85 | Civi::service('sql_triggers')->rebuild('civicrm_option_value'); | |
204aa6fb | 86 | $this->assertTrue( |
4b208509 EM |
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) | |
4f4ed4cf | 91 | ); |
15a68d41 SL |
92 | } |
93 | ||
8a475c3d EM |
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 | ||
15a68d41 | 102 | } |